Skip to content

Commit

Permalink
fixed some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TimoKramer committed Feb 23, 2020
1 parent a550949 commit 5b3b25b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 55 deletions.
49 changes: 29 additions & 20 deletions src/bob/execution/internals.clj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
(let [result (filter #(= (:RepoTags %) [name])
(docker/invoke states/images {:op :ImageList}))]
(if (zero? (count result))
(u/log-and-fail "Failed to find" name)
(u/log-and-fail "Failed to find" name "locally")
(do (log/debugf "Found image locally: %s" name)
name))))

Expand All @@ -54,7 +54,7 @@
(u/log-and-fail "Please provide a repository and a tag as image name:" split-name))))
pull-invoke (fn [split-name]
(let [{:keys [repo tag]} split-name
_ (log/debugf "Pulling image: %s" name)
_ (log/debugf "Pulling image with repo %s and tag %s" repo tag)
result (docker/invoke states/images {:op :ImageCreate
:params {:fromImage repo
:tag tag}})]
Expand All @@ -79,22 +79,23 @@
(if (contains? result :message)
(u/log-and-fail "Could not build image:" (:message result))
result)))
([image step evars] (let [resource (:needs_resource step)
working-dir (when resource (str "/root/" resource))
cmd (:cmd step)
_ (log/debugf "Creating new container with:
image: %s
cmd: %s
evars: %s
working-dir: %s"
image
cmd
evars
working-dir)
result (docker/invoke states/containers {:op :ContainerCreate
([image step evars] (let [resource (:needs_resource step)
working-dir (when resource (str "/root/" resource))
cmd (:cmd step)
_ (log/debugf "Creating new container with:
image: %s
cmd: %s
evars: %s
working-dir: %s"
image
cmd
evars
working-dir)
evars-nilled (if (empty? evars) nil evars)
result (docker/invoke states/containers {:op :ContainerCreate
:params {:body {:Image image
:Cmd cmd
:Env evars
:Env evars-nilled
:WorkingDir working-dir}}})]
(if (contains? result :message)
(u/log-and-fail "Could not build image:" (:message result))
Expand Down Expand Up @@ -181,6 +182,14 @@
(docker/invoke states/images {:op :ImageCreate :params {:fromImage "clojure" :tag "latest"}})
(docker/invoke states/images {:op :ImageDelete :params {:name "clojure:latest"}})



(docker/invoke states/containers {:op :ContainerCreate
:params {:body {:Image "oracle/graalvm-ce:19.3.0"
:Cmd "gu install native-image"
:Env {}
:WorkingDir nil}}})

(docker/invoke states/containers {:op :ContainerCreate
:params {:body {:Image "bobcd/bob:latest"
:Cmd ["ls"]
Expand All @@ -200,7 +209,7 @@

(defn pull-invoke [split-name]
(let [{:keys [repo tag]} split-name
_ (log/debugf "Pulling image: %s" name)
_ (log/debugf "Pulling image with repo %s and tag %s" repo tag)
result (docker/invoke states/images {:op :ImageCreate
:params {:fromImage repo
:tag tag}})]
Expand All @@ -212,7 +221,7 @@
(has-image name)
(validate-name name)
(pull-invoke (validate-name name))
(pull-invoke {:repo "bobcd/bob" :tags "latest"})
(pull-invoke {:repo "oracle/graalvm-ce" :tags "19.3.0"})
(pull-image "docker.io/bobcd/bob:latest")
(if (and (f/failed? (has-image name))
(f/failed? (pull-invoke (validate-name name))))
Expand All @@ -221,8 +230,8 @@
name))

(docker/invoke states/images {:op :ImageCreate
:params {:fromImage "bobcd/bob"
:tag "latest"}})
:params {:fromImage "alpine"
:tag "3.11.3"}})
(status-of "lucid_pasteur")
(:ExitCode (:State (docker/invoke states/containers {:op :ContainerInspect :params {:id "82edce1bfdea"}})))

Expand Down
9 changes: 9 additions & 0 deletions test/bob/execution/internals_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,12 @@
(testing "failed deletion of running container"
(with-redefs-fn {#'docker/invoke (constantly {:message "Failed"})}
#(is (f/failed? (delete-container 1))))))

(deftest test-commit-image
(testing "Successful creation of new image from container"
(with-redefs-fn {#'docker/invoke (constantly {:Id "sha256:f2763f84"})}
#(is (= {:Id "sha256:f2763f84"} (commit-image "12121212" "foo")))))

(testing "Failed creation of new image from container"
(with-redefs-fn {#'docker/invoke (constantly {:message "foobar"})}
#(is (f/failed? (commit-image "12121212" "foo"))))))
34 changes: 20 additions & 14 deletions test/bob/pipeline/internals_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,34 @@
(deftest image-mark-and-sweep-test
(log/merge-config! {:level :report})
(testing "marking an image for the first time"
(is (= {"build1" (list "image1")}
(mark-image-for-gc "image1" "build1"))))
(with-redefs [bob.pipeline.internals/images-produced (atom {})]
(is (= {"build1" (list "image1")}
(mark-image-for-gc "image1" "build1")))))

(testing "marking the same image is idempotent"
(is (= {"build1" (list "image1")}
(mark-image-for-gc "image1" "build1"))))
(with-redefs [bob.pipeline.internals/images-produced (atom {})]
(is (= {"build1" (list "image1")}
(mark-image-for-gc "image1" "build1")))))

(testing "marking a new image for the same build"
(is (= {"build1" (list "image2" "image1")}
(mark-image-for-gc "image2" "build1"))))
(with-redefs [bob.pipeline.internals/images-produced (atom {"build1" (list "image1")})]
(is (= {"build1" (list "image2" "image1")}
(mark-image-for-gc "image2" "build1")))))

(testing "marking a new image for another same build"
(is (= {"build1" (list "image2" "image1")
"build2" (list "image1")}
(mark-image-for-gc "image1" "build2"))))
(with-redefs [bob.pipeline.internals/images-produced (atom {"build1" (list "image2" "image1")})]
(is (= {"build1" (list "image2" "image1")
"build2" (list "image1")}
(mark-image-for-gc "image1" "build2")))))

(testing "sweep images for build1"
(with-redefs-fn {#'e/delete-container (fn [_ image]
(tu/check-and-fail
#(some #{image} ["image1" "image2"])))}
#(is (= {"build2" (list "image1")}
(gc-images "build1"))))))
(with-redefs [bob.pipeline.internals/images-produced (atom {"build1" (list "image2" "image1")
"build2" (list "image1")})]
(with-redefs-fn {#'e/delete-container (fn [_ image]
(tu/check-and-fail
#(some #{image} ["image1" "image2"])))}
#(is (= {"build2" (list "image1")}
(gc-images "build1")))))))

(deftest pid-updates
(testing "successful container id update"
Expand Down
35 changes: 14 additions & 21 deletions test/bob/resource/internals_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
(ns bob.resource.internals-test
(:require [clojure.test :refer :all]
[failjure.core :as f]
[clj-docker-client.core :as docker]
[bob.test-utils :as tu]
[bob.resource.db :as db]
[bob.execution.internals :as exint]
[bob.resource.internals :as resint :refer :all]))
[bob.execution.internals :as e]
[bob.resource.internals :refer :all]))

;; TODO: Test the rest of the fns too?

Expand All @@ -45,6 +46,7 @@
:name "r1"}
"test"))))))


(deftest get-resource-params-test
(testing "Testing if result from DB is mapped correctly"
(with-redefs-fn {#'db/resource-params-of (fn [pipeline name]
Expand Down Expand Up @@ -72,29 +74,20 @@
; #'clojure.java.io/input-stream (constantly nil)}
; #(is (f/failed? (put-container-archive "121212121" "/path/foo/bar.tar" "/root"))))))

(deftest test-commit-image
(testing "Successful creation of new image from container"
(with-redefs-fn {#'docker/invoke (constantly {:Id "sha256:f2763f84"})}
#(is (= {:Id "sha256:f2763f84"} (commit-image "12121212" "foo")))))

(testing "Failed creation of new image from container"
(with-redefs-fn {#'docker/invoke (constantly {:message "foobar"})}
#(is (f/failed? (commit-image "12121212" "foo"))))))

(deftest test-initial-image-of
(testing "Successfully creating initial image"
(with-redefs-fn {#'exint/build (constantly nil)
#'resint/put-container-archive (constantly nil)
#'resint/rm-r! (constantly nil)
#'resint/commit-image (constantly {:Id "sha256:f2763f84"})
#'exint/delete-container (constantly nil)}
(with-redefs-fn {#'e/build (constantly nil)
#'put-container-archive (constantly nil)
#'bob.resource.internals/rm-r! (constantly nil)
#'e/commit-image (constantly {:Id "sha256:f2763f84"})
#'e/delete-container (constantly nil)}
#(is (= {:Id "sha256:f2763f84"} (initial-image-of "/tmp/test.tar" "12121212" "foo")))))

(testing "Failed creation of initial image"
(with-redefs-fn {#'exint/build (constantly (f/fail "foobar"))
#'resint/put-container-archive (constantly nil)
#'resint/rm-r! (constantly nil)
#'resint/commit-image (constantly (f/fail "foobar"))
#'exint/delete-container (constantly nil)}
(with-redefs-fn {#'e/build (constantly (f/fail "foobar"))
#'put-container-archive (constantly nil)
#'bob.resource.internals/rm-r! (constantly nil)
#'e/commit-image (constantly (f/fail "foobar"))
#'e/delete-container (constantly nil)}
#(is (f/failed? (initial-image-of "/tmp/test.tar" "12121212" "foo")))))
)

0 comments on commit 5b3b25b

Please sign in to comment.