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:
parent
78d8edf540
commit
a3fda15a49
2 changed files with 58 additions and 52 deletions
10
README.md
10
README.md
|
|
@ -18,12 +18,12 @@ The library provides a set of functions to interact with the testcontainers. A s
|
||||||
```clojure
|
```clojure
|
||||||
(require '[clj-test-containers.core :as tc])
|
(require '[clj-test-containers.core :as tc])
|
||||||
|
|
||||||
(def container (-> (tc/create {:image-name "postgres:12.1"
|
(def container (-> (tc/create {:image-name "postgres:12.1"
|
||||||
:exposed-ports [5432]
|
:exposed-ports [5432]
|
||||||
:env-vars {"POSTGRES_PASSWORD" "verysecret"}})
|
:env-vars {"POSTGRES_PASSWORD" "verysecret"}})
|
||||||
(tc/bind-filesystem {:host-path "/tmp"
|
(tc/bind-filesystem! {:host-path "/tmp"
|
||||||
:container-path "/opt"
|
:container-path "/opt"
|
||||||
:mode :read-only})
|
:mode :read-only})
|
||||||
(tc/start!))
|
(tc/start!))
|
||||||
|
|
||||||
(do-database-testing (:host container)
|
(do-database-testing (:host container)
|
||||||
|
|
|
||||||
|
|
@ -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)
|
(if (= :read-write bind-mode)
|
||||||
org.testcontainers.containers.BindMode/READ_WRITE
|
BindMode/READ_WRITE
|
||||||
org.testcontainers.containers.BindMode/READ_ONLY))
|
BindMode/READ_ONLY))
|
||||||
|
|
||||||
(defn create
|
(defn create
|
||||||
"Sets the properties for a testcontainer instance"
|
"Sets the properties for a testcontainer instance"
|
||||||
[{:keys [image-name exposed-ports env-vars command]}]
|
[{: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))
|
(.setExposedPorts container (map int exposed-ports))
|
||||||
|
|
||||||
|
|
||||||
(if (some? env-vars)
|
(if (some? env-vars)
|
||||||
(doseq [pair env-vars]
|
(doseq [pair env-vars]
|
||||||
(.addEnv container (first pair) (second pair))))
|
(.addEnv container (first pair) (second pair))))
|
||||||
|
|
||||||
(if (some? command)
|
(if (some? command)
|
||||||
(.setCommand container command))
|
(.setCommand container command))
|
||||||
|
|
||||||
{:container container
|
{:container container
|
||||||
:exposed-ports (.getExposedPorts container)
|
:exposed-ports (.getExposedPorts container)
|
||||||
:env-vars (.getEnvMap container)
|
:env-vars (.getEnvMap container)
|
||||||
:host (.getHost 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!"
|
"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]}]
|
{:keys [resource-path container-path mode]}]
|
||||||
(assoc container-config :container (.withClasspathResourceMapping (:container container-config)
|
(assoc container-config :container (.withClasspathResourceMapping (:container container-config)
|
||||||
resource-path
|
resource-path
|
||||||
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
|
[container-config {:keys [host-path container-path mode]}]
|
||||||
{:keys [host-path container-path mode]}]
|
(assoc container-config
|
||||||
(assoc container-config :container (.withFileSystemBind (:container container-config)
|
:container (.withFileSystemBind (:container container-config)
|
||||||
host-path
|
host-path
|
||||||
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-conf
|
[container-config
|
||||||
{:keys [container-path path type]}]
|
{:keys [container-path path type]}]
|
||||||
(let [mountable-file (cond
|
(let [mountable-file (cond
|
||||||
(= :classpath-resource type) (org.testcontainers.utility.MountableFile/forClasspathResource path)
|
(= :classpath-resource type)
|
||||||
(= :host-path type) (org.testcontainers.utility.MountableFile/forHostPath path)
|
(MountableFile/forClasspathResource path)
|
||||||
:else :error)]
|
|
||||||
(assoc container-conf
|
(= :host-path type)
|
||||||
:container
|
(MountableFile/forHostPath path)
|
||||||
(.withCopyFileToContainer (:container container-conf)
|
:else
|
||||||
mountable-file
|
:error)]
|
||||||
|
(assoc container-config
|
||||||
|
:container
|
||||||
|
(.withCopyFileToContainer (:container container-config)
|
||||||
|
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-conf command]
|
[container-config command]
|
||||||
(let [container (:container container-conf)
|
(let [container (:container container-config)
|
||||||
result (.execInContainer container
|
result (.execInContainer container
|
||||||
(into-array command))]
|
(into-array command))]
|
||||||
{:exit-code (.getExitCode result)
|
{:exit-code (.getExitCode result)
|
||||||
: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-conf]
|
[container-config]
|
||||||
(let [container (:container container-conf)]
|
(let [container (:container container-config)]
|
||||||
(.start container)
|
(.start container)
|
||||||
(-> container-conf
|
(-> container-config
|
||||||
(assoc :id (.getContainerId container))
|
(assoc :id (.getContainerId container))
|
||||||
(assoc :mapped-ports (into {} (map (fn [port] [port
|
(assoc :mapped-ports (into {}
|
||||||
(.getMappedPort container port)])
|
(map (fn [port] [port (.getMappedPort container port)])
|
||||||
(:exposed-ports container-conf)))))))
|
(:exposed-ports container-config)))))))
|
||||||
|
|
||||||
(defn stop!
|
(defn stop!
|
||||||
"Stops the underlying container"
|
"Stops the underlying container"
|
||||||
[container-conf]
|
[container-config]
|
||||||
(.stop (:container container-conf))
|
(.stop (:container container-config))
|
||||||
(-> container-conf
|
(-> container-config
|
||||||
(dissoc :id)
|
(dissoc :id)
|
||||||
(dissoc :mapped-ports)))
|
(dissoc :mapped-ports)))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue