Compare commits

..

No commits in common. "main" and "0.4.0" have entirely different histories.
main ... 0.4.0

16 changed files with 416 additions and 801 deletions

49
.circleci/config.yml Normal file
View file

@ -0,0 +1,49 @@
# Clojure CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-clojure/ for more details
#
version: 2
jobs:
build:
machine:
image: ubuntu-1604:202004-01
working_directory: ~/repo
environment:
# Customize the JVM maximum heap limit
JVM_OPTS: -Xmx3200m
steps:
- checkout
# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "project.clj" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- run:
name: Install Leiningen
command: wget https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein && chmod a+x lein && lein --version
- run: lein deps
- save_cache:
paths:
- ~/.lein
- ~/.m2
key: v1-dependencies-{{ checksum "project.clj" }}
# check syntax and warn on reflection
- run: lein check
# run tests!
- run: lein test
# check style
- run: lein cljstyle check --report
- store_test_results:
path: target

View file

@ -1,18 +0,0 @@
name: Set up Build
description: Sets up Build
runs:
using: "composite"
steps:
- uses: actions/setup-java@v4
with:
java-version: '8'
distribution: temurin
- name: Cache dependencies
uses: actions/cache@v4
with:
path: ~/.m2/repository
key: ${{ runner.os }}-lein-${{ hashFiles('**/project.clj') }}
restore-keys: ${{ runner.os }}-lein-
- name: Clear existing docker image cache
shell: bash
run: docker image prune -af

View file

@ -1,22 +0,0 @@
name: Clojure CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Build
uses: ./.github/actions/setup-build
- name: Install dependencies
run: lein deps
- name: Run tests
run: lein test

View file

@ -1,28 +0,0 @@
name: Release
on:
release:
types: [published]
permissions:
contents: read
jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Build
uses: ./.github/actions/setup-build
- name: Set version
run: |
lein change version set '"${{github.event.release.tag_name}}"'
- name: Release ${{github.event.release.tag_name}}
run: lein with-profile release deploy maven
env:
OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }}
OSSRH_PASSWORD: ${{ secrets.OSSRH_PASSWORD }}

3
.gitignore vendored
View file

@ -8,11 +8,8 @@ pom.xml.asc
/.lein-*
/.nrepl-history
/.nrepl-port
.clj-kondo/
.hgignore
.hg/
.DS_Store
.lsp
.cpcache
/.idea/
/clj-test-containers.iml

View file

