incubator-deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@apache.org
Subject svn commit: r1073284 - in /incubator/deltacloud/trunk: server/lib/deltacloud/drivers/sbc/ tests/sbc/
Date Tue, 22 Feb 2011 10:20:49 GMT
Author: mfojtik
Date: Tue Feb 22 10:20:48 2011
New Revision: 1073284

URL: http://svn.apache.org/viewvc?rev=1073284&view=rev
Log:
Work around for SBC's HWP IDs
Cucumber features for SBC driver (thanks to Eric Woods)

Added:
    incubator/deltacloud/trunk/tests/sbc/
    incubator/deltacloud/trunk/tests/sbc/hardware_profile.feature
    incubator/deltacloud/trunk/tests/sbc/images.feature
    incubator/deltacloud/trunk/tests/sbc/instances.feature
    incubator/deltacloud/trunk/tests/sbc/realms.feature
Modified:
    incubator/deltacloud/trunk/server/lib/deltacloud/drivers/sbc/sbc_client.rb
    incubator/deltacloud/trunk/server/lib/deltacloud/drivers/sbc/sbc_driver.rb

Modified: incubator/deltacloud/trunk/server/lib/deltacloud/drivers/sbc/sbc_client.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/drivers/sbc/sbc_client.rb?rev=1073284&r1=1073283&r2=1073284&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/drivers/sbc/sbc_client.rb (original)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/drivers/sbc/sbc_client.rb Tue Feb 22
10:20:48 2011
@@ -19,10 +19,14 @@
 require 'base64'
 require 'net/https'
 require 'json'
+require 'digest/md5'
 
 module Deltacloud
   module Drivers
     module SBC
+      
+class FixtureNotFound < Exception; end
+  
 #
 # Client for the IBM Smart Business Cloud (SBC).
 #
@@ -108,44 +112,60 @@ class SBCClient
   # HTTP GET
   #
   def get(path, headers)
-    resp = @service.get(@rest_base + path, headers)
-    unless resp.is_a?(Net::HTTPSuccess)
-      backend_error!(resp)
+    if ENV['RACK_ENV'] == 'test'
+      mock_request(:get, path, {}, headers)
+    else
+      resp = @service.get(@rest_base + path, headers)
+      unless resp.is_a?(Net::HTTPSuccess)
+        backend_error!(resp)
+      end
+      resp.body
     end
-    resp.body
   end
 
   #
   # HTTP PUT
   #
   def put(path, body, headers)
-    resp = @service.put(@rest_base + path, body, headers)
-    unless resp.is_a?(Net::HTTPSuccess)
-      backend_error!(resp)
+    if ENV['RACK_ENV'] == 'test'
+      mock_request(:get, path, {}, headers)
+    else
+      resp = @service.put(@rest_base + path, body, headers)
+      unless resp.is_a?(Net::HTTPSuccess)
+        backend_error!(resp)
+      end
+      resp.body
     end
-    resp.body
   end
 
   #
   # HTTP POST
   #
   def post(path, body, headers)
-    resp = @service.post(@rest_base + path, body, headers)
-    unless resp.is_a?(Net::HTTPSuccess)
-      backend_error!(resp)
+    if ENV['RACK_ENV'] == 'test'
+      mock_request(:get, path, {}, headers)
+    else
+      resp = @service.post(@rest_base + path, body, headers)
+      unless resp.is_a?(Net::HTTPSuccess)
+        backend_error!(resp)
+      end
+      resp.body
     end
-    resp.body
   end
 
   #
   # HTTP DELETE
   #
   def delete(path, headers)
-    resp = @service.delete(@rest_base + path, headers)
-    unless resp.is_a?(Net::HTTPSuccess)
-      backend_error!(resp)
+    if ENV['RACK_ENV'] == 'test'
+      mock_request(:get, path, {}, headers)
+    else
+      resp = @service.delete(@rest_base + path, headers)
+      unless resp.is_a?(Net::HTTPSuccess)
+        backend_error!(resp)
+      end
+      resp.body
     end
-    resp.body
   end
 
   #
@@ -174,6 +194,55 @@ class SBCClient
   def urlencode(hash)
     hash.keys.map { |k| "#{URI.encode(k)}=#{URI.encode(hash[k])}" }.join("&")
   end
