Skip to content

Commit

Permalink
run function using new clj-docker
Browse files Browse the repository at this point in the history
  • Loading branch information
TimoKramer committed Feb 23, 2020
1 parent 448df78 commit 5c9f70b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 39 deletions.
44 changes: 23 additions & 21 deletions src/bob/execution/internals.clj
Original file line number Diff line number Diff line change
Expand Up @@ -105,31 +105,30 @@
{:running? running? :exit-code exit-code})))

(defn run
"Synchronously starts up a previously built container.
"Synchronously starts up a previously built container by id.
Returns the id when complete or and error in case on non-zero exit."
[id run-id]
(f/try-all [_ (log/debugf "Starting container %s" id)
_ (docker/start states/docker-conn id)
_ (docker/invoke states/containers {:op :ContainerStart :params {:id id}})
_ (log/debugf "Attaching to container %s for logs" id)
_ (docker/logs-live states/docker-conn
id
#(u/log-to-db % run-id))
status (-> (docker/inspect states/docker-conn id)
;;_ (docker/logs-live states/docker-conn
;; id
;; #(u/log-to-db % run-id))
status (-> (docker/invoke states/containers {:op :ContainerInspect :params {:id id}})
:State
:ExitCode)]
(if (zero? status)
(u/format-id id)
(do (log/debugf "Container %s exited with non-zero status: %s"
id
status)
(f/fail "Abnormal exit.")))
(f/when-failed [err]
(log/errorf "Error in running container %s: %s"
id
(f/message err))
err)))


(do (log/debugf (str status))
(f/when-failed [err]
(u/log-and-fail "Error in running container"
(str id ":")
(f/message err)))
(if (zero? status)
(u/format-id id)
(u/log-and-fail "Container"
"with id"
id
"exited with non-zero status:"
status)))))

(comment
(docker/ops states/images)
Expand Down Expand Up @@ -179,8 +178,11 @@
(docker/invoke states/images {:op :ImageCreate
:params {:fromImage "bobcd/bob"
:tag "latest"}})
(status-of "d82edce1bfdea")
(:State (docker/invoke states/containers {:op :ContainerInspect :params {:id "82edce1bfdea"}}))
(status-of "lucid_pasteur")
(:ExitCode (:State (docker/invoke states/containers {:op :ContainerInspect :params {:id "82edce1bfdea"}})))

(docker/invoke states/containers {:op :ContainerStart :params {:id "upbeat_neumann"}})
(run "lucid_pasteur" "run-id")

(log/infof "Creating new container with:
image: %s
Expand Down
31 changes: 13 additions & 18 deletions test/bob/execution/internals_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -105,33 +105,28 @@
(deftest container-runs
(testing "successful run"
(let [id "11235813213455"]
(with-redefs-fn {#'docker/start (fn [_ cid]
(with-redefs-fn {;#'docker/logs-live (constantly true)
#'docker/invoke (fn [_ cid]
(tu/check-and-fail
#(= id cid)))
#'docker/logs-live (constantly true)
#'docker/inspect (fn [_ cid]
(tu/check-and-fail
#(= id cid))
#(= id (-> cid
:params
:id)))
{:State {:ExitCode 0}})}
#(is (= "112358132134" (run id "run-id"))))))

(testing "successful run, non-zero exit"
(let [id "11235813213455"]
(with-redefs-fn {#'docker/start (fn [_ cid]
(tu/check-and-fail
#(= id cid)))
#'docker/logs-live (constantly true)
#'docker/inspect (fn [_ cid]
(with-redefs-fn {;#'docker/logs-live (constantly true)
#'docker/invoke (fn [_ cid]
(tu/check-and-fail
#(= id cid))
#(= id (-> cid
:params
:id)))
{:State {:ExitCode 1}})}
#(let [result (run id "run-id")]
(is (and (f/failed? result)
(= "Abnormal exit."
(f/message result))))))))
(is (and (f/failed? result)))))))

(testing "unsuccessful run"
(with-redefs-fn {#'docker/start (constantly nil)
#'docker/logs-live (constantly true)
#'docker/inspect (fn [_ _] (throw (Exception. "Failed")))}
(with-redefs-fn {;#'docker/logs-live (constantly true)
#'docker/invoke (fn [_ _] (throw (Exception. "Failed")))}
#(is (f/failed? (run "id" "run-id"))))))

0 comments on commit 5c9f70b

Please sign in to comment.