Liking cljdoc? Tell your friends :D

clj-test-containers

javahippie

What it is

This application is supposed to be a lightweight wrapper around the Testcontainers Java library.

What it isn't

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.

Usage

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)

Functions and Properties

create

Creates a testcontainers instance and returns them

Config parameters:

KeyTypeDescription
:image-nameString, mandatoryThe name and label of an image, e.g. postgres:12.2
:exposed-portsVector with ints, mandatoryAll ports which should be exposed and mapped to a local port
:env-varsMapA map with environment variables
:commandVector with stringsEnvironment Variables to be set in the container

Result:

KeyTypeDescription
:containerorg.testcontainers.containers.ContainerThe Testcontainers instance, accessible for everything this library doesn't provide (yet)
:exposed-portsVector with intsValue of the same input parameter
:env-varsMapValue of the same input parameter
:hostStringThe host for the Docker Container

Example:

(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"]})

start!

Starts the Testcontainer, which was defined by create

Config parameters:

KeyTypeDescription
First parameter:
container-configMap, mandatoryReturn value of the create function

Result:

KeyTypeDescription
:containerorg.testcontainers.containers.ContainerThe Testcontainers instance, accessible for everything this library doesn't provide (yet)
:exposed-portsVector with intsValue of the same input parameter
:env-varsMapValue of the same input parameter
:hostStringThe host for the Docker Container
:idStringThe ID of the started docker container
:mapped-portsMapA map containing the container port as key and the mapped local port as a value

Example:

(def container (create {:image-name "alpine:3.2"
                        :exposed-ports [80]
                        :env-vars {"MAGIC_NUMBER" "42"})
		            
(start! container)    

stop!

Stops the Testcontainer, which was defined by create

Config parameters:

KeyTypeDescription
First parameter:
container-configMap, mandatoryReturn value of the create function

Result:

The container-config

Example:

(def container (create {:image-name "alpine:3.2"
                        :exposed-ports [80]
                        :env-vars {"MAGIC_NUMBER" "42"})
                        
(start! container)    
(stop! container)    

map-classpath-resource!

Maps a resource from your classpath into the containers file system

Config parameters:

KeyTypeDescription
First parameter:
container-configMap, mandatoryReturn value of the create function
Second parameter:  
:resource-pathString, mandatoryPath of your classpath resource
:container-pathString, mandatoryPath, to which the resource should be mapped
:modeKeyword, mandatory:read-only or :read-write

Result:

The container-config

Example:

(map-classpath-resource! container {:resource-path "test.sql"
                             	      :container-path "/opt/test.sql"
                                    :mode :read-only})

bind-filesystem!

Binds a path from your local filesystem into the Docker container as a volume

Config parameters:

KeyTypeDescription
First parameter:
container-configMap, mandatoryReturn value of the create function
Second parameter:  
:host-pathString , mandatoryPath on your local filesystem
:container-pathString, mandatoryPath, to which the resource should be mapped
:modeKeyword, mandatory:read-only or :read-write

Result:

The container-config

Example:

(bind-filesystem! container {:host-path "."
                             :container-path "/opt"
                             :mode :read-only})

copy-file-to-container!

Copies a file from your filesystem or classpath into the running container

Config parameters:

KeyTypeDescription
First parameter:
container-configMap, mandatoryReturn value of the create function
Second parameter:  
:pathString, mandatoryPath to a classpath resource or file on your filesystem
:host-pathString, mandatoryPath, to which the file should be copied
:typeKeyword, mandatory:classpath-resource or :host-path

Result:

The container-config

Example:

(copy-file-to-container! container {:path "test.sql"
                                    :container-path "/opt/test.sql"
                                    :type :host-path})

execute-command!

Executes a command in the running container, and returns the result

Config parameters:

KeyTypeDescription
First parameter:
container-configMap, mandatoryReturn value of the create function
Second parameter:  
commandVector with Strings, mandatoryA vector containing the command and its parameters

Result:

KeyTypeDescription
:exit-codeintExit code of the executed command
:stdoutStringContent of stdout
:stdinStringContent of stdin

Example:

(execute-command! container ["tail" "/opt/test.sql"])

License

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