On Mon, 2012-11-05 at 17:12 +0200, [email protected] wrote:
> From: marios <[email protected]>
>
>
> Signed-off-by: marios <[email protected]>
> ---
> tests/cimi/networks_test.rb | 44 ++++++++++++++++++++++++++++++++++++++++++++
> tests/cimi/test_helper.rb | 43 ++++++++++++++++++++++++++++++++++++++-----
> 2 files changed, 82 insertions(+), 5 deletions(-)
> create mode 100644 tests/cimi/networks_test.rb
>
> diff --git a/tests/cimi/networks_test.rb b/tests/cimi/networks_test.rb
> new file mode 100644
> index 0000000..dc6a3cf
> --- /dev/null
> +++ b/tests/cimi/networks_test.rb
> +NETWORKS = "/networks"
> +test_collection = "networks"
> +describe "CIMI Networks collection" do
>+ include CIMI::Test::Methods
CIMI Tests should be subclasses of CIMI::Test::Spec (otherwise, we get
some weird constant sharing across tests); that also makes the include
necessary. Also, I've made up the convention that these tests should all
be called XXXBehavior ;) So this line should read
class NetworkCollectionBehavior < CIMI::Test::Spec
> + need_collection :networks
> +
> + it "must advertise the Networks collection in API entrypoint" do
> + res = cep.xml #get(NETWORKS).xml
> + (res/"networks").wont_be_empty
> + end
This check is already implied by the need_collection :networks.
> + #Run the common collection and member tests:
> + CommonCIMICollectionsTest::run_collection_and_member_tests_for("networks")
> +
> +
> +end
> diff --git a/tests/cimi/test_helper.rb b/tests/cimi/test_helper.rb
> index 8fd850a..adee8c3 100644
> --- a/tests/cimi/test_helper.rb
> +++ b/tests/cimi/test_helper.rb
> @@ -17,7 +17,7 @@
> require 'rubygems'
> require 'require_relative'
> require_relative '../helpers/common.rb'
> -
> +require_relative 'common_tests_cimi_collections.rb'
> require 'singleton'
> require_relative "../../server/lib/cimi/models"
>
> @@ -37,8 +37,18 @@ module CIMI
> @cimi["cep"]
> end
>
> + def cimi_root
> + @cimi["cep"].sub("/cloudEntryPoint", "")
> + end
We can not make any assumptions about URL structure; the CIMI conformant
way to get the baseURI is to look at the baseURI element in the CEP.
> + def basic_auth(u = nil, p = nil)
> + u ||= @cimi["user"]
> + p ||= @cimi["password"]
> + "Basic #{Base64.encode64("#{u}:#{p}")}"
> + end
> +
> def collections
> - xml.xpath("/c:CloudEntryPoint/c:*[@href]", ns).map { |c| c.name }
> + xml.xpath("/c:CloudEntryPoint/c:*[@href]", ns).map { |c|
> c.name.to_sym }
> end
>
> def features
> @@ -67,6 +77,14 @@ end
> module CIMI::Test::Methods
>
> module Global
> +
> + def each_collection_member(collection, &block)
> + res = get(collection)
> +
> res.xml.xpath("/xmlns:Collection/xmlns:#{collection.camelize.singularize}").each
> do |member|
> + yield member
> + end
> + end
> +
> def api
> CIMI::Test::config
> end
> @@ -76,18 +94,33 @@ module CIMI::Test::Methods
> end
>
> def get(path, params = {})
> - RestClient.get path, headers(params)
> + url, headers = url_headers(path, params)
> + RestClient.get url, headers
> end
>
> private
> - def headers(params)
> + def url_headers(path, params)
> headers = {}
> + unless params.delete(:noauth)
> + if params[:user]
> + u = params.delete(:user)
> + p = params.delete(:password)
> + headers['Authorization'] = api.basic_auth(u, p)
> + else
> + headers['Authorization'] = api.basic_auth
> + end
> + end
> if params[:accept]
> headers["Accept"] = "application/#{params.delete(:accept)}" if
> params[:accept]
> else #default to xml
> headers["Accept"] = "application/xml"
> end
> - headers
> + if path =~ /^https?:/
> + url = path
> + else
> + url = path.start_with?("/") ? api.cimi_root + path :
> api.cimi_root+"/#{path}"
> + end
> + [url, headers]
> end
> end
>