Extended config for wait strategies (#42)

This commit is contained in:
Tim Zöller 2022-03-18 14:03:42 +01:00
parent 6e94e86745
commit 71af11f503
2 changed files with 231 additions and 155 deletions

View file

@ -18,7 +18,8 @@
ImageFromDockerfile)
(org.testcontainers.utility
MountableFile
ResourceReaper)))
ResourceReaper)
(java.time Duration)))
(defn- resolve-bind-mode
(^BindMode [bind-mode]
@ -82,15 +83,21 @@
(defmethod wait :http
[{:keys [path
port
method
status-codes
tls
read-timeout
basic-credentials] :as options}
basic-credentials
headers
startup-timeout] :as options}
^GenericContainer container]
(let [for-http (Wait/forHttp path)]
(when port
(.forPort for-http port))
(when method
(.withMethod for-http method))
(doseq [status-code status-codes]
(.forStatusCode for-http status-code))
@ -98,27 +105,53 @@
(.usingTls for-http))
(when read-timeout
(.withReadTimeout for-http (java.time.Duration/ofSeconds read-timeout)))
(.withReadTimeout for-http (Duration/ofSeconds read-timeout)))
(when basic-credentials
(let [{username :username password :password} basic-credentials]
(let [{:keys [username password]} basic-credentials]
(.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)
{:wait-for-http (dissoc options :strategy)}))
(defmethod wait :health
[_ ^GenericContainer container]
(.waitingFor container (Wait/forHealthcheck))
[{:keys [startup-timeout]} ^GenericContainer container]
(let [strategy (Wait/forHealthcheck)]
(when startup-timeout
(.withStartupTimeout strategy (Duration/ofSeconds startup-timeout)))
(.waitingFor container strategy))
{:wait-for-healthcheck true})
(defmethod wait :log
[{:keys [message]} ^GenericContainer container]
(let [log-message (str ".*" message ".*\\n")]
(.waitingFor container (Wait/forLogMessage log-message 1))
[{:keys [message startup-timeout]} ^GenericContainer container]
(let [log-message (str ".*" message ".*\\n")
strategy (Wait/forLogMessage log-message 1)]
(when startup-timeout
(.withStartupTimeout strategy (Duration/ofSeconds startup-timeout)))
(.waitingFor container strategy)
{: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)
(s/fdef init

View file

@ -31,7 +31,9 @@
(let [container (sut/create {:image-name "postgres:12.2"
:exposed-ports [5432]
: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)
stopped-container (sut/stop! container)]
(is (some? (:id initialized-container)))
@ -41,6 +43,47 @@
(is (nil? (:id 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"
(let [container (sut/create-from-docker-file {:exposed-ports [80]
:docker-file "test/resources/Dockerfile"})