Fix ensure => absent with path => set.

The current code doesn't correctly account for an absent with a
path set, incorrectly trying to remove $name instead.  Fixing this
raised the issue that fragments left with no ensure will fail when
absent is set as the fragdir is deleted.

As a workaround for this we check the `ensure` parameter from the
concat{} resource with getparam() and then pass that to the fragment
if no specific ensure was passed in.  This effectively ensures
fragments inherit their parents ensure status.
This commit is contained in:
Ashley Penney 2014-01-21 16:28:42 -05:00
parent fd8ca0fb8b
commit b07f338f29
3 changed files with 64 additions and 9 deletions

View file

@ -29,16 +29,13 @@ define concat::fragment(
$content = undef,
$source = undef,
$order = 10,
$ensure = 'present',
$ensure = undef,
$mode = undef,
$owner = undef,
$group = undef,
$backup = undef
) {
validate_string($target)
if ! ($ensure in [ 'present', 'absent' ]) {
warning('Passing a value other than \'present\' or \'absent\' as the $ensure parameter to concat::fragment is deprecated. If you want to use the content of a file as a fragment please use the $source parameter.')
}
validate_string($content)
if !(is_string($source) or is_array($source)) {
fail('$source is not a string or an Array.')
@ -56,6 +53,14 @@ define concat::fragment(
if $backup {
warning('The $backup parameter to concat::fragment is deprecated and has no effect')
}
if $ensure == undef {
$_ensure = getparam(Concat[$target], 'ensure')
} else {
if ! ($ensure in [ 'present', 'absent' ]) {
warning('Passing a value other than \'present\' or \'absent\' as the $ensure parameter to concat::fragment is deprecated. If you want to use the content of a file as a fragment please use the $source parameter.')
}
$_ensure = $ensure
}
include concat::setup
@ -71,18 +76,18 @@ define concat::fragment(
# be paranoid and only allow the fragment's file resource's ensure param to
# be file, absent, or a file target
$safe_ensure = $ensure ? {
$safe_ensure = $_ensure ? {
'' => 'file',
undef => 'file',
'file' => 'file',
'present' => 'file',
'absent' => 'absent',
default => $ensure,
default => $_ensure,
}
# if it looks line ensure => /target syntax was used, fish that out
if ! ($ensure in ['', 'present', 'absent', 'file' ]) {
$ensure_target = $ensure
if ! ($_ensure in ['', 'present', 'absent', 'file' ]) {
$ensure_target = $_ensure
}
# the file type's semantics only allows one of: ensure => /target, content,

View file

@ -206,7 +206,7 @@ define concat(
force => true,
}
file { $name:
file { $path:
ensure => absent,
backup => $backup,
}

View file

@ -151,4 +151,54 @@ describe 'basic concat test' do
it { should contain '2' }
end
end
context 'ensure' do
context 'works when set to present with path set' do
pp="
concat { 'file':
ensure => present,
path => '/tmp/concat/file',
mode => '0644',
}
concat::fragment { '1':
target => 'file',
content => '1',
order => '01',
}
"
it_behaves_like 'successfully_applied', pp
describe file('/tmp/concat/file') do
it { should be_file }
it { should be_mode 644 }
it { should contain '1' }
end
end
context 'works when set to absent with path set' do
pp="
concat { 'file':
ensure => absent,
path => '/tmp/concat/file',
mode => '0644',
}
concat::fragment { '1':
target => 'file',
content => '1',
order => '01',
}
"
# Can't used shared examples as this will always trigger the exec when
# absent is set.
it 'applies the manifest twice with no stderr' do
expect(apply_manifest(pp, :catch_failures => true).stderr).to eq("")
expect(apply_manifest(pp, :catch_failures => true).stderr).to eq("")
end
describe file('/tmp/concat/file') do
it { should_not be_file }
end
end
end
end