Use .copyFileToContainer when the container is already started (#44)

This commit is contained in:
Rob Hanlon 2020-11-26 03:19:17 -08:00 committed by GitHub
parent f7befdf86d
commit 43d573e6f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 11 deletions

View file

@ -1,3 +1,5 @@
{:file-pattern #"\.(clj[sc]?|edn|cljstyle)$" {:files {:pattern #"\.(clj[sc]?|edn|cljstyle)$"},
:list-indent-size 1 :rules
:padding-lines 1} {:indentation {:list-indent 1},
:blank-lines {:padding-lines 1},
:namespaces {:indent-size 1}}}

1
.gitignore vendored
View file

@ -12,3 +12,4 @@ pom.xml.asc
.hg/ .hg/
.DS_Store .DS_Store
.lsp .lsp
.cpcache

View file

@ -19,7 +19,7 @@
[lambdaisland/kaocha-cloverage "1.0-45"] [lambdaisland/kaocha-cloverage "1.0-45"]
[lambdaisland/kaocha-junit-xml "0.0.76"] [lambdaisland/kaocha-junit-xml "0.0.76"]
[lambdaisland/kaocha-junit-xml "0.0.76"] [lambdaisland/kaocha-junit-xml "0.0.76"]
[mvxcvi/cljstyle "0.13.0" :exclusions [org.clojure/clojure]] [mvxcvi/cljstyle "0.14.0" :exclusions [org.clojure/clojure]]
[org.clojure/test.check "1.1.0"] [org.clojure/test.check "1.1.0"]
[org.clojure/tools.namespace "1.0.0"] [org.clojure/tools.namespace "1.0.0"]
[org.testcontainers/postgresql "1.15.0-rc2"]] [org.testcontainers/postgresql "1.15.0-rc2"]]

View file

@ -195,18 +195,24 @@
(resolve-bind-mode mode)))) (resolve-bind-mode mode))))
(defn copy-file-to-container! (defn copy-file-to-container!
"Copies a file into the running container" "If a container is not yet started, adds a mapping from mountable file to
[{:keys [^GenericContainer container] :as container-config} container path that will be copied to the container on startup. If the
container is already running, copy the file to the running container."
[{:keys [^GenericContainer container id] :as container-config}
{:keys [^String container-path ^String path type]}] {:keys [^String container-path ^String path type]}]
(let [^MountableFile mountable-file (let [^MountableFile mountable-file
(case type (case type
:classpath-resource (MountableFile/forClasspathResource path) :classpath-resource (MountableFile/forClasspathResource path)
:host-path (MountableFile/forHostPath path))] :host-path (MountableFile/forHostPath path))]
(assoc container-config (if id
:container (do
(.withCopyFileToContainer container (.copyFileToContainer container mountable-file container-path)
mountable-file container-config)
container-path)))) (assoc container-config
:container
(.withCopyFileToContainer container
mountable-file
container-path)))))
(defn execute-command! (defn execute-command!
"Executes a command in the container, and returns the result" "Executes a command in the container, and returns the result"

View file

@ -126,6 +126,7 @@
(is (nil? (:id stopped-container))) (is (nil? (:id stopped-container)))
(is (nil? (:mapped-ports stopped-container))))) (is (nil? (:mapped-ports stopped-container)))))
(testing "Copying a file from the classpath into the container" (testing "Copying a file from the classpath into the container"
(let [container (-> (sut/create {:image-name "postgres:12.2" (let [container (-> (sut/create {:image-name "postgres:12.2"
:exposed-ports [5432] :exposed-ports [5432]
@ -141,6 +142,44 @@
(is (some? (get (:mapped-ports initialized-container) 5432))) (is (some? (get (:mapped-ports initialized-container) 5432)))
(is (= 0 (:exit-code file-check))) (is (= 0 (:exit-code file-check)))
(is (nil? (:id stopped-container))) (is (nil? (:id stopped-container)))
(is (nil? (:mapped-ports stopped-container)))))
(testing "Copying a file from the host into a running container"
(let [container (sut/create {:image-name "postgres:12.2"
:exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}})
initialized-container (sut/start! container)
_ (sut/copy-file-to-container! initialized-container
{:path "test.sql"
:container-path "/opt/test.sql"
:type :host-path})
file-check (sut/execute-command! initialized-container
["tail" "/opt/test.sql"])
stopped-container (sut/stop! container)]
(is (some? (:id initialized-container)))
(is (some? (:mapped-ports initialized-container)))
(is (some? (get (:mapped-ports initialized-container) 5432)))
(is (= 0 (:exit-code file-check)))
(is (nil? (:id stopped-container)))
(is (nil? (:mapped-ports stopped-container)))))
(testing "Copying a file from the classpath into a running container"
(let [container (sut/create {:image-name "postgres:12.2"
:exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}})
initialized-container (sut/start! container)
_ (sut/copy-file-to-container! initialized-container
{:path "test.sql"
:container-path "/opt/test.sql"
:type :classpath-resource})
file-check (sut/execute-command! initialized-container
["tail" "/opt/test.sql"])
stopped-container (sut/stop! container)]
(is (some? (:id initialized-container)))
(is (some? (:mapped-ports initialized-container)))
(is (some? (get (:mapped-ports initialized-container) 5432)))
(is (= 0 (:exit-code file-check)))
(is (nil? (:id stopped-container)))
(is (nil? (:mapped-ports stopped-container)))))) (is (nil? (:mapped-ports stopped-container))))))
(deftest networking-test (deftest networking-test