deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@redhat.com
Subject [PATCH 1/2] API TESTS: refactoring of common code - avoid duplication
Date Thu, 09 Aug 2012 13:02:43 GMT
From: marios <marios@redhat.com>


Signed-off-by: marios <marios@redhat.com>
---
 tests/README                       |   15 ++++----
 tests/config.yaml                  |   10 +++---
 tests/deltacloud/instances_test.rb |   63 +++++++++++-------------------------
 tests/deltacloud/test_setup.rb     |   27 +++++++++++----
 4 files changed, 51 insertions(+), 64 deletions(-)

diff --git a/tests/README b/tests/README
index 4406f07..68c0ffa 100644
--- a/tests/README
+++ b/tests/README
@@ -61,14 +61,15 @@ rhevm:
 ec2:
   user: KEY
   password: SECRET_KEY
-  instances:
-    preferred_image: "ami-2b5fba42"
-    preferred_hwp: "m1.small"
-    preferred_realm: "us-east-1b"
+  preferred:
+    image: "ami-2b5fba42"
+    hardware_profile: "m1.small"
+    realm: "us-east-1b"
 
-You can add this 'instances' section under any driver in the config.yaml file and
-those values will be used in the instances test. You can also only include only
-some of those, for example a preferred_image but not a preferred_hwp.
+You can add this 'preferred' section under any driver in the config.yaml file and
+those values will be used in the instances test (and can also be used by other tests
+in future). You can also only include only some of those, for example a "image"
+ but not a "hardware_profile".
 
 
 FILE LAYOUT:
diff --git a/tests/config.yaml b/tests/config.yaml
index 5f905a6..d13b814 100644
--- a/tests/config.yaml
+++ b/tests/config.yaml
@@ -23,9 +23,9 @@ ec2:
     - "us-west-2"
     - "ap-southeast-1"
     - "ap-northeast-1"
-  preferred_provider: "us-east-1"
   #used for instances tests:
-  instances:
-    preferred_image: "ami-2b5fba42"
-    preferred_hwp: "m1.small"
-    preferred_realm: "us-east-1b"
+  preferred:
+    provider: "us-east-1"
+    image: "ami-2b5fba42"
+    hardware_profile: "m1.small"
+    realm: "us-east-1b"
diff --git a/tests/deltacloud/instances_test.rb b/tests/deltacloud/instances_test.rb
index fbfc07e..d6a916c 100644
--- a/tests/deltacloud/instances_test.rb
+++ b/tests/deltacloud/instances_test.rb
@@ -26,12 +26,7 @@ describe 'Deltacloud API instances collection' do
   begin
     #keep track of what we create for deletion after tests:
     @@created_resources = {:instances=>[], :keys=>[], :images=>[], :firewalls=>[]}
-    if api.instances_config["preferred_image"]
-      image_id = api.instances_config["preferred_image"]
-    else
-      image_list = get("/images")
-      image_id = (image_list.xml/'images/image').to_a.choice[:id]
-    end
+    image_id = get_a("image")
     res = post(INSTANCES, :image_id=>image_id)
     unless res.code == 201
       raise Exception.new("Failed to create instance from image_id #{image_id}")
@@ -60,8 +55,15 @@ puts "CLEANING UP... resources for deletion: #{@@created_resources.inspect}"
     #keys
     [:keys, :images, :firewalls].each do |col|
       @@created_resources[col].each do |k|
-        res = delete("/#{col}/#{k}")
-        @@created_resources[col].delete(k) if res.code == 204
+        attempts = 0
+        begin
+          res = delete("/#{col}/#{k}")
+          @@created_resources[col].delete(k) if res.code == 204
+        rescue Exception => e
+          sleep(10)
+          attempts += 1
+          retry if (attempts <=5)
+        end
       end
       @@created_resources.delete(col) if @@created_resources[col].empty?
     end
@@ -77,33 +79,6 @@ puts "CLEANUP attempt finished... resources looks like: #{@@created_resources.in
     end
   end
 
-  def get_image
-    if api.instances_config["preferred_image"]
-      image_id = api.instances_config["preferred_image"]
-    else
-      image_list = get("/images")
-      image_id = (image_list.xml/'images/image').to_a.choice[:id]
-    end
-  end
-
-  def get_realm
-    if api.instances_config["preferred_realm"]
-      realm_id = api.instances_config["preferred_realm"]
-    else
-      realms_list = get("/realms")
-      realm_id = (realms_list.xml/'realms/realm').to_a.choice[:id]
-    end
-  end
-
-  def get_hwp
-    if api.instances_config["preferred_hwp"]
-      hwp_id = api.instances_config["preferred_hwp"]
-    else
-      hw_profile_list = get("/hardware_profiles")
-      hwp_id = (hw_profile_list.xml/'hardware_profiles/hardware_profile').to_a.choice[:id]
-    end
-  end
-
   #Run the 'common' tests for all collections defined in common_tests_collections.rb
   CommonCollectionsTest::run_collection_and_member_tests_for("instances")
 
@@ -194,7 +169,7 @@ puts "CLEANUP attempt finished... resources looks like: #{@@created_resources.in
 
   it 'should allow to create new instance using image without realm' do
     #random image and create instance
