testcontainers-clj/src/clj_test_containers/core.clj

102 lines
3.7 KiB
Clojure
Raw Normal View History

2020-06-06 15:30:00 +00:00
(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))
(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}]
(let [container (org.testcontainers.containers.GenericContainer. image-name)]
(.setExposedPorts container (map int exposed-ports))
2020-06-06 11:57:31 +00:00
(if (some? env-vars)
(doseq [pair env-vars]
(.addEnv container (first pair) (second pair))))
(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-06 11:57:31 +00:00
2020-06-06 15:30:00 +00:00
(defn map-classpath-resource!
"Maps a resource in the classpath to the given container path. Should be called before starting the container!"
[container-config
{resource-path :resource-path
container-path :container-path
mode :mode}]
2020-06-06 11:57:31 +00:00
2020-06-06 15:30:00 +00:00
(assoc container-config :container (.withClasspathResourceMapping (:container container-config)
resource-path
container-path
(resolve-bind-mode mode))))
(defn bind-filesystem!
"Binds a source from the filesystem to the given container path. Should be called before starting the container!"
[container-config
{host-path :host-path
container-path :container-path
mode :mode}]
2020-06-06 11:57:31 +00:00
2020-06-06 15:30:00 +00:00
(assoc container-config :container (.withFileSystemBind (:container container-config)
host-path
container-path
(resolve-bind-mode mode))))
2020-06-06 11:57:31 +00:00
(defn copy-file-to-container!
2020-06-06 11:57:31 +00:00
"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 15:30:00 +00:00
(defn execute-command!
"Executes a command in the container, and returns the result"
[container-conf command]
(let [container (:container container-conf)
result (.execInContainer container
(into-array command))]
{:exit-code (.getExitCode result)
:stdout (.getStdout result)
:stderr (.getStderr result)}))
(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