diff --git a/src/clj_test_containers/core.clj b/src/clj_test_containers/core.clj index 7858ea2..7572786 100644 --- a/src/clj_test_containers/core.clj +++ b/src/clj_test_containers/core.clj @@ -1,21 +1,51 @@ -(ns clj-test-containers.core) +5(ns clj-test-containers.core) + +(defn- resolve-bind-mode [bind-mode] + (if (= :read-write bind-mode) + 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}] + command :command + volume :volume}] (let [container (org.testcontainers.containers.GenericContainer. image-name)] (.setExposedPorts container (map int exposed-ports)) - (doseq [pair env-vars] - (.addEnv container (first pair) (second pair))) + (if (some? env-vars) + (doseq [pair env-vars] + (.addEnv container (first pair) (second pair)))) (if (some? command) (.setCommand container command)) + (if (some? volume) + (configure-volume container volume)) + {:container container :exposed-ports (.getExposedPorts container) :env-vars (.getEnvMap container) diff --git a/test/clj_test_containers/core_test.clj b/test/clj_test_containers/core_test.clj index 2ff3bd1..7b3288d 100644 --- a/test/clj_test_containers/core_test.clj +++ b/test/clj_test_containers/core_test.clj @@ -14,3 +14,35 @@ (is (some? (get (:mapped-ports initialized-container) 5432))) (is (nil? (:id stopped-container))) (is (nil? (:mapped-ports stopped-container)))))) + +(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}}}) + 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 "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}}}) + 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))))))