Project

General

Profile

Download (12 KB) Statistics
| Branch: | Tag: | Revision:
031aec3e Ohad Levy
require "test_helper"

1d750dd7 Marek Hulan
class PuppetFactsParserTest < ActiveSupport::TestCase
031aec3e Ohad Levy
attr_reader :importer

def setup
1d750dd7 Marek Hulan
@importer = PuppetFactParser.new facts
e07f9a12 Dominic Cleal
User.current = users :admin
031aec3e Ohad Levy
end

test "should return list of interfaces" do
assert importer.interfaces.present?
34f84c8d Michael Moll
assert_not_nil importer.suggested_primary_interface(FactoryBot.build(:host))
e0910b7e Michael Moll
assert importer.interfaces.key?(importer.suggested_primary_interface(FactoryBot.build(:host)).first)
031aec3e Ohad Levy
end

d455f32c Marek Hulan
test "should parse virtual interfaces as vlan interfaces" do
parser = PuppetFactParser.new({'interfaces' => 'eth0_0', 'ipaddress_eth0_0' => '192.168.0.1'})
assert_equal 'eth0.0', parser.interfaces.keys.first
assert_equal '192.168.0.1', parser.interfaces['eth0.0']['ipaddress']
end

031aec3e Ohad Levy
test "should return an env" do
assert_kind_of Environment, importer.environment
end

test "should return an arch" do
assert_kind_of Architecture, importer.architecture
end

test "should return a model" do
assert_kind_of Model, importer.model
end

test "should return a domain" do
assert_kind_of Domain, importer.domain
end

1285cc0d Dominic Cleal
describe '#operatingsystem' do
let(:os) { importer.operatingsystem }
fe838fad Greg Sutcliffe
1285cc0d Dominic Cleal
test "should return an os" do
assert_kind_of Operatingsystem, os
assert_os_idempotent
end
9628014a Joseph Mitchell Magen
1285cc0d Dominic Cleal
test "should raise on an invalid os" do
@importer = PuppetFactParser.new({})
assert_raise ::Foreman::Exception do
importer.operatingsystem
end
end
d74b482e Florian Ernst
1285cc0d Dominic Cleal
test "should make non-numeric os version strings into numeric" do
@importer = PuppetFactParser.new({'operatingsystem' => 'AnyOS', 'operatingsystemrelease' => '1&2.3y4'})
assert_equal '12', os.major
assert_equal '34', os.minor
assert_os_idempotent
end
d74b482e Florian Ernst
1285cc0d Dominic Cleal
test "should allow OS version minor component to be nil" do
@importer = PuppetFactParser.new({'operatingsystem' => 'AnyOS', 'operatingsystemrelease' => '6'})
assert_equal "AnyOS 6", os.to_s
assert_equal '6', os.major
assert_empty os.minor
assert_os_idempotent
end
d74b482e Florian Ernst
1285cc0d Dominic Cleal
test "release_name should be nil when lsbdistcodename isn't set on Debian" do
@importer = PuppetFactParser.new(debian_facts.delete_if { |k, v| k == "lsbdistcodename" })
edd5310f Dominic Cleal
assert_nil os.release_name
1285cc0d Dominic Cleal
assert_os_idempotent
end
c2c32409 Greg Sutcliffe
1285cc0d Dominic Cleal
test "should set os.release_name to the lsbdistcodename fact on Debian" do
@importer = PuppetFactParser.new(debian_facts)
assert_equal 'wheezy', os.release_name
assert_os_idempotent
end
c2c32409 Greg Sutcliffe
1285cc0d Dominic Cleal
test "should not set os.release_name to the lsbdistcodename on non-Debian OS" do
assert_not_equal 'Santiago', os.release_name
end
07a1bb33 Michael Moll
1285cc0d Dominic Cleal
test "should set description field from lsbdistdescription" do
assert_equal "RHEL Server 6.2", os.description
end
c2c32409 Greg Sutcliffe
1285cc0d Dominic Cleal
test "should not alter description field if already set" do
# Need to instantiate @importer once with normal facts
first_os = @importer.operatingsystem
assert first_os.present?
# Now re-import with a different description
facts_with_desc = facts.merge({:lsbdistdescription => "A different string"})
@importer = PuppetFactParser.new facts_with_desc
second_os = @importer.operatingsystem
assert_equal "RHEL Server 6.2", second_os.description
assert_equal first_os, second_os
end
af6d54a3 Tomer Brisker
1285cc0d Dominic Cleal
test "should set description correctly for SLES" do
@importer = PuppetFactParser.new(sles_facts)
assert_equal 'SLES 11 SP3', os.description
assert_os_idempotent
end
7e2880b6 Ruediger Mueck
65c0d142 Michael Moll
test "should set version correctly for PSBM" do
@importer = PuppetFactParser.new("operatingsystem" => "PSBM",
"operatingsystemrelease" => "2.6.32-042stab111.11")
assert_equal '2', os.major
assert_equal '6', os.minor
assert_os_idempotent
end

