Project

General

Profile

Download (20.8 KB) Statistics
| Branch: | Tag: | Revision:
8838eb42 Ohad Levy
require 'test_helper'

class NicTest < ActiveSupport::TestCase
def setup
disable_orchestration
e07f9a12 Dominic Cleal
User.current = users :admin
43c4bd72 Marek Hulan
34f84c8d Michael Moll
@nic = FactoryBot.build_stubbed(:nic_managed, :host => FactoryBot.build_stubbed(:host, :managed => true))
8838eb42 Ohad Levy
end

def teardown
User.current = nil
end

test "should create simple interface" do
8c6bc83e Marek Hulan
i = Nic::Base.create! :mac => "cabbccddeeff", :host => FactoryBot.create(:host)
8838eb42 Ohad Levy
assert_equal "Nic::Base", i.class.to_s
end

test "type casting should return the correct class" do
8c6bc83e Marek Hulan
i = Nic::Base.create! :ip => "127.2.3.8", :mac => "babbccddeeff", :host => FactoryBot.create(:host),
43c4bd72 Marek Hulan
:type => "Nic::Interface"
8838eb42 Ohad Levy
assert_equal "Nic::Interface", i.type
end

test "should fail on invalid mac" do
8c6bc83e Marek Hulan
i = Nic::Base.new :mac => "abccddeeff", :host => FactoryBot.create(:host, :managed)
8838eb42 Ohad Levy
assert !i.valid?
assert i.errors.keys.include?(:mac)
end

e42aa400 Trey Dockendorf
test "should be valid with 64-bit mac address" do
8c6bc83e Marek Hulan
i = Nic::Base.new :mac => "babbccddeeff00112233445566778899aabbccdd", :host => FactoryBot.create(:host)
e42aa400 Trey Dockendorf
assert i.valid?
assert !i.errors.keys.include?(:mac)
end

4bae5ced Stephen Benjamin
test "should fail on invalid dns name" do
8c6bc83e Marek Hulan
i = Nic::Managed.new :mac => "dabbccddeeff", :host => FactoryBot.create(:host), :name => "invalid_dns_name"
4bae5ced Stephen Benjamin
assert !i.valid?
assert i.errors.keys.include?(:name)
end

8838eb42 Ohad Levy
test "should fix mac address" do
8c6bc83e Marek Hulan
interface = Nic::Base.create! :mac => "cabbccddeeff", :host => FactoryBot.create(:host)
8838eb42 Ohad Levy
assert_equal "ca:bb:cc:dd:ee:ff", interface.mac
end

e42aa400 Trey Dockendorf
test "should fix 64-bit mac address" do
8c6bc83e Marek Hulan
interface = Nic::Base.create! :mac => "babbccddeeff00112233445566778899aabbccdd", :host => FactoryBot.create(:host)
e42aa400 Trey Dockendorf
assert_equal "ba:bb:cc:dd:ee:ff:00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd", interface.mac
end

8838eb42 Ohad Levy
test "should fix ip address if a leading zero is used" do
8c6bc83e Marek Hulan
interface = Nic::Interface.create! :ip => "123.01.02.03", :mac => "dabbccddeeff", :host => FactoryBot.create(:host)
8838eb42 Ohad Levy
assert_equal "123.1.2.3", interface.ip
end

9a41f58f Tomas Strachota
test "type can't by updated" do
8c6bc83e Marek Hulan
interface = FactoryBot.create(:nic_managed, :host => FactoryBot.create(:host))
9a41f58f Tomas Strachota
interface.type = 'Nic::BMC'
interface.valid?
assert_includes interface.errors.keys, :type
end

fd8bf7f1 Lukas Zapletal
test "managed nic should generate progress report uuid" do
uuid = '710d4a8f-b1b6-47f5-9ef5-5892a19dabcd'
Foreman.stubs(:uuid).returns(uuid)
34f84c8d Michael Moll
nic = FactoryBot.build_stubbed(:nic_managed)
fd8bf7f1 Lukas Zapletal
assert_equal uuid, nic.progress_report_id
end

