deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@apache.org
Subject [10/65] git commit: Tests: Added Deltacloud internal API tests into server/tests/deltacloud
Date Fri, 20 Jul 2012 09:59:21 GMT
Tests: Added Deltacloud internal API tests into server/tests/deltacloud


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

Branch: refs/heads/master
Commit: c4fb708256d04e7b2052424d7bb46997db3e74f3
Parents: 1d77be0
Author: Michal Fojtik <mfojtik@redhat.com>
Authored: Wed Jul 18 17:56:17 2012 +0200
Committer: Michal fojtik <mfojtik@redhat.com>
Committed: Fri Jul 20 11:59:08 2012 +0200

----------------------------------------------------------------------
 server/Rakefile                                   |    7 +-
 server/tests/deltacloud/base_collection_test.rb   |   29 ++++
 server/tests/deltacloud/collections_test.rb       |   34 ++++
 server/tests/deltacloud/common.rb                 |   31 ++++
 server/tests/deltacloud/deltacloud_helper_test.rb |   52 +++++++
 server/tests/deltacloud/drivers_test.rb           |   72 +++++++++
 server/tests/deltacloud/rack_test.rb              |   45 ++++++
 server/tests/deltacloud/server_test.rb            |  125 ++++++++++++++++
 8 files changed, 394 insertions(+), 1 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/c4fb7082/server/Rakefile
----------------------------------------------------------------------
diff --git a/server/Rakefile b/server/Rakefile
index f65c414..bd50a35 100644
--- a/server/Rakefile
+++ b/server/Rakefile
@@ -109,5 +109,10 @@ task :routes do
 end
 
 Rake::TestTask.new do |t|
-  t.test_files = FileList['tests/helpers/**/*test.rb', 'tests/drivers/base/*test.rb', 'tests/drivers/models/*test.rb']
+  t.test_files = FileList[
+    'tests/helpers/**/*test.rb',      # Deltacloud extensions (core_ext) and other helpers
+    'tests/drivers/base/*test.rb',    # Deltacloud drivers API tests
+    'tests/drivers/models/*test.rb',  # Deltacloud models tests
+    'tests/deltacloud/*test.rb'       # Deltacloud internal API tests
+  ]
 end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/c4fb7082/server/tests/deltacloud/base_collection_test.rb
