Add create-from-docker-file (#14)
* Add create-from-docker-file * Update code base on review/feedback Fix the following - Fix the import style in one line - Use constructure once when creating the docker-image - Use some? instead of if - Remove :env-vars from the test - Move resources to test section
This commit is contained in:
parent
a3fda15a49
commit
c30d02aaaf
5 changed files with 82 additions and 8 deletions
23
README.md
23
README.md
|
|
@ -31,6 +31,29 @@ The library provides a set of functions to interact with the testcontainers. A s
|
|||
|
||||
(tc/stop! container)
|
||||
```
|
||||
|
||||
To create a container using your local Dockerfile
|
||||
|
||||
```clojure
|
||||
;; Example via test
|
||||
(require '[clojure.test :refer :all])
|
||||
(require '[clj-test-containers.core :refer :all])
|
||||
|
||||
(testing "Testing basic testcontainer image creation from docker file"
|
||||
(let [container (create-from-docker-file {:env-vars
|
||||
{"FOO" "bar"
|
||||
"MAGIC_NUMBER" "42"}
|
||||
:exposed-ports [80]
|
||||
:docker-file "resources/Dockerfile"})
|
||||
initialized-container (start! container)
|
||||
stopped-container (stop! container)]
|
||||
(is (some? (:id initialized-container)))
|
||||
(is (some? (:mapped-ports initialized-container)))
|
||||
(is (some? (get (:mapped-ports initialized-container) 80)))
|
||||
(is (nil? (:id stopped-container)))
|
||||
(is (nil? (:mapped-ports stopped-container)))))
|
||||
```
|
||||
|
||||
## Functions and Properties
|
||||
|
||||
### create
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
(ns clj-test-containers.core
|
||||
(:import [org.testcontainers.containers
|
||||
GenericContainer]
|
||||
[org.testcontainers.utility
|
||||
MountableFile]
|
||||
[org.testcontainers.containers BindMode]))
|
||||
(: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]))
|
||||
|
||||
(defn- resolve-bind-mode
|
||||
[bind-mode]
|
||||
|
|
@ -29,6 +30,23 @@
|
|||
:env-vars (.getEnvMap container)
|
||||
:host (.getHost container)}))
|
||||
|
||||
(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)}))
|
||||
|
||||
(defn map-classpath-resource!
|
||||
"Maps a resource in the classpath to the given container path. Should be called before starting the container!"
|
||||
[container-config
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
[clj-test-containers.core :refer :all]))
|
||||
|
||||
(deftest init-test
|
||||
(testing "Testing basic testcontainer generic image initialisation"
|
||||
(testing "Testing basic testcontainer generic image initialization"
|
||||
(let [container (create {:image-name "postgres:12.2"
|
||||
:exposed-ports [5432]
|
||||
:env-vars {"POSTGRES_PASSWORD" "pw"}})
|
||||
|
|
@ -13,6 +13,17 @@
|
|||
(is (some? (:mapped-ports initialized-container)))
|
||||
(is (some? (get (:mapped-ports initialized-container) 5432)))
|
||||
(is (nil? (:id stopped-container)))
|
||||
(is (nil? (:mapped-ports stopped-container)))))
|
||||
|
||||
(testing "Testing basic testcontainer image creation from docker file"
|
||||
(let [container (create-from-docker-file {:exposed-ports [80]
|
||||
:docker-file "test/resources/Dockerfile"})
|
||||
initialized-container (start! container)
|
||||
stopped-container (stop! container)]
|
||||
(is (some? (:id initialized-container)))
|
||||
(is (some? (:mapped-ports initialized-container)))
|
||||
(is (some? (get (:mapped-ports initialized-container) 80)))
|
||||
(is (nil? (:id stopped-container)))
|
||||
(is (nil? (:mapped-ports stopped-container))))))
|
||||
|
||||
(deftest execute-command-in-container
|
||||
|
|
@ -51,8 +62,8 @@
|
|||
:exposed-ports [5432]
|
||||
:env-vars {"POSTGRES_PASSWORD" "pw"}})
|
||||
(bind-filesystem! {:host-path "."
|
||||
:container-path "/opt"
|
||||
:mode :read-only}))
|
||||
:container-path "/opt"
|
||||
:mode :read-only}))
|
||||
initialized-container (start! container)
|
||||
file-check (execute-command! initialized-container ["tail" "/opt/README.md"])
|
||||
stopped-container (stop! container)]
|
||||
|
|
|
|||
2
test/resources/Dockerfile
Normal file
2
test/resources/Dockerfile
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
FROM nginx:alpine
|
||||
COPY index.html /usr/share/nginx/html/index.html
|
||||
20
test/resources/index.html
Normal file
20
test/resources/index.html
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||
<title>Hello World - Nginx Docker</title>
|
||||
<style>
|
||||
h1{
|
||||
font-weight:lighter;
|
||||
font-family: Arial, Helvetica, sans-serif;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>
|
||||
Hello World - Docker/Nginx
|
||||
</h1>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Reference in a new issue