This application is supposed to be a lightweight wrapper around the Testcontainers Java library.
This library does not provide tools to include testcontainers in your testing lifecycle. As there are many different test tools with different approaches to testing in the clojure world, handling the lifecycle is up to you.
The library provides a set of functions to interact with the testcontainers. A simple exampe could look like this:
(require '[clj-test-containers.core :as tc])
(def container (-> (tc/create {:image-name "postgres:12.1"
:exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "verysecret"}})
(tc/configure-volume! {:file-system-bind {:host-path "/tmp"
:container-path "/opt"
:mode :read-only}})))
(do-database-testing (:host container)
(get (:mapped-ports container) 5432))
(tc/stop! container)
Creates a testcontainers instance and returns them
Key | Type | Description |
---|---|---|
:image-name | String, mandatory | The name and label of an image, e.g. postgres:12.2 |
:exposed-ports | Vector with ints, mandatory | All ports which should be exposed and mapped to a local port |
:env-vars | Map | A map with environment variables |
:command | Vector with strings | Environment Variables to be set in the container |
Key | Type | Description |
---|---|---|
:container | org.testcontainers.containers.Container | The Testcontainers instance, accessible for everything this library doesn't provide (yet) |
:exposed-ports | Vector with ints | Value of the same input parameter |
:env-vars | Map | Value of the same input parameter |
:host | String | The host for the Docker Container |
(create {:image-name "alpine:3.2"
:exposed-ports [80]
:env-vars {"MAGIC_NUMBER" "42"
:command ["/bin/sh"
"-c"
"while true; do echo \"$MAGIC_NUMBER\" | nc -l -p 80; done"]})
Starts the Testcontainer, which was defined by create
Key | Type | Description |
---|---|---|
First parameter: | ||
container-config | Map, mandatory | Return value of the create function |
Key | Type | Description |
---|---|---|
:container | org.testcontainers.containers.Container | The Testcontainers instance, accessible for everything this library doesn't provide (yet) |
:exposed-ports | Vector with ints | Value of the same input parameter |
:env-vars | Map | Value of the same input parameter |
:host | String | The host for the Docker Container |
:id | String | The ID of the started docker container |
:mapped-ports | Map | A map containing the container port as key and the mapped local port as a value |
(def container (create {:image-name "alpine:3.2"
:exposed-ports [80]
:env-vars {"MAGIC_NUMBER" "42"})
(start! container)
Stops the Testcontainer, which was defined by create
Key | Type | Description |
---|---|---|
First parameter: | ||
container-config | Map, mandatory | Return value of the create function |
The container-config
(def container (create {:image-name "alpine:3.2"
:exposed-ports [80]
:env-vars {"MAGIC_NUMBER" "42"})
(start! container)
(stop! container)
Maps a resource from your classpath into the containers file system
Key | Type | Description |
---|---|---|
First parameter: | ||
container-config | Map, mandatory | Return value of the create function |
Second parameter: | ||
:resource-path | String, mandatory | Path of your classpath resource |
:container-path | String, mandatory | Path, to which the resource should be mapped |
:mode | Keyword, mandatory | :read-only or :read-write |
The container-config
(map-classpath-resource! container {:resource-path "test.sql"
:container-path "/opt/test.sql"
:mode :read-only})
Binds a path from your local filesystem into the Docker container as a volume
Key | Type | Description |
---|---|---|
First parameter: | ||
container-config | Map, mandatory | Return value of the create function |
Second parameter: | ||
:host-path | String , mandatory | Path on your local filesystem |
:container-path | String, mandatory | Path, to which the resource should be mapped |
:mode | Keyword, mandatory | :read-only or :read-write |
The container-config
(bind-filesystem! container {:host-path "."
:container-path "/opt"
:mode :read-only})
Copies a file from your filesystem or classpath into the running container
Key | Type | Description |
---|---|---|
First parameter: | ||
container-config | Map, mandatory | Return value of the create function |
Second parameter: | ||
:path | String, mandatory | Path to a classpath resource or file on your filesystem |
:host-path | String, mandatory | Path, to which the file should be copied |
:type | Keyword, mandatory | :classpath-resource or :host-path |
The container-config
(copy-file-to-container! container {:path "test.sql"
:container-path "/opt/test.sql"
:type :host-path})
Executes a command in the running container, and returns the result
Key | Type | Description |
---|---|---|
First parameter: | ||
container-config | Map, mandatory | Return value of the create function |
Second parameter: | ||
command | Vector with Strings, mandatory | A vector containing the command and its parameters |
Key | Type | Description |
---|---|---|
:exit-code | int | Exit code of the executed command |
:stdout | String | Content of stdout |
:stdin | String | Content of stdin |
(execute-command! container ["tail" "/opt/test.sql"])
Copyright © 2020 Tim Zöller
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close