diff --git a/src/clj_test_containers/core.clj b/src/clj_test_containers/core.clj index 7572786..891dbaa 100644 --- a/src/clj_test_containers/core.clj +++ b/src/clj_test_containers/core.clj @@ -5,37 +5,17 @@ org.testcontainers.containers.BindMode/READ_WRITE org.testcontainers.containers.BindMode/READ_ONLY)) -(defn- configure-volume [container {classpath-resource-mapping :classpath-resource-mapping - file-system-bind :file-system-bind}] - - (if (some? classpath-resource-mapping) - (let [{resource-path :resource-path - container-path :container-path - mode :mode} classpath-resource-mapping] - (.withClasspathResourceMapping container - resource-path - container-path - (resolve-bind-mode mode)))) - - (if (some? file-system-bind) - (let [{host-path :host-path - container-path :container-path - mode :mode} file-system-bind] - (.withFileSystemBind container - host-path - container-path)))) - (defn create "Sets the properties for a testcontainer instance" [{image-name :image-name exposed-ports :exposed-ports env-vars :env-vars - command :command - volume :volume}] + command :command}] (let [container (org.testcontainers.containers.GenericContainer. image-name)] (.setExposedPorts container (map int exposed-ports)) + (if (some? env-vars) (doseq [pair env-vars] (.addEnv container (first pair) (second pair)))) @@ -43,14 +23,50 @@ (if (some? command) (.setCommand container command)) - (if (some? volume) - (configure-volume container volume)) - {:container container :exposed-ports (.getExposedPorts container) :env-vars (.getEnvMap container) :host (.getHost container)})) + +(defn configure-volume [container-config + {classpath-resource-mapping :classpath-resource-mapping + file-system-bind :file-system-bind}] + + (when-let [{resource-path :resource-path + container-path :container-path + mode :mode} classpath-resource-mapping] + (.withClasspathResourceMapping (:container container-config) + resource-path + container-path + (resolve-bind-mode mode))) + + + (when-let [{host-path :host-path + container-path :container-path + mode :mode} file-system-bind] + (.withFileSystemBind (:container container-config) + host-path + container-path)) + + container-config) + +(defn copy-file-to-container + "Copies a file into the running container" + [container-conf {container-path :container-path + path :path + type :type}] + + (let [mountable-file (cond + (= :classpath-resource type) (org.testcontainers.utility.MountableFile/forClasspathResource path) + (= :host-path type) (org.testcontainers.utility.MountableFile/forHostPath path) + :else :error)] + (assoc container-conf + :container + (.withCopyFileToContainer (:container container-conf) + mountable-file + container-path)))) + (defn start "Starts the underlying testcontainer instance and adds new values to the response map, e.g. :id and :first-mapped-port" [container-conf] @@ -69,3 +85,5 @@ (-> container-conf (dissoc :id) (dissoc :mapped-ports))) + + diff --git a/test/clj_test_containers/core_test.clj b/test/clj_test_containers/core_test.clj index 7b3288d..977437f 100644 --- a/test/clj_test_containers/core_test.clj +++ b/test/clj_test_containers/core_test.clj @@ -18,12 +18,12 @@ (deftest init-with-volume-test (testing "Testing mapping of a classpath resource" - (let [container (create {:image-name "postgres:12.2" - :exposed-ports [5432] - :env-vars {"POSTGRES_PASSWORD" "pw"} - :volume {:classpath-resource-mapping {:resource-path "test.sql" - :container-path "/opt/test.sql" - :mode :read-only}}}) + (let [container (-> (create {:image-name "postgres:12.2" + :exposed-ports [5432] + :env-vars {"POSTGRES_PASSWORD" "pw"}}) + (configure-volume {:classpath-resource-mapping {:resource-path "test.sql" + :container-path "/opt/test.sql" + :mode :read-only}})) initialized-container (start container) stopped-container (stop container)] (is (some? (:id initialized-container))) @@ -33,12 +33,42 @@ (is (nil? (:mapped-ports stopped-container))))) (testing "Testing mapping of a filesystem-binding" - (let [container (create {:image-name "postgres:12.2" - :exposed-ports [5432] - :env-vars {"POSTGRES_PASSWORD" "pw"} - :volume {:file-system-bind {:host-path "/tmp" - :container-path "/opt" - :mode :read-only}}}) + (let [container (-> (create {:image-name "postgres:12.2" + :exposed-ports [5432] + :env-vars {"POSTGRES_PASSWORD" "pw"}}) + (configure-volume {:file-system-bind {:host-path "/tmp" + :container-path "/opt" + :mode :read-only}})) + initialized-container (start container) + stopped-container (stop container)] + (is (some? (:id initialized-container))) + (is (some? (:mapped-ports initialized-container))) + (is (some? (get (:mapped-ports initialized-container) 5432))) + (is (nil? (:id stopped-container))) + (is (nil? (:mapped-ports stopped-container))))) + + (testing "Copying a file from the host into the container" + (let [container (-> (create {:image-name "postgres:12.2" + :exposed-ports [5432] + :env-vars {"POSTGRES_PASSWORD" "pw"}}) + (copy-file-to-container {:path "test.sql" + :container-path "/opt" + :type :host-path})) + initialized-container (start container) + stopped-container (stop container)] + (is (some? (:id initialized-container))) + (is (some? (:mapped-ports initialized-container))) + (is (some? (get (:mapped-ports initialized-container) 5432))) + (is (nil? (:id stopped-container))) + (is (nil? (:mapped-ports stopped-container))))) + + (testing "Copying a file from the classpath into the container" + (let [container (-> (create {:image-name "postgres:12.2" + :exposed-ports [5432] + :env-vars {"POSTGRES_PASSWORD" "pw"}}) + (copy-file-to-container {:path "test.sql" + :container-path "/opt" + :type :classpath-resource})) initialized-container (start container) stopped-container (stop container)] (is (some? (:id initialized-container)))