You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by mf...@redhat.com on 2012/07/20 15:39:18 UTC

[PATCH core 4/4] Core: Added initial tests for Mock driver API

From: Michal Fojtik <mf...@redhat.com>


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/Rakefile                             |    3 +-
 server/tests/drivers/mock/images_test.rb    |   55 +++++++++++++++
 server/tests/drivers/mock/instances_test.rb |   99 +++++++++++++++++++++++++++
 server/tests/drivers/mock/realms_test.rb    |   37 ++++++++++
 4 files changed, 193 insertions(+), 1 deletion(-)
 create mode 100644 server/tests/drivers/mock/images_test.rb
 create mode 100644 server/tests/drivers/mock/instances_test.rb
 create mode 100644 server/tests/drivers/mock/realms_test.rb

diff --git a/server/Rakefile b/server/Rakefile
index ef1ab1a..f232933 100644
--- a/server/Rakefile
+++ b/server/Rakefile
@@ -118,6 +118,7 @@ Rake::TestTask.new do |t|
     '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
-    'tests/deltacloud/collections/*test.rb'   # Deltacloud collections
+    'tests/deltacloud/collections/*test.rb',  # Deltacloud collections
+    'tests/drivers/*/*test.rb'                # Deltacloud driver specific unit tests
   ]
 end
