Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove legacy facts #49

Merged
merged 5 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .puppet-lint.rc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
--fail-on-warnings
--no-parameter_documentation-check
--no-parameter_types-check
--no-legacy_facts-check
3 changes: 1 addition & 2 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -57,5 +57,4 @@
fail("monit supports osfamilies Debian and RedHat. Detected osfamily is <${facts['os']['family']}>.")
}
}
# </OS family handling>
}
2 changes: 1 addition & 1 deletion manifests/service.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
10 changes: 5 additions & 5 deletions spec/classes/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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}$})
Expand Down
242 changes: 95 additions & 147 deletions spec/defines/check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,165 +5,113 @@
'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]')
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("#{confdir}/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("#{confdir}/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("#{confdir}/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("#{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

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