From d085463986e8fcca916ab8e72a3dfc17ca1c0e3b Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Thu, 31 Aug 2017 02:27:48 +0200 Subject: [PATCH] Switch to rspec-puppet-facts --- spec/classes/rabbitmq_spec.rb | 539 ++++++++++------------------------ spec/spec_helper_local.rb | 88 +----- 2 files changed, 159 insertions(+), 468 deletions(-) diff --git a/spec/classes/rabbitmq_spec.rb b/spec/classes/rabbitmq_spec.rb index de6ff0d16..473d5c330 100644 --- a/spec/classes/rabbitmq_spec.rb +++ b/spec/classes/rabbitmq_spec.rb @@ -9,312 +9,187 @@ end end - context 'on Debian' do - with_debian_facts - it 'does not include rabbitmq::repo::apt' do - is_expected.not_to contain_class('rabbitmq::repo::apt') - end + # TODO: get Archlinux & OpenBSD facts from facterdb - it 'does ensure rabbitmq apt::source is absent when repos_ensure is false' do - is_expected.not_to contain_apt__source('rabbitmq') - end - end + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) { facts } - context 'on Debian' do - let(:params) { { repos_ensure: true } } + has_systemd = ( + (facts[:osfamily] == 'RedHat' && facts[:os]["release"]["major"].to_i >= 7) || + (facts[:osfamily] == 'Debian' && facts[:os]["release"]["full"] == '16.04') + ) - with_debian_facts + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_class('rabbitmq::install') } + it { is_expected.to contain_class('rabbitmq::config') } + it { is_expected.to contain_class('rabbitmq::service') } - it 'includes rabbitmq::repo::apt' do - is_expected.to contain_class('rabbitmq::repo::apt') - end + it { is_expected.to contain_package('rabbitmq-server').with_ensure('installed').with_name('rabbitmq-server') } - describe 'apt::source default values' do - it 'adds a repo with default values' do - is_expected.to contain_apt__source('rabbitmq').with(ensure: 'present', - location: 'http://www.rabbitmq.com/debian/', - release: 'testing', - repos: 'main') + context 'with default params' do + it { is_expected.not_to contain_class('rabbitmq::repo::apt') } + it { is_expected.not_to contain_apt__source('rabbitmq') } + it { is_expected.not_to contain_class('rabbitmq::repo::rhel') } + it { is_expected.not_to contain_yumrepo('rabbitmq') } end - end - - context 'with file_limit => unlimited' do - let(:params) { { file_limit: 'unlimited' } } - - it { is_expected.to contain_file('/etc/default/rabbitmq-server').with_content(%r{ulimit -n unlimited}) } - end - - context 'with file_limit => infinity' do - let(:params) { { file_limit: 'infinity' } } - - it { is_expected.to contain_file('/etc/default/rabbitmq-server').with_content(%r{ulimit -n infinity}) } - end - - context 'with file_limit => \'-1\'' do - let(:params) { { file_limit: '-1' } } - - it { is_expected.to contain_file('/etc/default/rabbitmq-server').with_content(%r{ulimit -n -1}) } - end - context 'with file_limit => \'1234\'' do - let(:params) { { file_limit: '1234' } } + context 'with repos_ensure => true' do + let(:params) { { repos_ensure: true } } - it { is_expected.to contain_file('/etc/default/rabbitmq-server').with_content(%r{ulimit -n 1234}) } - end - - context 'with file_limit => 1234' do - let(:params) { { file_limit: 1234 } } + case facts[:osfamily] + when 'Debian' + it 'includes rabbitmq::repo::apt' do + is_expected.to contain_class('rabbitmq::repo::apt') + .with_key_source('https://www.rabbitmq.com/rabbitmq-release-signing-key.asc') + .with_key_content(nil) + end - it { is_expected.to contain_file('/etc/default/rabbitmq-server').with_content(%r{ulimit -n 1234}) } - end + it 'adds a repo with default values' do + is_expected.to contain_apt__source('rabbitmq') + .with_ensure('present') + .with_location('http://www.rabbitmq.com/debian/') + .with_release('testing') + .with_repos('main') + end - context 'with file_limit => \'-42\'' do - let(:params) { { file_limit: '-42' } } + it { is_expected.not_to contain_class('rabbitmq::repo::rhel') } + when 'RedHat' + it { is_expected.not_to contain_class('rabbitmq::repo::apt') } + it { is_expected.to contain_class('rabbitmq::repo::rhel') } - it 'does not compile' do - expect { catalogue }.to raise_error(Puppet::Error, %r{\$file_limit must be a positive integer, '-1', 'unlimited', or 'infinity'}) + it 'the repo should be present, and contain the expected values' do + is_expected.to contain_yumrepo('rabbitmq') + .with_ensure('present') + .with_baseurl(%r{https://packagecloud.io/rabbitmq/rabbitmq-server/el/\d+/\$basearch$}) + .with_gpgkey('https://www.rabbitmq.com/rabbitmq-release-signing-key.asc') + end + else + it { is_expected.not_to contain_class('rabbitmq::repo::apt') } + it { is_expected.not_to contain_class('rabbitmq::repo::rhel') } + end end - end - context 'with file_limit => \'foo\'' do - let(:params) { { file_limit: 'foo' } } + context 'with no pin', :if => facts[:osfamily] == 'Debian' do + let(:params) { { repos_ensure: true, package_apt_pin: '' } } - it 'does not compile' do - expect { catalogue }.to raise_error(Puppet::Error, %r{\$file_limit must be a positive integer, '-1', 'unlimited', or 'infinity'}) + describe 'it sets up an apt::source' do + it { + is_expected.to contain_apt__source('rabbitmq').with( + 'location' => 'http://www.rabbitmq.com/debian/', + 'release' => 'testing', + 'repos' => 'main', + 'key' => '{"id"=>"0A9AF2115F4687BD29803A206B73A36E6026DFCA", "source"=>"https://www.rabbitmq.com/rabbitmq-release-signing-key.asc", "content"=>:undef}' + ) + } + end end - end - end - context 'on Redhat' do - with_redhat_facts - it 'does not include rabbitmq::repo::rhel' do - is_expected.not_to contain_class('rabbitmq::repo::rhel') - end - - context 'with file_limit => \'unlimited\'' do - let(:params) { { file_limit: 'unlimited' } } - - it { - is_expected.to contain_file('/etc/security/limits.d/rabbitmq-server.conf').with( - 'owner' => '0', - 'group' => '0', - 'mode' => '0644', - 'notify' => 'Class[Rabbitmq::Service]', - 'content' => <<-EOS -rabbitmq soft nofile unlimited -rabbitmq hard nofile unlimited -EOS - ) - } - end - - context 'with file_limit => \'infinity\'' do - let(:params) { { file_limit: 'infinity' } } - - it { - is_expected.to contain_file('/etc/security/limits.d/rabbitmq-server.conf').with( - 'owner' => '0', - 'group' => '0', - 'mode' => '0644', - 'notify' => 'Class[Rabbitmq::Service]', - 'content' => <<-EOS -rabbitmq soft nofile infinity -rabbitmq hard nofile infinity -EOS - ) - } - end + context 'with pin', :if => facts[:osfamily] == 'Debian' do + let(:params) { { repos_ensure: true, package_apt_pin: '700' } } - context 'with file_limit => \'-1\'' do - let(:params) { { file_limit: '-1' } } - - it { - is_expected.to contain_file('/etc/security/limits.d/rabbitmq-server.conf').with( - 'owner' => '0', - 'group' => '0', - 'mode' => '0644', - 'notify' => 'Class[Rabbitmq::Service]', - 'content' => <<-EOS -rabbitmq soft nofile -1 -rabbitmq hard nofile -1 -EOS - ) - } - end - - context 'with file_limit => \'1234\'' do - let(:params) { { file_limit: '1234' } } - - it { - is_expected.to contain_file('/etc/security/limits.d/rabbitmq-server.conf').with( - 'owner' => '0', - 'group' => '0', - 'mode' => '0644', - 'notify' => 'Class[Rabbitmq::Service]', - 'content' => <<-EOS -rabbitmq soft nofile 1234 -rabbitmq hard nofile 1234 -EOS - ) - } - end - - context 'with file_limit => \'-42\'' do - let(:params) { { file_limit: '-42' } } - - it 'does not compile' do - expect { catalogue }.to raise_error(Puppet::Error, %r{\$file_limit must be a positive integer, '-1', 'unlimited', or 'infinity'}) - end - end - - context 'with file_limit => \'foo\'' do - let(:params) { { file_limit: 'foo' } } + describe 'it sets up an apt::source and pin' do + it { + is_expected.to contain_apt__source('rabbitmq').with( + 'location' => 'http://www.rabbitmq.com/debian/', + 'release' => 'testing', + 'repos' => 'main', + 'key' => '{"id"=>"0A9AF2115F4687BD29803A206B73A36E6026DFCA", "source"=>"https://www.rabbitmq.com/rabbitmq-release-signing-key.asc", "content"=>:undef}' + ) + } - it 'does not compile' do - expect { catalogue }.to raise_error(Puppet::Error, %r{\$file_limit must be a positive integer, '-1', 'unlimited', or 'infinity'}) + it { + is_expected.to contain_apt__pin('rabbitmq').with( + 'packages' => '*', + 'priority' => '700', + 'origin' => 'www.rabbitmq.com' + ) + } + end end - end - end - - context 'on Redhat' do - let(:params) { { repos_ensure: false } } - with_redhat_facts - it 'does not contain class rabbitmq::repo::rhel when repos_ensure is false' do - is_expected.not_to contain_class('rabbitmq::repo::rhel') - end - it 'does not contain "rabbitmq" repo' do - is_expected.not_to contain_yumrepo('rabbitmq') - end - end - - context 'on Redhat' do - let(:params) { { repos_ensure: true } } - - with_redhat_facts - it 'contains class rabbitmq::repo::rhel' do - is_expected.to contain_class('rabbitmq::repo::rhel') - end - it 'contains "rabbitmq" repo' do - is_expected.to contain_yumrepo('rabbitmq') - end - it 'the repo should be present, and contain the expected values' do - is_expected.to contain_yumrepo('rabbitmq').with(ensure: 'present', - baseurl: 'https://packagecloud.io/rabbitmq/rabbitmq-server/el/7/$basearch', - gpgkey: 'https://www.rabbitmq.com/rabbitmq-release-signing-key.asc') - end - end - - context 'on RedHat 7.0 or higher' do - with_redhat_facts - - it { - is_expected.to contain_file('/etc/systemd/system/rabbitmq-server.service.d').with( - 'ensure' => 'directory', - 'owner' => '0', - 'group' => '0', - 'mode' => '0755', - 'selinux_ignore_defaults' => true - ) - } - - it { - is_expected.to contain_exec('rabbitmq-systemd-reload').with( - 'command' => '/usr/bin/systemctl daemon-reload', - 'notify' => 'Class[Rabbitmq::Service]', - 'refreshonly' => true - ) - } - context 'with file_limit => \'unlimited\'' do - let(:params) { { file_limit: 'unlimited' } } - - it { - is_expected.to contain_file('/etc/systemd/system/rabbitmq-server.service.d/limits.conf').with( - 'owner' => '0', - 'group' => '0', - 'mode' => '0644', - 'notify' => 'Exec[rabbitmq-systemd-reload]', - 'content' => <<-EOS -[Service] -LimitNOFILE=unlimited -EOS - ) - } - end - - context 'with file_limit => \'infinity\'' do - let(:params) { { file_limit: 'infinity' } } + ['unlimited', 'infinity', '-1', '1234'].each do |value| + context "with file_limit => '#{value}'" do + let(:params) { { file_limit: value } } + + + case facts[:osfamily] + when 'RedHat' + it { is_expected.not_to contain_file('/etc/default/rabbitmq-server') } + it { + is_expected.to contain_file('/etc/security/limits.d/rabbitmq-server.conf').with( + 'owner' => '0', + 'group' => '0', + 'mode' => '0644', + 'notify' => 'Class[Rabbitmq::Service]', + 'content' => <<-EOS +rabbitmq soft nofile #{value} +rabbitmq hard nofile #{value} + EOS + ) + } + when 'Debian' + it { is_expected.to contain_file('/etc/default/rabbitmq-server').with_content(%r{ulimit -n #{value}}) } + it { is_expected.not_to contain_file('/etc/security/limits.d/rabbitmq-server.conf') } + else + it { is_expected.not_to contain_file('/etc/default/rabbitmq-server') } + it { is_expected.not_to contain_file('/etc/security/limits.d/rabbitmq-server.conf') } + end - it { - is_expected.to contain_file('/etc/systemd/system/rabbitmq-server.service.d/limits.conf').with( - 'owner' => '0', - 'group' => '0', - 'mode' => '0644', - 'notify' => 'Exec[rabbitmq-systemd-reload]', - 'content' => <<-EOS + if has_systemd + it { + is_expected.to contain_file('/etc/systemd/system/rabbitmq-server.service.d/limits.conf').with( + 'owner' => '0', + 'group' => '0', + 'mode' => '0644', + 'notify' => 'Exec[rabbitmq-systemd-reload]', + 'content' => <<-EOS [Service] -LimitNOFILE=infinity -EOS - ) - } - end - - context 'with file_limit => \'-1\'' do - let(:params) { { file_limit: '-1' } } +LimitNOFILE=#{value} + EOS + ) + } + end + end + end - it { - is_expected.to contain_file('/etc/systemd/system/rabbitmq-server.service.d/limits.conf').with( - 'owner' => '0', - 'group' => '0', - 'mode' => '0644', - 'notify' => 'Exec[rabbitmq-systemd-reload]', - 'content' => <<-EOS -[Service] -LimitNOFILE=-1 -EOS - ) - } - end + ['-42', 'foo'].each do |value| + context "with file_limit => '#{value}'" do + let(:params) { { file_limit: value } } - context 'with file_limit => \'1234\'' do - let(:params) { { file_limit: '1234' } } + it 'does not compile' do + expect { catalogue }.to raise_error(Puppet::Error, %r{\$file_limit must be a positive integer, '-1', 'unlimited', or 'infinity'}) + end + end + end - it { - is_expected.to contain_file('/etc/systemd/system/rabbitmq-server.service.d/limits.conf').with( - 'owner' => '0', - 'group' => '0', - 'mode' => '0644', - 'notify' => 'Exec[rabbitmq-systemd-reload]', - 'content' => <<-EOS -[Service] -LimitNOFILE=1234 -EOS - ) - } - end - end + context 'on systems with systemd', :if => has_systemd do + it { + is_expected.to contain_file('/etc/systemd/system/rabbitmq-server.service.d').with( + 'ensure' => 'directory', + 'owner' => '0', + 'group' => '0', + 'mode' => '0755', + 'selinux_ignore_defaults' => true + ) + } - %w[Debian RedHat SUSE Archlinux].each do |distro| - osfacts = { - osfamily: distro, - staging_http_get: '', - puppetversion: Puppet.version - } - - case distro - when 'Debian' - osfacts[:lsbdistcodename] = 'squeeze' - osfacts[:lsbdistid] = 'Debian' - when 'RedHat' - osfacts[:operatingsystemmajrelease] = '7' - end + it { is_expected.to contain_file('/etc/systemd/system/rabbitmq-server.service.d/limits.conf') } - context "on #{distro}" do - with_distro_facts distro + it { + is_expected.to contain_exec('rabbitmq-systemd-reload').with( + 'command' => '/usr/bin/systemctl daemon-reload', + 'notify' => 'Class[Rabbitmq::Service]', + 'refreshonly' => true + ) + } + end - it { is_expected.to contain_class('rabbitmq::install') } - it { is_expected.to contain_class('rabbitmq::config') } - it { is_expected.to contain_class('rabbitmq::service') } + context 'on systems without systemd', :unless => has_systemd do + it { is_expected.not_to contain_file('/etc/systemd/system/rabbitmq-server.service.d') } + it { is_expected.not_to contain_file('/etc/systemd/system/rabbitmq-server.service.d/limits.conf') } + it { is_expected.not_to contain_exec('rabbitmq-systemd-reload') } + end context 'with admin_enable set to true' do let(:params) { { admin_enable: true, management_ip_address: '1.1.1.1' } } @@ -1325,112 +1200,4 @@ end end end - - ## - ## rabbitmq::install - ## - context 'on RHEL with repos_ensure' do - with_redhat_facts - let(:params) { { repos_ensure: true } } - - it 'installs the rabbitmq package' do - is_expected.to contain_package('rabbitmq-server').with( - 'ensure' => 'installed', - 'name' => 'rabbitmq-server' - ) - end - end - - context 'on RHEL' do - with_redhat_facts - let(:params) { { repos_ensure: false } } - - it 'installs the rabbitmq package [from EPEL] when $repos_ensure is false' do - is_expected.to contain_package('rabbitmq-server').with( - 'ensure' => 'installed', - 'name' => 'rabbitmq-server' - ) - end - end - - context 'on Debian' do - with_debian_facts - it 'installs the rabbitmq package' do - is_expected.to contain_package('rabbitmq-server').with( - 'ensure' => 'installed', - 'name' => 'rabbitmq-server' - ) - end - end - - context 'on Archlinux' do - with_archlinux_facts - it 'installs the rabbitmq package' do - is_expected.to contain_package('rabbitmq-server').with( - 'ensure' => 'installed' - ) - end - end - - context 'on OpenBSD' do - with_openbsd_facts - it 'installs the rabbitmq package' do - is_expected.to contain_package('rabbitmq-server').with( - 'ensure' => 'installed', - 'name' => 'rabbitmq' - ) - end - end - - context "on FreeBSD" do - with_freebsd_facts - it 'installs the rabbitmq package' do - is_expected.to contain_package('rabbitmq-server').with( - 'ensure' => 'installed', - 'name' => 'rabbitmq', - ) - end - end - - describe 'repo management on Debian' do - with_debian_facts - - context 'with no pin' do - let(:params) { { repos_ensure: true, package_apt_pin: '' } } - - describe 'it sets up an apt::source' do - it { - is_expected.to contain_apt__source('rabbitmq').with( - 'location' => 'http://www.rabbitmq.com/debian/', - 'release' => 'testing', - 'repos' => 'main', - 'key' => '{"id"=>"0A9AF2115F4687BD29803A206B73A36E6026DFCA", "source"=>"https://www.rabbitmq.com/rabbitmq-release-signing-key.asc", "content"=>:undef}' - ) - } - end - end - - context 'with pin' do - let(:params) { { repos_ensure: true, package_apt_pin: '700' } } - - describe 'it sets up an apt::source and pin' do - it { - is_expected.to contain_apt__source('rabbitmq').with( - 'location' => 'http://www.rabbitmq.com/debian/', - 'release' => 'testing', - 'repos' => 'main', - 'key' => '{"id"=>"0A9AF2115F4687BD29803A206B73A36E6026DFCA", "source"=>"https://www.rabbitmq.com/rabbitmq-release-signing-key.asc", "content"=>:undef}' - ) - } - - it { - is_expected.to contain_apt__pin('rabbitmq').with( - 'packages' => '*', - 'priority' => '700', - 'origin' => 'www.rabbitmq.com' - ) - } - end - end - end end diff --git a/spec/spec_helper_local.rb b/spec/spec_helper_local.rb index c614dede8..2d2bc27c1 100644 --- a/spec/spec_helper_local.rb +++ b/spec/spec_helper_local.rb @@ -1,82 +1,6 @@ -RSpec.shared_context 'default facts' do - let(:facts) do - { puppetversion: Puppet.version, - staging_http_get: '', - rabbitmq_version: '3.6.1' } - end -end - -RSpec.configure do |rspec| - rspec.include_context 'default facts' -end - -def with_debian_facts - let :facts do - super().merge( - operatingsystemmajrelease: '6', - lsbdistcodename: 'squeeze', - lsbdistid: 'Debian', - osfamily: 'Debian', - os: - { - name: 'Debian', - release: { full: '6.0' } - } - ) - end -end - -def with_openbsd_facts - # operatingsystemmajrelease is too broad - # operatingsystemrelease may contain X.X-current - # or other prefixes - let :facts do - super().merge( - kernelversion: '5.9', - osfamily: 'OpenBSD' - ) - end -end - -def with_freebsd_facts - # operatingsystemmajrelease is too broad - # operatingsystemrelease may contain X.X-current - # or other prefixes - let :facts do - super().merge( - kernelversion: '12', - osfamily: 'FreeBSD', - staging_http_get: '' - ) - end -end - -def with_redhat_facts - let :facts do - super().merge( - operatingsystemmajrelease: '7', - osfamily: 'Redhat', - os: - { - name: 'CentOS', - release: { major: '7' } - } - ) - end -end - -def with_suse_facts - let :facts do - super().merge(osfamily: 'SUSE') - end -end - -def with_archlinux_facts - let :facts do - super().merge(osfamily: 'Archlinux') - end -end - -def with_distro_facts(distro) - send("with_#{distro.downcase}_facts") -end +require 'rspec-puppet-facts' +include RspecPuppetFacts + # Original fact sources: +add_custom_fact :puppetversion, Puppet.version # Facter, but excluded from rspec-puppet-facts +add_custom_fact :staging_http_get, '' # puppet-staging +add_custom_fact :rabbitmq_version, '3.6.1' # puppet-rabbitmq