+  
+  #
+  # Reads a fake URL from local fixtures
+  #
+  def read_fake_url(filename)
+    fixture_file = "../tests/sbc/support/fixtures/#{filename}"
+    if File.exists?(fixture_file)
+      puts "Using fixture: #{fixture_file}"
+      return JSON::parse(File.read(fixture_file))
+    else
+      raise FixtureNotFound.new
+    end
+  end
+
+  #
+  # Executes a fake request from local fixtures
+  #
+  def mock_request(*args)
+    http_method, request_uri, params, headers = args[0].to_sym, args[1], args[2], args[3]
+    params ||= {}
+    fixture_filename = fixture_filename = "#{Digest::MD5.hexdigest("#{http_method}#{request_uri}#{params.inspect}#{headers.reject{|key,
value| key == "Authorization"}}")}.fixture"
+    puts "fixture filename: " + fixture_filename
+    begin
+      return read_fake_url(fixture_filename)[2]["body"]
+    rescue FixtureNotFound
+      if http_method.eql?(:get)
+        r = @service.get(@rest_base + request_uri, headers)
+      elsif http_method.eql?(:post)
+        r = @service.post(@rest_base + request_uri, body, headers)
+      elsif http_method.eql?(:put)
+        r = @service.put(@rest_base + request_uri, params, headers)
+      elsif http_method.eql?(:delete)
+        r = @service.delete(@rest_base + request_uri, headers)
+      end
+      response = {
+        "body" => r.body,
+        "status" => r.code,
+        "Content-Type" => r["Content-Type"]
+      }
+      fixtures_dir = "../tests/sbc/support/fixtures/"
+      FileUtils.mkdir_p(fixtures_dir)
+      puts "Saving fixture #{fixture_filename}"
+      File.open(File::join(fixtures_dir, fixture_filename), 'w') do |f|
+        f.puts [request_uri, http_method, response].to_json
+      end
+      retry
+    end
+  end
+  
 end
     end
   end

Modified: incubator/deltacloud/trunk/server/lib/deltacloud/drivers/sbc/sbc_driver.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/drivers/sbc/sbc_driver.rb?rev=1073284&r1=1073283&r2=1073284&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/drivers/sbc/sbc_driver.rb (original)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/drivers/sbc/sbc_driver.rb Tue Feb 22
10:20:48 2011
@@ -80,7 +80,7 @@ class SBCDriver < Deltacloud::BaseDriver
     body.delete('hwp_id')
     body.delete('realm_id')
 
-    # Lookup image if nil; avoids extra lookup
+    # Lookup image if nil; tries to avoids extra lookup
     if @last_image.nil? || @last_image['id'] != opts[:image_id]
       @last_image = sbc_client.list_images(image_id).map[0]
     end
@@ -88,7 +88,7 @@ class SBCDriver < Deltacloud::BaseDriver
     # Map DeltaCloud keywords to SBC
     body['imageID'] = opts[:image_id]
     body['location'] = opts[:realm_id] || @last_image['location']
-    body['instanceType'] = opts[:hwp_id] || @last_image['supportedInstanceTypes'][0]['id']
+    body['instanceType'] = opts[:hwp_id].gsub('-', '/') || @last_image['supportedInstanceTypes'][0]['id']
 
     # Submit instance, parse response
     convert_instance(sbc_client.create_instance(body).map[0])
@@ -194,7 +194,7 @@ class SBCDriver < Deltacloud::BaseDriver
       :actions => instance_actions_for(state),
       :public_addresses => [instance["primaryIP"]["ip"]],
       :private_addresses => [],
-      :instance_profile => InstanceProfile.new(instance["instanceType"]), # TODO: InstanceProfile
isn't looking up HW profiles?
+      :instance_profile => InstanceProfile.new(instance["instanceType"].gsub('/', '-')),
       :launch_time => instance["launchTime"],
       :keyname => instance["keyName"]
     )
@@ -231,63 +231,63 @@ class SBCDriver < Deltacloud::BaseDriver
   #
   # TODO: HWP IDs contain '/'; results in invalid URL
   #
-  define_hardware_profile('COP32.1/2048/60') do
+  define_hardware_profile('COP32.1-2048-60') do
     cpu				1
     memory			2 * 1024
     storage			60
     architecture	'i386'
   end
 
