Extended config for wait strategies (#42)
This commit is contained in:
parent
6e94e86745
commit
71af11f503
2 changed files with 231 additions and 155 deletions
|
|
@ -18,7 +18,8 @@
|
||||||
ImageFromDockerfile)
|
ImageFromDockerfile)
|
||||||
(org.testcontainers.utility
|
(org.testcontainers.utility
|
||||||
MountableFile
|
MountableFile
|
||||||
ResourceReaper)))
|
ResourceReaper)
|
||||||
|
(java.time Duration)))
|
||||||
|
|
||||||
(defn- resolve-bind-mode
|
(defn- resolve-bind-mode
|
||||||
(^BindMode [bind-mode]
|
(^BindMode [bind-mode]
|
||||||
|
|
@ -82,15 +83,21 @@
|
||||||
(defmethod wait :http
|
(defmethod wait :http
|
||||||
[{:keys [path
|
[{:keys [path
|
||||||
port
|
port
|
||||||
|
method
|
||||||
status-codes
|
status-codes
|
||||||
tls
|
tls
|
||||||
read-timeout
|
read-timeout
|
||||||
basic-credentials] :as options}
|
basic-credentials
|
||||||
|
headers
|
||||||
|
startup-timeout] :as options}
|
||||||
^GenericContainer container]
|
^GenericContainer container]
|
||||||
(let [for-http (Wait/forHttp path)]
|
(let [for-http (Wait/forHttp path)]
|
||||||
(when port
|
(when port
|
||||||
(.forPort for-http port))
|
(.forPort for-http port))
|
||||||
|
|
||||||
|
(when method
|
||||||
|
(.withMethod for-http method))
|
||||||
|
|
||||||
(doseq [status-code status-codes]
|
(doseq [status-code status-codes]
|
||||||
(.forStatusCode for-http status-code))
|
(.forStatusCode for-http status-code))
|
||||||
|
|
||||||
|
|
@ -98,27 +105,53 @@
|
||||||
(.usingTls for-http))
|
(.usingTls for-http))
|
||||||
|
|
||||||
(when read-timeout
|
(when read-timeout
|
||||||
(.withReadTimeout for-http (java.time.Duration/ofSeconds read-timeout)))
|
(.withReadTimeout for-http (Duration/ofSeconds read-timeout)))
|
||||||
|
|
||||||
(when basic-credentials
|
(when basic-credentials
|
||||||
(let [{username :username password :password} basic-credentials]
|
(let [{:keys [username password]} basic-credentials]
|
||||||
(.withBasicCredentials for-http username password)))
|
(.withBasicCredentials for-http username password)))
|
||||||
|
|
||||||
|
(when headers
|
||||||
|
(.withHeaders for-http headers))
|
||||||
|
|
||||||
|
(when startup-timeout
|
||||||
|
(.withStartupTimeout for-http (Duration/ofSeconds startup-timeout)))
|
||||||
|
|
||||||
(.waitingFor container for-http)
|
(.waitingFor container for-http)
|
||||||
|
|
||||||
{:wait-for-http (dissoc options :strategy)}))
|
{:wait-for-http (dissoc options :strategy)}))
|
||||||
|
|
||||||
(defmethod wait :health
|
(defmethod wait :health
|
||||||
[_ ^GenericContainer container]
|
[{:keys [startup-timeout]} ^GenericContainer container]
|
||||||
(.waitingFor container (Wait/forHealthcheck))
|
(let [strategy (Wait/forHealthcheck)]
|
||||||
|
|
||||||
|
(when startup-timeout
|
||||||
|
(.withStartupTimeout strategy (Duration/ofSeconds startup-timeout)))
|
||||||
|
|
||||||
|
(.waitingFor container strategy))
|
||||||
{:wait-for-healthcheck true})
|
{:wait-for-healthcheck true})
|
||||||
|
|
||||||
(defmethod wait :log
|
(defmethod wait :log
|
||||||
[{:keys [message]} ^GenericContainer container]
|
[{:keys [message startup-timeout]} ^GenericContainer container]
|
||||||
(let [log-message (str ".*" message ".*\\n")]
|
(let [log-message (str ".*" message ".*\\n")
|
||||||
(.waitingFor container (Wait/forLogMessage log-message 1))
|
strategy (Wait/forLogMessage log-message 1)]
|
||||||
|
|
||||||
|
(when startup-timeout
|
||||||
|
(.withStartupTimeout strategy (Duration/ofSeconds startup-timeout)))
|
||||||
|
|
||||||
|
(.waitingFor container strategy)
|
||||||
{:wait-for-log-message log-message}))
|
{:wait-for-log-message log-message}))
|
||||||
|
|
||||||
|
(defmethod wait :port
|
||||||
|
[{:keys [startup-timeout]} ^GenericContainer container]
|
||||||
|
(let [strategy (Wait/forListeningPort)]
|
||||||
|
|
||||||
|
(when startup-timeout
|
||||||
|
(.withStartupTimeout strategy (Duration/ofSeconds startup-timeout)))
|
||||||
|
|
||||||
|
(.waitingFor container strategy))
|
||||||
|
{:wait-for-port true})
|
||||||
|
|
||||||
(defmethod wait :default [_ _] nil)
|
(defmethod wait :default [_ _] nil)
|
||||||
|
|
||||||
(s/fdef init
|
(s/fdef init
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,9 @@
|
||||||
(let [container (sut/create {:image-name "postgres:12.2"
|
(let [container (sut/create {:image-name "postgres:12.2"
|
||||||
:exposed-ports [5432]
|
:exposed-ports [5432]
|
||||||
:env-vars {"POSTGRES_PASSWORD" "pw"}
|
:env-vars {"POSTGRES_PASSWORD" "pw"}
|
||||||
:wait-for {:wait-strategy :log :message "accept connections"}})
|
:wait-for {:wait-strategy :log
|
||||||
|
:message "accept connections"
|
||||||
|
:startup-timeout 10}})
|
||||||
initialized-container (sut/start! container)
|
initialized-container (sut/start! container)
|
||||||
stopped-container (sut/stop! container)]
|
stopped-container (sut/stop! container)]
|
||||||
(is (some? (:id initialized-container)))
|
(is (some? (:id initialized-container)))
|
||||||
|
|
@ -41,6 +43,47 @@
|
||||||
(is (nil? (:id stopped-container)))
|
(is (nil? (:id stopped-container)))
|
||||||
(is (nil? (:mapped-ports stopped-container)))))
|
(is (nil? (:mapped-ports stopped-container)))))
|
||||||
|
|
||||||
|
(testing "Testing basic testcontainer generic image initialisation with wait for host port"
|
||||||
|
(let [container (sut/create {:image-name "postgres:12.2"
|
||||||
|
:exposed-ports [5432]
|
||||||
|
:env-vars {"POSTGRES_PASSWORD" "pw"}
|
||||||
|
:wait-for {:wait-strategy :port}})
|
||||||
|
initialized-container (sut/start! container)
|
||||||
|
stopped-container (sut/stop! container)]
|
||||||
|
(is (some? (:id initialized-container)))
|
||||||
|
(is (some? (:mapped-ports initialized-container)))
|
||||||
|
(is (some? (get (:mapped-ports initialized-container) 5432)))
|
||||||
|
(is (= (:wait-for-port initialized-container) true))
|
||||||
|
(is (nil? (:id stopped-container)))
|
||||||
|
(is (nil? (:mapped-ports stopped-container)))))
|
||||||
|
|
||||||
|
(testing "Testing basic testcontainer generic image initialisation with wait for http"
|
||||||
|
(let [container (sut/create {:image-name "alpine:3.5"
|
||||||
|
:network-aliases ["foo"]
|
||||||
|
:command ["/bin/sh"
|
||||||
|
"-c"
|
||||||
|
"while true ; do printf 'HTTP/1.1 200 OK\\n\\nyay' | nc -l -p 8080; done"]
|
||||||
|
:exposed-ports [8080]
|
||||||
|
:wait-for {:wait-strategy :http
|
||||||
|
:path "/"
|
||||||
|
:port 8080
|
||||||
|
:method "GET"
|
||||||
|
:status-codes [200]
|
||||||
|
:headers {"Accept" "text/plain"}}})
|
||||||
|
initialized-container (sut/start! container)
|
||||||
|
stopped-container (sut/stop! container)]
|
||||||
|
(is (some? (:id initialized-container)))
|
||||||
|
(is (some? (:mapped-ports initialized-container)))
|
||||||
|
(is (some? (get (:mapped-ports initialized-container) 8080)))
|
||||||
|
(is (= (:wait-for-http initialized-container) {:headers {"Accept" "text/plain"}
|
||||||
|
:method "GET"
|
||||||
|
:path "/"
|
||||||
|
:port 8080
|
||||||
|
:status-codes [200]
|
||||||
|
:wait-strategy :http}))
|
||||||
|
(is (nil? (:id stopped-container)))
|
||||||
|
(is (nil? (:mapped-ports stopped-container)))))
|
||||||
|
|
||||||
(testing "Testing basic testcontainer image creation from docker file"
|
(testing "Testing basic testcontainer image creation from docker file"
|
||||||
(let [container (sut/create-from-docker-file {:exposed-ports [80]
|
(let [container (sut/create-from-docker-file {:exposed-ports [80]
|
||||||
:docker-file "test/resources/Dockerfile"})
|
:docker-file "test/resources/Dockerfile"})
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue