Skip to content

Commit

Permalink
improved build to use failjure return
Browse files Browse the repository at this point in the history
  • Loading branch information
TimoKramer committed Feb 23, 2020
1 parent 1f89b75 commit 448df78
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
54 changes: 31 additions & 23 deletions src/bob/execution/internals.clj
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,24 @@
[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
entry point: %s
environment: %s
working dir: %s"
image
cmd
evars
working-dir)
(docker/invoke states/containers {:op :ContainerCreate
:params {:Image image
:Cmd cmd
:Env evars
:WorkingDir working-dir}})))
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
:params {:body {:Image image
:Cmd cmd
:Env evars
:WorkingDir working-dir}}})]
(if (contains? result :message)
(u/log-and-fail "Could not build image:" (:message result))
result)))

(defn status-of
"Returns the status of a container by id."
Expand Down Expand Up @@ -126,11 +129,24 @@
(f/message err))
err)))



(comment
(docker/ops states/images)
(docker/doc states/images :ImageCreate)
(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 "bobcd/bob:latest"
:Cmd ["ls"]
:Env ["FOO=bar"]
:WorkingDir ""}}})
(build "dbobcd/bob:latest"
{:needs_resource "source"
:cmd ["ls"]}
["FOO=bar"])

(defn validate-name [name]
(let [split-name (cs/split name #":")]
(if (= 2 (count split-name))
Expand Down Expand Up @@ -163,17 +179,9 @@
(docker/invoke states/images {:op :ImageCreate
:params {:fromImage "bobcd/bob"
:tag "latest"}})
(docker/invoke states/containers {:op :ContainerCreate
:params {:body {:Image "docker.io/bobcd/bob:latest"
:Env ["FOO=bar"]
:WorkingDir ""}}})
(status-of "d82edce1bfdea")
(:State (docker/invoke states/containers {:op :ContainerInspect :params {:id "82edce1bfdea"}}))

(build "busybox:musl"
{:needs_resource "source"
:cmd "ls"}
{})
(log/infof "Creating new container with:
image: %s
entry point: %s
Expand Down
16 changes: 15 additions & 1 deletion test/bob/execution/internals_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,26 @@
(testing "missing tag in image name"
(with-redefs-fn {#'docker/invoke (constantly {:message "failed"})}
#(let [result (pull "clojure")]
(is (and (f/failed? result))))))
(is (f/failed? result)))))

(testing "image already locally available"
(with-redefs-fn {#'has-image (constantly true)}
#(is (= "img" (pull "img"))))))

(deftest container-build
(testing "successful build"
(with-redefs-fn {#'docker/invoke (constantly {:Id "83996a1d", :Warnings []})}
#(is (= {:Id "83996a1d", :Warnings []} (build "foo:bar"
{:needs_resource "source"
:cmd ["ls"]}
["FOO=bar"])))))
(testing "failing build"
(with-redefs-fn {#'docker/invoke (constantly {:message "failed"})}
#(is (f/failed? (build "foo:bar"
{:needs_resource "source"
:cmd ["ls"]}
["FOO=bar"]))))))

(deftest container-status
(testing "successful status fetch"
(with-redefs-fn {#'docker/invoke (constantly {:State {:Running false
Expand Down

0 comments on commit 448df78

Please sign in to comment.