-  define_hardware_profile('COP64.2/4096/60') do
+  define_hardware_profile('COP64.2-4096-60') do
     cpu				2
     memory			4 * 1024
     storage			60
     architecture	'i386_x64'
   end
 
-  define_hardware_profile('BRZ32.1/2048/60*175') do
+  define_hardware_profile('BRZ32.1-2048-60*175') do
     cpu				1
     memory			2 * 1024
     storage			175
     architecture	'i386'
   end
 
-  define_hardware_profile('BRZ64.2/4096/60*500*350') do
+  define_hardware_profile('BRZ64.2-4096-60*500*350') do
     cpu				2
     memory			4 * 1024
     storage			850
     architecture	'i386_x64'
   end
 
-  define_hardware_profile('SLV32.2/4096/60*350') do
+  define_hardware_profile('SLV32.2-4096-60*350') do
     cpu				3
     memory			5 * 1024
     storage			350
     architecture	'i386'
   end
 
-  define_hardware_profile('SLV64.4/8192/60*500*500') do
+  define_hardware_profile('SLV64.4-8192-60*500*500') do
     cpu				4
     memory			8 * 1024
     storage			1024
     architecture	'i386_x64'
   end
 
-  define_hardware_profile('GLD32.4/4096/60*350') do
+  define_hardware_profile('GLD32.4-4096-60*350') do
     cpu				4
     memory			4 * 1024
     storage			350
     architecture	'i386'
   end
 
-  define_hardware_profile('GLD64.8/16384/60*500*500') do
+  define_hardware_profile('GLD64.8-16384-60*500*500') do
     cpu				8
     memory			16 * 1024
     storage			1024
     architecture	'i386_x64'
   end
 
-  define_hardware_profile('PLT64.16/16384/60*500*500*500*500') do
+  define_hardware_profile('PLT64.16-16384-60*500*500*500*500') do
     cpu				16
     memory			16 * 1024
     storage			2048

Added: incubator/deltacloud/trunk/tests/sbc/hardware_profile.feature
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/tests/sbc/hardware_profile.feature?rev=1073284&view=auto
==============================================================================
--- incubator/deltacloud/trunk/tests/sbc/hardware_profile.feature (added)
+++ incubator/deltacloud/trunk/tests/sbc/hardware_profile.feature Tue Feb 22 10:20:48 2011
@@ -0,0 +1,37 @@
+Feature: Working with SBC hardware profiles 
+
+  Scenario: Get list of available hardware profiles
+    Given I enter hardware_profiles collection
+    And I am authorized with my credentials
+    When I request HTML response
+    Then result should be valid HTML
+    When I request XML response
+    Then result should be valid XML
+    And result should contain 9 hardware_profiles
+    And name of these hardware_profiles should be
+      |  COP32.1-2048-60  |
+      |  COP64.2-4096-60  |
+      |  BRZ32.1-2048-60*175  |
+      |  BRZ64.2-4096-60*500*350  |
+      |  SLV32.2-4096-60*350  |
+      |  SLV64.4-8192-60*500*500  |
+      |  GLD32.4-4096-60*350  |
+      |  GLD64.8-16384-60*500*500  |
+      |  PLT64.16-16384-60*500*500*500*500  |
+    And fixed properties should be
+      | architecture |
+      | memory  |
+      | cpu     |
+      | storage |
+
+  Scenario: Get details about COP32.1-2048-60 hardware_profile
+    Given I enter hardware_profiles collection
+    And I am authorized with my credentials
+    And I choose hardware_profile with id COP32.1-2048-60
+    When I request HTML response
+    Then result should be valid HTML
+    When I request XML response
+    Then result should be valid XML
+    And result should contain one hardware_profile
+    And name of this hardware_profile should be COP32.1-2048-60
+    # TODO: test value attributes for memory, architecture, storage, and cpu

