Added :reuse flag (#72)

Included a new flag for the container reuse feature
This commit is contained in:
Tim Zöller 2024-04-14 13:54:00 +02:00
parent 3809bbb28c
commit 0e2d483715
5 changed files with 60 additions and 17 deletions

View file

@ -68,19 +68,21 @@ Creates a testcontainers instance from a given Docker label and returns them
| ------------- | :------------- |:----------------------------------------------------------------------------------------------------| | ------------- | :------------- |:----------------------------------------------------------------------------------------------------|
| `:image-name` | String, mandatory | The name and label of an image, e.g. `postgres:12.2` | | `: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 | | `:exposed-ports` | Vector with ints, mandatory | All ports which should be exposed and mapped to a local port |
| `:reuse` | Boolean | Should the container be reused, if another Testcontainer with identical config is started? |
| `:env-vars` | Map | A map with environment variables | | `:env-vars` | Map | A map with environment variables |
| `:command` | Vector with strings | The start command of the container | | `: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` | 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 | | `: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 | | `: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} | | `:log-to` | Map | A map containing the [log strategy](doc/log-strategies.md) to use, e.g. {:log-strategy string} |
#### Result: #### Result:
| Key | Type | Description | | Key | Type | Description |
| ------------- | :------------- | :----- | |------------------|:------------------------------------------|:------------------------------------------------------------------------------------------|
| `:container` | `org.testcontainers.containers.Container` | The Testcontainers instance, accessible for everything this library doesn't provide (yet) | | `: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 | | `:exposed-ports` | Vector with ints | Value of the same input parameter |
| `:reuse` | Boolean | Is this container reusable? |
| `:env-vars` | Map | Value of the same input parameter | | `:env-vars` | Map | Value of the same input parameter |
| `:host` | String | The host for the Docker Container | | `:host` | String | The host for the Docker Container |
| `:network` | Map | The network configuration of the Container, if provided | | `:network` | Map | The network configuration of the Container, if provided |
@ -99,6 +101,20 @@ Creates a testcontainers instance from a given Docker label and returns them
"while true; do echo \"$MAGIC_NUMBER\" | nc -l -p 80; done"]}) "while true; do echo \"$MAGIC_NUMBER\" | nc -l -p 80; done"]})
``` ```
#### Example with reuse
```clojure
(create {:image-name "alpine:3.2"
:exposed-ports [80]
:reuse true
: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"]})
```
#### Example using wait-for and healthcheck: #### Example using wait-for and healthcheck:
```clojure ```clojure
@ -119,17 +135,18 @@ Initializes a given Testcontainer, which was e.g. provided by a library
#### Config parameters: #### Config parameters:
| Key | Type | Description | | Key | Type | Description |
| ------------- | :------------- |:----------------------------------------------------------------------------------------------------| |--------------------|:------------------------------------------------------------|:---------------------------------------------------------------------------------------------------|
| `:container` | `org.testcontainers.containers.GenericContainer`, mandatory | The name and label of an image, e.g. `postgres:12.2` | | `: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 | | `: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 | | `:reuse` | Boolean | Should the container be reused, if another Testcontainer with identical config is started? |
| `:command` | Vector with strings | The start command of the container | | `:env-vars` | Map | A map with environment variables |
| `:network` | Map | A map containing the configuration of a Docker Network (see: `create-network`) | | `:command` | Vector with strings | The start command of the container |
| `:network-aliases` | Map | A list of alias names for the container on the network | | `: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 | | `: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} | | `:log-to` | Map | A map containing the [log strategy](doc/log-strategies.md) to use, e.g. {:log-strategy string} |
| | | | | | | |
#### Result: #### Result:
@ -137,6 +154,7 @@ Initializes a given Testcontainer, which was e.g. provided by a library
| ------------- | :------------- |:------------------------------------------------------------------------------------------| | ------------- | :------------- |:------------------------------------------------------------------------------------------|
| `:container` | `org.testcontainers.containers.Container` | The Testcontainers instance, accessible for everything this library doesn't provide (yet) | | `: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 | | `:exposed-ports` | Vector with ints | Value of the same input parameter |
| `:reuse` | Boolean | Is this container reusable? |
| `:env-vars` | Map | Value of the same input parameter | | `:env-vars` | Map | Value of the same input parameter |
| `:host` | String | The host for the Docker Container | | `:host` | String | The host for the Docker Container |
| `:network` | Map | The network configuration of the Container, if provided | | `:network` | Map | The network configuration of the Container, if provided |
@ -177,6 +195,7 @@ Creates a testcontainer from a Dockerfile
| ------------- | :------------- | :----- | | ------------- | :------------- | :----- |
| `:docker-file` | String, mandatory | String containing a path to a Dockerfile | | `:docker-file` | String, mandatory | String containing a path to a Dockerfile |
| `:exposed-ports` | Vector with ints, mandatory | All ports which should be exposed and mapped to a local port | | `:exposed-ports` | Vector with ints, mandatory | All ports which should be exposed and mapped to a local port |
| `:reuse` | Boolean | Should the container be reused, if another Testcontainer with identical config is started? |
| `:env-vars` | Map | A map with environment variables | | `:env-vars` | Map | A map with environment variables |
| `:command` | Vector with strings | The start command of the container | | `: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` | Map | A map containing the configuration of a Docker Network (see: `create-network`) |
@ -191,6 +210,7 @@ Creates a testcontainer from a Dockerfile
| ------------- | :------------- | :----- | | ------------- | :------------- | :----- |
| `:container` | `org.testcontainers.containers.Container` | The Testcontainers instance, accessible for everything this library doesn't provide (yet) | | `: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 | | `:exposed-ports` | Vector with ints | Value of the same input parameter |
| `:reuse` | Boolean | Is this container reusable? |
| `:env-vars` | Map | Value of the same input parameter | | `:env-vars` | Map | Value of the same input parameter |
| `:host` | String | The host for the Docker Container | | `:host` | String | The host for the Docker Container |
| `:network` | Map | The network configuration of the Container, if provided | | `:network` | Map | The network configuration of the Container, if provided |
@ -227,6 +247,7 @@ Starts the Testcontainer, which was defined by `create`
| ------------- | :------------- | :----- | | ------------- | :------------- | :----- |
| `:container` | `org.testcontainers.containers.Container` | The Testcontainers instance, accessible for everything this library doesn't provide (yet) | | `: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 | | `:exposed-ports` | Vector with ints | Value of the same input parameter |
| `:reuse` | Boolean | Is this container reusable? |
| `:env-vars` | Map | Value of the same input parameter | | `:env-vars` | Map | Value of the same input parameter |
| `:host` | String | The host for the Docker Container | | `:host` | String | The host for the Docker Container |
| `:id` | String | The ID of the started docker container | | `:id` | String | The ID of the started docker container |

