#6 - Enabled command execution in the container
Also used it to enhance integration tests and make them more precise
This commit is contained in:
parent
dee0c8a572
commit
6dbebf15bd
2 changed files with 32 additions and 3 deletions
|
|
@ -67,6 +67,15 @@
|
||||||
mountable-file
|
mountable-file
|
||||||
container-path))))
|
container-path))))
|
||||||
|
|
||||||
|
(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)}))
|
||||||
|
|
||||||
(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-conf]
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,18 @@
|
||||||
(is (nil? (:id stopped-container)))
|
(is (nil? (:id stopped-container)))
|
||||||
(is (nil? (:mapped-ports stopped-container))))))
|
(is (nil? (:mapped-ports stopped-container))))))
|
||||||
|
|
||||||
|
(deftest execute-command-in-container
|
||||||
|
|
||||||
|
(testing "Executing a command in the running Docker container"
|
||||||
|
(let [container (create {:image-name "postgres:12.2"
|
||||||
|
:exposed-ports [5432]
|
||||||
|
:env-vars {"POSTGRES_PASSWORD" "pw"}})
|
||||||
|
initialized-container (start container)
|
||||||
|
result (execute-command initialized-container ["whoami"])
|
||||||
|
stopped-container (stop container)]
|
||||||
|
(is (= 0 (:exit-code result)))
|
||||||
|
(is (= "root\n" (:stdout result))))))
|
||||||
|
|
||||||
(deftest init-with-volume-test
|
(deftest init-with-volume-test
|
||||||
|
|
||||||
(testing "Testing mapping of a classpath resource"
|
(testing "Testing mapping of a classpath resource"
|
||||||
|
|
@ -25,10 +37,12 @@
|
||||||
:container-path "/opt/test.sql"
|
:container-path "/opt/test.sql"
|
||||||
:mode :read-only}}))
|
:mode :read-only}}))
|
||||||
initialized-container (start container)
|
initialized-container (start container)
|
||||||
|
file-check (execute-command initialized-container ["tail" "/opt/test.sql"])
|
||||||
stopped-container (stop container)]
|
stopped-container (stop container)]
|
||||||
(is (some? (:id initialized-container)))
|
(is (some? (:id initialized-container)))
|
||||||
(is (some? (:mapped-ports initialized-container)))
|
(is (some? (:mapped-ports initialized-container)))
|
||||||
(is (some? (get (:mapped-ports initialized-container) 5432)))
|
(is (some? (get (:mapped-ports initialized-container) 5432)))
|
||||||
|
(is (= 0 (:exit-code file-check)))
|
||||||
(is (nil? (:id stopped-container)))
|
(is (nil? (:id stopped-container)))
|
||||||
(is (nil? (:mapped-ports stopped-container)))))
|
(is (nil? (:mapped-ports stopped-container)))))
|
||||||
|
|
||||||
|
|
@ -36,14 +50,16 @@
|
||||||
(let [container (-> (create {:image-name "postgres:12.2"
|
(let [container (-> (create {:image-name "postgres:12.2"
|
||||||
:exposed-ports [5432]
|
:exposed-ports [5432]
|
||||||
:env-vars {"POSTGRES_PASSWORD" "pw"}})
|
:env-vars {"POSTGRES_PASSWORD" "pw"}})
|
||||||
(configure-volume {:file-system-bind {:host-path "/tmp"
|
(configure-volume {:file-system-bind {:host-path "."
|
||||||
:container-path "/opt"
|
:container-path "/opt"
|
||||||
:mode :read-only}}))
|
:mode :read-only}}))
|
||||||
initialized-container (start container)
|
initialized-container (start container)
|
||||||
|
file-check (execute-command initialized-container ["tail" "/opt/README.md"])
|
||||||
stopped-container (stop container)]
|
stopped-container (stop container)]
|
||||||
(is (some? (:id initialized-container)))
|
(is (some? (:id initialized-container)))
|
||||||
(is (some? (:mapped-ports initialized-container)))
|
(is (some? (:mapped-ports initialized-container)))
|
||||||
(is (some? (get (:mapped-ports initialized-container) 5432)))
|
(is (some? (get (:mapped-ports initialized-container) 5432)))
|
||||||
|
(is (= 0 (:exit-code file-check)))
|
||||||
(is (nil? (:id stopped-container)))
|
(is (nil? (:id stopped-container)))
|
||||||
(is (nil? (:mapped-ports stopped-container)))))
|
(is (nil? (:mapped-ports stopped-container)))))
|
||||||
|
|
||||||
|
|
@ -52,13 +68,15 @@
|
||||||
:exposed-ports [5432]
|
:exposed-ports [5432]
|
||||||
:env-vars {"POSTGRES_PASSWORD" "pw"}})
|
:env-vars {"POSTGRES_PASSWORD" "pw"}})
|
||||||
(copy-file-to-container {:path "test.sql"
|
(copy-file-to-container {:path "test.sql"
|
||||||
:container-path "/opt"
|
:container-path "/opt/test.sql"
|
||||||
:type :host-path}))
|
:type :host-path}))
|
||||||
initialized-container (start container)
|
initialized-container (start container)
|
||||||
|
file-check (execute-command initialized-container ["tail" "/opt/test.sql"])
|
||||||
stopped-container (stop container)]
|
stopped-container (stop container)]
|
||||||
(is (some? (:id initialized-container)))
|
(is (some? (:id initialized-container)))
|
||||||
(is (some? (:mapped-ports initialized-container)))
|
(is (some? (:mapped-ports initialized-container)))
|
||||||
(is (some? (get (:mapped-ports initialized-container) 5432)))
|
(is (some? (get (:mapped-ports initialized-container) 5432)))
|
||||||
|
(is (= 0 (:exit-code file-check)))
|
||||||
(is (nil? (:id stopped-container)))
|
(is (nil? (:id stopped-container)))
|
||||||
(is (nil? (:mapped-ports stopped-container)))))
|
(is (nil? (:mapped-ports stopped-container)))))
|
||||||
|
|
||||||
|
|
@ -67,12 +85,14 @@
|
||||||
:exposed-ports [5432]
|
:exposed-ports [5432]
|
||||||
:env-vars {"POSTGRES_PASSWORD" "pw"}})
|
:env-vars {"POSTGRES_PASSWORD" "pw"}})
|
||||||
(copy-file-to-container {:path "test.sql"
|
(copy-file-to-container {:path "test.sql"
|
||||||
:container-path "/opt"
|
:container-path "/opt/test.sql"
|
||||||
:type :classpath-resource}))
|
:type :classpath-resource}))
|
||||||
initialized-container (start container)
|
initialized-container (start container)
|
||||||
|
file-check (execute-command initialized-container ["tail" "/opt/test.sql"])
|
||||||
stopped-container (stop container)]
|
stopped-container (stop container)]
|
||||||
(is (some? (:id initialized-container)))
|
(is (some? (:id initialized-container)))
|
||||||
(is (some? (:mapped-ports initialized-container)))
|
(is (some? (:mapped-ports initialized-container)))
|
||||||
(is (some? (get (:mapped-ports initialized-container) 5432)))
|
(is (some? (get (:mapped-ports initialized-container) 5432)))
|
||||||
|
(is (= 0 (:exit-code file-check)))
|
||||||
(is (nil? (:id stopped-container)))
|
(is (nil? (:id stopped-container)))
|
||||||
(is (nil? (:mapped-ports stopped-container))))))
|
(is (nil? (:mapped-ports stopped-container))))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue