Skip to content

Commit be46f0e

Browse files
committed
Merge pull request #377 from petems/MODULES-1582-improve_validate_cmd
(MODULES-1582) File location placeholder
2 parents 696c89d + b3d007f commit be46f0e

File tree

2 files changed

+75
-27
lines changed

2 files changed

+75
-27
lines changed

lib/puppet/parser/functions/validate_cmd.rb

+16-5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ module Puppet::Parser::Functions
66
Perform validation of a string with an external command.
77
The first argument of this function should be a string to
88
test, and the second argument should be a path to a test command
9-
taking a file as last argument. If the command, launched against
10-
a tempfile containing the passed string, returns a non-null value,
11-
compilation will abort with a parse error.
9+
taking a % as a placeholder for the file path (will default to the end).
10+
If the command, launched against a tempfile containing the passed string,
11+
returns a non-null value, compilation will abort with a parse error.
1212
1313
If a third argument is specified, this will be the error message raised and
1414
seen by the user.
@@ -17,8 +17,12 @@ module Puppet::Parser::Functions
1717
1818
Example:
1919
20+
# Defaults to end of path
2021
validate_cmd($sudoerscontent, '/usr/sbin/visudo -c -f', 'Visudo failed to validate sudoers content')
2122
23+
# % as file location
24+
validate_cmd($haproxycontent, '/usr/sbin/haproxy -f % -c', 'Haproxy failed to validate config content')
25+
2226
ENDHEREDOC
2327
if (args.length < 2) or (args.length > 3) then
2428
raise Puppet::ParseError, ("validate_cmd(): wrong number of arguments (#{args.length}; must be 2 or 3)")
@@ -34,10 +38,17 @@ module Puppet::Parser::Functions
3438
begin
3539
tmpfile.write(content)
3640
tmpfile.close
41+
42+
if checkscript =~ /\s%(\s|$)/
43+
check_with_correct_location = checkscript.gsub(/%/,tmpfile.path)
44+
else
45+
check_with_correct_location = "#{checkscript} #{tmpfile.path}"
46+
end
47+
3748
if Puppet::Util::Execution.respond_to?('execute')
38-
Puppet::Util::Execution.execute("#{checkscript} #{tmpfile.path}")
49+
Puppet::Util::Execution.execute(check_with_correct_location)
3950
else
40-
Puppet::Util.execute("#{checkscript} #{tmpfile.path}")
51+
Puppet::Util.execute(check_with_correct_location)
4152
end
4253
rescue Puppet::ExecutionFailure => detail
4354
msg += "\n#{detail}"

spec/functions/validate_cmd_spec.rb

+59-22
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,74 @@
1212
scope.method(function_name)
1313
end
1414

15-
describe "with an explicit failure message" do
16-
it "prints the failure message on error" do
17-
expect {
18-
subject.call ['', '/bin/false', 'failure message!']
19-
}.to raise_error Puppet::ParseError, /failure message!/
15+
context 'with no % placeholder' do
16+
describe "with an explicit failure message" do
17+
it "prints the failure message on error" do
18+
expect {
19+
subject.call ['', '/bin/false', 'failure message!']
20+
}.to raise_error Puppet::ParseError, /failure message!/
21+
end
2022
end
21-
end
2223

23-
describe "on validation failure" do
24-
it "includes the command error output" do
25-
expect {
26-
subject.call ['', "#{TOUCHEXE} /cant/touch/this"]
27-
}.to raise_error Puppet::ParseError, /(cannot touch|o such file or)/
24+
describe "on validation failure" do
25+
it "includes the command error output" do
26+
expect {
27+
subject.call ['', "#{TOUCHEXE} /cant/touch/this"]
28+
}.to raise_error Puppet::ParseError, /(cannot touch|o such file or)/
29+
end
30+
31+
it "includes the command return value" do
32+
expect {
33+
subject.call ['', '/cant/run/this']
34+
}.to raise_error Puppet::ParseError, /returned 1\b/
35+
end
2836
end
2937

30-
it "includes the command return value" do
31-
expect {
32-
subject.call ['', '/cant/run/this']
33-
}.to raise_error Puppet::ParseError, /returned 1\b/
38+
describe "when performing actual validation" do
39+
it "can positively validate file content" do
40+
expect { subject.call ["non-empty", "#{TESTEXE} -s"] }.to_not raise_error
41+
end
42+
43+
it "can negatively validate file content" do
44+
expect {
45+
subject.call ["", "#{TESTEXE} -s"]
46+
}.to raise_error Puppet::ParseError, /failed to validate.*test -s/
47+
end
3448
end
3549
end
3650

37-
describe "when performing actual validation" do
38-
it "can positively validate file content" do
39-
expect { subject.call ["non-empty", "#{TESTEXE} -s"] }.to_not raise_error
51+
context 'with % placeholder' do
52+
describe "with an explicit failure message" do
53+
it "prints the failure message on error" do
54+
expect {
55+
subject.call ['', '/bin/false % -f', 'failure message!']
56+
}.to raise_error Puppet::ParseError, /failure message!/
57+
end
4058
end
59+
describe "on validation failure" do
60+
it "includes the command error output" do
61+
expect {
62+
subject.call ['', "#{TOUCHEXE} /cant/touch/this"]
63+
}.to raise_error Puppet::ParseError, /(cannot touch|o such file or)/
64+
end
65+
66+
it "includes the command return value" do
67+
expect {
68+
subject.call ['', '/cant/run/this % -z']
69+
}.to raise_error Puppet::ParseError, /Execution of '\/cant\/run\/this .+ -z' returned 1/
70+
end
71+
end
72+
73+
describe "when performing actual validation" do
74+
it "can positively validate file content" do
75+
expect { subject.call ["non-empty", "#{TESTEXE} -s %"] }.to_not raise_error
76+
end
4177

42-
it "can negatively validate file content" do
43-
expect {
44-
subject.call ["", "#{TESTEXE} -s"]
45-
}.to raise_error Puppet::ParseError, /failed to validate.*test -s/
78+
it "can negatively validate file content" do
79+
expect {
80+
subject.call ["", "#{TESTEXE} -s %"]
81+
}.to raise_error Puppet::ParseError, /failed to validate.*test -s/
82+
end
4683
end
4784
end
4885
end

0 commit comments

Comments
 (0)