test "host with managed nic should delegate progress report creation" do
uuid = '710d4a8f-b1b6-47f5-9ef5-5892a19dabcd'
8c6bc83e Marek Hulan
host = FactoryBot.create(:host, :managed)
fd8bf7f1 Lukas Zapletal
host.expects(:progress_report_id).returns(uuid)
assert_equal uuid, host.primary_interface.progress_report_id
end

8838eb42 Ohad Levy
test "should delegate subnet attributes" do
8f695d94 Shimon Shtein
subnet = subnets(:two)
d093bdb6 Jo Vandeginste
subnet6 = subnets(:seven)
8838eb42 Ohad Levy
domain = (subnet.domains.any? ? subnet.domains : subnet.domains << Domain.first).first
34f84c8d Michael Moll
interface = FactoryBot.build_stubbed(:nic_managed,
5f606e11 Daniel Lobato Garcia
:ip => "3.3.4.127",
:mac => "cabbccddeeff",
8c6bc83e Marek Hulan
:host => FactoryBot.create(:host),
5f606e11 Daniel Lobato Garcia
:subnet => subnet,
d093bdb6 Jo Vandeginste
:subnet6 => subnet6,
8c6bc83e Marek Hulan
:name => "a" + FactoryBot.create(:host).name,
5f606e11 Daniel Lobato Garcia
:domain => domain)
8838eb42 Ohad Levy
assert_equal subnet.network, interface.network
d093bdb6 Jo Vandeginste
assert_equal subnet6.network, interface.network6
8838eb42 Ohad Levy
assert_equal subnet.vlanid, interface.vlanid
55e2afa1 Tomer Brisker
assert_equal 42, interface.vlanid
f175f751 Baptiste Agasse
assert_equal 1496, interface.mtu
assert_equal subnet.mtu, interface.mtu
d093bdb6 Jo Vandeginste
end

test "should delegate subnet6 attributes if subnet is nil" do
subnet = nil
subnet6 = subnets(:seven)
domain = (subnet6.domains.any? ? subnet6.domains : subnet6.domains << Domain.first).first
34f84c8d Michael Moll
interface = FactoryBot.build_stubbed(:nic_managed,
d093bdb6 Jo Vandeginste
:ip => "3.3.4.127",
:mac => "cabbccddeeff",
8c6bc83e Marek Hulan
:host => FactoryBot.create(:host),
d093bdb6 Jo Vandeginste
:subnet => subnet,
:subnet6 => subnet6,
8c6bc83e Marek Hulan
:name => "a" + FactoryBot.create(:host).name,
d093bdb6 Jo Vandeginste
:domain => domain)
assert_equal subnet6.vlanid, interface.vlanid
f175f751 Baptiste Agasse
assert_equal subnet6.mtu, interface.mtu
d093bdb6 Jo Vandeginste
assert_equal subnet6.network, interface.network6
55e2afa1 Tomer Brisker
assert_equal 44, interface.vlanid
f175f751 Baptiste Agasse
assert_equal 9000, interface.mtu
8838eb42 Ohad Levy
end
21e07e92 Trey Dockendorf
8f695d94 Shimon Shtein
test "should reject subnet with mismatched taxonomy in host" do
taxonomy_to_test = [:organization, :location]

taxonomy_to_test.each do |taxonomy|
8c6bc83e Marek Hulan
tax_object1 = FactoryBot.build(taxonomy)
tax_object2 = FactoryBot.build(taxonomy)
8f695d94 Shimon Shtein
subnet = subnets(:one)
9d56081f Timo Goebel
subnet6 = subnets(:six)
8c6bc83e Marek Hulan
host = FactoryBot.build(:host)
8f695d94 Shimon Shtein
ee360810 Michael Moll
subnet_list = subnet.send(taxonomy.to_s.pluralize.to_s)
8f695d94 Shimon Shtein
subnet_list << tax_object1

ee360810 Michael Moll
subnet6_list = subnet6.send(taxonomy.to_s.pluralize.to_s)
9d56081f Timo Goebel
subnet6_list << tax_object1
8f695d94 Shimon Shtein
9d56081f Timo Goebel
host.send("#{taxonomy}=", tax_object2)

