Skip to content

Commit

Permalink
artifact/core adapted
Browse files Browse the repository at this point in the history
  • Loading branch information
TimoKramer committed Feb 23, 2020
1 parent 6b81cce commit 74baf38
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
34 changes: 31 additions & 3 deletions src/bob/artifact/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,35 @@
(do (log/error "Error locating Artifact Store")
(res/bad-request {:message "No such artifact store registered"}))))

(defn remove-container
"Removes container by id. Takes optional keyword argument :force to force-remove container.
Returns a Failure object if failed."
([run-id force-flag]
(let [run-id (str run-id)
force-flag (= force-flag :force)
result (docker/invoke states/containers
{:op :ContainerDelete
:params {:id run-id
:force force-flag}})]
(if-let [message (get result :message)]
(u/log-and-fail "Could not delete container:" message)
result)))
([run-id] (remove-container run-id :force)))

(defn upload-artifact
"Opens up a stream to the path in a container by id and POSTs it to the artifact store.
Returns a Failure object if failed."
[group name number artifact run-id path store-name]
(if-let [{url :url} (db/get-artifact-store states/db {:name store-name})]
(f/try-all [stream (docker/stream-path states/docker-conn run-id path)
(f/try-all [stream (clojure.java.io/input-stream
(java.io.ByteArrayInputStream.
(.getBytes
(docker/invoke states/containers {:op :ContainerArchive
:params {:id run-id
:path path}
:as :stream}))))
upload-url (clojure.string/join "/"
[url
"bob_artifact"
Expand All @@ -114,13 +136,19 @@
"Ok"
(f/when-failed [err]
(log/errorf "Error in uploading artifact: %s" (f/message err))
(f/try* (docker/rm states/docker-conn run-id))
(f/try* (remove-container run-id true))
err))
(do (log/error "Error locating Artifact Store")
(f/try* (docker/rm states/docker-conn run-id))
(f/try* (remove-container run-id true))
(f/fail "No such artifact store registered"))))

(comment
(docker/ops (docker/client {:category :containers :conn states/docker-conn}))
(slurp (clojure.java.io/input-stream (java.io.ByteArrayInputStream. (.getBytes (docker/invoke states/containers {:op :ContainerArchive :params {:id "0be7b883382b" :path "/opt/pyproject.toml"} :as :stream}) "UTF-8"))))
(docker/invoke states/containers {:op :ContainerDelete :params {:id "16a4b4e860b4" :force true}})
(upload-artifact "dev" "test" 1 "poetry.lock" "0be7b883382b" "/opt" "s3")
(remove-container "3c12417d5a7e" true)

(db/register-artifact-store states/db {:name "s3"
:url "http://localhost:8001"})

Expand Down
3 changes: 3 additions & 0 deletions src/bob/states.clj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
(m/defstate containers
:start (docker/client {:category :containers :conn docker-conn}))

(m/defstate ping
:start (docker/client {:category :_ping :conn docker-conn}))

(comment
(m/start)

Expand Down
30 changes: 18 additions & 12 deletions test/bob/artifact/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,29 @@
(= {:message "Cannot reach artifact store: Shizz"}
(:body result))))))))

(deftest container-removal
(testing "successful removal of container"
(with-redefs-fn {#'docker/invoke (constantly "")}
#(is (= "" (remove-container 1)))))

(testing "successful force removal of running container"
(with-redefs-fn {#'docker/invoke (constantly "")}
#(is (= "" (remove-container 1 :force)))))

(testing "failed removal of running container"
(with-redefs-fn {#'docker/invoke (constantly {:message "Failed"})}
#(is (f/failed? (remove-container 1))))))

; TODO reimplement the stream tests
(deftest artifact-upload
(testing "successful artifact upload"
(with-redefs-fn {#'db/get-artifact-store (constantly {:url "bob-url"})
#'docker/stream-path (fn [_ id path]
(tu/check-and-fail
#(and (= "1" id)
(= "/path" path)))
:stream)
#'docker/invoke (constantly "foo")
#'http/post (fn [url options]
(future
(tu/check-and-fail
#(and (= "bob-url/bob_artifact/dev/test/1/afile"
url)
(= "data"
(get-in options [:multipart 0 :name]))
(= :stream
(get-in options [:multipart 0 :content]))))))}
#(= "bob-url/bob_artifact/dev/test/1/afile"
url))))}
#(is (= "Ok"
(upload-artifact "dev" "test" 1 "afile" "1" "/path" "s3")))))

Expand All @@ -134,7 +140,7 @@

(testing "unsuccessful artifact upload"
(with-redefs-fn {#'db/get-artifact-store (constantly {:url "bob-url"})
#'docker/stream-path (constantly :stream)
#'docker/invoke (constantly :stream)
#'http/post (constantly (future (throw (Exception. "bad call"))))}
#(let [result (upload-artifact "dev" "test" 1 "afile" "1" "/path" "s3")]
(is (f/failed? result))))))

0 comments on commit 74baf38

Please sign in to comment.