From de4a48a07bf771febaa42b6f0084b0a88ae1d5c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Z=C3=B6ller?= Date: Wed, 5 Aug 2020 15:37:35 +0200 Subject: [PATCH] #2 - Add networking support --- src/clj_test_containers/core.clj | 41 +++++++++++++++++++++++--- test/clj_test_containers/core_test.clj | 23 +++++++++++++++ 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/clj_test_containers/core.clj b/src/clj_test_containers/core.clj index 799a643..640a28d 100644 --- a/src/clj_test_containers/core.clj +++ b/src/clj_test_containers/core.clj @@ -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))) diff --git a/test/clj_test_containers/core_test.clj b/test/clj_test_containers/core_test.clj index 973d15f..55706c7 100644 --- a/test/clj_test_containers/core_test.clj +++ b/test/clj_test_containers/core_test.clj @@ -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))))))