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/10/21 18:15:33 UTC

[PATCH core] CIMI: Added initial example of Test::Unit tests for CIMI frontend

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


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/Rakefile                    |   10 ++++-
 server/tests/cimi/cimi.rb          |    3 +
 server/tests/cimi/mock/api_test.rb |   91 ++++++++++++++++++++++++++++++++++++
 server/tests/common.rb             |    6 ++-
 4 files changed, 108 insertions(+), 2 deletions(-)
 create mode 100644 server/tests/cimi/cimi.rb
 create mode 100644 server/tests/cimi/mock/api_test.rb

diff --git a/server/Rakefile b/server/Rakefile
index a74cbce..d3ff8fb 100644
--- a/server/Rakefile
+++ b/server/Rakefile
@@ -56,12 +56,20 @@ namespace :test do
   %w(mock rackspace rhevm).each do |driver|
     desc "Run #{driver} unit tests"
     Rake::TestTask.new(driver) { |t|
-      t.test_files = ['tests/common.rb', "tests/drivers/#{driver}/setup.rb"] + FileList.new("tests/drivers/#{driver}/*_test.rb") + FileList.new('tests/rabbit_test.rb')
+      t.test_files = ["tests/drivers/#{driver}/setup.rb"] + FileList.new("tests/drivers/#{driver}/*_test.rb") + FileList.new('tests/rabbit_test.rb')
       t.options = "-v -v"
       t.verbose = true
       t.warning = false
     }
   end
+
+  desc "Run CIMI frontend tests"
+  Rake::TestTask.new "cimi" do |t|
+    t.test_files = ["tests/cimi/cimi.rb", "tests/cimi/mock/*_test.rb"]
+    t.options = "-v -v"
+    t.verbose = true
+    t.warning = false
+  end
 end
 
 desc "Call our Test::Unit suite"
diff --git a/server/tests/cimi/cimi.rb b/server/tests/cimi/cimi.rb
new file mode 100644
index 0000000..8d351ad
--- /dev/null
+++ b/server/tests/cimi/cimi.rb
@@ -0,0 +1,3 @@
+ENV['API_FRONTEND'] = "cimi"
+ENV['API_USER'] = 'mockuser'
+ENV['API_PASSWORD'] = 'mockpassword'
diff --git a/server/tests/cimi/mock/api_test.rb b/server/tests/cimi/mock/api_test.rb
new file mode 100644
index 0000000..6d951c7
--- /dev/null
+++ b/server/tests/cimi/mock/api_test.rb
@@ -0,0 +1,91 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+
+$:.unshift File.join(File.dirname(__FILE__), '..', '..', '..')
+require 'tests/common'
+
+module CimiUnitTest
+  class ApiTest < Test::Unit::TestCase
+    include Rack::Test::Methods
+
+    def app
+      Sinatra::Application
+    end
+
+    def test_it_redirect_client_to_entrypoint
+      get_url '/cimi'
+      last_response.status.should == 301
+      last_response.header['Location'].should == 'http://example.org/cimi/cloudEntryPoint'
+    end
+
+    def test_it_return_valid_content_type
+      get_url '/cimi/cloudEntryPoint'
+      last_response.content_type.should == 'application/CIMI-CloudEntryPoint+xml;charset=utf-8'
+    end
+
+    def test_it_return_valid_xmlns
+      get_url '/cimi/cloudEntryPoint'
+      (last_xml_response/'CloudEntryPoint')[:xmlns] == CMWG_NAMESPACE
+    end
+
+    def test_it_return_valid_root_element
+      get_url '/cimi/cloudEntryPoint'
+      last_xml_response.root.name == "CloudEntryPoint"
+    end
+
+    def test_it_include_all_properties
+      get_url '/cimi/cloudEntryPoint'
+      properties = ['uri', 'name', 'description', 'created', 'Volumes', 'Machines', 'MachineImages', 'MachineConfigurations'].sort
+      (last_xml_response/'CloudEntryPoint/*').collect { |p| p.name }.sort.should == properties
+    end
+
+    def test_collection_have_href_attributes
+      get_url '/cimi/cloudEntryPoint'
+      collections = [ 'Volumes', 'Machines', 'MachineImages', 'MachineConfigurations' ]
+      (last_xml_response/'CloudEntryPoint/*').each do |collection|
+        collection[:href].should_not nil
+      end
+    end
+
+    def test_collection_href_attributes_are_valid
+      valid_uris = {
+        'Volumes' => 'cimi/volumes',
+        'Machines' => 'cimi/machines',
+        'MachineImages' => 'cimi/machine_images',
+        'MachineConfiguration' => 'cimi/machine_configurations'
+      }
+      get_url '/cimi/cloudEntryPoint'
+      (last_xml_response/'CloudEntryPoint/*').each do |collection|
+        next unless valid_uris.keys.include? collection.name
+        collection[:href].should =~ /#{valid_uris[collection.name]}$/
+      end
+    end
+
+    def test_it_respond_to_json
+      get_url '/cimi/cloudEntryPoint', {}, :format => :json
+      JSON::parse(last_response.body).class.should == Hash
+    end
+
+    def test_json_include_all_properties
+      get_url '/cimi/cloudEntryPoint', {}, :format => :json
+      properties = ['uri', 'name', 'description', 'created', 'Volumes', 'Machines', 'MachineImages', 'MachineConfigurations'].sort
+      properties.each do |property|
+        JSON::parse(last_response.body).keys.include?(property).should == true
+      end
+    end
+
+  end
+end
diff --git a/server/tests/common.rb b/server/tests/common.rb
index e86f818..5b877f3 100644
--- a/server/tests/common.rb
+++ b/server/tests/common.rb
@@ -14,6 +14,8 @@
 # under the License.
 #
 
+ENV.delete 'API_VERBOSE'
+
 $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
 $top_srcdir = File::dirname(File::dirname(__FILE__))
 
@@ -26,7 +28,9 @@ require 'json'
 require 'digest/sha1'
 require 'base64'
 require 'rack/test'
-require 'deltacloud/server'
+
+server_dir = ENV['API_FRONTEND'] == 'cimi' ? 'cimi' : 'deltacloud'
+load File.join($top_srcdir, 'lib', server_dir, 'server.rb')
 
 driver
 
-- 
1.7.4.4