Skip to content

Commit

Permalink
merge class smartd $ensure and $autoupdate params
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Hoblitt committed Sep 14, 2013
1 parent 4c6b9bc commit 6475a9e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 74 deletions.
69 changes: 26 additions & 43 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@
#
# defaults to: `present`
#
# [*autoupdate*]
# Boolean.
#
# If true, smartmontools package will always been updated to the latest
# available version.
#
# defaults to: false
#
# [*package_name*]
# String.
#
Expand Down Expand Up @@ -108,21 +100,21 @@
# Copyright 2012 Massachusetts Institute of Technology
# Copyright (C) 2013 Joshua Hoblitt
#
class smartd ($ensure = 'present',
$autoupdate = $smartd::params::autoupdate,
$package_name = $smartd::params::package_name,
$service_name = $smartd::params::service_name,
$config_file = $smartd::params::config_file,
$devicescan = $smartd::params::devicescan,
$devicescan_options = $smartd::params::devicescan_options,
$devices = $smartd::params::devices,
$device_opts = $smartd::params::device_opts,
$mail_to = $smartd::params::mail_to,
$warning_schedule = $smartd::params::warning_schedule,
$enable_monit = $smartd::params::enable_monit,
) inherits smartd::params {
class smartd (
$ensure = 'present',
$package_name = $smartd::params::package_name,
$config_file = $smartd::params::config_file,
$devicescan = $smartd::params::devicescan,
$devicescan_options = $smartd::params::devicescan_options,
$devices = $smartd::params::devices,
$device_opts = $smartd::params::device_opts,
$mail_to = $smartd::params::mail_to,
$warning_schedule = $smartd::params::warning_schedule,
$enable_monit = $smartd::params::enable_monit,
) inherits smartd::params {
validate_re($ensure, '^present$|^latest$|^absent$|^purged$')

# Validate our booleans
validate_bool($autoupdate)
validate_bool($devicescan)
validate_bool($enable_monit)

Expand All @@ -135,27 +127,17 @@
'$warning_schedule must be either daily, once, or diminishing.')

case $ensure {
'present': {
if $autoupdate {
$pkg_ensure = 'latest'
} else {
$pkg_ensure = 'present'
}
$svc_ensure = 'running'
$svc_enable = true
$file_ensure = 'present'
'present','latest': {
$pkg_ensure = $ensure
$svc_ensure = 'running'
$svc_enable = true
$file_ensure = 'present'
}
'absent': {
$pkg_ensure = 'absent'
$svc_ensure = 'stopped'
$svc_enable = false
$file_ensure = 'absent'
}
'purged': {
$pkg_ensure = 'purged'
$svc_ensure = 'stopped'
$svc_enable = false
$file_ensure = 'absent'
'absent','purged': {
$pkg_ensure = $ensure
$svc_ensure = 'stopped'
$svc_enable = false
$file_ensure = 'absent'
}
default: {
fail("unsupported value of \$ensure: ${ensure}")
Expand All @@ -171,9 +153,10 @@
enable => $svc_enable,
hasrestart => true,
hasstatus => true,
require => Package[$package_name],
}

Package[$package_name] -> Service[$service_name]

file {$config_file:
ensure => $file_ensure,
owner => root,
Expand Down
1 change: 0 additions & 1 deletion manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# Copyright (C) 2013 Joshua Hoblitt
#
class smartd::params {
$autoupdate = false
$package_name = 'smartmontools'
$service_name = 'smartd'
$devicescan = true
Expand Down
73 changes: 43 additions & 30 deletions spec/classes/smartd_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
require 'spec_helper'

describe 'smartd', :type => 'class' do
describe 'smartd', :type => :class do

context 'on a non-supported osfamily' do
let(:params) {{}}
let :facts do {
:osfamily => 'foo',
:operatingsystem => 'bar'
}
let :facts do
{
:osfamily => 'foo',
:operatingsystem => 'bar'
}
end

it 'should fail' do
expect {
should raise_error(Puppet::Error, /smartd: unsupported OS family bar/)
Expand All @@ -18,22 +19,20 @@

context 'on a supported osfamily, default parameters' do
describe 'for osfamily RedHat' do
let(:params) {{}}
let(:facts) {{ :osfamily => 'RedHat' }}

it { should contain_package('smartmontools').with_ensure('present') }
it { should contain_service('smartd').with(
:ensure => 'running',
:enable => true,
:hasstatus => true,
:hasrestart => true,
:require => 'Package[smartmontools]'
)}
it { should contain_file('/etc/smartd.conf').with(
:ensure => 'present',
:owner => 'root',
:group => '0',
:mode => '0644',
:require => 'Package[smartmontools]',
:notify => 'Service[smartd]'
)}
it 'should contain File[/etc/smartd.conf] with correct contents' do
Expand All @@ -46,15 +45,14 @@
end

describe 'for osfamily Debian' do
let(:params) {{}}
let(:facts) {{ :osfamily => 'Debian' }}

it { should contain_package('smartmontools').with_ensure('present') }
it { should contain_service('smartd').with(
:ensure => 'running',
:enable => true,
:hasstatus => true,
:hasrestart => true,
:require => 'Package[smartmontools]'
)}
it { should contain_file('/etc/smartd.conf').with(
:ensure => 'present',
Expand All @@ -80,15 +78,14 @@
end

describe 'for osfamily FreeBSD' do
let(:params) {{}}
let(:facts) {{ :osfamily => 'FreeBSD' }}

it { should contain_package('smartmontools').with_ensure('present') }
it { should contain_service('smartd').with(
:ensure => 'running',
:enable => true,
:hasstatus => true,
:hasrestart => true,
:require => 'Package[smartmontools]'
)}
it { should contain_file('/usr/local/etc/smartd.conf').with(
:ensure => 'present',
Expand All @@ -112,46 +109,58 @@
context 'on a supported osfamily, custom parameters' do
let(:facts) {{ :osfamily => 'RedHat' }}

describe 'ensure => present' do
let(:params) {{ :ensure => 'present' }}

it { should contain_package('smartmontools').with_ensure('present') }
it { should contain_service('smartd').with_ensure('running').with_enable(true) }
it { should contain_file('/etc/smartd.conf').with_ensure('present') }
end

describe 'ensure => latest' do
let(:params) {{ :ensure => 'latest' }}

it { should contain_package('smartmontools').with_ensure('latest') }
it { should contain_service('smartd').with_ensure('running').with_enable(true) }
it { should contain_file('/etc/smartd.conf').with_ensure('present') }
end

describe 'ensure => absent' do
let(:params) {{ :ensure => 'absent' }}

it { should contain_package('smartmontools').with_ensure('absent') }
it { should contain_service('smartd').with_ensure('stopped').with_enable(false) }
it { should contain_file('/etc/smartd.conf').with_ensure('absent') }
end

describe 'ensure => badvalue' do
let(:params) {{ :ensure => 'badvalue' }}
it 'should fail' do
expect {
should raise_error(Puppet::Error, /unsupported value of $ensure: badvalue/)
}
end
end
describe 'ensure => purge' do
let(:params) {{ :ensure => 'purged' }}

describe 'autoupdate => true' do
let(:params) {{ :autoupdate => true }}
it { should contain_package('smartmontools').with_ensure('latest') }
it { should contain_service('smartd').with_ensure('running').with_enable(true) }
it { should contain_file('/etc/smartd.conf').with_ensure('present') }
it { should contain_package('smartmontools').with_ensure('purged') }
it { should contain_service('smartd').with_ensure('stopped').with_enable(false) }
it { should contain_file('/etc/smartd.conf').with_ensure('absent') }
end

describe 'autoupdate => badvalue' do
let(:params) {{ :autoupdate => 'badvalue' }}
describe 'ensure => badvalue' do
let(:params) {{ :ensure => 'badvalue' }}

it 'should fail' do
expect {
should raise_error(Puppet::Error, /"badvalue" is not a boolean./)
should raise_error(Puppet::Error, /unsupported value of $ensure: badvalue/)
}
end
end

describe 'devicescan => false' do
let(:params) {{ :devicescan => false }}

it { should contain_file('/etc/smartd.conf').with_ensure('present') }
it { should_not contain_file('/etc/smartd.conf').with_content(/^DEVICESCAN$/) }
end

describe 'devicescan_options => somevalue' do
let(:params) {{ :devicescan_options => 'somevalue' }}

it { should contain_file('/etc/smartd.conf').with_ensure('present') }
it 'should contain File[/etc/smartd.conf] with contents "DEVICESCAN somevalue"' do
verify_contents(subject, '/etc/smartd.conf', [
Expand All @@ -163,6 +172,7 @@

describe 'devices => [ /dev/sg1, /dev/sg2 ]' do
let(:params) {{ :devices => [ '/dev/sg1', '/dev/sg2', ] }}

it { should contain_file('/etc/smartd.conf').with_ensure('present') }
it 'should contain File[/etc/smartd.conf] with contents "/dev/sg1\n/dev/sg2"' do
verify_contents(subject, '/etc/smartd.conf', [
Expand All @@ -179,6 +189,7 @@
:device_opts => { '/dev/sg1' => '-o on -S on -a', '/dev/sg2' => '-o on -S on -a' }
}
end

it { should contain_file('/etc/smartd.conf').with_ensure('present') }
it 'should contain File[/etc/smartd.conf] with contents "/dev/sg1 -o on -S on -a\n/dev/sg2 -o on -S on -a"' do
verify_contents(subject, '/etc/smartd.conf', [
Expand All @@ -191,6 +202,7 @@

describe 'mail_to => someguy@localdomain' do
let(:params) {{ :mail_to => 'someguy@localdomain' }}

it { should contain_file('/etc/smartd.conf').with_ensure('present') }
it 'should contain File[/etc/smartd.conf] with contents "DEFAULT -m someguy@localdomain -M daily"' do
verify_contents(subject, '/etc/smartd.conf', [
Expand All @@ -201,6 +213,7 @@

describe 'warning_schedule => diminishing' do
let(:params) {{ :warning_schedule => 'diminishing' }}

it { should contain_file('/etc/smartd.conf').with_ensure('present') }
it 'should contain File[/etc/smartd.conf] with contents "DEFAULT -m root -M diminishing"' do
verify_contents(subject, '/etc/smartd.conf', [
Expand All @@ -211,6 +224,7 @@

describe 'warning_schedule => badvalue' do
let(:params) {{ :warning_schedule => 'badvalue' }}

it 'should fail' do
expect {
should raise_error(Puppet::Error, /$warning_schedule must be either daily, once, or diminishing./)
Expand All @@ -221,7 +235,6 @@
end


let(:title) { 'redhat' }
let(:facts) { {:osfamily=> 'RedHat', :lsbmajordistrelease => 6} }

context 'without params' do
Expand Down

0 comments on commit 6475a9e

Please sign in to comment.