From 43d573e6f51fe017aabf8c96da241176fb7b97c7 Mon Sep 17 00:00:00 2001 From: Rob Hanlon Date: Thu, 26 Nov 2020 03:19:17 -0800 Subject: [PATCH] Use .copyFileToContainer when the container is already started (#44) --- .cljstyle | 8 ++++-- .gitignore | 1 + project.clj | 2 +- src/clj_test_containers/core.clj | 20 ++++++++----- test/clj_test_containers/core_test.clj | 39 ++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 11 deletions(-) diff --git a/.cljstyle b/.cljstyle index 47bb398..d45284a 100644 --- a/.cljstyle +++ b/.cljstyle @@ -1,3 +1,5 @@ -{:file-pattern #"\.(clj[sc]?|edn|cljstyle)$" - :list-indent-size 1 - :padding-lines 1} +{:files {:pattern #"\.(clj[sc]?|edn|cljstyle)$"}, + :rules + {:indentation {:list-indent 1}, + :blank-lines {:padding-lines 1}, + :namespaces {:indent-size 1}}} diff --git a/.gitignore b/.gitignore index b2809f3..ccfbd01 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ pom.xml.asc .hg/ .DS_Store .lsp +.cpcache diff --git a/project.clj b/project.clj index 56b88ff..08255b0 100644 --- a/project.clj +++ b/project.clj @@ -19,7 +19,7 @@ [lambdaisland/kaocha-cloverage "1.0-45"] [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/tools.namespace "1.0.0"] [org.testcontainers/postgresql "1.15.0-rc2"]] diff --git a/src/clj_test_containers/core.clj b/src/clj_test_containers/core.clj index 22f7652..b39152c 100644 --- a/src/clj_test_containers/core.clj +++ b/src/clj_test_containers/core.clj @@ -195,18 +195,24 @@ (resolve-bind-mode mode)))) (defn copy-file-to-container! - "Copies a file into the running container" - [{:keys [^GenericContainer container] :as container-config} + "If a container is not yet started, adds a mapping from mountable file to + 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]}] (let [^MountableFile mountable-file (case type :classpath-resource (MountableFile/forClasspathResource path) :host-path (MountableFile/forHostPath path))] - (assoc container-config - :container - (.withCopyFileToContainer container - mountable-file - container-path)))) + (if id + (do + (.copyFileToContainer container mountable-file container-path) + container-config) + (assoc container-config + :container + (.withCopyFileToContainer container + mountable-file + container-path))))) (defn execute-command! "Executes a command in the container, and returns the result" diff --git a/test/clj_test_containers/core_test.clj b/test/clj_test_containers/core_test.clj index aab841e..cda1674 100644 --- a/test/clj_test_containers/core_test.clj +++ b/test/clj_test_containers/core_test.clj @@ -126,6 +126,7 @@ (is (nil? (:id stopped-container))) (is (nil? (:mapped-ports stopped-container))))) + (testing "Copying a file from the classpath into the container" (let [container (-> (sut/create {:image-name "postgres:12.2" :exposed-ports [5432] @@ -141,6 +142,44 @@ (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 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)))))) (deftest networking-test