From d9e9b9724d15b0a15eadf69de197ecedff6af9ca Mon Sep 17 00:00:00 2001 From: Rob Hanlon Date: Tue, 4 Aug 2020 10:58:12 -0700 Subject: [PATCH] Extract function --- src/clj_test_containers/core.clj | 30 +++++++++++++++----------- test/clj_test_containers/core_test.clj | 8 +++---- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/clj_test_containers/core.clj b/src/clj_test_containers/core.clj index 14b2075..da2d7f5 100644 --- a/src/clj_test_containers/core.clj +++ b/src/clj_test_containers/core.clj @@ -11,23 +11,27 @@ BindMode/READ_WRITE BindMode/READ_ONLY)) -(defn create +(defn init "Sets the properties for a testcontainer instance" - [{:keys [container image-name exposed-ports env-vars command]}] - (let [container (or container (GenericContainer. image-name))] - (.setExposedPorts container (map int exposed-ports)) + [{:keys [container exposed-ports env-vars command]}] + (.setExposedPorts container (map int exposed-ports)) - (if (some? env-vars) - (doseq [pair env-vars] - (.addEnv container (first pair) (second pair)))) + (when (some? env-vars) + (doseq [pair env-vars] + (.addEnv container (first pair) (second pair)))) - (if (some? command) - (.setCommand container command)) + (when (some? command) + (.setCommand container command)) - {:container container - :exposed-ports (.getExposedPorts container) - :env-vars (.getEnvMap container) - :host (.getHost container)})) + {:container container + :exposed-ports (.getExposedPorts container) + :env-vars (.getEnvMap container) + :host (.getHost container)}) + +(defn create + "Creates a generic testcontainer and sets its properties" + [{:keys [image-name] :as options}] + (init (assoc options :container (GenericContainer. image-name)))) (defn map-classpath-resource! "Maps a resource in the classpath to the given container path. Should be called before starting the container!" diff --git a/test/clj_test_containers/core_test.clj b/test/clj_test_containers/core_test.clj index f373315..b4cca83 100644 --- a/test/clj_test_containers/core_test.clj +++ b/test/clj_test_containers/core_test.clj @@ -4,7 +4,7 @@ (:import [org.testcontainers.containers PostgreSQLContainer])) -(deftest init-test +(deftest create-test (testing "Testing basic testcontainer generic image initialisation" (let [container (create {:image-name "postgres:12.2" :exposed-ports [5432] @@ -30,9 +30,9 @@ (is (= "root\n" (:stdout result))))) (testing "Executing a command in the running Docker container with a custom container" - (let [container (create {:container (PostgreSQLContainer. "postgres:12.2") - :exposed-ports [5432] - :env-vars {"POSTGRES_PASSWORD" "pw"}}) + (let [container (init {:container (PostgreSQLContainer. "postgres:12.2") + :exposed-ports [5432] + :env-vars {"POSTGRES_PASSWORD" "pw"}}) initialized-container (start! container) result (execute-command! initialized-container ["whoami"]) stopped-container (stop! container)]