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/17 16:00:11 UTC

[PATCH core 1/4] Tests: Added minitest tests for base_driver and core_ext

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


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/tests/drivers/base/base_driver_test.rb  |  125 ++++++++++++++++++++++++
 server/tests/helpers/core_ext/array_tests.rb   |   22 +++++
 server/tests/helpers/core_ext/hash_tests.rb    |   21 ++++
 server/tests/helpers/core_ext/integer_tests.rb |   17 ++++
 server/tests/helpers/core_ext/string_tests.rb  |   72 ++++++++++++++
 5 files changed, 257 insertions(+)
 create mode 100644 server/tests/drivers/base/base_driver_test.rb
 create mode 100644 server/tests/helpers/core_ext/array_tests.rb
 create mode 100644 server/tests/helpers/core_ext/hash_tests.rb
 create mode 100644 server/tests/helpers/core_ext/integer_tests.rb
 create mode 100644 server/tests/helpers/core_ext/string_tests.rb

diff --git a/server/tests/drivers/base/base_driver_test.rb b/server/tests/drivers/base/base_driver_test.rb
new file mode 100644
index 0000000..4173b50
--- /dev/null
+++ b/server/tests/drivers/base/base_driver_test.rb
@@ -0,0 +1,125 @@
+require 'minitest/autorun'
+
+load File.join(File.dirname(__FILE__), '..', '..', '..', 'lib', 'deltacloud', 'drivers', 'base_driver.rb')
+
+describe Deltacloud::BaseDriver do
+
+  before do
+    class TestDriver < Deltacloud::BaseDriver
+
+      define_hardware_profile('t1.micro') do
+        cpu                1
+        memory             613
+        storage            160
+        architecture       ['i386','x86_64']
+      end
+
+      feature :instances, :user_data
+      feature :instances, :user_name do
+        { :max_length => 50 }
+      end
+
+      define_instance_states do
+        start.to( :pending )          .automatically
+        pending.to( :running )        .automatically
+        pending.to( :stopping )       .on( :stop )
+        pending.to( :stopped )        .automatically
+        stopped.to( :running )        .on( :start )
+        running.to( :running )        .on( :reboot )
+        running.to( :stopping )       .on( :stop )
+        stopping.to(:stopped)         .automatically
+        stopping.to(:finish)          .automatically
+        stopped.to( :finish )         .automatically
+      end
+
+      def realms(credentials, opts={}); end
+    end
+    @driver = TestDriver.new
+  end
+
+  describe 'when creating a new driver' do
+
+    it 'must return the proper driver name' do
+      @driver.name.must_equal 'test'
+      TestDriver.name.must_equal 'TestDriver'
+    end
+
+  end
+
+  describe 'hardware profiles' do
+
+    it 'must allow to define custom hardware profiles' do
+      TestDriver.must_respond_to :define_hardware_profile
+    end
+
+    it 'should not allow to create duplicated profile' do
+      TestDriver.define_hardware_profile('t1.micro').must_be_nil
+      TestDriver.hardware_profiles.size.must_equal 1
+    end
+
+    it 'should return all defined hardware profiles' do
+      TestDriver.must_respond_to :hardware_profiles
+      TestDriver.hardware_profiles.wont_be_empty
+    end
+
+    it 'should allow to filter hardware profiles' do
+      @driver.filter_hardware_profiles(TestDriver.hardware_profiles, :architecture => 'i386').wont_be_empty
+      @driver.filter_hardware_profiles(TestDriver.hardware_profiles, :architecture => 'unknown').must_be_empty
+      @driver.filter_hardware_profiles(TestDriver.hardware_profiles, :id => 't1.micro').wont_be_empty
+      @driver.filter_hardware_profiles(TestDriver.hardware_profiles, :id => 'm1.unknown').must_be_empty
+    end
+
+  end
+
+  describe 'features' do
+
+    it 'should return all defined features' do
+      TestDriver.features.must_be_kind_of Hash
+      TestDriver.features.keys.must_include :instances
+      TestDriver.features[:instances].must_be_kind_of Array
+      TestDriver.features[:instances].must_include :user_data
+    end
+
+    it 'must have method to check if feature is defined' do
+      TestDriver.must_respond_to :'has_feature?'
+      TestDriver.has_feature?(:instances, :user_data).must_equal true
+      TestDriver.has_feature?(:instances, :user_unknown).must_equal false
+    end
+
+    it 'must return feature defined constraints' do
+      TestDriver.must_respond_to :'constraints'
+      TestDriver.constraints.must_be_kind_of Hash
+      TestDriver.constraints[:instances].must_be_kind_of Hash
+      TestDriver.constraints[:instances][:user_name].must_be_kind_of Hash
+      TestDriver.constraints[:instances][:user_name][:max_length].must_equal 50
+      TestDriver.constraints(:collection => :instances, :feature => :user_name).must_be_kind_of Hash
+      TestDriver.constraints(:collection => :instances, :feature => :user_name)[:max_length].must_equal 50
+    end
+
+  end
+
+  describe 'instance states' do
+
+    it 'should return defined instance state machine' do
+      TestDriver.must_respond_to :instance_state_machine
+      TestDriver.instance_state_machine.must_be_kind_of Deltacloud::StateMachine
+    end
+
+    it 'should return actions for given state' do
+      @driver.must_respond_to :instance_actions_for
+      @driver.instance_actions_for('RUNNING').must_be_kind_of Array
+      @driver.instance_actions_for('RUNNING').must_include :stop
+    end
+
+  end
+
+  describe 'capabilities' do
+
+    it 'should return if driver has given capability' do
+      @driver.has_capability?(:realms).must_equal true
+      @driver.has_capability?(:images).must_equal false
+    end
+
+  end
+
+end
diff --git a/server/tests/helpers/core_ext/array_tests.rb b/server/tests/helpers/core_ext/array_tests.rb
new file mode 100644
index 0000000..c116484
--- /dev/null
+++ b/server/tests/helpers/core_ext/array_tests.rb
@@ -0,0 +1,22 @@
+require 'minitest/autorun'
+
+load File.join(File.dirname(__FILE__), '..', '..', '..', 'lib', 'deltacloud', 'core_ext.rb')
+
+class TestArray < MiniTest::Unit::TestCase
+
+
+  def test_expand_opts!
+    assert_equal true, [].respond_to?(:"expand_opts!")
+    a = [1,2,3]
+    a.expand_opts!(:test => 1)
+    assert_equal Hash, a.last.class
+  end
+
+  def test_extract_opts!
+    assert_equal true, [].respond_to?(:"extract_opts!")
+    a = [1,2,3, { :test => 1}]
+    a.extract_opts!
+    assert_equal [1,2,3], a
+  end
+
+end
diff --git a/server/tests/helpers/core_ext/hash_tests.rb b/server/tests/helpers/core_ext/hash_tests.rb
new file mode 100644
index 0000000..196d70c
--- /dev/null
+++ b/server/tests/helpers/core_ext/hash_tests.rb
@@ -0,0 +1,21 @@
+require 'minitest/autorun'
+
+load File.join(File.dirname(__FILE__), '..', '..', '..', 'lib', 'deltacloud', 'core_ext.rb')
+
+class TestHash < MiniTest::Unit::TestCase
+
+
+  def test_gsub_keys
+    assert_equal true, {}.respond_to?(:"gsub_keys")
+    h = {
+      :'test-key-1' => '1',
+      :'test-key-2' => '2',
+      :'test-key-3' => '3',
+    }
+    h.gsub_keys(/test-key/, 'test')
+    assert_equal h['test-1'], '1'
+    assert_equal h['test-2'], '2'
+    assert_equal h['test-3'], '3'
+  end
+
+end
diff --git a/server/tests/helpers/core_ext/integer_tests.rb b/server/tests/helpers/core_ext/integer_tests.rb
new file mode 100644
index 0000000..2ddf09b
--- /dev/null
+++ b/server/tests/helpers/core_ext/integer_tests.rb
@@ -0,0 +1,17 @@
+require 'minitest/autorun'
+
+load File.join(File.dirname(__FILE__), '..', '..', '..', 'lib', 'deltacloud', 'core_ext.rb')
+
+class TestInteger < MiniTest::Unit::TestCase
+
+
+  def test_ordinalize
+    assert_equal true, 1.respond_to?(:"ordinalize")
+    assert_equal '1st', 1.ordinalize
+    assert_equal '2nd', 2.ordinalize
+    assert_equal '3rd', 3.ordinalize
+    assert_equal '6th', 6.ordinalize
+    assert_equal '1100th', 1100.ordinalize
+  end
+
+end
diff --git a/server/tests/helpers/core_ext/string_tests.rb b/server/tests/helpers/core_ext/string_tests.rb
new file mode 100644
index 0000000..89eeaa4
--- /dev/null
+++ b/server/tests/helpers/core_ext/string_tests.rb
@@ -0,0 +1,72 @@
+require 'minitest/autorun'
+
+load File.join(File.dirname(__FILE__), '..', '..', '..', 'lib', 'deltacloud', 'core_ext.rb')
+
+class TestString < MiniTest::Unit::TestCase
+
+
+  def test_blank?
+    assert_equal true, ''.respond_to?(:"blank?")
+    assert_equal true, ''.blank?
+    assert_equal true, ' '.blank?
+    assert_equal false, 'test'.blank?
+  end
+
+  def test_titlecase
+    assert_equal true, ''.respond_to?(:"titlecase")
+    assert_equal "This Is A String", 'this is a string'.titlecase
+    assert_equal 'This', 'this'.titlecase
+  end
+
+  def test_pluralize
+    assert_equal true, ''.respond_to?(:"pluralize")
+    assert_equal 'instances', 'instance'.pluralize
+    assert_equal 'properties', 'property'.pluralize
+    assert_equal 'addresses', 'address'.pluralize
+  end
+
+  def test_singularize
+    assert_equal true, ''.respond_to?(:"singularize")
+    assert_equal 'instance', 'instances'.singularize
+    assert_equal 'property', 'properties'.singularize
+    assert_equal 'address', 'addresses'.singularize
+  end
+
+  def test_underscore
+    assert_equal true, ''.respond_to?(:"underscore")
+    assert_equal 'test_model', 'TestModel'.underscore
+    assert_equal 'test/model', 'Test::Model'.underscore
+  end
+
+  def test_camelize
+    assert_equal true, ''.respond_to?(:"camelize")
+    assert_equal 'TestModel', 'test_model'.camelize
+    assert_equal 'testModel', 'test_model'.camelize(:lowercase_first_letter)
+  end
+
+  def test_uncapitalize
+    assert_equal true, ''.respond_to?(:"uncapitalize")
+    assert_equal 'testModel', 'TestModel'.uncapitalize
+    assert_equal 'test', 'Test'.uncapitalize
+  end
+
+  def test_upcase_first
+    assert_equal true, ''.respond_to?(:"upcase_first")
+    assert_equal 'Test', 'test'.upcase_first
+    assert_equal 'Test', 'Test'.upcase_first
+    assert_equal 'TestModel', 'testModel'.upcase_first
+  end
+
+  def test_truncate
+    assert_equal true, ''.respond_to?(:"truncate")
+    assert_equal 'ThisIs...cated', 'ThisIsALogStringThatNeedsToBeTruncated'.truncate
+    assert_equal 'Thi...ed', 'ThisIsALogStringThatNeedsToBeTruncated'.truncate(5)
+    assert_equal 'T...', 'ThisIsALogStringThatNeedsToBeTruncated'.truncate(1)
+    assert_equal 'This', 'This'.truncate(10)
+  end
+
+  def test_it_has_each
+    assert_equal true, ''.respond_to?(:each)
+  end
+
+end
-- 
1.7.10.2