Skip to content

Commit 436501c

Browse files
committed
with-network: Network test fixture function
1 parent 802f18a commit 436501c

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,25 @@ Creates a network. The optional map accepts config values for enabling ipv6 and
421421
(create-network)
422422
```
423423

424+
### with-network
425+
426+
Create and bind a network to *network* and run `test-fn`. The network is removed after the function executes.
427+
428+
#### Config parameters:
429+
430+
`with-network` takes an optional map of options, equivalent to `create-network`.
431+
432+
433+
#### Example:
434+
435+
```clojure
436+
;; Run tests within an ephemeral network
437+
(use-fixtures :once (tc/with-network {:ipv6? true}))
438+
439+
(deftest test-network-loaded
440+
(is (some? tc/*network*)))
441+
```
442+
424443
### perform-cleanup!
425444

426445
Stops and removes all containers which were created in the JVM, including the REPL session you are in. This is helpful,

src/clj_test_containers/core.clj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
BindMode/READ_WRITE
3131
BindMode/READ_ONLY)))
3232

33+
(def ^:dynamic *network* nil)
34+
3335
(defonce started-instances (atom #{}))
3436

3537
(defmulti wait
@@ -402,6 +404,16 @@
402404
(.exec))
403405
instance)
404406

407+
(defn with-network
408+
([] (with-network {}))
409+
([network-options]
410+
(fn [test-fn]
411+
(binding [*network* (create-network network-options)]
412+
(try
413+
(test-fn)
414+
(finally
415+
(remove-network! *network*)))))))
416+
405417
(defn- stop-and-remove-container! [instance]
406418
(let [docker-client (DockerClientFactory/instance)]
407419
(-> docker-client

0 commit comments

Comments
 (0)