This series adds spec for CloudAccount model.
In addition, it fixes the specs for HardwareProfiles and Image model. The previous specs required deltacloud-core server running. It is no longer necessary.
Apply them on the fresh 'next' branch -- these patches replace the previous HP and Image versions.
From: Tomas Sedovic tsedovic@redhat.com
One test is failing. --- src/spec/factories/image.rb | 6 +++ src/spec/models/image_spec.rb | 98 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 0 deletions(-) create mode 100644 src/spec/factories/image.rb create mode 100644 src/spec/models/image_spec.rb
diff --git a/src/spec/factories/image.rb b/src/spec/factories/image.rb new file mode 100644 index 0000000..5cefcc0 --- /dev/null +++ b/src/spec/factories/image.rb @@ -0,0 +1,6 @@ +Factory.define :image do |i| + i.sequence(:name) { |n| "image#{n}" } + i.sequence(:external_key) { |n| "key#{n}" } + i.architecture 'i686' + i.provider {|p| Provider.new } +end diff --git a/src/spec/models/image_spec.rb b/src/spec/models/image_spec.rb new file mode 100644 index 0000000..3ceb696 --- /dev/null +++ b/src/spec/models/image_spec.rb @@ -0,0 +1,98 @@ +require 'spec_helper' + +describe Image do + before(:each) do + @provider = Factory.build(:mock_provider) + @client = mock('DeltaCloud', :null_object => true) + @provider.stub!(:connect).and_return(@client) + end + + it "should have a unique external key" do + i1 = Factory.create(:image, :provider => @provider) + i2 = Factory.create(:image, :provider => @provider) + @provider.images = [i1, i2] + i1.should be_valid + i2.should be_valid + + i2.external_key = i1.external_key + i2.should_not be_valid + end + + it "should have a name" do + i = Factory.build(:image, :name => nil) + i.should_not be_valid + + i.name = '' + i.should_not be_valid + + i.name = "valid name" + i.should be_valid + end + + it "should not have a name that is too long" do + i = Factory.build(:image) + i.name = 'x' * 1025 + i.should_not be_valid + + i.name = 'x' * 1024 + i.should be_valid + end + + it "should have an architecture if it has a provider" do + i = Factory.build(:image, :architecture => nil) + i.should_not be_valid + + i.architecture = 'i686' + i.should be_valid + end + + it "should have either a provider or a pool specified" do + i = Factory.build(:image, :provider => nil, :portal_pool => nil) + i.should have(1).error_on(:provider) + i.should have(1).error_on(:portal_pool) + i.errors.on(:provider).should eql( + "provider or pool must be specified") + i.errors.on(:portal_pool).should eql( + "provider or pool must be specified") + + i.provider = @provider + i.should be_valid + + i.portal_pool = Factory.build(:portal_pool) + i.should have(1).error_on(:provider) + i.should have(1).error_on(:portal_pool) + i.errors.on(:provider).should eql( + "provider or pool must be blank") + i.errors.on(:portal_pool).should eql( + "provider or pool must be blank") + + i.provider = nil + i.should be_valid + end + + it "should have provider images only if it has a provider" do + i = Factory.build(:image, :portal_pool => PortalPool.new, + :provider => nil) + + i.aggregator_images << i + i.should have(1).error_on(:aggregator_images) + i.errors.on(:aggregator_images).should eql( + "Aggregator image only allowed for provider images") + + i.aggregator_images.clear + i.should be_valid + end + + it "should have aggregator images only if it has a pool" do + i = Factory.build(:image) + + i.provider_images << i + i.should have(1).error_on(:provider_images) + i.errors.on(:provider_images).should eql( + "Provider images only allowed for aggregator images") + + i.provider_images.clear + i.should be_valid + end + +end
From: Tomas Sedovic tsedovic@redhat.com
--- src/app/models/image.rb | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/src/app/models/image.rb b/src/app/models/image.rb index 894d4b3..d1e88f7 100644 --- a/src/app/models/image.rb +++ b/src/app/models/image.rb @@ -40,6 +40,7 @@ class Image < ActiveRecord::Base validates_uniqueness_of :external_key, :scope => [:provider_id, :portal_pool_id]
validates_presence_of :name + validates_length_of :name, :maximum => 1024
validates_presence_of :architecture, :if => :provider
From: Tomas Sedovic tsedovic@redhat.com
Some of the tests are failing. --- src/spec/models/hardware_profile_spec.rb | 138 ++++++++++++++++++++++++++++++ 1 files changed, 138 insertions(+), 0 deletions(-) create mode 100644 src/spec/models/hardware_profile_spec.rb
diff --git a/src/spec/models/hardware_profile_spec.rb b/src/spec/models/hardware_profile_spec.rb new file mode 100644 index 0000000..8f62be2 --- /dev/null +++ b/src/spec/models/hardware_profile_spec.rb @@ -0,0 +1,138 @@ +require 'spec_helper' + +describe HardwareProfile do + before(:each) do + @valid_attributes = Factory.attributes_for(:mock_hwp1) + end + + it "should create a new hardware profile" do + hp = HardwareProfile.new(@valid_attributes) + hp.should be_valid + end + + it "should not validate for missing name" do + hp = HardwareProfile.new(@valid_attributes) + + [nil, ""].each do |value| + hp.name = value + hp.should_not be_valid + end + end + + it "should require unique names" do + hp1 = Factory.create(:mock_hwp1) + hp2 = Factory.create(:mock_hwp2) + hp1.should be_valid + hp2.should be_valid + + hp2.name = hp1.name + hp2.should_not be_valid + end + + it "should require valid amount of memory" do + hp = HardwareProfile.new(@valid_attributes) + + [nil, "hello", -1].each do |fail_value| + hp.memory = fail_value + hp.should_not be_valid + end + end + + it "should require valid amount of storage" do + hp = HardwareProfile.new(@valid_attributes) + + [nil, "hello", -1].each do |fail_value| + hp.storage = fail_value + hp.should_not be_valid + end + end + + it "should not require architecture when there's no provider" do + hp = HardwareProfile.new(@valid_attributes) + hp.architecture = nil + hp.should be_valid + end + + it "should require architecture when it's with provider" do + #provider = Factory.build(:mock_provider) + hp = HardwareProfile.new(@valid_attributes) + hp.provider = Provider.new + + hp.should be_valid + hp.architecture = nil + hp.should_not be_valid + end + + it "should reject Aggregator profiles for custom Instance profiles" do + hp = HardwareProfile.new(@valid_attributes) + hp.aggregator_hardware_profiles << hp + hp.should_not be_valid + hp.should have(1).error_on(:aggregator_hardware_profiles) + hp.errors.on(:aggregator_hardware_profiles).should eql( + "Aggregator profiles are not allowed for custom Instance profiles") + + hp.aggregator_hardware_profiles.clear + hp.should be_valid + end + + it "should reject Provider profiles for custom Instance profiles" do + hp = HardwareProfile.new(@valid_attributes) + hp.provider_hardware_profiles << hp + hp.should_not be_valid + hp.should have(1).error_on(:provider_hardware_profiles) + hp.errors.on(:provider_hardware_profiles).should eql( + "Provider profiles are not allowed for custom Instance profiles") + + hp.provider_hardware_profiles.clear + hp.should be_valid + end + + it "should require either provider or pool to be blank" do + hp = HardwareProfile.new(@valid_attributes) + hp.provider = Provider.new + hp.portal_pool = PortalPool.new + hp.should_not be_valid + hp.should have(1).error_on(:provider) + hp.errors.on(:provider).should eql( + "provider or pool must be blank") + hp.should have(1).error_on(:portal_pool) + hp.errors.on(:portal_pool).should eql( + "provider or pool must be blank") + + hp.provider = nil + hp.should be_valid + + hp.provider = Provider.new + hp.portal_pool = nil + hp.should be_valid + end + + it "should allow Provider profiles only for provider profiles" do + hp = HardwareProfile.new(@valid_attributes) + hp.provider = nil + hp.portal_pool = PortalPool.new + + hp.provider_hardware_profiles << hp + hp.should have(1).error_on(:provider_hardware_profiles) + hp.errors.on(:provider_hardware_profiles).should eql( + "Provider profiles only allowed for provider profiles") + + hp.provider_hardware_profiles.clear + hp.should be_valid + end + + it "should allow Aggregator profiles only for pool profiles" do + hp = HardwareProfile.new(@valid_attributes) + hp.provider = Provider.new + hp.portal_pool = nil + + hp.aggregator_hardware_profiles << hp + hp.should have(1).error_on(:aggregator_hardware_profiles) + hp.errors.on(:aggregator_hardware_profiles).should eql( + "Aggregator profiles only allowed for pool profiles") + + hp.aggregator_hardware_profiles.clear + hp.should be_valid + end + +end
tsedovic@redhat.com wrote:
From: Tomas Sedovic tsedovic@redhat.com
Some of the tests are failing.
I just pushed my PortalPool -> Pool rename. Could you rebase these patches and re-send? Also you'll need to replace any instance of PortalPool with Pool and portal_pool with pool.
In addition one of the tests is failing with a 'stack too deep' error in will_paginate. I'm not sure what's causing that.
Thanks, Scott
src/spec/models/hardware_profile_spec.rb | 138 ++++++++++++++++++++++++++++++ 1 files changed, 138 insertions(+), 0 deletions(-) create mode 100644 src/spec/models/hardware_profile_spec.rb
diff --git a/src/spec/models/hardware_profile_spec.rb b/src/spec/models/hardware_profile_spec.rb new file mode 100644 index 0000000..8f62be2 --- /dev/null +++ b/src/spec/models/hardware_profile_spec.rb @@ -0,0 +1,138 @@ +require 'spec_helper'
+describe HardwareProfile do
- before(:each) do
- @valid_attributes = Factory.attributes_for(:mock_hwp1)
- end
- it "should create a new hardware profile" do
- hp = HardwareProfile.new(@valid_attributes)
- hp.should be_valid
- end
- it "should not validate for missing name" do
- hp = HardwareProfile.new(@valid_attributes)
- [nil, ""].each do |value|
hp.name = value
hp.should_not be_valid
- end
- end
- it "should require unique names" do
- hp1 = Factory.create(:mock_hwp1)
- hp2 = Factory.create(:mock_hwp2)
- hp1.should be_valid
- hp2.should be_valid
- hp2.name = hp1.name
- hp2.should_not be_valid
- end
- it "should require valid amount of memory" do
- hp = HardwareProfile.new(@valid_attributes)
- [nil, "hello", -1].each do |fail_value|
hp.memory = fail_value
hp.should_not be_valid
- end
- end
- it "should require valid amount of storage" do
- hp = HardwareProfile.new(@valid_attributes)
- [nil, "hello", -1].each do |fail_value|
hp.storage = fail_value
hp.should_not be_valid
- end
- end
- it "should not require architecture when there's no provider" do
- hp = HardwareProfile.new(@valid_attributes)
- hp.architecture = nil
- hp.should be_valid
- end
- it "should require architecture when it's with provider" do
- #provider = Factory.build(:mock_provider)
- hp = HardwareProfile.new(@valid_attributes)
- hp.provider = Provider.new
- hp.should be_valid
- hp.architecture = nil
- hp.should_not be_valid
- end
- it "should reject Aggregator profiles for custom Instance profiles" do
- hp = HardwareProfile.new(@valid_attributes)
- hp.aggregator_hardware_profiles << hp
- hp.should_not be_valid
- hp.should have(1).error_on(:aggregator_hardware_profiles)
- hp.errors.on(:aggregator_hardware_profiles).should eql(
"Aggregator profiles are not allowed for custom Instance profiles")
- hp.aggregator_hardware_profiles.clear
- hp.should be_valid
- end
- it "should reject Provider profiles for custom Instance profiles" do
- hp = HardwareProfile.new(@valid_attributes)
- hp.provider_hardware_profiles << hp
- hp.should_not be_valid
- hp.should have(1).error_on(:provider_hardware_profiles)
- hp.errors.on(:provider_hardware_profiles).should eql(
"Provider profiles are not allowed for custom Instance profiles")
- hp.provider_hardware_profiles.clear
- hp.should be_valid
- end
- it "should require either provider or pool to be blank" do
- hp = HardwareProfile.new(@valid_attributes)
- hp.provider = Provider.new
- hp.portal_pool = PortalPool.new
- hp.should_not be_valid
- hp.should have(1).error_on(:provider)
- hp.errors.on(:provider).should eql(
"provider or pool must be blank")
- hp.should have(1).error_on(:portal_pool)
- hp.errors.on(:portal_pool).should eql(
"provider or pool must be blank")
- hp.provider = nil
- hp.should be_valid
- hp.provider = Provider.new
- hp.portal_pool = nil
- hp.should be_valid
- end
- it "should allow Provider profiles only for provider profiles" do
- hp = HardwareProfile.new(@valid_attributes)
- hp.provider = nil
- hp.portal_pool = PortalPool.new
- hp.provider_hardware_profiles << hp
- hp.should have(1).error_on(:provider_hardware_profiles)
- hp.errors.on(:provider_hardware_profiles).should eql(
"Provider profiles only allowed for provider profiles")
- hp.provider_hardware_profiles.clear
- hp.should be_valid
- end
- it "should allow Aggregator profiles only for pool profiles" do
- hp = HardwareProfile.new(@valid_attributes)
- hp.provider = Provider.new
- hp.portal_pool = nil
- hp.aggregator_hardware_profiles << hp
- hp.should have(1).error_on(:aggregator_hardware_profiles)
- hp.errors.on(:aggregator_hardware_profiles).should eql(
"Aggregator profiles only allowed for pool profiles")
- hp.aggregator_hardware_profiles.clear
- hp.should be_valid
- end
+end
From: Tomas Sedovic tsedovic@redhat.com
--- src/app/models/hardware_profile.rb | 5 +++-- 1 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/src/app/models/hardware_profile.rb b/src/app/models/hardware_profile.rb index 7714acb..fd7fbe3 100644 --- a/src/app/models/hardware_profile.rb +++ b/src/app/models/hardware_profile.rb @@ -42,11 +42,12 @@ class HardwareProfile < ActiveRecord::Base validates_uniqueness_of :external_key, :scope => [:provider_id, :portal_pool_id]
validates_presence_of :name + validates_uniqueness_of :name
validates_presence_of :storage - validates_numericality_of :storage + validates_numericality_of :storage, :greater_than => 0 validates_presence_of :memory - validates_numericality_of :memory + validates_numericality_of :memory, :greater_than => 0
validates_presence_of :architecture, :if => :provider
From: Tomas Sedovic tsedovic@redhat.com
--- src/spec/factories/cloud_account.rb | 9 ++++ src/spec/models/cloud_account_spec.rb | 76 +++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 0 deletions(-) create mode 100644 src/spec/factories/cloud_account.rb create mode 100644 src/spec/models/cloud_account_spec.rb
diff --git a/src/spec/factories/cloud_account.rb b/src/spec/factories/cloud_account.rb new file mode 100644 index 0000000..39d40c6 --- /dev/null +++ b/src/spec/factories/cloud_account.rb @@ -0,0 +1,9 @@ +Factory.define :cloud_account do |ca| + ca.sequence(:username) { |n| "user#{n}" } + ca.password 'asdf' + ca.provider_id 1 +end + +Factory.define :mock_cloud_account, :parent => :cloud_account do |ca| + ca.username 'John Doe' +end diff --git a/src/spec/models/cloud_account_spec.rb b/src/spec/models/cloud_account_spec.rb new file mode 100644 index 0000000..cc205d3 --- /dev/null +++ b/src/spec/models/cloud_account_spec.rb @@ -0,0 +1,76 @@ +require 'spec_helper' + +describe CloudAccount do + + before(:each) do + @cloud_account = Factory.build :mock_cloud_account + @client = mock('DeltaCloud', :null_object => true) + @cloud_account.stub!(:connect).and_return(@client) + end + + it "should require a provider to be set" do + @cloud_account.provider_id = nil + @cloud_account.should_not be_valid + + @cloud_account.provider_id = 1 + @cloud_account.should be_valid + end + + it "should have a unique username" do + @cloud_account.save! + cloud_account2 = Factory.build :cloud_account + cloud_account2.username = @cloud_account.username + cloud_account2.should_not be_valid + + cloud_account2.username = 'unique username' + cloud_account2.should be_valid + end + + it "should require a password to be set" do + [nil, ''].each do |invalid_value| + @cloud_account.password = invalid_value + @cloud_account.should_not be_valid + end + + @cloud_account.password = "2CufFHQjoBL5fKDW9R" + @cloud_account.should be_valid + end + + it "should log connection errors" do + cloud_account = Factory.build :cloud_account + cloud_account.provider = Provider.new(:url => 'http://invalid.provider/url') + logger = mock('Logger', :null_object => true) + cloud_account.stub!(:logger).and_return(logger) + + logger.should_receive(:error).twice + cloud_account.connect.should be_nil + end + + it "should create new account if it doesn't exist yet" do + account = @cloud_account.attributes + CloudAccount.find_by_username_and_provider_id( + account["username"], account["provider_id"]).should be_nil + + + ca1 = CloudAccount.find_or_create(account) + ca1.should_not be_nil + + ca1.save! + + ca2 = CloudAccount.find_or_create(account) + ca2.should_not be_nil + ca2.should == ca1 + end + + it "should provide the correct name" do + @cloud_account.name.should eql('John Doe') + end + + it "should provide correct account prefix for realm" do + @cloud_account.provider = Provider.new(:name => "PerfectProvider(tm)") + @cloud_account.account_prefix_for_realm.should \ + eql("PerfectProvider(tm):John Doe") + end + + +end
deltacloud-devel@lists.fedorahosted.org