diff --git a/src/clj_test_containers/core.clj b/src/clj_test_containers/core.clj index 65a5854..dc307a7 100644 --- a/src/clj_test_containers/core.clj +++ b/src/clj_test_containers/core.clj @@ -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))) - -