summaryrefslogtreecommitdiff
path: root/spec/functions
diff options
context:
space:
mode:
Diffstat (limited to 'spec/functions')
-rwxr-xr-xspec/functions/ceiling_spec.rb39
-rwxr-xr-xspec/functions/concat_spec.rb17
-rwxr-xr-xspec/functions/delete_spec.rb31
-rwxr-xr-xspec/functions/fqdn_rotate_spec.rb17
-rwxr-xr-xspec/functions/is_domain_name_spec.rb7
-rwxr-xr-xspec/functions/prefix_spec.rb5
-rw-r--r--spec/functions/type3x_spec.rb43
-rwxr-xr-xspec/functions/type_spec.rb5
-rwxr-xr-xspec/functions/upcase_spec.rb27
-rwxr-xr-xspec/functions/uriescape_spec.rb7
-rwxr-xr-xspec/functions/validate_absolute_path_spec.rb38
-rwxr-xr-xspec/functions/validate_cmd_spec.rb81
12 files changed, 269 insertions, 48 deletions
diff --git a/spec/functions/ceiling_spec.rb b/spec/functions/ceiling_spec.rb
new file mode 100755
index 0000000..814aa7c
--- /dev/null
+++ b/spec/functions/ceiling_spec.rb
@@ -0,0 +1,39 @@
+#! /usr/bin/env ruby -S rspec
+
+require 'spec_helper'
+
+describe "the ceiling function" do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+
+ it "should exist" do
+ expect(Puppet::Parser::Functions.function("ceiling")).to eq("function_ceiling")
+ end
+
+ it "should raise a ParseError if there is less than 1 argument" do
+ expect { scope.function_ceiling([]) }.to( raise_error(Puppet::ParseError, /Wrong number of arguments/))
+ end
+
+ it "should should raise a ParseError if input isn't numeric (eg. String)" do
+ expect { scope.function_ceiling(["foo"]) }.to( raise_error(Puppet::ParseError, /Wrong argument type/))
+ end
+
+ it "should should raise a ParseError if input isn't numeric (eg. Boolean)" do
+ expect { scope.function_ceiling([true]) }.to( raise_error(Puppet::ParseError, /Wrong argument type/))
+ end
+
+ it "should return an integer when a numeric type is passed" do
+ result = scope.function_ceiling([12.4])
+ expect(result.is_a?(Integer)).to(eq(true))
+ end
+
+ it "should return the input when an integer is passed" do
+ result = scope.function_ceiling([7])
+ expect(result).to(eq(7))
+ end
+
+ it "should return the smallest integer greater than or equal to the input" do
+ result = scope.function_ceiling([3.8])
+ expect(result).to(eq(4))
+ 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/delete_spec.rb b/spec/functions/delete_spec.rb
index 39b3176..c8edd78 100755
--- a/spec/functions/delete_spec.rb
+++ b/spec/functions/delete_spec.rb
@@ -9,48 +9,53 @@ describe "the delete function" do
end
it "should raise a ParseError if there are fewer than 2 arguments" do
- expect { scope.function_delete([]) }.to( raise_error(Puppet::ParseError))
+ expect { scope.function_delete([]) }.to(raise_error(Puppet::ParseError))
end
it "should raise a ParseError if there are greater than 2 arguments" do
- expect { scope.function_delete([[], 'foo', 'bar']) }.to( raise_error(Puppet::ParseError))
+ expect { scope.function_delete([[], 'foo', 'bar']) }.to(raise_error(Puppet::ParseError))
end
it "should raise a TypeError if a number is passed as the first argument" do
- expect { scope.function_delete([1, 'bar']) }.to( raise_error(TypeError))
+ expect { scope.function_delete([1, 'bar']) }.to(raise_error(TypeError))
end
it "should delete all instances of an element from an array" do
- result = scope.function_delete([['a','b','c','b'],'b'])
- expect(result).to(eq(['a','c']))
+ result = scope.function_delete([['a', 'b', 'c', 'b'], 'b'])
+ expect(result).to(eq(['a', 'c']))
end
it "should delete all instances of a substring from a string" do
- result = scope.function_delete(['foobarbabarz','bar'])
+ result = scope.function_delete(['foobarbabarz', 'bar'])
expect(result).to(eq('foobaz'))
end
it "should delete a key from a hash" do
- result = scope.function_delete([{ 'a' => 1, 'b' => 2, 'c' => 3 },'b'])
- expect(result).to(eq({ 'a' => 1, 'c' => 3 }))
+ result = scope.function_delete([{'a' => 1, 'b' => 2, 'c' => 3}, 'b'])
+ expect(result).to(eq({'a' => 1, 'c' => 3}))
+ end
+
+ it 'should accept an array of items to delete' do
+ result = scope.function_delete([{'a' => 1, 'b' => 2, 'c' => 3}, ['b', 'c']])
+ expect(result).to(eq({'a' => 1}))
end
it "should not change origin array passed as argument" do
- origin_array = ['a','b','c','d']
+ origin_array = ['a', 'b', 'c', 'd']
result = scope.function_delete([origin_array, 'b'])
- expect(origin_array).to(eq(['a','b','c','d']))
+ expect(origin_array).to(eq(['a', 'b', 'c', 'd']))
end
it "should not change the origin string passed as argument" do
origin_string = 'foobarbabarz'
- result = scope.function_delete([origin_string,'bar'])
+ result = scope.function_delete([origin_string, 'bar'])
expect(origin_string).to(eq('foobarbabarz'))
end
it "should not change origin hash passed as argument" do
- origin_hash = { 'a' => 1, 'b' => 2, 'c' => 3 }
+ origin_hash = {'a' => 1, 'b' => 2, 'c' => 3}
result = scope.function_delete([origin_hash, 'b'])
- expect(origin_hash).to(eq({ 'a' => 1, 'b' => 2, 'c' => 3 }))
+ expect(origin_hash).to(eq({'a' => 1, 'b' => 2, 'c' => 3}))
end
end
diff --git a/spec/functions/fqdn_rotate_spec.rb b/spec/functions/fqdn_rotate_spec.rb
index 40057d4..673a8a3 100755
--- a/spec/functions/fqdn_rotate_spec.rb
+++ b/spec/functions/fqdn_rotate_spec.rb
@@ -40,4 +40,21 @@ describe "the fqdn_rotate function" do
result = scope.function_fqdn_rotate([value])
result.size.should(eq(4))
end
+
+ it "should use the Puppet::Util.deterministic_rand function if available" do
+ scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1")
+ if Puppet::Util.respond_to?(:deterministic_rand)
+ Puppet::Util.expects(:deterministic_rand).with(113646079810780526294648115052177588845,4)
+ end
+ scope.function_fqdn_rotate(["asdf"])
+ end
+
+ it "should not leave the global seed in a deterministic state" do
+ scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1").twice
+ scope.function_fqdn_rotate(["asdf"])
+ rand1 = rand()
+ scope.function_fqdn_rotate(["asdf"])
+ rand2 = rand()
+ expect(rand1).not_to eql(rand2)
+ end
end
diff --git a/spec/functions/is_domain_name_spec.rb b/spec/functions/is_domain_name_spec.rb
index 4d05f5c..5ab8369 100755
--- a/spec/functions/is_domain_name_spec.rb
+++ b/spec/functions/is_domain_name_spec.rb
@@ -61,4 +61,11 @@ describe "the is_domain_name function" do
result = scope.function_is_domain_name(["not valid"])
expect(result).to(be_falsey)
end
+
+ # Values obtained from Facter values will be frozen strings
+ # in newer versions of Facter:
+ it "should not throw an exception if passed a frozen string" do
+ result = scope.function_is_domain_name(["my.domain.name".freeze])
+ expect(result).to(be_truthy)
+ end
end
diff --git a/spec/functions/prefix_spec.rb b/spec/functions/prefix_spec.rb
index 34cac53..aec8a7d 100755
--- a/spec/functions/prefix_spec.rb
+++ b/spec/functions/prefix_spec.rb
@@ -25,4 +25,9 @@ describe "the prefix function" do
result = scope.function_prefix([['a','b','c'], 'p'])
expect(result).to(eq(['pa','pb','pc']))
end
+
+ it "returns a prefixed hash" do
+ result = scope.function_prefix([{'a' => 'b','b' => 'c','c' => 'd'}, 'p'])
+ expect(result).to(eq({'pa'=>'b','pb'=>'c','pc'=>'d'}))
+ end
end
diff --git a/spec/functions/type3x_spec.rb b/spec/functions/type3x_spec.rb
new file mode 100644
index 0000000..d21236a
--- /dev/null
+++ b/spec/functions/type3x_spec.rb
@@ -0,0 +1,43 @@
+#! /usr/bin/env ruby -S rspec
+require 'spec_helper'
+
+describe "the type3x function" do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+ it "should exist" do
+ expect(Puppet::Parser::Functions.function("type3x")).to eq("function_type3x")
+ end
+
+ it "should raise a ParseError if there is less than 1 arguments" do
+ expect { scope.function_type3x([]) }.to( raise_error(Puppet::ParseError))
+ end
+
+ it "should return string when given a string" do
+ result = scope.function_type3x(["aaabbbbcccc"])
+ expect(result).to(eq('string'))
+ end
+
+ it "should return array when given an array" do
+ result = scope.function_type3x([["aaabbbbcccc","asdf"]])
+ expect(result).to(eq('array'))
+ end
+
+ it "should return hash when given a hash" do
+ result = scope.function_type3x([{"a"=>1,"b"=>2}])
+ expect(result).to(eq('hash'))
+ end
+
+ it "should return integer when given an integer" do
+ result = scope.function_type3x(["1"])
+ expect(result).to(eq('integer'))
+ end
+
+ it "should return float when given a float" do
+ result = scope.function_type3x(["1.34"])
+ expect(result).to(eq('float'))
+ end
+
+ it "should return boolean when given a boolean" do
+ result = scope.function_type3x([true])
+ expect(result).to(eq('boolean'))
+ end
+end
diff --git a/spec/functions/type_spec.rb b/spec/functions/type_spec.rb
index 9dfe9d7..b683fcf 100755
--- a/spec/functions/type_spec.rb
+++ b/spec/functions/type_spec.rb
@@ -7,8 +7,9 @@ describe "the type function" do
expect(Puppet::Parser::Functions.function("type")).to eq("function_type")
end
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_type([]) }.to( raise_error(Puppet::ParseError))
+ it "should give a deprecation warning when called" do
+ scope.expects(:warning).with("type() DEPRECATED: This function will cease to function on Puppet 4; please use type3x() before upgrading to puppet 4 for backwards-compatibility, or migrate to the new parser's typing system.")
+ scope.function_type(["aoeu"])
end
it "should return string when given a string" do
diff --git a/spec/functions/upcase_spec.rb b/spec/functions/upcase_spec.rb
index 3cf8b05..0689099 100755
--- a/spec/functions/upcase_spec.rb
+++ b/spec/functions/upcase_spec.rb
@@ -9,7 +9,7 @@ describe "the upcase function" do
end
it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_upcase([]) }.to( raise_error(Puppet::ParseError))
+ expect { scope.function_upcase([]) }.to(raise_error(Puppet::ParseError))
end
it "should upcase a string" do
@@ -30,4 +30,29 @@ describe "the upcase function" do
result = scope.function_upcase([value])
result.should(eq('ABC'))
end
+
+ it 'should accept hashes and return uppercase' do
+ expect(
+ scope.function_upcase([{'test' => %w(this that and other thing)}])
+ ).to eq({'TEST' => %w(THIS THAT AND OTHER THING)})
+ end
+
+ if :test.respond_to?(:upcase)
+ it 'should accept hashes of symbols' do
+ expect(
+ scope.function_upcase([{:test => [:this, :that, :other]}])
+ ).to eq({:TEST => [:THIS, :THAT, :OTHER]})
+ end
+ it 'should return upcase symbol' do
+ expect(
+ scope.function_upcase([:test])
+ ).to eq(:TEST)
+ end
+ it 'should return mixed objects in upcease' do
+ expect(
+ scope.function_upcase([[:test, 'woot']])
+ ).to eq([:TEST, 'WOOT'])
+
+ end
+ end
end
diff --git a/spec/functions/uriescape_spec.rb b/spec/functions/uriescape_spec.rb
index 2321e5a..d0f37de 100755
--- a/spec/functions/uriescape_spec.rb
+++ b/spec/functions/uriescape_spec.rb
@@ -17,6 +17,13 @@ describe "the uriescape function" do
expect(result).to(eq(':/?%23[]@!$&\'()*+,;=%20%22%7B%7D'))
end
+ it "should uriescape an array of strings, while not touching up nonstrings" do
+ teststring = ":/?#[]@!$&'()*+,;= \"{}"
+ expectstring = ':/?%23[]@!$&\'()*+,;=%20%22%7B%7D'
+ result = scope.function_uriescape([[teststring, teststring, 1]])
+ expect(result).to(eq([expectstring, expectstring, 1]))
+ end
+
it "should do nothing if a string is already safe" do
result = scope.function_uriescape(["ABCdef"])
expect(result).to(eq('ABCdef'))
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