Extract function

This commit is contained in:
Rob Hanlon 2020-08-04 10:58:12 -07:00
parent ea848bc9c5
commit d9e9b9724d
No known key found for this signature in database
GPG key ID: 14C05B6156CB50E6
2 changed files with 21 additions and 17 deletions

View file

@ -11,23 +11,27 @@
BindMode/READ_WRITE BindMode/READ_WRITE
BindMode/READ_ONLY)) BindMode/READ_ONLY))
(defn create (defn init
"Sets the properties for a testcontainer instance" "Sets the properties for a testcontainer instance"
[{:keys [container image-name exposed-ports env-vars command]}] [{:keys [container exposed-ports env-vars command]}]
(let [container (or container (GenericContainer. image-name))]
(.setExposedPorts container (map int exposed-ports)) (.setExposedPorts container (map int exposed-ports))
(if (some? env-vars) (when (some? env-vars)
(doseq [pair env-vars] (doseq [pair env-vars]
(.addEnv container (first pair) (second pair)))) (.addEnv container (first pair) (second pair))))
(if (some? command) (when (some? command)
(.setCommand container command)) (.setCommand container command))
{:container container {:container container
:exposed-ports (.getExposedPorts container) :exposed-ports (.getExposedPorts container)
:env-vars (.getEnvMap container) :env-vars (.getEnvMap container)
:host (.getHost 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! (defn map-classpath-resource!
"Maps a resource in the classpath to the given container path. Should be called before starting the container!" "Maps a resource in the classpath to the given container path. Should be called before starting the container!"

View file

@ -4,7 +4,7 @@
(:import [org.testcontainers.containers (:import [org.testcontainers.containers
PostgreSQLContainer])) PostgreSQLContainer]))
(deftest init-test (deftest create-test
(testing "Testing basic testcontainer generic image initialisation" (testing "Testing basic testcontainer generic image initialisation"
(let [container (create {:image-name "postgres:12.2" (let [container (create {:image-name "postgres:12.2"
:exposed-ports [5432] :exposed-ports [5432]
@ -30,7 +30,7 @@
(is (= "root\n" (:stdout result))))) (is (= "root\n" (:stdout result)))))
(testing "Executing a command in the running Docker container with a custom container" (testing "Executing a command in the running Docker container with a custom container"
(let [container (create {:container (PostgreSQLContainer. "postgres:12.2") (let [container (init {:container (PostgreSQLContainer. "postgres:12.2")
:exposed-ports [5432] :exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}}) :env-vars {"POSTGRES_PASSWORD" "pw"}})
initialized-container (start! container) initialized-container (start! container)