From a6d6c10fde6f4d8e553aa221da11d2eb32956093 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Z=C3=B6ller?= Date: Thu, 4 Jun 2020 12:35:16 +0200 Subject: [PATCH] #1 - Added simple configuration for generic container initialisation --- src/clj_test_containers/core.clj | 53 ++++++++++++++------------ test/clj_test_containers/core_test.clj | 15 ++++++-- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/src/clj_test_containers/core.clj b/src/clj_test_containers/core.clj index e7aa2e0..b28127a 100644 --- a/src/clj_test_containers/core.clj +++ b/src/clj_test_containers/core.clj @@ -1,34 +1,39 @@ (ns clj-test-containers.core) -(defn init +(defn create "Sets the properties for a testcontainer instance" [{image-name :image-name + exposed-ports :exposed-ports env-vars :env-vars - command :command - fs-bind :file-system-bind - waiting-for :waiting-for - copy-file :copy-file-to-container - extra-host :extra-host - network :network - network-aliases :network-aliases - network-mode :network-mode - image-pull-policy :image-pull-policy - classpath-resource-mapping :classpath-resource-mapping - startup-timeout :startup-timeout - privileged-mode :privileged-mode - startup-check-strategy :startup-check-strategy - working-directory :working-directory - follow-ouptut :follow-output - log-consumer :log-consumer}] - (let [container (-> (org.testcontainers.containers.GenericContainer. image-name) - (.withExposedPorts (into-array Integer (map int exposed-ports))) - (.withEnv env-vars) - (as-> container - (if (some? command) - (.withCommand command) - container)))] + command :command}] + + (let [container (org.testcontainers.containers.GenericContainer. image-name)] + (.setExposedPorts container (map int exposed-ports)) + + (doseq [pair env-vars] + (.addEnv container (first pair) (second pair))) + + (if (some? command) + (.setCommand container command)) + {:container container :exposed-ports (.getExposedPorts container) :env-vars (.getEnvMap container) :host (.getHost container)})) + +(defn start + "Starts the underlying testcontainer instance and adds new values to the response map, e.g. :id and :first-mapped-port" + [container-conf] + (let [container (:container container-conf)] + (.start container) + (-> container-conf + (assoc :id (.getContainerId container)) + (assoc :mapped-ports (into {} (map (fn [port] [port + (.getMappedPort container port)]) + (:exposed-ports container-conf))))))) + +(defn stop + "Stops the underlying container" + [{container :container}] + (.stop container)) diff --git a/test/clj_test_containers/core_test.clj b/test/clj_test_containers/core_test.clj index e8c6aca..360d7b7 100644 --- a/test/clj_test_containers/core_test.clj +++ b/test/clj_test_containers/core_test.clj @@ -2,6 +2,15 @@ (:require [clojure.test :refer :all] [clj-test-containers.core :refer :all])) -(deftest a-test - (testing "FIXME, I fail." - (is (= 0 1)))) +(deftest init-test + (testing "Testing basic testcontainer generic image initialisation" + (let [container (create {:image-name "postgres:12.2" + :exposed-ports [5432] + :env-vars {"POSTGRES_PASSWORD" "pw"}}) + initialized-container (start container) + container-id (:id initialized-container) + mapped-ports (:mapped-ports initialized-container)] + (is (some? container-id)) + (is (some? mapped-ports)) + (is (some? (get mapped-ports 5432))) + (stop container))))