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 2011/02/02 14:52:30 UTC

Improved command line client, added tests

Hi,

This patch will fix nasty issues with our command line client reported
a while back by David.
I also created some basic Test::Unit tests using shoulda to test
if comand line client is working. Those should be part of Hudson as
well to make sure that client is working after future changes.

  -- Michal


[PATCH core 2/2] Fixed plain_formatter and base_object in client

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

---
 client/lib/base_object.rb     |    1 -
 client/lib/plain_formatter.rb |    7 +++++--
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/client/lib/base_object.rb b/client/lib/base_object.rb
index 8b0f394..06db703 100644
--- a/client/lib/base_object.rb
+++ b/client/lib/base_object.rb
@@ -108,7 +108,6 @@ module DeltaCloud
         # First of all search throught array for method name
         m = search_for_method(method_name)
         if m.nil?
-          warn "[WARNING] Method unsupported by API: '#{self.class}.#{method_name}(#{args.inspect})'"
           return nil
         else
           # Call appropriate handler for method
diff --git a/client/lib/plain_formatter.rb b/client/lib/plain_formatter.rb
index e17a5e6..baa0f9c 100644
--- a/client/lib/plain_formatter.rb
+++ b/client/lib/plain_formatter.rb
@@ -33,8 +33,11 @@ module DeltaCloud
 
       class HardwareProfile < Base
         def format
+          architecture = @obj.architecture ? @obj.architecture.value[0,6] : 'opaque'
+          memory = @obj.memory ? @obj.memory.value.to_s[0,10] : 'opaque'
+          storage = @obj.storage ? @obj.storage.value.to_s[0,10] : 'opaque'
           sprintf("%-15s | %-6s | %10s | %10s ", @obj.id[0, 15],
-            @obj.architecture.value[0,6], @obj.memory.value.to_s[0,10], @obj.storage.value.to_s[0,10])
+           architecture , memory, storage)
         end
       end
 
@@ -77,7 +80,7 @@ module DeltaCloud
     end
 
     def format(obj)
-      object_name = obj.class.name.classify.gsub(/^DeltaCloud::API::/, '')
+      object_name = obj.class.name.classify.gsub(/^DeltaCloud::API::(\w+)::/, '')
       format_class = DeltaCloud::PlainFormatter::FormatObject.const_get(object_name)
       format_class.new(obj).format
     end
-- 
1.7.3.4


[PATCH core 1/2] Added Test::Unit tests for command line client

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

---
 client/Rakefile        |   10 +++
 client/tests/cmd.rb    |  196 ++++++++++++++++++++++++++++++++++++++++++++++++
 client/tests/common.rb |   24 ++++++
 3 files changed, 230 insertions(+), 0 deletions(-)
 create mode 100644 client/tests/cmd.rb
 create mode 100644 client/tests/common.rb

diff --git a/client/Rakefile b/client/Rakefile
index 947525e..a6ba12b 100644
--- a/client/Rakefile
+++ b/client/Rakefile
@@ -17,6 +17,7 @@
 # under the License.
 
 require 'rake/gempackagetask'
+require 'rake/testtask'
 
 load 'deltacloud-client.gemspec'
 
@@ -25,6 +26,7 @@ task 'documentation' do
   load 'lib/documentation.rb'
 end
 
+
 spec = Gem::Specification.load('deltacloud-client.gemspec')
 Rake::GemPackageTask.new(spec) do |pkg|
   pkg.need_tar = true
@@ -44,6 +46,14 @@ task 'fixtures' do
   FileUtils.cp_r( File.dirname( __FILE__ ) + '/specs/fixtures', File.dirname( __FILE__ ) + '/specs/data' )
 end
 
+namespace :test do
+  Rake::TestTask.new(:cmd) do |t|
+    t.libs << "tests"
+    t.test_files = FileList['tests/cmd.rb']
+    t.verbose = true
+  end
+end
+
 desc "Clean Fixtures"
 task 'fixtures:clean' do
   FileUtils.rm_rf( File.dirname( __FILE__ ) + '/specs/data' )
