Use of java :import to keep the code clean (#12)

* Fix the method name

* Use :import for Java classes

To make the code more concise and less verbose

- Tidy up the whitespaces just a bit

* Add lein-cljfmt plugin

* Revert "Add lein-cljfmt plugin"

This reverts commit bff2e04625203e95b76b734e73ad58064f83944e.
This commit is contained in:
Burin Choomnuan 2020-07-16 16:44:26 -04:00 committed by GitHub
parent 78d8edf540
commit a3fda15a49
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 52 deletions

View file

@ -18,12 +18,12 @@ The library provides a set of functions to interact with the testcontainers. A s
```clojure
(require '[clj-test-containers.core :as tc])
(def container (-> (tc/create {:image-name "postgres:12.1"
:exposed-ports [5432]
(def container (-> (tc/create {:image-name "postgres:12.1"
:exposed-ports [5432]
:env-vars {"POSTGRES_PASSWORD" "verysecret"}})
(tc/bind-filesystem {:host-path "/tmp"
:container-path "/opt"
:mode :read-only})
(tc/bind-filesystem! {:host-path "/tmp"
:container-path "/opt"
:mode :read-only})
(tc/start!))
(do-database-testing (:host container)

View file

@ -1,89 +1,95 @@
(ns clj-test-containers.core)
(ns clj-test-containers.core
(:import [org.testcontainers.containers
GenericContainer]
[org.testcontainers.utility
MountableFile]
[org.testcontainers.containers BindMode]))
(defn- resolve-bind-mode [bind-mode]
(defn- resolve-bind-mode
[bind-mode]
(if (= :read-write bind-mode)
org.testcontainers.containers.BindMode/READ_WRITE
org.testcontainers.containers.BindMode/READ_ONLY))
BindMode/READ_WRITE
BindMode/READ_ONLY))
(defn create
"Sets the properties for a testcontainer instance"
[{:keys [image-name exposed-ports env-vars command]}]
(let [container (org.testcontainers.containers.GenericContainer. image-name)]
(let [container (GenericContainer. image-name)]
(.setExposedPorts container (map int exposed-ports))
(if (some? env-vars)
(doseq [pair env-vars]
(.addEnv container (first pair) (second pair))))
(if (some? command)
(.setCommand container command))
{:container container
:exposed-ports (.getExposedPorts container)
:env-vars (.getEnvMap container)
:host (.getHost container)}))
(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!"
[container-config
[container-config
{:keys [resource-path container-path mode]}]
(assoc container-config :container (.withClasspathResourceMapping (:container container-config)
resource-path
container-path
(assoc container-config :container (.withClasspathResourceMapping (:container container-config)
resource-path
container-path
(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!"
[container-config
{:keys [host-path container-path mode]}]
(assoc container-config :container (.withFileSystemBind (:container container-config)
host-path
container-path
(resolve-bind-mode mode))))
[container-config {:keys [host-path container-path mode]}]
(assoc container-config
:container (.withFileSystemBind (:container container-config)
host-path
container-path
(resolve-bind-mode mode))))
(defn copy-file-to-container!
"Copies a file into the running container"
[container-conf
[container-config
{:keys [container-path path type]}]
(let [mountable-file (cond
(= :classpath-resource type) (org.testcontainers.utility.MountableFile/forClasspathResource path)
(= :host-path type) (org.testcontainers.utility.MountableFile/forHostPath path)
:else :error)]
(assoc container-conf
:container
(.withCopyFileToContainer (:container container-conf)
mountable-file
(let [mountable-file (cond
(= :classpath-resource type)
(MountableFile/forClasspathResource path)
(= :host-path type)
(MountableFile/forHostPath path)
:else
:error)]
(assoc container-config
:container
(.withCopyFileToContainer (:container container-config)
mountable-file
container-path))))
(defn execute-command!
(defn execute-command!
"Executes a command in the container, and returns the result"
[container-conf command]
(let [container (:container container-conf)
[container-config command]
(let [container (:container container-config)
result (.execInContainer container
(into-array command))]
(into-array command))]
{:exit-code (.getExitCode result)
:stdout (.getStdout 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"
[container-conf]
(let [container (:container container-conf)]
[container-config]
(let [container (:container container-config)]
(.start container)
(-> container-conf
(-> container-config
(assoc :id (.getContainerId container))
(assoc :mapped-ports (into {} (map (fn [port] [port
(.getMappedPort container port)])
(:exposed-ports container-conf)))))))
(assoc :mapped-ports (into {}
(map (fn [port] [port (.getMappedPort container port)])
(:exposed-ports container-config)))))))
(defn stop!
"Stops the underlying container"
[container-conf]
(.stop (:container container-conf))
(-> container-conf
[container-config]
(.stop (:container container-config))
(-> container-config
(dissoc :id)
(dissoc :mapped-ports)))