2020-06-04 20:51:33 +00:00
|
|
|
5(ns clj-test-containers.core)
|
|
|
|
|
|
|
|
|
|
(defn- resolve-bind-mode [bind-mode]
|
|
|
|
|
(if (= :read-write bind-mode)
|
|
|
|
|
org.testcontainers.containers.BindMode/READ_WRITE
|
|
|
|
|
org.testcontainers.containers.BindMode/READ_ONLY))
|
|
|
|
|
|
2020-06-04 10:35:16 +00:00
|
|
|
(defn create
|
2020-06-04 08:58:10 +00:00
|
|
|
"Sets the properties for a testcontainer instance"
|
|
|
|
|
[{image-name :image-name
|
|
|
|
|
exposed-ports :exposed-ports
|
|
|
|
|
env-vars :env-vars
|
2020-06-06 11:57:31 +00:00
|
|
|
command :command}]
|
2020-06-04 10:35:16 +00:00
|
|
|
|
|
|
|
|
(let [container (org.testcontainers.containers.GenericContainer. image-name)]
|
|
|
|
|
(.setExposedPorts container (map int exposed-ports))
|
|
|
|
|
|
2020-06-06 11:57:31 +00:00
|
|
|
|
2020-06-04 20:51:33 +00:00
|
|
|
(if (some? env-vars)
|
|
|
|
|
(doseq [pair env-vars]
|
|
|
|
|
(.addEnv container (first pair) (second pair))))
|
2020-06-04 10:35:16 +00:00
|
|
|
|
|
|
|
|
(if (some? command)
|
|
|
|
|
(.setCommand container command))
|
|
|
|
|
|
2020-06-04 08:58:10 +00:00
|
|
|
{:container container
|
|
|
|
|
:exposed-ports (.getExposedPorts container)
|
|
|
|
|
:env-vars (.getEnvMap container)
|
|
|
|
|
:host (.getHost container)}))
|
2020-06-04 10:35:16 +00:00
|
|
|
|
2020-06-06 11:57:31 +00:00
|
|
|
|
|
|
|
|
(defn configure-volume [container-config
|
|
|
|
|
{classpath-resource-mapping :classpath-resource-mapping
|
|
|
|
|
file-system-bind :file-system-bind}]
|
|
|
|
|
|
|
|
|
|
(when-let [{resource-path :resource-path
|
|
|
|
|
container-path :container-path
|
|
|
|
|
mode :mode} classpath-resource-mapping]
|
|
|
|
|
(.withClasspathResourceMapping (:container container-config)
|
|
|
|
|
resource-path
|
|
|
|
|
container-path
|
|
|
|
|
(resolve-bind-mode mode)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(when-let [{host-path :host-path
|
|
|
|
|
container-path :container-path
|
|
|
|
|
mode :mode} file-system-bind]
|
|
|
|
|
(.withFileSystemBind (:container container-config)
|
|
|
|
|
host-path
|
|
|
|
|
container-path))
|
|
|
|
|
|
|
|
|
|
container-config)
|
|
|
|
|
|
|
|
|
|
(defn copy-file-to-container
|
|
|
|
|
"Copies a file into the running container"
|
|
|
|
|
[container-conf {container-path :container-path
|
|
|
|
|
path :path
|
|
|
|
|
type :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
|
|
|
|
|
container-path))))
|
|
|
|
|
|
2020-06-06 12:43:11 +00:00
|
|
|
(defn execute-command
|
|
|
|
|
[container-conf command]
|
|
|
|
|
(let [container (:container container-conf)
|
|
|
|
|
result (.execInContainer container
|
|
|
|
|
(into-array command))]
|
|
|
|
|
{:exit-code (.getExitCode result)
|
|
|
|
|
:stdout (.getStdout result)
|
|
|
|
|
:stderr (.getStderr result)}))
|
|
|
|
|
|
2020-06-04 10:35:16 +00:00
|
|
|
(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)]
|
|
|
|
|
(.start container)
|
|
|
|
|
(-> container-conf
|
|
|
|
|
(assoc :id (.getContainerId container))
|
|
|
|
|
(assoc :mapped-ports (into {} (map (fn [port] [port
|
|
|
|
|
(.getMappedPort container port)])
|
|
|
|
|
(:exposed-ports container-conf)))))))
|
|
|
|
|
|
|
|
|
|
(defn stop
|
|
|
|
|
"Stops the underlying container"
|
2020-06-04 11:04:30 +00:00
|
|
|
[container-conf]
|
|
|
|
|
(.stop (:container container-conf))
|
|
|
|
|
(-> container-conf
|
|
|
|
|
(dissoc :id)
|
|
|
|
|
(dissoc :mapped-ports)))
|
2020-06-06 11:57:31 +00:00
|
|
|
|
|
|
|
|
|