Skip to content

Commit f7423e4

Browse files
committed
Merge upstream PR#82 - with-network: Network test fixture function
testcontainers#82
2 parents c87b881 + 436501c commit f7423e4

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
@@ -438,6 +438,25 @@ Creates a network. The optional map accepts config values for enabling ipv6 and
438438
(create-network)
439439
```
440440

441+
### with-network
442+
443+
Create and bind a network to *network* and run `test-fn`. The network is removed after the function executes.
444+
445+
#### Config parameters:
446+
447+
`with-network` takes an optional map of options, equivalent to `create-network`.
448+
449+
450+
#### Example:
451+
452+
```clojure
453+
;; Run tests within an ephemeral network
454+
(use-fixtures :once (tc/with-network {:ipv6? true}))
455+
456+
(deftest test-network-loaded
457+
(is (some? tc/*network*)))
458+
```
459+
441460
### perform-cleanup!
442461

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

src/testcontainers_clj/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
@@ -408,6 +410,16 @@
408410
(.exec))
409411
instance)
410412

413+
(defn with-network
414+
([] (with-network {}))
415+
([network-options]
416+
(fn [test-fn]
417+
(binding [*network* (create-network network-options)]
418+
(try
419+
(test-fn)
420+
(finally
421+
(remove-network! *network*)))))))
422+
411423
(defn- stop-and-remove-container! [instance]
412424
(let [docker-client (DockerClientFactory/instance)]
413425
(-> docker-client

0 commit comments

Comments
 (0)