nic = Nic::Base.new :mac => "cabbccddeeff", :host => host
nic.subnet = subnet
nic.subnet6 = subnet6

refute nic.valid?, "Can't be valid with mismatching taxonomy: #{nic.errors.messages}"
cd14c1a9 Ivan Nečas
assert_includes nic.errors.keys, :subnet_id
assert_includes nic.errors.keys, :subnet6_id
8f695d94 Shimon Shtein
end
end

d6a19253 Dominic Cleal
test "should ignore subnet with mismatched taxonomy in host when settings disabled" do
disable_taxonomies do
34f84c8d Michael Moll
orgs = FactoryBot.build_stubbed_pair(:organization)
locs = FactoryBot.build_stubbed_pair(:location)
subn = FactoryBot.build_stubbed(:subnet_ipv4, :locations => [locs.first], :organizations => [orgs.first])
host = FactoryBot.build_stubbed(:host, :location => locs.last, :organization => orgs.last)
d6a19253 Dominic Cleal
nic = Nic::Base.new :mac => "cabbccddeeff", :host => host
nic.subnet = subn
assert_valid nic
end
end

8f695d94 Shimon Shtein
test "should accept subnets with aligned location and organization in host" do
8c6bc83e Marek Hulan
location1 = FactoryBot.build(:location)
organization1 = FactoryBot.build(:organization)
8f695d94 Shimon Shtein
subnet = subnets(:one)
8c6bc83e Marek Hulan
host = FactoryBot.build(:host)
8f695d94 Shimon Shtein
subnet.locations << location1
subnet.organizations << organization1
host.location = location1
host.organization = organization1

i = Nic::Base.new :mac => "cabbccddeeff", :host => host
i.subnet = subnet

assert i.valid?
end

fc2ddea7 David Swift
test "Nic::Managed#hostname should return blank for blank hostnames" do
8c6bc83e Marek Hulan
i = Nic::Managed.new :mac => "babbccddeeff00112233445566778899aabbccdd", :host => FactoryBot.create(:host), :subnet => subnets(:one), :domain => subnets(:one).domains.first, :name => ""
445dd7ab Daniel Lobato
assert i.name.blank?
assert i.domain.present?
assert i.hostname.blank?
fc2ddea7 David Swift
end
4f7a4d0b David Davis
43c4bd72 Marek Hulan
test "Mac address uniqueness validation is skipped for virtual NICs and unmanaged hosts" do
8c6bc83e Marek Hulan
host = FactoryBot.create(:host, :managed)
43c4bd72 Marek Hulan
Nic::Base.create! :mac => "cabbccddeeff", :host => host # physical
35241dd6 Marek Hulan
host.reload
43c4bd72 Marek Hulan
virtual = Nic::Base.new :mac => "cabbccddeeff", :host => host, :virtual => true
d455f32c Marek Hulan
assert virtual.valid?
assert virtual.save
43c4bd72 Marek Hulan
another_physical = Nic::Base.new :mac => "cabbccddeeff", :host => host
d455f32c Marek Hulan
refute another_physical.save
8c6bc83e Marek Hulan
another_physical_on_unmanaged = Nic::Base.new :mac => "cabbccddeeff", :host => FactoryBot.create(:host)
43c4bd72 Marek Hulan
assert another_physical_on_unmanaged.save
d455f32c Marek Hulan
end
57a32e98 Daniel Lobato
4feb1576 Marek Hulan
test "VLAN requires identifier" do
8c6bc83e Marek Hulan
nic = FactoryBot.build(:nic_managed, :virtual => true, :attached_to => 'eth0', :tag => 5, :managed => true, :identifier => '')
4feb1576 Marek Hulan
refute nic.valid?
assert_includes nic.errors.keys, :identifier
end

test "Alias requires identifier" do
8c6bc83e Marek Hulan
nic = FactoryBot.build(:nic_managed, :virtual => true, :attached_to => 'eth0', :managed => true, :identifier => '')
4feb1576 Marek Hulan
refute nic.valid?
assert_includes nic.errors.keys, :identifier
end