-    image_id = get_image
+    image_id = get_a("image")
     image_id.wont_be_nil
     res = post(INSTANCES, :image_id=>image_id)
     res.code.must_equal 201
@@ -211,9 +186,9 @@ puts "CLEANUP attempt finished... resources looks like: #{@@created_resources.in
 
   it 'should allow to create new instance using image and realm' do
     #random image, realm and create instance
-    image_id = get_image
+    image_id = get_a("image")
     image_id.wont_be_nil
-    realm_id = get_realm
+    realm_id = get_a("realm")
     realm_id.wont_be_nil
     res = post(INSTANCES, :image_id=>image_id, :realm_id=>realm_id)
     res.code.must_equal 201
@@ -231,18 +206,18 @@ puts "CLEANUP attempt finished... resources looks like: #{@@created_resources.in
 
   it 'should allow to create new instance using image, realm and hardware_profile' do
     #random image, realm, hardware_profile and create instance
-    image_id = get_image
+    image_id = get_a("image")
     image_id.wont_be_nil
     #check if this image defines compatible hw_profiles:
     res = get("/images/"+image_id)
     if (res.xml/'image/hardware_profiles').empty?
-      hwp_id = get_hwp
+      hwp_id = get_a("hardware_profile")
     else
       hwp_id = (res.xml/'image/hardware_profiles/hardware_profile').to_a.choice[:id]
     end
     hwp_id.wont_be_nil
     #random realm:
-    realm_id = get_realm
+    realm_id = get_a("realm")
     realm_id.wont_be_nil
     res = post(INSTANCES, :image_id=>image_id, :realm_id=>realm_id, :hwp_id => hwp_id)
     res.code.must_equal 201
@@ -288,7 +263,7 @@ puts "CLEANUP attempt finished... resources looks like: #{@@created_resources.in
         key_res.code.must_equal 201
         key_id = (key_res.xml/'key')[0][:id]
         #create instance with this key:
-        image_id = get_image
+        image_id = get_a("image")
         res = post(INSTANCES, :image_id => image_id, :keyname => key_id)
         res.code.must_equal 201
         instance_id = (res.xml/'instance')[0][:id]
@@ -309,7 +284,7 @@ puts "CLEANUP attempt finished... resources looks like: #{@@created_resources.in
 
     it 'should allow specification of name for created instance when supported' do
       instance_name = random_name
-      image_id = get_image
+      image_id = get_a("image")
       res = post(INSTANCES, :image_id => image_id, :name => instance_name)
       res.code.must_equal 201
       instance_id = (res.xml/'instance')[0][:id]
@@ -335,7 +310,7 @@ puts "CLEANUP attempt finished... resources looks like: #{@@created_resources.in
         fw_id = (fw_res.xml/'firewall')[0][:id]
         ((fw_res.xml/'firewall/name')[0].text).must_equal fw_name
         #create instance with this firewall:
-        image_id = get_image
+        image_id = get_a("image")
         res = post(INSTANCES, :image_id => image_id, :firewalls1 => fw_id)
         res.code.must_equal 201
         instance_id = (res.xml/'instance')[0][:id]
diff --git a/tests/deltacloud/test_setup.rb b/tests/deltacloud/test_setup.rb
index e034493..6beceb1 100644
--- a/tests/deltacloud/test_setup.rb
+++ b/tests/deltacloud/test_setup.rb
@@ -43,6 +43,12 @@ class String
     return self.gsub(/es$/, '') if self =~ /sses$/
     self.gsub(/s$/, '')
   end
+  def pluralize
+    return self + 'es' if self =~ /ess$/
+    return self[0, self.length-1] + "ies" if self =~ /ty$/
+    return self if self =~ /data$/
+    self + "s"
+  end
 end
 
 class Array
@@ -83,12 +89,8 @@ module Deltacloud
         @hash[driver]["bucket_locations"]
       end
 
-      def instances_config
-        @hash[driver]["instances"] || {}
-      end
-
-      def preferred_provider
-        @hash[driver]["preferred_provider"]
+      def preferred
+        @hash[driver]["preferred"] || {}
       end
 
       def driver
@@ -166,8 +168,8 @@ module Deltacloud::Test::Methods
     end
 
     def post(path, post_body, params={})
-      if api.preferred_provider and not params[:provider]
-        params[:provider] = api.preferred_provider
+      if api.preferred["provider"] and not params[:provider]
+        params[:provider] = api.preferred["provider"]
       end
       url, headers = process_url_params(path, params)
       RestClient.post url, post_body, headers
@@ -204,6 +206,15 @@ module Deltacloud::Test::Methods
       name.insert(0, "apitest")
     end
 
+    def get_a(item)
+      if api.preferred[item]
+        item_id = api.preferred[item]
+      else
+        item_list = get("/#{item.pluralize}")
+        item_id = (item_list.xml/"#{item.pluralize}/#{item}").to_a.choice[:id]
+      end
+    end
+
     private
 
     def process_url_params(path, params)
-- 
1.7.6.5


Mime
View raw message