----------------------------------------------------------------------
diff --git a/server/tests/deltacloud/base_collection_test.rb b/server/tests/deltacloud/base_collection_test.rb
new file mode 100644
index 0000000..2f390c3
--- /dev/null
+++ b/server/tests/deltacloud/base_collection_test.rb
@@ -0,0 +1,29 @@
+require 'minitest/autorun'
+
+load File.join(File.dirname(__FILE__), 'common.rb')
+
+describe Deltacloud::Collections::Base do
+
+  before do
+    @base = Deltacloud::Collections::Base
+  end
+
+  it 'has config set correctly' do
+    @base.config.must_be_kind_of Deltacloud::Server
+    @base.config.root_url.must_equal Deltacloud.config[:deltacloud].root_url
+  end
+
+  it 'has root_url set correctly' do
+    @base.root_url.must_equal  Deltacloud.config[:deltacloud].root_url
+  end
+
+  it 'has version set correctly' do
+    @base.version.must_equal  Deltacloud.config[:deltacloud].version
+  end
+
+  it 'provides helper for routes' do
+    @base.must_respond_to :route_for
+    @base.route_for('/hardware_profiles').must_equal "#{Deltacloud.config[:deltacloud].root_url}/hardware_profiles"
+  end
+
+end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/c4fb7082/server/tests/deltacloud/collections_test.rb
----------------------------------------------------------------------
diff --git a/server/tests/deltacloud/collections_test.rb b/server/tests/deltacloud/collections_test.rb
new file mode 100644
index 0000000..f010603
--- /dev/null
+++ b/server/tests/deltacloud/collections_test.rb
@@ -0,0 +1,34 @@
+require 'minitest/autorun'
+
+load File.join(File.dirname(__FILE__), 'common.rb')
+
+describe Deltacloud do
+
+  it 'must provide list of available collections names' do
+    Deltacloud.collection_names.wont_be_empty
+    Deltacloud.collection_names.must_include :drivers
+  end
+
+  it 'must provide access to collection classes' do
+    Deltacloud.collections.wont_be_empty
+    Deltacloud.collections.must_include Sinatra::Rabbit::DriversCollection
+  end
+
+  describe Deltacloud::Collections do
+
+    it 'must return collection by name' do
+      Deltacloud::Collections.must_respond_to :collection
+      Deltacloud::Collections.collection(:drivers).wont_be_nil
+      Deltacloud::Collections.collection(:drivers).must_equal Sinatra::Rabbit::DriversCollection
+    end
+
+    it 'must provide access to Deltacloud Sinatra modules' do
+      Deltacloud::Collections.must_respond_to :deltacloud_modules
+      Deltacloud::Collections.deltacloud_modules.wont_be_empty
+      Deltacloud::Collections.deltacloud_modules.must_include Deltacloud::Collections::Drivers
+    end
+
+  end
+
+
+end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/c4fb7082/server/tests/deltacloud/common.rb
----------------------------------------------------------------------
diff --git a/server/tests/deltacloud/common.rb b/server/tests/deltacloud/common.rb
new file mode 100644
index 0000000..b28f82d
--- /dev/null
+++ b/server/tests/deltacloud/common.rb
@@ -0,0 +1,31 @@
+require 'logger'
+require 'rack/test'
+require 'nokogiri'
+require 'pp'
+
+load File.join(File.dirname(__FILE__), '..', '..', 'lib', 'deltacloud_rack.rb')
+
+include Rack::Test::Methods
+
+def status; last_response.status; end
+def headers; last_response.headers; end
+def response_body; last_response.body; end
+def xml; Nokogiri::XML(response_body); end
+def root_url; Deltacloud.config[:deltacloud].root_url; end
+def formats; [ 'application/xml', 'application/json', 'text/html' ]; end
+
+# Set the default driver used for server API tests
+#
+ENV['API_DRIVER'] = 'mock'
+ENV['RACK_ENV']   = 'test'
+
+# Setup Deltacloud::API Sinatra instance
+#
+Deltacloud::configure do |server|
+  server.root_url '/api'
+  server.version '1.0.0'
+  server.klass 'Deltacloud::API'
+  server.logger Rack::DeltacloudLogger.setup(ENV['API_LOG'], ENV['API_VERBOSE'])
+end
+
+Deltacloud.require_frontend!

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/c4fb7082/server/tests/deltacloud/deltacloud_helper_test.rb
----------------------------------------------------------------------
diff --git a/server/tests/deltacloud/deltacloud_helper_test.rb b/server/tests/deltacloud/deltacloud_helper_test.rb
new file mode 100644
index 0000000..67d4af6
--- /dev/null
+++ b/server/tests/deltacloud/deltacloud_helper_test.rb
@@ -0,0 +1,52 @@
+require 'minitest/autorun'
+load File.join(File.dirname(__FILE__), 'common.rb')
+
+describe Deltacloud::Helpers::Application do
+
+  before do
+    class ApplicationHelper
+      include Deltacloud::Helpers::Drivers
+      include Deltacloud::Helpers::Application
+    end
+    @helper = ApplicationHelper.new
+  end
+
+  it 'provides list of supported collections for the current driver' do
+    @helper.supported_collections.wont_be_empty
+    @helper.supported_collections.must_include Sinatra::Rabbit::InstancesCollection
+    @helper.supported_collections.wont_include Sinatra::Rabbit::LoadBalancersCollection
+    Thread.current[:driver] = 'ec2'
+    @helper.supported_collections.must_include Sinatra::Rabbit::LoadBalancersCollection
+    Thread.current[:driver] = 'mock'
+  end
+
+  it 'provides name for the authentication feature' do
+    @helper.auth_feature_name.wont_be_nil
+    @helper.auth_feature_name.must_equal 'key'
+    Thread.current[:driver] = 'gogrid'
+    @helper.auth_feature_name.must_equal 'password'
+    Thread.current[:driver] = 'mock'
+  end
+
+  it 'provides HTTP methods for instance actions' do
+    @helper.instance_action_method(:stop).wont_be_nil
+    @helper.instance_action_method(:stop).must_equal :post
+    @helper.instance_action_method(:destroy).must_equal :delete
+  end
+
+  it 'provide helper to parse from XML to JSON' do
+    @helper.to_json('<xml>1</xml>').must_equal '{"xml":"1"}'
+  end
+
+  it 'provide helper for wrapping text nodes with CDATA' do
+    @helper.render_cdata('test').must_equal '<![CDATA[test]]>'
+    @helper.render_cdata('').must_equal '<![CDATA[]]>'
+    @helper.render_cdata('test   ').must_equal '<![CDATA[test]]>'
+  end
+
+  it 'provide helper to access driver entrypoints' do
+    @helper.driver_provider(Deltacloud::Drivers.driver_config[:ec2]).must_be_kind_of Hash
+    @helper.driver_provider(Deltacloud::Drivers.driver_config[:ec2]).keys.wont_be_empty
+  end
+
+end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/c4fb7082/server/tests/deltacloud/drivers_test.rb
----------------------------------------------------------------------
diff --git a/server/tests/deltacloud/drivers_test.rb b/server/tests/deltacloud/drivers_test.rb
new file mode 100644
index 0000000..90d96ac
--- /dev/null
+++ b/server/tests/deltacloud/drivers_test.rb
@@ -0,0 +1,72 @@
+require 'minitest/autorun'
+
+load File.join(File.dirname(__FILE__), 'common.rb')
+
+describe Deltacloud::Drivers do
+
+  it 'must provider access to all drivers configuration' do
+    Deltacloud::Drivers.must_respond_to :driver_config
+    Deltacloud::Drivers.driver_config.must_be_kind_of Hash
+    Deltacloud::Drivers.driver_config.keys.must_include :mock
+    Deltacloud::Drivers.driver_config[:mock][:name].wont_be_nil
+    Deltacloud::Drivers.driver_config[:mock][:name].must_equal 'Mock'
+  end
+
+  it 'must provide list of entrypoints for some drivers' do
+    Deltacloud::Drivers.driver_config[:ec2].wont_be_nil
+    Deltacloud::Drivers.driver_config[:ec2][:entrypoints].must_be_kind_of Hash
+  end
+
+end
+
+describe Deltacloud::Helpers::Drivers do
+
+  before do
+    class DriversHelper
+      include Deltacloud::Helpers::Drivers
+    end
+    @helper = DriversHelper.new
+  end
+
+  it 'should report the current driver as a Symbol' do
+    @helper.driver_symbol.wont_be_nil
+    @helper.driver_symbol.must_be_kind_of Symbol
+    @helper.driver_symbol.must_equal :mock
+  end
+
+  it 'should report the current driver name' do
+    @helper.driver_name.wont_be_nil
+    @helper.driver_name.must_be_kind_of String
+    @helper.driver_name.must_equal 'mock'
+  end
+
+  it 'should provide the current driver class name' do
+    @helper.driver_class_name.wont_be_nil
+    @helper.driver_class_name.must_equal 'Mock'
+  end
+
+  it 'should provide the path to the current driver' do
+    @helper.driver_source_name.wont_be_nil
+    @helper.driver_source_name.must_equal '../drivers/mock/mock_driver.rb'
+  end
+
+  it 'should provide access to the driver instance' do
+    @helper.driver_class.must_be_kind_of Deltacloud::Drivers::Mock::MockDriver
+  end
+
+  it 'should autoload the driver' do
+    Thread.current[:driver] = 'ec2'
+    @helper.driver.must_be_kind_of Deltacloud::Drivers::Ec2::Ec2Driver
+    Thread.current[:driver] = 'mock'
+  end
+
+  it 'should throw an exception on unknown driver' do
+    begin
+      Thread.current[:driver] = 'unknown'
+      Proc.new { @helper.driver }.must_raise RuntimeError
+    ensure
+      Thread.current[:driver] = 'mock'
+    end
+  end
+
+end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/c4fb7082/server/tests/deltacloud/rack_test.rb
----------------------------------------------------------------------
diff --git a/server/tests/deltacloud/rack_test.rb b/server/tests/deltacloud/rack_test.rb
new file mode 100644
index 0000000..d550c7c
--- /dev/null
+++ b/server/tests/deltacloud/rack_test.rb
@@ -0,0 +1,45 @@
+require 'minitest/autorun'
+load File.join(File.dirname(__FILE__), 'common.rb')
+
+describe Deltacloud do
+
+  before do
+    Deltacloud::configure :cimi do |server|
+      server.root_url '/cimi'
+      server.version '0.0.1'
+      server.klass 'CIMI'
+      server.logger Logger
+    end
+  end
+
+  it 'should return the servers configuration' do
+    Deltacloud.config.must_be_kind_of Hash
+    Deltacloud.config.keys.must_include :deltacloud
+    Deltacloud.config.keys.must_include :cimi
+    Deltacloud.config[:deltacloud].must_be_kind_of Deltacloud::Server
+  end
+
+  it 'should be able to require the correct frontned' do
+    Deltacloud[:deltacloud].klass.must_equal Deltacloud::API
+  end
+
+  describe Deltacloud::Server do
+
+    it 'should provide the correct root_url' do
+      Deltacloud.config[:deltacloud].root_url.must_equal '/api'
+      Deltacloud.config[:cimi].root_url.must_equal '/cimi'
+    end
+
+    it 'should provide the correct version' do
+      Deltacloud.config[:deltacloud].version.must_equal '1.0.0'
+      Deltacloud.config[:cimi].version.must_equal '0.0.1'
+    end
+
+    it 'should provide the logger facility' do
+      Deltacloud.config[:deltacloud].logger.must_equal Rack::DeltacloudLogger
+      Deltacloud.config[:cimi].logger.must_equal Logger
+    end
+
+  end
+
+end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/c4fb7082/server/tests/deltacloud/server_test.rb
----------------------------------------------------------------------
diff --git a/server/tests/deltacloud/server_test.rb b/server/tests/deltacloud/server_test.rb
new file mode 100644
index 0000000..b97a286
--- /dev/null
+++ b/server/tests/deltacloud/server_test.rb
@@ -0,0 +1,125 @@
+require 'minitest/autorun'
+
+load File.join(File.dirname(__FILE__), 'common.rb')
+
+describe Deltacloud::API do
+
+  before do
+    def app; Deltacloud::API; end
+  end
+
+  it 'has the config set property' do
+    Deltacloud::API.config.must_be_kind_of Deltacloud::Server
+    Deltacloud::API.config.root_url.must_equal Deltacloud[:deltacloud].root_url
+  end
+
+  it 'must provide the root_url entrypoint' do
+    get Deltacloud::API.config.root_url
+    status.must_equal 200
+    xml.root.name.must_equal 'api'
+  end
+
+  it 'must advertise current API version in response headers' do
+    get Deltacloud::API.config.root_url
+    headers['Server'].must_match /Apache-Deltacloud\/(\d+).(\d+).(\d+)/
+    xml.root.name.must_equal 'api'
+  end
+
+  it 'must advertise current API driver in response headers' do
+    get Deltacloud::API.config.root_url
+    headers['X-Deltacloud-Driver'].must_equal ENV['API_DRIVER']
+    xml.root.name.must_equal 'api'
+  end
+
+  it 'must support matrix parameters for changing API driver' do
+    get Deltacloud::API.config.root_url + ';driver=ec2'
+    headers['X-Deltacloud-Driver'].must_equal 'ec2'
+    xml.root.name.must_equal 'api'
+  end
+
+  it 'must support matrix parameters for changing API provider' do
+    get Deltacloud::API.config.root_url + ';provider=someprovider'
+    headers['X-Deltacloud-Provider'].must_equal 'someprovider'
+    xml.root.name.must_equal 'api'
+  end
+
+  it 'must support matrix parameters for changing both API provider and driver' do
+    get Deltacloud::API.config.root_url + ';provider=someprovider;driver=ec2'
+    headers['X-Deltacloud-Provider'].must_equal 'someprovider'
+    headers['X-Deltacloud-Driver'].must_equal 'ec2'
+    xml.root.name.must_equal 'api'
+  end
+
+  it 'must change driver back to default when no matrix param set' do
+    get Deltacloud::API.config.root_url + ';driver=ec2'
+    headers['X-Deltacloud-Driver'].must_equal 'ec2'
+    get Deltacloud::API.config.root_url
+    headers['X-Deltacloud-Driver'].must_equal ENV['API_DRIVER']
+    xml.root.name.must_equal 'api'
+  end
+
+  it 'must change provider back to default when no matrix param set' do
+    get Deltacloud::API.config.root_url + ';provider=someprovider'
+    headers['X-Deltacloud-Provider'].must_equal 'someprovider'
+    get Deltacloud::API.config.root_url
+    headers['X-Deltacloud-Provider'].must_be_nil
+    xml.root.name.must_equal 'api'
+  end
+
+  it 'must support matrix params when they are part of URL' do
+    get Deltacloud::API.config.root_url + ';driver=ec2/hardware_profiles'
+    headers['X-Deltacloud-Driver'].must_equal 'ec2'
+    status.must_equal 200
+    xml.root.name.must_equal 'hardware_profiles'
+  end
+
+  it 'must switch driver using X-Deltacloud-Driver HTTP header in request' do
+    header 'X-Deltacloud-Driver', 'ec2'
+    get Deltacloud::API.config.root_url
+    headers['X-Deltacloud-Driver'].must_equal 'ec2'
+    header 'X-Deltacloud-Driver', 'mock'
+    get Deltacloud::API.config.root_url
+    headers['X-Deltacloud-Driver'].must_equal 'mock'
+  end
+
+  it 'must switch provider using X-Deltacloud-Provider HTTP header in request' do
+    header 'X-Deltacloud-Provider', 'someprovider'
+    get Deltacloud::API.config.root_url
+    headers['X-Deltacloud-Provider'].must_equal 'someprovider'
+    header 'X-Deltacloud-Provider', nil
+    get Deltacloud::API.config.root_url
+    headers['X-Deltacloud-Provider'].must_be_nil
+  end
+
+  it 'must support media type negotiation for JSON format' do
+    header 'Accept', 'application/json'
+    get Deltacloud::API.config.root_url
+    status.must_equal 200
+    headers['Content-Type'].must_equal 'application/json'
+    JSON::parse(response_body).must_be_kind_of Hash
+    JSON::parse(response_body)['api'].must_be_kind_of Hash
+  end
+
+  it 'must support media type negotiation for HTML format' do
+    header 'Accept', 'text/html'
+    get Deltacloud::API.config.root_url
+    status.must_equal 200
+    headers['Content-Type'].must_equal 'text/html'
+    response_body.must_match /^<\!DOCTYPE html>/
+  end
+
+  it 'must support media type negotiation for XML format' do
+    header 'Accept', 'application/xml'
+    get Deltacloud::API.config.root_url
+    status.must_equal 200
+    headers['Content-Type'].must_equal 'application/xml'
+    xml.root.name.must_equal 'api'
+  end
+
+  it 'must return proper error when unknown media type requested' do
+    header 'Accept', 'application/unknown'
+    get Deltacloud::API.config.root_url
+    status.must_equal 406
+  end
+
+end


Mime
View raw message