a45253c2 Marek Hulan
test "#alias? detects alias based on virtual and identifier attributes" do
34f84c8d Michael Moll
nic = FactoryBot.build_stubbed(:nic_managed, :virtual => true, :attached_to => 'eth0', :managed => true, :identifier => 'eth0')
a45253c2 Marek Hulan
refute nic.alias?

nic.identifier = 'eth0:0'
assert nic.alias?

nic.virtual = false
refute nic.alias?
end

test "Alias subnet can only use static boot mode if it's managed" do
34f84c8d Michael Moll
nic = FactoryBot.build_stubbed(:nic_managed, :virtual => true, :attached_to => 'eth0', :managed => true, :identifier => 'eth0:0')
nic.host = FactoryBot.build_stubbed(:host)
nic.subnet = FactoryBot.build_stubbed(:subnet_ipv4, :boot_mode => Subnet::BOOT_MODES[:dhcp])
a45253c2 Marek Hulan
refute nic.valid?
assert_includes nic.errors.keys, :subnet_id

nic.subnet.boot_mode = Subnet::BOOT_MODES[:static]
nic.valid?
refute_includes nic.errors.keys, :subnet_id

nic.managed = false
nic.subnet.boot_mode = Subnet::BOOT_MODES[:dhcp]
nic.valid?
refute_includes nic.errors.keys, :subnet_id
end

4feb1576 Marek Hulan
test "BMC does not require identifier" do
8c6bc83e Marek Hulan
nic = FactoryBot.build(:nic_bmc, :managed => true, :identifier => '')
4feb1576 Marek Hulan
nic.valid?
refute_includes nic.errors.keys, :identifier
end

b3bdb092 Marek Hulan
test "Bond requires identifier if managed" do
8c6bc83e Marek Hulan
nic = FactoryBot.build(:nic_bond, :attached_devices => 'eth0,eth1', :managed => true, :identifier => 'bond0')
b3bdb092 Marek Hulan
nic.valid?
refute_includes nic.errors.keys, :identifier
end

test "Bond does not require identifier if not managed" do
8c6bc83e Marek Hulan
nic = FactoryBot.build(:nic_bond, :attached_devices => 'eth0,eth1', :managed => false, :identifier => '')
b3bdb092 Marek Hulan
nic.valid?
refute_includes nic.errors.keys, :identifier
end

6d05514a Tomas Strachota
context 'physical?' do
test 'returns true for a physical interface' do
34f84c8d Michael Moll
nic = FactoryBot.build_stubbed(:nic_managed, :virtual => false)
6d05514a Tomas Strachota
assert nic.physical?
end

test 'returns false for a virtual interface' do
34f84c8d Michael Moll
nic = FactoryBot.build_stubbed(:nic_managed, :virtual => true)
6d05514a Tomas Strachota
refute nic.physical?
end
end

57a32e98 Daniel Lobato
context 'BMC' do
setup do
43c4bd72 Marek Hulan
disable_orchestration
8c6bc83e Marek Hulan
@subnet = FactoryBot.create(:subnet_ipv4, :dhcp, :ipam => IPAM::MODES[:db])
@domain = FactoryBot.create(:domain)
@interface = FactoryBot.create(:nic_bmc, :ip => @subnet.unused_ip.suggest_ip,
:host => FactoryBot.create(:host),
43c4bd72 Marek Hulan
:subnet => @subnet, :domain => @domain, :name => 'bmc')
57a32e98 Daniel Lobato
end

test 'Nic::BMC should have hostname containing name and domain name' do
43c4bd72 Marek Hulan
assert_equal @interface.hostname, "#{@interface.shortname}.#{@domain.name}"
57a32e98 Daniel Lobato
end

test 'Nic::BMC should have hostname containing name when domain nil' do
@interface.domain = nil
assert_equal @interface.name, @interface.hostname
end

test '.proxy uses any BMC SmartProxy if none is found in subnet' do
assert @subnet.proxies.select { |proxy| proxy.features.map(&:name).include?('BMC') }
61802878 Shlomi Zadok
assert_includes SmartProxy.with_features('BMC').map { |sp| sp.url + '/bmc' }, @interface.proxy.url
57a32e98 Daniel Lobato
end

