You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by ma...@redhat.com on 2012/08/09 15:02:43 UTC

[PATCH 1/2] API TESTS: refactoring of common code - avoid duplication

From: marios <ma...@redhat.com>


Signed-off-by: marios <ma...@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


Re: [PATCH 1/2] API TESTS: refactoring of common code - avoid duplication

Posted by "marios@redhat.com" <ma...@redhat.com>.
On 10/08/12 16:04, Michal Fojtik wrote:
> On Aug 9, 2012, at 3:02 PM, marios@redhat.com wrote:
> 
> ACK. Small nits inline.
> 
>> From: marios <ma...@redhat.com>
>>
>>
>> Signed-off-by: marios <ma...@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/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
> 
> Why we need to have this in test_setup, when we already have core_ext
> with pluralize() method ?
> 

yeah, because these tests act as a deltacloud client. I mean, the
deltacloud code itself is not featured/imported/required here at all -
the API tests are completely independent. You can remove this directory
from the repo altogether and run it as separate entity if you wanted.

marios


>> @@ -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
>>
> 



Re: [PATCH 1/2] API TESTS: refactoring of common code - avoid duplication

Posted by Michal Fojtik <mf...@redhat.com>.
On Aug 9, 2012, at 3:02 PM, marios@redhat.com wrote:

ACK. Small nits inline.

> From: marios <ma...@redhat.com>
> 
> 
> Signed-off-by: marios <ma...@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/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

Why we need to have this in test_setup, when we already have core_ext
with pluralize() method ?

> @@ -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
> 


Re: [PATCH 2/2] API TESTS: adds filtering by hwp/arch to images test

Posted by Michal Fojtik <mf...@redhat.com>.
On Aug 9, 2012, at 3:02 PM, marios@redhat.com wrote:

ACK.

> From: marios <ma...@redhat.com>
> 
> 
> Signed-off-by: marios <ma...@redhat.com>
> ---
> tests/deltacloud/images_test.rb |   25 +++++++++++++++++++++++++
> 1 files changed, 25 insertions(+), 0 deletions(-)
> 
> diff --git a/tests/deltacloud/images_test.rb b/tests/deltacloud/images_test.rb
> index 2c3af5c..1883b72 100644
> --- a/tests/deltacloud/images_test.rb
> +++ b/tests/deltacloud/images_test.rb
> @@ -70,4 +70,29 @@ describe 'Deltacloud API Images collection' do
>     end
>   end
> 
> +
> +  it 'should be able to filter images list by owner_id attribute' do
> +    #get an image - either random or from "preferred" section of config.yaml:
> +    image_id = get_a("image")
> +    image = get(IMAGES+"/#{image_id}")
> +    owner_id = (image.xml/"image/owner_id").text
> +    filtered_image_list = get(IMAGES, {:owner_id=>owner_id})
> +    #check owner_id of each:
> +    (filtered_image_list.xml/"images/image").each do |img|
> +      (img/"owner_id").text.must_equal owner_id
> +    end
> +  end
> +
> +  it 'should be able to filter images list by architecture attribute' do
> +    #get an image - either random or from "preferred" section of config.yaml:
> +    image_id = get_a("image")
> +    image = get(IMAGES+"/#{image_id}")
> +    arch = (image.xml/"image/architecture").text
> +    filtered_image_list = get(IMAGES, {:architecture=>arch})
> +    #check architecture of each:
> +    (filtered_image_list.xml/"images/image").each do |img|
> +      (img/"architecture").text.must_equal arch
> +    end
> +  end
> +
> end
> -- 
> 1.7.6.5
> 


[PATCH 2/2] API TESTS: adds filtering by hwp/arch to images test

Posted by ma...@redhat.com.
From: marios <ma...@redhat.com>


Signed-off-by: marios <ma...@redhat.com>
---
 tests/deltacloud/images_test.rb |   25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/tests/deltacloud/images_test.rb b/tests/deltacloud/images_test.rb
index 2c3af5c..1883b72 100644
--- a/tests/deltacloud/images_test.rb
+++ b/tests/deltacloud/images_test.rb
@@ -70,4 +70,29 @@ describe 'Deltacloud API Images collection' do
     end
   end
 
+
+  it 'should be able to filter images list by owner_id attribute' do
+    #get an image - either random or from "preferred" section of config.yaml:
+    image_id = get_a("image")
+    image = get(IMAGES+"/#{image_id}")
+    owner_id = (image.xml/"image/owner_id").text
+    filtered_image_list = get(IMAGES, {:owner_id=>owner_id})
+    #check owner_id of each:
+    (filtered_image_list.xml/"images/image").each do |img|
+      (img/"owner_id").text.must_equal owner_id
+    end
+  end
+
+  it 'should be able to filter images list by architecture attribute' do
+    #get an image - either random or from "preferred" section of config.yaml:
+    image_id = get_a("image")
+    image = get(IMAGES+"/#{image_id}")
+    arch = (image.xml/"image/architecture").text
+    filtered_image_list = get(IMAGES, {:architecture=>arch})
+    #check architecture of each:
+    (filtered_image_list.xml/"images/image").each do |img|
+      (img/"architecture").text.must_equal arch
+    end
+  end
+
 end
-- 
1.7.6.5