deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@apache.org
Subject [4/13] git commit: EC2: Added initial support for EC2 driver unit tests
Date Tue, 24 Jul 2012 07:39:47 GMT
EC2: Added initial support for EC2 driver unit tests

Signed-off-by: Michal fojtik <mfojtik@redhat.com>


Project: http://git-wip-us.apache.org/repos/asf/deltacloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltacloud/commit/3e9b13a0
Tree: http://git-wip-us.apache.org/repos/asf/deltacloud/tree/3e9b13a0
Diff: http://git-wip-us.apache.org/repos/asf/deltacloud/diff/3e9b13a0

Branch: refs/heads/master
Commit: 3e9b13a0b12f150f63a18a5e04dbaa78387e6156
Parents: 6bb0bac
Author: Michal Fojtik <mfojtik@redhat.com>
Authored: Mon Jul 23 14:36:23 2012 +0200
Committer: Michal fojtik <mfojtik@redhat.com>
Committed: Tue Jul 24 09:38:00 2012 +0200

----------------------------------------------------------------------
 server/Gemfile                                     |    3 +
 server/Rakefile                                    |    3 +-
 server/tests/deltacloud/server_test.rb             |   65 +-
 server/tests/deprecated/api/library_test.rb        |   42 -
 server/tests/drivers/base/library_test.rb          |   46 +
 server/tests/drivers/ec2/common.rb                 |   61 +
 ...001_must_throw_error_when_wrong_credentials.yml |   63 +
 .../test_0002_must_return_list_of_images.yml       | 8749 +++++++++++++++
 .../test_0002_must_return_list_of_instances.yml    | 1633 +++
 .../test_0002_must_return_list_of_keys.yml         |   69 +
 .../test_0002_must_return_list_of_realms.yml       |   85 +
 .../test_0003_must_allow_to_filter_images.yml      |  783 ++
 .../test_0003_must_allow_to_filter_instances.yml   | 6779 +++++++++++
 .../test_0003_must_allow_to_filter_keys.yml        |  163 +
 .../test_0003_must_allow_to_filter_realms.yml      |  167 +
 ...st_0004_must_allow_to_retrieve_single_image.yml |  199 +
 ...0004_must_allow_to_retrieve_single_instance.yml |  244 +
 ...test_0004_must_allow_to_retrieve_single_key.yml |  131 +
 ...st_0004_must_allow_to_retrieve_single_realm.yml |  134 +
 ...ate_a_new_instance_if_instance_supported-10.yml |   52 +
 ...eate_a_new_instance_if_instance_supported-8.yml |   60 +
 ...eate_a_new_instance_if_instance_supported-9.yml |   60 +
 ...w_instance_if_instance_supported-stopped-10.yml |   63 +
 ...create_a_new_instance_if_instance_supported.yml |  886 ++
 .../test_0005_must_allow_to_create_a_new_key.yml   |  164 +
 ...tance_count_parameter-i-58ebc420-running-10.yml |   60 +
 ...tance_count_parameter-i-58ebc420-stopped-10.yml |   62 +
 ...tance_count_parameter-i-66ebc41e-running-10.yml |   51 +
 ...stance_count_parameter-i-66ebc41e-running-7.yml |   60 +
 ...stance_count_parameter-i-66ebc41e-running-8.yml |   51 +
 ...stance_count_parameter-i-66ebc41e-running-9.yml |   51 +
 ...tance_count_parameter-i-66ebc41e-stopped-10.yml |   62 +
 ...nstances_using_the_instance_count_parameter.yml |  250 +
 ...llow_to_reboot_instance_in_running_state-10.yml |   51 +
 ...allow_to_reboot_instance_in_running_state-7.yml |   60 +
 ...allow_to_reboot_instance_in_running_state-8.yml |   60 +
 ...allow_to_reboot_instance_in_running_state-9.yml |   60 +
 ...reboot_instance_in_running_state-stopped-10.yml |   62 +
 ...t_allow_to_reboot_instance_in_running_state.yml |  288 +
 server/tests/drivers/ec2/images_test.rb            |   49 +
 server/tests/drivers/ec2/instance_test.rb          |  104 +
 server/tests/drivers/ec2/keys_test.rb              |   52 +
 server/tests/drivers/ec2/realms_test.rb            |   44 +
 .../tests/drivers/models/instance_address_test.rb  |   24 +
 44 files changed, 22141 insertions(+), 64 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3e9b13a0/server/Gemfile
----------------------------------------------------------------------
diff --git a/server/Gemfile b/server/Gemfile
index ef08023..369e8b9 100644
--- a/server/Gemfile
+++ b/server/Gemfile
@@ -6,5 +6,8 @@ group :development do
   gem "rack-test"
   gem "minitest"
   gem "simplecov"