1285cc0d Dominic Cleal
test "should not set description if lsbdistdescription is missing" do
facts.delete('lsbdistdescription')
@importer = PuppetFactParser.new(facts)
refute os.description
assert_os_idempotent
end
d4cac085 Michael Moll
1285cc0d Dominic Cleal
test 'should accept y.z minor version' do
34f84c8d Michael Moll
FactoryBot.build(:operatingsystem, name: "CentOS",
1285cc0d Dominic Cleal
major: "7",
minor: "2.1511",
description: "CentOS Linux 7.2.1511")
@importer = PuppetFactParser.new("operatingsystem" => "CentOS",
"lsbdistdescription" => "CentOS Linux release 7.2.1511 (Core) ",
"operatingsystemrelease" => "7.2.1511")
assert_valid os
assert_os_idempotent
end
d4cac085 Michael Moll
1285cc0d Dominic Cleal
test "should set os.major and minor correctly from AIX facts" do
@importer = PuppetFactParser.new(aix_facts)
assert_equal 'AIX', os.family
assert_equal '6100', os.major
assert_equal '0604', os.minor
assert_os_idempotent
end

test 'should handle FreeBSD rolling releases correctly' do
@importer = PuppetFactParser.new(freebsd_stable_facts)
assert_equal '10', os.major
assert_equal '1', os.minor
assert_os_idempotent
end

test 'should handle FreeBSD patch releases correctly' do
@importer = PuppetFactParser.new(freebsd_patch_facts)
assert_equal '10', os.major
assert_equal '1', os.minor
assert_os_idempotent
end

test "should set os.major and minor correctly from Solaris 10 facts" do
@importer = PuppetFactParser.new(read_json_fixture('facts/solaris10.json'))
os = @importer.operatingsystem
assert_equal 'Solaris', os.family
assert_equal '10', os.major
assert_equal '9', os.minor
assert_os_idempotent
end
13875289 Dominic Cleal
end

d455f32c Marek Hulan
test "#get_interfaces" do
34f84c8d Michael Moll
host = FactoryBot.build(:host, :hostgroup => FactoryBot.build(:hostgroup))
d455f32c Marek Hulan
parser = get_parser(host.facts_hash)

assert_empty parser.send(:get_interfaces)

34f84c8d Michael Moll
interfaces = FactoryBot.build(:fact_value,
:fact_name => FactoryBot.build(:fact_name, :name => 'interfaces'),
d455f32c Marek Hulan
:host => host,
:value => '')
parser = get_parser(host.facts_hash)
assert_empty parser.send(:get_interfaces)

interfaces.update_attribute :value, 'lo,eth0,eth0.0,eth1'
parser = get_parser(host.facts_hash)
%w(lo eth0 eth0.0 eth1).each do |interface|
assert_includes parser.send(:get_interfaces), interface
end
end

test "#get_facts_for_interface(interface)" do
34f84c8d Michael Moll
host = FactoryBot.build(:host, :hostgroup => FactoryBot.build(:hostgroup))
8c6bc83e Marek Hulan
FactoryBot.create(:fact_value,
:fact_name => FactoryBot.create(:fact_name, :name => 'link_eth0'),
d455f32c Marek Hulan
:host => host,
:value => 'true')
8c6bc83e Marek Hulan
FactoryBot.create(:fact_value,
:fact_name => FactoryBot.create(:fact_name, :name => 'macaddress_eth0'),
d455f32c Marek Hulan
:host => host,
:value => '00:00:00:00:00:ab')
8c6bc83e Marek Hulan
FactoryBot.create(:fact_value,
:fact_name => FactoryBot.create(:fact_name, :name => 'ipaddress_eth0'),
d455f32c Marek Hulan
:host => host,
:value => '192.168.0.1')
8c6bc83e Marek Hulan
FactoryBot.create(:fact_value,
:fact_name => FactoryBot.create(:fact_name, :name => 'custom_fact_eth0'),
d455f32c Marek Hulan
:host => host,
:value => 'custom_value')
8c6bc83e Marek Hulan
FactoryBot.create(:fact_value,
:fact_name => FactoryBot.create(:fact_name, :name => 'link_eth0_0'),
d455f32c Marek Hulan
:host => host,
:value => 'false')
8c6bc83e Marek Hulan
FactoryBot.create(:fact_value,
:fact_name => FactoryBot.create(:fact_name, :name => 'macaddress_eth0_0'),
d455f32c Marek Hulan
:host => host,
:value => '00:00:00:00:00:cd')
8c6bc83e Marek Hulan
FactoryBot.create(:fact_value,
:fact_name => FactoryBot.create(:fact_name, :name => 'ipaddress_eth0_0'),
d455f32c Marek Hulan
:host => host,
:value => '192.168.0.2')
8c6bc83e Marek Hulan
FactoryBot.create(:fact_value,
:fact_name => FactoryBot.create(:fact_name, :name => 'custom_fact_eth0_0'),
d455f32c Marek Hulan
:host => host,
:value => 'another_value')
parser = get_parser(host.facts_hash)

