#3 - Filesytem mappings into the container

This commit is contained in:
Tim Zöller 2020-06-04 22:51:33 +02:00
parent ca251dff7f
commit ec2fdf42eb
2 changed files with 66 additions and 4 deletions

View file

@ -1,21 +1,51 @@
(ns clj-test-containers.core)
5(ns clj-test-containers.core)
(defn- resolve-bind-mode [bind-mode]
(if (= :read-write bind-mode)
org.testcontainers.containers.BindMode/READ_WRITE
org.testcontainers.containers.BindMode/READ_ONLY))
(defn- configure-volume [container {classpath-resource-mapping :classpath-resource-mapping
file-system-bind :file-system-bind}]
(if (some? classpath-resource-mapping)
(let [{resource-path :resource-path
container-path :container-path
mode :mode} classpath-resource-mapping]
(.withClasspathResourceMapping container
resource-path
container-path
(resolve-bind-mode mode))))
(if (some? file-system-bind)
(let [{host-path :host-path
container-path :container-path
mode :mode} file-system-bind]
(.withFileSystemBind container
host-path
container-path))))
(defn create
"Sets the properties for a testcontainer instance"
[{image-name :image-name
exposed-ports :exposed-ports
env-vars :env-vars
command :command}]
command :command
volume :volume}]
(let [container (org.testcontainers.containers.GenericContainer. image-name)]
(.setExposedPorts container (map int exposed-ports))
(if (some? env-vars)
(doseq [pair env-vars]
(.addEnv container (first pair) (second pair)))
(.addEnv container (first pair) (second pair))))
(if (some? command)
(.setCommand container command))
(if (some? volume)
(configure-volume container volume))
{:container container
:exposed-ports (.getExposedPorts container)
:env-vars (.getEnvMap container)

View file

@ -14,3 +14,35 @@
(is (some? (get (:mapped-ports initialized-container) 5432)))
(is (nil? (:id stopped-container)))
(is (nil? (:mapped-ports stopped-container))))))
(deftest init-with-volume-test
(testing "Testing mapping of a classpath resource"
(let [container (create {:image-name "postgres:12.2"
:exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}
:volume {:classpath-resource-mapping {:resource-path "test.sql"
:container-path "/opt/test.sql"
:mode :read-only}}})
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) 5432)))
(is (nil? (:id stopped-container)))
(is (nil? (:mapped-ports stopped-container)))))
(testing "Testing mapping of a filesystem-binding"
(let [container (create {:image-name "postgres:12.2"
:exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}
:volume {:file-system-bind {:host-path "/tmp"
:container-path "/opt"
:mode :read-only}}})
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) 5432)))
(is (nil? (:id stopped-container)))
(is (nil? (:mapped-ports stopped-container))))))