with-network: Network test fixture function

This commit is contained in:
Ryan Sundberg 2024-04-30 11:08:45 -07:00
parent 802f18af55
commit 436501caf5
No known key found for this signature in database
GPG key ID: 6A2322F8C0ABF7AB
2 changed files with 31 additions and 0 deletions

View file

@ -421,6 +421,25 @@ Creates a network. The optional map accepts config values for enabling ipv6 and
(create-network) (create-network)
``` ```
### with-network
Create and bind a network to *network* and run `test-fn`. The network is removed after the function executes.
#### Config parameters:
`with-network` takes an optional map of options, equivalent to `create-network`.
#### Example:
```clojure
;; Run tests within an ephemeral network
(use-fixtures :once (tc/with-network {:ipv6? true}))
(deftest test-network-loaded
(is (some? tc/*network*)))
```
### perform-cleanup! ### perform-cleanup!
Stops and removes all containers which were created in the JVM, including the REPL session you are in. This is helpful, Stops and removes all containers which were created in the JVM, including the REPL session you are in. This is helpful,

View file

@ -30,6 +30,8 @@
BindMode/READ_WRITE BindMode/READ_WRITE
BindMode/READ_ONLY))) BindMode/READ_ONLY)))
(def ^:dynamic *network* nil)
(defonce started-instances (atom #{})) (defonce started-instances (atom #{}))
(defmulti wait (defmulti wait
@ -402,6 +404,16 @@
(.exec)) (.exec))
instance) instance)
(defn with-network
([] (with-network {}))
([network-options]
(fn [test-fn]
(binding [*network* (create-network network-options)]
(try
(test-fn)
(finally
(remove-network! *network*)))))))
(defn- stop-and-remove-container! [instance] (defn- stop-and-remove-container! [instance]
(let [docker-client (DockerClientFactory/instance)] (let [docker-client (DockerClientFactory/instance)]
(-> docker-client (-> docker-client