---
server/Rakefile | 7 +-
server/tests/api_test.rb | 37 ++++
server/tests/common.rb | 90 ++++++++++
server/tests/deltacloud_test.rb | 60 -------
server/tests/hardware_profiles_test.rb | 120 +++++++++++++
server/tests/images_test.rb | 173 ++++++++++--------
server/tests/instance_states_test.rb | 52 ++++++
server/tests/instances_test.rb | 303 ++++++++++++++++++++------------
server/tests/realms_test.rb | 110 +++++++-----
server/tests/storage_snapshots_test.rb | 48 -----
server/tests/storage_volumes_test.rb | 48 -----
11 files changed, 657 insertions(+), 391 deletions(-)
create mode 100644 server/tests/api_test.rb
create mode 100644 server/tests/common.rb
delete mode 100644 server/tests/deltacloud_test.rb
create mode 100644 server/tests/hardware_profiles_test.rb
create mode 100644 server/tests/instance_states_test.rb
delete mode 100644 server/tests/storage_snapshots_test.rb
delete mode 100644 server/tests/storage_volumes_test.rb
diff --git a/server/Rakefile b/server/Rakefile
index 39442b2..5b91c0c 100644
--- a/server/Rakefile
+++ b/server/Rakefile
@@ -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
}
diff --git a/server/tests/api_test.rb b/server/tests/api_test.rb
new file mode 100644
index 0000000..cbab5bf
--- /dev/null
+++ b/server/tests/api_test.rb
@@ -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
diff --git a/server/tests/common.rb b/server/tests/common.rb
new file mode 100644
index 0000000..ff47f58
--- /dev/null
+++ b/server/tests/common.rb
@@ -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
+
diff --git a/server/tests/deltacloud_test.rb b/server/tests/deltacloud_test.rb
deleted file mode 100644
index bf046bf..0000000
--- a/server/tests/deltacloud_test.rb
+++ /dev/null
@@ -1,60 +0,0 @@
-require 'rubygems'
-require 'test/unit'
-require 'rack/test'
-require 'base64'
-require 'nokogiri'
-require 'pp'
-require 'sinatra'
-
-ENV['API_DRIVER']='mock'
-ENV['API_HOST']='localhost'
-
-require 'server'
-
-set :environment => :test
-
-module DeltacloudTest
- include Rack::Test::Methods
-
- def app
- Sinatra::Application
- end
-
- def test_if_response_is_valid
- get '/api/'+...@collection+'.xml', @params, rack_headers
- assert last_response.ok?
- end
-
- def test_if_http_status_is_correct_with_wrong_credentials
- return if ['realms'].include?(@collection)
- wrong_header = rack_headers
- wrong_header['HTTP_AUTHORIZATION'] = authorization('wronguser',
'wrongpassword')
- get '/api/'+...@collection+'.xml', @params, wrong_header
- assert_equal 403, last_response.status
- end
-
- def test_if_index_operation_proper_root_element
- get '/api/'+...@collection+'.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- assert_equal @collection.gsub('_', '-'), doc.root.name
- end
-
- def test_html_response
- get '/api/'+...@collection+'.html', @params, rack_headers
- doc = Nokogiri::HTML.parse(last_response.body)
- assert_equal 'html', doc.root.name
- end
-
- def authorization(username, password)
- "Basic " + Base64.encode64("#{username}:#{password}")
- end
-
- def rack_headers
- return {
- 'HTTP_AUTHORIZATION' => authorization('mockuser', 'mockpassword'),
- 'SERVER_PORT' => '4040',
- 'Accept' => 'application/xml;q=1'
- }
- end
-
-end
diff --git a/server/tests/hardware_profiles_test.rb
b/server/tests/hardware_profiles_test.rb
new file mode 100644
index 0000000..28805a3
--- /dev/null
+++ b/server/tests/hardware_profiles_test.rb
@@ -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_profi...@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/'proper...@name="architecture"]').first['kind'].should ==
'fixed'
+ (profile/'proper...@name="architecture"]').first['unit'].should ==
'label'
+
+ (profile/'proper...@name="memory"]').first['kind'].should == 'range'
+ (profile/'proper...@name="memory"]').first['unit'].should == 'MB'
+ (profile/'proper...@name="memory"]/range').size.should == 1
+
(profile/'proper...@name="memory"]/range').first.attributes.keys.sort.should ==
[ 'first', 'last' ]
+
+ (profile/'proper...@name="cpu"]').first['kind'].should == 'fixed'
+ (profile/'proper...@name="cpu"]').first['unit'].should == 'count'
+
+ (profile/'proper...@name="storage"]').first['kind'].should == 'enum'
+ (profile/'proper...@name="storage"]').first['unit'].should == 'GB'
+ (profile/'proper...@name="storage"]/enum').size.should == 1
+ (profile/'proper...@name="storage"]/enum/entry').map.size.should == 3
+ (profile/'proper...@name="storage"]/enum/entry').each do |entry|
+ entry.attributes.keys.should == [ 'value' ]
+ entry['value'].should_not == nil
+ end
+ end
+
+ end
+end
diff --git a/server/tests/images_test.rb b/server/tests/images_test.rb
index 177852d..63c6907 100644
--- a/server/tests/images_test.rb
+++ b/server/tests/images_test.rb
@@ -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/ima...@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
- include DeltacloudTest
+ 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
+
+ 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
diff --git a/server/tests/instance_states_test.rb
b/server/tests/instance_states_test.rb
new file mode 100644
index 0000000..72c07b1
--- /dev/null
+++ b/server/tests/instance_states_test.rb
@@ -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
diff --git a/server/tests/instances_test.rb b/server/tests/instances_test.rb
index 8dcf8c9..90e3d6b 100644
--- a/server/tests/instances_test.rb
+++ b/server/tests/instances_test.rb
@@ -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
-
- attr_accessor :temp_inst_id
-
- 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 app
+ Sinatra::Application
+ 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_require_authentication
+ require_authentication?('/api/instances').should == true
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_returns_instances
+ do_xml_request '/api/instances', {}, true
+ (last_xml_response/'instances/instance').map.size.should > 0
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_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
- 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_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_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_inst1_has_correct_attributes
+ do_xml_request '/api/instances', {}, true
+ instance = (last_xml_response/'instances/instan...@id="inst2"]')
+ test_instance_attributes(instance)
+ end
- def test_001_create_instance
- @params = {
- :name => '_test-instance',
- :image_id => 'img1'
- }
+ def test_it_returns_valid_realm
+ do_xml_request '/api/instances/inst1', {}, true
+ instance = (last_xml_response/'instance')
+ test_instance_attributes(instance)
+ end
- post '/api/instances.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
+ 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
- self.temp_inst_id = doc.xpath('/instance/id').text
+ 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
- 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'
- def test_create_instance_with_hwp_id
+ 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
- @params = {
- :name => '_test-instance',
- :image_id => 'img1',
- :hwp_id => 'm1-xlarge'
- }
+ 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
- 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'
- }
+ 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/li...@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/li...@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
- 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_z0_reboot_instance
+ $created_instances.each do |instance_id|
+ do_xml_request "/api/instances/#{instance_id}", {}, true
+ reboot_url =
(last_xml_response/'actions/li...@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_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_z1_stop_created_instances
+ $created_instances.each do |instance_id|
+ do_xml_request "/api/instances/#{instance_id}", {}, true
+ stop_url =
(last_xml_response/'actions/li...@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_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_z2_destroy_created_instances
+ $created_instances.each do |instance_id|
+ do_xml_request "/api/instances/#{instance_id}", {}, true
+ destroy_url =
(last_xml_response/'actions/li...@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_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
+ private
+
+ 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
- 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
-
- include DeltacloudTest
-
end
diff --git a/server/tests/realms_test.rb b/server/tests/realms_test.rb
index b6053b3..97d539f 100644
--- a/server/tests/realms_test.rb
+++ b/server/tests/realms_test.rb
@@ -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/rea...@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
diff --git a/server/tests/storage_snapshots_test.rb
b/server/tests/storage_snapshots_test.rb
deleted file mode 100644
index e527ee6..0000000
--- a/server/tests/storage_snapshots_test.rb
+++ /dev/null
@@ -1,48 +0,0 @@
-require 'tests/deltacloud_test'
-
-class StorageSnapshotsTest < Test::Unit::TestCase
-
- def initialize(*args)
- @collection = 'storage_snapshots'
- @operations = [:index, :show]
- @options = [:id, :architecture, :memory, :storage]
- @params = {}
- super(*args)
- end
-
- def test_if_storage_snapshots_are_not_empty
- get '/api/storage_snapshots.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- assert_not_equal 0, doc.xpath('/storage-snapshots/storage-snapshot').size
- end
-
- [:id, :created, :state, :'storage-volume'].each do |option|
- method_name = :"test_if_storage_snapshots_index_contain_#{option}"
- send :define_method, method_name do
- get '/api/storage_snapshots.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- storage_volume =
doc.xpath('/storage-snapshots/storage-snapshot[1]').first
- assert_not_nil storage_volume.xpath(option.to_s).first
- end
- end
-
- [:id, :created, :state, :'storage-volume'].each do |option|
- method_name = :"test_if_storage_volume_show_contain_#{option}"
- send :define_method, method_name do
- get '/api/storage_snapshots/snap3.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- storage_volume = doc.xpath('/storage-snapshot').first
- assert_not_nil storage_volume.xpath(option.to_s).first
- end
- end
-
- def test_storage_snapshots_filtering_by_id
- get '/api/storage_snapshots.xml', { :id => 'snap3'}, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- assert_equal 1, doc.xpath('/storage-snapshots/storage-snapshot').size
- assert_equal 'snap3',
doc.xpath('/storage-snapshots/storage-snapshot/id').first.text
- end
-
- include DeltacloudTest
-
-end
diff --git a/server/tests/storage_volumes_test.rb
b/server/tests/storage_volumes_test.rb
deleted file mode 100644
index e56da45..0000000
--- a/server/tests/storage_volumes_test.rb
+++ /dev/null
@@ -1,48 +0,0 @@
-require 'tests/deltacloud_test'
-
-class StorageVolumesTest < Test::Unit::TestCase
-
- def initialize(*args)
- @collection = 'storage_volumes'
- @operations = [:index, :show]
- @options = [:id, :architecture, :memory, :storage]
- @params = {}
- super(*args)
- end
-
- def test_if_storage_volumes_are_not_empty
- get '/api/storage_volumes.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- assert_not_equal 0, doc.xpath('/storage-volumes/storage-volume').size
- end
-
- [:id, :created, :capacity, :device, :state].each do |option|
- method_name = :"test_if_storage_volumes_index_contain_#{option}"
- send :define_method, method_name do
- get '/api/storage_volumes.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- storage_volume = doc.xpath('/storage-volumes/storage-volume[1]').first
- assert_not_nil storage_volume.xpath(option.to_s).first
- end
- end
-
- [:id, :created, :capacity, :device, :state].each do |option|
- method_name = :"test_if_storage_volume_show_contain_#{option}"
- send :define_method, method_name do
- get '/api/storage_volumes/vol2.xml', @params, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- storage_volume = doc.xpath('/storage-volume').first
- assert_not_nil storage_volume.xpath(option.to_s).first
- end
- end
-
- def test_storage_volumes_filtering_by_id
- get '/api/storage_volumes.xml', { :id => 'vol2'}, rack_headers
- doc = Nokogiri::XML.parse(last_response.body)
- assert_equal 1, doc.xpath('/storage-volumes/storage-volume').size
- assert_equal 'vol2',
doc.xpath('/storage-volumes/storage-volume/id').first.text
- end
-
- include DeltacloudTest
-
-end
--
1.7.1.1