diff --git a/README.md b/README.md index 6efebe1..ba80bea 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,29 @@ The library provides a set of functions to interact with the testcontainers. A s (tc/stop! container) ``` + +To create a container using your local Dockerfile + +```clojure +;; Example via test +(require '[clojure.test :refer :all]) +(require '[clj-test-containers.core :refer :all]) + +(testing "Testing basic testcontainer image creation from docker file" + (let [container (create-from-docker-file {:env-vars + {"FOO" "bar" + "MAGIC_NUMBER" "42"} + :exposed-ports [80] + :docker-file "resources/Dockerfile"}) + initialized-container (start! container) + stopped-container (stop! container)] + (is (some? (:id initialized-container))) + (is (some? (:mapped-ports initialized-container))) + (is (some? (get (:mapped-ports initialized-container) 80))) + (is (nil? (:id stopped-container))) + (is (nil? (:mapped-ports stopped-container))))) +``` + ## Functions and Properties ### create diff --git a/src/clj_test_containers/core.clj b/src/clj_test_containers/core.clj index dc307a7..bda7988 100644 --- a/src/clj_test_containers/core.clj +++ b/src/clj_test_containers/core.clj @@ -1,9 +1,10 @@ (ns clj-test-containers.core - (:import [org.testcontainers.containers - GenericContainer] - [org.testcontainers.utility - MountableFile] - [org.testcontainers.containers BindMode])) + (:require [clojure.java.io :as io]) + (:import [org.testcontainers.containers GenericContainer] + [org.testcontainers.utility MountableFile] + [org.testcontainers.containers BindMode] + [org.testcontainers.images.builder ImageFromDockerfile] + [java.nio.file Path Paths])) (defn- resolve-bind-mode [bind-mode] @@ -29,6 +30,23 @@ :env-vars (.getEnvMap container) :host (.getHost container)})) +(defn create-from-docker-file + [{:keys [exposed-ports env-vars command docker-file] + :or {docker-file "Dockerfile"}}] + (let [docker-image (-> (ImageFromDockerfile.) + (.withDockerfile (Paths/get "." (into-array [docker-file])))) + container (GenericContainer. docker-image)] + (.setExposedPorts container (map int exposed-ports)) + (if (some? env-vars) + (doseq [[k v] env-vars] + (.addEnv container k v))) + (if (some? command) + (.setCommand container command)) + {:container container + :exposed-ports (.getExposedPorts container) + :env-vars (.getEnvMap container) + :host (.getHost container)})) + (defn map-classpath-resource! "Maps a resource in the classpath to the given container path. Should be called before starting the container!" [container-config diff --git a/test/clj_test_containers/core_test.clj b/test/clj_test_containers/core_test.clj index 9e9de05..c78067e 100644 --- a/test/clj_test_containers/core_test.clj +++ b/test/clj_test_containers/core_test.clj @@ -3,7 +3,7 @@ [clj-test-containers.core :refer :all])) (deftest init-test - (testing "Testing basic testcontainer generic image initialisation" + (testing "Testing basic testcontainer generic image initialization" (let [container (create {:image-name "postgres:12.2" :exposed-ports [5432] :env-vars {"POSTGRES_PASSWORD" "pw"}}) @@ -13,6 +13,17 @@ (is (some? (:mapped-ports initialized-container))) (is (some? (get (:mapped-ports initialized-container) 5432))) (is (nil? (:id stopped-container))) + (is (nil? (:mapped-ports stopped-container))))) + + (testing "Testing basic testcontainer image creation from docker file" + (let [container (create-from-docker-file {:exposed-ports [80] + :docker-file "test/resources/Dockerfile"}) + initialized-container (start! container) + stopped-container (stop! container)] + (is (some? (:id initialized-container))) + (is (some? (:mapped-ports initialized-container))) + (is (some? (get (:mapped-ports initialized-container) 80))) + (is (nil? (:id stopped-container))) (is (nil? (:mapped-ports stopped-container)))))) (deftest execute-command-in-container @@ -51,8 +62,8 @@ :exposed-ports [5432] :env-vars {"POSTGRES_PASSWORD" "pw"}}) (bind-filesystem! {:host-path "." - :container-path "/opt" - :mode :read-only})) + :container-path "/opt" + :mode :read-only})) initialized-container (start! container) file-check (execute-command! initialized-container ["tail" "/opt/README.md"]) stopped-container (stop! container)] diff --git a/test/resources/Dockerfile b/test/resources/Dockerfile new file mode 100644 index 0000000..5ef646a --- /dev/null +++ b/test/resources/Dockerfile @@ -0,0 +1,2 @@ +FROM nginx:alpine +COPY index.html /usr/share/nginx/html/index.html diff --git a/test/resources/index.html b/test/resources/index.html new file mode 100644 index 0000000..5bc105f --- /dev/null +++ b/test/resources/index.html @@ -0,0 +1,20 @@ + + +
+ + + +