commit
94cbeca231
12 changed files with 775 additions and 250 deletions
|
@ -1,3 +1,7 @@
|
|||
fixtures:
|
||||
repositories:
|
||||
'stdlib':
|
||||
repo: 'git://github.com/puppetlabs/puppetlabs-stdlib.git'
|
||||
ref: '3.0.0'
|
||||
symlinks:
|
||||
'concat': '#{source_dir}'
|
||||
|
|
|
@ -6,3 +6,4 @@ license 'Apache 2.0'
|
|||
summary 'Concat module'
|
||||
description 'Concat module'
|
||||
project_page 'http://github.com/puppetlabs/puppetlabs-concat'
|
||||
dependency 'puppetlabs/stdlib', '>= 3.0.0'
|
||||
|
|
|
@ -154,6 +154,9 @@ Contributors:
|
|||
**Joshua Hoblitt**
|
||||
|
||||
* Remove requirement to manually include `concat::setup` in the manifest
|
||||
* Style improvements
|
||||
* Parameter validation / refactor parameter handling
|
||||
* Test coverage
|
||||
|
||||
Contact:
|
||||
--------
|
||||
|
|
|
@ -36,6 +36,16 @@ define concat::fragment(
|
|||
$group = undef,
|
||||
$backup = 'puppet'
|
||||
) {
|
||||
validate_absolute_path($target)
|
||||
validate_re($ensure, '^$|^present$|^absent$|^file$|^directory$')
|
||||
validate_string($content)
|
||||
validate_string($source)
|
||||
validate_string($order)
|
||||
validate_string($mode)
|
||||
validate_string($owner)
|
||||
validate_string($group)
|
||||
validate_string($backup)
|
||||
|
||||
include concat::setup
|
||||
|
||||
$safe_name = regsubst($name, '[/\n]', '_', 'GM')
|
||||
|
|
|
@ -10,22 +10,27 @@
|
|||
# The path to the final file. Use this in case you want to differentiate
|
||||
# between the name of a resource and the file path. Note: Use the name you
|
||||
# provided in the target of your fragments.
|
||||
# [*mode*]
|
||||
# The mode of the final file
|
||||
# [*owner*]
|
||||
# Who will own the file
|
||||
# [*group*]
|
||||
# Who will own the file
|
||||
# [*mode*]
|
||||
# The mode of the final file
|
||||
# [*force*]
|
||||
# Enables creating empty files if no fragments are present
|
||||
# [*warn*]
|
||||
# Adds a normal shell style comment top of the file indicating that it is
|
||||
# built by puppet
|
||||
# [*warn_message*]
|
||||
# A custom message string that overides the default.
|
||||
# [*force*]
|
||||
# [*backup*]
|
||||
# Controls the filebucketing behavior of the final file and see File type
|
||||
# reference for its use. Defaults to 'puppet'
|
||||
# [*replace*]
|
||||
# Whether to replace a file that already exists on the local system
|
||||
# [*order*]
|
||||
# [*ensure_newline*]
|
||||
#
|
||||
# === Actions:
|
||||
# * Creates fragment directories if it didn't exist already
|
||||
|
@ -54,18 +59,30 @@ define concat(
|
|||
$group = undef,
|
||||
$mode = '0644',
|
||||
$warn = false,
|
||||
$warn_message = undef,
|
||||
$force = false,
|
||||
$backup = 'puppet',
|
||||
$replace = true,
|
||||
$gnu = undef,
|
||||
$order = 'alpha',
|
||||
$ensure_newline = false,
|
||||
$ensure_newline = false
|
||||
) {
|
||||
validate_re($ensure, '^present$|^absent$')
|
||||
validate_absolute_path($path)
|
||||
validate_string($owner)
|
||||
validate_string($group)
|
||||
validate_string($mode)
|
||||
validate_bool($warn)
|
||||
validate_string($warn_message)
|
||||
validate_bool($force)
|
||||
validate_string($backup)
|
||||
validate_bool($replace)
|
||||
validate_re($order, '^alpha$|^numeric$')
|
||||
validate_bool($ensure_newline)
|
||||
|
||||
include concat::setup
|
||||
|
||||
$safe_name = regsubst($name, '/', '_', 'G')
|
||||
$concatdir = $concat::setup::concatdir
|
||||
$version = $concat::setup::majorversion
|
||||
$fragdir = "${concatdir}/${safe_name}"
|
||||
$concat_name = 'fragments.concat.out'
|
||||
$default_warn_message = '# This file is managed by Puppet. DO NOT EDIT.'
|
||||
|
@ -75,73 +92,44 @@ define concat(
|
|||
default => $group,
|
||||
}
|
||||
|
||||
case $warn {
|
||||
'true', true, 'yes', 'on': {
|
||||
$warnmsg = $default_warn_message
|
||||
}
|
||||
'false', false, 'no', 'off': {
|
||||
$warnmsg = ''
|
||||
}
|
||||
default: {
|
||||
$warnmsg = $warn
|
||||
if $warn == true {
|
||||
$use_warn_message = $warn_message ? {
|
||||
undef => $default_warn_message,
|
||||
default => $warn_message,
|
||||
}
|
||||
} else {
|
||||
$use_warn_message = undef
|
||||
}
|
||||
|
||||
$warnmsg_escaped = regsubst($warnmsg, "'", "'\\\\''", 'G')
|
||||
$warnmsg_escaped = regsubst($use_warn_message, "'", "'\\\\''", 'G')
|
||||
$warnflag = $warnmsg_escaped ? {
|
||||
'' => '',
|
||||
default => "-w '${warnmsg_escaped}'"
|
||||
}
|
||||
|
||||
case $force {
|
||||
'true', true, 'yes', 'on': {
|
||||
$forceflag = '-f'
|
||||
}
|
||||
'false', false, 'no', 'off': {
|
||||
$forceflag = ''
|
||||
}
|
||||
default: {
|
||||
fail("Improper 'force' value given to concat: ${force}")
|
||||
}
|
||||
$forceflag = $force ? {
|
||||
true => '-f',
|
||||
false => '',
|
||||
}
|
||||
|
||||
case $order {
|
||||
'numeric': {
|
||||
$orderflag = '-n'
|
||||
}
|
||||
'alpha': {
|
||||
$orderflag = ''
|
||||
}
|
||||
default: {
|
||||
fail("Improper 'order' value given to concat: ${order}")
|
||||
}
|
||||
$orderflag = $order ? {
|
||||
'numeric' => '-n',
|
||||
'alpha' => '',
|
||||
}
|
||||
|
||||
case $ensure_newline {
|
||||
'true', true, 'yes', 'on': {
|
||||
$newlineflag = '-l'
|
||||
}
|
||||
'false', false, 'no', 'off': {
|
||||
$newlineflag = ''
|
||||
}
|
||||
default: {
|
||||
fail("Improper 'ensure_newline' value given to concat: ${ensure_newline}")
|
||||
}
|
||||
$newlineflag = $ensure_newline ? {
|
||||
true => '-l',
|
||||
false => '',
|
||||
}
|
||||
|
||||
File {
|
||||
owner => $::id,
|
||||
owner => $owner,
|
||||
group => $safe_group,
|
||||
mode => $mode,
|
||||
backup => $backup,
|
||||
replace => $replace
|
||||
}
|
||||
|
||||
$source_real = $version ? {
|
||||
24 => 'puppet:///concat/null',
|
||||
default => undef,
|
||||
}
|
||||
|
||||
if $ensure == 'present' {
|
||||
file { $fragdir:
|
||||
ensure => directory,
|
||||
|
@ -154,7 +142,6 @@ define concat(
|
|||
notify => Exec["concat_${name}"],
|
||||
purge => true,
|
||||
recurse => true,
|
||||
source => $source_real,
|
||||
}
|
||||
|
||||
file { "${fragdir}/fragments.concat":
|
||||
|
@ -169,23 +156,23 @@ define concat(
|
|||
ensure => present,
|
||||
path => $path,
|
||||
alias => "concat_${name}",
|
||||
group => $safe_group,
|
||||
mode => $mode,
|
||||
owner => $owner,
|
||||
source => "${fragdir}/${concat_name}",
|
||||
}
|
||||
|
||||
# remove extra whitespace from string interopolation to make testing easier
|
||||
$command = strip(regsubst("${concat::setup::concatdir}/bin/concatfragments.sh -o ${fragdir}/${concat_name} -d ${fragdir} ${warnflag} ${forceflag} ${orderflag} ${newlineflag}", '\s+', ' ', 'G'))
|
||||
|
||||
exec { "concat_${name}":
|
||||
alias => "concat_${fragdir}",
|
||||
command => "${concat::setup::concatdir}/bin/concatfragments.sh -o ${fragdir}/${concat_name} -d ${fragdir} ${warnflag} ${forceflag} ${orderflag} ${newlineflag}",
|
||||
command => $command,
|
||||
notify => File[$name],
|
||||
subscribe => File[$fragdir],
|
||||
unless => "${command} -t",
|
||||
require => [
|
||||
File[$fragdir],
|
||||
File["${fragdir}/fragments"],
|
||||
File["${fragdir}/fragments.concat"],
|
||||
],
|
||||
subscribe => File[$fragdir],
|
||||
unless => "${concat::setup::concatdir}/bin/concatfragments.sh -o ${fragdir}/${concat_name} -d ${fragdir} -t ${warnflag} ${forceflag} ${orderflag} ${newlineflag}",
|
||||
}
|
||||
|
||||
if $::id == 'root' {
|
||||
|
|
|
@ -1,27 +1,23 @@
|
|||
# === Class: concat::setup
|
||||
#
|
||||
# Sets up the concat system.
|
||||
# Sets up the concat system. This is a private class.
|
||||
#
|
||||
# [$concatdir]
|
||||
# is where the fragments live and is set on the fact concat_basedir.
|
||||
# Since puppet should always manage files in $concatdir and they should
|
||||
# not be deleted ever, /tmp is not an option.
|
||||
#
|
||||
# [$puppetversion]
|
||||
# should be either 24 or 25 to enable a 24 compatible
|
||||
# mode, in 24 mode you might see phantom notifies this is a side effect
|
||||
# of the method we use to clear the fragments directory.
|
||||
#
|
||||
# The regular expression below will try to figure out your puppet version
|
||||
# but this code will only work in 0.24.8 and newer.
|
||||
#
|
||||
# It also copies out the concatfragments.sh file to ${concatdir}/bin
|
||||
#
|
||||
class concat::setup {
|
||||
$id = $::id
|
||||
$root_group = $id ? {
|
||||
if $caller_module_name != $module_name {
|
||||
fail("Use of private class ${name} by ${caller_module_name}")
|
||||
}
|
||||
|
||||
$owner = $::id
|
||||
$root_group = $owner ? {
|
||||
root => 0,
|
||||
default => $id
|
||||
default => $owner
|
||||
}
|
||||
|
||||
if $::concat_basedir {
|
||||
|
@ -30,22 +26,16 @@ class concat::setup {
|
|||
fail ('$concat_basedir not defined. Try running again with pluginsync=true on the [master] and/or [main] section of your node\'s \'/etc/puppet/puppet.conf\'.')
|
||||
}
|
||||
|
||||
$majorversion = regsubst($::puppetversion, '^[0-9]+[.]([0-9]+)[.][0-9]+$', '\1')
|
||||
$fragments_source = $majorversion ? {
|
||||
24 => 'puppet:///concat/concatfragments.sh',
|
||||
default => 'puppet:///modules/concat/concatfragments.sh'
|
||||
}
|
||||
|
||||
file { "${concatdir}/bin/concatfragments.sh":
|
||||
owner => $id,
|
||||
owner => $owner,
|
||||
group => $root_group,
|
||||
mode => '0755',
|
||||
source => $fragments_source,
|
||||
source => 'puppet:///modules/concat/concatfragments.sh',
|
||||
}
|
||||
|
||||
file { [ $concatdir, "${concatdir}/bin" ]:
|
||||
ensure => directory,
|
||||
owner => $id,
|
||||
owner => $owner,
|
||||
group => $root_group,
|
||||
mode => '0750',
|
||||
}
|
||||
|
|
|
@ -1,37 +0,0 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe 'concat::fragment', :type => :defne do
|
||||
let(:title) { 'motd_header' }
|
||||
|
||||
let(:facts) do
|
||||
{
|
||||
:concat_basedir => '/var/lib/puppet/concat',
|
||||
:id => 'root',
|
||||
}
|
||||
end
|
||||
|
||||
let :pre_condition do
|
||||
"concat{ '/etc/motd': }"
|
||||
end
|
||||
|
||||
context 'target => /etc/motd' do
|
||||
let(:params) {{ :target => '/etc/motd' }}
|
||||
it do
|
||||
should contain_class('concat::setup')
|
||||
should contain_concat('/etc/motd')
|
||||
should contain_concat__fragment('motd_header').with({
|
||||
:target => '/etc/motd',
|
||||
})
|
||||
should contain_file('/var/lib/puppet/concat/_etc_motd/fragments/10_motd_header' ).with({
|
||||
:ensure => 'present',
|
||||
:mode => '0644',
|
||||
:owner => 'root',
|
||||
:group => 0,
|
||||
:source => nil,
|
||||
:content => nil,
|
||||
:backup => 'puppet',
|
||||
:alias => 'concat_fragment_motd_header',
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,131 +0,0 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe 'concat' do
|
||||
basedir = '/var/lib/puppet/concat'
|
||||
let(:title) { '/etc/foo.bar' }
|
||||
let(:facts) { {
|
||||
:concat_basedir => '/var/lib/puppet/concat',
|
||||
:id => 'root',
|
||||
} }
|
||||
|
||||
directories = [
|
||||
"#{basedir}/_etc_foo.bar",
|
||||
"#{basedir}/_etc_foo.bar/fragments",
|
||||
]
|
||||
|
||||
directories.each do |dirs|
|
||||
it do
|
||||
should contain_file(dirs).with({
|
||||
'ensure' => 'directory',
|
||||
'backup' => 'puppet',
|
||||
'group' => 0,
|
||||
'mode' => '0644',
|
||||
'owner' => 'root',
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
files = [
|
||||
"/etc/foo.bar",
|
||||
"#{basedir}/_etc_foo.bar/fragments.concat",
|
||||
]
|
||||
|
||||
files.each do |file|
|
||||
it do
|
||||
should contain_file(file).with({
|
||||
'ensure' => 'present',
|
||||
'backup' => 'puppet',
|
||||
'group' => 0,
|
||||
'mode' => '0644',
|
||||
'owner' => 'root',
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
it do
|
||||
should contain_exec("concat_/etc/foo.bar").with_command(
|
||||
"#{basedir}/bin/concatfragments.sh " +
|
||||
"-o #{basedir}/_etc_foo.bar/fragments.concat.out " +
|
||||
"-d #{basedir}/_etc_foo.bar "
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'concat' do
|
||||
|
||||
basedir = '/var/lib/puppet/concat'
|
||||
let(:title) { 'foobar' }
|
||||
let(:target) { '/etc/foo.bar' }
|
||||
let(:facts) { {
|
||||
:concat_basedir => '/var/lib/puppet/concat',
|
||||
:id => 'root',
|
||||
} }
|
||||
|
||||
directories = [
|
||||
"#{basedir}/foobar",
|
||||
"#{basedir}/foobar/fragments",
|
||||
]
|
||||
|
||||
directories.each do |dirs|
|
||||
it do
|
||||
should contain_file(dirs).with({
|
||||
'ensure' => 'directory',
|
||||
'backup' => 'puppet',
|
||||
'group' => 0,
|
||||
'mode' => '0644',
|
||||
'owner' => 'root',
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
files = [
|
||||
"foobar",
|
||||
"#{basedir}/foobar/fragments.concat",
|
||||
]
|
||||
|
||||
files.each do |file|
|
||||
it do
|
||||
should contain_file(file).with({
|
||||
'ensure' => 'present',
|
||||
'backup' => 'puppet',
|
||||
'group' => 0,
|
||||
'mode' => '0644',
|
||||
'owner' => 'root',
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
it do
|
||||
should contain_exec("concat_foobar").with_command(
|
||||
"#{basedir}/bin/concatfragments.sh " +
|
||||
"-o #{basedir}/foobar/fragments.concat.out " +
|
||||
"-d #{basedir}/foobar "
|
||||
)
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
describe 'concat' do
|
||||
let(:title) { '/etc/foo.bar' }
|
||||
let(:params) { {
|
||||
:group => 'something',
|
||||
:owner => 'someone',
|
||||
:mode => '0755'
|
||||
} }
|
||||
let(:facts) { {
|
||||
:concat_basedir => '/var/lib/puppet/concat',
|
||||
:id => 'root',
|
||||
} }
|
||||
|
||||
it do
|
||||
should contain_file("/etc/foo.bar").with( {
|
||||
'ensure' => 'present',
|
||||
'owner' => 'someone',
|
||||
'group' => 'something',
|
||||
'mode' => '0755',
|
||||
} )
|
||||
end
|
||||
end
|
||||
|
||||
# vim:sw=2:ts=2:expandtab:textwidth=79
|
|
@ -21,5 +21,6 @@ RSpec.configure do |c|
|
|||
|
||||
# Install modules and dependencies
|
||||
puppet_module_install(:source => proj_root, :module_name => 'concat')
|
||||
shell('puppet module install puppetlabs-stdlib')
|
||||
end
|
||||
end
|
||||
|
|
76
spec/unit/classes/concat_setup_spec.rb
Normal file
76
spec/unit/classes/concat_setup_spec.rb
Normal file
|
@ -0,0 +1,76 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe 'concat::setup', :type => :class do
|
||||
|
||||
shared_examples 'setup' do |id, concatdir|
|
||||
id = 'root' if id.nil?
|
||||
concatdir = '/foo' if concatdir.nil?
|
||||
|
||||
let(:facts) {{ :id => id, :concat_basedir => concatdir }}
|
||||
|
||||
it do
|
||||
should contain_file("#{concatdir}/bin/concatfragments.sh").with({
|
||||
:owner => id,
|
||||
:group => id == 'root' ? 0 : id,
|
||||
:mode => '0755',
|
||||
:source => 'puppet:///modules/concat/concatfragments.sh',
|
||||
})
|
||||
end
|
||||
|
||||
[concatdir, "#{concatdir}/bin"].each do |file|
|
||||
it do
|
||||
should contain_file(file).with({
|
||||
:ensure => 'directory',
|
||||
:owner => id,
|
||||
:group => id == 'root' ? 0 : id,
|
||||
:mode => '0750',
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
it do
|
||||
should contain_file('/usr/local/bin/concatfragments.sh').with({
|
||||
:ensure => 'absent',
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
context 'facts' do
|
||||
# concat::setup is a private module so we need pretend that we are calling
|
||||
# it from elsewhere in the same module
|
||||
let(:pre_condition) do
|
||||
"$caller_module_name = 'concat'"
|
||||
end
|
||||
|
||||
context 'id =>' do
|
||||
context 'root' do
|
||||
it_behaves_like 'setup', 'root'
|
||||
end
|
||||
|
||||
context 'apenny' do
|
||||
it_behaves_like 'setup', 'apenny'
|
||||
end
|
||||
end
|
||||
|
||||
context 'concat_basedir =>' do
|
||||
context '/foo' do
|
||||
it_behaves_like 'setup', 'root', '/foo'
|
||||
end
|
||||
|
||||
context 'undef' do
|
||||
let(:facts) {{ :id => 'root' }}
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /#{Regexp.escape('$concat_basedir not defined. Try running again with pluginsync=true on the [master] and/or [main] section of your node\'s \'/etc/puppet/puppet.conf\'.')}/)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end # facts
|
||||
|
||||
context 'called from another module namespace' do
|
||||
let(:facts) {{ :id => 'root', :concat_basedir => '/foo' }}
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /Use of private class concat::setup/)
|
||||
end
|
||||
end
|
||||
end
|
239
spec/unit/defines/concat_fragment_spec.rb
Normal file
239
spec/unit/defines/concat_fragment_spec.rb
Normal file
|
@ -0,0 +1,239 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe 'concat::fragment', :type => :define do
|
||||
|
||||
shared_examples 'fragment' do |title, params|
|
||||
params = {} if params.nil?
|
||||
|
||||
id = 'root'
|
||||
|
||||
p = {
|
||||
:content => nil,
|
||||
:source => nil,
|
||||
:order => 10,
|
||||
:ensure => 'present',
|
||||
:mode => '0644',
|
||||
:owner => id,
|
||||
:group => nil,
|
||||
:backup => 'puppet',
|
||||
}.merge(params)
|
||||
|
||||
safe_name = title.gsub(/[\/\n]/, '_')
|
||||
safe_target_name = p[:target].gsub(/[\/\n]/, '_')
|
||||
concatdir = '/var/lib/puppet/concat'
|
||||
fragdir = "#{concatdir}/#{safe_target_name}"
|
||||
if p[:group].nil?
|
||||
safe_group = id == 'root' ? 0 : id
|
||||
else
|
||||
safe_group = p[:group]
|
||||
end
|
||||
|
||||
let(:title) { title }
|
||||
let(:facts) do
|
||||
{
|
||||
:concat_basedir => concatdir,
|
||||
:id => id,
|
||||
}
|
||||
end
|
||||
let(:params) { params }
|
||||
let(:pre_condition) do
|
||||
"concat{ '#{p[:target]}': }"
|
||||
end
|
||||
|
||||
it do
|
||||
should contain_class('concat::setup')
|
||||
should contain_concat(p[:target])
|
||||
should contain_file("#{fragdir}/fragments/#{p[:order]}_#{safe_name}").with({
|
||||
:ensure => p[:ensure],
|
||||
:mode => p[:mode],
|
||||
:owner => p[:owner],
|
||||
:group => safe_group,
|
||||
:source => p[:source],
|
||||
:content => p[:content],
|
||||
:backup => p[:backup],
|
||||
:alias => "concat_fragment_#{title}",
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
context 'target =>' do
|
||||
context '/etc/motd' do
|
||||
it_behaves_like 'fragment', 'motd_header', {
|
||||
:target => '/etc/motd',
|
||||
}
|
||||
end
|
||||
|
||||
['./etc/motd', 'etc/motd', false].each do |target|
|
||||
context target do
|
||||
let(:title) { 'motd_header' }
|
||||
let(:facts) {{ :concat_basedir => '/tmp', :id => 'root' }}
|
||||
let(:params) {{ :target => target }}
|
||||
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /is not an absolute path/)
|
||||
end
|
||||
end
|
||||
end
|
||||
end # target =>
|
||||
|
||||
context 'ensure =>' do
|
||||
['', 'present', 'absent', 'file', 'directory'].each do |ens|
|
||||
context ens do
|
||||
it_behaves_like 'fragment', 'motd_header', {
|
||||
:ensure => ens,
|
||||
:target => '/etc/motd',
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
context 'invalid' do
|
||||
let(:title) { 'motd_header' }
|
||||
let(:facts) {{ :concat_basedir => '/tmp', :id => 'root' }}
|
||||
let(:params) {{ :ensure => 'invalid', :target => '/etc/motd' }}
|
||||
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /#{Regexp.escape('does not match "^$|^present$|^absent$|^file$|^directory$"')}/)
|
||||
end
|
||||
end
|
||||
end # ensure =>
|
||||
|
||||
context 'content =>' do
|
||||
['', 'ashp is our hero'].each do |content|
|
||||
context content do
|
||||
it_behaves_like 'fragment', 'motd_header', {
|
||||
:content => content,
|
||||
:target => '/etc/motd',
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
context 'false' do
|
||||
let(:title) { 'motd_header' }
|
||||
let(:facts) {{ :concat_basedir => '/tmp', :id => 'root' }}
|
||||
let(:params) {{ :content => false, :target => '/etc/motd' }}
|
||||
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /is not a string/)
|
||||
end
|
||||
end
|
||||
end # content =>
|
||||
|
||||
context 'source =>' do
|
||||
['', '/foo/bar'].each do |source|
|
||||
context source do
|
||||
it_behaves_like 'fragment', 'motd_header', {
|
||||
:source => source,
|
||||
:target => '/etc/motd',
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
context 'false' do
|
||||
let(:title) { 'motd_header' }
|
||||
let(:facts) {{ :concat_basedir => '/tmp', :id => 'root' }}
|
||||
let(:params) {{ :source => false, :target => '/etc/motd' }}
|
||||
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /is not a string/)
|
||||
end
|
||||
end
|
||||
end # source =>
|
||||
|
||||
context 'order =>' do
|
||||
['', '42', 'a', 'z'].each do |order|
|
||||
context '\'\'' do
|
||||
it_behaves_like 'fragment', 'motd_header', {
|
||||
:order => order,
|
||||
:target => '/etc/motd',
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
context 'false' do
|
||||
let(:title) { 'motd_header' }
|
||||
let(:facts) {{ :concat_basedir => '/tmp', :id => 'root' }}
|
||||
let(:params) {{ :order => false, :target => '/etc/motd' }}
|
||||
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /is not a string/)
|
||||
end
|
||||
end
|
||||
end # order =>
|
||||
|
||||
context 'mode =>' do
|
||||
context '1755' do
|
||||
it_behaves_like 'fragment', 'motd_header', {
|
||||
:mode => '1755',
|
||||
:target => '/etc/motd',
|
||||
}
|
||||
end
|
||||
|
||||
context 'false' do
|
||||
let(:title) { 'motd_header' }
|
||||
let(:facts) {{ :concat_basedir => '/tmp', :id => 'root' }}
|
||||
let(:params) {{ :mode => false, :target => '/etc/motd' }}
|
||||
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /is not a string/)
|
||||
end
|
||||
end
|
||||
end # mode =>
|
||||
|
||||
context 'owner =>' do
|
||||
context 'apenny' do
|
||||
it_behaves_like 'fragment', 'motd_header', {
|
||||
:owner => 'apenny',
|
||||
:target => '/etc/motd',
|
||||
}
|
||||
end
|
||||
|
||||
context 'false' do
|
||||
let(:title) { 'motd_header' }
|
||||
let(:facts) {{ :concat_basedir => '/tmp', :id => 'root' }}
|
||||
let(:params) {{ :owner => false, :target => '/etc/motd' }}
|
||||
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /is not a string/)
|
||||
end
|
||||
end
|
||||
end # owner =>
|
||||
|
||||
context 'group =>' do
|
||||
context 'apenny' do
|
||||
it_behaves_like 'fragment', 'motd_header', {
|
||||
:group => 'apenny',
|
||||
:target => '/etc/motd',
|
||||
}
|
||||
end
|
||||
|
||||
context 'false' do
|
||||
let(:title) { 'motd_header' }
|
||||
let(:facts) {{ :concat_basedir => '/tmp', :id => 'root' }}
|
||||
let(:params) {{ :group => false, :target => '/etc/motd' }}
|
||||
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /is not a string/)
|
||||
end
|
||||
end
|
||||
end # group =>
|
||||
|
||||
context 'backup =>' do
|
||||
context 'apenny' do
|
||||
it_behaves_like 'fragment', 'motd_header', {
|
||||
:backup => 'apenny',
|
||||
:target => '/etc/motd',
|
||||
}
|
||||
end
|
||||
|
||||
context 'false' do
|
||||
let(:title) { 'motd_header' }
|
||||
let(:facts) {{ :concat_basedir => '/tmp', :id => 'root' }}
|
||||
let(:params) {{ :backup => false, :target => '/etc/motd' }}
|
||||
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /is not a string/)
|
||||
end
|
||||
end
|
||||
end # backup =>
|
||||
|
||||
end
|
382
spec/unit/defines/concat_spec.rb
Normal file
382
spec/unit/defines/concat_spec.rb
Normal file
|
@ -0,0 +1,382 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe 'concat', :type => :define do
|
||||
|
||||
shared_examples 'concat' do |title, params|
|
||||
params = {} if params.nil?
|
||||
|
||||
id = 'root'
|
||||
|
||||
# default param values
|
||||
p = {
|
||||
:ensure => 'present',
|
||||
:path => title,
|
||||
:owner => id,
|
||||
:group => '0',
|
||||
:mode => '0644',
|
||||
:warn => false,
|
||||
:warn_message => nil,
|
||||
:force => false,
|
||||
:backup => 'puppet',
|
||||
:replace => true,
|
||||
:order => 'alpha',
|
||||
:ensure_newline => false,
|
||||
}.merge(params)
|
||||
|
||||
safe_name = title.gsub('/', '_')
|
||||
concatdir = '/var/lib/puppet/concat'
|
||||
fragdir = "#{concatdir}/#{safe_name}"
|
||||
concat_name = 'fragments.concat.out'
|
||||
default_warn_message = '# This file is managed by Puppet. DO NOT EDIT.'
|
||||
|
||||
file_defaults = {
|
||||
:owner => p[:owner],
|
||||
:group => p[:group],
|
||||
:mode => p[:mode],
|
||||
:backup => p[:backup],
|
||||
:replace => p[:replace],
|
||||
}
|
||||
|
||||
let(:title) { title }
|
||||
let(:params) { params }
|
||||
let(:facts) do
|
||||
{
|
||||
:concat_basedir => concatdir,
|
||||
:id => id,
|
||||
}
|
||||
end
|
||||
|
||||
if p[:ensure] == 'present'
|
||||
it do
|
||||
should contain_file(fragdir).with(file_defaults.merge({
|
||||
:ensure => 'directory',
|
||||
}))
|
||||
end
|
||||
|
||||
it do
|
||||
should contain_file("#{fragdir}/fragments").with(file_defaults.merge({
|
||||
:ensure => 'directory',
|
||||
:force => true,
|
||||
:ignore => ['.svn', '.git', '.gitignore'],
|
||||
:purge => true,
|
||||
:recurse => true,
|
||||
}))
|
||||
end
|
||||
|
||||
[
|
||||
"#{fragdir}/fragments.concat",
|
||||
"#{fragdir}/#{concat_name}",
|
||||
].each do |file|
|
||||
it do
|
||||
should contain_file(file).with(file_defaults.merge({
|
||||
:ensure => 'present',
|
||||
}))
|
||||
end
|
||||
end
|
||||
|
||||
it do
|
||||
should contain_file(title).with(file_defaults.merge({
|
||||
:ensure => 'present',
|
||||
:path => p[:path],
|
||||
:alias => "concat_#{title}",
|
||||
:source => "#{fragdir}/#{concat_name}",
|
||||
}))
|
||||
end
|
||||
|
||||
cmd = "#{concatdir}/bin/concatfragments.sh " +
|
||||
"-o #{concatdir}/#{safe_name}/fragments.concat.out " +
|
||||
"-d #{concatdir}/#{safe_name}"
|
||||
|
||||
# flag order: fragdir, warnflag, forceflag, orderflag, newlineflag
|
||||
if p[:warn]
|
||||
message = p[:warn_message] || default_warn_message
|
||||
cmd += " -w \'#{message}\'"
|
||||
end
|
||||
|
||||
cmd += " -f" if p[:force]
|
||||
cmd += " -n" if p[:order] == 'numeric'
|
||||
cmd += " -l" if p[:ensure_newline] == true
|
||||
|
||||
it do
|
||||
should contain_exec("concat_#{title}").with({
|
||||
:alias => "concat_#{fragdir}",
|
||||
:command => cmd,
|
||||
:unless => "#{cmd} -t",
|
||||
})
|
||||
|
||||
if id == 'root'
|
||||
should contain_exec("concat_#{title}").with({
|
||||
:user => 'root',
|
||||
:group => p[:group],
|
||||
})
|
||||
end
|
||||
end
|
||||
else
|
||||
[
|
||||
fragdir,
|
||||
"#{fragdir}/fragments",
|
||||
"#{fragdir}/fragments.concat",
|
||||
"#{fragdir}/#{concat_name}",
|
||||
].each do |file|
|
||||
it do
|
||||
should contain_file(file).with(file_defaults.merge({
|
||||
:ensure => 'absent',
|
||||
:backup => false,
|
||||
:force => true,
|
||||
}))
|
||||
end
|
||||
end
|
||||
|
||||
it do
|
||||
should contain_file(title).with(file_defaults.merge({
|
||||
:ensure => 'absent',
|
||||
}))
|
||||
end
|
||||
|
||||
it do
|
||||
should contain_file(title).with(file_defaults.merge({
|
||||
:ensure => 'absent',
|
||||
}))
|
||||
end
|
||||
|
||||
it do
|
||||
should contain_exec("concat_#{title}").with({
|
||||
:alias => "concat_#{fragdir}",
|
||||
:command => 'true',
|
||||
:path => '/bin:/usr/bin',
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'title' do
|
||||
context 'without path param' do
|
||||
# title/name is the default value for the path param. therefore, the
|
||||
# title must be an absolute path unless path is specified
|
||||
['/foo', '/foo/bar', '/foo/bar/baz'].each do |title|
|
||||
context title do
|
||||
it_behaves_like 'concat', '/etc/foo.bar'
|
||||
end
|
||||
end
|
||||
|
||||
['./foo', 'foo', 'foo/bar'].each do |title|
|
||||
context title do
|
||||
let(:title) { title }
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /is not an absolute path/)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'with path param' do
|
||||
['./foo', 'foo', 'foo/bar'].each do |title|
|
||||
context title do
|
||||
it_behaves_like 'concat', title, { :path => '/etc/foo.bar' }
|
||||
end
|
||||
end
|
||||
end
|
||||
end # title =>
|
||||
|
||||
context 'ensure =>' do
|
||||
['present', 'absent'].each do |ens|
|
||||
context ens do
|
||||
it_behaves_like 'concat', '/etc/foo.bar', { :ensure => ens }
|
||||
end
|
||||
end
|
||||
|
||||
context 'invalid' do
|
||||
let(:title) { '/etc/foo.bar' }
|
||||
let(:params) {{ :ensure => 'invalid' }}
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /#{Regexp.escape('does not match "^present$|^absent$"')}/)
|
||||
end
|
||||
end
|
||||
end # ensure =>
|
||||
|
||||
context 'path =>' do
|
||||
context '/foo' do
|
||||
it_behaves_like 'concat', '/etc/foo.bar', { :path => '/foo' }
|
||||
end
|
||||
|
||||
['./foo', 'foo', 'foo/bar', false].each do |path|
|
||||
context path do
|
||||
let(:title) { '/etc/foo.bar' }
|
||||
let(:params) {{ :path => path }}
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /is not an absolute path/)
|
||||
end
|
||||
end
|
||||
end
|
||||
end # path =>
|
||||
|
||||
context 'owner =>' do
|
||||
context 'apenney' do
|
||||
it_behaves_like 'concat', '/etc/foo.bar', { :owner => 'apenny' }
|
||||
end
|
||||
|
||||
context 'false' do
|
||||
let(:title) { '/etc/foo.bar' }
|
||||
let(:params) {{ :owner => false }}
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /is not a string/)
|
||||
end
|
||||
end
|
||||
end # owner =>
|
||||
|
||||
context 'group =>' do
|
||||
context 'apenney' do
|
||||
it_behaves_like 'concat', '/etc/foo.bar', { :group => 'apenny' }
|
||||
end
|
||||
|
||||
context 'false' do
|
||||
let(:title) { '/etc/foo.bar' }
|
||||
let(:params) {{ :group => false }}
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /is not a string/)
|
||||
end
|
||||
end
|
||||
end # group =>
|
||||
|
||||
context 'mode =>' do
|
||||
context '1755' do
|
||||
it_behaves_like 'concat', '/etc/foo.bar', { :mode => '1755' }
|
||||
end
|
||||
|
||||
context 'false' do
|
||||
let(:title) { '/etc/foo.bar' }
|
||||
let(:params) {{ :mode => false }}
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /is not a string/)
|
||||
end
|
||||
end
|
||||
end # mode =>
|
||||
|
||||
context 'warn =>' do
|
||||
[true, false].each do |warn|
|
||||
context warn do
|
||||
it_behaves_like 'concat', '/etc/foo.bar', { :warn => warn }
|
||||
end
|
||||
end
|
||||
|
||||
context '123' do
|
||||
let(:title) { '/etc/foo.bar' }
|
||||
let(:params) {{ :warn => 123 }}
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /is not a boolean/)
|
||||
end
|
||||
end
|
||||
end # warn =>
|
||||
|
||||
context 'warn_message =>' do
|
||||
context '# ashp replaced your file' do
|
||||
# should do nothing unless warn == true;
|
||||
# but we can't presently test that because concatfragments.sh isn't run
|
||||
# from rspec-puppet tests
|
||||
context 'warn =>' do
|
||||
context 'true' do
|
||||
it_behaves_like 'concat', '/etc/foo.bar', {
|
||||
:warn => true,
|
||||
:warn_message => '# ashp replaced your file'
|
||||
}
|
||||
end
|
||||
|
||||
context 'false' do
|
||||
it_behaves_like 'concat', '/etc/foo.bar', {
|
||||
:warn => false,
|
||||
:warn_message => '# ashp replaced your file'
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'false' do
|
||||
let(:title) { '/etc/foo.bar' }
|
||||
let(:params) {{ :warn_message => false }}
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /is not a string/)
|
||||
end
|
||||
end
|
||||
end # warn_message =>
|
||||
|
||||
context 'force =>' do
|
||||
[true, false].each do |force|
|
||||
context force do
|
||||
it_behaves_like 'concat', '/etc/foo.bar', { :force => force }
|
||||
end
|
||||
end
|
||||
|
||||
context '123' do
|
||||
let(:title) { '/etc/foo.bar' }
|
||||
let(:params) {{ :force => 123 }}
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /is not a boolean/)
|
||||
end
|
||||
end
|
||||
end # force =>
|
||||
|
||||
context 'backup =>' do
|
||||
context 'reverse' do
|
||||
it_behaves_like 'concat', '/etc/foo.bar', { :backup => 'reverse' }
|
||||
end
|
||||
|
||||
context 'false' do
|
||||
let(:title) { '/etc/foo.bar' }
|
||||
let(:params) {{ :backup => false }}
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /is not a string/)
|
||||
end
|
||||
end
|
||||
end # backup =>
|
||||
|
||||
context 'replace =>' do
|
||||
[true, false].each do |replace|
|
||||
context replace do
|
||||
it_behaves_like 'concat', '/etc/foo.bar', { :replace => replace }
|
||||
end
|
||||
end
|
||||
|
||||
context '123' do
|
||||
let(:title) { '/etc/foo.bar' }
|
||||
let(:params) {{ :replace => 123 }}
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /is not a boolean/)
|
||||
end
|
||||
end
|
||||
end # replace =>
|
||||
|
||||
context 'order =>' do
|
||||
['alpha', 'numeric'].each do |order|
|
||||
context order do
|
||||
it_behaves_like 'concat', '/etc/foo.bar', { :order => order }
|
||||
end
|
||||
end
|
||||
|
||||
context 'invalid' do
|
||||
let(:title) { '/etc/foo.bar' }
|
||||
let(:params) {{ :order => 'invalid' }}
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /#{Regexp.escape('does not match "^alpha$|^numeric$"')}/)
|
||||
end
|
||||
end
|
||||
end # order =>
|
||||
|
||||
context 'ensure_newline =>' do
|
||||
[true, false].each do |ensure_newline|
|
||||
context 'true' do
|
||||
it_behaves_like 'concat', '/etc/foo.bar', { :ensure_newline => ensure_newline}
|
||||
end
|
||||
end
|
||||
|
||||
context '123' do
|
||||
let(:title) { '/etc/foo.bar' }
|
||||
let(:params) {{ :ensure_newline => 123 }}
|
||||
it 'should fail' do
|
||||
expect { should }.to raise_error(Puppet::Error, /is not a boolean/)
|
||||
end
|
||||
end
|
||||
end # ensure_newline =>
|
||||
|
||||
end
|
||||
|
||||
# vim:sw=2:ts=2:expandtab:textwidth=79
|
Loading…
Reference in a new issue