From e71c88a1a4f16346c1a1c7ec0b9ac4a9dcc9ffb5 Mon Sep 17 00:00:00 2001 From: Mattias Holmqvist Date: Thu, 12 May 2011 23:10:45 +0200 Subject: [PATCH] First version of run-script-on-nodes-matching. Added some missing code to ssh_test to stub out ssh in test. --- .../src/main/clojure/org/jclouds/compute2.clj | 4 ++++ .../clojure/org/jclouds/compute2_test.clj | 22 +++++++++++++++++-- .../src/test/clojure/org/jclouds/ssh_test.clj | 17 +++++++++++++- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/compute/src/main/clojure/org/jclouds/compute2.clj b/compute/src/main/clojure/org/jclouds/compute2.clj index a53d95b865..588e947da4 100644 --- a/compute/src/main/clojure/org/jclouds/compute2.clj +++ b/compute/src/main/clojure/org/jclouds/compute2.clj @@ -233,6 +233,10 @@ Here's an example of creating and running a small linux node in the group webser ([#^ComputeService compute id] (.destroyNode compute id))) +(defn run-script-on-nodes-matching [#^ComputeService compute pred command template] + "Run a script on the nodes matching the given predicate" + (.runScriptOnNodesMatching compute (to-predicate pred) command template)) + (defmacro state-predicate [node state] `(= (.getState ~node) (. org.jclouds.compute.domain.NodeState ~state))) diff --git a/compute/src/test/clojure/org/jclouds/compute2_test.clj b/compute/src/test/clojure/org/jclouds/compute2_test.clj index 1fa416cc88..d9ca0ea609 100644 --- a/compute/src/test/clojure/org/jclouds/compute2_test.clj +++ b/compute/src/test/clojure/org/jclouds/compute2_test.clj @@ -20,10 +20,17 @@ (ns org.jclouds.compute2-test (:use [org.jclouds.compute2] :reload-all) (:use clojure.test) + (:require [org.jclouds.ssh-test :as ssh-test]) (:import org.jclouds.compute.domain.OsFamily clojure.contrib.condition.Condition - java.net.InetAddress)) + java.net.InetAddress + org.jclouds.scriptbuilder.domain.Statements + org.jclouds.compute.options.TemplateOptions + org.jclouds.compute.options.TemplateOptions$Builder + org.jclouds.domain.Credentials + java.util.NoSuchElementException + )) (defmacro with-private-vars [[ns fns] & tests] "Refers private fns from ns and runs tests in context. From users mailing @@ -34,7 +41,7 @@ list, Alan Dipert and MeikelBrandmeyer." (deftest os-families-test (is (some #{"centos"} (map str (os-families))))) -(def *compute* (compute-service "stub" "" "")) +(def *compute* (compute-service "stub" "" "" :extensions [(ssh-test/ssh-test-client ssh-test/no-op-ssh-client)])) (defn clean-stub-fixture "This should allow basic tests to easily be run with another service." @@ -85,6 +92,17 @@ list, Alan Dipert and MeikelBrandmeyer." (is (= 0 (count (nodes-with-details-matching *compute* #(and (running? %) (localhost? %)))))) (is (= 1 (count (nodes-with-details-matching *compute* #(and (running? %) (not (localhost? %)))))))) +(deftest run-script-on-nodes-matching-test + (let [echo (Statements/exec "echo hello") + script-options (.. (TemplateOptions$Builder/overrideCredentialsWith (Credentials. "user" "password")) + (runAsRoot false) + (wrapInInitScript false)) + pred #(= (.getGroup %) "scriptednode")] + (is (create-node *compute* "scriptednode" (build-template *compute* {}))) + (is (run-script-on-nodes-matching *compute* pred echo script-options)) + (is (thrown? NoSuchElementException + (run-script-on-nodes-matching *compute* #(= (.getGroup %) "nonexistingnode") echo script-options))))) + (deftest build-template-test (let [service (compute-service "stub" "user" "password")] (testing "nullary" diff --git a/compute/src/test/clojure/org/jclouds/ssh_test.clj b/compute/src/test/clojure/org/jclouds/ssh_test.clj index c0356d8339..ea85c59886 100644 --- a/compute/src/test/clojure/org/jclouds/ssh_test.clj +++ b/compute/src/test/clojure/org/jclouds/ssh_test.clj @@ -17,6 +17,16 @@ ; ==================================================================== ; +(ns org.jclouds.ssh-test + (:require + [clojure.contrib.logging :as logging]) + (:import + org.jclouds.ssh.SshClient + org.jclouds.domain.Credentials + org.jclouds.io.Payload + org.jclouds.net.IPSocket + org.jclouds.compute.domain.ExecResponse)) + (defn instantiate [impl-class & args] (let [constructor (first (filter @@ -73,7 +83,12 @@ (^org.jclouds.ssh.SshClient create [_ ^IPSocket socket ^String username ^bytes password-or-key] - (factory-fn socket username password-or-key))) + (factory-fn socket username password-or-key)) + (^org.jclouds.ssh.SshClient + create + [_ ^IPSocket socket ^Credentials credentials] + (factory-fn socket (.identity credentials) (.credential credentials))) + ) (deftype Module [factory binder]