Factor out service args into a fixture so other modules can reuse tests.

This commit is contained in:
Phil Hagelberg 2010-03-19 15:41:07 -07:00
parent 7a4b513191
commit 0d0b4e849f
1 changed files with 30 additions and 44 deletions

View File

@ -4,49 +4,39 @@
(:import [org.jclouds.blobstore BlobStoreContextFactory] (:import [org.jclouds.blobstore BlobStoreContextFactory]
[java.io ByteArrayOutputStream])) [java.io ByteArrayOutputStream]))
(def stub-context (.createContext (BlobStoreContextFactory.) "transient" "" "")) (defn clean-stub-fixture
(def stub-blobstore (.getBlobStore stub-context)) "This should allow basic tests to easily be run with another service."
[service account key & options]
(fn [f]
(with-blobstore [(apply blobstore service account key options)]
(doseq [container (containers)]
(delete-container (.getName container)))
(f))))
(defn clean-stub-fixture [f] (use-fixtures :each (clean-stub-fixture "transient" "" ""))
(with-blobstore [stub-blobstore]
(doseq [container (containers)]
(delete-container (.getName container)))
(f)))
(use-fixtures :each clean-stub-fixture)
(deftest blobstore?-test (deftest blobstore?-test
(is (blobstore? stub-blobstore))) (is (blobstore? *blobstore*)))
(deftest blobstore-context?-test
(is (blobstore-context? stub-context)))
(deftest blobstore-context-test
(is (= stub-context (blobstore-context stub-blobstore))))
(deftest as-blobstore-test (deftest as-blobstore-test
(is (blobstore? (blobstore "transient" "user" "password"))) (is (blobstore? (blobstore "transient" "user" "password")))
(is (blobstore? (as-blobstore stub-blobstore))) (is (blobstore? (as-blobstore *blobstore*)))
(is (blobstore? (as-blobstore stub-context)))) (is (blobstore? (as-blobstore (blobstore-context *blobstore*)))))
(deftest with-blobstore-test
(with-blobstore [stub-blobstore]
(is (= stub-blobstore *blobstore*))))
(deftest create-existing-container-test (deftest create-existing-container-test
(is (not (container-exists? stub-blobstore ""))) (is (not (container-exists? *blobstore* "")))
(is (not (container-exists? ""))) (is (not (container-exists? "")))
(is (create-container stub-blobstore "fred")) (is (create-container *blobstore* "fred"))
(is (container-exists? stub-blobstore "fred"))) (is (container-exists? *blobstore* "fred")))
(deftest create-container-test (deftest create-container-test
(is (create-container stub-blobstore "fred")) (is (create-container *blobstore* "fred"))
(is (container-exists? stub-blobstore "fred"))) (is (container-exists? *blobstore* "fred")))
(deftest containers-test (deftest containers-test
(is (empty? (containers stub-blobstore))) (is (empty? (containers *blobstore*)))
(is (create-container stub-blobstore "fred")) (is (create-container *blobstore* "fred"))
(is (= 1 (count (containers stub-blobstore))))) (is (= 1 (count (containers *blobstore*)))))
(deftest list-container-test (deftest list-container-test
(is (create-container "container")) (is (create-container "container"))
@ -92,28 +82,24 @@
;; TODO: more tests involving blob-specific functions ;; TODO: more tests involving blob-specific functions
(deftest corruption-hunt (deftest corruption-hunt
(let [service "transient" (let [container-name "test"
account ""
secret-key ""
container-name "test"
name "work-file" name "work-file"
upload-filename "/home/phil/work-file"
total-downloads 100 total-downloads 100
threads 10 threads 10]
blob-s (blobstore service account secret-key)]
;; upload ;; upload
(create-container blob-s container-name) (create-container container-name)
(when-not (blob-exists? blob-s container-name name) (when-not (blob-exists? container-name name)
(create-blob blob-s container-name name (let [data-stream (java.io.ByteArrayOutputStream.)]
(java.io.File. upload-filename))) (dotimes [i 5000000] (.write data-stream i))
(create-blob container-name name (.toByteArray data-stream))))
;; download ;; download
(let [total (atom total-downloads)] (let [total (atom total-downloads)]
(defn new-agent [] (defn new-agent []
(agent name)) (agent name))
(defn dl-and-restart [file] (defn dl-and-restart [blob-s file]
(when-not (<= @total 0) (when-not (<= @total 0)
(with-open [baos (java.io.ByteArrayOutputStream.)] (with-open [baos (java.io.ByteArrayOutputStream.)]
(try (try
@ -124,14 +110,14 @@
(.write of (.toByteArray baos))) (.write of (.toByteArray baos)))
(throw e)))) (throw e))))
(swap! total dec) (swap! total dec)
(send *agent* dl-and-restart) (send *agent* (partial dl-and-restart blob-s))
file)) file))
(defn start-agents [] (defn start-agents []
(let [agents (map (fn [_] (new-agent)) (let [agents (map (fn [_] (new-agent))
(range threads))] (range threads))]
(doseq [a agents] (doseq [a agents]
(send-off a dl-and-restart)) (send-off a (partial dl-and-restart *blobstore*)))
agents)) agents))
(let [agents (start-agents)] (let [agents (start-agents)]