#2 - Add networking support
This commit is contained in:
parent
dda1e4b4a5
commit
de4a48a07b
2 changed files with 60 additions and 4 deletions
|
|
@ -2,7 +2,7 @@
|
|||
(:require [clojure.java.io :as io])
|
||||
(:import [org.testcontainers.containers GenericContainer]
|
||||
[org.testcontainers.utility MountableFile]
|
||||
[org.testcontainers.containers BindMode]
|
||||
[org.testcontainers.containers BindMode Network]
|
||||
[org.testcontainers.images.builder ImageFromDockerfile]
|
||||
[java.nio.file Path Paths]))
|
||||
|
||||
|
|
@ -14,18 +14,26 @@
|
|||
|
||||
(defn init
|
||||
"Sets the properties for a testcontainer instance"
|
||||
[{:keys [container exposed-ports env-vars command]}]
|
||||
[{:keys [container exposed-ports env-vars command network network-aliases]}]
|
||||
|
||||
(.setExposedPorts container (map int exposed-ports))
|
||||
|
||||
(run! (fn [[k v]] (.addEnv container k v)) env-vars)
|
||||
|
||||
(when command
|
||||
(.setCommand container command))
|
||||
(.setCommand container (into-array String command)))
|
||||
|
||||
(when network
|
||||
(.setNetwork container (:network network)))
|
||||
|
||||
(when network-aliases
|
||||
(.setNetworkAliases container (java.util.ArrayList. network-aliases)))
|
||||
|
||||
{:container container
|
||||
:exposed-ports (vec (.getExposedPorts container))
|
||||
:env-vars (into {} (.getEnvMap container))
|
||||
:host (.getHost container)})
|
||||
:host (.getHost container)
|
||||
:network network})
|
||||
|
||||
(defn create
|
||||
"Creates a generic testcontainer and sets its properties"
|
||||
|
|
@ -106,3 +114,28 @@
|
|||
(-> container-config
|
||||
(dissoc :id)
|
||||
(dissoc :mapped-ports)))
|
||||
|
||||
|
||||
(defn- build-network
|
||||
[{:keys [ipv6 driver]}]
|
||||
(let [builder (Network/builder)]
|
||||
|
||||
(when ipv6
|
||||
(.enableIpv6 builder true))
|
||||
|
||||
(when driver
|
||||
(.driver builder driver))
|
||||
|
||||
(let [network (.build builder)]
|
||||
{:network network
|
||||
:id (.getId network)
|
||||
:name (.getName network)
|
||||
:ipv6 (.getEnableIpv6 network)
|
||||
:driver (.getDriver network)})))
|
||||
|
||||
(defn init-network
|
||||
"Creates a network. The optional map accepts config values for enabling ipv6 and setting the driver"
|
||||
([]
|
||||
(build-network {}))
|
||||
([options]
|
||||
(build-network options)))
|
||||
|
|
|
|||
|
|
@ -120,3 +120,26 @@
|
|||
(is (= 0 (:exit-code file-check)))
|
||||
(is (nil? (:id stopped-container)))
|
||||
(is (nil? (:mapped-ports stopped-container))))))
|
||||
|
||||
|
||||
(deftest networking-test
|
||||
|
||||
(testing "Putting two containers into the same network and check their communication"
|
||||
(let [network (init-network)
|
||||
server-container (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"]})
|
||||
client-container (create {:image-name "alpine:3.5"
|
||||
:network network
|
||||
:command ["top"]})
|
||||
started-server (start! server-container)
|
||||
started-client (start! client-container)
|
||||
response (execute-command! started-client ["wget", "-O", "-", "http://foo:8080"])
|
||||
stopped-server (stop! started-server)
|
||||
stopped-client (stop! started-client)]
|
||||
|
||||
(is (= 0 (:exit-code response)))
|
||||
(is (= "yay" (:stdout response))))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue