From 7871a22d6c951ad638887cba0dc4722df7fb4ccd Mon Sep 17 00:00:00 2001 From: markuszilch Date: Tue, 13 Feb 2024 15:52:47 +0100 Subject: [PATCH 1/5] remove legacy fact syntax --- .puppet-lint.rc | 1 - spec/classes/init_spec.rb | 10 +++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/.puppet-lint.rc b/.puppet-lint.rc index d2e5007..dd8272c 100644 --- a/.puppet-lint.rc +++ b/.puppet-lint.rc @@ -1,4 +1,3 @@ --fail-on-warnings --no-parameter_documentation-check --no-parameter_types-check ---no-legacy_facts-check diff --git a/spec/classes/init_spec.rb b/spec/classes/init_spec.rb index adf8dad..e6b15f8 100644 --- a/spec/classes/init_spec.rb +++ b/spec/classes/init_spec.rb @@ -6,7 +6,7 @@ context "on #{os}" do let(:facts) { facts } - case facts[:osfamily] + case facts[:os]['family'] when 'Debian' config_file = '/etc/monit/monitrc' config_dir = '/etc/monit/conf.d' @@ -17,7 +17,7 @@ config_dir = '/etc/monit.d' service_hasstatus = true monit_version = '5' - config_file = case facts[:operatingsystem] + config_file = case facts[:os]['name'] when 'Amazon' '/etc/monit.conf' else @@ -64,14 +64,14 @@ end monit_config_fixture = if monit_version == '4' - File.read(fixtures("monitrc.4.#{facts[:osfamily]}")) + File.read(fixtures("monitrc.4.#{facts[:os]['family']}")) else - File.read(fixtures("monitrc.#{facts[:osfamily]}")) + File.read(fixtures("monitrc.#{facts[:os]['family']}")) end it { is_expected.to contain_file('monit_config').with_content(monit_config_fixture) } - if facts[:osfamily] == 'Debian' + if facts[:os]['family'] == 'Debian' it do is_expected.to contain_file('/etc/default/monit').with('notify' => 'Service[monit]'). with_content(%r{^#{default_file_content}$}) From 82577257a577c04c5d2508e29bb686f867385bba Mon Sep 17 00:00:00 2001 From: markuszilch Date: Tue, 13 Feb 2024 16:17:06 +0100 Subject: [PATCH 2/5] remove legacy facts in pp files as well --- manifests/params.pp | 2 +- manifests/service.pp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/manifests/params.pp b/manifests/params.pp index 455c87f..73cd68a 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -30,7 +30,7 @@ $mmonit_password = 'monit' $mmonit_without_credential = false - case $facts['osfamily'] { + case $facts['os']['family'] { 'Debian': { $config_file = '/etc/monit/monitrc' $config_dir = '/etc/monit/conf.d' diff --git a/manifests/service.pp b/manifests/service.pp index 4275b88..c76250a 100644 --- a/manifests/service.pp +++ b/manifests/service.pp @@ -9,7 +9,7 @@ } if $monit::service_manage { - if $facts['osfamily'] == 'Debian' { + if $facts['os']['family'] == 'Debian' { file { '/etc/default/monit': content => $monit::default_file_content, notify => Service[$monit::service_name], From ba66e53b2d3759454e98f58b8c209572f2b2b4a9 Mon Sep 17 00:00:00 2001 From: markuszilch Date: Tue, 13 Feb 2024 23:20:56 +0100 Subject: [PATCH 3/5] remove unnecessary comment --- manifests/params.pp | 1 - 1 file changed, 1 deletion(-) diff --git a/manifests/params.pp b/manifests/params.pp index 73cd68a..2979961 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -57,5 +57,4 @@ fail("monit supports osfamilies Debian and RedHat. Detected osfamily is <${facts['os']['family']}>.") } } - # } From a55cb2621d338f78b29bdcaa67ee531b031865f6 Mon Sep 17 00:00:00 2001 From: markuszilch Date: Thu, 15 Feb 2024 11:18:08 +0100 Subject: [PATCH 4/5] set os facts for tests --- spec/defines/check_spec.rb | 236 ++++++++++++++----------------------- 1 file changed, 89 insertions(+), 147 deletions(-) diff --git a/spec/defines/check_spec.rb b/spec/defines/check_spec.rb index 4ee93db..0630d0e 100644 --- a/spec/defines/check_spec.rb +++ b/spec/defines/check_spec.rb @@ -5,165 +5,107 @@ 'include ::monit' end let(:title) { 'test' } - let(:facts) do - { - osfamily: 'Debian', - lsbdistcodename: 'squeeze', - monit_version: '5', - } - end - - context 'with default values for parameters' do - it { is_expected.to compile.with_all_deps } - it { is_expected.to contain_class('monit') } - - it do - is_expected.to contain_file('/etc/monit/conf.d/test').with('ensure' => 'present', - 'owner' => 'root', - 'group' => 'root', - 'mode' => '0644', - 'source' => nil, - 'content' => nil, - 'notify' => 'Service[monit]', - 'require' => 'Package[monit]') - end - end - %w[absent present].each do |value| - context "with ensure set to valid <#{value}>" do - let(:params) do - { - ensure: value, - } + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) do + facts end - it do - is_expected.to contain_file('/etc/monit/conf.d/test').with('ensure' => value, - 'owner' => 'root', - 'group' => 'root', - 'mode' => '0644', - 'source' => nil, - 'content' => nil, - 'notify' => 'Service[monit]', - 'require' => 'Package[monit]') + context 'with default values for parameters' do + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_class('monit') } + + it do + is_expected.to contain_file('/etc/monit/conf.d/test').with('ensure' => 'present', + 'owner' => 'root', + 'group' => 'root', + 'mode' => '0644', + 'source' => nil, + 'content' => nil, + 'notify' => 'Service[monit]', + 'require' => 'Package[monit]') + end end - end - end - - context 'with content set to a valid value' do - content = <<~END - check process ntpd with pidfile /var/run/ntpd.pid - start program = "/etc/init.d/ntpd start" - stop program = "/etc/init.d/ntpd stop" - if failed host 127.0.0.1 port 123 type udp then alert - if 5 restarts within 5 cycles then timeout - END - let(:params) do - { - content: content, - } - end - it do - is_expected.to contain_file('/etc/monit/conf.d/test').with('ensure' => 'present', - 'owner' => 'root', - 'group' => 'root', - 'mode' => '0644', - 'source' => nil, - 'content' => content, - 'notify' => 'Service[monit]', - 'require' => 'Package[monit]') - end - end - - context 'with source set to a valid value' do - let(:params) do - { - source: 'puppet:///modules/monit/ntp', - } - end - - it do - is_expected.to contain_file('/etc/monit/conf.d/test').with('ensure' => 'present', - 'owner' => 'root', - 'group' => 'root', - 'mode' => '0644', - 'source' => 'puppet:///modules/monit/ntp', - 'content' => nil, - 'notify' => 'Service[monit]', - 'require' => 'Package[monit]') - end - end - - context 'with content and source set at the same time' do - let(:params) do - { - content: 'content', - source: 'puppet:///modules/subject/test', - } - end + %w[absent present].each do |value| + context "with ensure set to valid <#{value}>" do + let(:params) do + { + ensure: value, + } + end - it 'fails' do - is_expected.to compile.and_raise_error(%r{Parameters source and content are mutually exclusive}) - end - end + it do + is_expected.to contain_file('/etc/monit/conf.d/test').with('ensure' => value, + 'owner' => 'root', + 'group' => 'root', + 'mode' => '0644', + 'source' => nil, + 'content' => nil, + 'notify' => 'Service[monit]', + 'require' => 'Package[monit]') + end + end + end - describe 'variable type and content validations' do - # set needed custom facts and variables - let(:facts) do - { - osfamily: 'Debian', - lsbdistcodename: 'squeeze', - monit_version: '5', - } - end - let(:validation_params) do - { - # :param => 'value', - } - end + context 'with content set to a valid value' do + content = <<~END + check process ntpd with pidfile /var/run/ntpd.pid + start program = "/etc/init.d/ntpd start" + stop program = "/etc/init.d/ntpd stop" + if failed host 127.0.0.1 port 123 type udp then alert + if 5 restarts within 5 cycles then timeout + END + let(:params) do + { + content: content, + } + end - validations = { - 'regex_file_ensure' => { - name: ['ensure'], - valid: %w[present absent], - invalid: ['file', 'directory', 'link', ['array'], { 'ha' => 'sh' }, 3, 2.42, true, false, nil], - message: 'match for Enum\[\'absent\', \'present\'\]', - }, - 'string' => { - name: ['content'], - valid: ['string'], - invalid: [['array'], { 'ha' => 'sh' }, 3, 2.42, true, false], - message: 'value of type Undef or String', - }, - 'string_file_source' => { - name: ['source'], - valid: ['puppet:///modules/subject/test'], - invalid: [['array'], { 'ha' => 'sh' }, 3, 2.42, true, false], - message: 'value of type Undef or String', - }, - } + it do + is_expected.to contain_file('/etc/monit/conf.d/test').with('ensure' => 'present', + 'owner' => 'root', + 'group' => 'root', + 'mode' => '0644', + 'source' => nil, + 'content' => content, + 'notify' => 'Service[monit]', + 'require' => 'Package[monit]') + end + end - validations.sort.each do |type, var| - var[:name].each do |var_name| - var[:valid].each do |valid| - context "with #{var_name} (#{type}) set to valid #{valid} (as #{valid.class})" do - let(:params) { validation_params.merge("#{var_name}": valid) } + context 'with source set to a valid value' do + let(:params) do + { + source: 'puppet:///modules/monit/ntp', + } + end - it { is_expected.to compile } - end + it do + is_expected.to contain_file('/etc/monit/conf.d/test').with('ensure' => 'present', + 'owner' => 'root', + 'group' => 'root', + 'mode' => '0644', + 'source' => 'puppet:///modules/monit/ntp', + 'content' => nil, + 'notify' => 'Service[monit]', + 'require' => 'Package[monit]') end + end - var[:invalid].each do |invalid| - context "with #{var_name} (#{type}) set to invalid #{invalid} (as #{invalid.class})" do - let(:params) { validation_params.merge("#{var_name}": invalid) } + context 'with content and source set at the same time' do + let(:params) do + { + content: 'content', + source: 'puppet:///modules/subject/test', + } + end - it 'fails' do - is_expected.to compile.and_raise_error(%r{expects a #{var[:message]}}) - end - end + it 'fails' do + is_expected.to compile.and_raise_error(%r{Parameters source and content are mutually exclusive}) end - end # var[:name].each - end # validations.sort.each - end # describe 'variable type and content validations' + end + end + end end From f484339e67ae653f7af985ac7d9190d7a06bb09b Mon Sep 17 00:00:00 2001 From: markuszilch Date: Thu, 15 Feb 2024 21:20:08 +0100 Subject: [PATCH 5/5] dynamic confdir resolution for tests --- spec/defines/check_spec.rb | 70 +++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 32 deletions(-) diff --git a/spec/defines/check_spec.rb b/spec/defines/check_spec.rb index 0630d0e..35594e7 100644 --- a/spec/defines/check_spec.rb +++ b/spec/defines/check_spec.rb @@ -12,19 +12,25 @@ facts end + confdir = if facts[:os]['family'] == 'RedHat' + '/etc/monit.d' + else + '/etc/monit/conf.d' + end + context 'with default values for parameters' do it { is_expected.to compile.with_all_deps } it { is_expected.to contain_class('monit') } it do - is_expected.to contain_file('/etc/monit/conf.d/test').with('ensure' => 'present', - 'owner' => 'root', - 'group' => 'root', - 'mode' => '0644', - 'source' => nil, - 'content' => nil, - 'notify' => 'Service[monit]', - 'require' => 'Package[monit]') + is_expected.to contain_file("#{confdir}/test").with('ensure' => 'present', + 'owner' => 'root', + 'group' => 'root', + 'mode' => '0644', + 'source' => nil, + 'content' => nil, + 'notify' => 'Service[monit]', + 'require' => 'Package[monit]') end end @@ -37,14 +43,14 @@ end it do - is_expected.to contain_file('/etc/monit/conf.d/test').with('ensure' => value, - 'owner' => 'root', - 'group' => 'root', - 'mode' => '0644', - 'source' => nil, - 'content' => nil, - 'notify' => 'Service[monit]', - 'require' => 'Package[monit]') + is_expected.to contain_file("#{confdir}/test").with('ensure' => value, + 'owner' => 'root', + 'group' => 'root', + 'mode' => '0644', + 'source' => nil, + 'content' => nil, + 'notify' => 'Service[monit]', + 'require' => 'Package[monit]') end end end @@ -64,14 +70,14 @@ end it do - is_expected.to contain_file('/etc/monit/conf.d/test').with('ensure' => 'present', - 'owner' => 'root', - 'group' => 'root', - 'mode' => '0644', - 'source' => nil, - 'content' => content, - 'notify' => 'Service[monit]', - 'require' => 'Package[monit]') + is_expected.to contain_file("#{confdir}/test").with('ensure' => 'present', + 'owner' => 'root', + 'group' => 'root', + 'mode' => '0644', + 'source' => nil, + 'content' => content, + 'notify' => 'Service[monit]', + 'require' => 'Package[monit]') end end @@ -83,14 +89,14 @@ end it do - is_expected.to contain_file('/etc/monit/conf.d/test').with('ensure' => 'present', - 'owner' => 'root', - 'group' => 'root', - 'mode' => '0644', - 'source' => 'puppet:///modules/monit/ntp', - 'content' => nil, - 'notify' => 'Service[monit]', - 'require' => 'Package[monit]') + is_expected.to contain_file("#{confdir}/test").with('ensure' => 'present', + 'owner' => 'root', + 'group' => 'root', + 'mode' => '0644', + 'source' => 'puppet:///modules/monit/ntp', + 'content' => nil, + 'notify' => 'Service[monit]', + 'require' => 'Package[monit]') end end