+  gem "webmock"
+  gem "vcr"
+  gem "aws"
 end
 

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3e9b13a0/server/Rakefile
----------------------------------------------------------------------
diff --git a/server/Rakefile b/server/Rakefile
index f232933..41eb7fa 100644
--- a/server/Rakefile
+++ b/server/Rakefile
@@ -119,6 +119,7 @@ Rake::TestTask.new do |t|
     'tests/drivers/models/*test.rb',          # Deltacloud models tests
     'tests/deltacloud/*test.rb',              # Deltacloud internal API tests
     'tests/deltacloud/collections/*test.rb',  # Deltacloud collections
-    'tests/drivers/*/*test.rb'                # Deltacloud driver specific unit tests
+    'tests/drivers/mock/*test.rb',            # Deltacloud Mock driver specific unit tests
+    'tests/drivers/ec2/*test.rb'              # Deltacloud EC2 driver specific unit tests
   ]
 end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3e9b13a0/server/tests/deltacloud/server_test.rb
----------------------------------------------------------------------
diff --git a/server/tests/deltacloud/server_test.rb b/server/tests/deltacloud/server_test.rb
index 811a3ff..816112a 100644
--- a/server/tests/deltacloud/server_test.rb
+++ b/server/tests/deltacloud/server_test.rb
@@ -8,65 +8,88 @@ describe Deltacloud::API do
   end
 
   it 'has the config set property' do
-    Deltacloud::API.config.must_be_kind_of Deltacloud::Server
-    Deltacloud::API.config.root_url.must_equal Deltacloud[:deltacloud].root_url
+    Deltacloud::config[:deltacloud].must_be_kind_of Deltacloud::Server
+    root_url.must_equal Deltacloud[:deltacloud].root_url
   end
 
   it 'must provide the root_url entrypoint' do
-    get Deltacloud::API.config.root_url
+    get root_url
     status.must_equal 200
     xml.root.name.must_equal 'api'
   end
 
   it 'must advertise current API version in response headers' do
-    get Deltacloud::API.config.root_url
+    get root_url
     headers['Server'].must_match /Apache-Deltacloud\/(\d+).(\d+).(\d+)/
     xml.root.name.must_equal 'api'
   end
 
   it 'must advertise current API driver in response headers' do
-    get Deltacloud::API.config.root_url
+    get root_url
     headers['X-Deltacloud-Driver'].must_equal ENV['API_DRIVER']
     xml.root.name.must_equal 'api'
   end
 
+  it 'must force authentication when :force_auth param set' do
+    get root_url, :force_auth => '1'
+    status.must_equal 401
+    authorize 'mockuser', 'mockpassword'
+    get root_url, :force_auth => '1'
+    status.must_equal 200
+  end
+
+  it 'must return list of collections on OPTIONS' do
+    options root_url
+    headers['Allow'].wont_be_nil
+    headers['Allow'].split(',').must_include 'Sinatra::Rabbit::DriversCollection'
+  end
+
+  it 'must support setting driver and provider using POST' do
+    post root_url, { :driver => :ec2 }
+    headers['Location'].wont_be_nil
+    headers['Location'].must_match /\/api;driver=ec2/
+    post root_url, { :provider => :test_provider }
+    headers['Location'].wont_be_nil
+    headers['Location'].must_match /\/api;provider=test_provider/
+  end
+
   it 'must support matrix parameters for changing API driver' do
-    get Deltacloud::API.config.root_url + ';driver=ec2'
+    get root_url + ';driver=ec2'
     headers['X-Deltacloud-Driver'].must_equal 'ec2'
     xml.root.name.must_equal 'api'
   end
 
   it 'must support matrix parameters for changing API provider' do
-    get Deltacloud::API.config.root_url + ';provider=someprovider'
+    get root_url + ';provider=someprovider'
     headers['X-Deltacloud-Provider'].must_equal 'someprovider'
     xml.root.name.must_equal 'api'
   end
 
   it 'must support matrix parameters for changing both API provider and driver' do
-    get Deltacloud::API.config.root_url + ';provider=someprovider;driver=ec2'
+    get root_url + ';provider=someprovider;driver=ec2'
     headers['X-Deltacloud-Provider'].must_equal 'someprovider'
     headers['X-Deltacloud-Driver'].must_equal 'ec2'
     xml.root.name.must_equal 'api'
   end
 
   it 'must change driver back to default when no matrix param set' do
