Project

General

Profile

« Previous | Next » 

Revision ba64f022

Added by Marek Hulán about 9 years ago

Fixes #10442 - dry up host managed logic

View differences:

app/models/nic/base.rb
validates :mac, :uniqueness => {:scope => :virtual},
:if => Proc.new { |nic| nic.managed? && nic.host && nic.host.managed? && !nic.host.compute? && !nic.virtual? }, :allow_blank => true
validates :mac, :presence => true,
:if => Proc.new { |nic| nic.managed? && nic.host && nic.host.managed? && !nic.host.compute? && !nic.virtual? && SETTINGS[:unattended] }
:if => Proc.new { |nic| nic.managed? && nic.host_managed? && !nic.host.compute? && !nic.virtual? }
validates :mac, :mac_address => true, :allow_blank => true
# TODO uniq on primary per host
......
validate :exclusive_primary_interface
validate :exclusive_provision_interface
validates :domain, :presence => true, :if => Proc.new { |nic| nic.host && nic.host.managed? && nic.primary? && SETTINGS[:unattended] }
validate :valid_domain, :if => Proc.new { |nic| nic.host && nic.host.managed? && nic.primary? && SETTINGS[:unattended] }
validates :ip, :presence => true, :if => Proc.new { |nic| nic.host && nic.host.managed? && nic.require_ip_validation? && SETTINGS[:unattended] }
validates :domain, :presence => true, :if => Proc.new { |nic| nic.host_managed? && nic.primary? }
validate :valid_domain, :if => Proc.new { |nic| nic.host_managed? && nic.primary? }
validates :ip, :presence => true, :if => Proc.new { |nic| nic.host_managed? && nic.require_ip_validation? }
validate :validate_host_taxonomy
......
end
end
# we don't consider host as managed if we are in non-unattended mode
# in which case host managed? flag can be true but we should consider
# everything as unmanaged
def host_managed?
self.host && self.host.managed? && SETTINGS[:unattended]
end
protected
def uniq_fields_with_hosts
test/factories/host_related.rb
type 'GroupParameter'
end
factory :nic_base do
factory :nic_base, :class => Nic::Base do
type 'Nic::Base'
sequence(:identifier) { |n| "eth#{n}" }
sequence(:mac) { |n| "00:00:00:00:" + n.to_s(16).rjust(4, '0').insert(2, ':') }
end
test/unit/nics/base_test.rb
require 'test_helper'
class NicBaseTest < ActiveSupport::TestCase
setup do
disable_orchestration
end
test '#host_managed? returns false if interface does not have a host' do
nic = FactoryGirl.build(:nic_base)
nic.host = nil
refute nic.host_managed?
end
test '#host_managed? returns false if associated host is unmanaged' do
nic = FactoryGirl.build(:nic_base)
nic.host = FactoryGirl.build(:host)
nic.host.managed = false
refute nic.host_managed?
end
test '#host_managed? returns false in non-unattended mode' do
nic = FactoryGirl.build(:nic_base)
nic.host = FactoryGirl.build(:host)
nic.host.managed = true
original, SETTINGS[:unattended] = SETTINGS[:unattended], false
refute nic.host_managed?
SETTINGS[:unattended] = original
end
test '#host_managed? return true if associated host is managed in unattended mode' do
nic = FactoryGirl.build(:nic_base)
nic.host = FactoryGirl.build(:host)
nic.host.managed = true
original, SETTINGS[:unattended] = SETTINGS[:unattended], true
assert nic.host_managed?
SETTINGS[:unattended] = original
end
end

Also available in: Unified diff