Compare commits
No commits in common. "main" and "0.4.0" have entirely different histories.
16 changed files with 416 additions and 801 deletions
49
.circleci/config.yml
Normal file
49
.circleci/config.yml
Normal 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
|
||||
18
.github/actions/setup-build/action.yml
vendored
18
.github/actions/setup-build/action.yml
vendored
|
|
@ -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
|
||||
22
.github/workflows/clojure.yml
vendored
22
.github/workflows/clojure.yml
vendored
|
|
@ -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
|
||||
28
.github/workflows/release.yml
vendored
28
.github/workflows/release.yml
vendored
|
|
@ -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
3
.gitignore
vendored
|
|
@ -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
|
||||
|
|
|
|||
42
CHANGELOG.md
42
CHANGELOG.md
|
|
@ -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
|
||||
|
|
|
|||
244
README.md
244
README.md
|
|
@ -1,35 +1,30 @@
|
|||
# clj-test-containers
|
||||
|
||||
[](<LINK>)
|
||||
|
||||
[](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])
|
||||
|
||||
(def container (-> (tc/create {:image-name "postgres:12.1"
|
||||
(def container (-> (tc/create {:image-name "postgres:12.1"
|
||||
:exposed-ports [5432]
|
||||
:env-vars {"POSTGRES_PASSWORD" "verysecret"}})
|
||||
(tc/bind-filesystem! {:host-path "/tmp"
|
||||
:env-vars {"POSTGRES_PASSWORD" "verysecret"}})
|
||||
(tc/bind-filesystem! {:host-path "/tmp"
|
||||
:container-path "/opt"
|
||||
:mode :read-only})
|
||||
(tc/start!)))
|
||||
:mode :read-only})
|
||||
(tc/start!))
|
||||
|
||||
(do-database-testing (:host container)
|
||||
(get (:mapped-ports container) 5432))
|
||||
|
|
@ -43,10 +38,10 @@ If you'd rather create a container from a Dockerfile in your project, it could l
|
|||
|
||||
(require '[clj-test-containers.core :as tc])
|
||||
|
||||
(def container (-> (tc/create-from-docker-file {:env-vars {"FOO" "bar"}
|
||||
:exposed-ports [80]
|
||||
:docker-file "resources/Dockerfile"})
|
||||
(tc/start!)))
|
||||
(def container (-> (tc/create-from-docker-file {:env-vars {"FOO" "bar"}
|
||||
:exposed-ports [80]
|
||||
:docker-file "resources/Dockerfile"})
|
||||
(tc/start!))
|
||||
```
|
||||
|
||||
If you prefer to use prebuilt containers from the Testcontainers project, you can do it like this
|
||||
|
|
@ -55,29 +50,28 @@ If you prefer to use prebuilt containers from the Testcontainers project, you ca
|
|||
(require '[clj-test-containers.core :as tc])
|
||||
(:import [org.testcontainers.containers PostgreSQLContainer])
|
||||
|
||||
(def container (-> (tc/init {:container (PostgreSQLContainer. "postgres:12.2")
|
||||
:exposed-ports [5432]})
|
||||
(tc/start!)))
|
||||
(def container (-> (tc/init {:container (PostgreSQLContainer. "postgres:12.2")
|
||||
:exposed-ports [5432]})
|
||||
(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} |
|
||||
| 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 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:
|
||||
|
||||
|
|
@ -93,52 +87,50 @@ Creates a testcontainers instance from a given Docker label and returns them
|
|||
#### Example:
|
||||
|
||||
```clojure
|
||||
(create {:image-name "alpine:3.2"
|
||||
:exposed-ports [80]
|
||||
:env-vars {"MAGIC_NUMBER" "42"}
|
||||
:network (create-network)
|
||||
(create {:image-name "alpine:3.2"
|
||||
:exposed-ports [80]
|
||||
:env-vars {"MAGIC_NUMBER" "42"}
|
||||
:network (create-network)
|
||||
:network-aliases ["api-server"]
|
||||
:command ["/bin/sh"
|
||||
"-c"
|
||||
"while true; do echo \"$MAGIC_NUMBER\" | nc -l -p 80; done"]})
|
||||
:command ["/bin/sh"
|
||||
"-c"
|
||||
"while true; do echo \"$MAGIC_NUMBER\" | nc -l -p 80; done"]})
|
||||
```
|
||||
|
||||
#### Example using wait-for and healthcheck:
|
||||
|
||||
```clojure
|
||||
(create {:image-name "alpine:3.2"
|
||||
:exposed-ports [80]
|
||||
:env-vars {"MAGIC_NUMBER" "42"}
|
||||
:network (create-network)
|
||||
(create {:image-name "alpine:3.2"
|
||||
:exposed-ports [80]
|
||||
:env-vars {"MAGIC_NUMBER" "42"}
|
||||
:network (create-network)
|
||||
:network-aliases ["api-server"]
|
||||
:wait-for {:strategy :health}
|
||||
:command ["/bin/sh"
|
||||
"-c"
|
||||
"while true; do echo \"$MAGIC_NUMBER\" | nc -l -p 80; done"]})
|
||||
:wait-for {:strategy :health}
|
||||
:command ["/bin/sh"
|
||||
"-c"
|
||||
"while true; do echo \"$MAGIC_NUMBER\" | nc -l -p 80; done"]})
|
||||
```
|
||||
|
||||
### 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} |
|
||||
| | | |
|
||||
|
||||
| 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 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 |
|
||||
|
|
@ -150,29 +142,28 @@ Initializes a given Testcontainer, which was e.g. provided by a library
|
|||
|
||||
```clojure
|
||||
;; PostgreSQL container needs a separate library! This is not included.
|
||||
(init {:container (org.testcontainers.containers.PostgreSQLContainer)
|
||||
(init {:container (org.testcontainers.containers.PostgreSQLContainer)
|
||||
:exposed-ports [80]
|
||||
:env-vars {"MAGIC_NUMBER" "42"}
|
||||
:command ["/bin/sh"
|
||||
"-c"
|
||||
"while true; do echo \"$MAGIC_NUMBER\" | nc -l -p 80; done"]})
|
||||
:env-vars {"MAGIC_NUMBER" "42"}
|
||||
:command ["/bin/sh"
|
||||
"-c"
|
||||
"while true; do echo \"$MAGIC_NUMBER\" | nc -l -p 80; done"]})
|
||||
```
|
||||
|
||||
#### Example using wait-for and a log message:
|
||||
|
||||
```clojure
|
||||
;; PostgreSQL container needs a separate library! This is not included.
|
||||
(init {:container (org.testcontainers.containers.PostgreSQLContainer)
|
||||
(init {:container (org.testcontainers.containers.PostgreSQLContainer)
|
||||
:exposed-ports [80]
|
||||
:env-vars {"MAGIC_NUMBER" "42"}
|
||||
:wait-for {:strategy :log :message "accept connections"}
|
||||
:command ["/bin/sh"
|
||||
"-c"
|
||||
"while true; do echo \"$MAGIC_NUMBER\" | nc -l -p 80; done"]})
|
||||
:env-vars {"MAGIC_NUMBER" "42"}
|
||||
:wait-for {:strategy :log :message "accept connections"}
|
||||
:command ["/bin/sh"
|
||||
"-c"
|
||||
"while true; do echo \"$MAGIC_NUMBER\" | nc -l -p 80; done"]})
|
||||
```
|
||||
|
||||
### 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 |
|
||||
|
|
@ -203,18 +193,17 @@ Creates a testcontainer from a Dockerfile
|
|||
#### Example:
|
||||
|
||||
```clojure
|
||||
(create-from-docker-file {:docker-file "resources/Dockerfile"
|
||||
(create-from-docker-file {:docker-file "resources/Dockerfile"
|
||||
:exposed-ports [5432]
|
||||
:env-vars {"MAGIC_NUMBER" "42"}
|
||||
:command ["/bin/sh"
|
||||
"-c"
|
||||
"while true; do echo \"$MAGIC_NUMBER\" | nc -l -p 80; done"]})
|
||||
: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:
|
||||
|
|
@ -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) |
|
||||
|
|
@ -239,17 +227,17 @@ Starts the Testcontainer, which was defined by `create`
|
|||
#### Example:
|
||||
|
||||
```clojure
|
||||
(def container (create {:image-name "alpine:3.2"
|
||||
(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,15 +248,14 @@ Stops the Testcontainer, which was defined by `create`
|
|||
| `container-config` | Map, mandatory | Return value of the `create` function |
|
||||
|
||||
#### Result:
|
||||
|
||||
The `container-config`
|
||||
|
||||
#### Example:
|
||||
|
||||
```clojure
|
||||
(def container (create {:image-name "alpine:3.2"
|
||||
(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,37 +263,37 @@ The `container-config`
|
|||
|
||||
---
|
||||
|
||||
### map-classpath-resource!
|
||||
|
||||
### map-classpath-resource!
|
||||
Maps a resource from your classpath into the containers file system
|
||||
|
||||
#### Config parameters:
|
||||
|
||||
| Key | Type | Description |
|
||||
| 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` |
|
||||
| `: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` |
|
||||
|
||||
|
||||
#### Result:
|
||||
|
||||
The `container-config`
|
||||
|
||||
#### Example:
|
||||
|
||||
```clojure
|
||||
(map-classpath-resource! container {:resource-path "test.sql"
|
||||
:container-path "/opt/test.sql"
|
||||
:mode :read-only})
|
||||
(map-classpath-resource! container {:resource-path "test.sql"
|
||||
:container-path "/opt/test.sql"
|
||||
:mode :read-only})
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### bind-filesystem!
|
||||
|
||||
### bind-filesystem!
|
||||
Binds a path from your local filesystem into the Docker container as a volume
|
||||
|
||||
#### Config parameters:
|
||||
|
|
@ -320,51 +307,51 @@ 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:
|
||||
|
||||
```clojure
|
||||
(bind-filesystem! container {:host-path "."
|
||||
(bind-filesystem! container {:host-path "."
|
||||
:container-path "/opt"
|
||||
:mode :read-only})
|
||||
:mode :read-only})
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### copy-file-to-container!
|
||||
|
||||
### copy-file-to-container!
|
||||
Copies a file from your filesystem or classpath into the running container
|
||||
|
||||
#### Config parameters:
|
||||
|
||||
| Key | Type | Description |
|
||||
| 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 |
|
||||
| `:container-path` | String, mandatory | Path, to which the file should be copied |
|
||||
| `:type` | Keyword, mandatory | `:classpath-resource` or `:host-path` |
|
||||
| `: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` |
|
||||
|
||||
|
||||
#### Result:
|
||||
|
||||
The `container-config`
|
||||
|
||||
#### Example:
|
||||
|
||||
```clojure
|
||||
(copy-file-to-container! container {:path "test.sql"
|
||||
(copy-file-to-container! container {:path "test.sql"
|
||||
:container-path "/opt/test.sql"
|
||||
:type :host-path})
|
||||
:type :host-path})
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 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 |
|
||||
|
|
@ -414,42 +403,35 @@ Creates a network. The optional map accepts config values for enabling ipv6 and
|
|||
|
||||
```clojure
|
||||
;;Create with config
|
||||
(create-network {:ipv6 false
|
||||
:driver "overlay"})
|
||||
(create-network {:ipv6 false
|
||||
: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:
|
||||
|
||||
#### Config parameters:
|
||||
None
|
||||
|
||||
#### Result:
|
||||
|
||||
#### 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.
|
||||
|
|
|
|||
43
deps.edn
43
deps.edn
|
|
@ -1,27 +1,26 @@
|
|||
{:paths ["src" "resources"]
|
||||
{: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"}}}
|
||||
:aliases {:dev {:extra-paths "dev-src"}
|
||||
:test {:extra-paths ["test" "test/resources"]
|
||||
: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"}}
|
||||
:main-opts ["-m" "kaocha.runner" "--reporter" "kaocha.report/documentation"]}
|
||||
: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"
|
||||
:exclusions [org.clojure/clojure]}}
|
||||
:main-opts ["-m" "cljstyle.main" "check"]}}}
|
||||
:cljstyle {:extra-deps {mvxcvi/cljstyle {:mvn/version "0.13.0"
|
||||
:exclusions [org.clojure/clojure]}}
|
||||
:main-opts ["-m" "cljstyle.main" "check"]}}}
|
||||
|
|
|
|||
37
doc/intro.md
37
doc/intro.md
|
|
@ -1,38 +1,27 @@
|
|||
# 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)
|
||||
(.start))
|
||||
|
||||
(.withExposedPorts (into-array Integer [(int 5432)]))
|
||||
(.withEnv "POSTGRES_PASSSWORD" pw)
|
||||
(.start))
|
||||
|
||||
```
|
||||
|
||||
But doing so is quite wordy and requires developers to use a lot of methods that manipulate a java instance.
|
||||
But doing so is quite wordy and requires developers to use a lot of methods that manipulate a java instance.
|
||||
|
||||
```clojure
|
||||
(-> (tc/create {:image-name "postgres:12.1"
|
||||
:exposed-ports [5432]
|
||||
:env-vars {"POSTGRES_PASSWORD" pw}})
|
||||
tc/start!)
|
||||
(-> (tc/create {:image-name "postgres:12.1"
|
||||
:exposed-ports [5432]
|
||||
:env-vars {"POSTGRES_PASSWORD" pw}})
|
||||
tc/start!)
|
||||
```
|
||||
|
|
|
|||
|
|
@ -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))}}
|
||||
```
|
||||
|
|
@ -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,88 +10,80 @@ 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])
|
||||
|
||||
(deftest db-integration-test
|
||||
(testing "A simple PostgreSQL integration test"
|
||||
(let [pw "db-pass"
|
||||
postgres (-> (tc/create {:image-name "postgres:12.1"
|
||||
:exposed-ports [5432]
|
||||
:env-vars {"POSTGRES_PASSWORD" pw}}))]
|
||||
(tc/start! postgres)
|
||||
(let [datasource (jdbc/get-datasource {:dbtype "postgresql"
|
||||
:dbname "postgres"
|
||||
:user "postgres"
|
||||
:password pw
|
||||
:host (:host postgres)
|
||||
:port (get (:mapped-ports container) 5432)})]
|
||||
(is (= [{:one 1 :two 2}] (with-open [connection (jdbc/get-connection datasource)]
|
||||
(jdbc/execute! connection ["SELECT 1 ONE, 2 TWO"])))))
|
||||
(tc/stop! postgres))))
|
||||
(testing "A simple PostgreSQL integration test"
|
||||
(let [pw "db-pass"
|
||||
postgres (-> (tc/create {:image-name "postgres:12.1"
|
||||
:exposed-ports [5432]
|
||||
:env-vars {"POSTGRES_PASSWORD" pw}}))]
|
||||
(tc/start! postgres)
|
||||
(let [datasource (jdbc/get-datasource {:dbtype "postgresql"
|
||||
:dbname "postgres"
|
||||
:user "postgres"
|
||||
:password pw
|
||||
:host (:host postgres)
|
||||
:port (get (:mapped-ports container) 5432)})]
|
||||
(is (= [{:one 1 :two 2}] (with-open [connection (jdbc/get-connection datasource)]
|
||||
(jdbc/execute! connection ["SELECT 1 ONE, 2 TWO"])))))
|
||||
(tc/stop! postgres))))
|
||||
```
|
||||
|
||||
## 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"])
|
||||
|
||||
> {:exit-code 0
|
||||
:stdout "root"}
|
||||
:stdout "root"}
|
||||
```
|
||||
|
||||
## 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
|
||||
{:resource-path "test.sql"
|
||||
(map-classpath-resource! container
|
||||
{:resource-path "test.sql"
|
||||
:container-path "/opt/test.sql"
|
||||
:mode :read-only})
|
||||
:mode :read-only})
|
||||
```
|
||||
|
||||
```clojure
|
||||
(bind-filesystem! {:host-path "."
|
||||
:container-path "/opt"
|
||||
:mode :read-only})
|
||||
(bind-filesystem! {:host-path "."
|
||||
:container-path "/opt"
|
||||
:mode :read-only})
|
||||
```
|
||||
|
||||
It is also possible to copy files into a running container instance:
|
||||
|
||||
```clojure
|
||||
(copy-file-to-container! {:path "test.sql"
|
||||
:container-path "/opt/test.sql"
|
||||
:type :host-path})
|
||||
(copy-file-to-container! {:path "test.sql"
|
||||
:container-path "/opt/test.sql"
|
||||
:type :host-path})
|
||||
```
|
||||
|
||||
# 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"
|
||||
postgres (tc/start! (tc/create {:image-name "postgres:12.2"
|
||||
:exposed-ports [5432]
|
||||
:env-vars {"POSTGRES_PASSWORD" pw}}))]
|
||||
(let [{pw "apassword"
|
||||
postgres (tc/start! (tc/create {:image-name "postgres:12.2"
|
||||
:exposed-ports [5432]
|
||||
:env-vars {"POSTGRES_PASSWORD" pw}}))}]
|
||||
(my-app/initialize-db! {:dbtype "postgresql"
|
||||
:dbname "postgres"
|
||||
:user "postgres"
|
||||
|
|
|
|||
|
|
@ -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}})
|
||||
```
|
||||
37
project.clj
37
project.clj
|
|
@ -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")
|
||||
|
|
|
|||
|
|
@ -1,28 +1,23 @@
|
|||
(ns clj-test-containers.core
|
||||
(:require
|
||||
[clj-test-containers.spec.core :as cs]
|
||||
[clojure.spec.alpha :as s]
|
||||
[clojure.string])
|
||||
[clj-test-containers.spec.core :as cs]
|
||||
[clojure.spec.alpha :as s]
|
||||
[clojure.string])
|
||||
(: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)
|
||||
(org.testcontainers.images.builder
|
||||
ImageFromDockerfile)
|
||||
(org.testcontainers.utility
|
||||
MountableFile)))
|
||||
(java.nio.file
|
||||
Paths)
|
||||
(org.testcontainers.containers
|
||||
BindMode
|
||||
GenericContainer
|
||||
Network)
|
||||
(org.testcontainers.containers.output
|
||||
ToStringConsumer)
|
||||
(org.testcontainers.containers.wait.strategy
|
||||
Wait)
|
||||
(org.testcontainers.images.builder
|
||||
ImageFromDockerfile)
|
||||
(org.testcontainers.utility
|
||||
MountableFile)))
|
||||
|
||||
(defn- resolve-bind-mode
|
||||
(^BindMode [bind-mode]
|
||||
|
|
@ -30,149 +25,99 @@
|
|||
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
|
||||
strategies.
|
||||
"Sets a wait strategy to the container. Supports :http, :health and :log as
|
||||
strategies.
|
||||
|
||||
## HTTP Strategy
|
||||
The :http strategy will only accept the container as initialized if it can be
|
||||
accessed via HTTP. It accepts a path, a port, a vector of status codes, a
|
||||
boolean that specifies if TLS is enabled, a read timeout in seconds and a map
|
||||
with basic credentials, containing username and password. Only the path is
|
||||
required, all others are optional.
|
||||
## HTTP Strategy
|
||||
The :http strategy will only accept the container as initialized if it can be
|
||||
accessed via HTTP. It accepts a path, a port, a vector of status codes, a
|
||||
boolean that specifies if TLS is enabled, a read timeout in seconds and a map
|
||||
with basic credentials, containing username and password. Only the path is
|
||||
required, all others are optional.
|
||||
|
||||
Example:
|
||||
Example:
|
||||
|
||||
```clojure
|
||||
(wait {:wait-strategy :http
|
||||
:port 80
|
||||
:path \"/\"
|
||||
:status-codes [200 201]
|
||||
:tls true
|
||||
:read-timeout 5
|
||||
:basic-credentials {:username \"user\"
|
||||
:password \"password\"
|
||||
:startup-timeout 60}}
|
||||
container)
|
||||
```
|
||||
```clojure
|
||||
(wait {:wait-strategy :http
|
||||
:port 80
|
||||
:path \"/\"
|
||||
:status-codes [200 201]
|
||||
:tls true
|
||||
:read-timeout 5
|
||||
:basic-credentials {:username \"user\"
|
||||
: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.
|
||||
## Health Strategy
|
||||
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:
|
||||
Example:
|
||||
|
||||
```clojure
|
||||
(wait {:wait-strategy :health
|
||||
:startup-timeout 60} container)
|
||||
```
|
||||
```clojure
|
||||
(wait {:wait-strategy :health :true} container)
|
||||
```
|
||||
|
||||
## Log Strategy
|
||||
The :log strategy accepts a message which simply causes the output of your
|
||||
container's log to be used in determining if the container is ready or not.
|
||||
The output is `grepped` against the log message.
|
||||
## Log Strategy
|
||||
The :log strategy accepts a message which simply causes the output of your
|
||||
container's log to be used in determining if the container is ready or not.
|
||||
The output is `grepped` against the log message.
|
||||
|
||||
Example:
|
||||
Example:
|
||||
|
||||
```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
|
||||
```"
|
||||
:wait-strategy)
|
||||
```clojure
|
||||
(wait {:wait-strategy :log
|
||||
: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
|
||||
|
|
@ -203,11 +148,11 @@
|
|||
(when network-aliases
|
||||
(.setNetworkAliases container network-aliases))
|
||||
|
||||
(merge init-options {:container container
|
||||
(merge init-options {:container container
|
||||
:exposed-ports (vec (.getExposedPorts container))
|
||||
:env-vars (into {} (.getEnvMap container))
|
||||
:host (.getHost container)
|
||||
:network network} (wait wait-for container)))
|
||||
:env-vars (into {} (.getEnvMap container))
|
||||
:host (.getHost container)
|
||||
:network network} (wait wait-for container)))
|
||||
|
||||
(s/fdef create
|
||||
:args (s/cat :create-options ::cs/create-options)
|
||||
|
|
@ -235,11 +180,11 @@
|
|||
[{:keys [^GenericContainer container] :as container-config}
|
||||
{:keys [^String resource-path ^String container-path mode]}]
|
||||
(assoc container-config
|
||||
:container
|
||||
(.withClasspathResourceMapping container
|
||||
resource-path
|
||||
container-path
|
||||
(resolve-bind-mode mode))))
|
||||
:container
|
||||
(.withClasspathResourceMapping container
|
||||
resource-path
|
||||
container-path
|
||||
(resolve-bind-mode mode))))
|
||||
|
||||
(defn bind-filesystem!
|
||||
"Binds a source from the filesystem to the given container path. Should be
|
||||
|
|
@ -247,11 +192,11 @@
|
|||
[{:keys [^GenericContainer container] :as container-config}
|
||||
{:keys [^String host-path ^String container-path mode]}]
|
||||
(assoc container-config
|
||||
:container
|
||||
(.withFileSystemBind container
|
||||
host-path
|
||||
container-path
|
||||
(resolve-bind-mode mode))))
|
||||
:container
|
||||
(.withFileSystemBind container
|
||||
host-path
|
||||
container-path
|
||||
(resolve-bind-mode mode))))
|
||||
|
||||
(defn copy-file-to-container!
|
||||
"If a container is not yet started, adds a mapping from mountable file to
|
||||
|
|
@ -262,16 +207,16 @@
|
|||
(let [^MountableFile mountable-file
|
||||
(case type
|
||||
:classpath-resource (MountableFile/forClasspathResource path)
|
||||
:host-path (MountableFile/forHostPath path))]
|
||||
:host-path (MountableFile/forHostPath path))]
|
||||
(if id
|
||||
(do
|
||||
(.copyFileToContainer container mountable-file container-path)
|
||||
container-config)
|
||||
(assoc container-config
|
||||
:container
|
||||
(.withCopyFileToContainer container
|
||||
mountable-file
|
||||
container-path)))))
|
||||
:container
|
||||
(.withCopyFileToContainer container
|
||||
mountable-file
|
||||
container-path)))))
|
||||
|
||||
(defn execute-command!
|
||||
"Executes a command in the container, and returns the result"
|
||||
|
|
@ -282,39 +227,28 @@
|
|||
: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
|
||||
`string-log` key. This function enables the dumping of the logs when passed to
|
||||
the `dump-logs` function.
|
||||
## String Strategy
|
||||
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.
|
||||
|
||||
Example:
|
||||
Example:
|
||||
|
||||
```clojure
|
||||
{:log-strategy :string}
|
||||
```
|
||||
```clojure
|
||||
{:log-strategy :string}
|
||||
```
|
||||
|
||||
Then, later in your program, you can access the logs thus:
|
||||
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-strategy :fn
|
||||
:function (fn [log-line] (println \"From Container: \" log-line)}
|
||||
```
|
||||
"
|
||||
:log-strategy)
|
||||
```clojure
|
||||
(def container-config (tc/start! container))
|
||||
(tc/dump-logs container-config)
|
||||
```
|
||||
"
|
||||
:log-strategy)
|
||||
|
||||
(defmethod log :string
|
||||
[_ ^GenericContainer container]
|
||||
|
|
@ -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,14 +278,16 @@
|
|||
[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
|
||||
(merge {:id container-id
|
||||
:mapped-ports mapped-ports
|
||||
:image-name image-name} logger)
|
||||
:image-name image-name} logger)
|
||||
(dissoc :log-to))))
|
||||
|
||||
(defn stop!
|
||||
|
|
@ -387,43 +317,21 @@
|
|||
|
||||
(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)
|
||||
:driver (.getDriver network)}))))
|
||||
:name network-name
|
||||
:ipv6 (.getEnableIpv6 network)
|
||||
:driver (.getDriver network)}))))
|
||||
|
||||
(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!))
|
||||
|
|
|
|||
|
|
@ -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})
|
||||
|
||||
|
|
|
|||
|
|
@ -9,9 +9,9 @@
|
|||
|
||||
(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"}})
|
||||
:env-vars {"POSTGRES_PASSWORD" "pw"}})
|
||||
initialized-container (sut/start! container)
|
||||
stopped-container (sut/stop! container)]
|
||||
(is (some? (:id initialized-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"}
|
||||
:log-to {:log-strategy :string}})
|
||||
(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}})
|
||||
:env-vars {"POSTGRES_PASSWORD" "pw"}
|
||||
:wait-for {:wait-strategy :log :message "accept connections"}})
|
||||
initialized-container (sut/start! container)
|
||||
stopped-container (sut/stop! container)]
|
||||
(is (some? (:id initialized-container)))
|
||||
|
|
@ -58,47 +41,9 @@
|
|||
(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"})
|
||||
:docker-file "test/resources/Dockerfile"})
|
||||
initialized-container (sut/start! container)
|
||||
stopped-container (sut/stop! container)]
|
||||
(is (some? (:id initialized-container)))
|
||||
|
|
@ -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,9 +64,9 @@
|
|||
(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"}})
|
||||
:env-vars {"POSTGRES_PASSWORD" "pw"}})
|
||||
initialized-container (sut/start! container)
|
||||
result (sut/execute-command! initialized-container ["whoami"])
|
||||
_stopped-container (sut/stop! container)]
|
||||
|
|
@ -132,12 +76,12 @@
|
|||
(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"
|
||||
:env-vars {"POSTGRES_PASSWORD" "pw"}})
|
||||
(sut/map-classpath-resource! {:resource-path "test.sql"
|
||||
:container-path "/opt/test.sql"
|
||||
:mode :read-only}))
|
||||
:mode :read-only}))
|
||||
initialized-container (sut/start! container)
|
||||
file-check (sut/execute-command! initialized-container ["tail" "/opt/test.sql"])
|
||||
stopped-container (sut/stop! container)]
|
||||
|
|
@ -149,12 +93,12 @@
|
|||
(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 "."
|
||||
:container-path "/opt"
|
||||
:mode :read-only}))
|
||||
:env-vars {"POSTGRES_PASSWORD" "pw"}})
|
||||
(sut/bind-filesystem! {:host-path "."
|
||||
:container-path "/opt"
|
||||
:mode :read-only}))
|
||||
initialized-container (sut/start! container)
|
||||
file-check (sut/execute-command! initialized-container ["tail" "/opt/README.md"])
|
||||
stopped-container (sut/stop! container)]
|
||||
|
|
@ -166,12 +110,12 @@
|
|||
(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"
|
||||
:container-path "/opt/test.sql"
|
||||
:type :host-path}))
|
||||
:env-vars {"POSTGRES_PASSWORD" "pw"}})
|
||||
(sut/copy-file-to-container! {:path "test.sql"
|
||||
:container-path "/opt/test.sql"
|
||||
:type :host-path}))
|
||||
initialized-container (sut/start! container)
|
||||
file-check (sut/execute-command! initialized-container ["tail" "/opt/test.sql"])
|
||||
stopped-container (sut/stop! container)]
|
||||
|
|
@ -184,12 +128,12 @@
|
|||
|
||||
|
||||
(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"
|
||||
:container-path "/opt/test.sql"
|
||||
:type :classpath-resource}))
|
||||
:env-vars {"POSTGRES_PASSWORD" "pw"}})
|
||||
(sut/copy-file-to-container! {:path "test.sql"
|
||||
:container-path "/opt/test.sql"
|
||||
:type :classpath-resource}))
|
||||
initialized-container (sut/start! container)
|
||||
file-check (sut/execute-command! initialized-container ["tail" "/opt/test.sql"])
|
||||
stopped-container (sut/stop! container)]
|
||||
|
|
@ -201,14 +145,14 @@
|
|||
(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"}})
|
||||
:env-vars {"POSTGRES_PASSWORD" "pw"}})
|
||||
initialized-container (sut/start! container)
|
||||
_ (sut/copy-file-to-container! initialized-container
|
||||
{:path "test.sql"
|
||||
{:path "test.sql"
|
||||
:container-path "/opt/test.sql"
|
||||
:type :host-path})
|
||||
:type :host-path})
|
||||
file-check (sut/execute-command! initialized-container
|
||||
["tail" "/opt/test.sql"])
|
||||
stopped-container (sut/stop! container)]
|
||||
|
|
@ -220,14 +164,14 @@
|
|||
(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"}})
|
||||
:env-vars {"POSTGRES_PASSWORD" "pw"}})
|
||||
initialized-container (sut/start! container)
|
||||
_ (sut/copy-file-to-container! initialized-container
|
||||
{:path "test.sql"
|
||||
{:path "test.sql"
|
||||
:container-path "/opt/test.sql"
|
||||
:type :classpath-resource})
|
||||
:type :classpath-resource})
|
||||
file-check (sut/execute-command! initialized-container
|
||||
["tail" "/opt/test.sql"])
|
||||
stopped-container (sut/stop! container)]
|
||||
|
|
@ -241,15 +185,15 @@
|
|||
(deftest networking-test
|
||||
(testing "Putting two containers into the same network and check their communication"
|
||||
(let [network (sut/create-network)
|
||||
server-container (sut/create {:image-name "alpine:3.5"
|
||||
:network network
|
||||
server-container (sut/create {:image-name "alpine:3.5"
|
||||
:network network
|
||||
:network-aliases ["foo"]
|
||||
:command ["/bin/sh"
|
||||
"-c"
|
||||
"while true ; do printf 'HTTP/1.1 200 OK\\n\\nyay' | nc -l -p 8080; done"]})
|
||||
:command ["/bin/sh"
|
||||
"-c"
|
||||
"while true ; do printf 'HTTP/1.1 200 OK\\n\\nyay' | nc -l -p 8080; done"]})
|
||||
client-container (sut/create {:image-name "alpine:3.5"
|
||||
:network network
|
||||
:command ["top"]})
|
||||
:network network
|
||||
:command ["top"]})
|
||||
started-server (sut/start! server-container)
|
||||
started-client (sut/start! client-container)
|
||||
response (sut/execute-command! started-client ["wget", "-O", "-", "http://foo:8080"])
|
||||
|
|
|
|||
Loading…
Reference in a new issue