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_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!"

View file

@ -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)]