Issue 772: Fix clojure tests when running with Clojure 1.3.0. Added ^:dynamic to Vars to support Clojure 1.3.0. Also cleaned up tests a bit, not using earmuffs when inappropriate.

This commit is contained in:
Mattias Holmqvist 2011-12-08 00:32:08 +01:00
parent db8f84bab8
commit 9865616c2c
5 changed files with 92 additions and 93 deletions

View File

@ -100,9 +100,9 @@ Options can also be specified for extension modules
(blobstore-context? (first args)) (.getBlobStore (first args)) (blobstore-context? (first args)) (.getBlobStore (first args))
:else (apply blobstore args))) :else (apply blobstore args)))
(def *blobstore*) (def ^{:dynamic :true} *blobstore*)
(def *max-retries* 3) (def ^{:dynamic :true} *max-retries* 3)
(defmacro with-blobstore [[& blobstore-or-args] & body] (defmacro with-blobstore [[& blobstore-or-args] & body]
`(binding [*blobstore* (as-blobstore ~@blobstore-or-args)] `(binding [*blobstore* (as-blobstore ~@blobstore-or-args)]
@ -249,7 +249,7 @@ Note: (apply concat coll) or (lazy-cat coll) are not lazy wrt coll itself."
([container-name path #^BlobStore blobstore] ([container-name path #^BlobStore blobstore]
(.blobMetadata blobstore container-name path))) (.blobMetadata blobstore container-name path)))
(defn get-blob (defn ^{:dynamic :true} get-blob
"Get blob from given path" "Get blob from given path"
([container-name path] ([container-name path]
(get-blob container-name path *blobstore*)) (get-blob container-name path *blobstore*))

View File

@ -34,95 +34,94 @@
(delete-container blobstore (.getName container))) (delete-container blobstore (.getName container)))
(f))) (f)))
(def *blobstore* (blobstore "transient" "" "")) (def blobstore-stub (blobstore "transient" "" ""))
(use-fixtures :each (clean-stub-fixture *blobstore*)) (use-fixtures :each (clean-stub-fixture blobstore-stub))
(deftest blobstore?-test (deftest blobstore?-test
(is (blobstore? *blobstore*))) (is (blobstore? blobstore-stub)))
(deftest as-blobstore-test (deftest as-blobstore-test
(is (blobstore? (blobstore "transient" "user" "password"))) (is (blobstore? (blobstore "transient" "user" "password"))))
(is (blobstore? *blobstore*)))
(deftest create-existing-container-test (deftest create-existing-container-test
(is (not (container-exists? *blobstore* ""))) (is (not (container-exists? blobstore-stub "")))
(is (create-container *blobstore* "fred")) (is (create-container blobstore-stub "fred"))
(is (container-exists? *blobstore* "fred"))) (is (container-exists? blobstore-stub "fred")))
(deftest create-container-test (deftest create-container-test
(is (create-container *blobstore* "fred")) (is (create-container blobstore-stub "fred"))
(is (container-exists? *blobstore* "fred"))) (is (container-exists? blobstore-stub "fred")))
(deftest locations-test (deftest locations-test
(is (not (empty? (locations *blobstore*)))) (is (not (empty? (locations blobstore-stub))))
(is (create-container *blobstore* "fred" (is (create-container blobstore-stub "fred"
:location (first (locations *blobstore*))))) :location (first (locations blobstore-stub)))))
(deftest containers-test (deftest containers-test
(is (empty? (containers *blobstore*))) (is (empty? (containers blobstore-stub)))
(is (create-container *blobstore* "fred")) (is (create-container blobstore-stub "fred"))
(is (= 1 (count (containers *blobstore*))))) (is (= 1 (count (containers blobstore-stub)))))
(deftest blobs-test (deftest blobs-test
(is (create-container *blobstore* "container")) (is (create-container blobstore-stub "container"))
(is (empty? (blobs *blobstore* "container"))) (is (empty? (blobs blobstore-stub "container")))
(is (put-blob *blobstore* "container" (is (put-blob blobstore-stub "container"
(blob "blob1" :payload "blob1" :calculate-md5 true))) (blob "blob1" :payload "blob1" :calculate-md5 true)))
(is (put-blob *blobstore* "container" (is (put-blob blobstore-stub "container"
(blob "blob2" :payload "blob2" :calculate-md5 true))) (blob "blob2" :payload "blob2" :calculate-md5 true)))
(is (= 2 (count (blobs *blobstore* "container")))) (is (= 2 (count (blobs blobstore-stub "container"))))
(is (= 1 (count (blobs *blobstore* "container" :max-results 1)))) (is (= 1 (count (blobs blobstore-stub "container" :max-results 1))))
(create-directory *blobstore* "container" "dir") (create-directory blobstore-stub "container" "dir")
(is (put-blob *blobstore* "container" (is (put-blob blobstore-stub "container"
(blob "dir/blob2" :payload "blob2" :calculate-md5 true))) (blob "dir/blob2" :payload "blob2" :calculate-md5 true)))
(is (= 3 (count-blobs *blobstore* "container"))) (is (= 3 (count-blobs blobstore-stub "container")))
(is (= 3 (count (blobs *blobstore* "container")))) (is (= 3 (count (blobs blobstore-stub "container"))))
(is (= 4 (count (blobs *blobstore* "container" :recursive true)))) (is (= 4 (count (blobs blobstore-stub "container" :recursive true))))
(is (= 3 (count (blobs *blobstore* "container" :with-details true)))) (is (= 3 (count (blobs blobstore-stub "container" :with-details true))))
(is (= 1 (count (blobs *blobstore* "container" :in-directory "dir"))))) (is (= 1 (count (blobs blobstore-stub "container" :in-directory "dir")))))
(deftest large-container-list-test (deftest large-container-list-test
(let [container-name "test" (let [container-name "test"
total-blobs 5000] total-blobs 5000]
;; create a container full of blobs ;; create a container full of blobs
(create-container *blobstore* container-name) (create-container blobstore-stub container-name)
(dotimes [i total-blobs] (put-blob *blobstore* container-name (dotimes [i total-blobs] (put-blob blobstore-stub container-name
(blob (str i) (blob (str i)
:payload (str i) :payload (str i)
:calculate-md5 true))) :calculate-md5 true)))
;; verify ;; verify
(is (= total-blobs (count-blobs *blobstore* container-name))))) (is (= total-blobs (count-blobs blobstore-stub container-name)))))
(deftest container-seq-test (deftest container-seq-test
(is (create-container *blobstore* "container")) (is (create-container blobstore-stub "container"))
(is (empty? (container-seq *blobstore* "container"))) (is (empty? (container-seq blobstore-stub "container")))
(is (empty? (container-seq *blobstore* "container" "/a")))) (is (empty? (container-seq blobstore-stub "container" "/a"))))
(deftest get-blob-test (deftest get-blob-test
(is (create-container *blobstore* "blob")) (is (create-container blobstore-stub "blob"))
(is (put-blob *blobstore* "blob" (is (put-blob blobstore-stub "blob"
(blob "blob1" :payload "blob1" :calculate-md5 true))) (blob "blob1" :payload "blob1" :calculate-md5 true)))
(is (put-blob *blobstore* "blob" (is (put-blob blobstore-stub "blob"
(blob "blob2" :payload "blob2" :calculate-md5 true))) (blob "blob2" :payload "blob2" :calculate-md5 true)))
(is (= "blob2" (Strings2/toStringAndClose (get-blob-stream *blobstore* (is (= "blob2" (Strings2/toStringAndClose (get-blob-stream blobstore-stub
"blob" "blob2"))))) "blob" "blob2")))))
(deftest put-blob-test (deftest put-blob-test
;; Check multipart works ;; Check multipart works
(is (create-container *blobstore* "blobs")) (is (create-container blobstore-stub "blobs"))
(is (put-blob *blobstore* "blobs" (is (put-blob blobstore-stub "blobs"
(blob "blob1" :payload "blob1") (blob "blob1" :payload "blob1")
:multipart? true)) :multipart? true))
(is (= 1 (count (blobs *blobstore* "blobs"))))) (is (= 1 (count (blobs blobstore-stub "blobs")))))
(deftest sign-get-test (deftest sign-get-test
(let [request (sign-get *blobstore* "container" "path")] (let [request (sign-get blobstore-stub "container" "path")]
(is (= "http://localhost/container/path" (str (.getEndpoint request)))) (is (= "http://localhost/container/path" (str (.getEndpoint request))))
(is (= "GET" (.getMethod request))))) (is (= "GET" (.getMethod request)))))
(deftest sign-put-test (deftest sign-put-test
(let [request (sign-put *blobstore* "container" (let [request (sign-put blobstore-stub "container"
(blob "path" :content-length 10))] (blob "path" :content-length 10))]
(is (= "http://localhost/container/path" (str (.getEndpoint request)))) (is (= "http://localhost/container/path" (str (.getEndpoint request))))
(is (= "PUT" (.getMethod request))) (is (= "PUT" (.getMethod request)))
@ -131,7 +130,7 @@
(first (.get (.getHeaders request) "Content-Type")))))) (first (.get (.getHeaders request) "Content-Type"))))))
(deftest sign-put-with-headers-test (deftest sign-put-with-headers-test
(let [request (sign-put *blobstore* (let [request (sign-put blobstore-stub
"container" "container"
(blob "path" (blob "path"
:content-length 10 :content-length 10
@ -147,7 +146,7 @@
(is (= "g" (first (.get (.getHeaders request) "Content-Encoding")))))) (is (= "g" (first (.get (.getHeaders request) "Content-Encoding"))))))
(deftest sign-delete-test (deftest sign-delete-test
(let [request (sign-delete *blobstore* "container" "path")] (let [request (sign-delete blobstore-stub "container" "path")]
(is (= "http://localhost/container/path" (str (.getEndpoint request)))) (is (= "http://localhost/container/path" (str (.getEndpoint request))))
(is (= "DELETE" (.getMethod request))))) (is (= "DELETE" (.getMethod request)))))
@ -161,34 +160,34 @@
(deftest payload-protocol-test (deftest payload-protocol-test
(is (instance? org.jclouds.io.Payload (payload "test"))) (is (instance? org.jclouds.io.Payload (payload "test")))
(is (blob "blob1" :payload (payload "blob1"))) (is (blob "blob1" :payload (payload "blob1")))
(is (create-container *blobstore* "container")) (is (create-container blobstore-stub "container"))
(is (= "blob1" (is (= "blob1"
(do (do
(put-blob *blobstore* "container" (put-blob blobstore-stub "container"
(blob "blob1" (blob "blob1"
:payload "blob1")) :payload "blob1"))
(Strings2/toStringAndClose (get-blob-stream *blobstore* (Strings2/toStringAndClose (get-blob-stream blobstore-stub
"container" "blob1"))))) "container" "blob1")))))
(is (= "blob2" (is (= "blob2"
(do (do
(put-blob *blobstore* "container" (put-blob blobstore-stub "container"
(blob "blob2" (blob "blob2"
:payload (StringBufferInputStream. "blob2"))) :payload (StringBufferInputStream. "blob2")))
(Strings2/toStringAndClose (get-blob-stream *blobstore* (Strings2/toStringAndClose (get-blob-stream blobstore-stub
"container" "blob2"))))) "container" "blob2")))))
(is (= "blob3" (is (= "blob3"
(do (do
(put-blob *blobstore* "container" (put-blob blobstore-stub "container"
(blob "blob3" (blob "blob3"
:payload (.getBytes "blob3"))) :payload (.getBytes "blob3")))
(Strings2/toStringAndClose (get-blob-stream *blobstore* (Strings2/toStringAndClose (get-blob-stream blobstore-stub
"container" "blob3"))))) "container" "blob3")))))
(is (= "blob4" (is (= "blob4"
(do (do
(put-blob *blobstore* "container" (put-blob blobstore-stub "container"
(blob "blob4" (blob "blob4"
:payload #(.write % (.getBytes "blob4")))) :payload #(.write % (.getBytes "blob4"))))
(Strings2/toStringAndClose (get-blob-stream *blobstore* (Strings2/toStringAndClose (get-blob-stream blobstore-stub
"container" "blob4")))))) "container" "blob4"))))))
;; TODO: more tests involving blob-specific functions ;; TODO: more tests involving blob-specific functions

View File

@ -109,7 +109,7 @@ See http://code.google.com/p/jclouds for details."
(compute-context? (first args)) (.getComputeService (first args)) (compute-context? (first args)) (.getComputeService (first args))
:else (apply compute-service args))) :else (apply compute-service args)))
(def *compute*) (def ^{:dynamic :true} *compute*)
(defmacro with-compute-service (defmacro with-compute-service
"Specify the default compute service" "Specify the default compute service"

View File

@ -42,7 +42,7 @@ list, Alan Dipert and MeikelBrandmeyer."
(deftest os-families-test (deftest os-families-test
(is (some #{"centos"} (map str (os-families))))) (is (some #{"centos"} (map str (os-families)))))
(def *compute* (compute-service "stub" "" "" :extensions [(ssh-test/ssh-test-client ssh-test/no-op-ssh-client)])) (def compute-stub (compute-service "stub" "" "" :extensions [(ssh-test/ssh-test-client ssh-test/no-op-ssh-client)]))
(defn clean-stub-fixture (defn clean-stub-fixture
"This should allow basic tests to easily be run with another service." "This should allow basic tests to easily be run with another service."
@ -52,50 +52,50 @@ list, Alan Dipert and MeikelBrandmeyer."
(destroy-node compute-service (.getId node))) (destroy-node compute-service (.getId node)))
(f))) (f)))
(use-fixtures :each (clean-stub-fixture *compute*)) (use-fixtures :each (clean-stub-fixture compute-stub))
(deftest compute-service?-test (deftest compute-service?-test
(is (compute-service? *compute*))) (is (compute-service? compute-stub)))
(deftest as-compute-service-test (deftest as-compute-service-test
(is (compute-service? (compute-service "stub" "user" "password"))) (is (compute-service? (compute-service "stub" "user" "password")))
(is (compute-service? *compute*)) (is (compute-service? compute-stub))
(is (compute-service? (compute-service (compute-context *compute*))))) (is (compute-service? (compute-service (compute-context compute-stub)))))
(deftest nodes-test (deftest nodes-test
(is (create-node *compute* "fred" (build-template *compute* {} ))) (is (create-node compute-stub "fred" (build-template compute-stub {} )))
(is (= 1 (count (nodes-in-group *compute* "fred")))) (is (= 1 (count (nodes-in-group compute-stub "fred"))))
;; pass in a function that selects node metadata based on NodeMetadata field ;; pass in a function that selects node metadata based on NodeMetadata field
(is (= 1 (count (nodes-with-details-matching *compute* (in-group? "fred"))))) (is (= 1 (count (nodes-with-details-matching compute-stub (in-group? "fred")))))
;; or make your query inline ;; or make your query inline
(is (= 1 (count (nodes-with-details-matching *compute* #(= (.getGroup %) "fred"))))) (is (= 1 (count (nodes-with-details-matching compute-stub #(= (.getGroup %) "fred")))))
;; or get real fancy, and use the underlying Predicate object jclouds uses ;; or get real fancy, and use the underlying Predicate object jclouds uses
(is (= 1 (count (nodes-with-details-matching *compute* (is (= 1 (count (nodes-with-details-matching compute-stub
(reify com.google.common.base.Predicate (reify com.google.common.base.Predicate
(apply [this input] (= (.getGroup input) "fred"))))))) (apply [this input] (= (.getGroup input) "fred")))))))
(is (= 0 (count (nodes-with-details-matching *compute* (in-group? "othergroup"))))) (is (= 0 (count (nodes-with-details-matching compute-stub (in-group? "othergroup")))))
(suspend-nodes-matching *compute* (in-group? "fred")) (suspend-nodes-matching compute-stub (in-group? "fred"))
(is (suspended? (first (nodes-with-details-matching *compute* (in-group? "fred"))))) (is (suspended? (first (nodes-with-details-matching compute-stub (in-group? "fred")))))
(resume-nodes-matching *compute* (in-group? "fred")) (resume-nodes-matching compute-stub (in-group? "fred"))
(is (running? (first (nodes-in-group *compute* "fred")))) (is (running? (first (nodes-in-group compute-stub "fred"))))
(reboot-nodes-matching *compute* (in-group? "fred")) (reboot-nodes-matching compute-stub (in-group? "fred"))
(is (running? (first (nodes-in-group *compute* "fred")))) (is (running? (first (nodes-in-group compute-stub "fred"))))
(is (create-nodes *compute* "fred" 2 (build-template *compute* {} ))) (is (create-nodes compute-stub "fred" 2 (build-template compute-stub {} )))
(is (= 3 (count (nodes-in-group *compute* "fred")))) (is (= 3 (count (nodes-in-group compute-stub "fred"))))
(is (= "fred" (group (first (nodes *compute*))))) (is (= "fred" (group (first (nodes compute-stub)))))
(destroy-nodes-matching *compute* (in-group? "fred")) (destroy-nodes-matching compute-stub (in-group? "fred"))
(is (terminated? (first (nodes-in-group *compute* "fred"))))) (is (terminated? (first (nodes-in-group compute-stub "fred")))))
(defn localhost? [node] (defn localhost? [node]
"Returns true if the localhost address is in the node's private ips" "Returns true if the localhost address is in the node's private ips"
(seq? (some #(= "localhost" %) (private-ips node)))) (seq? (some #(= "localhost" %) (private-ips node))))
(deftest compound-predicate-test (deftest compound-predicate-test
(is (create-node *compute* "my-group" (build-template *compute* {}))) (is (create-node compute-stub "my-group" (build-template compute-stub {})))
(is (= 0 (count (nodes-with-details-matching *compute* #(and (suspended? %) (not (localhost? %))))))) (is (= 0 (count (nodes-with-details-matching compute-stub #(and (suspended? %) (not (localhost? %)))))))
(is (= 0 (count (nodes-with-details-matching *compute* #(and (suspended? %) (localhost? %)))))) (is (= 0 (count (nodes-with-details-matching compute-stub #(and (suspended? %) (localhost? %))))))
(is (= 0 (count (nodes-with-details-matching *compute* #(and (running? %) (localhost? %)))))) (is (= 0 (count (nodes-with-details-matching compute-stub #(and (running? %) (localhost? %))))))
(is (= 1 (count (nodes-with-details-matching *compute* #(and (running? %) (not (localhost? %)))))))) (is (= 1 (count (nodes-with-details-matching compute-stub #(and (running? %) (not (localhost? %))))))))
(deftest run-script-on-nodes-matching-with-options-test (deftest run-script-on-nodes-matching-with-options-test
(let [echo (Statements/exec "echo hello") (let [echo (Statements/exec "echo hello")
@ -103,20 +103,20 @@ list, Alan Dipert and MeikelBrandmeyer."
(runAsRoot false) (runAsRoot false)
(wrapInInitScript false)) (wrapInInitScript false))
pred #(= (.getGroup %) "scriptednode")] pred #(= (.getGroup %) "scriptednode")]
(is (create-node *compute* "scriptednode" (build-template *compute* {}))) (is (create-node compute-stub "scriptednode" (build-template compute-stub {})))
(is (run-script-on-nodes-matching *compute* pred echo script-options)) (is (run-script-on-nodes-matching compute-stub pred echo script-options))
(is (thrown? NoSuchElementException (is (thrown? NoSuchElementException
(run-script-on-nodes-matching *compute* #(= (.getGroup %) "nonexistingnode") echo script-options))))) (run-script-on-nodes-matching compute-stub #(= (.getGroup %) "nonexistingnode") echo script-options)))))
(deftest run-script-on-node-with-options-test (deftest run-script-on-node-with-options-test
(let [echo (Statements/exec "echo hello") (let [echo (Statements/exec "echo hello")
script-options (.. (RunScriptOptions$Builder/overrideCredentialsWith (Credentials. "user" "password")) script-options (.. (RunScriptOptions$Builder/overrideCredentialsWith (Credentials. "user" "password"))
(runAsRoot false) (runAsRoot false)
(wrapInInitScript false)) (wrapInInitScript false))
test_node (create-node *compute* "scriptednode" (build-template *compute* {}))] test_node (create-node compute-stub "scriptednode" (build-template compute-stub {}))]
(is (run-script-on-node *compute* (id test_node) echo script-options)) (is (run-script-on-node compute-stub (id test_node) echo script-options))
(is (thrown? NoSuchElementException (is (thrown? NoSuchElementException
(run-script-on-node *compute* "nonexistingnode" echo script-options))))) (run-script-on-node compute-stub "nonexistingnode" echo script-options)))))
(deftest build-template-test (deftest build-template-test
(let [service (compute-service "stub" "user" "password")] (let [service (compute-service "stub" "user" "password")]

View File

@ -24,7 +24,7 @@
(com.google.common.collect ImmutableSet)) (com.google.common.collect ImmutableSet))
(:require [clojure.string :as string])) (:require [clojure.string :as string]))
(def module-lookup (def ^{:dynamic :true} module-lookup
{:log4j 'org.jclouds.logging.log4j.config.Log4JLoggingModule {:log4j 'org.jclouds.logging.log4j.config.Log4JLoggingModule
:slf4j 'org.jclouds.logging.slf4j.config.SLF4JLoggingModule :slf4j 'org.jclouds.logging.slf4j.config.SLF4JLoggingModule
:lognull 'org.jclouds.logging.config.NullLoggingModule :lognull 'org.jclouds.logging.config.NullLoggingModule