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 bda7988..763f4ae 100644 --- a/src/clj_test_containers/core.clj +++ b/src/clj_test_containers/core.clj @@ -12,23 +12,27 @@ BindMode/READ_WRITE BindMode/READ_ONLY)) -(defn create +(defn init "Sets the properties for a testcontainer instance" - [{:keys [image-name exposed-ports env-vars command]}] - (let [container (GenericContainer. image-name)] - (.setExposedPorts container (map int exposed-ports)) + [{:keys [container exposed-ports env-vars command]}] + (.setExposedPorts container (map int exposed-ports)) - (if (some? env-vars) - (doseq [pair env-vars] - (.addEnv container (first pair) (second pair)))) + (run! (fn [[k v]] (.addEnv container k v)) env-vars) - (if (some? command) - (.setCommand container command)) + (when command + (.setCommand container command)) - {:container container - :exposed-ports (.getExposedPorts container) - :env-vars (.getEnvMap container) - :host (.getHost container)})) + {:container container + :exposed-ports (vec (.getExposedPorts container)) + :env-vars (into {} (.getEnvMap container)) + :host (.getHost container)}) + +(defn create + "Creates a generic testcontainer and sets its properties" + [{:keys [image-name] :as options}] + (->> (GenericContainer. image-name) + (assoc options :container) + init)) (defn create-from-docker-file [{:keys [exposed-ports env-vars command docker-file] diff --git a/test/clj_test_containers/core_test.clj b/test/clj_test_containers/core_test.clj index c78067e..973d15f 100644 --- a/test/clj_test_containers/core_test.clj +++ b/test/clj_test_containers/core_test.clj @@ -1,9 +1,12 @@ (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 create-test + (testing "Testing basic testcontainer generic image initialisation" -(deftest init-test - (testing "Testing basic testcontainer generic image initialization" (let [container (create {:image-name "postgres:12.2" :exposed-ports [5432] :env-vars {"POSTGRES_PASSWORD" "pw"}}) @@ -30,12 +33,22 @@ (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 (init {:container (PostgreSQLContainer. "postgres:12.2") + :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)))))) (deftest init-volume-test