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/08 02:55:22 UTC

[PATCH 2/2] tests: very simple CIMI tests

From: David Lutterkort <lu...@redhat.com>

---
 tests/Rakefile            |    4 ++
 tests/cimi/cep_test.rb    |   38 ++++++++++++++++
 tests/cimi/test_helper.rb |  108 +++++++++++++++++++++++++++++++++++++++++++++
 tests/config.yaml         |    6 +++
 4 files changed, 156 insertions(+), 0 deletions(-)
 create mode 100644 tests/cimi/cep_test.rb
 create mode 100644 tests/cimi/test_helper.rb

diff --git a/tests/Rakefile b/tests/Rakefile
index 88c1966..f276dbb 100644
--- a/tests/Rakefile
+++ b/tests/Rakefile
@@ -30,4 +30,8 @@ namespace :test do
     end
   end
 
+  Rake::TestTask.new(:cimi) do |t|
+    t.test_files = FileList["cimi/*_test.rb"]
+  end
+
 end
diff --git a/tests/cimi/cep_test.rb b/tests/cimi/cep_test.rb
new file mode 100644
index 0000000..3dc2be5
--- /dev/null
+++ b/tests/cimi/cep_test.rb
@@ -0,0 +1,38 @@
+#
+# 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 "test_helper.rb"
+
+describe "CIMI Entry Point" do
+  include CIMI::Test::Methods
+
+  it "should return XML if asked to" do
+    res = cep :accept => :xml
+    res.headers[:content_type].must_equal "application/xml"
+    res.xml.root.name.must_equal "CloudEntryPoint"
+    names = res.xml.xpath("/c:CloudEntryPoint", api.ns).map { |e| e.name }
+    names.must_equal ["CloudEntryPoint"]
+    (res.xml/"CloudEntryPoint/id").text.must_equal api.cep_url
+  end
+
+  it "should return JSON if asked to" do
+    res = cep :accept => :json
+    res.headers[:content_type].must_equal "application/json"
+    res.json["id"].must_equal api.cep_url
+  end
+end
diff --git a/tests/cimi/test_helper.rb b/tests/cimi/test_helper.rb
new file mode 100644
index 0000000..f26fa96
--- /dev/null
+++ b/tests/cimi/test_helper.rb
@@ -0,0 +1,108 @@
+#
+# 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.
+
+require 'rubygems'
+require 'require_relative'
+require_relative '../helpers/common.rb'
+
+require 'singleton'
+
+# Add CIMI specific config stuff
+module CIMI
+  module Test
+    class Config
+
+      include Singleton
+
+      def initialize
+        @hash = Deltacloud::Test::yaml_config
+        @cimi = @hash["cimi"]
+      end
+
+      def cep_url
+        @cimi["cep"]
+      end
+
+      def collections
+        xml.xpath("/c:CloudEntryPoint/c:*[@href]", ns).map { |c| c.name }
+      end
+
+      def features
+        {}
+      end
+
+      def ns
+        { "c" => "http://www.dmtf.org/cimi" }
+      end
+
+      private
+      def xml
+        unless @xml
+          @xml = RestClient.get(cep_url, "Accept" => "application/xml").xml
+        end
+        @xml
+      end
+    end
+
+    def self.config
+      Config::instance
+    end
+  end
+end
+
+module CIMI::Test::Methods
+
+  module Global
+    def api
+      CIMI::Test::config
+    end
+
+    def cep(params = {})
+      get(api.cep_url, params)
+    end
+
+    def get(path, params = {})
+      RestClient.get path, headers(params)
+    end
+
+    private
+    def headers(params)
+      headers = {}
+      if params[:accept]
+        headers["Accept"] = "application/#{params.delete(:accept)}" if params[:accept]
+      else #default to xml
+        headers["Accept"] = "application/xml"
+      end
+      headers
+    end
+  end
+
+  module ClassMethods
+    def need_collection(name)
+      before :each do
+        unless api.collections.include?(name.to_sym)
+          skip "Server at #{api.cep_url} doesn't support #{name}"
+        end
+      end
+    end
+  end
+
+  def self.included(base)
+    base.extend ClassMethods
+    base.extend Global
+    base.send(:include, Global)
+  end
+end
diff --git a/tests/config.yaml b/tests/config.yaml
index 5f905a6..1651261 100644
--- a/tests/config.yaml
+++ b/tests/config.yaml
@@ -29,3 +29,9 @@ ec2:
     preferred_image: "ami-2b5fba42"
     preferred_hwp: "m1.small"
     preferred_realm: "us-east-1b"
+
+# CIMI testing
+cimi:
+  cep: "http://localhost:3001/cimi/cloudEntryPoint"
+  user: "mockuser"
+  password: "mockpassword"
-- 
1.7.7.6