diff --git a/client/tests/cmd.rb b/client/tests/cmd.rb
new file mode 100644
index 0000000..2cd90a6
--- /dev/null
+++ b/client/tests/cmd.rb
@@ -0,0 +1,196 @@
+require 'rubygems'
+require 'shoulda'
+require 'tests/common'
+
+include DeltaCloud::TestHelper
+
+class CommandLineTest < Test::Unit::TestCase
+  context "a command line client" do
+
+    should "respond to --help argument" do
+      assert_nothing_raised do 
+        base_client('--help')
+      end
+    end
+
+    should "return API version with --version argument" do
+      assert_match /Deltacloud API\(mock\) (\d+)\.(\d+)/, client('--version')
+    end
+
+    should "return list all collections with --list argument" do
+      output = nil
+      assert_nothing_raised do
+        output = client('--list')
+      end
+      assert_not_nil output
+      assert_match /images/, output
+      assert_match /instances/, output
+      assert_match /realms/, output
+      assert_match /hardware_profiles/, output
+    end
+
+    should 'respond with proper error when accessing unknow collection' do
+      output = client('unknown_collection')
+      assert_match /^ERROR: Unknown collection/, output
+    end
+
+  end
+end
+
+class CmdRealmTest < Test::Unit::TestCase
+  context "a realms" do
+
+    should "be listed using realms argument" do
+      output = nil
+      assert_nothing_raised do
+        output = client('realms')
+      end
+      assert_match /^us/m, output
+      assert_match /^eu/m, output
+    end
+
+    should "be filtered using show --id argument" do
+      output = nil
+      assert_nothing_raised do
+        output = client('realms show --id us')
+      end
+      assert_match /^us/, output
+      assert_no_match /^eu/, output
+    end
+
+  end
+end
+
+class CmdHardwareProfilesTest < Test::Unit::TestCase
+  context "a hardware profiles" do
+
+    should "be listed using hardware_profiles argument" do
+      output = nil
+      assert_nothing_raised do
+        output = client('hardware_profiles')
+      end
+      assert_no_warning output
+      assert_match /^m1-small/m, output
+      assert_match /^m1-large/m, output
+      assert_match /^m1-xlarge/m, output
+      assert_match /^opaque/m, output
+    end
+
+    should "be filtered using show --id argument" do
+      output = nil
+      assert_nothing_raised do
+        output = client('hardware_profiles show --id m1-large')
+      end
+      assert_no_warning output
+      assert_match /^m1-large/, output
+      assert_no_match /^m1-small/, output
+    end
+  end
+end
+
+class CmdImagesTest < Test::Unit::TestCase
+
+  context "a images" do
+
+    should "be listed using images argument" do
+      output = nil
+      assert_nothing_raised do
+        output = client('images')
+      end
+      assert_no_warning output
+      assert_match /^img2/m, output
+      assert_match /^img1/m, output
+      assert_match /^img3/m, output
+    end
+
+    should "be filtered using show --id argument" do
+      output = nil
+      assert_nothing_raised do
+        output = client('images show --id img2')
+      end
+      assert_no_warning output
+      assert_match /^img2/m, output
+      assert_no_match /^img1/m, output
+    end
+
+    should "be filtered using --arch argument" do
+      output = nil
+      assert_nothing_raised do
+        output = client('images --arch x86_64')
+      end
+      assert_no_warning output
+      assert_match /x86_64/, output
+      assert_no_match /i386/, output
+    end
+
+  end
+
+end
+
+class CmdInstancesTest < Test::Unit::TestCase
+
+  context 'an instances' do
+
+    should 'be listed using instances argument' do
+      output = nil
+      assert_nothing_raised do
+        output = client('instances')
+      end
+      assert_no_warning output
+      assert_match /^inst1/, output
+    end
+
+    should 'be filtered using --id argument' do
+      output = nil
+      assert_nothing_raised do
+        output = client('instances show --id inst0')
+      end
+      assert_no_warning output
+      assert_match /^inst0/m, output
+      assert_no_match /^inst1/m, output
+    end
+
+  end
+
+  context 'an instance' do
+
+    should 'be created supplying --image-id argument and -p argument' do
+      output = nil
+      assert_nothing_raised do
+        output = client('instances create --image-id img1 -p m1-small')
+      end
+      assert_no_warning output
+      assert_match /^inst(\d+)/, output
+      @@created_instance_id = output.match(/^inst(\d+)/).to_a.first
+    end
+
+    should 'be rebooted using reboot operation' do
+      output = nil
+      assert_nothing_raised do
+        output = client("instances reboot --id #{@@created_instance_id}")
+      end
+      assert_no_warning output
+      assert_match /#{@@created_instance_id}/, output
+      assert_match /RUNNING/, output
+    end
+
+    should 'be stopped using stop operation' do
+      output = nil
+      assert_nothing_raised do
+        output = client("instances stop --id #{@@created_instance_id}")
+      end
+      assert_no_warning output
+      assert_match /#{@@created_instance_id}/, output
+      assert_match /STOPPED/, output
+    end
+
+    should 'be destroyed using destroy operation' do
+      output = nil
+      assert_nothing_raised do
+        output = client("instances destroy --id #{@@created_instance_id}")
+      end
+      assert_no_warning output
+    end
+
+  end
+end
diff --git a/client/tests/common.rb b/client/tests/common.rb
new file mode 100644
index 0000000..6ca04b5
--- /dev/null
+++ b/client/tests/common.rb
@@ -0,0 +1,24 @@
+module DeltaCloud
+  module TestHelper
+
+    include Test::Unit::Assertions
+
+    API_URL   = "http://localhost:3001/api"
+    API_USER  = "mockuser"
+    API_PASWD = "mockpassword"
+
+    def base_client(args)
+      `bin/deltacloudc #{args}`
+    end
+
+    def client(args)
+      args = "-u http://mockuser:mockpassword@localhost:3001/api " + args
+      base_client(args)
+    end
+
+    def assert_no_warning(output)
+      assert_no_match /\[WARNING\] Method unsupported by API: '(\w+)'/, output
+    end
+
+  end
+end
-- 
1.7.3.4