2020-07-16 20:44:26 +00:00
|
|
|
(ns clj-test-containers.core
|
2020-08-05 05:13:31 +00:00
|
|
|
(:require [clojure.java.io :as io])
|
|
|
|
|
(:import [org.testcontainers.containers GenericContainer]
|
|
|
|
|
[org.testcontainers.utility MountableFile]
|
|
|
|
|
[org.testcontainers.containers BindMode]
|
|
|
|
|
[org.testcontainers.images.builder ImageFromDockerfile]
|
|
|
|
|
[java.nio.file Path Paths]))
|
2020-06-04 20:51:33 +00:00
|
|
|
|
2020-07-16 20:44:26 +00:00
|
|
|
(defn- resolve-bind-mode
|
|
|
|
|
[bind-mode]
|
2020-06-04 20:51:33 +00:00
|
|
|
(if (= :read-write bind-mode)
|
2020-07-16 20:44:26 +00:00
|
|
|
BindMode/READ_WRITE
|
|
|
|
|
BindMode/READ_ONLY))
|
2020-06-04 20:51:33 +00:00
|
|
|
|
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"
|
2020-06-13 12:49:07 +00:00
|
|
|
[{:keys [image-name exposed-ports env-vars command]}]
|
2020-07-16 20:44:26 +00:00
|
|
|
(let [container (GenericContainer. image-name)]
|
2020-06-04 10:35:16 +00:00
|
|
|
(.setExposedPorts container (map int exposed-ports))
|
2020-07-16 20:44:26 +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-07-16 20:44:26 +00:00
|
|
|
|
2020-06-04 10:35:16 +00:00
|
|
|
(if (some? command)
|
|
|
|
|
(.setCommand container command))
|
2020-07-16 20:44:26 +00:00
|
|
|
|
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-08-05 05:13:31 +00:00
|
|
|
(defn create-from-docker-file
|
|
|
|
|
[{:keys [exposed-ports env-vars command docker-file]
|
|
|
|
|
:or {docker-file "Dockerfile"}}]
|
|
|
|
|
(let [docker-image (-> (ImageFromDockerfile.)
|
|
|
|
|
(.withDockerfile (Paths/get "." (into-array [docker-file]))))
|
|
|
|
|
container (GenericContainer. docker-image)]
|
|
|
|
|
(.setExposedPorts container (map int exposed-ports))
|
|
|
|
|
(if (some? env-vars)
|
|
|
|
|
(doseq [[k v] env-vars]
|
|
|
|
|
(.addEnv container k v)))
|
|
|
|
|
(if (some? command)
|
|
|
|
|
(.setCommand container command))
|
|
|
|
|
{:container container
|
|
|
|
|
:exposed-ports (.getExposedPorts container)
|
|
|
|
|
:env-vars (.getEnvMap container)
|
|
|
|
|
:host (.getHost container)}))
|
|
|
|
|
|
2020-07-16 20:44:26 +00:00
|
|
|
(defn map-classpath-resource!
|
2020-06-06 15:30:00 +00:00
|
|
|
"Maps a resource in the classpath to the given container path. Should be called before starting the container!"
|
2020-07-16 20:44:26 +00:00
|
|
|
[container-config
|
2020-06-13 12:49:07 +00:00
|
|
|
{:keys [resource-path container-path mode]}]
|
2020-07-16 20:44:26 +00:00
|
|
|
(assoc container-config :container (.withClasspathResourceMapping (:container container-config)
|
|
|
|
|
resource-path
|
|
|
|
|
container-path
|
2020-06-06 15:30:00 +00:00
|
|
|
(resolve-bind-mode mode))))
|
|
|
|
|
|
2020-07-16 20:44:26 +00:00
|
|
|
(defn bind-filesystem!
|
2020-06-06 15:30:00 +00:00
|
|
|
"Binds a source from the filesystem to the given container path. Should be called before starting the container!"
|
2020-07-16 20:44:26 +00:00
|
|
|
[container-config {:keys [host-path container-path mode]}]
|
|
|
|
|
(assoc container-config
|
|
|
|
|
:container (.withFileSystemBind (:container container-config)
|
|
|
|
|
host-path
|
|
|
|
|
container-path
|
|
|
|
|
(resolve-bind-mode mode))))
|
2020-06-06 11:57:31 +00:00
|
|
|
|
2020-06-06 13:18:31 +00:00
|
|
|
(defn copy-file-to-container!
|
2020-06-06 11:57:31 +00:00
|
|
|
"Copies a file into the running container"
|
2020-07-16 20:44:26 +00:00
|
|
|
[container-config
|
2020-06-13 12:49:07 +00:00
|
|
|
{:keys [container-path path type]}]
|
2020-07-16 20:44:26 +00:00
|
|
|
(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
|
2020-06-06 11:57:31 +00:00
|
|
|
container-path))))
|
|
|
|
|
|
2020-07-16 20:44:26 +00:00
|
|
|
(defn execute-command!
|
2020-06-06 15:30:00 +00:00
|
|
|
"Executes a command in the container, and returns the result"
|
2020-07-16 20:44:26 +00:00
|
|
|
[container-config command]
|
|
|
|
|
(let [container (:container container-config)
|
2020-06-06 12:43:11 +00:00
|
|
|
result (.execInContainer container
|
2020-07-16 20:44:26 +00:00
|
|
|
(into-array command))]
|
2020-06-06 12:43:11 +00:00
|
|
|
{:exit-code (.getExitCode result)
|
|
|
|
|
:stdout (.getStdout result)
|
|
|
|
|
:stderr (.getStderr result)}))
|
|
|
|
|
|
2020-07-16 20:44:26 +00:00
|
|
|
(defn start!
|
2020-06-04 10:35:16 +00:00
|
|
|
"Starts the underlying testcontainer instance and adds new values to the response map, e.g. :id and :first-mapped-port"
|
2020-07-16 20:44:26 +00:00
|
|
|
[container-config]
|
|
|
|
|
(let [container (:container container-config)]
|
2020-06-04 10:35:16 +00:00
|
|
|
(.start container)
|
2020-07-16 20:44:26 +00:00
|
|
|
(-> container-config
|
2020-06-04 10:35:16 +00:00
|
|
|
(assoc :id (.getContainerId container))
|
2020-07-16 20:44:26 +00:00
|
|
|
(assoc :mapped-ports (into {}
|
|
|
|
|
(map (fn [port] [port (.getMappedPort container port)])
|
|
|
|
|
(:exposed-ports container-config)))))))
|
2020-06-04 10:35:16 +00:00
|
|
|
|
2020-06-06 13:18:31 +00:00
|
|
|
(defn stop!
|
2020-06-04 10:35:16 +00:00
|
|
|
"Stops the underlying container"
|
2020-07-16 20:44:26 +00:00
|
|
|
[container-config]
|
|
|
|
|
(.stop (:container container-config))
|
|
|
|
|
(-> container-config
|
2020-06-04 11:04:30 +00:00
|
|
|
(dissoc :id)
|
|
|
|
|
(dissoc :mapped-ports)))
|