test '.proxy chooses BMC SmartProxy in Nic::BMC subnet if available' do
@subnet.dhcp.features << Feature.find_by_name('BMC')
assert_equal @interface.proxy.url, @subnet.dhcp.url + '/bmc'
end

test '.proxy raises exception if BMC SmartProxy cannot be found' do
61802878 Shlomi Zadok
SmartProxy.with_features('BMC').map {|sp| sp.features = []}
57a32e98 Daniel Lobato
assert_raise Foreman::Exception do
6a3b4abc Dominic Cleal
@interface.reload.proxy
57a32e98 Daniel Lobato
end
end
43c4bd72 Marek Hulan
6a2fce1f Marek Hulan
test "bmc requires MAC address if managed" do
8c6bc83e Marek Hulan
bmc = FactoryBot.build(:nic_bmc, :managed => true, :mac => '')
6a2fce1f Marek Hulan
refute bmc.valid?
assert_includes bmc.errors.keys, :mac
end

test "bmc does not require MAC address if unmanaged" do
8c6bc83e Marek Hulan
bmc = FactoryBot.build(:nic_bmc, :managed => false, :mac => '')
6a2fce1f Marek Hulan
bmc.valid?
refute_includes bmc.errors.keys, :mac
end

43c4bd72 Marek Hulan
context "on managed host" do
setup do
8c6bc83e Marek Hulan
@host = FactoryBot.create(:host, :managed, :ip => '127.0.0.1')
43c4bd72 Marek Hulan
end

test "we can't destroy primary interface of managed host" do
interface = @host.primary_interface
refute interface.destroy
assert_includes interface.errors.keys, :primary
end

test "we can destroy non primary interface of managed host" do
8c6bc83e Marek Hulan
interface = FactoryBot.create(:nic_managed, :primary => false, :host => @host)
43c4bd72 Marek Hulan
assert interface.destroy
end

test "we can destroy primary interface when deleting the host" do
interface = @host.primary_interface
refute interface.destroy

# we must reload the object (interface contains validation errors preventing deletion)
@host.reload
assert @host.destroy
end

test "we can't destroy provision interface of managed host" do
interface = @host.provision_interface
refute interface.destroy
assert_includes interface.errors.keys, :provision
end

test "we can destroy non provision interface of managed host" do
8c6bc83e Marek Hulan
interface = FactoryBot.create(:nic_managed, :provision => false, :host => @host)
43c4bd72 Marek Hulan
assert interface.destroy
end

test "we can destroy provision interface when deleting the host" do
interface = @host.provision_interface
refute interface.destroy

# we must reload the object (interface contains validtion errors preventin deletion)
@host.reload
assert @host.destroy
end
end

context "on unmanaged host" do
setup do
8c6bc83e Marek Hulan
@host = FactoryBot.create(:host)
43c4bd72 Marek Hulan
end

test "we can destroy any interface of unmanaged host" do
interface = @host.primary_interface
assert interface.destroy
end

test "we can destroy any interface of unmanaged host" do
interface = @host.provision_interface
assert interface.destroy
end

test "host can have one primary interface at most" do
# factory already created primary interface
8c6bc83e Marek Hulan
interface = FactoryBot.build(:nic_managed, :primary => true, :host => @host)
43c4bd72 Marek Hulan
refute interface.save
assert_includes interface.errors.keys, :primary

interface.primary = false
interface.name = ''
assert interface.save
end

test "provision flag is set for primary interface automatically" do
8c6bc83e Marek Hulan
primary = FactoryBot.build(:nic_managed, :primary => true, :provision => false,
:domain => FactoryBot.build(:domain))
host = FactoryBot.create(:host, :interfaces => [primary])
225cfa5b Dominic Cleal
assert host.save!
43c4bd72 Marek Hulan
primary.reload
225cfa5b Dominic Cleal
assert_equal primary, host.provision_interface
43c4bd72 Marek Hulan
end
end
57a32e98 Daniel Lobato
end
cad1b13c Tomas Strachota
context "allowed type registration" do
setup do
class DefaultTestNic < Nic::Base
end

