Install and configure cljstyle

This commit is contained in:
Rob Hanlon 2020-08-06 23:14:19 -07:00
parent 1e24824916
commit 7b52904635
No known key found for this signature in database
GPG key ID: 14C05B6156CB50E6
3 changed files with 72 additions and 27 deletions

View file

@ -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 version: 2.1
jobs:
build: executors:
ubuntu:
machine: machine:
image: ubuntu-1604:202004-01 image: ubuntu-1604:202004-01
working_directory: ~/repo working_directory: ~/repo
jobs:
build:
executor: ubuntu
environment: environment:
# Customize the JVM maximum heap limit # Customize the JVM maximum heap limit
JVM_OPTS: -Xmx3200m JVM_OPTS: -Xmx3200m
@ -40,3 +44,19 @@ jobs:
- store_test_results: - store_test_results:
path: target 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"

View file

@ -1,10 +1,19 @@
(ns clj-test-containers.core (ns clj-test-containers.core
(:require [clojure.java.io :as io]) (:require
(:import [org.testcontainers.containers GenericContainer] [clojure.java.io :as io])
[org.testcontainers.utility MountableFile] (:import
[org.testcontainers.containers BindMode Network] (java.nio.file
[org.testcontainers.images.builder ImageFromDockerfile] Path
[java.nio.file Path Paths])) Paths)
(org.testcontainers.containers
BindMode
GenericContainer
Network)
(org.testcontainers.images.builder
ImageFromDockerfile)
(org.testcontainers.utility
MountableFile)))
(defn- resolve-bind-mode (defn- resolve-bind-mode
[bind-mode] [bind-mode]
@ -12,6 +21,7 @@
BindMode/READ_WRITE BindMode/READ_WRITE
BindMode/READ_ONLY)) BindMode/READ_ONLY))
(defn init (defn init
"Sets the properties for a testcontainer instance" "Sets the properties for a testcontainer instance"
[{:keys [container exposed-ports env-vars command network network-aliases]}] [{:keys [container exposed-ports env-vars command network network-aliases]}]
@ -35,6 +45,7 @@
:host (.getHost container) :host (.getHost container)
:network network}) :network network})
(defn create (defn create
"Creates a generic testcontainer and sets its properties" "Creates a generic testcontainer and sets its properties"
[{:keys [image-name] :as options}] [{:keys [image-name] :as options}]
@ -42,6 +53,7 @@
(assoc options :container) (assoc options :container)
init)) init))
(defn create-from-docker-file (defn create-from-docker-file
"Creates a testcontainer from a provided Dockerfile" "Creates a testcontainer from a provided Dockerfile"
[{:keys [docker-file] :as options}] [{:keys [docker-file] :as options}]
@ -50,6 +62,7 @@
(assoc options :container) (assoc options :container)
init)) init))
(defn map-classpath-resource! (defn map-classpath-resource!
"Maps a resource in the classpath to the given container path. Should be called before starting the container!" "Maps a resource in the classpath to the given container path. Should be called before starting the container!"
[container-config [container-config
@ -59,6 +72,7 @@
container-path container-path
(resolve-bind-mode mode)))) (resolve-bind-mode mode))))
(defn bind-filesystem! (defn bind-filesystem!
"Binds a source from the filesystem to the given container path. Should be called before starting the container!" "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]}] [container-config {:keys [host-path container-path mode]}]
@ -68,6 +82,7 @@
container-path container-path
(resolve-bind-mode mode)))) (resolve-bind-mode mode))))
(defn copy-file-to-container! (defn copy-file-to-container!
"Copies a file into the running container" "Copies a file into the running container"
[container-config [container-config
@ -86,6 +101,7 @@
mountable-file mountable-file
container-path)))) container-path))))
(defn execute-command! (defn execute-command!
"Executes a command in the container, and returns the result" "Executes a command in the container, and returns the result"
[container-config command] [container-config command]
@ -96,6 +112,7 @@
:stdout (.getStdout result) :stdout (.getStdout result)
:stderr (.getStderr result)})) :stderr (.getStderr result)}))
(defn start! (defn start!
"Starts the underlying testcontainer instance and adds new values to the response map, e.g. :id and :first-mapped-port" "Starts the underlying testcontainer instance and adds new values to the response map, e.g. :id and :first-mapped-port"
[container-config] [container-config]
@ -107,6 +124,7 @@
(map (fn [port] [port (.getMappedPort container port)]) (map (fn [port] [port (.getMappedPort container port)])
(:exposed-ports container-config))))))) (:exposed-ports container-config)))))))
(defn stop! (defn stop!
"Stops the underlying container" "Stops the underlying container"
[container-config] [container-config]
@ -118,8 +136,8 @@
(defn- build-network (defn- build-network
[{:keys [ipv6 driver]}] [{:keys [ipv6 driver]}]
(let [builder (Network/builder)] (let [builder (Network/builder)]
(when ipv6 (when ipv6
(.enableIpv6 builder true)) (.enableIpv6 builder true))
@ -127,12 +145,13 @@
(.driver builder driver)) (.driver builder driver))
(let [network (.build builder)] (let [network (.build builder)]
{:network network {:network network
:id (.getId network) :id (.getId network)
:name (.getName network) :name (.getName network)
:ipv6 (.getEnableIpv6 network) :ipv6 (.getEnableIpv6 network)
:driver (.getDriver network)}))) :driver (.getDriver network)})))
(defn init-network (defn init-network
"Creates a network. The optional map accepts config values for enabling ipv6 and setting the driver" "Creates a network. The optional map accepts config values for enabling ipv6 and setting the driver"
([] ([]

View file

@ -1,13 +1,17 @@
(ns clj-test-containers.core-test (ns clj-test-containers.core-test
(:require [clojure.test :refer :all] (:require
[clj-test-containers.core :refer :all]) [clj-test-containers.core :refer :all]
(:import [org.testcontainers.containers PostgreSQLContainer])) [clojure.test :refer :all])
(:import
(org.testcontainers.containers
PostgreSQLContainer)))
(deftest create-test (deftest create-test
(testing "Testing basic testcontainer generic image initialisation" (testing "Testing basic testcontainer generic image initialisation"
(let [container (create {:image-name "postgres:12.2" (let [container (create {:image-name "postgres:12.2"
:exposed-ports [5432] :exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}}) :env-vars {"POSTGRES_PASSWORD" "pw"}})
initialized-container (start! container) initialized-container (start! container)
stopped-container (stop! container)] stopped-container (stop! container)]
@ -37,6 +41,7 @@
(is (= 0 (:exit-code result))) (is (= 0 (:exit-code result)))
(is (= "root\n" (:stdout result)))))) (is (= "root\n" (:stdout result))))))
(deftest execute-command-in-container (deftest execute-command-in-container
(testing "Executing a command in the running Docker container" (testing "Executing a command in the running Docker container"
@ -49,11 +54,12 @@
(is (= 0 (:exit-code result))) (is (= 0 (:exit-code result)))
(is (= "root\n" (:stdout result)))))) (is (= "root\n" (:stdout result))))))
(deftest init-volume-test (deftest init-volume-test
(testing "Testing mapping of a classpath resource" (testing "Testing mapping of a classpath resource"
(let [container (-> (create {:image-name "postgres:12.2" (let [container (-> (create {:image-name "postgres:12.2"
:exposed-ports [5432] :exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}}) :env-vars {"POSTGRES_PASSWORD" "pw"}})
(map-classpath-resource! {:resource-path "test.sql" (map-classpath-resource! {:resource-path "test.sql"
:container-path "/opt/test.sql" :container-path "/opt/test.sql"
@ -70,7 +76,7 @@
(testing "Testing mapping of a filesystem-binding" (testing "Testing mapping of a filesystem-binding"
(let [container (-> (create {:image-name "postgres:12.2" (let [container (-> (create {:image-name "postgres:12.2"
:exposed-ports [5432] :exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}}) :env-vars {"POSTGRES_PASSWORD" "pw"}})
(bind-filesystem! {:host-path "." (bind-filesystem! {:host-path "."
:container-path "/opt" :container-path "/opt"
@ -87,7 +93,7 @@
(testing "Copying a file from the host into the container" (testing "Copying a file from the host into the container"
(let [container (-> (create {:image-name "postgres:12.2" (let [container (-> (create {:image-name "postgres:12.2"
:exposed-ports [5432] :exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}}) :env-vars {"POSTGRES_PASSWORD" "pw"}})
(copy-file-to-container! {:path "test.sql" (copy-file-to-container! {:path "test.sql"
:container-path "/opt/test.sql" :container-path "/opt/test.sql"
@ -104,7 +110,7 @@
(testing "Copying a file from the classpath into the container" (testing "Copying a file from the classpath into the container"
(let [container (-> (create {:image-name "postgres:12.2" (let [container (-> (create {:image-name "postgres:12.2"
:exposed-ports [5432] :exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "pw"}}) :env-vars {"POSTGRES_PASSWORD" "pw"}})
(copy-file-to-container! {:path "test.sql" (copy-file-to-container! {:path "test.sql"
:container-path "/opt/test.sql" :container-path "/opt/test.sql"
@ -121,15 +127,15 @@
(deftest networking-test (deftest networking-test
(testing "Putting two containers into the same network and check their communication" (testing "Putting two containers into the same network and check their communication"
(let [network (init-network) (let [network (init-network)
server-container (create {:image-name "alpine:3.5" server-container (create {:image-name "alpine:3.5"
:network network :network network
:network-aliases ["foo"] :network-aliases ["foo"]
:command ["/bin/sh" :command ["/bin/sh"
"-c" "-c"
"while true ; do printf 'HTTP/1.1 200 OK\\n\\nyay' | nc -l -p 8080; done"]}) "while true ; do printf 'HTTP/1.1 200 OK\\n\\nyay' | nc -l -p 8080; done"]})
client-container (create {:image-name "alpine:3.5" client-container (create {:image-name "alpine:3.5"
:network network :network network
:command ["top"]}) :command ["top"]})
@ -138,6 +144,6 @@
response (execute-command! started-client ["wget", "-O", "-", "http://foo:8080"]) response (execute-command! started-client ["wget", "-O", "-", "http://foo:8080"])
stopped-server (stop! started-server) stopped-server (stop! started-server)
stopped-client (stop! started-client)] stopped-client (stop! started-client)]
(is (= 0 (:exit-code response))) (is (= 0 (:exit-code response)))
(is (= "yay" (:stdout response)))))) (is (= "yay" (:stdout response))))))