From 7b5290463541f1a5a8daf57e4368c4484b672da2 Mon Sep 17 00:00:00 2001 From: Rob Hanlon Date: Thu, 6 Aug 2020 23:14:19 -0700 Subject: [PATCH] Install and configure cljstyle --- .circleci/config.yml | 32 +++++++++++++++++----- src/clj_test_containers/core.clj | 37 +++++++++++++++++++------- test/clj_test_containers/core_test.clj | 30 ++++++++++++--------- 3 files changed, 72 insertions(+), 27 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index fdd2b0d..10cad47 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,15 +1,19 @@ -# Clojure CircleCI 2.0 configuration file +# Clojure CircleCI 2.1 configuration file # -# Check https://circleci.com/docs/2.0/language-clojure/ for more details +# Check https://circleci.com/docs/2.0/language-ubuntu/ for more details # -version: 2 -jobs: - build: +version: 2.1 + +executors: + ubuntu: machine: image: ubuntu-1604:202004-01 - working_directory: ~/repo +jobs: + build: + executor: ubuntu + environment: # Customize the JVM maximum heap limit JVM_OPTS: -Xmx3200m @@ -40,3 +44,19 @@ jobs: - store_test_results: path: target + + style: + executor: ubuntu + + steps: + - checkout + - run: + name: Install cljstyle + environment: + CLJSTYLE_VERSION: 0.13.0 + command: | + wget https://github.com/greglook/cljstyle/releases/download/${CLJSTYLE_VERSION}/cljstyle_${CLJSTYLE_VERSION}_linux.tar.gz + tar -xzf cljstyle_${CLJSTYLE_VERSION}_linux.tar.gz + - run: + name: Check style + command: "./cljstyle check --report" diff --git a/src/clj_test_containers/core.clj b/src/clj_test_containers/core.clj index 640a28d..9d84860 100644 --- a/src/clj_test_containers/core.clj +++ b/src/clj_test_containers/core.clj @@ -1,10 +1,19 @@ (ns clj-test-containers.core - (:require [clojure.java.io :as io]) - (:import [org.testcontainers.containers GenericContainer] - [org.testcontainers.utility MountableFile] - [org.testcontainers.containers BindMode Network] - [org.testcontainers.images.builder ImageFromDockerfile] - [java.nio.file Path Paths])) + (:require + [clojure.java.io :as io]) + (:import + (java.nio.file + Path + Paths) + (org.testcontainers.containers + BindMode + GenericContainer + Network) + (org.testcontainers.images.builder + ImageFromDockerfile) + (org.testcontainers.utility + MountableFile))) + (defn- resolve-bind-mode [bind-mode] @@ -12,6 +21,7 @@ BindMode/READ_WRITE BindMode/READ_ONLY)) + (defn init "Sets the properties for a testcontainer instance" [{:keys [container exposed-ports env-vars command network network-aliases]}] @@ -35,6 +45,7 @@ :host (.getHost container) :network network}) + (defn create "Creates a generic testcontainer and sets its properties" [{:keys [image-name] :as options}] @@ -42,6 +53,7 @@ (assoc options :container) init)) + (defn create-from-docker-file "Creates a testcontainer from a provided Dockerfile" [{:keys [docker-file] :as options}] @@ -50,6 +62,7 @@ (assoc options :container) init)) + (defn map-classpath-resource! "Maps a resource in the classpath to the given container path. Should be called before starting the container!" [container-config @@ -59,6 +72,7 @@ container-path (resolve-bind-mode mode)))) + (defn bind-filesystem! "Binds a source from the filesystem to the given container path. Should be called before starting the container!" [container-config {:keys [host-path container-path mode]}] @@ -68,6 +82,7 @@ container-path (resolve-bind-mode mode)))) + (defn copy-file-to-container! "Copies a file into the running container" [container-config @@ -86,6 +101,7 @@ mountable-file container-path)))) + (defn execute-command! "Executes a command in the container, and returns the result" [container-config command] @@ -96,6 +112,7 @@ :stdout (.getStdout result) :stderr (.getStderr result)})) + (defn start! "Starts the underlying testcontainer instance and adds new values to the response map, e.g. :id and :first-mapped-port" [container-config] @@ -107,6 +124,7 @@ (map (fn [port] [port (.getMappedPort container port)]) (:exposed-ports container-config))))))) + (defn stop! "Stops the underlying container" [container-config] @@ -118,8 +136,8 @@ (defn- build-network [{:keys [ipv6 driver]}] - (let [builder (Network/builder)] - + (let [builder (Network/builder)] + (when ipv6 (.enableIpv6 builder true)) @@ -127,12 +145,13 @@ (.driver builder driver)) (let [network (.build builder)] - {:network network + {: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" ([] diff --git a/test/clj_test_containers/core_test.clj b/test/clj_test_containers/core_test.clj index 52499ff..e17a6fb 100644 --- a/test/clj_test_containers/core_test.clj +++ b/test/clj_test_containers/core_test.clj @@ -1,13 +1,17 @@ (ns clj-test-containers.core-test - (:require [clojure.test :refer :all] - [clj-test-containers.core :refer :all]) - (:import [org.testcontainers.containers PostgreSQLContainer])) + (:require + [clj-test-containers.core :refer :all] + [clojure.test :refer :all]) + (:import + (org.testcontainers.containers + PostgreSQLContainer))) + (deftest create-test (testing "Testing basic testcontainer generic image initialisation" (let [container (create {:image-name "postgres:12.2" - :exposed-ports [5432] + :exposed-ports [5432] :env-vars {"POSTGRES_PASSWORD" "pw"}}) initialized-container (start! container) stopped-container (stop! container)] @@ -37,6 +41,7 @@ (is (= 0 (:exit-code result))) (is (= "root\n" (:stdout result)))))) + (deftest execute-command-in-container (testing "Executing a command in the running Docker container" @@ -49,11 +54,12 @@ (is (= 0 (:exit-code result))) (is (= "root\n" (:stdout result)))))) + (deftest init-volume-test (testing "Testing mapping of a classpath resource" (let [container (-> (create {:image-name "postgres:12.2" - :exposed-ports [5432] + :exposed-ports [5432] :env-vars {"POSTGRES_PASSWORD" "pw"}}) (map-classpath-resource! {:resource-path "test.sql" :container-path "/opt/test.sql" @@ -70,7 +76,7 @@ (testing "Testing mapping of a filesystem-binding" (let [container (-> (create {:image-name "postgres:12.2" - :exposed-ports [5432] + :exposed-ports [5432] :env-vars {"POSTGRES_PASSWORD" "pw"}}) (bind-filesystem! {:host-path "." :container-path "/opt" @@ -87,7 +93,7 @@ (testing "Copying a file from the host into the container" (let [container (-> (create {:image-name "postgres:12.2" - :exposed-ports [5432] + :exposed-ports [5432] :env-vars {"POSTGRES_PASSWORD" "pw"}}) (copy-file-to-container! {:path "test.sql" :container-path "/opt/test.sql" @@ -104,7 +110,7 @@ (testing "Copying a file from the classpath into the container" (let [container (-> (create {:image-name "postgres:12.2" - :exposed-ports [5432] + :exposed-ports [5432] :env-vars {"POSTGRES_PASSWORD" "pw"}}) (copy-file-to-container! {:path "test.sql" :container-path "/opt/test.sql" @@ -121,15 +127,15 @@ (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"]}) + "-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"]}) @@ -138,6 +144,6 @@ 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))))))