forked from redhat-openstack/openstack-puppet-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #836 from mhaskel/MODULES-1180
Add defined type for handling custom configs
- Loading branch information
Showing
6 changed files
with
284 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# See README.md for usage information | ||
define apache::custom_config ( | ||
$ensure = 'present', | ||
$confdir = $::apache::confd_dir, | ||
$content = undef, | ||
$priority = '25', | ||
$source = undef, | ||
$verify_command = '/usr/sbin/apachectl -t', | ||
$verify_config = true, | ||
) { | ||
|
||
if $content and $source { | ||
fail('Only one of $content and $source can be specified.') | ||
} | ||
|
||
if $ensure == 'present' and ! $content and ! $source { | ||
fail('One of $content and $source must be specified.') | ||
} | ||
|
||
validate_re($ensure, '^(present|absent)$', | ||
"${ensure} is not supported for ensure. | ||
Allowed values are 'present' and 'absent'.") | ||
|
||
validate_bool($verify_config) | ||
|
||
## Apache include does not always work with spaces in the filename | ||
$filename = regsubst($name, ' ', '_', 'G') | ||
|
||
if ! $verify_config or $ensure == 'absent' { | ||
$notifies = Service['httpd'] | ||
} else { | ||
$notifies = undef | ||
} | ||
|
||
file { "apache_${name}": | ||
ensure => $ensure, | ||
path => "${confdir}/${priority}-${filename}.conf", | ||
content => $content, | ||
source => $source, | ||
require => Package['httpd'], | ||
notify => $notifies, | ||
} | ||
|
||
if $ensure == 'present' and $verify_config { | ||
exec { "service notify for ${name}": | ||
command => $verify_command, | ||
subscribe => File["apache_${name}"], | ||
refreshonly => true, | ||
notify => Service['httpd'], | ||
before => Exec["remove ${name} if invalid"], | ||
} | ||
|
||
exec { "remove ${name} if invalid": | ||
command => "/bin/rm ${confdir}/${priority}-${filename}.conf", | ||
unless => $verify_command, | ||
subscribe => File["apache_${name}"], | ||
refreshonly => true, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require 'spec_helper_acceptance' | ||
require_relative './version.rb' | ||
|
||
describe 'apache::custom_config define', :unless => UNSUPPORTED_PLATFORMS.include?(fact('osfamily')) do | ||
context 'invalid config' do | ||
it 'should not add the config' do | ||
pp = <<-EOS | ||
class { 'apache': } | ||
apache::custom_config { 'acceptance_test': | ||
content => 'INVALID', | ||
} | ||
EOS | ||
|
||
apply_manifest(pp, :expect_failures => true) | ||
end | ||
|
||
describe file("#{$confd_dir}/25-acceptance_test.conf") do | ||
it { is_expected.not_to be_file } | ||
end | ||
end | ||
|
||
context 'valid config' do | ||
it 'should add the config' do | ||
pp = <<-EOS | ||
class { 'apache': } | ||
apache::custom_config { 'acceptance_test': | ||
content => '# just a comment', | ||
} | ||
EOS | ||
|
||
apply_manifest(pp, :catch_failures => true) | ||
end | ||
|
||
describe file("#{$confd_dir}/25-acceptance_test.conf") do | ||
it { is_expected.to contain '# just a comment' } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
require 'spec_helper' | ||
|
||
describe 'apache::custom_config', :type => :define do | ||
let :pre_condition do | ||
'class { "apache": }' | ||
end | ||
let :title do | ||
'rspec' | ||
end | ||
let :facts do | ||
{ | ||
:osfamily => 'Debian', | ||
:operatingsystemrelease => '6', | ||
:concat_basedir => '/', | ||
:lsbdistcodename => 'squeeze', | ||
:operatingsystem => 'Debian', | ||
:id => 'root', | ||
:kernel => 'Linux', | ||
:path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', | ||
} | ||
end | ||
context 'defaults with content' do | ||
let :params do | ||
{ | ||
'content' => '# Test', | ||
} | ||
end | ||
it { is_expected.to contain_exec("service notify for rspec").with({ | ||
'refreshonly' => 'true', | ||
'subscribe' => 'File[apache_rspec]', | ||
'command' => '/usr/sbin/apachectl -t', | ||
'notify' => 'Service[httpd]', | ||
'before' => 'Exec[remove rspec if invalid]', | ||
}) | ||
} | ||
it { is_expected.to contain_exec("remove rspec if invalid").with({ | ||
'unless' => '/usr/sbin/apachectl -t', | ||
'subscribe' => 'File[apache_rspec]', | ||
'refreshonly' => 'true', | ||
}) | ||
} | ||
it { is_expected.to contain_file("apache_rspec").with({ | ||
'ensure' => 'present', | ||
'content' => '# Test', | ||
'require' => 'Package[httpd]', | ||
}) | ||
} | ||
end | ||
context 'set everything with source' do | ||
let :params do | ||
{ | ||
'confdir' => '/dne', | ||
'priority' => '30', | ||
'source' => 'puppet:///modules/apache/test', | ||
'verify_command' => '/bin/true', | ||
} | ||
end | ||
it { is_expected.to contain_exec("service notify for rspec").with({ | ||
'command' => '/bin/true', | ||
}) | ||
} | ||
it { is_expected.to contain_exec("remove rspec if invalid").with({ | ||
'command' => '/bin/rm /dne/30-rspec.conf', | ||
'unless' => '/bin/true', | ||
}) | ||
} | ||
it { is_expected.to contain_file("apache_rspec").with({ | ||
'path' => '/dne/30-rspec.conf', | ||
'ensure' => 'present', | ||
'source' => 'puppet:///modules/apache/test', | ||
'require' => 'Package[httpd]', | ||
}) | ||
} | ||
end | ||
context 'verify_config => false' do | ||
let :params do | ||
{ | ||
'content' => '# test', | ||
'verify_config' => false, | ||
} | ||
end | ||
it { is_expected.to_not contain_exec('service notify for rspec') } | ||
it { is_expected.to_not contain_exec('remove rspec if invalid') } | ||
it { is_expected.to contain_file('apache_rspec').with({ | ||
'notify' => 'Service[httpd]' | ||
}) | ||
} | ||
end | ||
context 'ensure => absent' do | ||
let :params do | ||
{ | ||
'ensure' => 'absent' | ||
} | ||
end | ||
it { is_expected.to_not contain_exec('service notify for rspec') } | ||
it { is_expected.to_not contain_exec('remove rspec if invalid') } | ||
it { is_expected.to contain_file('apache_rspec').with({ | ||
'ensure' => 'absent', | ||
}) | ||
} | ||
end | ||
describe 'validation' do | ||
context 'both content and source' do | ||
let :params do | ||
{ | ||
'content' => 'foo', | ||
'source' => 'bar', | ||
} | ||
end | ||
it do | ||
expect { | ||
should compile | ||
}.to raise_error(Puppet::Error, /Only one of \$content and \$source can be specified\./) | ||
end | ||
end | ||
context 'neither content nor source' do | ||
it do | ||
expect { | ||
should compile | ||
}.to raise_error(Puppet::Error, /One of \$content and \$source must be specified\./) | ||
end | ||
end | ||
context 'bad ensure' do | ||
let :params do | ||
{ | ||
'content' => 'foo', | ||
'ensure' => 'foo', | ||
} | ||
end | ||
it do | ||
expect { | ||
should compile | ||
}.to raise_error(Puppet::Error, /is not supported for ensure/) | ||
end | ||
end | ||
end | ||
end |