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] [_ ^GenericContainer container]
(let [to-string-consumer (ToStringConsumer.)] (let [to-string-consumer (ToStringConsumer.)]
(.followOutput container to-string-consumer) (.followOutput container to-string-consumer)
{:string-log (fn [] {:log (fn []
(-> (.toUtf8String to-string-consumer) (-> (.toUtf8String to-string-consumer)
(clojure.string/replace #"\n+" "\n")))})) (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 (defn dump-logs
"Dumps the logs found by invoking the function on the :string-log key" "Dumps the logs found by invoking the function on the :string-log key"
[container-config] [container-config]
((:string-log container-config))) ((:log container-config)))
(defn start! (defn start!
"Starts the underlying testcontainer instance and adds new values to the "Starts the underlying testcontainer instance and adds new values to the
@ -267,10 +267,10 @@
(let [map-port (fn map-port (let [map-port (fn map-port
[port] [port]
[port (.getMappedPort container 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 (-> container-config
(merge {:id (.getContainerId container) :mapped-ports mapped-ports} (merge {:id (.getContainerId container) :mapped-ports mapped-ports} logger)
(log log-to container))
(dissoc :log-to)))) (dissoc :log-to))))
(defn stop! (defn stop!

View file

@ -1,7 +1,8 @@
(ns clj-test-containers.core-test (ns clj-test-containers.core-test
(:require (:require
[clj-test-containers.core :as sut] [clj-test-containers.core :as sut]
[clojure.test :refer [deftest is testing]]) [clojure.test :refer [deftest is testing]]
[clojure.string :refer [includes?]])
(:import (:import
(org.testcontainers.containers (org.testcontainers.containers
PostgreSQLContainer))) PostgreSQLContainer)))
@ -19,6 +20,22 @@
(is (nil? (:id stopped-container))) (is (nil? (:id stopped-container)))
(is (nil? (:mapped-ports 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" (testing "Testing basic testcontainer generic image initialisation with wait for log message"
(let [container (sut/create {:image-name "postgres:12.2" (let [container (sut/create {:image-name "postgres:12.2"
:exposed-ports [5432] :exposed-ports [5432]