-    get Deltacloud::API.config.root_url + ';driver=ec2'
+    get root_url + ';driver=ec2'
     headers['X-Deltacloud-Driver'].must_equal 'ec2'
-    get Deltacloud::API.config.root_url
+    get root_url
     headers['X-Deltacloud-Driver'].must_equal ENV['API_DRIVER']
     xml.root.name.must_equal 'api'
   end
 
   it 'must change provider back to default when no matrix param set' do
-    get Deltacloud::API.config.root_url + ';provider=someprovider'
+    get root_url + ';provider=someprovider'
     headers['X-Deltacloud-Provider'].must_equal 'someprovider'
-    get Deltacloud::API.config.root_url
+    get root_url
     headers['X-Deltacloud-Provider'].must_be_nil
     xml.root.name.must_equal 'api'
   end
 
   it 'must support matrix params when they are part of URL' do
-    get Deltacloud::API.config.root_url + ';driver=ec2/hardware_profiles'
+    get root_url + ';driver=ec2/hardware_profiles'
     headers['X-Deltacloud-Driver'].must_equal 'ec2'
     status.must_equal 200
     xml.root.name.must_equal 'hardware_profiles'
@@ -74,25 +97,25 @@ describe Deltacloud::API do
 
   it 'must switch driver using X-Deltacloud-Driver HTTP header in request' do
     header 'X-Deltacloud-Driver', 'ec2'
-    get Deltacloud::API.config.root_url
+    get root_url
     headers['X-Deltacloud-Driver'].must_equal 'ec2'
     header 'X-Deltacloud-Driver', 'mock'
-    get Deltacloud::API.config.root_url
+    get root_url
     headers['X-Deltacloud-Driver'].must_equal 'mock'
   end
 
   it 'must switch provider using X-Deltacloud-Provider HTTP header in request' do
     header 'X-Deltacloud-Provider', 'someprovider'
-    get Deltacloud::API.config.root_url
+    get root_url
     headers['X-Deltacloud-Provider'].must_equal 'someprovider'
     header 'X-Deltacloud-Provider', nil
-    get Deltacloud::API.config.root_url
+    get root_url
     headers['X-Deltacloud-Provider'].must_be_nil
   end
 
   it 'must support media type negotiation for JSON format' do
     header 'Accept', 'application/json'
-    get Deltacloud::API.config.root_url
+    get root_url
     status.must_equal 200
     headers['Content-Type'].must_equal 'application/json'
     JSON::parse(response_body).must_be_kind_of Hash
@@ -101,7 +124,7 @@ describe Deltacloud::API do
 
   it 'must support media type negotiation for HTML format' do
     header 'Accept', 'text/html'
-    get Deltacloud::API.config.root_url
+    get root_url
     status.must_equal 200
     headers['Content-Type'].must_equal 'text/html'
     response_body.must_match /^<\!DOCTYPE html>/
@@ -109,7 +132,7 @@ describe Deltacloud::API do
 
   it 'must support media type negotiation for XML format' do
     header 'Accept', 'application/xml'
-    get Deltacloud::API.config.root_url
+    get root_url
     status.must_equal 200
     headers['Content-Type'].must_equal 'application/xml'
     xml.root.name.must_equal 'api'
@@ -117,7 +140,7 @@ describe Deltacloud::API do
 
   it 'must return proper error when unknown media type requested' do
     header 'Accept', 'application/unknown'
-    get Deltacloud::API.config.root_url
+    get root_url
     status.must_equal 406
   end
 

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3e9b13a0/server/tests/deprecated/api/library_test.rb
----------------------------------------------------------------------
diff --git a/server/tests/deprecated/api/library_test.rb b/server/tests/deprecated/api/library_test.rb
index 025ece6..8d68e20 100644
--- a/server/tests/deprecated/api/library_test.rb
+++ b/server/tests/deprecated/api/library_test.rb
@@ -3,46 +3,4 @@ require_relative '../../lib/deltacloud/api'
 
 require 'pp'
 
-describe 'Deltacloud API Library' do
 
-  it 'should return the driver configuration' do
-    Deltacloud.must_respond_to :drivers
-    Deltacloud.drivers.wont_be_nil
-    Deltacloud.drivers.must_be_kind_of Hash
-  end
-
-  it 'should be constructed just using the driver parameter' do
-    Deltacloud.new(:mock).must_be_instance_of Deltacloud::Library
-    Deltacloud.new(:mock).current_provider.must_be_nil
-    Deltacloud.new(:mock).current_driver.must_equal 'mock'
-    Deltacloud.new(:mock).backend.must_be_instance_of Deltacloud::Drivers::Mock::MockDriver
-    Deltacloud.new(:mock).credentials.user.must_equal 'mockuser'
-    Deltacloud.new(:mock).credentials.password.must_equal 'mockpassword'
-  end
-
-  it 'should allow to set credentials' do
-    Deltacloud.new(:mock, :user => 'testuser', :password => 'testpassword').credentials.user.must_equal
'testuser'
-    Deltacloud.new(:mock, :user => 'testuser', :password => 'testpassword').credentials.password.must_equal
'testpassword'
-  end
-
-  it 'should allow to set the provider' do
-    Deltacloud.new(:mock, :provider => 'someprovider').current_provider.must_equal 'someprovider'
-    Deltacloud.new(:mock).current_provider.must_be_nil
-  end
-
-  it 'should return pre-defined providers for the driver' do
-    Deltacloud.new(:ec2).providers[:entrypoints].must_be_kind_of Hash
-  end
-
-  it 'should yield the backend driver' do
-    Deltacloud.new :mock do |mock|
-      mock.must_be_instance_of Deltacloud::Drivers::Mock::MockDriver
-    end
-  end
-
-  it 'should return the API version' do
-    Deltacloud::API_VERSION.wont_be_empty
-    Deltacloud::new(:mock).version.wont_be_empty
-  end
-
-end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3e9b13a0/server/tests/drivers/base/library_test.rb
----------------------------------------------------------------------
diff --git a/server/tests/drivers/base/library_test.rb b/server/tests/drivers/base/library_test.rb
new file mode 100644
index 0000000..5f68145
--- /dev/null
+++ b/server/tests/drivers/base/library_test.rb
@@ -0,0 +1,46 @@
+require 'minitest/autorun'
+require_relative File.join('..', '..', '..', 'lib', 'deltacloud', 'api.rb')
+
+describe 'Deltacloud API Library' do
+
+  it 'should return the driver configuration' do
+    Deltacloud.must_respond_to :drivers
+    Deltacloud.drivers.wont_be_nil
+    Deltacloud.drivers.must_be_kind_of Hash
+  end
+
+  it 'should be constructed just using the driver parameter' do
+    Deltacloud.new(:mock).must_be_instance_of Deltacloud::Library
+    Deltacloud.new(:mock).current_provider.must_be_nil
+    Deltacloud.new(:mock).current_driver.must_equal 'mock'
+    Deltacloud.new(:mock).backend.must_be_instance_of Deltacloud::Drivers::Mock::MockDriver
+    Deltacloud.new(:mock).credentials.user.must_equal 'mockuser'
+    Deltacloud.new(:mock).credentials.password.must_equal 'mockpassword'
+  end
+
+  it 'should allow to set credentials' do
+    Deltacloud.new(:mock, :user => 'testuser', :password => 'testpassword').credentials.user.must_equal
'testuser'
+    Deltacloud.new(:mock, :user => 'testuser', :password => 'testpassword').credentials.password.must_equal
'testpassword'
+  end
+
+  it 'should allow to set the provider' do
+    Deltacloud.new(:mock, :provider => 'someprovider').current_provider.must_equal 'someprovider'
+    Deltacloud.new(:mock).current_provider.must_be_nil
+  end
+
+  it 'should return pre-defined providers for the driver' do
+    Deltacloud.new(:ec2).providers[:entrypoints].must_be_kind_of Hash
+  end
+
+  it 'should yield the backend driver' do
+    Deltacloud.new :mock do |mock|
+      mock.must_be_instance_of Deltacloud::Drivers::Mock::MockDriver
+    end
+  end
+
+  it 'should return the API version' do
+    Deltacloud::API_VERSION.wont_be_empty
+    Deltacloud::new(:mock).version.wont_be_empty
+  end
+
+end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3e9b13a0/server/tests/drivers/ec2/common.rb
----------------------------------------------------------------------
diff --git a/server/tests/drivers/ec2/common.rb b/server/tests/drivers/ec2/common.rb
new file mode 100644
index 0000000..08baed1
--- /dev/null
+++ b/server/tests/drivers/ec2/common.rb
@@ -0,0 +1,61 @@
+# Make less noise to console
+ENV['RACK_ENV'] ||= 'test'
+
+# Warning: RightHttpConnection has to be required before WebMock is required !!!
+# Lets require that:
+require 'right_http_connection'
+
+require 'vcr'
+require 'time'
+
+# This code was originally copied from:
+# https://github.com/jtrupiano/timecop/issues/8#issuecomment-1396047
+#
+# Since 'timecop' gem has broken 'timezone' support, this small monkey-patching
+# on Time object seems to fix this issue.
+
+class Time
+  module TimeMock
+    attr_accessor :mock_time
+
+    def mock_now
+      @mock_time || Time.original_now
+    end
+
+    def be(a_time)
+      @mock_time = Time.parse(a_time)
+    end
+
+  end
+
+  class << self
+    include TimeMock
+    alias_method :original_now, :now
+    alias_method :now, :mock_now
+  end
+end
+
+# Freeze time, so EC2 signatures have all the same time
+# This will avoid IncorrectSignature exceptions
+
+Time.be(DateTime.parse("2012-07-23 12:21:00 +0000").to_s)
+
+VCR.configure do |c|
+  # NOTE: Empty this directory before re-recording
+  c.cassette_library_dir = File.join(File.dirname(__FILE__), 'fixtures')
+  c.hook_into :webmock
+  # Set this to :new_epizodes when you want to 're-record'
+  c.default_cassette_options = { :record => :none }
+end
+
+# Some test scenarios use .wait_for! method that do multiple retries
+# for requests. We need to deal with that passing a Proc that use
+# different cassette for each request
+
+def record_retries(name='')
+  {
+    :before => Proc.new { |r, &block|
+      VCR.use_cassette("#{__name__}-#{name.empty? ? '' : "#{name}-"}#{r}", &block)
+    }
+  }
+end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3e9b13a0/server/tests/drivers/ec2/fixtures/test_0001_must_throw_error_when_wrong_credentials.yml
----------------------------------------------------------------------
diff --git a/server/tests/drivers/ec2/fixtures/test_0001_must_throw_error_when_wrong_credentials.yml
b/server/tests/drivers/ec2/fixtures/test_0001_must_throw_error_when_wrong_credentials.yml
new file mode 100644
index 0000000..cd006d8
--- /dev/null
+++ b/server/tests/drivers/ec2/fixtures/test_0001_must_throw_error_when_wrong_credentials.yml
@@ -0,0 +1,63 @@
+---
+http_interactions:
+- request:
+    method: get
+    uri: https://ec2.us-east-1.amazonaws.com/?AWSAccessKeyId=unknown&Action=DescribeImages&Filter.1.Name=image-type&Filter.1.Value.1=machine&Owner.1=amazon&Signature=tZKogpJnRap1OrEk+vl7M8gWfNOLFySYcq7JG7qTLv0=&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2012-07-23T12:21:00.000Z&Version=2010-08-31
+    body:
+      encoding: US-ASCII
+      string: ''
+    headers:
+      Accept:
+      - ! '*/*'
+      User-Agent:
+      - ''
+  response:
+    status:
+      code: 401
+      message: Unauthorized
+    headers:
+      Transfer-Encoding:
+      - chunked
+      Date:
+      - Mon, 23 Jul 2012 12:23:47 GMT
+      Server:
+      - AmazonEC2
+    body:
+      encoding: US-ASCII
+      string: ! '<?xml version="1.0" encoding="UTF-8"?>
+
+        <Response><Errors><Error><Code>AuthFailure</Code><Message>AWS
was not able
+        to validate the provided access credentials</Message></Error></Errors><RequestID>c256916e-e7d4-4608-9899-bea730f4d2d9</RequestID></Response>'
+    http_version: 
+  recorded_at: Mon, 23 Jul 2012 12:21:00 GMT
+- request:
+    method: get
+    uri: https://ec2.us-east-1.amazonaws.com/?AWSAccessKeyId=unknown&Action=DescribeAvailabilityZones&Signature=pDra/mlZMKekW8hNooLe1vdJb+U2li9cVe/ZvXHl7Vc=&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2012-07-23T12:21:00.000Z&Version=2010-08-31
+    body:
+      encoding: US-ASCII
+      string: ''
+    headers:
+      Accept:
+      - ! '*/*'
+      User-Agent:
+      - ''
+  response:
+    status:
+      code: 401
+      message: Unauthorized
+    headers:
+      Transfer-Encoding:
+      - chunked
+      Date:
+      - Mon, 23 Jul 2012 12:26:14 GMT
+      Server:
+      - AmazonEC2
+    body:
+      encoding: US-ASCII
+      string: ! '<?xml version="1.0" encoding="UTF-8"?>
+
+        <Response><Errors><Error><Code>AuthFailure</Code><Message>AWS
was not able
+        to validate the provided access credentials</Message></Error></Errors><RequestID>b25d2950-e46e-4b24-a0a1-040a0cd371d2</RequestID></Response>'
+    http_version: 
+  recorded_at: Mon, 23 Jul 2012 12:21:00 GMT
+recorded_with: VCR 2.1.1


Mime
View raw message