Skip to content

Commit

Permalink
make $::networking fact work in tests
Browse files Browse the repository at this point in the history
- Don't seed the legacy facts, or seed them with intentionally bad data
  to ensure they are not used.
- Don't clear out all the keys of $facts['networking'] when merging
  facts.
- Add deep_merge method to Hash in spec_helper, cribbed from
  ActiveSupport
  • Loading branch information
rrotter committed Dec 5, 2024
1 parent 6690c14 commit 3b7398f
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 33 deletions.
5 changes: 3 additions & 2 deletions spec/classes/profile/haproxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
networking: {
ip: my_ip,
primary: 'eth0',
hostname: 'thisnode',
},
ipaddress: my_ip,
hostname: 'thisnode',
ipaddress: 'INVALID_DO_NOT_USE',
hostname: 'INVALID_DO_NOT_USE',
)
end

Expand Down
13 changes: 9 additions & 4 deletions spec/classes/profile/hathitrust/hosts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@
end

let(:facts) do
os_facts.merge(
ipaddress: my_ip,
hostname: hostname,
fqdn: fqdn,
os_facts.deep_merge(
ipaddress: 'INVALID_DO_NOT_USE',
hostname: 'INVALID_DO_NOT_USE',
fqdn: 'INVALID_DO_NOT_USE',
networking: {
'ip' => my_ip,
'hostname' => hostname,
'fqdn' => fqdn,
},
)
end

Expand Down
6 changes: 4 additions & 2 deletions spec/classes/profile/known_host_public_keys_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
context 'with fqdn of example.invalid and some ssh public keys' do
let(:facts) do
{
'fqdn' => 'example.invalid',
'ssh' => {
networking: {
'fqdn' => 'example.invalid',
},
ssh: {
'ecdsa' => {
'type' => 'ecdsa-sha2-nistp256',
'key' => 'ecdsa_key',
Expand Down
12 changes: 8 additions & 4 deletions spec/classes/profile/kubernetes/dns_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,28 @@
context "on #{os}" do
let(:hiera_config) { 'spec/fixtures/hiera/kubernetes/first_cluster_config.yaml' }
let(:facts) do
os_facts.merge(
'networking' => {
my_facts = os_facts.deep_merge(
networking: {
'interfaces' => {
'ens4' => {
'ip' => '10.123.234.5',
},
},
'ip' => '10.123.234.5',
'primary' => 'ens4',
},
)
my_facts[:networking]['interfaces'].delete('eth0')
my_facts
end

it { is_expected.to compile }

it do
expect(exported_resources).to contain_concat_fragment("/etc/hosts ipv4 #{facts[:ipaddress]}")
expect(exported_resources).to contain_concat_fragment("/etc/hosts ipv4 #{facts[:networking]['ip']}")
.with_target('/etc/hosts')
.with_order('04')
.with_content("#{facts[:ipaddress]} #{facts[:fqdn]} #{facts[:hostname]}\n")
.with_content("#{facts[:networking]['ip']} #{facts[:fqdn]} #{facts[:hostname]}\n")
end

it do
Expand Down
5 changes: 3 additions & 2 deletions spec/classes/profile/kubernetes/keepalived_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@
context 'with fqdn of default.invalid and an ssh-rsa public key' do
let(:facts) do
{
'fqdn' => 'default.invalid',
'ssh' => {
fqdn: 'INVALID_DO_NOT_USE',
networking: { 'fqdn' => 'default.invalid' },
ssh: {
'rsa' => {
'type' => 'ssh-rsa',
'key' => 'abc123',
Expand Down
9 changes: 8 additions & 1 deletion spec/classes/profile/quod/prod/haproxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
describe 'nebula::profile::quod::prod::haproxy' do
on_supported_os.each do |os, os_facts|
context "on #{os}" do
let(:facts) { os_facts.merge(hostname: 'thisnode', datacenter: 'somedc') }
let(:facts) do
os_facts.deep_merge(
datacenter: 'somedc',
networking: {
'hostname' => 'thisnode',
},
)
end

it { is_expected.to compile }

Expand Down
2 changes: 1 addition & 1 deletion spec/classes/role/deb_server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
context "on #{os}" do
include_context 'with mocked query for nodes in other datacenters'

let(:facts) { os_facts.merge(networking: { ip: Faker::Internet.ip_v4_address, interfaces: {} }) }
let(:facts) { os_facts.deep_merge(networking: { ip: Faker::Internet.ip_v4_address, interfaces: {} }) }
let(:hiera_config) { 'spec/fixtures/hiera/deb_server_config.yaml' }

it { is_expected.to compile }
Expand Down
8 changes: 4 additions & 4 deletions spec/classes/role/htvm_webhost_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
on_supported_os.each do |os, os_facts|
context "on #{os}" do
include_context 'with setup for htvm node', os_facts

# binding.irb
it { is_expected.to compile }

it do
Expand Down Expand Up @@ -40,10 +40,10 @@

context 'with ens4' do
let(:facts) do
os_facts.merge(
os_facts.deep_merge(
networking: {
ip: '1.2.3.123',
interfaces: { 'ens4' => {} },
'ip' => '1.2.3.123',
'interfaces' => { 'ens4' => {} },
},
is_virtual: true,
)
Expand Down
28 changes: 18 additions & 10 deletions spec/classes/role/kubernetes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@
context "on #{os}" do
let(:hiera_config) { 'spec/fixtures/hiera/kubernetes/first_cluster_config.yaml' }
let(:facts) do
os_facts.merge(
'networking' => {
my_facts = os_facts.deep_merge(
networking: {
'interfaces' => {
'ens4' => {
'ip' => '10.123.234.5',
},
},
'ip' => '10.123.234.5',
'primary' => 'ens4',
},
)
my_facts[:networking]['interfaces'].delete('eth0')
my_facts
end

it { is_expected.to contain_class('Nebula::Profile::Ntp') }
Expand All @@ -40,15 +44,19 @@
context "on #{os}" do
let(:hiera_config) { 'spec/fixtures/hiera/kubernetes/first_cluster_config.yaml' }
let(:facts) do
os_facts.merge(
'networking' => {
my_facts = os_facts.deep_merge(
networking: {
'interfaces' => {
'ens4' => {
'ip' => '10.123.234.5',
},
},
'ip' => '10.123.234.5',
'primary' => 'ens4',
},
)
my_facts[:networking]['interfaces'].delete('eth0')
my_facts
end

it { is_expected.to contain_class('Nebula::Profile::Ntp') }
Expand Down Expand Up @@ -89,27 +97,27 @@
case role
when 'etcd'
it do
expect(exported_resources).to contain_concat_fragment("cluster pki for #{facts[:hostname]}")
expect(exported_resources).to contain_concat_fragment("cluster pki for #{facts[:networking]['hostname']}")
.with_tag('first_cluster_pki_generation')
.with_target('/var/local/generate_pki.sh')
.with_order('02')
.with_content("ETCD_NODES=(\"${ETCD_NODES[@]}\" \"#{facts[:hostname]}/#{facts[:ipaddress]}\")\n")
.with_content("ETCD_NODES=(\"${ETCD_NODES[@]}\" \"#{facts[:networking]['hostname']}/#{facts[:networking]['ip']}\")\n")
end
when 'controller'
it do
expect(exported_resources).to contain_concat_fragment("cluster pki for #{facts[:hostname]}")
expect(exported_resources).to contain_concat_fragment("cluster pki for #{facts[:networking]['hostname']}")
.with_tag('first_cluster_pki_generation')
.with_target('/var/local/generate_pki.sh')
.with_order('02')
.with_content("KUBE_CONTROLLERS=(\"${KUBE_CONTROLLERS[@]}\" \"#{facts[:hostname]}/#{facts[:ipaddress]}\")\n")
.with_content("KUBE_CONTROLLERS=(\"${KUBE_CONTROLLERS[@]}\" \"#{facts[:networking]['hostname']}/#{facts[:networking]['ip']}\")\n")
end
when 'worker'
it do
expect(exported_resources).to contain_concat_fragment("cluster pki for #{facts[:hostname]}")
expect(exported_resources).to contain_concat_fragment("cluster pki for #{facts[:networking]['hostname']}")
.with_tag('first_cluster_pki_generation')
.with_target('/var/local/generate_pki.sh')
.with_order('02')
.with_content("KUBE_WORKERS=(\"${KUBE_WORKERS[@]}\" \"#{facts[:hostname]}/#{facts[:ipaddress]}\")\n")
.with_content("KUBE_WORKERS=(\"${KUBE_WORKERS[@]}\" \"#{facts[:networking]['hostname']}/#{facts[:networking]['ip']}\")\n")
end

it { is_expected.to contain_package('lvm2') }
Expand Down
20 changes: 20 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,23 @@ def stub(name)
def allow_call(dbl)
allow(dbl).to receive(:call)
end

class Hash
# deep_merge copied with minor adaptation from:
# https://github.com/rails/rails/blob/main/activesupport/lib/active_support/deep_mergeable.rb
def deep_merge(other, &block)
dup.deep_merge!(other, &block)
end

def deep_merge!(other, &block)
merge!(other) do |key, this_val, other_val|
if this_val.is_a?(Hash) && other_val.is_a?(Hash)
this_val.deep_merge(other_val, &block)
elsif block_given?
block.call(key, this_val, other_val)
else
other_val
end
end
end
end
5 changes: 2 additions & 3 deletions spec/support/contexts/with_htvm_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

RSpec.shared_context 'with setup for htvm node' do |os_facts|
let(:facts) do
os_facts.merge(
hostname: 'thisnode',
os_facts.deep_merge(
datacenter: 'somedc',
networking: { ip: Faker::Internet.ip_v4_address, interfaces: {} },
networking: { 'ip' => Faker::Internet.ip_v4_address, 'interfaces' => {}, 'hostname' => 'thisnode' },
)
end
let(:hiera_config) { 'spec/fixtures/hiera/hathitrust_config.yaml' }
Expand Down

0 comments on commit 3b7398f

Please sign in to comment.