From: Petr Blaho <pblaho(a)redhat.com>
https://www.aeolusproject.org/redmine/issues/2488
Add tests for Aeolus::Image::Warehouse::TargetImage
Add tests for Aeolus::Image::Warehouse::ProviderImage
Add tests for Aeolus::Image::Warehouse::ImageBuild
Add tests for Aeolus::Image::Warehouse::Image
Add tests for Aeolus::Image::Warehouse::Template
Add tests for Aeolus::Image::Warehouse::WarehouseModel
Add tests for Aeolus::Image::Warehouse::WarehouseBucketObject
Add tests for Aeolus::Image::Warehouse::WarehouseBucket
Add tests for Aeolus::Image::Warehouse::WarehouseConnection
Add tests for Aeolus::Image::Warehouse::WarehouseClient
---
spec/models/warehouse/image_build_spec.rb | 205 +++++++++++
spec/models/warehouse/image_spec.rb | 242 +++++++++++++
spec/models/warehouse/provider_image_spec.rb | 115 ++++++
spec/models/warehouse/target_image_spec.rb | 186 ++++++++++
spec/models/warehouse/template_spec.rb | 76 ++++
spec/models/warehouse/warehouse_client_spec.rb | 445 ++++++++++++++++++++++++
spec/models/warehouse/warehouse_model_spec.rb | 94 +++++
spec/spec_helper.rb | 19 +
8 files changed, 1382 insertions(+), 0 deletions(-)
create mode 100644 spec/models/warehouse/image_build_spec.rb
create mode 100644 spec/models/warehouse/image_spec.rb
create mode 100644 spec/models/warehouse/provider_image_spec.rb
create mode 100644 spec/models/warehouse/target_image_spec.rb
create mode 100644 spec/models/warehouse/template_spec.rb
create mode 100644 spec/models/warehouse/warehouse_client_spec.rb
create mode 100644 spec/models/warehouse/warehouse_model_spec.rb
diff --git a/spec/models/warehouse/image_build_spec.rb
b/spec/models/warehouse/image_build_spec.rb
new file mode 100644
index 0000000..a49e0b7
--- /dev/null
+++ b/spec/models/warehouse/image_build_spec.rb
@@ -0,0 +1,205 @@
+# Copyright 2011 Red Hat, Inc.
+#
+# Licensed 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 'spec_helper'
+
+module Aeolus
+ module Image
+ module Warehouse
+ describe ImageBuild do
+ before(:each) do
+ @image_build_attributes = {
+ :image => 'image',
+ :uuid => 'uuid',
+ :template => 'template',
+ :other_attribute => 'other_attribute',
+ :another_attribute => 'another_attribute'
+ }
+ @object = mock(Object, :attr_list => @image_build_attributes.keys, :attrs
=> @image_build_attributes)
+ @image_build_attributes.each do |key, value|
+ @object.stub(key.to_sym, value)
+ end
+ @image_build = ImageBuild.new(@object)
+
+
+ @target_image_mock_with_no_build = mock(TargetImage, :build => nil)
+ @target_image_mock_with_correct_build = mock(TargetImage, :build =>
@image_build)
+ @other_target_image_mock_with_correct_build = mock(TargetImage, :build =>
@image_build)
+
+ @other_image_build_attributes = @image_build_attributes.merge(:uuid =>
'other_uuid')
+ @other_object = mock(Object, :attr_list =>
@other_image_build_attributes.keys, :attrs => @other_image_build_attributes)
+ @other_image_build_attributes.each do |key, value|
+ @other_object.stub(key.to_sym, value)
+ end
+ @other_image_build = ImageBuild.new(@other_object)
+
+ @target_image_mock_with_other_image_build = mock(TargetImage, :build =>
@other_image_build)
+
+ @all_target_images = [ @target_image_mock_with_no_build,
@target_image_mock_with_correct_build, @other_target_image_mock_with_correct_build,
@target_image_mock_with_other_image_build ]
+
+ @correct_target_images = [ @target_image_mock_with_correct_build,
@other_target_image_mock_with_correct_build ]
+
+ @first_target_image_provider_images = []
+ 3.times do
+ @first_target_image_provider_images << mock(ProviderImage,
:target_image => @target_image_mock_with_correct_build)
+ end
+
+ @second_target_image_provider_images = []
+ 3.times do
+ @second_target_image_provider_images << mock(ProviderImage,
:target_image => @other_target_image_mock_with_correct_build)
+ end
+
+ @incorrect_target_image_provider_images = []
+ 3.times do
+ @incorrect_target_image_provider_images << mock(ProviderImage,
:target_image => @target_image_mock_with_other_image_build)
+ end
+
+ @correct_provider_images = @first_target_image_provider_images +
@second_target_image_provider_images
+ @incorrect_provider_images = @incorrect_target_image_provider_images
+ @all_provider_images = @correct_provider_images + @incorrect_provider_images
+
+ end
+
+ context "@bucket_name" do
+
+ it "should be set correctly" do
+ # accessor set in WarehouseModel
+ @image_build.class.bucket_name.should be_eql('builds')
+ end
+
+ end
+
+ context "#initialize" do
+
+ before(:each) do
+ @attr_writers = [ :image ]
+ @attr_accessors = @image_build_attributes.keys - @attr_writers
+ end
+
+ it "should correctly set attribute writers" do
+ @attr_writers.each do |writer|
+ @image_build.respond_to?(:"#{writer.to_s}=").should be_true
+ end
+ end
+
+ it "should correctly set attribute accessors" do
+ @attr_accessors.each do |accessor|
+ @image_build.respond_to?(:"#{accessor.to_s}").should be_true
+ @image_build.respond_to?(:"#{accessor.to_s}=").should be_true
+ end
+ end
+
+ it "should set attributes to correct values" do
+ @attr_accessors.each do |key|
+ @image_build.send(:"#{key.to_s}").should
be_equal(@image_build_attributes[key])
+ end
+ end
+
+ end
+
+ #TODO: implement this test
+ context ".find_all_by_image_uuid" do
+
+ end
+
+ context "#image" do
+ context "with @image present" do
+ before(:each) do
+ @image_mock = mock(Image)
+ Image.stub(:find).and_return(@image_mock)
+ end
+ it "should call Image.find with correct parameter" do
+ Image.should_receive(:find).with(@image_build.instance_variable_get(
:@image ))
+ @image_build.image
+ end
+
+ it "should return found Image" do
+ @image_build.image.should be_eql(@image_mock)
+ end
+ end
+
+ context "with @image absent" do
+ before(:each) do
+ @image_build.instance_variable_set(:@image, nil)
+ end
+ it "should not call Image.find at all" do
+ Image.should_not_receive(:find)
+ @image_build.image
+ end
+ end
+ end
+
+ context "#target_images" do
+ before(:each) do
+ TargetImage.stub(:all).and_return(@all_target_images)
+ end
+
+ context "should return collection" do
+ it "with correct target image" do
+ @image_build.target_images.should
include(@target_image_mock_with_correct_build)
+ end
+ it "with other correct target image" do
+ @image_build.target_images.should
include(@other_target_image_mock_with_correct_build)
+ end
+ it "without target image with other build" do
+ @image_build.target_images.should_not
include(@target_image_mock_with_other_image_build)
+ end
+ it "without target image without build" do
+ @image_build.target_images.should_not
include(@target_image_mock_with_no_build)
+ end
+ end
+ end
+
+
+ context "#provider_images" do
+ before(:each) do
+ @image_build.stub(:target_images).and_return(@correct_target_images)
+ ProviderImage.stub(:all).and_return(@all_provider_images)
+ end
+
+ context "should return collection" do
+ it "with correct provider images" do
+ @correct_provider_images.each do |provider_image|
+ @image_build.provider_images.should include(provider_image)
+ end
+ end
+ it "without provider image with other target image" do
+ @incorrect_provider_images.each do |provider_image|
+ @image_build.provider_images.should_not include(provider_image)
+ end
+ end
+ end
+ end
+
+ context "#delete!" do
+
+ before(:each) do
+ @correct_target_images.each{|ti| ti.stub(:delete!)}
+ @image_build.stub(:target_images).and_return(@correct_target_images)
+ ImageBuild.stub(:delete)
+ end
+ it "should delete all associated provider images" do
+ @correct_target_images.each{|ti| ti.should_receive(:delete!)}
+ @image_build.delete!
+ end
+ it "should call TargetImage.delete with @uuid" do
+
ImageBuild.should_receive(:delete).with(@image_build.instance_variable_get(:@uuid))
+ @image_build.delete!
+ end
+
+ end
+ end
+ end
+ end
+end
diff --git a/spec/models/warehouse/image_spec.rb b/spec/models/warehouse/image_spec.rb
new file mode 100644
index 0000000..b204eca
--- /dev/null
+++ b/spec/models/warehouse/image_spec.rb
@@ -0,0 +1,242 @@
+# Copyright 2011 Red Hat, Inc.
+#
+# Licensed 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 'spec_helper'
+
+module Aeolus
+ module Image
+ module Warehouse
+ describe Image do
+ let(:body) { 'body' }
+ before(:each) do
+ @image_attributes = {
+ :latest_build => 'latest_build',
+ :uuid => 'uuid',
+ :body => body,
+ :other_attribute => 'other_attribute',
+ :another_attribute => 'another_attribute'
+ }
+ @object = mock(Object, :attr_list => @image_attributes.keys, :attrs =>
@image_attributes)
+ @image_attributes.each do |key, value|
+ @object.stub(key.to_sym, value)
+ end
+ @image = Image.new(@object)
+
+ end
+
+ context "@bucket_name" do
+
+ it "should be set correctly" do
+ # accessor set in WarehouseModel
+ @image.class.bucket_name.should be_eql('images')
+ end
+
+ end
+
+ context "#initialize" do
+
+ before(:each) do
+ @attr_writers = [ :latest_build ]
+ @attr_accessors = @image_attributes.keys - @attr_writers
+ end
+
+ it "should correctly set attribute writers" do
+ @attr_writers.each do |writer|
+ @image.respond_to?(:"#{writer.to_s}=").should be_true
+ end
+ end
+
+ it "should correctly set attribute accessors" do
+ @attr_accessors.each do |accessor|
+ @image.respond_to?(:"#{accessor.to_s}").should be_true
+ @image.respond_to?(:"#{accessor.to_s}=").should be_true
+ end
+ end
+
+ it "should set attributes to correct values" do
+ @attr_accessors.each do |key|
+ @image.send(:"#{key.to_s}").should
be_equal(@image_attributes[key])
+ end
+ end
+
+ end
+
+ context "#template_xml" do
+
+ before(:each) do
+ @body =
"<template><item>X</item></template>"
+ @template_xml = Nokogiri::XML(@body)
+
+ @empty_body = "<template></template>"
+ @empty_tempalte_xml = Nokogiri::XML(@empty_body)
+ end
+
+ context "with correct associated objects" do
+
+ before(:each) do
+ @target_template = mock(Template, :body => @template_xml.to_s)
+ @target_image = [ mock(TargetImage, :target_template =>
@target_template) ]
+ @image_builds = [ mock(ImageBuild, :target_images => @target_image) ]
+ @image.stub(:image_builds).and_return(@image_builds)
+ end
+
+ # TODO: There shoud be a way to test equality of Nokogiri::XML documents
better than string comparison
+ it "should return correct template" do
+ @image.template_xml.to_s.should be_eql((a)template_xml.to_s)
+ end
+
+ end
+
+ context "with incorect associated object" do
+
+ before(:each) do
+ @image.stub(:image_builds).and_return(nil)
+ end
+
+ # TODO: There shoud be a way to test equality of Nokogiri::XML documents
better than string comparison
+ it "should return empty template" do
+ @image.template_xml.to_s.should be_eql((a)empty_tempalte_xml.to_s)
+ end
+ end
+ end
+
+ context "#name" do
+ subject { @image.name }
+ context "with /image/name text value defined in body" do
+ let(:body) {
"<image><name>image-name-string</name></image>" }
+ before(:each) do
+ @image.instance_variable_set(:@xml_body, Nokogiri::XML(body))
+ end
+
+ it "should return /image/name text value" do
+ puts subject
+ should be_eql('image-name-string')
+ end
+ end
+
+ context "without /image/name text value defined in body" do
+ before(:each) do
+ @image.stub(:template_xml).and_return(Nokogiri::XML(template_xml))
+ end
+ context "with /template/name text value from template_xml" do
+ let(:template_xml) {
"<template><name>template-name-string</name></template>"
}
+ it "should return /template/name text value" do
+ should be_eql('template-name-string')
+ end
+ end
+
+ context "without /template/name text value from template_xml" do
+ let(:template_xml) { "<template></template>" }
+ it "should return empty string" do
+ should be_eql("")
+ end
+ end
+ end
+ end
+
+ context "#os" do
+ subject { @image.os }
+ let(:template_xml) {
"<template><os><name>template-os-name</name><version>template-os-version</version><arch>template-os-arch</arch></os></template>"
}
+ before(:each) do
+ @image.stub(:template_xml).and_return(Nokogiri::XML(template_xml))
+ end
+
+ it "should return correct OS struct" do
+ subject.name.should be_eql("template-os-name")
+ subject.version.should be_eql("template-os-version")
+ subject.arch.should be_eql("template-os-arch")
+ end
+ end
+
+ context "#description" do
+
+ subject { @image.description }
+ let(:template_xml) {
"<template><description>template-description-string</description></template>"
}
+
+ before(:each) do
+ @image.stub(:template_xml).and_return(Nokogiri::XML(template_xml))
+ end
+
+ it "should return /template/description text value from template_xml"
do
+ should be_eql("template-description-string")
+ end
+
+ end
+
+ context "#latest_pushed_build" do
+
+ context "with @latest_build present" do
+ before(:each) do
+ @latest_build_mock = mock(ImageBuild)
+ ImageBuild.stub(:find).and_return(@latest_build_mock)
+ end
+ it "should call ImageBuild.find with correct parameter" do
+ ImageBuild.should_receive(:find).with(@image.instance_variable_get(
:@latest_build ))
+ @image.latest_pushed_build
+ end
+
+ it "should return found ImageBuild" do
+ @image.latest_pushed_build.should be_eql(@latest_build_mock)
+ end
+ end
+
+ context "with @latest_build absent" do
+ before(:each) do
+ @image.instance_variable_set(:@latest_build, nil)
+ end
+ it "should not call ImageBuild.find at all" do
+ ImageBuild.should_not_receive(:find)
+ @image.latest_pushed_build
+ end
+ end
+ end
+
+ context "#image_builds" do
+
+ before(:each) do
+ ImageBuild.stub(:find_all_by_image_uuid)
+ end
+
+ it "should call ImageBuild.find_all_by_image_uuid with @uuid" do
+
ImageBuild.should_receive(:find_all_by_image_uuid).with(@image.instance_variable_get(:@uuid))
+ @image.image_builds
+ end
+
+ end
+
+ context "#delete!" do
+
+ before(:each) do
+ @image_builds = [ mock(ImageBuild) ]
+ @image_builds.each{|ib| ib.stub(:delete!)}
+ @image.stub(:image_builds).and_return(@image_builds)
+ Image.stub(:delete)
+ end
+
+ it "should delete all associated image builds" do
+ @image_builds.each{|im| im.should_receive(:delete!)}
+ @image.delete!
+ end
+
+ it "should call Image.delete with @uuid" do
+ Image.should_receive(:delete).with(@image.instance_variable_get(:@uuid))
+ @image.delete!
+ end
+
+ end
+
+ end
+ end
+ end
+end
diff --git a/spec/models/warehouse/provider_image_spec.rb
b/spec/models/warehouse/provider_image_spec.rb
new file mode 100644
index 0000000..85f1a3f
--- /dev/null
+++ b/spec/models/warehouse/provider_image_spec.rb
@@ -0,0 +1,115 @@
+# Copyright 2011 Red Hat, Inc.
+#
+# Licensed 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 'spec_helper'
+
+module Aeolus
+ module Image
+ module Warehouse
+ describe ProviderImage do
+ before(:each) do
+ @provider_image_attributes = {
+ :provider => 'provider',
+ :target_image => 'target_image',
+ :uuid => 'uuid',
+ :other_attribute => 'other_attribute',
+ :another_attribute => 'another_attribute'
+ }
+ @object = mock(Object, :attr_list => @provider_image_attributes.keys, :attrs
=> @provider_image_attributes)
+ @provider_image_attributes.each do |key, value|
+ @object.stub(key.to_sym, value)
+ end
+ @provider_image = ProviderImage.new(@object)
+ end
+
+ context "@bucket_name" do
+
+ it "should be set correctly" do
+ # accessor set in WarehouseModel
+ @provider_image.class.bucket_name.should be_eql('provider_images')
+ end
+
+ end
+
+ context "#initialize" do
+
+ before(:each) do
+ @attr_writers = [ :provider, :target_image ]
+ @attr_accessors = @provider_image_attributes.keys - @attr_writers
+ end
+
+ it "should correctly set attribute writers" do
+ @attr_writers.each do |writer|
+ @provider_image.respond_to?(:"#{writer.to_s}=").should be_true
+ end
+ end
+
+ it "should correctly set attribute accessors" do
+ @attr_accessors.each do |accessor|
+ @provider_image.respond_to?(:"#{accessor.to_s}").should be_true
+ @provider_image.respond_to?(:"#{accessor.to_s}=").should be_true
+ end
+ end
+
+ it "should set attributes to correct values" do
+ @attr_accessors.each do |key|
+ @provider_image.send(:"#{key.to_s}").should
be_equal(@provider_image_attributes[key])
+ end
+ end
+
+ end
+
+ context "#target_image" do
+ context "with @target_image present" do
+ before(:each) do
+ @target_image_mock = mock(TargetImage)
+ TargetImage.stub(:find).and_return(@target_image_mock)
+ end
+ it "should call TargetImage.find with correct parameter" do
+
TargetImage.should_receive(:find).with(@provider_image.instance_variable_get(
:@target_image ))
+ @provider_image.target_image
+ end
+
+ it "should return found TargetImage" do
+ @provider_image.target_image.should be_eql(@target_image_mock)
+ end
+ end
+
+ context "with @target_image absent" do
+ before(:each) do
+ @provider_image.instance_variable_set(:@target_image, nil)
+ end
+ it "should not call TargetImage.find at all" do
+ TargetImage.should_not_receive(:find)
+ @provider_image.target_image
+ end
+ end
+ end
+
+ context "#provider_name" do
+ it "should return @provider" do
+ @provider_image.provider_name.should
be_eql(@provider_image.instance_variable_get(:@provider))
+ end
+ end
+
+ context "#delete!" do
+ it "should call ProviderImage.delete with @uuid" do
+
ProviderImage.should_receive(:delete).with(@provider_image.instance_variable_get(:@uuid))
+ @provider_image.delete!
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spec/models/warehouse/target_image_spec.rb
b/spec/models/warehouse/target_image_spec.rb
new file mode 100644
index 0000000..e34cdc9
--- /dev/null
+++ b/spec/models/warehouse/target_image_spec.rb
@@ -0,0 +1,186 @@
+# Copyright 2011 Red Hat, Inc.
+#
+# Licensed 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 'spec_helper'
+
+module Aeolus
+ module Image
+ module Warehouse
+ describe TargetImage do
+ before(:each) do
+ @target_image_attributes = {
+ :build => 'build',
+ :uuid => 'uuid',
+ :template => 'template',
+ :other_attribute => 'other_attribute',
+ :another_attribute => 'another_attribute'
+ }
+ @object = mock(Object, :attr_list => @target_image_attributes.keys, :attrs
=> @target_image_attributes)
+ @target_image_attributes.each do |key, value|
+ @object.stub(key.to_sym, value)
+ end
+ @target_image = TargetImage.new(@object)
+
+
+ @provider_image_mock_with_no_target_image = mock(ProviderImage, :target_image
=> nil)
+ @provider_image_mock_with_correct_target_image = mock(ProviderImage,
:target_image => @target_image)
+ @other_provider_image_mock_with_correct_target_image = mock(ProviderImage,
:target_image => @target_image)
+
+ @other_target_image_attributes = @target_image_attributes.merge(:uuid =>
'other_uuid')
+ @other_object = mock(Object, :attr_list =>
@other_target_image_attributes.keys, :attrs => @other_target_image_attributes)
+ @other_target_image_attributes.each do |key, value|
+ @other_object.stub(key.to_sym, value)
+ end
+ @other_target_image = TargetImage.new(@other_object)
+ @provider_image_mock_with_other_target_image = mock(ProviderImage,
:target_image => @other_target_image)
+
+ @all_provider_images = [ @provider_image_mock_with_correct_target_image,
@other_provider_image_mock_with_correct_target_image,
@provider_image_mock_with_other_target_image, @provider_image_mock_with_no_target_image ]
+
+ @correct_provider_images = [ @provider_image_mock_with_correct_target_image,
@other_provider_image_mock_with_correct_target_image ]
+
+ end
+
+ context "@bucket_name" do
+
+ it "should be set correctly" do
+ # accessor set in WarehouseModel
+ @target_image.class.bucket_name.should be_eql('target_images')
+ end
+
+ end
+
+ context "#initialize" do
+
+ before(:each) do
+ @attr_writers = [ :build ]
+ @attr_accessors = @target_image_attributes.keys - @attr_writers
+ end
+
+ it "should correctly set attribute writers" do
+ @attr_writers.each do |writer|
+ @target_image.respond_to?(:"#{writer.to_s}=").should be_true
+ end
+ end
+
+ it "should correctly set attribute accessors" do
+ @attr_accessors.each do |accessor|
+ @target_image.respond_to?(:"#{accessor.to_s}").should be_true
+ @target_image.respond_to?(:"#{accessor.to_s}=").should be_true
+ end
+ end
+
+ it "should set attributes to correct values" do
+ @attr_accessors.each do |key|
+ @target_image.send(:"#{key.to_s}").should
be_equal(@target_image_attributes[key])
+ end
+ end
+
+ end
+
+ context "#build" do
+ context "with @build present" do
+ before(:each) do
+ @image_build_mock = mock(ImageBuild)
+ ImageBuild.stub(:find).and_return(@image_build_mock)
+ end
+ it "should call TargetImage.find with correct parameter" do
+ ImageBuild.should_receive(:find).with(@target_image.instance_variable_get(
:@build ))
+ @target_image.build
+ end
+
+ it "should return found TargetImage" do
+ @target_image.build.should be_eql(@image_build_mock)
+ end
+ end
+
+ context "with @build absent" do
+ before(:each) do
+ @target_image.instance_variable_set(:@build, nil)
+ end
+ it "should not call TargetImage.find at all" do
+ ImageBuild.should_not_receive(:find)
+ @target_image.build
+ end
+ end
+ end
+
+ context "#provider_images" do
+ before(:each) do
+ ProviderImage.stub(:all).and_return(@all_provider_images)
+ end
+
+ context "should return collection" do
+ it "with correct provider image" do
+ @target_image.provider_images.should
include(@provider_image_mock_with_correct_target_image)
+ end
+ it "with other correct provider image" do
+ @target_image.provider_images.should
include(@other_provider_image_mock_with_correct_target_image)
+ end
+ it "without provider image with other target image" do
+ @target_image.provider_images.should_not
include(@provider_image_mock_with_other_target_image)
+ end
+ it "without provider image without target image" do
+ @target_image.provider_images.should_not
include(@provider_image_mock_with_no_target_image)
+ end
+ end
+ end
+
+ context "#template" do
+ context "with @template present" do
+ before(:each) do
+ @template_mock = mock(Template)
+ Template.stub(:find).and_return(@template_mock)
+ end
+ it "should call Template.find with correct parameter" do
+ Template.should_receive(:find).with(@target_image.instance_variable_get(
:@template ))
+ @target_image.target_template
+ end
+
+ it "should return found Template" do
+ @target_image.target_template.should be_eql(@template_mock)
+ end
+ end
+
+ context "with @template absent" do
+ before(:each) do
+ @target_image.instance_variable_set(:@template, nil)
+ end
+ it "should not call Template.find at all" do
+ Template.should_not_receive(:find)
+ @target_image.target_template
+ end
+ end
+ end
+
+ context "#delete!" do
+
+ before(:each) do
+ @correct_provider_images.each{|pi| pi.stub(:delete!)}
+ @target_image.stub(:provider_images).and_return(@correct_provider_images)
+ TargetImage.stub(:delete)
+ end
+ it "should delete all associated provider images" do
+ @correct_provider_images.each{|pi| pi.should_receive(:delete!)}
+ @target_image.delete!
+ end
+ it "should call TargetImage.delete with @uuid" do
+
TargetImage.should_receive(:delete).with(@target_image.instance_variable_get(:@uuid))
+ @target_image.delete!
+ end
+
+ end
+ end
+ end
+ end
+end
diff --git a/spec/models/warehouse/template_spec.rb
b/spec/models/warehouse/template_spec.rb
new file mode 100644
index 0000000..ddd7faf
--- /dev/null
+++ b/spec/models/warehouse/template_spec.rb
@@ -0,0 +1,76 @@
+# Copyright 2011 Red Hat, Inc.
+#
+# Licensed 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 'spec_helper'
+
+module Aeolus
+ module Image
+ module Warehouse
+ describe Template do
+ let(:body) { 'body' }
+ before(:each) do
+ @template_attributes = {
+ :attribute => 'attribute',
+ :other_attribute => 'other_attribute',
+ :another_attribute => 'another_attribute'
+ }
+ @object = mock(Object, :attr_list => @template_attributes.keys, :attrs =>
@template_attributes, :body => 'body')
+ @template_attributes.each do |key, value|
+ @object.stub(key.to_sym, value)
+ end
+ @template = Template.new(@object)
+
+ end
+
+ context "@bucket_name" do
+
+ it "should be set correctly" do
+ # accessor set in WarehouseModel
+ @template.class.bucket_name.should be_eql('templates')
+ end
+
+ end
+
+ context "#initialize" do
+
+ before(:each) do
+ @attr_writers = []
+ @attr_accessors = @template_attributes.keys - @attr_writers
+ end
+
+ it "should correctly set attribute writers" do
+ @attr_writers.each do |writer|
+ @template.respond_to?(:"#{writer.to_s}=").should be_true
+ end
+ end
+
+ it "should correctly set attribute accessors" do
+ @attr_accessors.each do |accessor|
+ @template.respond_to?(:"#{accessor.to_s}").should be_true
+ @template.respond_to?(:"#{accessor.to_s}=").should be_true
+ end
+ end
+
+ it "should set attributes to correct values" do
+ @attr_accessors.each do |key|
+ puts @template.send(:"#{key.to_s}")
+ @template.send(:"#{key.to_s}").should
be_equal(@template_attributes[key])
+ end
+ end
+
+ end
+ end
+ end
+ end
+end
diff --git a/spec/models/warehouse/warehouse_client_spec.rb
b/spec/models/warehouse/warehouse_client_spec.rb
new file mode 100644
index 0000000..797f92a
--- /dev/null
+++ b/spec/models/warehouse/warehouse_client_spec.rb
@@ -0,0 +1,445 @@
+# Copyright 2011 Red Hat, Inc.
+#
+# Licensed 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 'spec_helper'
+
+module Aeolus
+ module Image
+ module Warehouse
+ describe BucketObject do
+ let(:connection) { 'connection' }
+ let(:key) { 'key' }
+ let(:bucket) { mock(Bucket, :name => 'bucket_name') }
+
+ subject { BucketObject.new( connection, key, bucket ) }
+
+ context "#initialize" do
+
+ it "should set instance varibales correctly" do
+ subject.instance_variable_get(:@connection).should be_eql(connection)
+ subject.instance_variable_get(:@key).should be_eql(key)
+ subject.instance_variable_get(:@bucket).should be_eql(bucket)
+ subject.instance_variable_get(:@path).should
be_eql('/bucket_name/key')
+ end
+ end
+
+ context ".create" do
+
+ pending
+
+ end
+
+ context "#body" do
+
+ let(:connection_hash) { { :plain => true } }
+
+ it "should call @connection.do_request with correct parameters" do
+
subject.instance_variable_get(:@connection).should_receive(:do_request).with(subject.instance_variable_get(:@path),
connection_hash )
+ subject.body
+ end
+ end
+
+ context "#set_body" do
+
+ let(:body) { 'body' }
+ let(:connection_hash) { { :content => body, :method => :put } }
+
+ it "should call @connection.do_request with correct parameters" do
+
subject.instance_variable_get(:@connection).should_receive(:do_request).with(subject.instance_variable_get(:@path),
connection_hash )
+ subject.set_body(body)
+ end
+
+ end
+
+ context "#attr_list" do
+
+ let(:connection_hash) { { :content => 'op=parts', :method =>
:post } }
+ let(:do_request_result) { Nokogiri::XML(do_request_result_string) }
+ let(:do_request_result_string) { "<object></object>" }
+ before(:each) do
+
subject.instance_variable_get(:@connection).stub(:do_request).and_return(do_request_result)
+ end
+
+ it "should call @connection.do_request with correct parameters" do
+ subject.instance_variable_get(:@connection).should_receive(:do_request).with(
subject.instance_variable_get(:@path), connection_hash )
+ subject.attr_list
+ end
+
+ context "when @connection.do_request returns good data" do
+ let(:do_request_result_string) { "<object><object_attr
name='name_1'>value_1</object_attr><object_attr
name='name_2'>value_2</object_attr></object>" }
+
+ it "should return correct values" do
+ subject.attr_list.should be_eql(["name_1", "name_2"])
+ end
+ end
+
+ context "when @connection.do_request returns bad data" do
+ let(:do_request_result_string) { "<object>/<object>" }
+
+ it "should return correct values" do
+ subject.attr_list.should_not be_eql(["name_1",
"name_2"])
+ subject.attr_list.should be_eql([])
+ end
+ end
+ end
+
+ context "#attrs" do
+
+ let(:attrs) { { 'attr1' => 'val1', 'attr2' =>
'val2', 'attr_empty' => '', 'attr_nil' => nil,
'attr-dash' => 'dash' } }
+ let(:attr_list_without_dash) { attrs.keys - ['attr-dash'] }
+ let(:attr_list_with_dash) { attrs.keys - attr_list_without_dash }
+ let(:attr_list) { attr_list_with_dash + attr_list_without_dash }
+ let(:connection_hash) { { :plain => true } }
+ before(:each) do
+ attrs.each do |key, value|
+ subject.instance_variable_get(:@connection).stub(:do_request).with(
"#{subject.instance_variable_get(:@path)}/#{key}", connection_hash
).and_return(value)
+ end
+ end
+
+ it "should call @connection.do_request for each attribute without
dash" do
+ attr_list_without_dash.each do |attr|
+
subject.instance_variable_get(:@connection).should_receive(:do_request).with(
"#{subject.instance_variable_get(:@path)}/#{attr}", connection_hash )
+ end
+ attr_list_with_dash.each do |attr|
+
subject.instance_variable_get(:@connection).should_not_receive(:do_request).with(
"#{subject.instance_variable_get(:@path)}/#{attr}", connection_hash )
+ end
+ subject.attrs( attr_list )
+ end
+
+ it "should return correct values" do
+ subject.attrs(attr_list).should be_eql(attrs.except(*attr_list_with_dash))
+ end
+
+ end
+
+ context "#attr" do
+
+ let(:good_attribute_name) { "good_name" }
+ let(:good_attribute_value) { "good_value" }
+ let(:bad_attribute_name) { "bad_name" }
+ before(:each) do
+ subject.stub(:attrs).and_return( { good_attribute_name =>
good_attribute_value } )
+ end
+
+ context "when passed good name of attribute" do
+ it "should return correct value" do
+ subject.attr(good_attribute_name).should be_eql(good_attribute_value)
+ end
+ end
+
+ context "when passed bad name of attribute" do
+ it "should return nil" do
+ subject.attr(bad_attribute_name).should be_nil
+ end
+ end
+
+ context "when passed nil as name of attribute" do
+ it "should return nil" do
+ subject.attr(nil).should be_nil
+ end
+ end
+
+ end
+
+ context "#set_attrs" do
+
+ let(:attrs_hash) { { :attr1 => 'value1', :attr2 =>
'value2' } }
+ before(:each) do
+ subject.stub(:set_attr)
+ end
+ it "should call set_attr with correct parameters for each attribute"
do
+ attrs_hash.each do |key, value|
+ subject.should_receive(:set_attr).with(key, value)
+ end
+ subject.set_attrs(attrs_hash)
+ end
+
+ end
+
+ context "#set_attr" do
+
+ let(:name) { 'name' }
+ let(:content) { 'content' }
+ let(:connection_hash) { { :method => :put, :content => content } }
+ it "should call @connection.do_request with correct parameters" do
+
subject.instance_variable_get(:@connection).should_receive(:do_request).with("#{subject.instance_variable_get(:@path)}/#{name}",
connection_hash )
+ subject.set_attr(name, content)
+ end
+ end
+
+ context "#delete!" do
+
+ let(:connection_hash) { { :method => :delete } }
+ before(:each) do
+ subject.instance_variable_get(:@connection).stub(:do_request)
+ end
+ it "should call @connection.do_request with correct parameters" do
+
subject.instance_variable_get(:@connection).should_receive(:do_request).with(subject.instance_variable_get(:@path),
connection_hash )
+ subject.delete!
+ end
+
+ end
+ end
+
+ describe Bucket do
+
+ subject { bucket }
+ let(:name) { 'bucket_name' }
+ let(:do_request_result_string) { "<object>/<object>" }
+ let(:do_request_result) { Nokogiri::XML(do_request_result_string) }
+ let(:connection) { mock(Object, :do_request => do_request_result) }
+ let(:bucket) { Bucket.new(name, connection) }
+ context "#initialize" do
+
+ it "should set instance variables correctly" do
+ subject.instance_variable_get(:@name).should be_eql(name)
+ subject.instance_variable_get(:@connection).should be_eql(connection)
+ end
+ end
+
+ context "#to_s" do
+
+ it "should return correct string" do
+ subject.to_s.should be_eql("Bucket: #{name}")
+ end
+
+ end
+
+ context "#object_names" do
+ let(:connection_path) { "/#{name}" }
+ let(:do_request_result_string) {
"<objects><object><key>name1</key></object><object><key>name2</key></object></objects>"
}
+
+ it "should call @connection.do_request with correct parameters" do
+ connection.should_receive(:do_request).with(connection_path)
+ subject.object_names
+ end
+
+ it "should return array of object names" do
+ puts subject.object_names
+ subject.object_names.should be_eql(['name1', 'name2'])
+ end
+
+ end
+
+ context "#objects" do
+ let(:object_names) { %w{ name1 name2 } }
+ let(:mock_object) { mock(BucketObject) }
+ before(:each) do
+ subject.stub(:object_names).and_return(object_names)
+ subject.stub(:object).and_return( mock_object )
+ end
+
+ it "should return BucketObject for each object name" do
+ subject.objects.should be_eql([mock_object, mock_object])
+ end
+ end
+
+ context "#object" do
+
+ let(:object_name) { 'object_name' }
+ let(:mock_object) { mock(BucketObject) }
+ before(:each) do
+ BucketObject.stub(:new).and_return(mock_object)
+ end
+ it "should call BucketObject.new with correct parameters" do
+ BucketObject.should_receive(:new).with(connection, object_name, subject)
+ subject.object(object_name)
+ end
+
+ it "should return new BucketObject" do
+ subject.object(object_name).should be_eql(mock_object)
+ end
+
+ end
+
+ context "#create_object" do
+
+ let(:object_name) { 'object_name' }
+ let(:object_body) { 'object_body' }
+ let(:object_attrs) { { :attr1 => 'value1', :attr2 =>
'value2' } }
+ let(:mock_object) { mock(BucketObject) }
+ before(:each) do
+ BucketObject.stub(:create).and_return(mock_object)
+ end
+ it "should call BucketObject.create with correct parameters" do
+ BucketObject.should_receive(:create).with(connection, object_name, subject,
object_body, object_attrs)
+ subject.create_object(object_name, object_body, object_attrs)
+ end
+
+ it "should return new BucketObject" do
+ subject.create_object(object_name, object_body, object_attrs).should
be_eql(mock_object)
+ end
+
+ end
+
+ context "#include?" do
+
+ let(:object_names) { %w{ name1 name2 } }
+ before(:each) do
+ subject.stub(:object_names).and_return(object_names)
+ end
+
+ context "when passed key of existing object" do
+ it { subject.include?('name1').should be_true }
+ end
+ context "when passed key of nonexisting object" do
+ it { subject.include?('name3').should be_false }
+ end
+
+ end
+ end
+
+ describe Connection do
+ subject { connection }
+
+ let(:uri) { 'uri' }
+ let(:connection) { Connection.new(uri) }
+ context "#initialize" do
+
+ it "should set up instance variables correctly" do
+ subject.instance_variable_get(:@uri).should be_eql(uri)
+ end
+
+ end
+
+ context "#do_request" do
+ let(:path) { 'path' }
+ let(:opts) { { :method => 'method', :content =>
'content', :plain => false, :headers => {} } }
+ let(:result) { 'result' }
+ let(:xml_result) { Nokogiri::XML(result) }
+
+ before(:each) do
+ RestClient::Request.stub(:execute).and_return(result)
+ end
+
+ context "with meaningful parameters" do
+
+ it "should call RestClient::Request.execute with correct
parameters" do
+ RestClient::Request.should_receive(:execute).with(:method =>
opts[:method], :url => uri + path, :payload => opts[:content], :headers =>
opts[:headers])
+ subject.do_request(path, opts)
+ end
+ end
+ context "with no parameters" do
+
+ it "should call RestClient::Request.execute with meaningful
defaults" do
+ RestClient::Request.should_receive(:execute).with(:method => :get, :url
=> uri + '', :payload => '', :headers => {})
+ subject.do_request()
+ end
+ end
+
+ context "with opts[:plain] = true" do
+ let(:opts) { { :method => 'method', :content =>
'content', :plain => true, :headers => {} } }
+ it "should return plain result" do
+ subject.do_request(path, opts).should be_eql(result)
+ end
+ end
+
+ context "with opts[:plain] = false" do
+ let(:opts) { { :method => 'method', :content =>
'content', :plain => false, :headers => {} } }
+ it "should return plain result" do
+ subject.do_request(path, opts).to_s.should be_eql(xml_result.to_s)
+ subject.do_request(path, opts).class.should be_eql(xml_result.class)
+ end
+ end
+ end
+
+ describe Client do
+
+ subject { client }
+ let(:client) { Client.new(uri) }
+ let(:uri) { 'uri' }
+ let(:connection) { mock(Object, :do_request => do_request_result) }
+ let(:do_request_result) { Nokogiri::XML(do_request_result_string) }
+ let(:do_request_result_string) { "<object>/<object>" }
+ before(:each) do
+ Connection.stub(:new).and_return(connection)
+ end
+
+ context "#initialize" do
+
+ it "should set instance variables correctly" do
+ subject.instance_variable_get(:@connection).should be_eql(connection)
+ end
+
+ end
+
+ context "#create_bucket" do
+ let(:bucket) { mock(Bucket) }
+ let(:bucket_name) { 'bucket_name' }
+ let(:path) { "/#{bucket_name}" }
+ let(:connection_hash) { { :method => :put } }
+ before(:each) do
+ Bucket.stub(:new).and_return(bucket)
+ end
+
+ it "should call @connection.do_request with correct parameters" do
+ connection.should_receive(:do_request).with(path, connection_hash)
+ subject.create_bucket(bucket_name)
+ end
+
+ it "should return Bucket object" do
+ subject.create_bucket(bucket_name).should be_eql(bucket)
+ end
+
+ end
+
+ context "#bucket" do
+ let(:bucket) { mock(Bucket) }
+ let(:bucket_name) { 'bucket_name' }
+ before(:each) do
+ Bucket.stub(:new).and_return(bucket)
+ end
+
+ it "should return Bucket object" do
+ subject.create_bucket(bucket_name).should be_eql(bucket)
+ end
+
+ end
+
+ context "#buckets" do
+ let(:bucket_names) { %w{ bucket1 bucket2 bucket3 } }
+ let(:do_request_result_string) { "<api>#{bucket_names.map{|bn|
"<link rel='bucket' href='#{bn}'/>"}}</api>" }
+ it "should return array of bucket names" do
+ subject.buckets.should be_eql(bucket_names)
+ end
+
+ end
+
+ context "#get_iwhd_version" do
+ let(:iwhd_version) { 'iwhd_version' }
+ let(:do_request_result_string) { "<api
service='image_warehouse' version='#{iwhd_version}'></api>"
}
+ it "should return iwhd version value" do
+ subject.get_iwhd_version.should be_eql(iwhd_version)
+ end
+
+ end
+
+ context "#query" do
+ let(:bucket_name) { 'bucket_name' }
+ let(:path) { "/#{bucket_name}/_query" }
+ let(:query_string) { "query_string" }
+ let(:connection_hash) { { :method => :post, :content => query_string }
}
+
+ it "should call @connection.do_request with correct parameters" do
+
+ connection.should_receive(:do_request).with(path, connection_hash)
+ subject.query(bucket_name, query_string)
+ end
+
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spec/models/warehouse/warehouse_model_spec.rb
b/spec/models/warehouse/warehouse_model_spec.rb
new file mode 100644
index 0000000..cfaa8f6
--- /dev/null
+++ b/spec/models/warehouse/warehouse_model_spec.rb
@@ -0,0 +1,94 @@
+# Copyright 2011 Red Hat, Inc.
+#
+# Licensed 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 'spec_helper'
+
+module Aeolus
+ module Image
+ module Warehouse
+ describe WarehouseModel do
+ subject { @warehouse_model }
+ before(:each) do
+ @warehouse_model_attributes = {
+ :attribute => 'attribute',
+ :other_attribute => 'other_attribute'
+ }
+ @object = mock(Object, :attr_list => @warehouse_model_attributes.keys,
:attrs => @warehouse_model_attributes)
+ @warehouse_model_attributes.each do |key, value|
+ @object.stub(key.to_sym, value)
+ end
+ @warehouse_model = WarehouseModel.new(@object)
+ end
+
+ context "#==" do
+ let(:other_object) { mock(Object, :attr_list =>
other_warehouse_attributes.keys, :attrs => other_warehouse_attributes) }
+ let(:other_warehouse_model) { WarehouseModel.new(other_object) }
+
+ context "when other object has the same instance variables list" do
+ context "with the same values" do
+ let(:other_warehouse_attributes) { @warehouse_model_attributes }
+ it { subject.==(other_warehouse_model).should be_true }
+ end
+
+ context "with different values" do
+ let(:other_warehouse_attributes) {
@warehouse_model_attributes.merge(:attribute => 'other_value') }
+ it { subject.==(other_warehouse_model).should be_false }
+ end
+ end
+
+ context "when other object has different instance variables list" do
+ let(:other_warehouse_attributes) {
@warehouse_model_attributes.merge(:another_attribute => 'another_attribute') }
+
+ it { subject.==(other_warehouse_model).should be_false }
+
+ end
+ end
+
+ # cannot be tested - method returns uuid
+ # which may be created by initialize method in child of WarehouseModel
+ context "#id"
+
+ context ".set_warehouse_and_bucket" do
+
+ it "should set warehouse" do
+ pending
+ end
+
+ it "should set bucket" do
+ pending
+ end
+ end
+
+ context ".bucket_objects" do
+
+ context "when bucket is not present" do
+
+ it "should set warehouse and bucket" do
+ pending
+ end
+ end
+ end
+
+ context ".first" do
+
+ before(:each) do
+ WarehouseModel.stub(:bucket_objects).and_return(bucket_objects)
+ end
+
+ end
+
+ end
+ end
+ end
+end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 4252180..bd4a318 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -17,3 +17,22 @@ $: << File.expand_path(File.join(File.dirname(__FILE__),
"../lib/aeolus_image/mo
require 'rubygems'
require File.join(File.dirname(__FILE__), '../lib', 'aeolus_image')
require 'vcr_setup'
+
+require 'rspec'
+require 'warehouse/warehouse_client'
+require 'warehouse/warehouse_model'
+require 'warehouse/image'
+require 'warehouse/image_build'
+require 'warehouse/target_image'
+require 'warehouse/provider_image'
+require 'warehouse/template'
+
+RSpec.configure do |config|
+ config.before(:all) do
+# Aeolus::Image::BaseCommand.class_eval do
+# def load_config
+# YAML::load(File.open(File.join(File.dirname(__FILE__),
"/../examples/aeolus-cli")))
+# end
+# end
+ end
+end
--
1.7.6.4