class HumanizedTestNic < Nic::Base
def self.humanized_name
"Custom"
end
end

class DisallowedTestNic < Nic::Base
end

Nic::Base.register_type(DefaultTestNic)
Nic::Base.register_type(HumanizedTestNic)
end

test "base registers allowed nic types" do
expected_types = [DefaultTestNic, HumanizedTestNic]
445dd7ab Daniel Lobato
expected_types.map(&:name).each do |type|
assert Nic::Base.allowed_types.map(&:name).include? type
end
cad1b13c Tomas Strachota
end

test "type_by_name returns nil for an unknown name" do
edd5310f Dominic Cleal
assert_nil Nic::Base.type_by_name("UNKNOWN_NAME")
cad1b13c Tomas Strachota
end

test "type_by_name finds the class" do
assert_equal HumanizedTestNic, Nic::Base.type_by_name("custom")
end

test "type_by_name returns nil for classes that aren't allowed" do
edd5310f Dominic Cleal
assert_nil Nic::Base.type_by_name("DisallowedTestNic")
cad1b13c Tomas Strachota
end

3de2d5aa Michael Moll
test 'saved_change_to_fqdn? should be true if name changes' do
@nic.stubs(:saved_change_to_name?).returns(true)
@nic.stubs(:saved_change_to_domain_id?).returns(false)
assert @nic.saved_change_to_fqdn?
43c4bd72 Marek Hulan
end

3de2d5aa Michael Moll
test 'saved_change_to_fqdn? should be true if domain changes' do
@nic.stubs(:saved_change_to_name?).returns(false)
@nic.stubs(:saved_change_to_domain_id?).returns(true)
assert @nic.saved_change_to_fqdn?
43c4bd72 Marek Hulan
end

3de2d5aa Michael Moll
test 'saved_change_to_fqdn? should be true if name and domain change' do
@nic.stubs(:saved_change_to_name?).returns(true)
@nic.stubs(:saved_change_to_domain_id?).returns(true)
assert @nic.saved_change_to_fqdn?
43c4bd72 Marek Hulan
end
cad1b13c Tomas Strachota
end
cbe1391f Shlomi Zadok
test 'new nic name containing existing domain should set nic domain' do
8c6bc83e Marek Hulan
domain = FactoryBot.create(:domain)
host = FactoryBot.create(:host)
cbe1391f Shlomi Zadok
nic_name = [host.name, domain.name].join('.')
8c6bc83e Marek Hulan
interface = FactoryBot.create(:nic_managed, :host => host, :name => nic_name)
cbe1391f Shlomi Zadok
refute_nil(interface.domain)
assert_equal(interface.domain, domain)
end

53f87c0b Tomer Brisker
test 'new nic name containing existing subdomain should set nic domain correctly' do
nic_name = 'hostname.sub.bigdomain'
8c6bc83e Marek Hulan
interface = FactoryBot.build_stubbed(:nic_managed, :name => nic_name)
subdomain = FactoryBot.create(:domain, :name => 'sub.bigdomain')
53f87c0b Tomer Brisker
interface.send(:normalize_name)
assert_equal subdomain, interface.domain
end

test 'new nic name containing non-existing subdomain should not set nic domain' do
nic_name = 'hostname.undefined-subdomain.bigdomain'
8c6bc83e Marek Hulan
FactoryBot.create(:domain, :name => 'bigdomain')
interface = FactoryBot.build_stubbed(:nic_managed, :name => nic_name)
53f87c0b Tomer Brisker
interface.send(:normalize_name)
refute interface.domain
end

cbe1391f Shlomi Zadok
test 'new nic with non-existing domain should not set nic domain' do
8c6bc83e Marek Hulan
host = FactoryBot.create(:host)
cbe1391f Shlomi Zadok
nic_name = [host.name, 'domain.name'].join('.')
8c6bc83e Marek Hulan
interface = FactoryBot.create(:nic_managed, :host => host, :name => nic_name)
cbe1391f Shlomi Zadok
assert_nil(interface.domain)
end

