You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltacloud.apache.org by mf...@apache.org on 2010/07/14 10:07:11 UTC
svn commit: r963965 - in /incubator/deltacloud/trunk/server: ./ tests/
Author: mfojtik
Date: Wed Jul 14 08:07:10 2010
New Revision: 963965
URL: http://svn.apache.org/viewvc?rev=963965&view=rev
Log:
Refreshed and fixed Test::Unit tests
Added:
incubator/deltacloud/trunk/server/tests/api_test.rb
incubator/deltacloud/trunk/server/tests/common.rb
incubator/deltacloud/trunk/server/tests/hardware_profiles_test.rb
incubator/deltacloud/trunk/server/tests/instance_states_test.rb
Removed:
incubator/deltacloud/trunk/server/tests/deltacloud_test.rb
incubator/deltacloud/trunk/server/tests/storage_snapshots_test.rb
incubator/deltacloud/trunk/server/tests/storage_volumes_test.rb
Modified:
incubator/deltacloud/trunk/server/Rakefile
incubator/deltacloud/trunk/server/tests/images_test.rb
incubator/deltacloud/trunk/server/tests/instances_test.rb
incubator/deltacloud/trunk/server/tests/realms_test.rb
Modified: incubator/deltacloud/trunk/server/Rakefile
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/Rakefile?rev=963965&r1=963964&r2=963965&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/Rakefile (original)
+++ incubator/deltacloud/trunk/server/Rakefile Wed Jul 14 08:07:10 2010
@@ -26,13 +26,14 @@ require 'rake/gempackagetask'
desc "Run basic unit tests"
Rake::TestTask.new("test") { |t|
t.test_files = FileList[
+ 'tests/api_test.rb',
+ 'tests/hardware_profiles_test.rb',
'tests/realms_test.rb',
'tests/images_test.rb',
'tests/instances_test.rb',
- 'tests/storage_volumes_test.rb',
- 'tests/storage_snapshots_test.rb',
+ 'tests/instance_states_test.rb',
]
- t.verbose = false
+ t.verbose = true
t.warning = false
}
Added: incubator/deltacloud/trunk/server/tests/api_test.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/tests/api_test.rb?rev=963965&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/tests/api_test.rb (added)
+++ incubator/deltacloud/trunk/server/tests/api_test.rb Wed Jul 14 08:07:10 2010
@@ -0,0 +1,37 @@
+require 'tests/common'
+
+module DeltacloudUnitTest
+ class ApiTest < Test::Unit::TestCase
+ include Rack::Test::Methods
+
+ def app
+ Sinatra::Application
+ end
+
+ def test_it_returns_entry_points
+ do_xml_request '/api'
+ (last_xml_response/'/api/link').map.size.should > 0
+ end
+
+ def test_it_has_correct_attributes_set
+ do_xml_request '/api'
+ (last_xml_response/'/api/link').each do |link|
+ link.attributes.keys.sort.should == [ 'href', 'rel' ]
+ end
+ end
+
+ def test_it_responses_to_html
+ do_request '/api', {}, false, { :format => :html }
+ last_response.status.should == 200
+ Nokogiri::HTML(last_response.body).search('html').first.name.should == 'html'
+ end
+
+ def test_it_responses_to_json
+ do_request '/api', {}, false, { :format => :json }
+ last_response.status.should == 200
+ JSON::parse(last_response.body).class.should == Hash
+ JSON::parse(last_response.body)['api'].class.should == Hash
+ end
+
+ end
+end
Added: incubator/deltacloud/trunk/server/tests/common.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/tests/common.rb?rev=963965&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/tests/common.rb (added)
+++ incubator/deltacloud/trunk/server/tests/common.rb Wed Jul 14 08:07:10 2010
@@ -0,0 +1,90 @@
+require 'rubygems'
+require 'base64'
+require 'test/unit'
+require 'spec'
+require 'nokogiri'
+require 'json'
+require 'ap'
+
+# 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'
+
+set :environment => :test
+set :loggining => true
+set :raise_errors => false
+set :show_exceptions => false
+
+require 'rack/test'
+
+Spec::Runner.configure do |conf|
+ conf.include Rack::Test::Methods
+end
+
+module DeltacloudTestCommon
+
+ def auth_hash(credentials)
+ "Basic " + Base64.encode64("#{credentials[:user]}:#{credentials[:password]}")
+ end
+
+ def authenticate(opts={ :format => :xml })
+ credentials = opts[:credentials] || { :user => ENV['API_USER'], :password => ENV['API_PASSWORD']}
+ return {
+ 'HTTP_AUTHORIZATION' => auth_hash(credentials),
+ }.merge(accept_header(opts[:format]))
+ end
+
+ def default_headers
+ { 'SERVER_PORT' => ENV['API_PORT'] }
+ end
+
+ def accept_header(format=:xml)
+ case format
+ when :json then { 'Accept' => 'application/json' }
+ when :xml then { 'Accept' => 'application/xml;q=1' }
+ else { 'Accept' => 'application/xhtml+xml;text/html' }
+ end.merge(default_headers)
+ end
+
+ def create_url(url, format = :xml)
+ "#{url}.#{format.to_s}"
+ end
+
+ def do_request(uri, params=nil, authentication=false, opts={ :format => :xml })
+ get create_url(uri, opts[:format]), params || {}, (authentication) ? authenticate(opts) : {}
+ end
+
+ def do_xml_request(uri, params=nil, authentication=false)
+ get create_url(uri), params || {}, (authentication) ? authenticate : {}
+ puts "[401] Authentication required to get #{uri}" if last_response.status == 401
+ if last_response.status == 200
+ @xml_response = false
+ @xml_response = Nokogiri::XML(last_response.body)
+ end
+ end
+
+ def require_authentication?(uri)
+ get uri, {}
+ true if last_response.status.eql?(401)
+ end
+
+ def last_xml_response
+ @xml_response || Nokogiri::XML::Document.new
+ end
+
+ def add_created_instance(id)
+ $created_instances ||= []
+ $created_instances << id
+ end
+
+end
+
+include DeltacloudTestCommon
+
Added: incubator/deltacloud/trunk/server/tests/hardware_profiles_test.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/tests/hardware_profiles_test.rb?rev=963965&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/tests/hardware_profiles_test.rb (added)
+++ incubator/deltacloud/trunk/server/tests/hardware_profiles_test.rb Wed Jul 14 08:07:10 2010
@@ -0,0 +1,120 @@
+require 'tests/common'
+
+module DeltacloudUnitTest
+ class HardwareProfilesTest < Test::Unit::TestCase
+ include Rack::Test::Methods
+
+ def app
+ Sinatra::Application
+ end
+
+ def test_it_returns_hardware_profiles
+ do_xml_request '/api/hardware_profiles'
+ (last_xml_response/'hardware_profiles/hardware_profile').map.size.should > 0
+ end
+
+ def test_it_has_correct_attributes_set
+ do_xml_request '/api/hardware_profiles'
+ (last_xml_response/'hardware_profiles/hardware_profile').each do |profile|
+ profile.attributes.keys.sort.should == [ 'href', 'id' ]
+ end
+ end
+
+ def test_hardware_profiles_have_name
+ do_xml_request '/api/hardware_profiles'
+ (last_xml_response/'hardware_profiles/hardware_profile').each do |profile|
+ (profile/'name').text.should_not == nil
+ end
+ end
+
+ def test_hardware_profiles_have_unique_name
+ do_xml_request '/api/hardware_profiles'
+ names = []
+ (last_xml_response/'hardware_profiles/hardware_profile').each do |profile|
+ names << (profile/'name').text
+ end
+ names.should == names.uniq
+ end
+
+ def test_hardware_profiles_have_unique_id
+ do_xml_request '/api/hardware_profiles'
+ ids = []
+ (last_xml_response/'hardware_profiles/hardware_profile').each do |profile|
+ ids << profile['id']
+ end
+ ids.should == ids.uniq
+ end
+
+ def test_m1_xlarge_profile_has_correct_attributes
+ do_xml_request '/api/hardware_profiles'
+ profile = (last_xml_response/'hardware_profiles/hardware_profile[@id="m1-xlarge"]')
+ test_profile_properties(profile)
+ end
+
+ def test_it_returns_valid_hardware_profile
+ do_xml_request '/api/hardware_profiles/m1-xlarge'
+ profile = (last_xml_response/'hardware_profile')
+ test_profile_properties(profile)
+ end
+
+ def test_it_responses_to_json
+ do_request '/api/hardware_profiles', {}, false, { :format => :json }
+ JSON::parse(last_response.body).class.should == Hash
+ JSON::parse(last_response.body)['hardware_profiles'].class.should == Array
+
+ do_request '/api/hardware_profiles/m1-xlarge', {}, false, { :format => :json }
+ last_response.status.should == 200
+ JSON::parse(last_response.body).class.should == Hash
+ JSON::parse(last_response.body)['hardware_profile'].class.should == Hash
+ end
+
+ def test_it_responses_to_html
+ do_request '/api/hardware_profiles', {}, false, { :format => :html }
+ last_response.status.should == 200
+ Nokogiri::HTML(last_response.body).search('html').first.name.should == 'html'
+
+ do_request '/api/hardware_profiles/m1-xlarge', {}, false, { :format => :html }
+ last_response.status.should == 200
+ Nokogiri::HTML(last_response.body).search('html').first.name.should == 'html'
+ end
+
+ def test_it_returns_error_on_wrong_name
+ do_request '/api/hardware_profiles/m1-unknown-wrongname', {}, false, { :format => :html }
+ last_response.status.should == 404
+ do_xml_request '/api/hardware_profiles/m1-unknown-wrongname'
+ last_response.status.should == 404
+ do_request '/api/hardware_profiles/m1-unknown-wrongname', {}, false, { :format => :json }
+ last_response.status.should == 404
+ end
+
+ private
+
+ def test_profile_properties(profile)
+
+ (profile/'property').each do |properties|
+ properties.attributes.keys.sort.should == [ 'kind', 'name', 'unit', 'value' ]
+ end
+
+ (profile/'property[@name="architecture"]').first['kind'].should == 'fixed'
+ (profile/'property[@name="architecture"]').first['unit'].should == 'label'
+
+ (profile/'property[@name="memory"]').first['kind'].should == 'range'
+ (profile/'property[@name="memory"]').first['unit'].should == 'MB'
+ (profile/'property[@name="memory"]/range').size.should == 1
+ (profile/'property[@name="memory"]/range').first.attributes.keys.sort.should == [ 'first', 'last' ]
+
+ (profile/'property[@name="cpu"]').first['kind'].should == 'fixed'
+ (profile/'property[@name="cpu"]').first['unit'].should == 'count'
+
+ (profile/'property[@name="storage"]').first['kind'].should == 'enum'
+ (profile/'property[@name="storage"]').first['unit'].should == 'GB'
+ (profile/'property[@name="storage"]/enum').size.should == 1
+ (profile/'property[@name="storage"]/enum/entry').map.size.should == 3
+ (profile/'property[@name="storage"]/enum/entry').each do |entry|
+ entry.attributes.keys.should == [ 'value' ]
+ entry['value'].should_not == nil
+ end
+ end
+
+ end
+end
Modified: incubator/deltacloud/trunk/server/tests/images_test.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/tests/images_test.rb?rev=963965&r1=963964&r2=963965&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/tests/images_test.rb (original)
+++ incubator/deltacloud/trunk/server/tests/images_test.rb Wed Jul 14 08:07:10 2010
@@ -1,94 +1,111 @@
-require 'tests/deltacloud_test'
+require 'tests/common'
-class ImagesTest < Test::Unit::TestCase
+module DeltacloudUnitTest
+ class HardwareProfilesTest < Test::Unit::TestCase
+ include Rack::Test::Methods
- def initialize(*args)
- @collection = 'images'
- @operations = [:index, :show]
- @params = {}
- super(*args)
- end
+ def app
+ Sinatra::Application
+ end
- def test_if_images_are_not_empty
- get '/api/images.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- assert_not_equal 0, doc.xpath('/images/image').size
- end
+ def test_it_require_authentication
+ require_authentication?('/api/images').should == true
+ end
- [:id, :owner_id, :name, :description, :architecture].each do |option|
- method_name = :"test_if_images_index_contain_#{option}"
- send :define_method, method_name do
- get '/api/images.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- elt = doc.xpath('/images/image[1]').first
- assert_not_nil elt.xpath(option.to_s).first
+ def test_it_returns_images
+ do_xml_request '/api/images', {}, true
+ (last_xml_response/'images/image').map.size.should > 0
end
- end
- [:id, :owner_id, :name, :description, :architecture].each do |option|
- method_name = :"test_if_image_show_contain_#{option}"
- send :define_method, method_name do
- get '/api/images/img1.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- elt = doc.xpath('/image').first
- assert_not_nil elt.xpath(option.to_s).first
+ def test_it_has_correct_attributes_set
+ do_xml_request '/api/images', {}, true
+ (last_xml_response/'images/image').each do |image|
+ image.attributes.keys.sort.should == [ 'href', 'id' ]
+ end
end
- end
- def test_images_filtering_by_id
- @params={ :id => 'img1' }
- get '/api/images.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- assert_equal 1, doc.xpath('/images/image').size
- assert_equal @params[:id], doc.xpath('/images/image/id').first.text
- end
+ def test_img1_has_correct_attributes
+ do_xml_request '/api/images', {}, true
+ image = (last_xml_response/'images/image[@id="img1"]')
+ test_image_attributes(image)
+ end
- def test_images_filtering_by_owner_id
- @params={ :owner_id => 'fedoraproject' }
- get '/api/images.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- assert_equal 2, doc.xpath('/images/image').size
- assert_equal @params[:owner_id], doc.xpath('/images/image/owner_id')[0].text
- assert_equal @params[:owner_id], doc.xpath('/images/image/owner_id')[1].text
- end
+ def test_it_returns_valid_image
+ do_xml_request '/api/images/img1', {}, true
+ image = (last_xml_response/'image')
+ test_image_attributes(image)
+ end
- def test_images_filtering_by_architecture
- @params={ :architecture => 'i386' }
- get '/api/images.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- assert_equal 2, doc.xpath('/images/image').size
- assert_equal @params[:architecture], doc.xpath('/images/image/architecture')[0].text
- assert_equal @params[:architecture], doc.xpath('/images/image/architecture')[1].text
- end
+ def test_it_has_unique_ids
+ do_xml_request '/api/images', {}, true
+ ids = []
+ (last_xml_response/'images/image').each do |image|
+ ids << image['id'].to_s
+ end
+ ids.sort.should == ids.sort.uniq
+ end
- def test_images_filtering_by_id_and_owner_id
- @params={ :id => 'img1', :owner_id => 'fedoraproject' }
- get '/api/images.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- assert_equal 1, doc.xpath('/images/image').size
- assert_equal @params[:owner_id], doc.xpath('/images/image/owner_id')[0].text
- assert_equal @params[:id], doc.xpath('/images/image/id')[0].text
- end
+ def test_it_has_valid_urls
+ do_xml_request '/api/images', {}, true
+ ids = []
+ images = (last_xml_response/'images/image')
+ images.each do |image|
+ do_xml_request image['href'].to_s, {}, true
+ (last_xml_response/'image').first['href'].should == image['href'].to_s
+ end
+ end
- def test_images_filtering_by_id_and_owner_id_and_architecture
- @params={ :id => 'img1', :owner_id => 'fedoraproject', :architecture => 'x86_64' }
- get '/api/images.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- assert_equal 1, doc.xpath('/images/image').size
- assert_equal @params[:owner_id], doc.xpath('/images/image/owner_id')[0].text
- assert_equal @params[:id], doc.xpath('/images/image/id')[0].text
- assert_equal @params[:architecture], doc.xpath('/images/image/architecture')[0].text
- end
+ def test_it_can_filter_using_owner_id
+ do_xml_request '/api/images', { :owner_id => 'mockuser' }, true
+ (last_xml_response/'images/image').size.should == 1
+ (last_xml_response/'images/image/owner_id').first.text.should == 'mockuser'
+ end
- def test_images_filtering_by_id_and_architecture
- @params={ :id => 'img1', :architecture => 'x86_64' }
- get '/api/images.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- assert_equal 1, doc.xpath('/images/image').size
- assert_equal @params[:id], doc.xpath('/images/image/id')[0].text
- assert_equal @params[:architecture], doc.xpath('/images/image/architecture')[0].text
- end
+ def test_it_can_filter_using_unknown_owner_id
+ do_xml_request '/api/images', { :architecture => 'unknown_user' }, true
+ (last_xml_response/'images/image').size.should == 0
+ end
+
+ def test_it_can_filter_using_architecture
+ do_xml_request '/api/images', { :architecture => 'x86_64' }, true
+ (last_xml_response/'images/image').size.should == 1
+ (last_xml_response/'images/image/architecture').first.text.should == 'x86_64'
+ end
+
+ def test_it_can_filter_using_unknown_architecture
+ do_xml_request '/api/images', { :architecture => 'unknown_arch' }, true
+ (last_xml_response/'images/image').size.should == 0
+ end
- include DeltacloudTest
+ def test_it_responses_to_json
+ do_request '/api/images', {}, true, { :format => :json }
+ JSON::parse(last_response.body).class.should == Hash
+ JSON::parse(last_response.body)['images'].class.should == Array
+
+ do_request '/api/images/img1', {}, true, { :format => :json }
+ last_response.status.should == 200
+ JSON::parse(last_response.body).class.should == Hash
+ JSON::parse(last_response.body)['image'].class.should == Hash
+ end
+
+ def test_it_responses_to_html
+ do_request '/api/images', {}, true, { :format => :html }
+ last_response.status.should == 200
+ Nokogiri::HTML(last_response.body).search('html').first.name.should == 'html'
+
+ do_request '/api/images/img1', {}, true, { :format => :html }
+ last_response.status.should == 200
+ Nokogiri::HTML(last_response.body).search('html').first.name.should == 'html'
+ end
+
+ private
+ def test_image_attributes(image)
+ (image/'name').text.should_not nil
+ (image/'owner_id').text.should_not nil
+ (image/'description').text.should_not nil
+ (image/'architecture').text.should_not nil
+ end
+
+ end
end
Added: incubator/deltacloud/trunk/server/tests/instance_states_test.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/tests/instance_states_test.rb?rev=963965&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/tests/instance_states_test.rb (added)
+++ incubator/deltacloud/trunk/server/tests/instance_states_test.rb Wed Jul 14 08:07:10 2010
@@ -0,0 +1,52 @@
+require 'tests/common'
+
+module DeltacloudUnitTest
+ class RealmsTest < Test::Unit::TestCase
+ include Rack::Test::Methods
+
+ def app
+ Sinatra::Application
+ end
+
+ def test_it_not_require_authentication
+ require_authentication?('/api/realms').should_not == true
+ end
+
+ def test_it_returns_instance_states
+ do_xml_request '/api/instance_states', {}, true
+ (last_xml_response/'states/state').map.size.should > 0
+ end
+
+ def test_each_state_has_transition
+ do_xml_request '/api/instance_states', {}, true
+ (last_xml_response/'states/state').each do |state|
+ next if state['name'].eql?('finish') # Finnish state doesn't have transitions
+ (state/'transition').map.size.should > 0
+ (state/'transition').each do |transition|
+ transition['to'].should_not == nil
+ end
+ end
+ end
+
+ def test_it_responses_to_json
+ do_request '/api/instance_states', {}, false, { :format => :json }
+ JSON::parse(last_response.body).class.should == Array
+ JSON::parse(last_response.body).first['transitions'].class.should == Array
+ JSON::parse(last_response.body).first['name'].should == 'start'
+ end
+
+ def test_it_responses_to_html
+ do_request '/api/instance_states', {}, false, { :format => :html }
+ last_response.status.should == 200
+ Nokogiri::HTML(last_response.body).search('html').first.name.should == 'html'
+ end
+
+ def test_it_responses_to_png
+ do_request '/api/instance_states', {}, false, { :format => :png }
+ last_response.status.should == 200
+ last_response.headers['Content-Type'].should == 'image/png'
+ last_response.headers['Content-Length'].should == '4371'
+ end
+
+ end
+end
Modified: incubator/deltacloud/trunk/server/tests/instances_test.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/tests/instances_test.rb?rev=963965&r1=963964&r2=963965&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/tests/instances_test.rb (original)
+++ incubator/deltacloud/trunk/server/tests/instances_test.rb Wed Jul 14 08:07:10 2010
@@ -1,136 +1,219 @@
-require 'tests/deltacloud_test'
+require 'tests/common'
-class InstancesTest < Test::Unit::TestCase
+module DeltacloudUnitTest
+ class InstancesTest < Test::Unit::TestCase
+ include Rack::Test::Methods
- def initialize(*args)
- @collection = 'instances'
- @operations = [:index, :show]
- @options = [:id, :architecture, :memory, :storage]
- @params = {}
- self.temp_inst_id = 'inst2'
- super(*args)
- end
+ def app
+ Sinatra::Application
+ end
- attr_accessor :temp_inst_id
+ def test_it_require_authentication
+ require_authentication?('/api/instances').should == true
+ end
- def test_if_instances_are_not_empty
- get '/api/instances.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- assert_not_equal 0, doc.xpath('/instances/instance').size
- end
+ def test_it_returns_instances
+ do_xml_request '/api/instances', {}, true
+ (last_xml_response/'instances/instance').map.size.should > 0
+ end
- [:id, :name, :owner_id, :image, :realm, :state, :actions, :'public-addresses', :'private-addresses'].each do |option|
- method_name = :"test_if_instances_index_contain_#{option}"
- send :define_method, method_name do
- get '/api/instances.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- instance = doc.xpath('/instances/instance[1]').first
- assert_not_nil instance.xpath(option.to_s).first
+ def test_it_has_correct_attributes_set
+ do_xml_request '/api/images', {}, true
+ (last_xml_response/'images/image').each do |image|
+ image.attributes.keys.sort.should == [ 'href', 'id' ]
+ end
end
- end
- [:id, :name, :owner_id, :image, :realm, :state, :actions, :'public-addresses', :'private-addresses'].each do |option|
- method_name = :"test_if_instance_show_contain_#{option}"
- send :define_method, method_name do
- get '/api/instances/inst1.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- instance = doc.xpath('/instance').first
- assert_not_nil instance.xpath(option.to_s).first
+ def test_it_has_unique_ids
+ do_xml_request '/api/instances', {}, true
+ ids = []
+ (last_xml_response/'instances/instance').each do |image|
+ ids << image['id'].to_s
+ end
+ ids.sort.should == ids.sort.uniq
end
- end
- def test_instances_filtering_by_id
- get '/api/instances.xml', { :id => 'inst1'}, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- assert_equal 1, doc.xpath('/instances/instance').size
- assert_equal 'inst1', doc.xpath('/instances/instance/id').first.text
- end
+ def test_inst1_has_correct_attributes
+ do_xml_request '/api/instances', {}, true
+ instance = (last_xml_response/'instances/instance[@id="inst2"]')
+ test_instance_attributes(instance)
+ end
- def test_instances_filtering_by_state
- get '/api/instances.xml', { :state => 'RUNNING'}, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- doc.xpath('/instances/instance').each do |instance|
- assert_equal 'RUNNING', instance.xpath('state').first.text
+ def test_it_returns_valid_realm
+ do_xml_request '/api/instances/inst1', {}, true
+ instance = (last_xml_response/'instance')
+ test_instance_attributes(instance)
end
- end
- def test_instances_filtering_by_unknown_state
- get '/api/instances.xml', { :state => '_TEST_UNKNOWN_STATE'}, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- assert_equal 0, doc.xpath('/instances/instance').size
- end
+ def test_it_responses_to_json
+ do_request '/api/instances', {}, true, { :format => :json }
+ JSON::parse(last_response.body).class.should == Hash
+ JSON::parse(last_response.body)['instances'].class.should == Array
+
+ do_request '/api/instances/inst2', {}, true, { :format => :json }
+ last_response.status.should == 200
+ JSON::parse(last_response.body).class.should == Hash
+ JSON::parse(last_response.body)['instance'].class.should == Hash
+ end
- def test_001_create_instance
- @params = {
- :name => '_test-instance',
- :image_id => 'img1'
- }
-
- post '/api/instances.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
-
- self.temp_inst_id = doc.xpath('/instance/id').text
-
- assert_equal @params[:name], doc.xpath('/instance/name').first.text
- image_href = doc.xpath('/instance/image').first[:href].to_s
- image_id = image_href.gsub(/.*\/(\w+)$/, '\1')
- assert_equal @params[:image_id], image_id
- end
+ def test_it_responses_to_html
+ do_request '/api/instances', {}, true, { :format => :html }
+ last_response.status.should == 200
+ Nokogiri::HTML(last_response.body).search('html').first.name.should == 'html'
+
+ do_request '/api/instances/inst1', {}, true, { :format => :html }
+ last_response.status.should == 200
+ Nokogiri::HTML(last_response.body).search('html').first.name.should == 'html'
+ end
- def test_create_instance_with_hwp_id
+ def test_it_create_a_new_instance_using_image_id
+ params = {
+ :image_id => 'img1'
+ }
+ post '/api/instances', params, authenticate(:format => :xml)
+ last_response.status.should == 302
+ last_response.headers['Location'].should_not == nil
+ do_xml_request last_response.headers['Location'], {}, true
+ (last_xml_response/'instance/name').should_not == nil
+ add_created_instance (last_xml_response/'instance').first['id']
+ test_instance_attributes(last_xml_response/'instance')
+ end
- @params = {
- :name => '_test-instance',
- :image_id => 'img1',
- :hwp_id => 'm1-xlarge'
- }
-
- post '/api/instances.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- hwp_href = doc.xpath('/instance/hardware_profile').first[:href].to_s
- hwp_id = hwp_href.gsub(/.*\/([\w\-]+)$/, '\1')
- assert_equal @params[:hwp_id], hwp_id
- end
+ def test_it_create_a_new_instance_using_image_id_and_name
+ params = {
+ :image_id => 'img1',
+ :name => "unit_test_instance1"
+ }
+ post '/api/instances', params, authenticate(:format => :xml)
+ last_response.status.should == 302
+ last_response.headers['Location'].should_not == nil
+ do_xml_request last_response.headers['Location'], {}, true
+ (last_xml_response/'instance/name').text.should == 'unit_test_instance1'
+ add_created_instance (last_xml_response/'instance').first['id']
+ test_instance_attributes(last_xml_response/'instance')
+ end
- def test_create_instance_with_realm_id
+ def test_it_create_a_new_instance_using_image_id_and_name_and_hwp
+ params = {
+ :image_id => 'img1',
+ :name => "unit_test_instance1",
+ :hwp_id => "m1-xlarge"
+ }
+ post '/api/instances', params, authenticate(:format => :xml)
+ last_response.status.should == 302
+ last_response.headers['Location'].should_not == nil
+ do_xml_request last_response.headers['Location'], {}, true
+ (last_xml_response/'instance/name').text.should == 'unit_test_instance1'
+ (last_xml_response/'instance/hardware_profile').first['id'].should == 'm1-xlarge'
+ add_created_instance (last_xml_response/'instance').first['id']
+ test_instance_attributes(last_xml_response/'instance')
+ end
- @params = {
- :name => '_test-instance',
- :image_id => 'img1',
- :realm_id => 'us'
- }
-
- post '/api/instances.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- realm_href = doc.xpath('/instance/realm').first[:href].to_s
- realm_id = realm_href.gsub(/.*\/([\w\-]+)$/, '\1')
- assert_equal @params[:realm_id], realm_id
- end
+ def test_it_z0_stop_and_start_instance
+ $created_instances.each do |instance_id|
+ do_xml_request "/api/instances/#{instance_id}", {}, true
+ stop_url = (last_xml_response/'actions/link[@rel="stop"]').first['href']
+ stop_url.should_not == nil
+ post create_url(stop_url), {}, authenticate(:format => :xml)
+ last_response.status.should == 200
+ instance = Nokogiri::XML(last_response.body)
+ test_instance_attributes(instance)
+ (instance/'state').text.should == 'STOPPED'
+ do_xml_request "/api/instances/#{instance_id}", {}, true
+ start_url = (last_xml_response/'actions/link[@rel="start"]').first['href']
+ start_url.should_not == nil
+ post create_url(start_url), {}, authenticate(:format => :xml)
+ last_response.status.should == 200
+ instance = Nokogiri::XML(last_response.body)
+ test_instance_attributes(instance)
+ (instance/'state').text.should == 'RUNNING'
+ end
+ end
- def test_002_stop_instance
- post '/api/instances/'+self.temp_inst_id+'/stop.xml', rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- assert_equal 'STOPPED', doc.xpath('/instance/state').first.text
- end
+ def test_z0_reboot_instance
+ $created_instances.each do |instance_id|
+ do_xml_request "/api/instances/#{instance_id}", {}, true
+ reboot_url = (last_xml_response/'actions/link[@rel="reboot"]').first['href']
+ reboot_url.should_not == nil
+ post create_url(reboot_url), {}, authenticate(:format => :xml)
+ last_response.status.should == 200
+ instance = Nokogiri::XML(last_response.body)
+ test_instance_attributes(instance)
+ (instance/'state').text.should == 'RUNNING'
+ end
+ end
- def test_003_start_instance
- post '/api/instances/'+self.temp_inst_id+'/start.xml', rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- assert_equal 'RUNNING', doc.xpath('/instance/state').first.text
- end
+ def test_z1_stop_created_instances
+ $created_instances.each do |instance_id|
+ do_xml_request "/api/instances/#{instance_id}", {}, true
+ stop_url = (last_xml_response/'actions/link[@rel="stop"]').first['href']
+ stop_url.should_not == nil
+ post create_url(stop_url), {}, authenticate(:format => :xml)
+ last_response.status.should == 200
+ instance = Nokogiri::XML(last_response.body)
+ test_instance_attributes(instance)
+ (instance/'state').text.should == 'STOPPED'
+ end
+ end
- def test_004_reboot_instance
- post '/api/instances/'+self.temp_inst_id+'/reboot.xml', rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- assert_equal 'RUNNING', doc.xpath('/instance/state').first.text
- end
+ def test_z2_destroy_created_instances
+ $created_instances.each do |instance_id|
+ do_xml_request "/api/instances/#{instance_id}", {}, true
+ destroy_url = (last_xml_response/'actions/link[@rel="destroy"]').first['href']
+ destroy_url.should_not == nil
+ delete create_url(destroy_url), {}, authenticate(:format => :xml)
+ last_response.status.should == 302
+ do_xml_request last_response.headers['Location'], {}, true
+ (last_xml_response/'instances').should_not == nil
+ do_xml_request "/api/instances/#{instance_id}", {}, true
+ last_response.status.should == 404
+ end
+ end
- def test_005_destroy_instance
- delete '/api/instances/'+self.temp_inst_id+'.xml', {}, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- assert last_response.ok?
- end
+ private
- include DeltacloudTest
+ def test_instance_attributes(instance)
+ (instance/'name').should_not == nil
+ (instance/'owner_id').should_not == nil
+ ['RUNNING', 'STOPPED'].include?((instance/'state').text).should == true
+
+ (instance/'public_addreses').should_not == nil
+ (instance/'public_addresses/address').map.size.should > 0
+ (instance/'public_addresses/address').first.text.should_not == ""
+
+ (instance/'private_addresses').should_not == nil
+ (instance/'private_addresses/address').map.size.should > 0
+ (instance/'private_addresses/address').first.text.should_not == ""
+
+ (instance/'actions/link').map.size.should > 0
+ (instance/'actions/link').each do |link|
+ link['href'].should_not == ""
+ link['rel'].should_not == ""
+ link['method'].should_not == ""
+ ['get', 'post', 'delete', 'put'].include?(link['method']).should == true
+ end
+
+ (instance/'image').size.should > 0
+ (instance/'image').first['href'].should_not == ""
+ (instance/'image').first['id'].should_not == ""
+ do_xml_request (instance/'image').first['href'], {}, true
+ (last_xml_response/'image').should_not == nil
+ (last_xml_response/'image').first['href'] == (instance/'image').first['href']
+
+ (instance/'realm').size.should > 0
+ (instance/'realm').first['href'].should_not == ""
+ (instance/'realm').first['id'].should_not == ""
+ do_xml_request (instance/'realm').first['href']
+ (last_xml_response/'realm').should_not == nil
+ (last_xml_response/'realm').first['href'] == (instance/'realm').first['href']
+
+ (instance/'hardware_profile').size.should > 0
+ (instance/'hardware_profile').first['href'].should_not == ""
+ (instance/'hardware_profile').first['id'].should_not == ""
+ do_xml_request (instance/'hardware_profile').first['href']
+ (last_xml_response/'hardware_profile').should_not == nil
+ (last_xml_response/'hardware_profile').first['href'] == (instance/'hardware_profile').first['href']
+ end
+ end
end
Modified: incubator/deltacloud/trunk/server/tests/realms_test.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/tests/realms_test.rb?rev=963965&r1=963964&r2=963965&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/tests/realms_test.rb (original)
+++ incubator/deltacloud/trunk/server/tests/realms_test.rb Wed Jul 14 08:07:10 2010
@@ -1,56 +1,78 @@
-require 'tests/deltacloud_test'
+require 'tests/common'
-class RealmsTest < Test::Unit::TestCase
+module DeltacloudUnitTest
+ class RealmsTest < Test::Unit::TestCase
+ include Rack::Test::Methods
- def initialize(*args)
- @collection = 'realms'
- @operations = [:index, :show]
- @options = [:id, :name, :state]
- @params = {}
- super(*args)
- end
+ def app
+ Sinatra::Application
+ end
- def test_if_realms_are_not_empty
- get '/api/realms.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- assert_not_equal 0, doc.xpath('/realms/realm').size
- end
+ def test_it_not_require_authentication
+ require_authentication?('/api/realms').should_not == true
+ end
- [:id, :name, :state].each do |option|
- method_name = :"test_if_realms_index_contain_#{option}"
- send :define_method, method_name do
- get '/api/realms.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- realm = doc.xpath('/realms/realm[1]').first
- assert_not_nil realm.xpath(option.to_s).first
+ def test_it_returns_realms
+ do_xml_request '/api/realms', {}, true
+ (last_xml_response/'realms/realm').map.size.should > 0
end
- end
- [:id, :name, :state].each do |option|
- method_name = :"test_if_realm_show_contain_#{option}"
- send :define_method, method_name do
- get '/api/realms/us.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- realm = doc.xpath('/realm').first
- assert_not_nil realm.xpath(option.to_s).first
+ def test_it_has_correct_attributes_set
+ do_xml_request '/api/realms', {}, true
+ (last_xml_response/'realms/realm').each do |realm|
+ realm.attributes.keys.sort.should == [ 'href', 'id' ]
+ end
end
- end
- def test_realms_filtering_by_state
- @params[:state] = 'AVAILABLE'
- get '/api/realms.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- assert_equal 2, doc.xpath('/realms/realm').size
- assert_equal @params[:state], doc.xpath('/realms/realm/state').first.text
- end
+ def test_us_has_correct_attributes
+ do_xml_request '/api/realms', {}, true
+ realm = (last_xml_response/'realms/realm[@id="us"]')
+ test_realm_attributes(realm)
+ end
- def test_realms_filtering_by_id
- get '/api/realms.xml', { :id => 'us'}, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- assert_equal 1, doc.xpath('/realms/realm').size
- assert_equal 'us', doc.xpath('/realms/realm/id').first.text
- end
+ def test_it_returns_valid_realm
+ do_xml_request '/api/realms/us', {}, true
+ realm = (last_xml_response/'realm')
+ test_realm_attributes(realm)
+ end
- include DeltacloudTest
+ def test_it_has_unique_ids
+ do_xml_request '/api/realms', {}, true
+ ids = []
+ (last_xml_response/'realms/realm').each do |realm|
+ ids << realm['id'].to_s
+ end
+ ids.sort.should == ids.sort.uniq
+ end
+
+ def test_it_responses_to_json
+ do_request '/api/realms', {}, false, { :format => :json }
+ JSON::parse(last_response.body).class.should == Hash
+ JSON::parse(last_response.body)['realms'].class.should == Array
+
+ do_request '/api/realms/us', {}, false, { :format => :json }
+ last_response.status.should == 200
+ JSON::parse(last_response.body).class.should == Hash
+ JSON::parse(last_response.body)['realm'].class.should == Hash
+ end
+
+ def test_it_responses_to_html
+ do_request '/api/realms', {}, false, { :format => :html }
+ last_response.status.should == 200
+ Nokogiri::HTML(last_response.body).search('html').first.name.should == 'html'
+
+ do_request '/api/realms/us', {}, false, { :format => :html }
+ last_response.status.should == 200
+ Nokogiri::HTML(last_response.body).search('html').first.name.should == 'html'
+ end
+ private
+
+ def test_realm_attributes(realm)
+ (realm/'name').should_not == nil
+ (realm/'limit').should_not == nil
+ ['AVAILABLE'].include?((realm/'state').text).should == true
+ end
+
+ end
end