From ea848bc9c5372344d8ab12128c772b64773e0084 Mon Sep 17 00:00:00 2001 From: Rob Hanlon Date: Mon, 3 Aug 2020 15:59:50 -0700 Subject: [PATCH] Allow for provision of a Testcontainer instance Testcontainers provides many different container implementations that can be very useful. This simple change allows callers to provide their own, premade container instance so consumers can flexibly choose their container type. If no container instance is supplied, then clj-test-containers falls back to its original behavior. --- project.clj | 3 ++- src/clj_test_containers/core.clj | 4 ++-- test/clj_test_containers/core_test.clj | 16 ++++++++++++++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/project.clj b/project.clj index 0fad771..22c64df 100644 --- a/project.clj +++ b/project.clj @@ -7,7 +7,8 @@ :dependencies [[org.clojure/clojure "1.10.1"] [org.testcontainers/testcontainers "1.14.3"]] :plugins [[metosin/bat-test "0.4.4"]] - :bat-test {:report [:pretty {:type :junit + :bat-test {:report [:pretty {:type :junit :output-to "target/junit.xml"}]} + :profiles {:dev {:dependencies [[org.testcontainers/postgresql "1.14.3"]]}} :target-path "target/%s") diff --git a/src/clj_test_containers/core.clj b/src/clj_test_containers/core.clj index dc307a7..14b2075 100644 --- a/src/clj_test_containers/core.clj +++ b/src/clj_test_containers/core.clj @@ -13,8 +13,8 @@ (defn create "Sets the properties for a testcontainer instance" - [{:keys [image-name exposed-ports env-vars command]}] - (let [container (GenericContainer. image-name)] + [{:keys [container image-name exposed-ports env-vars command]}] + (let [container (or container (GenericContainer. image-name))] (.setExposedPorts container (map int exposed-ports)) (if (some? env-vars) diff --git a/test/clj_test_containers/core_test.clj b/test/clj_test_containers/core_test.clj index 9e9de05..f373315 100644 --- a/test/clj_test_containers/core_test.clj +++ b/test/clj_test_containers/core_test.clj @@ -1,6 +1,8 @@ (ns clj-test-containers.core-test (:require [clojure.test :refer :all] - [clj-test-containers.core :refer :all])) + [clj-test-containers.core :refer :all]) + (:import [org.testcontainers.containers + PostgreSQLContainer])) (deftest init-test (testing "Testing basic testcontainer generic image initialisation" @@ -19,7 +21,17 @@ (testing "Executing a command in the running Docker container" (let [container (create {:image-name "postgres:12.2" - :exposed-ports [5432] + :exposed-ports [5432] + :env-vars {"POSTGRES_PASSWORD" "pw"}}) + initialized-container (start! container) + result (execute-command! initialized-container ["whoami"]) + stopped-container (stop! container)] + (is (= 0 (:exit-code result))) + (is (= "root\n" (:stdout result))))) + + (testing "Executing a command in the running Docker container with a custom container" + (let [container (create {:container (PostgreSQLContainer. "postgres:12.2") + :exposed-ports [5432] :env-vars {"POSTGRES_PASSWORD" "pw"}}) initialized-container (start! container) result (execute-command! initialized-container ["whoami"])