test 'update nic domain should update nic name' do
8c6bc83e Marek Hulan
host = FactoryBot.create(:host)
cbe1391f Shlomi Zadok
existing_domain = Domain.first
8c6bc83e Marek Hulan
interface = FactoryBot.create(:nic_managed, :host => host, :name => 'nick')
cbe1391f Shlomi Zadok
# no domain
assert_equal(interface.name, 'nick')
9ae8fa19 Michael Moll
interface.update(:domain_id => existing_domain.id)
cbe1391f Shlomi Zadok
name_should_be = "nick.#{existing_domain.name}"
assert_equal(name_should_be, interface.name)
8c6bc83e Marek Hulan
new_domain = FactoryBot.create(:domain)
9ae8fa19 Michael Moll
interface.update(:domain_id => new_domain.id)
cbe1391f Shlomi Zadok
name_should_change_to = "nick.#{new_domain.name}"
assert_equal(name_should_change_to, interface.name)
end
f175f751 Baptiste Agasse
test 'nic MTU fact should override subnet MTU' do
domain = FactoryBot.create(:domain)
subnet = FactoryBot.create(:subnet_ipv4, :domains => [domain], :mtu => 9000)
interface = FactoryBot.build(:nic_managed, :name => 'nick', :subnet => subnet)
interface.attrs['mtu'] = 1500
assert_equal 1500, interface.mtu
end
6b65706a Baptiste Agasse
test 'nic with both subnet and subnet6 should be valid if VLAN ID is consistent between subnets' do
host = FactoryBot.create(:host)
domain = FactoryBot.create(:domain)
subnet = FactoryBot.create(:subnet_ipv4, :domains => [domain], :vlanid => 14)
subnet6 = FactoryBot.create(:subnet_ipv6, :domains => [domain], :vlanid => 14)
interface = FactoryBot.create(:nic_managed, :host => host, :name => 'nick', :subnet => subnet, :subnet6 => subnet6)
assert_valid interface
end

test 'nic with both subnet and subnet6 should not be valid if VLAN ID mismatch between subnets' do
host = FactoryBot.create(:host)
domain = FactoryBot.create(:domain)
subnet = FactoryBot.create(:subnet_ipv4, :domains => [domain], :vlanid => 3)
subnet6 = FactoryBot.create(:subnet_ipv6, :domains => [domain], :vlanid => 4)
interface = FactoryBot.build(:nic_managed, :host => host, :name => 'nick', :subnet => subnet, :subnet6 => subnet6)
refute_valid interface
assert_includes interface.errors.keys, :subnet_id

subnet6 = FactoryBot.create(:subnet_ipv6, :domains => [domain], :vlanid => nil)
interface = FactoryBot.build(:nic_managed, :host => host, :name => 'nick', :subnet => subnet, :subnet6 => subnet6)
refute_valid interface
assert_includes interface.errors.keys, :subnet_id
end

test 'nic with both subnet and subnet6 should be valid if MTU is consistent between subnets' do
host = FactoryBot.create(:host)
domain = FactoryBot.create(:domain)
subnet = FactoryBot.create(:subnet_ipv4, :domains => [domain], :mtu => 1496)
subnet6 = FactoryBot.create(:subnet_ipv6, :domains => [domain], :mtu => 1496)
interface = FactoryBot.build(:nic_managed, :host => host, :name => 'nick', :subnet => subnet, :subnet6 => subnet6)
assert_valid interface
end

test 'nic with both subnet and subnet6 should not be valid if MTU mismatch between subnets' do
host = FactoryBot.create(:host)
domain = FactoryBot.create(:domain)
subnet = FactoryBot.create(:subnet_ipv4, :domains => [domain], :mtu => 1496)
subnet6 = FactoryBot.create(:subnet_ipv6, :domains => [domain], :mtu => 1500)
interface = FactoryBot.build(:nic_managed, :host => host, :name => 'nick', :subnet => subnet, :subnet6 => subnet6)
refute_valid interface
assert_includes interface.errors.keys, :subnet_id
end
4bae5ced Stephen Benjamin
end