Enhanced logging mechanism, added a test for it

This commit is contained in:
Tim Zöller 2020-10-23 22:42:11 +02:00
parent dac2b1ba4b
commit f40dcec5f0
2 changed files with 27 additions and 10 deletions

View file

@ -244,18 +244,18 @@
[_ ^GenericContainer container]
(let [to-string-consumer (ToStringConsumer.)]
(.followOutput container to-string-consumer)
{:string-log (fn []
(-> (.toUtf8String to-string-consumer)
(clojure.string/replace #"\n+" "\n")))}))
{:log (fn []
(-> (.toUtf8String to-string-consumer)
(clojure.string/replace #"\n+" "\n")))}))
(defmethod log :slf4j [_ _] nil)
(defmethod log :slf4j [_ _] nil) ;; Not yet implemented
(defmethod log :default [_ _] nil)
(defmethod log :default [_ _] nil) ;; Not yet implemented
(defn dump-logs
"Dumps the logs found by invoking the function on the :string-log key"
[container-config]
((:string-log container-config)))
((:log container-config)))
(defn start!
"Starts the underlying testcontainer instance and adds new values to the
@ -267,10 +267,10 @@
(let [map-port (fn map-port
[port]
[port (.getMappedPort container port)])
mapped-ports (into {} (map map-port) exposed-ports)]
mapped-ports (into {} (map map-port) exposed-ports)
logger (log log-to container)]
(-> container-config
(merge {:id (.getContainerId container) :mapped-ports mapped-ports}
(log log-to container))
(merge {:id (.getContainerId container) :mapped-ports mapped-ports} logger)
(dissoc :log-to))))
(defn stop!

View file

@ -1,7 +1,8 @@
(ns clj-test-containers.core-test
(:require
[clj-test-containers.core :as sut]
[clojure.test :refer [deftest is testing]])
[clojure.test :refer [deftest is testing]]
[clojure.string :refer [includes?]])
(:import
(org.testcontainers.containers
PostgreSQLContainer)))
@ -19,6 +20,22 @@
(is (nil? (:id stopped-container)))
(is (nil? (:mapped-ports stopped-container)))))
(testing "Testing log access to the container"
(let [container (sut/init {:container (PostgreSQLContainer. "postgres:12.2")
:log-to {:log-strategy :string}})
initialized-container (sut/start! container)]
(Thread/sleep 500)
(is (includes? (sut/dump-logs initialized-container) "database system is ready to accept connections"))))
(comment
(def cnt (sut/create {:image-name "postgres:12.2"
:exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}
:wait-for {:wait-strategy :log :message "accept connections"}
:log-to {:log-strategy :string}}))
(def cnt (sut/start! cnt))
(sut/dump-logs cnt))
(testing "Testing basic testcontainer generic image initialisation with wait for log message"
(let [container (sut/create {:image-name "postgres:12.2"
:exposed-ports [5432]