summaryrefslogtreecommitdiff
path: root/spec/unit/puppet/parser
diff options
context:
space:
mode:
Diffstat (limited to 'spec/unit/puppet/parser')
-rw-r--r--spec/unit/puppet/parser/functions/concat_spec.rb15
-rw-r--r--spec/unit/puppet/parser/functions/floor_spec.rb39
-rw-r--r--spec/unit/puppet/parser/functions/fqdn_rotate_spec.rb2
-rw-r--r--spec/unit/puppet/parser/functions/get_module_path_spec.rb6
-rw-r--r--spec/unit/puppet/parser/functions/is_function_available.rb31
-rw-r--r--spec/unit/puppet/parser/functions/merge_spec.rb2
-rw-r--r--spec/unit/puppet/parser/functions/str2saltedsha512_spec.rb6
-rw-r--r--spec/unit/puppet/parser/functions/suffix_spec.rb19
-rw-r--r--spec/unit/puppet/parser/functions/validate_array_spec.rb6
-rw-r--r--spec/unit/puppet/parser/functions/validate_augeas_spec.rb102
-rw-r--r--spec/unit/puppet/parser/functions/validate_bool_spec.rb8
-rw-r--r--spec/unit/puppet/parser/functions/validate_cmd_spec.rb81
-rw-r--r--spec/unit/puppet/parser/functions/validate_hash_spec.rb7
-rw-r--r--spec/unit/puppet/parser/functions/validate_string_spec.rb3
14 files changed, 306 insertions, 21 deletions
diff --git a/spec/unit/puppet/parser/functions/concat_spec.rb b/spec/unit/puppet/parser/functions/concat_spec.rb
new file mode 100644
index 0000000..123188b
--- /dev/null
+++ b/spec/unit/puppet/parser/functions/concat_spec.rb
@@ -0,0 +1,15 @@
+#! /usr/bin/env ruby -S rspec
+require 'spec_helper'
+
+describe "the concat function" do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+
+ it "should raise a ParseError if there is less than 1 arguments" do
+ lambda { scope.function_concat([]) }.should( raise_error(Puppet::ParseError))
+ end
+
+ it "should be able to concat an array" do
+ result = scope.function_concat([['1','2','3'],['4','5','6']])
+ result.should(eq(['1','2','3','4','5','6']))
+ end
+end
diff --git a/spec/unit/puppet/parser/functions/floor_spec.rb b/spec/unit/puppet/parser/functions/floor_spec.rb
new file mode 100644
index 0000000..dbc8c77
--- /dev/null
+++ b/spec/unit/puppet/parser/functions/floor_spec.rb
@@ -0,0 +1,39 @@
+#! /usr/bin/env ruby -S rspec
+
+require 'spec_helper'
+
+describe "the floor function" do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+
+ it "should exist" do
+ Puppet::Parser::Functions.function("floor").should == "function_floor"
+ end
+
+ it "should raise a ParseError if there is less than 1 argument" do
+ lambda { scope.function_floor([]) }.should( raise_error(Puppet::ParseError, /Wrong number of arguments/))
+ end
+
+ it "should should raise a ParseError if input isn't numeric (eg. String)" do
+ lambda { scope.function_floor(["foo"]) }.should( raise_error(Puppet::ParseError, /Wrong argument type/))
+ end
+
+ it "should should raise a ParseError if input isn't numeric (eg. Boolean)" do
+ lambda { scope.function_floor([true]) }.should( raise_error(Puppet::ParseError, /Wrong argument type/))
+ end
+
+ it "should return an integer when a numeric type is passed" do
+ result = scope.function_floor([12.4])
+ result.is_a?(Integer).should(eq(true))
+ end
+
+ it "should return the input when an integer is passed" do
+ result = scope.function_floor([7])
+ result.should(eq(7))
+ end
+
+ it "should return the largest integer less than or equal to the input" do
+ result = scope.function_floor([3.8])
+ result.should(eq(3))
+ end
+end
+
diff --git a/spec/unit/puppet/parser/functions/fqdn_rotate_spec.rb b/spec/unit/puppet/parser/functions/fqdn_rotate_spec.rb
index 4eb799d..2577723 100644
--- a/spec/unit/puppet/parser/functions/fqdn_rotate_spec.rb
+++ b/spec/unit/puppet/parser/functions/fqdn_rotate_spec.rb
@@ -19,7 +19,7 @@ describe "the fqdn_rotate function" do
end
it "should rotate a string to give the same results for one host" do
- scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1").twice
+ scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1").twice
scope.function_fqdn_rotate(["abcdefg"]).should eql(scope.function_fqdn_rotate(["abcdefg"]))
end
diff --git a/spec/unit/puppet/parser/functions/get_module_path_spec.rb b/spec/unit/puppet/parser/functions/get_module_path_spec.rb
index e761706..486bef6 100644
--- a/spec/unit/puppet/parser/functions/get_module_path_spec.rb
+++ b/spec/unit/puppet/parser/functions/get_module_path_spec.rb
@@ -15,11 +15,11 @@ describe Puppet::Parser::Functions.function(:get_module_path) do
end
it 'should only allow one argument' do
- expect { scope.function_get_module_path([]) }.should raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/)
- expect { scope.function_get_module_path(['1','2','3']) }.should raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/)
+ expect { scope.function_get_module_path([]) }.to raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/)
+ expect { scope.function_get_module_path(['1','2','3']) }.to raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/)
end
it 'should raise an exception when the module cannot be found' do
- expect { scope.function_get_module_path(['foo']) }.should raise_error(Puppet::ParseError, /Could not find module/)
+ expect { scope.function_get_module_path(['foo']) }.to raise_error(Puppet::ParseError, /Could not find module/)
end
describe 'when locating a module' do
let(:modulepath) { "/tmp/does_not_exist" }
diff --git a/spec/unit/puppet/parser/functions/is_function_available.rb b/spec/unit/puppet/parser/functions/is_function_available.rb
new file mode 100644
index 0000000..bd40c51
--- /dev/null
+++ b/spec/unit/puppet/parser/functions/is_function_available.rb
@@ -0,0 +1,31 @@
+#!/usr/bin/env rspec
+require 'spec_helper'
+
+describe "the is_function_available function" do
+ before :all do
+ Puppet::Parser::Functions.autoloader.loadall
+ end
+
+ before :each do
+ @scope = Puppet::Parser::Scope.new
+ end
+
+ it "should exist" do
+ Puppet::Parser::Functions.function("is_function_available").should == "function_is_function_available"
+ end
+
+ it "should raise a ParseError if there is less than 1 arguments" do
+ lambda { @scope.function_is_function_available([]) }.should( raise_error(Puppet::ParseError))
+ end
+
+ it "should return false if a nonexistent function is passed" do
+ result = @scope.function_is_function_available(['jeff_mccunes_left_sock'])
+ result.should(eq(false))
+ end
+
+ it "should return true if an available function is passed" do
+ result = @scope.function_is_function_available(['require'])
+ result.should(eq(true))
+ end
+
+end
diff --git a/spec/unit/puppet/parser/functions/merge_spec.rb b/spec/unit/puppet/parser/functions/merge_spec.rb
index db7d837..04169e7 100644
--- a/spec/unit/puppet/parser/functions/merge_spec.rb
+++ b/spec/unit/puppet/parser/functions/merge_spec.rb
@@ -25,7 +25,7 @@ describe Puppet::Parser::Functions.function(:merge) do
describe 'when calling merge on the scope instance' do
it 'should require all parameters are hashes' do
- expect { new_hash = scope.function_merge([{}, '2'])}.should raise_error(Puppet::ParseError, /unexpected argument type String/)
+ expect { new_hash = scope.function_merge([{}, '2'])}.to raise_error(Puppet::ParseError, /unexpected argument type String/)
end
it 'should be able to merge two hashes' do
diff --git a/spec/unit/puppet/parser/functions/str2saltedsha512_spec.rb b/spec/unit/puppet/parser/functions/str2saltedsha512_spec.rb
index a692c31..df8fb8e 100644
--- a/spec/unit/puppet/parser/functions/str2saltedsha512_spec.rb
+++ b/spec/unit/puppet/parser/functions/str2saltedsha512_spec.rb
@@ -9,11 +9,11 @@ describe "the str2saltedsha512 function" do
end
it "should raise a ParseError if there is less than 1 argument" do
- expect { scope.function_str2saltedsha512([]) }.should( raise_error(Puppet::ParseError) )
+ expect { scope.function_str2saltedsha512([]) }.to( raise_error(Puppet::ParseError) )
end
it "should raise a ParseError if there is more than 1 argument" do
- expect { scope.function_str2saltedsha512(['foo', 'bar', 'baz']) }.should( raise_error(Puppet::ParseError) )
+ expect { scope.function_str2saltedsha512(['foo', 'bar', 'baz']) }.to( raise_error(Puppet::ParseError) )
end
it "should return a salted-sha512 password hash 136 characters in length" do
@@ -22,7 +22,7 @@ describe "the str2saltedsha512 function" do
end
it "should raise an error if you pass a non-string password" do
- expect { scope.function_str2saltedsha512([1234]) }.should( raise_error(Puppet::ParseError) )
+ expect { scope.function_str2saltedsha512([1234]) }.to( raise_error(Puppet::ParseError) )
end
it "should generate a valid password" do
diff --git a/spec/unit/puppet/parser/functions/suffix_spec.rb b/spec/unit/puppet/parser/functions/suffix_spec.rb
new file mode 100644
index 0000000..c28f719
--- /dev/null
+++ b/spec/unit/puppet/parser/functions/suffix_spec.rb
@@ -0,0 +1,19 @@
+#! /usr/bin/env ruby -S rspec
+require 'spec_helper'
+
+describe "the suffix function" do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+
+ it "should exist" do
+ Puppet::Parser::Functions.function("suffix").should == "function_suffix"
+ end
+
+ it "should raise a ParseError if there is less than 1 arguments" do
+ lambda { scope.function_suffix([]) }.should( raise_error(Puppet::ParseError))
+ end
+
+ it "should return a suffixed array" do
+ result = scope.function_suffix([['a','b','c'], 'p'])
+ result.should(eq(['ap','bp','cp']))
+ end
+end
diff --git a/spec/unit/puppet/parser/functions/validate_array_spec.rb b/spec/unit/puppet/parser/functions/validate_array_spec.rb
index 8eee72a..4b31cfd 100644
--- a/spec/unit/puppet/parser/functions/validate_array_spec.rb
+++ b/spec/unit/puppet/parser/functions/validate_array_spec.rb
@@ -9,12 +9,12 @@ describe Puppet::Parser::Functions.function(:validate_array) do
%w{ true false }.each do |the_string|
it "should not compile when #{the_string} is a string" do
Puppet[:code] = "validate_array('#{the_string}')"
- expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not an Array/)
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not an Array/)
end
it "should not compile when #{the_string} is a bare word" do
Puppet[:code] = "validate_array(#{the_string})"
- expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not an Array/)
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not an Array/)
end
end
@@ -32,7 +32,7 @@ describe Puppet::Parser::Functions.function(:validate_array) do
$foo = undef
validate_array($foo)
ENDofPUPPETcode
- expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not an Array/)
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not an Array/)
end
end
end
diff --git a/spec/unit/puppet/parser/functions/validate_augeas_spec.rb b/spec/unit/puppet/parser/functions/validate_augeas_spec.rb
new file mode 100644
index 0000000..ab5c140
--- /dev/null
+++ b/spec/unit/puppet/parser/functions/validate_augeas_spec.rb
@@ -0,0 +1,102 @@
+require 'spec_helper'
+
+describe Puppet::Parser::Functions.function(:validate_augeas), :if => Puppet.features.augeas? do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+
+ # The subject of these examplres is the method itself.
+ subject do
+ # This makes sure the function is loaded within each test
+ function_name = Puppet::Parser::Functions.function(:validate_augeas)
+ scope.method(function_name)
+ end
+
+ context 'Using Puppet::Parser::Scope.new' do
+
+ describe 'Garbage inputs' do
+ inputs = [
+ [ nil ],
+ [ [ nil ] ],
+ [ { 'foo' => 'bar' } ],
+ [ { } ],
+ [ '' ],
+ [ "one", "one", "MSG to User", "4th arg" ],
+ ]
+
+ inputs.each do |input|
+ it "validate_augeas(#{input.inspect}) should fail" do
+ expect { subject.call [input] }.to raise_error Puppet::ParseError
+ end
+ end
+ end
+
+ describe 'Valid inputs' do
+ inputs = [
+ [ "root:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns' ],
+ [ "proc /proc proc nodev,noexec,nosuid 0 0\n", 'Fstab.lns'],
+ ]
+
+ inputs.each do |input|
+ it "validate_augeas(#{input.inspect}) should not fail" do
+ expect { subject.call input }.not_to raise_error
+ end
+ end
+ end
+
+ describe "Valid inputs which should raise an exception without a message" do
+ # The intent here is to make sure valid inputs raise exceptions when they
+ # don't specify an error message to display. This is the behvior in
+ # 2.2.x and prior.
+ inputs = [
+ [ "root:x:0:0:root\n", 'Passwd.lns' ],
+ [ "127.0.1.1\n", 'Hosts.lns' ],
+ ]
+
+ inputs.each do |input|
+ it "validate_augeas(#{input.inspect}) should fail" do
+ expect { subject.call input }.to raise_error /validate_augeas.*?matched less than it should/
+ end
+ end
+ end
+
+ describe "Nicer Error Messages" do
+ # The intent here is to make sure the function returns the 3rd argument
+ # in the exception thrown
+ inputs = [
+ [ "root:x:0:0:root\n", 'Passwd.lns', [], 'Failed to validate passwd content' ],
+ [ "127.0.1.1\n", 'Hosts.lns', [], 'Wrong hosts content' ],
+ ]
+
+ inputs.each do |input|
+ it "validate_augeas(#{input.inspect}) should fail" do
+ expect { subject.call input }.to raise_error /#{input[2]}/
+ end
+ end
+ end
+
+ describe "Passing simple unit tests" do
+ inputs = [
+ [ "root:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns', ['$file/foobar']],
+ [ "root:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns', ['$file/root/shell[.="/bin/sh"]', 'foobar']],
+ ]
+
+ inputs.each do |input|
+ it "validate_augeas(#{input.inspect}) should fail" do
+ expect { subject.call input }.not_to raise_error
+ end
+ end
+ end
+
+ describe "Failing simple unit tests" do
+ inputs = [
+ [ "foobar:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns', ['$file/foobar']],
+ [ "root:x:0:0:root:/root:/bin/sh\n", 'Passwd.lns', ['$file/root/shell[.="/bin/sh"]', 'foobar']],
+ ]
+
+ inputs.each do |input|
+ it "validate_augeas(#{input.inspect}) should fail" do
+ expect { subject.call input }.to raise_error /testing path/
+ end
+ end
+ end
+ end
+end
diff --git a/spec/unit/puppet/parser/functions/validate_bool_spec.rb b/spec/unit/puppet/parser/functions/validate_bool_spec.rb
index 31ab8fb..261fb23 100644
--- a/spec/unit/puppet/parser/functions/validate_bool_spec.rb
+++ b/spec/unit/puppet/parser/functions/validate_bool_spec.rb
@@ -10,7 +10,7 @@ describe Puppet::Parser::Functions.function(:validate_bool) do
it "should not compile when #{the_string} is a string" do
Puppet[:code] = "validate_bool('#{the_string}')"
- expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not a boolean/)
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a boolean/)
end
it "should compile when #{the_string} is a bare word" do
@@ -22,12 +22,12 @@ describe Puppet::Parser::Functions.function(:validate_bool) do
it "should not compile when an arbitrary string is passed" do
Puppet[:code] = 'validate_bool("jeff and dan are awesome")'
- expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not a boolean/)
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a boolean/)
end
it "should not compile when no arguments are passed" do
Puppet[:code] = 'validate_bool()'
- expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /wrong number of arguments/)
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
end
it "should compile when multiple boolean arguments are passed" do
@@ -45,7 +45,7 @@ describe Puppet::Parser::Functions.function(:validate_bool) do
$bar = false
validate_bool($foo, $bar, true, false, 'jeff')
ENDofPUPPETcode
- expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not a boolean/)
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a boolean/)
end
end
end
diff --git a/spec/unit/puppet/parser/functions/validate_cmd_spec.rb b/spec/unit/puppet/parser/functions/validate_cmd_spec.rb
new file mode 100644
index 0000000..69ea7f4
--- /dev/null
+++ b/spec/unit/puppet/parser/functions/validate_cmd_spec.rb
@@ -0,0 +1,81 @@
+require 'spec_helper'
+
+describe Puppet::Parser::Functions.function(:validate_cmd) do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+
+ # The subject of these examplres is the method itself.
+ subject do
+ # This makes sure the function is loaded within each test
+ function_name = Puppet::Parser::Functions.function(:validate_cmd)
+ scope.method(function_name)
+ end
+
+ context 'Using Puppet::Parser::Scope.new' do
+
+ describe 'Garbage inputs' do
+ inputs = [
+ [ nil ],
+ [ [ nil ] ],
+ [ { 'foo' => 'bar' } ],
+ [ { } ],
+ [ '' ],
+ [ "one", "one", "MSG to User", "4th arg" ],
+ ]
+
+ inputs.each do |input|
+ it "validate_cmd(#{input.inspect}) should fail" do
+ expect { subject.call [input] }.to raise_error Puppet::ParseError
+ end
+ end
+ end
+
+ describe 'Valid inputs' do
+ inputs = [
+ [ '/full/path/to/something', '/bin/echo' ],
+ [ '/full/path/to/something', '/bin/cat' ],
+ ]
+
+ inputs.each do |input|
+ it "validate_cmd(#{input.inspect}) should not fail" do
+ expect { subject.call input }.not_to raise_error
+ end
+ end
+ end
+
+ describe "Valid inputs which should raise an exception without a message" do
+ # The intent here is to make sure valid inputs raise exceptions when they
+ # don't specify an error message to display. This is the behvior in
+ # 2.2.x and prior.
+ inputs = [
+ [ "hello", "/bin/false" ],
+ ]
+
+ inputs.each do |input|
+ it "validate_cmd(#{input.inspect}) should fail" do
+ expect { subject.call input }.to raise_error /validate_cmd.*?failed to validate content with command/
+ end
+ end
+ end
+
+ describe "Nicer Error Messages" do
+ # The intent here is to make sure the function returns the 3rd argument
+ # in the exception thrown
+ inputs = [
+ [ "hello", [ "bye", "later", "adios" ], "MSG to User" ],
+ [ "greetings", "salutations", "Error, greetings does not match salutations" ],
+ ]
+
+ inputs.each do |input|
+ it "validate_cmd(#{input.inspect}) should fail" do
+ expect { subject.call input }.to raise_error /#{input[2]}/
+ end
+ end
+ end
+
+ describe "Test output message" do
+ it "validate_cmd('whatever', 'kthnksbye') should fail" do
+ expect { subject.call ['whatever', 'kthnksbye'] }.to raise_error /kthnksbye.* returned 1/
+ end
+ end
+ end
+end
diff --git a/spec/unit/puppet/parser/functions/validate_hash_spec.rb b/spec/unit/puppet/parser/functions/validate_hash_spec.rb
index f63db1d..a0c35c2 100644
--- a/spec/unit/puppet/parser/functions/validate_hash_spec.rb
+++ b/spec/unit/puppet/parser/functions/validate_hash_spec.rb
@@ -11,12 +11,12 @@ describe Puppet::Parser::Functions.function(:validate_hash) do
it "should not compile when #{the_string} is a string" do
Puppet[:code] = "validate_hash('#{the_string}')"
- expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not a Hash/)
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a Hash/)
end
it "should not compile when #{the_string} is a bare word" do
Puppet[:code] = "validate_hash(#{the_string})"
- expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not a Hash/)
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a Hash/)
end
end
@@ -35,10 +35,9 @@ describe Puppet::Parser::Functions.function(:validate_hash) do
$foo = undef
validate_hash($foo)
ENDofPUPPETcode
- expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not a Hash/)
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a Hash/)
end
end
end
-
diff --git a/spec/unit/puppet/parser/functions/validate_string_spec.rb b/spec/unit/puppet/parser/functions/validate_string_spec.rb
index f40bf2a..3b4fb3e 100644
--- a/spec/unit/puppet/parser/functions/validate_string_spec.rb
+++ b/spec/unit/puppet/parser/functions/validate_string_spec.rb
@@ -29,7 +29,7 @@ describe Puppet::Parser::Functions.function(:validate_string) do
it "should not compile when #{the_string} is a bare word" do
Puppet[:code] = "validate_string(#{the_string})"
- expect { scope.compiler.compile }.should raise_error(Puppet::ParseError, /is not a string/)
+ expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a string/)
end
end
@@ -58,4 +58,3 @@ describe Puppet::Parser::Functions.function(:validate_string) do
end
end
end
-