diff --git a/server/tests/drivers/mock/images_test.rb b/server/tests/drivers/mock/images_test.rb
new file mode 100644
index 0000000..746000c
--- /dev/null
+++ b/server/tests/drivers/mock/images_test.rb
@@ -0,0 +1,55 @@
+require 'minitest/autorun'
+
+require_relative File.join('..', '..', '..', 'lib', 'deltacloud', 'api.rb')
+
+describe 'MockDriver Images' do
+
+  before do
+    @driver = Deltacloud::new(:mock, :user => 'mockuser', :password => 'mockpassword')
+  end
+
+  it 'must throw error when wrong credentials' do
+    Proc.new do
+      @driver.backend.images(OpenStruct.new(:user => 'unknown', :password => 'wrong'))
+    end.must_raise Deltacloud::ExceptionHandler::AuthenticationFailure, 'Authentication Failure'
+  end
+
+  it 'must return list of images' do
+    @driver.images.wont_be_empty
+    @driver.images.first.must_be_kind_of Image
+  end
+
+  it 'must allow to filter images' do
+    @driver.images(:id => 'img1').wont_be_empty
+    @driver.images(:id => 'img1').must_be_kind_of Array
+    @driver.images(:id => 'img1').size.must_equal 1
+    @driver.images(:id => 'img1').first.id.must_equal 'img1'
+    @driver.images(:owner_id => 'mockuser').size.must_equal 1
+    @driver.images(:owner_id => 'mockuser').first.owner_id.must_equal 'mockuser'
+    @driver.images(:id => 'unknown').must_be_empty
+  end
+
+  it 'must allow to retrieve single image' do
+    @driver.image(:id => 'img1').wont_be_nil
+    @driver.image(:id => 'img1').must_be_kind_of Image
+    @driver.image(:id => 'img1').id.must_equal 'img1'
+    @driver.image(:id => 'unknown').must_be_nil
+  end
+
+  it 'must allow to create a new image if instance supported' do
+    @driver.create_image(:id => 'inst1', :name => 'img1-test', :description => 'Test1').must_be_kind_of Image
+    @driver.image(:id => 'img1-test').wont_be_nil
+    @driver.image(:id => 'img1-test').id.must_equal 'img1-test'
+    @driver.image(:id => 'img1-test').name.must_equal 'img1-test'
+    @driver.image(:id => 'img1-test').description.must_equal 'Test1'
+    Proc.new { @driver.create_image(:id => 'unknown-instance', :name => 'test') }.must_raise Deltacloud::ExceptionHandler::ProviderError, 'CreateImageNotSupported'
+    @driver.image(:id => 'test').must_be_nil
+  end
+
+  it 'must allow to destroy created image' do
+    @driver.create_image(:id => 'inst1', :name => 'img1-test-destroy').must_be_kind_of Image
+    @driver.destroy_image('img1-test-destroy')
+    @driver.image(:id => 'img1-test-destroy').must_be_nil
+  end
+
+end
diff --git a/server/tests/drivers/mock/instances_test.rb b/server/tests/drivers/mock/instances_test.rb
new file mode 100644
index 0000000..fcb690a
--- /dev/null
+++ b/server/tests/drivers/mock/instances_test.rb
@@ -0,0 +1,99 @@
+require 'minitest/autorun'
+
+require_relative File.join('..', '..', '..', 'lib', 'deltacloud', 'api.rb')
+
+describe 'MockDriver Instances' do
+
+  before do
+    @driver = Deltacloud::new(:mock, :user => 'mockuser', :password => 'mockpassword')
+  end
+
+  it 'must throw error when wrong credentials' do
+    Proc.new do
+      @driver.backend.instances(OpenStruct.new(:user => 'unknown', :password => 'wrong'))
+    end.must_raise Deltacloud::ExceptionHandler::AuthenticationFailure, 'Authentication Failure'
+  end
+
+  it 'must return list of instances' do
+    @driver.instances.wont_be_empty
+    @driver.instances.first.must_be_kind_of Instance
+  end
+
+  it 'must allow to filter instances' do
+    @driver.instances(:id => 'inst1').wont_be_empty
+    @driver.instances(:id => 'inst1').must_be_kind_of Array
+    @driver.instances(:id => 'inst1').size.must_equal 1
+    @driver.instances(:id => 'inst1').first.id.must_equal 'inst1'
+    @driver.instances(:owner_id => 'mockuser').size.must_equal 2
+    @driver.instances(:owner_id => 'mockuser').first.owner_id.must_equal 'mockuser'
+    @driver.instances(:id => 'unknown').must_be_empty
+  end
+
+  it 'must allow to retrieve single instance' do
+    @driver.instance(:id => 'inst1').wont_be_nil
+    @driver.instance(:id => 'inst1').must_be_kind_of Instance
+    @driver.instance(:id => 'inst1').id.must_equal 'inst1'
+    @driver.instance(:id => 'unknown').must_be_nil
+  end
+
+  it 'must allow to create a new instance if instance supported' do
+    instance = @driver.create_instance('img1', :name => 'inst1-test', :realm_id => 'us', :hwp_id => 'm1-small')
+    instance.must_be_kind_of Instance
+    @driver.instance(:id => instance.id).wont_be_nil
+    @driver.instance(:id => instance.id).id.must_equal instance.id
+    @driver.instance(:id => instance.id).name.must_equal 'inst1-test'
+    @driver.instance(:id => instance.id).instance_profile.name.must_equal 'm1-small'
+    @driver.instance(:id => instance.id).realm_id.must_equal 'us'
+    @driver.instance(:id => instance.id).owner_id.must_equal 'mockuser'
+    @driver.instance(:id => instance.id).state.must_equal 'RUNNING'
+    @driver.instance(:id => instance.id).public_addresses.wont_be_empty
+    @driver.instance(:id => instance.id).actions.must_include :reboot
+    @driver.instance(:id => instance.id).actions.must_include :stop
+    @driver.destroy_instance(instance.id)
+    @driver.instance(:id => instance.id).must_be_nil
+  end
+
+  it 'must allow to destroy created instance' do
+    instance = @driver.create_instance('img1', :name => 'inst1-test-destroy')
+    instance.must_be_kind_of Instance
+    @driver.destroy_instance(instance.id)
+    @driver.instance(:id => instance.id).must_be_nil
+  end
+
+  it 'must allow to stop instance in running state' do
+    instance = @driver.create_instance('img1', :name => 'inst1-test-destroy')
+    instance.must_be_kind_of Instance
+    instance.state.must_equal 'RUNNING'
+    @driver.stop_instance(instance.id)
+    @driver.instance(:id => instance.id).state.must_equal 'STOPPED'
+    @driver.destroy_instance(instance.id)
+    @driver.instance(:id => instance.id).must_be_nil
+  end
+
+  it 'must allow to start instance in stopped state' do
+    instance = @driver.create_instance('img1', :name => 'inst1-test-destroy')
+    instance.must_be_kind_of Instance
+    instance.state.must_equal 'RUNNING'
+    @driver.stop_instance(instance.id)
+    @driver.instance(:id => instance.id).state.must_equal 'STOPPED'
+    @driver.start_instance(instance.id)
+    @driver.instance(:id => instance.id).state.must_equal 'RUNNING'
+    @driver.destroy_instance(instance.id)
+    @driver.instance(:id => instance.id).must_be_nil
+  end
+
+  it 'must allow to reboot instance in running state' do
+    instance = @driver.create_instance('img1', :name => 'inst1-test-destroy')
+    instance.must_be_kind_of Instance
+    instance.state.must_equal 'RUNNING'
+    @driver.reboot_instance(instance.id)
+    @driver.instance(:id => instance.id).state.must_equal 'RUNNING'
+    @driver.stop_instance(instance.id)
+    @driver.instance(:id => instance.id).state.must_equal 'STOPPED'
+    @driver.reboot_instance(instance.id)
+    @driver.instance(:id => instance.id).state.must_equal 'RUNNING'
+    @driver.destroy_instance(instance.id)
+    @driver.instance(:id => instance.id).must_be_nil
+  end
+
+end
diff --git a/server/tests/drivers/mock/realms_test.rb b/server/tests/drivers/mock/realms_test.rb
new file mode 100644
index 0000000..046e5b5
--- /dev/null
+++ b/server/tests/drivers/mock/realms_test.rb
@@ -0,0 +1,37 @@
+require 'minitest/autorun'
+
+require_relative File.join('..', '..', '..', 'lib', 'deltacloud', 'api.rb')
+
+describe 'MockDriver Realms' do
+
+  before do
+    @driver = Deltacloud::new(:mock, :user => 'mockuser', :password => 'mockpassword')
+  end
+
+  it 'must throw error when wrong credentials' do
+    Proc.new do
+      @driver.backend.realms(OpenStruct.new(:user => 'unknown', :password => 'wrong'))
+    end.must_raise Deltacloud::ExceptionHandler::AuthenticationFailure, 'Authentication Failure'
+  end
+
+  it 'must return list of realms' do
+    @driver.realms.wont_be_empty
+    @driver.realms.first.must_be_kind_of Realm
+  end
+
+  it 'must allow to filter realms' do
+    @driver.realms(:id => 'us').wont_be_empty
+    @driver.realms(:id => 'us').must_be_kind_of Array
+    @driver.realms(:id => 'us').size.must_equal 1
+    @driver.realms(:id => 'us').first.id.must_equal 'us'
+    @driver.realms(:id => 'unknown').must_be_empty
+  end
+
+  it 'must allow to retrieve single realm' do
+    @driver.realm(:id => 'us').wont_be_nil
+    @driver.realm(:id => 'us').must_be_kind_of Realm
+    @driver.realm(:id => 'us').id.must_equal 'us'
+    @driver.realm(:id => 'unknown').must_be_nil
+  end
+
+end
-- 
1.7.10.2