You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by lu...@redhat.com on 2012/08/30 01:37:43 UTC
CIMI: test harness and basic CEP tests
These patches establish a test harness for CIMI tests that makes it
possible to run the same tests with XML and JSON; see
tests/cimi/cep_test.rb for how to write them.
Right now, the special CIMI harness is triggered by giving the tests a name
ending in 'Behavior' - better ways to trigger that would be much
appreciated.
David
[PATCH 3/3] CIMI tests: revamp to allow same test against XML and JSON output
Posted by lu...@redhat.com.
From: David Lutterkort <lu...@redhat.com>
- use CIMI models to represent data retrieved from server
- add special Spec class to run each spec twice: once
using XML, once using JSON
---
server/lib/cimi/models/schema.rb | 2 +
tests/cimi/cep_test.rb | 58 ++++++++-----------------------------
tests/cimi/test_helper.rb | 59 ++++++++++++++++++++++++++++++++++++++
3 files changed, 73 insertions(+), 46 deletions(-)
diff --git a/server/lib/cimi/models/schema.rb b/server/lib/cimi/models/schema.rb
index a0e4637..57a190d 100644
--- a/server/lib/cimi/models/schema.rb
+++ b/server/lib/cimi/models/schema.rb
@@ -14,6 +14,8 @@
# under the License.
#
+require_relative "../../deltacloud/core_ext"
+
# The smarts of converting from XML and JSON into internal objects
class CIMI::Model::Schema
diff --git a/tests/cimi/cep_test.rb b/tests/cimi/cep_test.rb
index 8db3986..fcce068 100644
--- a/tests/cimi/cep_test.rb
+++ b/tests/cimi/cep_test.rb
@@ -18,55 +18,21 @@ $:.unshift File.join(File.dirname(__FILE__))
require "test_helper.rb"
-describe "CIMI Entry Point" do
- include CIMI::Test::Methods
-
- describe "XML form" do
- # Cache the response for all assertions
- res = cep(:accept => :xml)
-
- it "should set the proper content type" do
- res.headers[:content_type].must_equal "application/xml"
- end
-
- it "should use CloudEntryPoint as the XML root" do
- res.xml.root.name.must_equal "CloudEntryPoint"
- names = res.xml.xpath("/c:CloudEntryPoint", api.ns).map { |e| e.name }
- names.must_equal ["CloudEntryPoint"]
- end
-
- it "should have an id equal to the CEP URL" do
- (res.xml/"CloudEntryPoint/id").text.must_equal api.cep_url
- end
-
- it "should have a baseURI" do
- (res.xml/"CloudEntryPoint/baseURI").text.must_be_uri
- end
-
- it "should have a name" do
- (res.xml/"CloudEntryPoint/name").wont_be_empty
- end
+describe "CIMI Entry Point Behavior" do
+ # We'd like to call this :cep, but there's already a method by that name
+ model :subject, CIMI::Model::CloudEntryPoint, :cache => true do |fmt|
+ cep(:accept => fmt)
end
- describe "JSON form" do
- # Cache the response for all assertions
- res = cep(:accept => :json)
-
- it "should set the proper content type" do
- res.headers[:content_type].must_equal "application/json"
- end
-
- it "should return JSON if asked to" do
- res.headers[:content_type].must_equal "application/json"
- res.json["id"].must_equal api.cep_url
- end
+ it "should have an id equal to the CEP URL" do
+ subject.id.must_equal api.cep_url
+ end
- it "should have a baseURI" do
- res.json["baseURI"].must_be_uri
- end
+ it "should have a baseURI" do
+ subject.base_uri.must_be_uri
+ end
- it "should have a name" do
- res.json["name"].wont_be_empty
- end
+ it "should have a name" do
+ subject.name.wont_be_empty
end
end
diff --git a/tests/cimi/test_helper.rb b/tests/cimi/test_helper.rb
index f26fa96..8e3ed9f 100644
--- a/tests/cimi/test_helper.rb
+++ b/tests/cimi/test_helper.rb
@@ -19,6 +19,7 @@ require 'require_relative'
require_relative '../helpers/common.rb'
require 'singleton'
+require_relative "../../server/lib/cimi/models"
# Add CIMI specific config stuff
module CIMI
@@ -106,3 +107,61 @@ module CIMI::Test::Methods
base.send(:include, Global)
end
end
+
+# Special spec class for 'behavior' tests that need to be run once
+# for XML and once for JSON
+class CIMI::Test::Spec < MiniTest::Spec
+ include CIMI::Test::Methods
+
+ CONTENT_TYPES = { :xml => "application/xml",
+ :json => "application/json" }
+
+ def use_format(fmt)
+ @format = fmt
+ @content_type = CONTENT_TYPES[fmt]
+ end
+
+ def self.it desc = "anonymous", &block
+ block ||= proc { skip "(no tests defined)" }
+
+ CONTENT_TYPES.keys.each do |fmt|
+ super("#{desc} [#{fmt}]") do
+ use_format(fmt)
+ instance_eval &block
+ end
+ end
+ end
+
+ def self.model(name, model_class, opts = {}, &block)
+ define_method name do
+ @_memoized ||= {}
+ @@_cache ||= {}
+ @_memoized.fetch("#{name}_#{@format}") do |k|
+ if opts[:cache]
+ @_memoized[k] = @@_cache.fetch(k) do |k|
+ @@_cache[k] = fetch_model(k, model_class, &block)
+ end
+ else
+ @_memoized[k] = fetch_model(k, model_class, &block)
+ end
+ end
+ end
+ end
+
+ def last_response
+ @@_cache ||= {}
+ @@_cache[:last_response]
+ end
+
+ private
+
+ def fetch_model(k, model_class, &block)
+ response = instance_exec(@format, &block)
+ @@_cache[:last_response] = response
+ assert_equal @content_type, response.headers[:content_type]
+ # FIXME: for XML check that the correct namespace is set
+ model_class.parse(response.body, @content_type)
+ end
+end
+
+MiniTest::Spec.register_spec_type(/Behavior$/, CIMI::Test::Spec)
--
1.7.7.6
[PATCH 2/3] CIMI base model: add 'parse' method
Posted by lu...@redhat.com.
From: David Lutterkort <lu...@redhat.com>
Dispatch between from_xml and from_json depending on content type
---
server/lib/cimi/models/base.rb | 10 ++++++++++
1 files changed, 10 insertions(+), 0 deletions(-)
diff --git a/server/lib/cimi/models/base.rb b/server/lib/cimi/models/base.rb
index 9e4b286..b375ce0 100644
--- a/server/lib/cimi/models/base.rb
+++ b/server/lib/cimi/models/base.rb
@@ -168,6 +168,16 @@ class CIMI::Model::Base
model
end
+ def self.parse(text, content_type)
+ if content_type == "application/xml"
+ from_xml(text)
+ elsif content_type == "application/json"
+ from_json(text)
+ else
+ raise "Can not parse content type #{content_type}"
+ end
+ end
+
#
# Serialize
#
--
1.7.7.6
[PATCH 1/3] CIMI base model: no need to quote symbol names
Posted by lu...@redhat.com.
From: David Lutterkort <lu...@redhat.com>
---
server/lib/cimi/models/base.rb | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/server/lib/cimi/models/base.rb b/server/lib/cimi/models/base.rb
index a41fb21..9e4b286 100644
--- a/server/lib/cimi/models/base.rb
+++ b/server/lib/cimi/models/base.rb
@@ -107,7 +107,7 @@ class CIMI::Model::Base
@schema_duped
end
- private :'clone_base_schema', :'base_schema_cloned?'
+ private :clone_base_schema, :base_schema_cloned?
def inherited(child)
child.instance_eval do
--
1.7.7.6