2014-05-07 11:49:25 +02:00
|
|
|
#! /usr/bin/env ruby -S rspec
|
2012-12-06 11:01:19 +01:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2013-11-19 19:42:19 +01:00
|
|
|
TESTEXE = File.exists?('/usr/bin/test') ? '/usr/bin/test' : '/bin/test'
|
|
|
|
TOUCHEXE = File.exists?('/usr/bin/touch') ? '/usr/bin/touch' : '/bin/touch'
|
|
|
|
|
2012-12-06 11:01:19 +01:00
|
|
|
describe Puppet::Parser::Functions.function(:validate_cmd) do
|
|
|
|
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
|
|
|
|
|
|
|
|
subject do
|
|
|
|
function_name = Puppet::Parser::Functions.function(:validate_cmd)
|
|
|
|
scope.method(function_name)
|
|
|
|
end
|
|
|
|
|
2014-12-09 15:42:31 +01:00
|
|
|
context 'with no % placeholder' do
|
|
|
|
describe "with an explicit failure message" do
|
|
|
|
it "prints the failure message on error" do
|
|
|
|
expect {
|
|
|
|
subject.call ['', '/bin/false', 'failure message!']
|
|
|
|
}.to raise_error Puppet::ParseError, /failure message!/
|
|
|
|
end
|
2012-12-06 11:01:19 +01:00
|
|
|
end
|
|
|
|
|
2014-12-09 15:42:31 +01:00
|
|
|
describe "on validation failure" do
|
|
|
|
it "includes the command error output" do
|
|
|
|
expect {
|
|
|
|
subject.call ['', "#{TOUCHEXE} /cant/touch/this"]
|
|
|
|
}.to raise_error Puppet::ParseError, /(cannot touch|o such file or)/
|
|
|
|
end
|
|
|
|
|
|
|
|
it "includes the command return value" do
|
|
|
|
expect {
|
|
|
|
subject.call ['', '/cant/run/this']
|
|
|
|
}.to raise_error Puppet::ParseError, /returned 1\b/
|
|
|
|
end
|
2012-12-06 11:01:19 +01:00
|
|
|
end
|
|
|
|
|
2014-12-09 15:42:31 +01:00
|
|
|
describe "when performing actual validation" do
|
|
|
|
it "can positively validate file content" do
|
|
|
|
expect { subject.call ["non-empty", "#{TESTEXE} -s"] }.to_not raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can negatively validate file content" do
|
|
|
|
expect {
|
|
|
|
subject.call ["", "#{TESTEXE} -s"]
|
|
|
|
}.to raise_error Puppet::ParseError, /failed to validate.*test -s/
|
|
|
|
end
|
2012-12-06 11:01:19 +01:00
|
|
|
end
|
2013-09-19 06:48:45 +02:00
|
|
|
end
|
2012-12-06 11:01:19 +01:00
|
|
|
|
2014-12-09 15:42:31 +01:00
|
|
|
context 'with % placeholder' do
|
|
|
|
describe "with an explicit failure message" do
|
|
|
|
it "prints the failure message on error" do
|
|
|
|
expect {
|
|
|
|
subject.call ['', '/bin/false % -f', 'failure message!']
|
|
|
|
}.to raise_error Puppet::ParseError, /failure message!/
|
|
|
|
end
|
2012-12-06 11:01:19 +01:00
|
|
|
end
|
2014-12-09 15:42:31 +01:00
|
|
|
describe "on validation failure" do
|
|
|
|
it "includes the command error output" do
|
|
|
|
expect {
|
|
|
|
subject.call ['', "#{TOUCHEXE} /cant/touch/this"]
|
|
|
|
}.to raise_error Puppet::ParseError, /(cannot touch|o such file or)/
|
|
|
|
end
|
|
|
|
|
|
|
|
it "includes the command return value" do
|
|
|
|
expect {
|
|
|
|
subject.call ['', '/cant/run/this % -z']
|
|
|
|
}.to raise_error Puppet::ParseError, /Execution of '\/cant\/run\/this .+ -z' returned 1/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when performing actual validation" do
|
|
|
|
it "can positively validate file content" do
|
|
|
|
expect { subject.call ["non-empty", "#{TESTEXE} -s %"] }.to_not raise_error
|
|
|
|
end
|
2012-12-06 11:01:19 +01:00
|
|
|
|
2014-12-09 15:42:31 +01:00
|
|
|
it "can negatively validate file content" do
|
|
|
|
expect {
|
|
|
|
subject.call ["", "#{TESTEXE} -s %"]
|
|
|
|
}.to raise_error Puppet::ParseError, /failed to validate.*test -s/
|
|
|
|
end
|
2012-12-06 11:01:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|