From: Michal Fojtik <[email protected]>
Signed-off-by: Michal fojtik <[email protected]> --- server/Rakefile | 1 + server/tests/cimi/db/database_helper_test.rb | 190 +++++++++++++++++++++++++++ server/tests/cimi/db/db_helper.rb | 22 ++++ server/tests/cimi/db/schema_test.rb | 94 +++++++++++++ 4 files changed, 307 insertions(+) create mode 100644 server/tests/cimi/db/database_helper_test.rb create mode 100644 server/tests/cimi/db/db_helper.rb create mode 100644 server/tests/cimi/db/schema_test.rb diff --git a/server/Rakefile b/server/Rakefile index cc17034..b8b3ea1 100644 --- a/server/Rakefile +++ b/server/Rakefile @@ -206,6 +206,7 @@ namespace :test do t.loader = :testrb end t.test_files = FileList[ + 'tests/cimi/db/*test.rb', # CIMI frontend database tests 'tests/cimi/model/*spec.rb', # CIMI frontend serialization API tests 'tests/cimi/collections/*test.rb', # CIMI frontend API tests ] diff --git a/server/tests/cimi/db/database_helper_test.rb b/server/tests/cimi/db/database_helper_test.rb new file mode 100644 index 0000000..7bd3e23 --- /dev/null +++ b/server/tests/cimi/db/database_helper_test.rb @@ -0,0 +1,190 @@ +require 'rubygems' +require 'require_relative' if RUBY_VERSION < '1.9' +require 'minitest/autorun' + +require_relative 'db_helper.rb' +require_relative '../spec_helper.rb' +require_relative './../collections/common.rb' + +class DatabaseHelper + include Deltacloud::Helpers::Database +end + +describe Deltacloud::Helpers::Database do + include Deltacloud::DatabaseTestHelper + + before do + ENV['RACK_ENV'] = 'development' + @db = DatabaseHelper.new + end + + it 'report if application is running under test environment' do + ENV['RACK_ENV'] = 'test' + @db.test_environment?.must_equal true + ENV['RACK_ENV'] = 'development' + @db.test_environment?.must_equal false + end + + it 'report if given entity is provided by database' do + @db.provides?('machine_template').must_equal true + @db.provides?('machine').must_equal false + end + + it 'reports the current provider' do + @db.current_provider.must_equal 'default' + end + + it 'create provider when it does not exists' do + @db.current_db.must_be_kind_of Deltacloud::Database::Provider + @db.current_db.driver.must_equal 'mock' + @db.current_db.url.must_equal @db.current_provider + @db.current_db.must_respond_to :entities + @db.current_db.must_respond_to :machine_templates + @db.current_db.must_respond_to :address_templates + @db.current_db.must_respond_to :volume_configurations + @db.current_db.must_respond_to :volume_templates + end + + it 'extract attributes both from JSON and XMLSimple' do + xml_simple_test = { 'property' => [ { 'key' => 'template', 'content' => "value"} ] } + json_test = { 'properties' => { 'template' => 'value' } } + + @db.extract_attribute_value('property', xml_simple_test).must_equal('template' => 'value') + @db.extract_attribute_value('properties', json_test).must_equal('template' => 'value') + end + + it 'must return entity for given model' do + provider = Deltacloud::Database::Provider + entity = Deltacloud::Database::Entity + @db.current_db.wont_be_nil + + new_entity = @db.current_db.add_entity( + :name => 'testMachine1', + :description => 'testMachine1 description', + :ent_properties => JSON::dump(:key => 'value'), + :be_kind => 'instance', + :be_id => 'inst1' + ) + + check_entity_base_attrs new_entity, entity, @db.current_db + + result = @db.get_entity(Instance.new(:id => 'inst1')) + result.must_equal new_entity + + new_entity.destroy.wont_equal false + end + + it 'must load attributes for entity for given model' do + provider = Deltacloud::Database::Provider + entity = Deltacloud::Database::Entity + @db.current_db.wont_be_nil + + new_entity = @db.current_db.add_entity( + :name => 'testMachine1', + :description => 'testMachine1 description', + :ent_properties => JSON::dump(:key => 'value'), + :be_kind => 'instance', + :be_id => 'inst1' + ) + + check_entity_base_attrs new_entity, entity, @db.current_db + + result = @db.load_attributes_for(Instance.new(:id => 'inst1')) + result.must_be_kind_of Hash + result[:name].must_equal new_entity.name + result[:description].must_equal new_entity.description + result[:property].must_equal JSON::parse(new_entity.ent_properties) + + new_entity.destroy.wont_equal false + end + + it 'must delete attributes for entity for given model' do + provider = Deltacloud::Database::Provider + entity = Deltacloud::Database::Entity + @db.current_db.wont_be_nil + + new_entity = @db.current_db.add_entity( + :name => 'testMachine1', + :description => 'testMachine1 description', + :ent_properties => JSON::dump(:key => 'value'), + :be_kind => 'instance', + :be_id => 'inst1' + ) + + check_entity_base_attrs new_entity, entity, @db.current_db + + result = @db.delete_attributes_for(Instance.new(:id => 'inst1')) + result.wont_equal false + result.exists?.must_equal false + end + + it 'must store JSON attributes for entity for given model' do + provider = Deltacloud::Database::Provider + entity = Deltacloud::Database::Entity + @db.current_db.wont_be_nil + + mock_instance = Instance.new(:id => 'inst1') + mock_json = ' +{ + "resourceURI": "http://schemas.dmtf.org/cimi/1/MachineCreate", + "name": "myDatabaseMachine", + "description": "This is a demo machine", + "properties": { + "foo": "bar", + "life": "is life" + }, + "machineTemplate": { + "machineConfig": { "href": "http://localhost:3001/cimi/machine_configurations/m1-small" }, + "machineImage": { "href": "http://localhost:3001/cimi/machine_images/img1" } + } +} + ' + result = @db.store_attributes_for(mock_instance, JSON::parse(mock_json)) + result.must_be_kind_of entity + check_entity_base_attrs result, entity, @db.current_db + load_result = @db.load_attributes_for(mock_instance) + load_result.must_be_kind_of Hash + load_result.wont_be_empty + load_result[:name].must_equal 'myDatabaseMachine' + load_result[:description].must_equal 'This is a demo machine' + load_result[:property].must_be_kind_of Hash + load_result[:property].wont_be_empty + load_result[:property]['foo'].must_equal 'bar' + load_result[:property]['life'].must_equal 'is life' + result.destroy.wont_equal false + end + + it 'must store XML attributes for entity for given model' do + provider = Deltacloud::Database::Provider + entity = Deltacloud::Database::Entity + @db.current_db.wont_be_nil + + mock_instance = Instance.new(:id => 'inst1') + mock_xml = ' +<MachineCreate> + <name>myMachineXML123</name> + <description>Description of my new Machine</description> + <machineTemplate> + <machineConfig href="http://localhost:3001/cimi/machine_configurations/m1-small"/> + <machineImage href="http://localhost:3001/cimi/machine_images/img1"/> + </machineTemplate> + <property key="test">value</property> + <property key="foo">bar</property> +</MachineCreate> + ' + result = @db.store_attributes_for(mock_instance, XmlSimple.xml_in(mock_xml)) + result.must_be_kind_of entity + check_entity_base_attrs result, entity, @db.current_db + load_result = @db.load_attributes_for(mock_instance) + load_result.must_be_kind_of Hash + load_result.wont_be_empty + load_result[:name].must_equal 'myMachineXML123' + load_result[:description].must_equal 'Description of my new Machine' + load_result[:property].must_be_kind_of Hash + load_result[:property].wont_be_empty + load_result[:property]['test'].must_equal 'value' + result.destroy.wont_equal false + end + + +end diff --git a/server/tests/cimi/db/db_helper.rb b/server/tests/cimi/db/db_helper.rb new file mode 100644 index 0000000..9bbdf30 --- /dev/null +++ b/server/tests/cimi/db/db_helper.rb @@ -0,0 +1,22 @@ +# Memory database +ENV['DATABASE_LOCATION'] = 'sqlite:/' + +module Deltacloud + module DatabaseTestHelper + + def check_entity_base_attrs(entity, entity_klass, provider) + entity.must_be_kind_of entity_klass + entity.id.wont_be_nil + entity.created_at.wont_be_nil + entity.name.wont_be_empty + entity.description.wont_be_empty + entity.provider.must_equal provider + entity.model.must_equal entity_klass + entity.to_hash[:name].must_equal entity.name + entity.to_hash[:description].must_equal entity.description + entity.to_hash[:property].must_be_kind_of Hash + entity.to_hash[:property].wont_be_empty + end + + end +end diff --git a/server/tests/cimi/db/schema_test.rb b/server/tests/cimi/db/schema_test.rb new file mode 100644 index 0000000..db5e71c --- /dev/null +++ b/server/tests/cimi/db/schema_test.rb @@ -0,0 +1,94 @@ +require 'rubygems' +require 'require_relative' if RUBY_VERSION < '1.9' + +require_relative 'db_helper.rb' +require_relative '../spec_helper.rb' + +describe Deltacloud::Database do + + include Deltacloud::DatabaseTestHelper + + before do + @db = Deltacloud.database + end + + it 'has valid database connection' do + @db.test_connection.must_equal true + end + + it 'creates the database schema' do + @db.tables.wont_be_empty + @db.tables.must_include :providers + @db.tables.must_include :entities + end + + it 'should allow creation of providers' do + provider = Deltacloud::Database::Provider + provider.table_name.must_equal :providers + new_provider = provider.create(:driver => 'mock', :url => 'test1') + new_provider.must_be_kind_of provider + new_provider.destroy.wont_equal false + end + + it 'should not allow creation of provider with nil driver' do + provider = Deltacloud::Database::Provider + lambda { + new_provider = provider.create(:driver => nil, :url => 'test1') + }.must_raise Sequel::InvalidValue + end + + it 'allow creation of simple entity' do + provider = Deltacloud::Database::Provider + entity = Deltacloud::Database::Entity + entity.table_name.must_equal :entities + + entity_provider = provider.create(:driver => :mock) + + new_entity = entity_provider.add_entity( + :name => 'testEntity1', + :description => 'testEntity1 description', + :ent_properties => JSON::dump(:key => 'value') + ) + + check_entity_base_attrs new_entity, entity, entity_provider + + new_entity.destroy.wont_equal false + entity_provider.destroy.wont_equal false + end + + it 'allow creation of extended entities' do + provider = Deltacloud::Database::Provider + entity = Deltacloud::Database::MachineTemplate + entity.table_name.must_equal :entities + entity_provider = provider.create(:driver => :mock) + new_entity = entity_provider.add_machine_template( + :name => 'testMachineTemplate1', + :description => 'testMachineTemplate1 description', + :ent_properties => JSON::dump(:key => 'value'), + :machine_config => 'http://example.com/cimi/machine_configurations/m1-small', + :machine_image => 'http://example.com/cimi/machine_image/img1' + ) + check_entity_base_attrs new_entity, entity, entity_provider + + new_entity.machine_config.wont_be_empty + new_entity.machine_image.wont_be_empty + + new_entity.destroy.wont_equal false + entity_provider.destroy.wont_equal false + end + + it 'validate presence of required attributes for extended entities' do + provider = Deltacloud::Database::Provider + entity = Deltacloud::Database::MachineTemplate + entity.table_name.must_equal :entities + entity_provider = provider.create(:driver => :mock) + lambda { + new_entity = entity_provider.add_machine_template( + :name => 'testMachineTemplate1', + :description => 'testMachineTemplate1 description', + :ent_properties => JSON::dump(:key => 'value'), + ) + }.must_raise Sequel::ValidationFailed + end + +end -- 1.8.0.2
