summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rwxr-xr-xspec/acceptance/concat_spec.rb22
-rwxr-xr-xspec/functions/concat_spec.rb17
-rwxr-xr-xspec/functions/validate_absolute_path_spec.rb38
-rwxr-xr-xspec/functions/validate_cmd_spec.rb81
-rwxr-xr-xspec/unit/puppet/parser/functions/basename_spec.rb46
5 files changed, 172 insertions, 32 deletions
diff --git a/spec/acceptance/concat_spec.rb b/spec/acceptance/concat_spec.rb
index 7bda365..caf2f7d 100755
--- a/spec/acceptance/concat_spec.rb
+++ b/spec/acceptance/concat_spec.rb
@@ -14,5 +14,27 @@ describe 'concat function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('oper
apply_manifest(pp, :catch_failures => true)
end
+ it 'should concat arrays and primitives to array' do
+ pp = <<-EOS
+ $output = concat(['1','2','3'],'4','5','6',['7','8','9'])
+ validate_array($output)
+ if size($output) != 9 {
+ fail("${output} should have 9 elements.")
+ }
+ EOS
+
+ apply_manifest(pp, :catch_failures => true)
+ end
+ it 'should concat multiple arrays to one' do
+ pp = <<-EOS
+ $output = concat(['1','2','3'],['4','5','6'],['7','8','9'])
+ validate_array($output)
+ if size($output) != 6 {
+ fail("${output} should have 9 elements.")
+ }
+ EOS
+
+ apply_manifest(pp, :catch_failures => true)
+ end
end
end
diff --git a/spec/functions/concat_spec.rb b/spec/functions/concat_spec.rb
index 49cb2ad..49fa6bb 100755
--- a/spec/functions/concat_spec.rb
+++ b/spec/functions/concat_spec.rb
@@ -4,14 +4,19 @@ require 'spec_helper'
describe "the concat function" do
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
- it "should raise a ParseError if the client does not provide two arguments" do
+ it "should raise a ParseError if the client does not provide at least two arguments" do
expect { scope.function_concat([]) }.to(raise_error(Puppet::ParseError))
+ expect { scope.function_concat([[1]]) }.to(raise_error(Puppet::ParseError))
end
it "should raise a ParseError if the first parameter is not an array" do
expect { scope.function_concat([1, []])}.to(raise_error(Puppet::ParseError))
end
+ it "should not raise a ParseError if the client provides more than two arguments" do
+ expect { scope.function_concat([[1],[2],[3]]) }.not_to raise_error
+ end
+
it "should be able to concat an array" do
result = scope.function_concat([['1','2','3'],['4','5','6']])
expect(result).to(eq(['1','2','3','4','5','6']))
@@ -32,4 +37,14 @@ describe "the concat function" do
result = scope.function_concat([array_original,['4','5','6']])
array_original.should(eq(['1','2','3']))
end
+
+ it "should be able to concat multiple arrays" do
+ result = scope.function_concat([['1','2','3'],['4','5','6'],['7','8','9']])
+ expect(result).to(eq(['1','2','3','4','5','6','7','8','9']))
+ end
+
+ it "should be able to concat mix of primitives and arrays to a final array" do
+ result = scope.function_concat([['1','2','3'],'4',['5','6','7']])
+ expect(result).to(eq(['1','2','3','4','5','6','7']))
+ end
end
diff --git a/spec/functions/validate_absolute_path_spec.rb b/spec/functions/validate_absolute_path_spec.rb
index 342ae84..36c836b 100755
--- a/spec/functions/validate_absolute_path_spec.rb
+++ b/spec/functions/validate_absolute_path_spec.rb
@@ -39,6 +39,11 @@ describe Puppet::Parser::Functions.function(:validate_absolute_path) do
expect { subject.call [path] }.not_to raise_error
end
end
+ valid_paths do
+ it "validate_absolute_path(#{valid_paths.inspect}) should not fail" do
+ expect { subject.call [valid_paths] }.not_to raise_error
+ end
+ end
end
context "Puppet without mocking" do
@@ -47,6 +52,11 @@ describe Puppet::Parser::Functions.function(:validate_absolute_path) do
expect { subject.call [path] }.not_to raise_error
end
end
+ valid_paths do
+ it "validate_absolute_path(#{valid_paths.inspect}) should not fail" do
+ expect { subject.call [valid_paths] }.not_to raise_error
+ end
+ end
end
end
@@ -55,6 +65,7 @@ describe Puppet::Parser::Functions.function(:validate_absolute_path) do
[
nil,
[ nil ],
+ [ nil, nil ],
{ 'foo' => 'bar' },
{ },
'',
@@ -66,19 +77,28 @@ describe Puppet::Parser::Functions.function(:validate_absolute_path) do
end
context 'Relative paths' do
- %w{
- relative1
- .
- ..
- ./foo
- ../foo
- etc/puppetlabs/puppet
- opt/puppet/bin
- }.each do |path|
+ def self.rel_paths
+ %w{
+ relative1
+ .
+ ..
+ ./foo
+ ../foo
+ etc/puppetlabs/puppet
+ opt/puppet/bin
+ }
+ end
+ rel_paths.each do |path|
it "validate_absolute_path(#{path.inspect}) should fail" do
expect { subject.call [path] }.to raise_error Puppet::ParseError
end
end
+ rel_paths do
+ it "validate_absolute_path(#{rel_paths.inspect}) should fail" do
+ expect { subject.call [rel_paths] }.to raise_error Puppet::ParseError
+ end
+ end
end
end
end
+
diff --git a/spec/functions/validate_cmd_spec.rb b/spec/functions/validate_cmd_spec.rb
index a6e68df..7cb9782 100755
--- a/spec/functions/validate_cmd_spec.rb
+++ b/spec/functions/validate_cmd_spec.rb
@@ -12,37 +12,74 @@ describe Puppet::Parser::Functions.function(:validate_cmd) do
scope.method(function_name)
end
- 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!/
+ 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
end
- end
- 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)/
+ 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
end
- it "includes the command return value" do
- expect {
- subject.call ['', '/cant/run/this']
- }.to raise_error Puppet::ParseError, /returned 1\b/
+ 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
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
+ 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
end
+ 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
- it "can negatively validate file content" do
- expect {
- subject.call ["", "#{TESTEXE} -s"]
- }.to raise_error Puppet::ParseError, /failed to validate.*test -s/
+ it "can negatively validate file content" do
+ expect {
+ subject.call ["", "#{TESTEXE} -s %"]
+ }.to raise_error Puppet::ParseError, /failed to validate.*test -s/
+ end
end
end
end
diff --git a/spec/unit/puppet/parser/functions/basename_spec.rb b/spec/unit/puppet/parser/functions/basename_spec.rb
new file mode 100755
index 0000000..8a2d0dc
--- /dev/null
+++ b/spec/unit/puppet/parser/functions/basename_spec.rb
@@ -0,0 +1,46 @@
+#! /usr/bin/env ruby -S rspec
+require 'spec_helper'
+
+describe "the basename function" do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+
+ it "should exist" do
+ Puppet::Parser::Functions.function("basename").should == "function_basename"
+ end
+
+ it "should raise a ParseError if there is less than 1 argument" do
+ lambda { scope.function_basename([]) }.should( raise_error(Puppet::ParseError))
+ end
+
+ it "should raise a ParseError if there are more than 2 arguments" do
+ lambda { scope.function_basename(['a', 'b', 'c']) }.should( raise_error(Puppet::ParseError))
+ end
+
+ it "should return basename for an absolute path" do
+ result = scope.function_basename(['/path/to/a/file.ext'])
+ result.should(eq('file.ext'))
+ end
+
+ it "should return basename for a relative path" do
+ result = scope.function_basename(['path/to/a/file.ext'])
+ result.should(eq('file.ext'))
+ end
+
+ it "should strip extention when extension specified (absolute path)" do
+ result = scope.function_basename(['/path/to/a/file.ext', '.ext'])
+ result.should(eq('file'))
+ end
+
+ it "should strip extention when extension specified (relative path)" do
+ result = scope.function_basename(['path/to/a/file.ext', '.ext'])
+ result.should(eq('file'))
+ end
+
+ it "should complain about non-string first argument" do
+ lambda { scope.function_basename([[]]) }.should( raise_error(Puppet::ParseError))
+ end
+
+ it "should complain about non-string second argument" do
+ lambda { scope.function_basename(['/path/to/a/file.ext', []]) }.should( raise_error(Puppet::ParseError))
+ end
+end