result = parser.send(:get_facts_for_interface, 'eth0')
assert_equal 'true', result[:link]
assert_equal '00:00:00:00:00:ab', result['macaddress']
assert_equal '192.168.0.1', result['ipaddress']
assert_equal 'custom_value', result['custom_fact']
end

test "#ipmi_interface" do
34f84c8d Michael Moll
host = FactoryBot.build(:host, :hostgroup => FactoryBot.build(:hostgroup))
d455f32c Marek Hulan
parser = get_parser(host.facts_hash)

result = parser.ipmi_interface
assert_equal({}, result)

8c6bc83e Marek Hulan
FactoryBot.create(:fact_value,
:fact_name => FactoryBot.create(:fact_name, :name => 'ipmi_ipaddress'),
d455f32c Marek Hulan
:host => host,
:value => '192.168.0.1')
8c6bc83e Marek Hulan
FactoryBot.create(:fact_value,
:fact_name => FactoryBot.create(:fact_name, :name => 'ipmi_custom'),
d455f32c Marek Hulan
:host => host,
:value => 'custom_value')
parser = get_parser(host.facts_hash)

result = parser.ipmi_interface
assert result.present?
assert_equal '192.168.0.1', result[:ipaddress]
assert_equal 'custom_value', result['custom']
end

test "#interfaces with underscores are mapped correctly" do
parser = get_parser({:interfaces => 'eth1_1,eth1_2,eth1,eth2',
:ipaddress_eth1_1 => '192.168.0.1',
:ipaddress_eth1_2 => '192.168.0.2',
:ipaddress_eth1 => '192.168.0.3',
:ipaddress_eth2 => '192.168.0.4'})
assert_not_nil parser.interfaces['eth1.1']
assert_equal '192.168.0.1', parser.interfaces['eth1.1'][:ipaddress]
assert_not_nil parser.interfaces['eth1.2']
assert_equal '192.168.0.2', parser.interfaces['eth1.2'][:ipaddress]
assert_not_nil parser.interfaces['eth1']
assert_equal '192.168.0.3', parser.interfaces['eth1'][:ipaddress]
assert_not_nil parser.interfaces['eth2']
assert_equal '192.168.0.4', parser.interfaces['eth2'][:ipaddress]
end

d07bcaf0 Dominic Cleal
test "#interfaces are mapped case-insensitively and parses Windows LAN name" do
parser = get_parser({:interfaces => 'Local_Area_Connection_2',
:ipaddress_local_area_connection_2 => '172.30.43.87',
:macaddress_local_area_connection_2 => '00:50:56:B7:69:F6',
:netmask_local_area_connection_2 => '255.255.255.0',
:network_local_area_connection_2 => '172.30.43.0'})
assert_not_nil parser.interfaces['local_area_connection_2']
assert_equal '172.30.43.87', parser.interfaces['local_area_connection_2'][:ipaddress]
assert_equal '255.255.255.0', parser.interfaces['local_area_connection_2'][:netmask]
assert_equal '00:50:56:B7:69:F6', parser.interfaces['local_area_connection_2'][:macaddress]
assert_equal '172.30.43.0', parser.interfaces['local_area_connection_2'][:network]
end

031aec3e Ohad Levy
private

d455f32c Marek Hulan
def get_parser(facts)
PuppetFactParser.new(facts)
end

031aec3e Ohad Levy
def facts
1d750dd7 Marek Hulan
# return the equivalent of Facter.to_hash
ea98080f Dominic Cleal
@json ||= read_json_fixture('facts/facts.json')['facts']
031aec3e Ohad Levy
end

d74b482e Florian Ernst
def debian_facts
ea98080f Dominic Cleal
read_json_fixture('facts/facts_debian.json')['facts']
d74b482e Florian Ernst
end
7e2880b6 Ruediger Mueck
07a1bb33 Michael Moll
def sles_facts
ea98080f Dominic Cleal
read_json_fixture('facts/facts_sles.json')['facts']
07a1bb33 Michael Moll
end

7e2880b6 Ruediger Mueck
def aix_facts
ea98080f Dominic Cleal
read_json_fixture('facts/facts_aix.json')['facts']
7e2880b6 Ruediger Mueck
end
d4cac085 Michael Moll
def freebsd_stable_facts
ea98080f Dominic Cleal
read_json_fixture('facts/facts_freebsd_stable.json')['facts']
d4cac085 Michael Moll
end

def freebsd_patch_facts
ea98080f Dominic Cleal
read_json_fixture('facts/facts_freebsd_patch.json')['facts']
d4cac085 Michael Moll
end
1285cc0d Dominic Cleal
def assert_os_idempotent(previous_os = self.os)
assert_equal previous_os, importer.operatingsystem, 'Different operating system returned on second call'
assert_equal previous_os.attributes, importer.operatingsystem.attributes, 'Different operating system attributes set on second call'
end
fe838fad Greg Sutcliffe
end