Added: incubator/deltacloud/trunk/tests/sbc/images.feature
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/tests/sbc/images.feature?rev=1073284&view=auto
==============================================================================
--- incubator/deltacloud/trunk/tests/sbc/images.feature (added)
+++ incubator/deltacloud/trunk/tests/sbc/images.feature Tue Feb 22 10:20:48 2011
@@ -0,0 +1,29 @@
+Feature: Working with SBC images
+
+  Scenario: Get list of available images
+    Given I enter images collection
+    And I am authorized with my credentials
+    When I request HTML response
+    Then result should be valid HTML
+    When I request XML response
+    Then result should be valid XML
+    And result should contain 285 images
+    And name of these images should be
+      | IBM Lotus Domino Enterprise Server V8.5.2 - PAYG |
+      | SUSE Linux Enterprise Server 11 for x86 |
+      | IBM Rational Reqmnt Composer 2.0.0.2 - BYOL |
+      
+   Scenario: Get details about Mashup Center image
+    Given I enter images collection
+    And I am authorized with my credentials
+    And I choose image with id 20006009
+    When I request HTML response
+    Then result should be valid HTML
+    When I request XML response
+    Then result should be valid XML
+    And result should contain one image
+    And attribute id should be set to 20006009
+    And the image should have properties set to
+      |  state  |  AVAILABLE  |
+      |  owner_id  |  SYSTEM  |
+      |  architecture  |  i386  |
\ No newline at end of file

Added: incubator/deltacloud/trunk/tests/sbc/instances.feature
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/tests/sbc/instances.feature?rev=1073284&view=auto
==============================================================================
--- incubator/deltacloud/trunk/tests/sbc/instances.feature (added)
+++ incubator/deltacloud/trunk/tests/sbc/instances.feature Tue Feb 22 10:20:48 2011
@@ -0,0 +1,45 @@
+Feature: Working with IBM SBC instances
+
+  Scenario: Get list of all instances
+    Given I enter instances collection
+    And I am authorized with my credentials
+    When I request HTML response
+    Then result should be valid HTML
+    When I request XML response
+    Then result should be valid XML
+    And result should contain 3 instances
+    And name of these instances should be
+      |  Win2008  |
+      |  EricTest1  |
+      |  EricTest2  |
+    And each instance should have properties set to
+      |  state  |  RUNNING  |
+      
+  Scenario: Get details about instance 48151
+    Given I enter instances collection
+    And I am authorized with my credentials
+    And I choose instance with id 48151
+    When I request HTML response
+    Then result should be valid HTML
+    When I request XML response
+    Then result should be valid XML
+    And result should contain one instance
+    And instance should be in RUNNING state
+    And instance should have defined actions
+      | reboot |
+      | destroy |
+    And attribute id should be set to 48151
+    #TODO: And the property realm should have attribute realm set to 41
+    #TODO: And the property image should have attribute id set to 20006009
+    
+  Scenario: Restart instance 48151
+    Given I enter instances collection
+    And I am authorized with my credentials
+    And I choose instance with id 48151
+    When I request XML response
+    Then result should be valid XML
+    And result should contain one instance
+    And attribute id should be set to 48151
+    Then I want to reboot this instance
+    And I follow reboot link in actions
+    And instance should be in RUNNING state
\ No newline at end of file

Added: incubator/deltacloud/trunk/tests/sbc/realms.feature
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/tests/sbc/realms.feature?rev=1073284&view=auto
==============================================================================
--- incubator/deltacloud/trunk/tests/sbc/realms.feature (added)
+++ incubator/deltacloud/trunk/tests/sbc/realms.feature Tue Feb 22 10:20:48 2011
@@ -0,0 +1,31 @@
+Feature: Working with SBC realms
+
+  Scenario: Get list of available realms
+    Given I enter realms collection
+    And I am authorized with my credentials
+    When I request HTML response
+    Then result should be valid HTML
+    When I request XML response
+    Then result should be valid XML
+    And result should contain 4 realms
+    And name of these realms should be
+      | RTP |
+      | EHN |
+      | us-co-dc1 |
+      | ca-on-dc1 |
+    And each realm should have properties set to
+      | state |  AVAILABLE  |
+
+  Scenario: Get details about RTP realm
+    Given I enter realms collection
+    And I am authorized with my credentials
+    And I choose realm with id 41
+    When I request HTML response
+    Then result should be valid HTML
+    When I request XML response
+    Then result should be valid XML
+    And result should contain one realm
+    And attribute id should be set to 41
+    And the realm should have properties set to
+      |  name  |  RTP  |
+      |  state  |  AVAILABLE  |
\ No newline at end of file



Mime
View raw message