View file

@ -183,6 +183,7 @@
"Sets the properties for a testcontainer instance" "Sets the properties for a testcontainer instance"
[{:keys [^GenericContainer container [{:keys [^GenericContainer container
exposed-ports exposed-ports
reuse
env-vars env-vars
command command
network network
@ -194,6 +195,9 @@
(doseq [[k v] env-vars] (doseq [[k v] env-vars]
(.addEnv container k v)) (.addEnv container k v))
(when reuse
(.withReuse container true))
(when command (when command
(.setCommand container ^"[Ljava.lang.String;" (into-array String command))) (.setCommand container ^"[Ljava.lang.String;" (into-array String command)))
@ -425,5 +429,6 @@
;; REPL Helpers ;; REPL Helpers
(comment (comment
(start! (create {:image-name "postgres:12.1"})) (start! (create {:image-name "postgres:12.1" :reuse true}))
(perform-cleanup!)) (perform-cleanup!)
)

View file

@ -14,6 +14,9 @@
(s/def ::exposed-ports (s/def ::exposed-ports
(s/coll-of (s/int-in 1 65535))) (s/coll-of (s/int-in 1 65535)))
(s/def ::reuse
boolean?)
(s/def ::env-vars (s/def ::env-vars
(s/map-of string? string?)) (s/map-of string? string?))

View file

@ -25,13 +25,15 @@
::csc/exposed-ports ::csc/exposed-ports
::csc/env-vars ::csc/env-vars
::csc/host] ::csc/host]
:opt-un [::network :opt-un [::csc/reuse
::network
::wait-for ::wait-for
::log-to])) ::log-to]))
(s/def ::init-options (s/def ::init-options
(s/keys :req-un [::csc/container] (s/keys :req-un [::csc/container]
:opt-un [::csc/exposed-ports :opt-un [::csc/exposed-ports
::csc/reuse
::csc/env-vars ::csc/env-vars
::csc/command ::csc/command
::network ::network

View file

@ -115,7 +115,19 @@
result (sut/execute-command! initialized-container ["whoami"]) result (sut/execute-command! initialized-container ["whoami"])
_stopped-container (sut/stop! container)] _stopped-container (sut/stop! container)]
(is (= 0 (:exit-code result))) (is (= 0 (:exit-code result)))
(is (= "root\n" (:stdout result)))))) (is (= "root\n" (:stdout result)))))
(testing "Reusing a container with the :reuse flag"
(let [container-1 (sut/init {:container (PostgreSQLContainer. "postgres:15.3")
:exposed-ports [5432]
:reuse true})
container-2 (sut/init {:container (PostgreSQLContainer. "postgres:15.3")
:exposed-ports [5432]
:reuse true})
initialized-container-1 (sut/start! container-1)
initialized-container-2 (sut/start! container-2)]
(is (= (.getContainerId (:container initialized-container-1))
(.getContainerId (:container initialized-container-2)))))))
(deftest execute-command-in-container (deftest execute-command-in-container