deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@redhat.com
Subject [PATCH] CIMI API Tests - adds discovery for URIs
Date Fri, 30 Nov 2012 11:58:07 GMT
From: marios <marios@redhat.com>


Signed-off-by: marios <marios@redhat.com>
---
 tests/cimi/part2_test.rb  |  6 +++---
 tests/cimi/part4_test.rb  | 19 +++++++++++--------
 tests/cimi/part5_test.rb  | 16 +++++++++-------
 tests/cimi/test_helper.rb | 21 ++++++++++++++++++++-
 4 files changed, 43 insertions(+), 19 deletions(-)

diff --git a/tests/cimi/part2_test.rb b/tests/cimi/part2_test.rb
index 0742a90..d04e026 100644
--- a/tests/cimi/part2_test.rb
+++ b/tests/cimi/part2_test.rb
@@ -48,12 +48,12 @@ end
 
   # 2.4:  Create a new CredentialResource
   log.info("Create a new CredentialResource: credential_resources is not a supported collection.")
-
   # 2.5: Create a new Machine
   model :machine do |fmt|
     cep_json = cep(:accept => :json)
-
-    RestClient.post(cep_json.json["machines"]["href"],
+    #discover the 'addURI' for creating Machine
+    add_uri = discover_uri_for("add", "machines")
+    RestClient.post(add_uri,
       "<Machine>" +
         "<name>cimi_machine_" + fmt.to_s() + "</name>" +
         "<machineTemplate>" +
diff --git a/tests/cimi/part4_test.rb b/tests/cimi/part4_test.rb
index e5c1433..8e27c9c 100644
--- a/tests/cimi/part4_test.rb
+++ b/tests/cimi/part4_test.rb
@@ -49,7 +49,8 @@ class AddVolumeToMachine < CIMI::Test::Spec
 
 # Create a machine to attach the volume
    cep_json = cep(:accept => :json)
-   machine = RestClient.post(cep_json.json["machines"]["href"],
+   machine_add_uri = discover_uri_for("add", "machines")
+   machine = RestClient.post(machine_add_uri,
      "<Machine>" +
        "<name>cimi_machine</name>" +
        "<machineTemplate>" +
@@ -63,7 +64,8 @@ class AddVolumeToMachine < CIMI::Test::Spec
 
   # 4.3:  Create a new Volume
   model :volume do |fmt|
-    RestClient.post(cep_json.json["volumes"]["href"],
+    volume_add_uri = discover_uri_for("add", "volumes")
+    RestClient.post(volume_add_uri,
       "<Volume>" +
         "<name>cimi_volume_" + fmt.to_s() +"</name>" +
         "<description>volume for testing</description>" +
@@ -99,9 +101,9 @@ class AddVolumeToMachine < CIMI::Test::Spec
     last_response.json["resourceURI"].must_equal RESOURCE_URI.gsub("Create", "")
   end
 
-  # 4.4: Attach the new Volume to a Machine
   log.info(machine.json["id"].to_s() + " is the machine id")
-  volume = RestClient.post(cep_json.json["volumes"]["href"],
+  volume_add_uri = discover_uri_for("add", "volumes")
+  volume = RestClient.post(volume_add_uri,
   "<Volume>" +
     "<name>cimi_volume_for_attach</name>" +
     "<description>volume for attach testing</description>" +
@@ -114,9 +116,10 @@ class AddVolumeToMachine < CIMI::Test::Spec
 {'Authorization' => api.basic_auth, :accept => :json})
 
   log.info(volume.json["id"].to_s() + " is the volume id")
-
+  # 4.4: Attach the new Volume to a Machine
   model :machineWithVolume, :only => :xml do
-    RestClient.put(machine.json["id"] + "/volume_attach",
+  attach_uri = discover_uri_for_subcollection("add", machine.json['id'], "volumes")
+    RestClient.post(attach_uri,
     "<MachineVolume xmlns=\"http://schemas.dmtf.org/cimi/1/MachineVolume\">" +
     "<initialLocation>/dev/sdf</initialLocation>" +
     "<volume href=\"" + volume.json["id"] + "\"/>" +
@@ -138,8 +141,8 @@ class AddVolumeToMachine < CIMI::Test::Spec
     log.info( machine.json["id"] +
     "/volumes/" + volume.json["id"].split("/volumes/")[1])
      sleep 5
-    response = RestClient.delete(machine.json["id"] +
-      "/volumes/" + volume.json["id"].split("/volumes/")[1],
+    detach_uri = discover_uri_for("delete", "", machineWithVolume.operations)
+    response = RestClient.delete(detach_uri,
     {'Authorization' => api.basic_auth, :accept => :xml})
       response.code.must_equal 200
   end
diff --git a/tests/cimi/part5_test.rb b/tests/cimi/part5_test.rb
index 52f8b58..a5a58a9 100644
--- a/tests/cimi/part5_test.rb
+++ b/tests/cimi/part5_test.rb
@@ -29,7 +29,9 @@ class ManipulateAMachine < CIMI::Test::Spec
   # 2.1: Query the Machine
   # For some providers - need to create a machine before querying it.
   cep_json = cep(:accept => :json)
-  machine_created = RestClient.post(cep_json.json["machines"]["href"],
+  #discover machine create URI:
+  machine_add_uri = discover_uri_for("add", "machines")
+  machine_created = RestClient.post(machine_add_uri,
     "<Machine>" +
       "<name>cimi_machine_part5</name>" +
       "<machineTemplate>" +
@@ -99,13 +101,13 @@ class ManipulateAMachine < CIMI::Test::Spec
       machine.state.upcase.eql?("STARTED"))
       log.info(RESOURCE_URI.gsub("Machine", "action/restart"))
         # This relies on odering and needs to be improved
-      machine[:operations][0][0].must_include RESOURCE_URI.gsub( "Machine", "action/restart")
-      machine[:operations][1][0].must_include RESOURCE_URI.gsub( "Machine", "action/stop")
-      machine[:operations][2][0].must_include RESOURCE_URI.gsub( "Machine", "action/capture")
+      assert_silent { discover_uri_for("restart","", machine.operations) }
+      assert_silent { discover_uri_for("stop","", machine.operations) }
+      assert_silent { discover_uri_for("capture","", machine.operations) }
     elsif machine.state.upcase.eql?("STOPPED")
-      machine[:operations][0][0].must_include RESOURCE_URI.gsub( "Machine", "action/start")
-      machine[:operations][1][0].must_include RESOURCE_URI.gsub( "Machine", "action/destroy")
-      machine[:operations][2][0].must_include RESOURCE_URI.gsub( "Machine", "action/capture")
+      assert_silent { discover_uri_for(/\/start/,"", machine.operations) }
+      assert_silent  { discover_uri_for("delete","", machine.operations) }
+      assert_silent { discover_uri_for("capture","", machine.operations) }
     else
       log.info("machine is in an intermediate state: " +  machine.state)
     end
diff --git a/tests/cimi/test_helper.rb b/tests/cimi/test_helper.rb
index ace38bd..49356dd 100644
--- a/tests/cimi/test_helper.rb
+++ b/tests/cimi/test_helper.rb
@@ -103,6 +103,24 @@ module CIMI::Test::Methods
       get(api.cep_url, params)
     end
 
+    def discover_uri_for(op, collection, operations = nil)
+      unless operations
+        cep_json = cep(:accept => :json)
+        #get the collection operations:
+        operations = get(cep_json.json["#{collection}"]["href"], {:accept=> :json}).json["operations"]
+      end
+      op_regex = Regexp.new(op, Regexp::IGNORECASE) # "add" == /add/i
+      op_uri = operations.inject(""){|res,current| res = current["href"] if current["rel"]
=~ op_regex; res}
+      raise "Couldn't discover the #{collection} Collection #{op} URI" if op_uri.empty?
+      op_uri
+    end
+
+    def discover_uri_for_subcollection(op, resource_id, subcollection)
+      subcollection_uri = get(resource_id, {:accept=> :json}).json[subcollection]["href"]
+      subcollection_ops = get(subcollection_uri, {:accept=> :json}).json["operations"]
+      discover_uri_for(op, "", subcollection_ops)
+    end
+
     def get(path, params = {})
       RestClient.get absolute_url(path), headers(params)
     end
@@ -174,7 +192,8 @@ module CIMI::Test::Methods
     end
 
     def machine_stop_start(machine, action, state)
-      response = RestClient.post( machine.id + "/" + action,
+      uri = discover_uri_for(action, "", machine.operations)
+      response = RestClient.post( uri,
             "<Action xmlns=\"http://schemas.dmtf.org/cimi/1\">" +
               "<action> http://http://schemas.dmtf.org/cimi/1/action/" + action + "</action>"
+
             "</Action>",
-- 
1.7.11.7


Mime
View raw message