Skip to content

Commit

Permalink
Merge pull request redhat-openstack#243 from jmkeyes/add-validation-c…
Browse files Browse the repository at this point in the history
…ommand

Support running a validation command on the destination file.
  • Loading branch information
Morgan Haskel committed Feb 12, 2015
2 parents bccd7ed + 0133d3a commit 3a634af
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 18 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ Ensure there's a newline at the end of the fragments.
- ensure_newline => true
- ensure_newline => false

#####`validate_cmd`
Ensure the destination file passes the following validation command.

######Example
- validate_cmd => '/usr/sbin/apache2 -t -f %'
- validate_cmd => '/usr/sbin/visudo -c -f %'

####concat::fragment

#####`target`
Expand Down
23 changes: 14 additions & 9 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
$replace = true,
$order = 'alpha',
$ensure_newline = false,
$validate_cmd = undef,
$gnu = undef
) {
validate_re($ensure, '^present$|^absent$')
Expand All @@ -81,6 +82,9 @@
validate_bool($replace)
validate_re($order, '^alpha$|^numeric$')
validate_bool($ensure_newline)
if $validate_cmd and ! is_string($validate_cmd) {
fail('$validate_cmd must be a string')
}
if $gnu {
warning('The $gnu parameter to concat is deprecated and has no effect')
}
Expand Down Expand Up @@ -173,15 +177,16 @@
}

file { $name:
ensure => present,
owner => $owner,
group => $group,
mode => $mode,
replace => $replace,
path => $path,
alias => "concat_${name}",
source => "${fragdir}/${concat_name}",
backup => $backup,
ensure => present,
owner => $owner,
group => $group,
mode => $mode,
replace => $replace,
path => $path,
alias => "concat_${name}",
source => "${fragdir}/${concat_name}",
validate_cmd => $validate_cmd,
backup => $backup,
}

# remove extra whitespace from string interpolation to make testing easier
Expand Down
35 changes: 35 additions & 0 deletions spec/acceptance/validation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'spec_helper_acceptance'

describe 'concat validate_cmd parameter' do
basedir = default.tmpdir('concat')
context '=> "/usr/bin/test -e %"' do
before(:all) do
pp = <<-EOS
file { '#{basedir}':
ensure => directory
}
EOS

apply_manifest(pp)
end
pp = <<-EOS
concat { '#{basedir}/file':
validate_cmd => '/usr/bin/test -e %',
}
concat::fragment { 'content':
target => '#{basedir}/file',
content => 'content',
}
EOS

it 'applies the manifest twice with no stderr' do
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_changes => true)
end

describe file("#{basedir}/file") do
it { should be_file }
it { should contain 'content' }
end
end
end
36 changes: 27 additions & 9 deletions spec/unit/defines/concat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
:replace => true,
:order => 'alpha',
:ensure_newline => false,
:validate_cmd => nil,
}.merge(params)

safe_name = title.gsub('/', '_')
Expand Down Expand Up @@ -77,15 +78,16 @@

it do
should contain_file(title).with(file_defaults.merge({
:ensure => 'present',
:owner => p[:owner],
:group => p[:group],
:mode => p[:mode],
:replace => p[:replace],
:path => p[:path],
:alias => "concat_#{title}",
:source => "#{fragdir}/#{concat_name}",
:backup => p[:backup],
:ensure => 'present',
:owner => p[:owner],
:group => p[:group],
:mode => p[:mode],
:replace => p[:replace],
:path => p[:path],
:alias => "concat_#{title}",
:source => "#{fragdir}/#{concat_name}",
:validate_cmd => p[:validate_cmd],
:backup => p[:backup],
}))
end

Expand Down Expand Up @@ -381,6 +383,22 @@
end
end # ensure_newline =>

context 'validate_cmd =>' do
context '/usr/bin/test -e %' do
it_behaves_like 'concat', '/etc/foo.bar', { :validate_cmd => '/usr/bin/test -e %' }
end

[ 1234, true ].each do |cmd|
context cmd do
let(:title) { '/etc/foo.bar' }
let(:params) {{ :validate_cmd => cmd }}
it 'should fail' do
expect { should }.to raise_error(Puppet::Error, /\$validate_cmd must be a string/)
end
end
end
end # validate_cmd =>

describe 'deprecated parameter' do
context 'gnu =>' do
context 'foo' do
Expand Down

0 comments on commit 3a634af

Please sign in to comment.