parent
9f36f0db78
commit
1983cdea75
3 changed files with 34 additions and 11 deletions
|
|
@ -1,7 +1,8 @@
|
||||||
(ns clj-test-containers.core
|
(ns clj-test-containers.core
|
||||||
(:require
|
(:require
|
||||||
[clj-test-containers.spec.core :as cs]
|
[clj-test-containers.spec.core :as cs]
|
||||||
[clojure.spec.alpha :as s])
|
[clojure.spec.alpha :as s]
|
||||||
|
[clojure.string])
|
||||||
(:import
|
(:import
|
||||||
(java.nio.file
|
(java.nio.file
|
||||||
Paths)
|
Paths)
|
||||||
|
|
@ -9,6 +10,8 @@
|
||||||
BindMode
|
BindMode
|
||||||
GenericContainer
|
GenericContainer
|
||||||
Network)
|
Network)
|
||||||
|
(org.testcontainers.containers.output
|
||||||
|
ToStringConsumer)
|
||||||
(org.testcontainers.containers.wait.strategy
|
(org.testcontainers.containers.wait.strategy
|
||||||
Wait)
|
Wait)
|
||||||
(org.testcontainers.images.builder
|
(org.testcontainers.images.builder
|
||||||
|
|
@ -24,15 +27,15 @@
|
||||||
|
|
||||||
(defmulti wait
|
(defmulti wait
|
||||||
"Sets a wait strategy to the container.
|
"Sets a wait strategy to the container.
|
||||||
Supports :http, :health and :log as strategies.
|
Supports :http, :health and :log as strategies.
|
||||||
|
|
||||||
## HTTP Strategy
|
## HTTP Strategy
|
||||||
The :http strategy will only accept the container as initialized if it can be accessed
|
The :http strategy will only accept the container as initialized if it can be accessed
|
||||||
via HTTP. It accepts a path, a port, a vector of status codes, a boolean that specifies
|
via HTTP. It accepts a path, a port, a vector of status codes, a boolean that specifies
|
||||||
if TLS is enabled, a read timeout in seconds and a map with basic credentials, containing
|
if TLS is enabled, a read timeout in seconds and a map with basic credentials, containing
|
||||||
username and password. Only the path is required, all others are optional.
|
username and password. Only the path is required, all others are optional.
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
```clojure
|
```clojure
|
||||||
(wait {:strategy :http
|
(wait {:strategy :http
|
||||||
:port 80
|
:port 80
|
||||||
|
|
@ -93,7 +96,7 @@
|
||||||
|
|
||||||
(defn init
|
(defn init
|
||||||
"Sets the properties for a testcontainer instance"
|
"Sets the properties for a testcontainer instance"
|
||||||
[{:keys [container exposed-ports env-vars command network network-aliases wait-for]}]
|
[{:keys [container exposed-ports env-vars command network network-aliases wait-for capture-logs?]}]
|
||||||
|
|
||||||
(.setExposedPorts container (map int exposed-ports))
|
(.setExposedPorts container (map int exposed-ports))
|
||||||
|
|
||||||
|
|
@ -112,6 +115,7 @@
|
||||||
:exposed-ports (vec (.getExposedPorts container))
|
:exposed-ports (vec (.getExposedPorts container))
|
||||||
:env-vars (into {} (.getEnvMap container))
|
:env-vars (into {} (.getEnvMap container))
|
||||||
:host (.getHost container)
|
:host (.getHost container)
|
||||||
|
:capture-logs? capture-logs?
|
||||||
:network network} (wait wait-for container)))
|
:network network} (wait wait-for container)))
|
||||||
|
|
||||||
(s/fdef create
|
(s/fdef create
|
||||||
|
|
@ -179,13 +183,26 @@
|
||||||
:stdout (.getStdout result)
|
:stdout (.getStdout result)
|
||||||
:stderr (.getStderr result)}))
|
:stderr (.getStderr result)}))
|
||||||
|
|
||||||
|
(defn dump-logs
|
||||||
|
[container-config]
|
||||||
|
((:logs container-config)))
|
||||||
|
|
||||||
|
(defn capture-logs
|
||||||
|
[container]
|
||||||
|
(let [to-string-consumer (ToStringConsumer.)]
|
||||||
|
(.followOutput container to-string-consumer)
|
||||||
|
(fn []
|
||||||
|
(-> (.toUtf8String to-string-consumer)
|
||||||
|
(clojure.string/replace #"\n+" "\n")))))
|
||||||
|
|
||||||
(defn start!
|
(defn start!
|
||||||
"Starts the underlying testcontainer instance and adds new values to the response map, e.g. :id and :first-mapped-port"
|
"Starts the underlying testcontainer instance and adds new values to the response map, e.g. :id and :first-mapped-port"
|
||||||
[container-config]
|
[container-config]
|
||||||
(let [container (:container container-config)]
|
(let [{:keys [container capture-logs?]} container-config]
|
||||||
(.start container)
|
(.start container)
|
||||||
(-> container-config
|
(-> container-config
|
||||||
(assoc :id (.getContainerId container))
|
(assoc :id (.getContainerId container))
|
||||||
|
(assoc :logs (when capture-logs? (capture-logs container)))
|
||||||
(assoc :mapped-ports (into {}
|
(assoc :mapped-ports (into {}
|
||||||
(map (fn [port] [port (.getMappedPort container port)])
|
(map (fn [port] [port (.getMappedPort container port)])
|
||||||
(:exposed-ports container-config)))))))
|
(:exposed-ports container-config)))))))
|
||||||
|
|
|
||||||
|
|
@ -45,3 +45,6 @@
|
||||||
|
|
||||||
(s/def ::check
|
(s/def ::check
|
||||||
boolean?)
|
boolean?)
|
||||||
|
|
||||||
|
(s/def ::capture-logs?
|
||||||
|
(s/nilable boolean?))
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,8 @@
|
||||||
::csc/env-vars
|
::csc/env-vars
|
||||||
::csc/host]
|
::csc/host]
|
||||||
:opt-un [::network
|
:opt-un [::network
|
||||||
::wait-for]))
|
::wait-for
|
||||||
|
::csc/capture-logs?]))
|
||||||
|
|
||||||
(s/def ::init-options
|
(s/def ::init-options
|
||||||
(s/keys :req-un [::csc/container]
|
(s/keys :req-un [::csc/container]
|
||||||
|
|
@ -31,6 +32,7 @@
|
||||||
::csc/command
|
::csc/command
|
||||||
::network
|
::network
|
||||||
::wait-for
|
::wait-for
|
||||||
|
::csc/capture-logs?
|
||||||
::csc/network-aliases]))
|
::csc/network-aliases]))
|
||||||
|
|
||||||
(s/def ::create-options
|
(s/def ::create-options
|
||||||
|
|
@ -40,6 +42,7 @@
|
||||||
::csc/command
|
::csc/command
|
||||||
::network
|
::network
|
||||||
::wait-for
|
::wait-for
|
||||||
|
::csc/capture-logs?
|
||||||
::csc/network-aliases]))
|
::csc/network-aliases]))
|
||||||
|
|
||||||
(s/def ::create-network-options
|
(s/def ::create-network-options
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue