incubator-deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 1/3] Added basic RackSpace unit tests
Date Wed, 30 Mar 2011 13:31:11 GMT
From: Michal Fojtik <mfojtik@redhat.com>

---
 server/Rakefile                                    |   26 +++-
 server/tests/common.rb                             |    4 +-
 server/tests/drivers/mock/setup.rb                 |    3 +
 server/tests/drivers/rackspace/api_test.rb         |   41 +++++
 .../drivers/rackspace/hardware_profiles_test.rb    |   53 ++++++
 server/tests/drivers/rackspace/images_test.rb      |   40 +++++
 server/tests/drivers/rackspace/instances_test.rb   |  167 ++++++++++++++++++++
 server/tests/drivers/rackspace/realms_test.rb      |   36 +++++
 server/tests/drivers/rackspace/setup.rb            |    3 +
 9 files changed, 364 insertions(+), 9 deletions(-)
 create mode 100644 server/tests/drivers/mock/setup.rb
 create mode 100644 server/tests/drivers/rackspace/api_test.rb
 create mode 100644 server/tests/drivers/rackspace/hardware_profiles_test.rb
 create mode 100644 server/tests/drivers/rackspace/images_test.rb
 create mode 100644 server/tests/drivers/rackspace/instances_test.rb
 create mode 100644 server/tests/drivers/rackspace/realms_test.rb
 create mode 100644 server/tests/drivers/rackspace/setup.rb

diff --git a/server/Rakefile b/server/Rakefile
index e562cb5..3453bfe 100644
--- a/server/Rakefile
+++ b/server/Rakefile
@@ -28,12 +28,26 @@ begin
 rescue LoadError
 end
 
-desc "Run basic unit tests"
-Rake::TestTask.new("test") { |t|
-  t.test_files = FileList.new('tests/**/*_test.rb')
-  t.verbose = true
-  t.warning = false
-}
+namespace :test do
+
+  desc "Run mock unit tests"
+  Rake::TestTask.new("mock") { |t|
+    t.test_files = FileList.new('tests/drivers/mock/*_test.rb') + FileList.new('tests/rabbit_test.rb')
+    t.options = "-v -v"
+    t.verbose = true
+    t.warning = false
+  }
+
+  desc "Run rackspace unit tests"
+  Rake::TestTask.new("rackspace") { |t|
+    t.test_files = ['tests/common.rb', 'tests/drivers/rackspace/setup.rb'] + FileList.new('tests/drivers/rackspace/*_test.rb')
+    t.options = "-v -v"
+    t.verbose = true
+    t.warning = false
+  }
+
+
+end
 
 begin
   require 'yard'
diff --git a/server/tests/common.rb b/server/tests/common.rb
index 6efe089..e134388 100644
--- a/server/tests/common.rb
+++ b/server/tests/common.rb
@@ -1,6 +1,7 @@
 $:.unshift File.join(File.dirname(__FILE__), '..')
 
 require 'rubygems'
+require 'yaml'
 require 'base64'
 require 'test/unit'
 require 'spec'
@@ -10,11 +11,8 @@ require 'json'
 # Set proper environment variables for running test
 
 ENV['RACK_ENV']     = 'test'
-ENV['API_DRIVER']   = 'mock'
 ENV['API_HOST']     = 'localhost'
 ENV['API_PORT']     = '4040'
-ENV['API_USER']     = 'mockuser'
-ENV['API_PASSWORD'] = 'mockpassword'
 
 require 'server'
 
diff --git a/server/tests/drivers/mock/setup.rb b/server/tests/drivers/mock/setup.rb
new file mode 100644
index 0000000..60a7094
--- /dev/null
+++ b/server/tests/drivers/mock/setup.rb
@@ -0,0 +1,3 @@
+ENV['API_DRIVER']   = "mock"
+ENV['API_USER']     = 'mockuser'
+ENV['API_PASSWORD'] = 'mockpassword'
diff --git a/server/tests/drivers/rackspace/api_test.rb b/server/tests/drivers/rackspace/api_test.rb
new file mode 100644
index 0000000..e351388
--- /dev/null
+++ b/server/tests/drivers/rackspace/api_test.rb
@@ -0,0 +1,41 @@
+$:.unshift File.join(File.dirname(__FILE__), '..', '..', '..')
+require 'tests/common'
+
+module RackspaceTest
+
+  class ApiTest < Test::Unit::TestCase
+    include Rack::Test::Methods
+
+    def app
+      Sinatra::Application
+    end
+
+    def test_01_it_returns_entry_points
+      do_xml_request '/api;driver=rackspace/?force_auth=1', {}, true
+      (last_xml_response/'/api').first[:driver].should == 'rackspace'
+      (last_xml_response/'/api/link').length.should > 0
+    end
+
+    def test_02_it_has_rackspace_features
+      do_xml_request '/api;driver=rackspace'
+      features = (last_xml_response/'/api/link[@rel="instances"]/feature').collect { |f|
f[:name] }
+      features.include?('user_name').should == true
+      features.include?('authentication_password').should == true
+      features.include?('user_files').should == true
+      features.length.should == 3
+    end
+
+    def test_03_it_has_rackspace_collections
+      do_xml_request '/api;driver=rackspace'
+      collections = (last_xml_response/'/api/link').collect { |f| f[:rel] }
+      collections.include?('instance_states').should == true
+      collections.include?('instances').should == true
+      collections.include?('images').should == true
+      collections.include?('buckets').should == true
+      collections.include?('realms').should == true
+      collections.include?('hardware_profiles').should == true
+      collections.length.should == 6
+    end
+
+  end
+end
diff --git a/server/tests/drivers/rackspace/hardware_profiles_test.rb b/server/tests/drivers/rackspace/hardware_profiles_test.rb
new file mode 100644
index 0000000..90720a4
--- /dev/null
+++ b/server/tests/drivers/rackspace/hardware_profiles_test.rb
@@ -0,0 +1,53 @@
+$:.unshift File.join(File.dirname(__FILE__), '..', '..', '..')
+require 'tests/common'
+
+module RackspaceTest
+
+  class HardwareProfilesTest < Test::Unit::TestCase
+    include Rack::Test::Methods
+
+    def app
+      Sinatra::Application
+    end
+
+    def test_01_it_returns_hardware_profiles
+      do_xml_request '/api;driver=rackspace/hardware_profiles', {}, true
+      (last_xml_response/'hardware_profiles/hardware_profile').length.should == 7
+    end
+
+    def test_02_each_hardware_profile_has_a_name
+      do_xml_request '/api;driver=rackspace/hardware_profiles', {}, true
+      (last_xml_response/'hardware_profiles/hardware_profile').each do |profile|
+        (profile/'name').text.should_not == nil
+        (profile/'name').text.should_not == ''
+      end
+    end
+
+    def test_03_each_hardware_profile_has_correct_properties
+      do_xml_request '/api;driver=rackspace/hardware_profiles', {}, true
+      (last_xml_response/'hardware_profiles/hardware_profile').each do |profile|
+        (profile/'property[@name="architecture"]').first[:value].should == 'x86_64'
+        (profile/'property[@name="memory"]').first[:unit].should == 'MB'
+        (profile/'property[@name="memory"]').first[:kind].should == 'fixed'
+        (profile/'property[@name="storage"]').first[:unit].should == 'GB'
+        (profile/'property[@name="storage"]').first[:kind].should == 'fixed'
+      end
+    end
+
+    def test_04_it_returns_single_hardware_profile
+      do_xml_request '/api;driver=rackspace/hardware_profiles/1', {}, true
+      (last_xml_response/'hardware_profile/name').first.text.should == '1'
+      (last_xml_response/'hardware_profile/property[@name="architecture"]').first[:value].should
== 'x86_64'
+      (last_xml_response/'hardware_profile/property[@name="memory"]').first[:value].should
== '256'
+      (last_xml_response/'hardware_profile/property[@name="storage"]').first[:value].should
== '10'
+    end
+
+    def test_05_it_filter_hardware_profiles
+      do_xml_request '/api;driver=rackspace/hardware_profiles?architecture=i386', {}, true
+      (last_xml_response/'hardware_profiles/hardware_profile').length.should == 0
+      do_xml_request '/api;driver=rackspace/hardware_profiles?architecture=x86_64', {}, true
+      (last_xml_response/'hardware_profiles/hardware_profile').length.should == 7
+    end
+
+  end
+end
diff --git a/server/tests/drivers/rackspace/images_test.rb b/server/tests/drivers/rackspace/images_test.rb
new file mode 100644
index 0000000..66fdc0a
--- /dev/null
+++ b/server/tests/drivers/rackspace/images_test.rb
@@ -0,0 +1,40 @@
+$:.unshift File.join(File.dirname(__FILE__), '..', '..', '..')
+require 'tests/common'
+
+module RackspaceTest
+
+  class ImagesTest < Test::Unit::TestCase
+    include Rack::Test::Methods
+
+    def app
+      Sinatra::Application
+    end
+
+    def test_01_it_returns_images
+      do_xml_request '/api;driver=rackspace/images', {}, true
+      (last_xml_response/'images/image').length.should > 0
+    end
+
+    def test_02_each_image_has_correct_properties
+      do_xml_request '/api;driver=rackspace/images', {}, true
+      (last_xml_response/'images/image').each do |image|
+        (image/'name').should_not == nil
+        (image/'name').should_not == ''
+        (image/'description').should_not == nil
+        (image/'description').should_not == ''
+        (image/'architecture').should_not == nil
+        (image/'architecture').should_not == ''
+        (image/'state').text.should == 'ACTIVE'
+        (image/'owner_id').text.should == ENV['API_USER']
+        (image/'actions/link').length.should == 1
+        (image/'actions/link').first[:rel].should == 'create_instance'
+      end
+    end
+
+    def test_03_it_returns_single_image
+      do_xml_request '/api;driver=rackspace/images/71', {}, true
+      (last_xml_response/'image').length.should == 1
+    end
+
+  end
+end
diff --git a/server/tests/drivers/rackspace/instances_test.rb b/server/tests/drivers/rackspace/instances_test.rb
new file mode 100644
index 0000000..fc1af4f
--- /dev/null
+++ b/server/tests/drivers/rackspace/instances_test.rb
@@ -0,0 +1,167 @@
+$:.unshift File.join(File.dirname(__FILE__), '..', '..', '..')
+require 'tests/common'
+
+module RackspaceTest
+
+  class InstancesTest < Test::Unit::TestCase
+    include Rack::Test::Methods
+
+    def app
+      Sinatra::Application
+    end
+
+    def test_01_01_it_can_create_instance_without_hardware_profile
+      params = {
+        :image_id => '71',
+        :'api[driver]' => 'rackspace',
+      }
+      header 'Accept', accept_header(:xml)
+      post '/api/instances', params, authenticate
+      last_response.status.should == 201 # Created
+      @@instance = Nokogiri::XML(last_response.body)
+      (@@instance/'instance').length.should > 0
+      (@@instance/'instance/name').first.text.should_not == nil
+      (@@instance/'instance/name').first.text.should_not == nil
+      (@@instance/'instance/owner_id').first.text.should_not == ''
+      (@@instance/'instance/owner_id').first.text.should == ENV['API_USER']
+      (@@instance/'instance/state').first.text.should == 'PENDING'
+    end
+
+    def test_01_02_it_can_create_instance_with_hardware_profile
+      params = {
+        :image_id => '71',
+        :hwp_id => '2',
+        :'api[driver]' => 'rackspace',
+      }
+      header 'Accept', accept_header(:xml)
+      post '/api/instances', params, authenticate
+      last_response.status.should == 201 # Created
+      @@instance2 = Nokogiri::XML(last_response.body)
+      (@@instance2/'instance').length.should > 0
+      (@@instance2/'instance/name').first.text.should_not == nil
+      (@@instance2/'instance/name').first.text.should_not == nil
+      (@@instance2/'instance/owner_id').first.text.should_not == ''
+      (@@instance2/'instance/owner_id').first.text.should == ENV['API_USER']
+      (@@instance2/'instance/state').first.text.should == 'PENDING'
+    end
+
+    def test_02_01_created_instance_has_correct_authentication
+      (@@instance/'instance/authentication').first.should_not == nil
+      (@@instance/'instance/authentication').first[:type].should == 'password'
+      (@@instance/'instance/authentication/login/username').first.text.should == 'root'
+      (@@instance/'instance/authentication/login/password').first.text.should_not == nil
+      (@@instance/'instance/authentication/login/password').first.text.should_not == ''
+    end
+
+    def test_02_02_created_instance_has_correct_authentication
+      (@@instance2/'instance/authentication').first.should_not == nil
+      (@@instance2/'instance/authentication').first[:type].should == 'password'
+      (@@instance2/'instance/authentication/login/username').first.text.should == 'root'
+      (@@instance2/'instance/authentication/login/password').first.text.should_not == nil
+      (@@instance2/'instance/authentication/login/password').first.text.should_not == ''
+    end
+
+    def test_03_01_created_instance_has_correct_addresses
+      (@@instance/'instance/public_addresses/address').length.should > 0
+      (@@instance/'instance/public_addresses/address').first.text.should_not == nil
+      (@@instance/'instance/public_addresses/address').first.text.should_not == ''
+    end
+
+    def test_03_02_created_instance_has_correct_addresses
+      (@@instance2/'instance/public_addresses/address').length.should > 0
+      (@@instance2/'instance/public_addresses/address').first.text.should_not == nil
+      (@@instance2/'instance/public_addresses/address').first.text.should_not == ''
+    end
+
+    def test_03_02_created_instance_has_correct_hardware_profile
+      (@@instance2/'instance/hardware_profile').length.should == 1
+      (@@instance2/'instance/hardware_profile').first[:id].should == "2"
+      (@@instance2/'instance/hardware_profile').first[:href].should_not == nil
+    end
+
+    def test_04_01_created_instance_goes_to_running_state
+      20.times do
+        do_xml_request "/api;driver=rackspace/instances/#{(@@instance/'instance').first[:id]}",
{}, true
+        last_response.status.should_not == 500
+        state = (last_xml_response/'instance/state').first.text
+        break if state=='RUNNING'
+        sleep(5)
+      end
+      @@instance = last_xml_response
+      do_xml_request "/api;driver=rackspace/instances/#{(@@instance/'instance').first[:id]}",
{}, true
+      last_response.status.should_not == 500
+      (last_xml_response/'instance/state').first.text.should == 'RUNNING'
+      (last_xml_response/'instance/actions/link[@rel="reboot"]').first.should_not == nil
+      (last_xml_response/'instance/actions/link[@rel="stop"]').first.should_not == nil
+      (last_xml_response/'instance/actions/link[@rel="create_image"]').first.should_not ==
nil
+      (last_xml_response/'instance/actions/link[@rel="run"]').first.should_not == nil
+    end
+
+    def test_04_02_created_instance_goes_to_running_state
+      20.times do
+        do_xml_request "/api;driver=rackspace/instances/#{(@@instance2/'instance').first[:id]}",
{}, true
+        last_response.status.should_not == 500
+        state = (last_xml_response/'instance/state').first.text
+        break if state=='RUNNING'
+        sleep(5)
+      end
+      @@instance2 = last_xml_response
+      do_xml_request "/api;driver=rackspace/instances/#{(@@instance2/'instance').first[:id]}",
{}, true
+      last_response.status.should_not == 500
+      (last_xml_response/'instance/state').first.text.should == 'RUNNING'
+      (last_xml_response/'instance/actions/link[@rel="reboot"]').first.should_not == nil
+      (last_xml_response/'instance/actions/link[@rel="stop"]').first.should_not == nil
+      (last_xml_response/'instance/actions/link[@rel="create_image"]').first.should_not ==
nil
+      (last_xml_response/'instance/actions/link[@rel="run"]').first.should_not == nil
+    end
+
+    def test_05_created_instance_can_be_rebooted
+      params = {
+        :'api[driver]' => 'rackspace',
+      }
+      header 'Accept', accept_header(:xml)
+      post "/api/instances/#{(@@instance/'instance').first[:id]}/reboot", params, authenticate
+      last_response.status.should == 200
+      20.times do
+        do_xml_request "/api;driver=rackspace/instances/#{(@@instance/'instance').first[:id]}",
{}, true
+        last_response.status.should_not == 500
+        state = (last_xml_response/'instance/state').first.text
+        break if state=='RUNNING'
+        sleep(5)
+      end
+    end
+
+    def test_06_created_instance_can_be_destroyed
+      params = {
+        :'api[driver]' => 'rackspace',
+      }
+      header 'Accept', accept_header(:xml)
+      post "/api/instances/#{(@@instance/'instance').first[:id]}/stop", params, authenticate
+      last_response.status.should == 200
+      20.times do
+        do_xml_request "/api;driver=rackspace/instances/#{(@@instance/'instance').first[:id]}",
{}, true
+        last_response.status.should_not == 500
+        break if last_response.status == 404
+        sleep(5)
+      end
+      last_response.status.should == 404
+    end
+
+    def test_06_02_created_instance_can_be_destroyed
+      params = {
+        :'api[driver]' => 'rackspace',
+      }
+      header 'Accept', accept_header(:xml)
+      post "/api/instances/#{(@@instance2/'instance').first[:id]}/stop", params, authenticate
+      last_response.status.should == 200
+      20.times do
+        do_xml_request "/api;driver=rackspace/instances/#{(@@instance2/'instance').first[:id]}",
{}, true
+        last_response.status.should_not == 500
+        break if last_response.status == 404
+        sleep(5)
+      end
+      last_response.status.should == 404
+    end
+
+  end
+end
diff --git a/server/tests/drivers/rackspace/realms_test.rb b/server/tests/drivers/rackspace/realms_test.rb
new file mode 100644
index 0000000..ca44d15
--- /dev/null
+++ b/server/tests/drivers/rackspace/realms_test.rb
@@ -0,0 +1,36 @@
+$:.unshift File.join(File.dirname(__FILE__), '..', '..', '..')
+require 'tests/common'
+
+module RackspaceTest
+
+  class RealmsTest < Test::Unit::TestCase
+    include Rack::Test::Methods
+
+    def app
+      Sinatra::Application
+    end
+
+    def test_01_it_returns_realms
+      do_xml_request '/api;driver=rackspace/realms', {}, true
+      (last_xml_response/'realms/realm').length.should == 1
+    end
+
+    def test_02_each_realm_has_a_name
+      do_xml_request '/api;driver=rackspace/realms', {}, true
+      (last_xml_response/'realms/realm').each do |profile|
+        (profile/'name').text.should_not == nil
+        (profile/'name').text.should_not == ''
+        (profile/'name').text.should == 'United States'
+      end
+    end
+
+    def test_03_it_returns_single_realm
+      do_xml_request '/api;driver=rackspace/realms/us', {}, true
+      (last_xml_response/'realm').first[:id].should == 'us'
+      (last_xml_response/'realm/name').first.text.should == 'United States'
+      (last_xml_response/'realm/state').first.text.should == 'AVAILABLE'
+      (last_xml_response/'realm/limit').first.text.should == ''
+    end
+
+  end
+end
diff --git a/server/tests/drivers/rackspace/setup.rb b/server/tests/drivers/rackspace/setup.rb
new file mode 100644
index 0000000..723ed7a
--- /dev/null
+++ b/server/tests/drivers/rackspace/setup.rb
@@ -0,0 +1,3 @@
+ENV['API_DRIVER']   = "rackspace"
+ENV['API_USER']     = '<RACKSPACE_USERNAME>'
+ENV['API_PASSWORD'] = '<RACKSPACE_API_KEY>'
-- 
1.7.4.1


Mime
View raw message