@ -1,46 +1,6 @@
# Change Log
All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/).
## [0.7.4] - 2022-11-16
### Changed
- [#64](https://github.com/javahippie/clj-test-containers/pull/64): Fix container path parameter for copy-file-to-container! in README
- [#65](https://github.com/javahippie/clj-test-containers/issues/65): Upgrade to testcontainers-java 1.17.6
- [#66](https://github.com/javahippie/clj-test-containers/issues/66): Remove usage of deprecated API calls for cleanup
- [#67](https://github.com/javahippie/clj-test-containers/issues/67): Update Dependencies
## [0.7.3] - 2202-09-30
### Changed
- [#63](https://github.com/javahippie/clj-test-containers/issues/63): Upgrade to testcontainers-java 1.17.4
## [0.7.2] - 2202-07-03
### Changed
- [#61](https://github.com/javahippie/clj-test-containers/issues/61): Upgrade to testcontainers-java 1.17.3
## [0.7.1] - 2202-06-12
### Added
- [#58](https://github.com/javahippie/clj-test-containers/issues/58): Add new `allowInsecure` flag to the HTTP wait strategy
### Changed
- [#57](https://github.com/javahippie/clj-test-containers/issues/57): Upgrade to testcontainers-java 1.17.2
## [0.7.0] - 2022-04-23
### Changed
- [#56](https://github.com/javahippie/clj-test-containers/issues/56): Update to Testcontainers 1.17.1
## [0.6.0] - 2022-03-26
### Changed
- [#55](https://github.com/javahippie/clj-test-containers/issues/55): Upgrade to latest Testcontainers version
### Added
- [#42](https://github.com/javahippie/clj-test-containers/issues/42): Extend wait strategies
- [#41](https://github.com/javahippie/clj-test-containers/issues/41): Complete logging mechanisms
## [0.5.0] - 2021-08-18
### Changed
- [#49](https://github.com/javahippie/clj-test-containers/issues/49): Updated to latest Testcontainers version
- [#50](https://github.com/javahippie/clj-test-containers/issues/50): supress Reflection warnings
- [#52](https://github.com/javahippie/clj-test-containers/pull/52): Fix unbalanced parens and braces
## [0.4.0] - 2020-12-16
### Added
- [#43](https://github.com/javahippie/clj-test-containers/issues/43): Provide a way to remove all running testcontainers instances in a REPL session
@ -52,7 +12,7 @@ All notable changes to this project will be documented in this file. This change
## [0.3.0] - 2020-10-23
### Added
- [#25](https://github.com/javahippie/clj-test-containers/issues/25): Add support for a container wait strategy
- [#25](https://github.com/javahippie/clj-test-containers/issues/25): Add support for a container wait stategy
- [#35](https://github.com/javahippie/clj-test-containers/issues/35): Add support for docker version 2.4.0 - upgrading testcontainers-java version
- [#27](https://github.com/javahippie/clj-test-containers/issues/27): Add support for accessing container logs
- [#38](https://github.com/javahippie/clj-test-containers/pull/38): Add type hints to silence reflection warnings

View file

@ -1,24 +1,19 @@
# clj-test-containers
[![javahippie](https://circleci.com/gh/javahippie/clj-test-containers.svg?style=svg)](<LINK>)
[![Clojars Project](http://clojars.org/clj-test-containers/latest-version.svg)](http://clojars.org/clj-test-containers)
## What it is
This library is a lightweight wrapper around the [Testcontainers Java library](https://www.testcontainers.org/).
## 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.
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.
## Integration with test runners
There is an [experimental kaocha plugin](https://github.com/lambdaschmiede/kaocha-testcontainers-plugin) you can try out
## Usage
The library provides a set of functions to interact with the testcontainers. A simple example, how to create a container
with a Docker label, could look like this:
The library provides a set of functions to interact with the testcontainers. A simple example, how to create a container with a Docker label, could look like this:
```clojure
(require '[clj-test-containers.core :as tc])
@ -29,7 +24,7 @@ with a Docker label, could look like this:
(tc/bind-filesystem! {:host-path "/tmp"
:container-path "/opt"
:mode :read-only})
(tc/start!)))
(tc/start!))
(do-database-testing (:host container)
(get (:mapped-ports container) 5432))
@ -46,7 +41,7 @@ If you'd rather create a container from a Dockerfile in your project, it could l
(def container (-> (tc/create-from-docker-file {:env-vars {"FOO" "bar"}
:exposed-ports [80]
:docker-file "resources/Dockerfile"})
(tc/start!)))
(tc/start!))
```
If you prefer to use prebuilt containers from the Testcontainers project, you can do it like this
@ -57,27 +52,26 @@ If you prefer to use prebuilt containers from the Testcontainers project, you ca
(def container (-> (tc/init {:container (PostgreSQLContainer. "postgres:12.2")
:exposed-ports [5432]})
(tc/start!)))
(tc/start!))
```
## Functions and Properties
### create
Creates a testcontainers instance from a given Docker label and returns them
#### Config parameters:
| 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 | The start command of the container |
| `:network` | Map | A map containing the configuration of a Docker Network (see: `create-network`) |
| `:network-aliases` | Map | A list of alias names for the container on the network |
| `:wait-for` | Map | A map containing the [wait strategy](doc/wait-strategies.md) to use and the condition to check for |
| `:log-to` | Map | A map containing the [log strategy](doc/log-strategies.md) to use, e.g. {:log-strategy string} |
| `:wait-for` | Map | A map containing the wait strategy to use and the condition to check for |
| `:log-to` | Map | A map containing the log strategy to use, e.g. {:log-strategy string} |
#### Result:
@ -118,27 +112,25 @@ Creates a testcontainers instance from a given Docker label and returns them
```
### init
Initializes a given Testcontainer, which was e.g. provided by a library
#### Config parameters:
| Key | Type | Description |
| ------------- | :------------- |:----------------------------------------------------------------------------------------------------|
| ------------- | :------------- | :----- |
| `:container` | `org.testcontainers.containers.GenericContainer`, 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 | The start command of the container |
| `:network` | Map | A map containing the configuration of a Docker Network (see: `create-network`) |
| `:network-aliases` | Map | A list of alias names for the container on the network |
| `:wait-for` | Map | A map containing the [wait strategy](doc/wait-strategies.md) to use and the condition to check for |
| `:log-to` | Map | A map containing the [log strategy](doc/log-strategies.md) to use, e.g. {:log-strategy string} |
| `:wait-for` | Map | A map containing the wait strategy to use and the condition to check for |
| `:log-to` | Map | A map containing the log strategy to use, e.g. {:log-strategy string} |
| | | |
#### Result:
| 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 |
@ -172,7 +164,6 @@ Initializes a given Testcontainer, which was e.g. provided by a library
```
### create-from-docker-file
Creates a testcontainer from a Dockerfile
#### Config parameters:
@ -185,10 +176,9 @@ Creates a testcontainer from a Dockerfile
| `:command` | Vector with strings | The start command of the container |
| `:network` | Map | A map containing the configuration of a Docker Network (see: `create-network`) |
| `:network-aliases` | Map | A list of alias names for the container on the network |
| `:wait-for` | Map | A map containing the [wait strategy](doc/wait-strategies.md) to use and the condition to check for |
| `:log-to` | Map | A map containing the [log strategy](doc/log-strategies.md) to use, e.g. {:log-strategy string} |
| `:wait-for` | Map | A map containing the wait strategy to use and the condition to check for |
| `:log-to` | Map | A map containing the log strategy to use, e.g. {:log-strategy string} |
| | | |
#### Result:
| Key | Type | Description |
@ -214,7 +204,6 @@ Creates a testcontainer from a Dockerfile
---
### start!
Starts the Testcontainer, which was defined by `create`
#### Config parameters:
@ -226,7 +215,6 @@ Starts the Testcontainer, which was defined by `create`
| | | |
#### Result:
| Key | Type | Description |
| ------------- | :------------- | :----- |
| `:container` | `org.testcontainers.containers.Container` | The Testcontainers instance, accessible for everything this library doesn't provide (yet) |
@ -241,15 +229,15 @@ Starts the Testcontainer, which was defined by `create`
```clojure
(def container (create {:image-name "alpine:3.2"
:exposed-ports [80]
:env-vars {"MAGIC_NUMBER" "42"}}))
:env-vars {"MAGIC_NUMBER" "42"})
(start! container)
```
---
### stop!
### stop!
Stops the Testcontainer, which was defined by `create`
#### Config parameters:
@ -260,7 +248,6 @@ Stops the Testcontainer, which was defined by `create`
| `container-config` | Map, mandatory | Return value of the `create` function |
#### Result:
The `container-config`
#### Example:
@ -268,7 +255,7 @@ The `container-config`
```clojure
(def container (create {:image-name "alpine:3.2"
:exposed-ports [80]
:env-vars {"MAGIC_NUMBER" "42"}}))
:env-vars {"MAGIC_NUMBER" "42"})
(start! container)
(stop! container)
@ -276,8 +263,8 @@ The `container-config`
---
### map-classpath-resource!
### map-classpath-resource!
Maps a resource from your classpath into the containers file system
#### Config parameters:
@ -291,8 +278,8 @@ Maps a resource from your classpath into the containers file system
| `:container-path` | String, mandatory | Path, to which the resource should be mapped |
| `:mode` | Keyword, mandatory | `:read-only` or `:read-write` |
#### Result:
#### Result:
The `container-config`
#### Example:
@ -305,8 +292,8 @@ The `container-config`
---
### bind-filesystem!
### bind-filesystem!
Binds a path from your local filesystem into the Docker container as a volume
#### Config parameters:
@ -320,8 +307,8 @@ Binds a path from your local filesystem into the Docker container as a volume
| `:container-path` | String, mandatory | Path, to which the resource should be mapped |
| `:mode` | Keyword, mandatory | `:read-only` or `:read-write` |
#### Result:
#### Result:
The `container-config`
#### Example:
@ -334,8 +321,8 @@ The `container-config`
---
### copy-file-to-container!
### copy-file-to-container!
Copies a file from your filesystem or classpath into the running container
#### Config parameters:
@ -346,11 +333,11 @@ Copies a file from your filesystem or classpath into the running container
| `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 |
| `:container-path` | String, mandatory | Path, to which the file should be copied |
| `:host-path` | String, mandatory | Path, to which the file should be copied |
| `:type` | Keyword, mandatory | `:classpath-resource` or `:host-path` |
#### Result:
#### Result:
The `container-config`
#### Example:
@ -363,8 +350,8 @@ The `container-config`
---
### execute-command!
### execute-command!
Executes a command in the running container, and returns the result
#### Config parameters:
@ -376,8 +363,9 @@ Executes a command in the running container, and returns the result
| Second parameter: |   |   |
| `command` | Vector with Strings, mandatory | A vector containing the command and its parameters |
#### Result:
#### Result:
| Key | Type | Description |
| ------------- | :------------- | :----- |
| `:exit-code` | int | Exit code of the executed command |
@ -391,9 +379,9 @@ Executes a command in the running container, and returns the result
```
### create-network
Creates a network. The optional map accepts config values for enabling ipv6 and setting the driver
#### Config parameters:
| Key | Type | Description |
@ -401,8 +389,9 @@ Creates a network. The optional map accepts config values for enabling ipv6 and
| `:ipv6` | boolean | Should the network enable IPv6? |
| `:driver` | String | The network driver used by Docker, e.g. `bridge` or `host` |
#### Result:
#### Result:
| Key | Type | Description |
| ------------- | :------------- | :----- |
| `:network` | `org.testcontainers.containers.Network` | The instance of the network |
@ -415,41 +404,34 @@ Creates a network. The optional map accepts config values for enabling ipv6 and
```clojure
;;Create with config
(create-network {:ipv6 false
:driver "overlay"})
:driver "overlay")
;;Create with default config
(create-network)
```
### perform-cleanup!
Stops and removes all containers which were created in the JVM, including the REPL session you are in. This is helpful,
if you are exploring functionality with containers in the REPL, and create lots of instances on the fly without stopping
them. Testcontainers will remove all containers upon JVM shutdown, but the REPL keeps the JVM alive for a long time.
Stops and removes all containers which were created in the JVM, including the REPL session you are in. This is helpful, if you are exploring functionality with containers in the REPL, and create lots of instances on the fly without stopping them. Testcontainers will remove all containers upon JVM shutdown, but the REPL keeps the JVM alive for a long time.
#### Config parameters:
None
#### Result:
None
#### Example:
```clojure
(perform-cleanup!)
```
### dump-logs
Call on a started container. Provided logging was enabled for a container, returns the given log presentation, e.g. as a
string
Call on a started container.
Provided logging was enabled for a container, returns the given log presentation, e.g. as a string
| Key | Type | Description |
| ------------- | :------------- | :----- |
| `container-config` | Map, mandatory | The configuration describing the container for which the log should be retrieved |
## License
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.
Distributed under the Eclipse Public License either version 1.0 or (at
your option) any later version.

View file

@ -1,27 +1,26 @@
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.10.3"}
org.testcontainers/testcontainers {:mvn/version "1.19.7"}}
:deps {org.clojure/clojure {:mvn/version "1.10.1"}
org.testcontainers/testcontainers {:mvn/version "1.15.0"}}
:aliases {:dev {:extra-paths "dev-src"}
:test {:extra-paths ["test" "test/resources"]
:extra-deps {expound/expound {:mvn/version "0.9.0"}
lambdaisland/kaocha {:mvn/version "1.88.1376"}
lambdaisland/kaocha-cloverage {:mvn/version "1.1.89"}
lambdaisland/kaocha-junit-xml {:mvn/version "1.17.101"}
org.clojure/test.check {:mvn/version "1.1.1"}
org.testcontainers/postgresql {:mvn/version "1.19.7"}}}
:extra-deps {expound/expound {:mvn/version "0.8.5"}
lambdaisland/kaocha {:mvn/version "1.0.700"}
lambdaisland/kaocha-cloverage {:mvn/version "1.0.63"}
lambdaisland/kaocha-junit-xml {:mvn/version "0.0.76"}
org.clojure/test.check {:mvn/version "1.1.0"}
org.testcontainers/postgresql {:mvn/version "1.14.3"}}}
:test-runner {:extra-paths ["test" "test/resources"]
:extra-deps {expound/expound {:mvn/version "0.9.0"}
lambdaisland/kaocha {:mvn/version "1.88.1376"}
lambdaisland/kaocha-cloverage {:mvn/version "1.1.89"}
lambdaisland/kaocha-junit-xml {:mvn/version "1.17.101"}
org.clojure/test.check {:mvn/version "1.1.1"}
orchestra {:mvn/version "2021.01.01-1"}
org.testcontainers/postgresql {:mvn/version "1.19.7"}}
:extra-deps {expound/expound {:mvn/version "0.8.5"}
lambdaisland/kaocha {:mvn/version "1.0.700"}
lambdaisland/kaocha-cloverage {:mvn/version "1.0.63"}
lambdaisland/kaocha-junit-xml {:mvn/version "0.0.76"}
org.clojure/test.check {:mvn/version "1.1.0"}
org.testcontainers/postgresql {:mvn/version "1.14.3"}}
:main-opts ["-m" "kaocha.runner" "--reporter" "kaocha.report/documentation"]}
:cljstyle {:extra-deps {mvxcvi/cljstyle {:mvn/version "0.16.630"
:cljstyle {:extra-deps {mvxcvi/cljstyle {:mvn/version "0.13.0"
:exclusions [org.clojure/clojure]}}
:main-opts ["-m" "cljstyle.main" "check"]}}}

View file

@ -1,26 +1,15 @@
# Introduction
## Who is this library for?
This library is meant for Clojure developers who want to write integration tests without having to worry about the
infrastructure of the application.
This library is meant for Clojure developers who want to write integration tests without having to worry about the infrastructure of the application.
## What are Testcontainers?
Depending on the complexity of your application, setting up the infrastructure for integration tests is not a simple
task. Even if you only need a single database for the integration tests, you need to make it available to every system
that executes the tests. But often, one database is not enough and you need to integrate with Webservices, Message
Queues, Search Indexes, Caches… Testcontainers try to solve this problem: Very simply put, the testcontainers Java
library provides an interface to interact with Docker and enables developers to easily bring up Docker containers for
executing tests, and tearing them down again, afterwards. See more
at [https://www.testcontainers.org/](https://www.testcontainers.org/)
Depending on the complexity of your application, setting up the infrastructure for integration tests is not a simple task. Even if you only need a single database for the integration tests, you need to make it available to every system that executes the tests. But often, one database is not enough and you need to integrate with Webservices, Message Queues, Search Indexes, Caches… Testcontainers try to solve this problem: Very simply put, the testcontainers Java library provides an interface to interact with Docker and enables developers to easily bring up Docker containers for executing tests, and tearing them down again, afterwards. See more at [https://www.testcontainers.org/](https://www.testcontainers.org/)
## Why do we need a Clojure wrapper?
As Testcontainers is a Java library, we do not *need* a Clojure wrapper to work with it. It is completely possible to use it directly via Java interop code:
As Testcontainers is a Java library, we do not *need* a Clojure wrapper to work with it. It is completely possible to
use it directly via Java interop code:
```clojure
```java
(-> (org.testcontainers.containers.GenericContainer. "postgres:12.2")
(.withExposedPorts (into-array Integer [(int 5432)]))
(.withEnv "POSTGRES_PASSSWORD" pw)

View file

@ -1,34 +0,0 @@
# Log strategies
This library offers two ways to access the logs of the running container: The :string strategy and the :fn strategy.
## String Strategy
The `:string` strategy sets up a function in the returned map, under the `:log` key. This function enables the
dumping of the logs when passed to the `dump-logs` function.
Example:
```clojure
{:log-to {:log-strategy :string}}
```
Then, later in your program, you can access the logs thus:
```clojure
(def container-config (tc/start! container))
(tc/dump-logs container-config)
```
## Function Strategy
The `:fn` strategy accepts an additional parameter `:function` in the configuration map, which allows you to pass a
function to the Testcontainers log mechanism which accepts a single String parameter and gets called for every log line.
This way you can pass the container logging on to the logging library of your choice.
Example:
```clojure
{:log-to {:log-strategy :fn
:function (fn [log-line] (println "From Container: " log-line))}}
```

View file

@ -2,8 +2,7 @@
The list of functions included in the library until now is as following:
* `create`: Creates a new Testcontainers instance, accepts parameters for mapped ports, environment variables and a
start command
* `create`: Creates a new Testcontainers instance, accepts parameters for mapped ports, environment variables and a start command
* `map-classpath-resource!`: Maps a resource from your classpath into the containers file system
* `bind-filesystem!`: Binds a path from your local filesystem into the Docker container as a volume
* `start!`: Starts the container
@ -11,8 +10,7 @@ The list of functions included in the library until now is as following:
* `copy-file-to-container!`: Copies a file from your filesystem or classpath into the running container
* `execute-command!`: Executes a command in the running container, and returns the result
The functions accept and return a map structure, which enables us to operate them on the same data structure in a
consistent way. The example shown with Java Interop above would look like this, when using the wrapped functions:
The functions accept and return a map structure, which enables us to operate them on the same data structure in a consistent way. The example shown with Java Interop above would look like this, when using the wrapped functions:
```clojure
(require '[clj-test-containers.core :as tc])
@ -37,9 +35,7 @@ consistent way. The example shown with Java Interop above would look like this,
## Executing commands inside the container
The `execute-command` function enables us to run commands inside the container. The function accepts a container and a
vector of strings as parameters, with the first string being the command, followed by potential parameters. The function
returns a map with an `:exit-code`, `:stdout` and `:stderr`:
The `execute-command` function enables us to run commands inside the container. The function accepts a container and a vector of strings as parameters, with the first string being the command, followed by potential parameters. The function returns a map with an `:exit-code`, `:stdout` and `:stderr`:
```clojure
(execute-command! container ["whoami"])
@ -50,9 +46,7 @@ returns a map with an `:exit-code`, `:stdout` and `:stderr`:
## Mounting files into the container
For some test scenarios it can be helpful to mount files from your filesystem or the resource path of your application
into the container, before it is started. This could be helpful if you want to load a dumpfile into your database,
before executing the tests. You can do this with the functions `map-classpath-resource!` and `bind-filesystem!`:
For some test scenarios it can be helpful to mount files from your filesystem or the resource path of your application into the container, before it is started. This could be helpful if you want to load a dumpfile into your database, before executing the tests. You can do this with the functions `map-classpath-resource!` and `bind-filesystem!`:
```clojure
(map-classpath-resource! container
@ -76,23 +70,20 @@ It is also possible to copy files into a running container instance:
```
# Fixtures for Clojure Test
The above example creates a Testcontainers instance in the test function itself. If we did this for all of our integration tests, this would spin up a docker image for every test function, and tear it down again, afterwards. If we want to create one image for all tests in the same namespace, we can use Clojures [`use-fixtures`](https://clojuredocs.org/clojure.test/use-fixtures) function, which is described like this:
The above example creates a Testcontainers instance in the test function itself. If we did this for all of our
integration tests, this would spin up a docker image for every test function, and tear it down again, afterwards. If we
want to create one image for all tests in the same namespace, we can use
Clojures [`use-fixtures`](https://clojuredocs.org/clojure.test/use-fixtures) function, which is described like this:
> Wrap test runs in a fixture function to perform setup and
teardown. Using a fixture-type of :each wraps every test
individually, while :once wraps the whole run in a single function.
> Wrap test runs in a fixture function to perform setup and teardown. Using a fixture-type of :each wraps every test individually, while :once wraps the whole run in a single function.
Assuming we have a function `initialize-db!` in our application which sets up a JDBC connection and stores it in an
atom, a fixture for Testcontainers could look like this:
Assuming we have a function `initialize-db!` in our application which sets up a JDBC connection and stores it in an atom, a fixture for Testcontainers could look like this:
```clojure
(use-fixtures :once (fn [f]
(let [pw "apassword"
(let [{pw "apassword"
postgres (tc/start! (tc/create {:image-name "postgres:12.2"
:exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" pw}}))]
:env-vars {"POSTGRES_PASSWORD" pw}}))}]
(my-app/initialize-db! {:dbtype "postgresql"
:dbname "postgres"
:user "postgres"

View file

@ -1,97 +0,0 @@
# Wait strategies
Testcontainers provides a set of wait strategies which help us determine if and when a container is ready to accept
requests. Wait strategies are defined for the `:wait-for` key in the container configuration with the
key `:wait-strategy` determining which strategy to select. The `start!` function will block until the container is ready
and continue processing afterwards.
## HTTP Wait Strategy
The HTTP wait strategy will perform an HTTP call according to the following configuration and only continue if the
criteria is met.
| Parameter | Type | Description |
|-------------------|------------|---------------------------------------------------------|
| wait-strategy | Keyword | :http |
| path | String | The HTTP path to access |
| port | int | The HTTP port to access |
| method | String | The HTTP method to use (get, post, put...) |
| status-codes | seq of int | The status codes which mark a successful request |
| tls | boolean | Should TLS be used? |
| allow-insecure | boolean | Should insecure HTTP endpoints be trusted? |
| read-timeout | long | The duration in seconds the HTTP may take |
| basic-credentials | Map | {:username "User" :password "Password"} |
| headers | Map | HTTP Headers, e.g. {"Accept" "application/json"} |
| startup-timeout | long | The duration in seconds the container may take to start |
Example:
```clojure
(tc/create {:image-name "alpine:3.5"
:command ["/bin/sh"
"-c"
"while true ; do printf 'HTTP/1.1 200 OK\\n\\nyay' | nc -l -p 8080; done"]
:exposed-ports [8080]
:wait-for {:wait-strategy :http
:path "/"
:port 8080
:method "GET"
:status-codes [200]
:tls false
:read-timout 5
:headers {"Accept" "text/plain"}
:startup-timeout 20}})
```
## Health Wait Strategy
The Health Wait Strategy uses a health check defined in the Dockerfile to determine if the container is ready.
| Parameter | Type | Description |
|-----------------|---------|---------------------------------------------------------|
| wait-strategy | Keyword | :port |
| startup-timeout | long | The duration in seconds the container may take to start |
```clojure
(tc/create {:image-name "alpine:3.5"
:exposed-ports [8080]
:wait-for {:wait-strategy :health
:startup-timeout 20}})
```
## Log Wait Strategy
The Log Wait Strategy waits until a certain phrase appears in the Docker containers' log.
| Parameter | Type | Description |
|-----------------|---------|---------------------------------------------------------|
| wait-strategy | Keyword | :log |
| message | String | A substring of an expected line in the containers log |
| times | int | The number of times the predicate has to match |
| startup-timeout | long | The duration in seconds the container may take to start |
```clojure
(tc/create {:image-name "postgres:12.2"
:exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}
:wait-for {:wait-strategy :log
:message "accept connections"
:startup-timeout 10}})
```
## Port Wait Strategy
This strategy is the default selcted by Testcontainers if no wait strategy was defined. It waits for the first port in
the containers port mapping to be opened. It does not accept any parameters beside the `startup-timeout`
| Parameter | Type | Description |
|-----------------|---------|---------------------------------------------------------|
| wait-strategy | Keyword | :port |
| startup-timeout | long | The duration in seconds the container may take to start |
```clojure
(tc/create {:image-name "postgres:12.2"
:exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}
:wait-for {:wait-strategy :port}})
```

View file

@ -1,34 +1,29 @@
(defproject testcontainers-clj "unspecified"
:description "A lightweight, official wrapper around the Testcontainers Java library"
(defproject clj-test-containers "0.4.0"
:description "A lightweight, unofficial wrapper around the Testcontainers Java library"
:url "https://github.com/testcontainers/testcontainers-clj"
:url "https://github.com/javahippie/clj-test-containers"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.10.3"]
[org.testcontainers/testcontainers "1.19.7"]]
:dependencies [[org.clojure/clojure "1.10.1"]
[org.testcontainers/testcontainers "1.15.1"]]
:aliases {"test" ["run" "-m" "kaocha.runner"]
"cljstyle" ["run" "-m" "cljstyle.main"]}
:plugins [[jainsahab/lein-githooks "1.0.0"]]
:profiles {:dev {:dependencies [[expound "0.9.0"]
[lambdaisland/kaocha "1.88.1376"]
[lambdaisland/kaocha-cloverage "1.1.89"]
[lambdaisland/kaocha-junit-xml "1.17.101"]
[mvxcvi/cljstyle "0.16.630" :exclusions [org.clojure/clojure]]
[org.clojure/test.check "1.1.1"]
[orchestra "2021.01.01-1"]
[org.clojure/tools.namespace "1.5.0"]
[org.testcontainers/postgresql "1.19.7"]
[com.fzakaria/slf4j-timbre "0.4.1"]
[nrepl "1.0.0"]]
:source-paths ["dev-src"]}
:release {:deploy-repositories [["maven" {:url "https://oss.sonatype.org/service/local/staging/deploy/maven2"
:username :env/ossrh_username
:password :env/ossrh_password
:sign-releases false}]]}}
:profiles {:dev {:dependencies [[expound "0.8.5"]
[lambdaisland/kaocha "1.0.641"]
[lambdaisland/kaocha-cloverage "1.0-45"]
[lambdaisland/kaocha-junit-xml "0.0.76"]
[lambdaisland/kaocha-junit-xml "0.0.76"]
[mvxcvi/cljstyle "0.14.0" :exclusions [org.clojure/clojure]]
[org.clojure/test.check "1.1.0"]
[org.clojure/tools.namespace "1.0.0"]
[org.testcontainers/postgresql "1.15.1"]
[com.fzakaria/slf4j-timbre "0.3.20"]]
:source-paths ["dev-src"]}}
:target-path "target/%s")

View file

@ -6,16 +6,11 @@
(:import
(java.nio.file
Paths)
(java.time
Duration)
(org.testcontainers DockerClientFactory)
(org.testcontainers.containers
BindMode
GenericContainer
Network)
(org.testcontainers.containers.output
BaseConsumer
OutputFrame
ToStringConsumer)
(org.testcontainers.containers.wait.strategy
Wait)
@ -30,7 +25,9 @@
BindMode/READ_WRITE
BindMode/READ_ONLY)))
(defonce started-instances (atom #{}))
(defn- reaper-instance
[]
(org.testcontainers.utility.ResourceReaper/instance))
(defmulti wait
"Sets a wait strategy to the container. Supports :http, :health and :log as
@ -53,20 +50,19 @@
:tls true
:read-timeout 5
:basic-credentials {:username \"user\"
:password \"password\"
:startup-timeout 60}}
:password \"password\"}}
container)
```
## Health Strategy
The :health strategy enables support for Docker's healthcheck feature,
whereby you can directly leverage the healthy state of your container as your wait condition.
The :health strategy only accepts a true or false value. This enables support
for Docker's healthcheck feature, whereby you can directly leverage the
healthy state of your container as your wait condition.
Example:
```clojure
(wait {:wait-strategy :health
:startup-timeout 60} container)
(wait {:wait-strategy :health :true} container)
```
## Log Strategy
@ -78,101 +74,50 @@
```clojure
(wait {:wait-strategy :log
:message \"accept connections\"
:startup-timeout 60} container)
```
## Port Strategy
The port strategy waits for the first of the mapped ports to be opened. It only accepts the startup-timeout
value as a parameter.
Example:
```clojure
(wait {:wait-strategy :port
:startup-timeout 60} container
:message \"accept connections\"} container)
```"
:wait-strategy)
(defmethod wait :http
[{:keys [path
port
method
status-codes
tls
allow-insecure
read-timeout
basic-credentials
headers
startup-timeout] :as options}
basic-credentials] :as options}
^GenericContainer container]
(let [for-http (Wait/forHttp path)]
(when port
(.forPort for-http port))
(when method
(.withMethod for-http method))
(doseq [status-code status-codes]
(.forStatusCode for-http status-code))
(when tls
(.usingTls for-http))
(when allow-insecure
(.allowInsecure for-http))
(when read-timeout
(.withReadTimeout for-http (Duration/ofSeconds read-timeout)))
(.withReadTimeout for-http (java.time.Duration/ofSeconds read-timeout)))
(when basic-credentials
(let [{:keys [username password]} basic-credentials]
(let [{username :username password :password} basic-credentials]
(.withBasicCredentials for-http username password)))
(when headers
(.withHeaders for-http headers))
(when startup-timeout
(.withStartupTimeout for-http (Duration/ofSeconds startup-timeout)))
(.waitingFor container for-http)
{:wait-for-http (dissoc options :strategy)}))
(defmethod wait :health
[{:keys [startup-timeout]} ^GenericContainer container]
(let [strategy (Wait/forHealthcheck)]
(when startup-timeout
(.withStartupTimeout strategy (Duration/ofSeconds startup-timeout)))
(.waitingFor container strategy))
[_ ^GenericContainer container]
(.waitingFor container (Wait/forHealthcheck))
{:wait-for-healthcheck true})
(defmethod wait :log
[{:keys [message times startup-timeout]} ^GenericContainer container]
(let [log-message (str ".*" message ".*\\n")
strategy (Wait/forLogMessage log-message 1)]
(when times
(.withTimes strategy times))
(when startup-timeout
(.withStartupTimeout strategy (Duration/ofSeconds startup-timeout)))
(.waitingFor container strategy)
[{:keys [message]} ^GenericContainer container]
(let [log-message (str ".*" message ".*\\n")]
(.waitingFor container (Wait/forLogMessage log-message 1))
{:wait-for-log-message log-message}))
(defmethod wait :port
[{:keys [startup-timeout]} ^GenericContainer container]
(let [strategy (Wait/forListeningPort)]
(when startup-timeout
(.withStartupTimeout strategy (Duration/ofSeconds startup-timeout)))
(.waitingFor container strategy))
{:wait-for-port true})
(defmethod wait :default [_ _] nil)
(s/fdef init
@ -282,10 +227,11 @@
:stderr (.getStderr result)}))
(defmulti log
"Sets a log strategy on the container as a means of accessing the container logs.
"Sets a log strategy on the container as a means of accessing the container
logs. It currently only supports a :string as the strategy to use.
## String Strategy
The `:string` strategy sets up a function in the returned map, under the
The :string strategy sets up a function in the returned map, under the
`string-log` key. This function enables the dumping of the logs when passed to
the `dump-logs` function.
@ -300,18 +246,6 @@
```clojure
(def container-config (tc/start! container))
(tc/dump-logs container-config)
```
## Function Strategy
The `:fn` strategy accepts an additional parameter `:function` in the configuration
map, which allows you to pass a function to the Testcontainers log mechanism
which accepts a single String parameter and gets called for every log line. This
way you can pass the container logging on to the logging library of your choice.
Example:
```clojure
{:log-strategy :fn
:function (fn [log-line] (println \"From Container: \" log-line)}
```
"
:log-strategy)
@ -324,20 +258,14 @@
(-> (.toUtf8String to-string-consumer)
(clojure.string/replace #"\n+" "\n")))}))
(defmethod log :fn [{:keys [function]} ^GenericContainer container]
(.followOutput container (proxy [BaseConsumer] []
(^void accept [^OutputFrame frame]
(function (.getUtf8String frame))))))
(defmethod log :slf4j [_ _] nil) ;; Not yet implemented
(defmethod log :default [_ _] nil)
(defmethod log :default [_ _] nil) ;; Not yet implemented
(defn dump-logs
"Dumps the logs found by invoking the function on the :string-log key"
[container-config]
(let [log-fn (:log container-config)]
(if (some? log-fn)
(log-fn)
(throw (IllegalStateException. "You are trying to access the container logs, but have not configured a log configuration with :log-to")))))
((:log container-config)))
(defn start!
"Starts the underlying testcontainer instance and adds new values to the
@ -350,10 +278,12 @@
[port]
[port (.getMappedPort container port)])
mapped-ports (into {} (map map-port) exposed-ports)
container-id ^String (.getContainerId container)
image-name ^String (.getDockerImageName container)
container-id (.getContainerId container)
image-name (.getDockerImageName container)
logger (log log-to container)]
(swap! started-instances conj {:type :container :id container-id})
(.registerContainerForCleanup (reaper-instance)
container-id
image-name)
(-> container-config
(merge {:id container-id
:mapped-ports mapped-ports
@ -387,7 +317,7 @@
(let [network (.build builder)
network-name (.getName network)]
(swap! started-instances conj {:type :network :id :network-name})
(.registerNetworkIdForCleanup (reaper-instance) network-name)
{:network network
:name network-name
:ipv6 (.getEnableIpv6 network)
@ -395,35 +325,13 @@
(def ^:deprecated init-network create-network)
(defn- remove-network! [instance]
(-> (DockerClientFactory/instance)
(.client)
(.removeNetworkCmd (:id instance))
(.exec))
instance)
(defn- stop-and-remove-container! [instance]
(let [docker-client (DockerClientFactory/instance)]
(-> docker-client
(.client)
(.stopContainerCmd (:id instance))
(.exec))
(-> docker-client
(.client)
(.removeContainerCmd (:id instance))
(.exec)))
instance)
(defn perform-cleanup!
"Stops and removes all container instances which were created in the active JVM or REPL session"
[]
(for [instance @started-instances]
(swap! started-instances disj (case (:type instance)
:container (stop-and-remove-container! instance)
:network (remove-network! instance)))))
(.performCleanup (reaper-instance)))
;; REPL Helpers
;;; REPL Helpers
(comment
(start! (create {:image-name "postgres:12.1"}))
(perform-cleanup!))

View file

@ -35,7 +35,7 @@
(s/def ::log
keyword?)
(s/def ::wait-strategy #{:http :health :port :log})
(s/def ::wait-strategy #{:http :health :log})
(s/def ::log-strategy #{:string})

View file

@ -9,7 +9,7 @@
(deftest create-test
(testing "Testing basic testcontainer generic image initialisation"
(let [container (sut/create {:image-name "postgres:15.3"
(let [container (sut/create {:image-name "postgres:12.2"
:exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}})
initialized-container (sut/start! container)
@ -20,35 +20,18 @@
(is (nil? (:id stopped-container)))
(is (nil? (:mapped-ports stopped-container)))))
(testing "Testing log access to the container with string logs"
(let [container (sut/create {:image-name "postgres:15.3"
:exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}
(testing "Testing log access to the container"
(let [container (sut/init {:container (PostgreSQLContainer. "postgres:12.2")
:log-to {:log-strategy :string}})
initialized-container (sut/start! container)]
(Thread/sleep 500)
(is (includes? (sut/dump-logs initialized-container) "database system is ready to accept connections"))))
(testing "Testing log access to the container with function logs"
(let [logs (atom [])]
(sut/start! (sut/init {:container (PostgreSQLContainer. "postgres:15.3")
:exposed-ports [5432]
:log-to {:log-strategy :fn
:function #(swap! logs conj %)}}))
(is (filter #(includes? "database system is ready to accept connections" %) @logs))))
(testing "Testing log access to the container with unconfigured logger"
(let [container (sut/start! (sut/init {:container (PostgreSQLContainer. "postgres:15.3")
:exposed-ports [5432]}))]
(is (thrown? IllegalStateException (sut/dump-logs container)))))
(testing "Testing basic testcontainer generic image initialisation with wait for log message"
(let [container (sut/create {:image-name "postgres:15.3"
(let [container (sut/create {:image-name "postgres:12.2"
:exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}
:wait-for {:wait-strategy :log
:message "accept connections"
:startup-timeout 10}})
:wait-for {:wait-strategy :log :message "accept connections"}})
initialized-container (sut/start! container)
stopped-container (sut/stop! container)]
(is (some? (:id initialized-container)))
@ -58,44 +41,6 @@
(is (nil? (:id stopped-container)))
(is (nil? (:mapped-ports stopped-container)))))
(testing "Testing basic testcontainer generic image initialisation with wait for host port"
(let [container (sut/create {:image-name "postgres:15.3"
:exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}
:wait-for {:wait-strategy :port}})
initialized-container (sut/start! container)
stopped-container (sut/stop! container)]
(is (some? (:id initialized-container)))
(is (some? (:mapped-ports initialized-container)))
(is (some? (get (:mapped-ports initialized-container) 5432)))
(is (= (:wait-for-port initialized-container) true))
(is (nil? (:id stopped-container)))
(is (nil? (:mapped-ports stopped-container)))))
(testing "Testing basic testcontainer generic image initialisation with wait for http"
(let [container (sut/create {:image-name "bitnami/nginx:1.22"
:network-aliases ["foo"]
:exposed-ports [8080]
:wait-for {:wait-strategy :http
:path "/"
:port 8080
:method "GET"
:status-codes [200]
:headers {"Accept" "text/plain"}}})
initialized-container (sut/start! container)
stopped-container (sut/stop! container)]
(is (some? (:id initialized-container)))
(is (some? (:mapped-ports initialized-container)))
(is (some? (get (:mapped-ports initialized-container) 8080)))
(is (= (:wait-for-http initialized-container) {:headers {"Accept" "text/plain"}
:method "GET"
:path "/"
:port 8080
:status-codes [200]
:wait-strategy :http}))
(is (nil? (:id stopped-container)))
(is (nil? (:mapped-ports stopped-container)))))
(testing "Testing basic testcontainer image creation from docker file"
(let [container (sut/create-from-docker-file {:exposed-ports [80]
:docker-file "test/resources/Dockerfile"})
@ -109,8 +54,7 @@
(testing "Executing a command in the running Docker container with a custom container"
(let [container (sut/init {:container (PostgreSQLContainer. "postgres:15.3")
:exposed-ports [5432]})
(let [container (sut/init {:container (PostgreSQLContainer. "postgres:12.2")})
initialized-container (sut/start! container)
result (sut/execute-command! initialized-container ["whoami"])
_stopped-container (sut/stop! container)]
@ -120,7 +64,7 @@
(deftest execute-command-in-container
(testing "Executing a command in the running Docker container"
(let [container (sut/create {:image-name "postgres:15.3"
(let [container (sut/create {:image-name "postgres:12.2"
:exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}})
initialized-container (sut/start! container)
@ -132,7 +76,7 @@
(deftest init-volume-test
(testing "Testing mapping of a classpath resource"
(let [container (-> (sut/create {:image-name "postgres:15.3"
(let [container (-> (sut/create {:image-name "postgres:12.2"
:exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}})
(sut/map-classpath-resource! {:resource-path "test.sql"
@ -149,7 +93,7 @@
(is (nil? (:mapped-ports stopped-container)))))
(testing "Testing mapping of a filesystem-binding"
(let [container (-> (sut/create {:image-name "postgres:15.3"
(let [container (-> (sut/create {:image-name "postgres:12.2"
:exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}})
(sut/bind-filesystem! {:host-path "."
@ -166,7 +110,7 @@
(is (nil? (:mapped-ports stopped-container)))))
(testing "Copying a file from the host into the container"
(let [container (-> (sut/create {:image-name "postgres:15.3"
(let [container (-> (sut/create {:image-name "postgres:12.2"
:exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}})
(sut/copy-file-to-container! {:path "test.sql"
@ -184,7 +128,7 @@
(testing "Copying a file from the classpath into the container"
(let [container (-> (sut/create {:image-name "postgres:15.3"
(let [container (-> (sut/create {:image-name "postgres:12.2"
:exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}})
(sut/copy-file-to-container! {:path "test.sql"
@ -201,7 +145,7 @@
(is (nil? (:mapped-ports stopped-container)))))
(testing "Copying a file from the host into a running container"
(let [container (sut/create {:image-name "postgres:15.3"
(let [container (sut/create {:image-name "postgres:12.2"
:exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}})
initialized-container (sut/start! container)
@ -220,7 +164,7 @@
(is (nil? (:mapped-ports stopped-container)))))
(testing "Copying a file from the classpath into a running container"
(let [container (sut/create {:image-name "postgres:15.3"
(let [container (sut/create {:image-name "postgres:12.2"
:exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}})
initialized-container (sut/start! container)