summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.markdown7
-rw-r--r--lib/puppet/parser/functions/concat.rb37
-rw-r--r--lib/puppet/parser/functions/validate_augeas.rb81
-rw-r--r--spec/unit/puppet/parser/functions/concat_spec.rb15
-rw-r--r--spec/unit/puppet/parser/functions/get_module_path_spec.rb6
-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/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_hash_spec.rb6
-rw-r--r--spec/unit/puppet/parser/functions/validate_string_spec.rb2
-rw-r--r--spec/unit/puppet/type/file_line_spec.rb6
13 files changed, 263 insertions, 21 deletions
diff --git a/README.markdown b/README.markdown
index 922383c..c58d31f 100644
--- a/README.markdown
+++ b/README.markdown
@@ -98,6 +98,13 @@ Requires a string or array of strings as input.
- *Type*: rvalue
+concat
+-----
+Appends the contents of the second array onto the first array.
+
+
+- *Type*: rvalue
+
defined_with_params
-------------------
Takes a resource reference and an optional hash of attributes.
diff --git a/lib/puppet/parser/functions/concat.rb b/lib/puppet/parser/functions/concat.rb
new file mode 100644
index 0000000..c86aa00
--- /dev/null
+++ b/lib/puppet/parser/functions/concat.rb
@@ -0,0 +1,37 @@
+#
+# concat.rb
+#
+
+module Puppet::Parser::Functions
+ newfunction(:concat, :type => :rvalue, :doc => <<-EOS
+Appends the contents of array 2 onto array 1.
+
+*Example:*
+
+ concat(['1','2','3'],['4','5','6'])
+
+Would result in:
+
+ ['1','2','3','4','5','6']
+ EOS
+ ) do |arguments|
+
+ # Check that 2 arguments have been given ...
+ raise(Puppet::ParseError, "concat(): Wrong number of arguments " +
+ "given (#{arguments.size} for 2)") if arguments.size != 2
+
+ a = arguments[0]
+ b = arguments[1]
+
+ # Check that both args are arrays.
+ unless a.is_a?(Array) and b.is_a?(Array)
+ raise(Puppet::ParseError, 'concat(): Requires array to work with')
+ end
+
+ result = a.concat(b)
+
+ return result
+ end
+end
+
+# vim: set ts=2 sw=2 et :
diff --git a/lib/puppet/parser/functions/validate_augeas.rb b/lib/puppet/parser/functions/validate_augeas.rb
new file mode 100644
index 0000000..154d660
--- /dev/null
+++ b/lib/puppet/parser/functions/validate_augeas.rb
@@ -0,0 +1,81 @@
+module Puppet::Parser::Functions
+ newfunction(:validate_augeas, :doc => <<-'ENDHEREDOC') do |args|
+ Perform validation of a string using an Augeas lens
+ The first argument of this function should be a string to
+ test, and the second argument should be the name of the Augeas lens to use.
+ If Augeas fails to parse the string with the lens, the compilation will
+ abort with a parse error.
+
+ A third argument can be specified, listing paths which should
+ not be found in the file. The `$file` variable points to the location
+ of the temporary file being tested in the Augeas tree.
+
+ For example, if you want to make sure your passwd content never contains
+ a user `foo`, you could write:
+
+ validate_augeas($passwdcontent, 'Passwd.lns', ['$file/foo'])
+
+ Or if you wanted to ensure that no users used the '/bin/barsh' shell,
+ you could use:
+
+ validate_augeas($passwdcontent, 'Passwd.lns', ['$file/*[shell="/bin/barsh"]']
+
+ If a fourth argument is specified, this will be the error message raised and
+ seen by the user.
+
+ A helpful error message can be returned like this:
+
+ validate_augeas($sudoerscontent, 'Sudoers.lns', [], 'Failed to validate sudoers content with Augeas')
+
+ ENDHEREDOC
+ unless Puppet.features.augeas?
+ raise Puppet::ParseError, ("validate_augeas(): this function requires the augeas feature. See http://projects.puppetlabs.com/projects/puppet/wiki/Puppet_Augeas#Pre-requisites for how to activate it.")
+ end
+
+ if (args.length < 2) or (args.length > 4) then
+ raise Puppet::ParseError, ("validate_augeas(): wrong number of arguments (#{args.length}; must be 2, 3, or 4)")
+ end
+
+ msg = args[3] || "validate_augeas(): Failed to validate content against #{args[1].inspect}"
+
+ require 'augeas'
+ aug = Augeas::open(nil, nil, Augeas::NO_MODL_AUTOLOAD)
+ begin
+ content = args[0]
+
+ # Test content in a temporary file
+ tmpfile = Tempfile.new("validate_augeas")
+ begin
+ tmpfile.write(content)
+ ensure
+ tmpfile.close
+ end
+
+ # Check for syntax
+ lens = args[1]
+ aug.transform(
+ :lens => lens,
+ :name => 'Validate_augeas',
+ :incl => tmpfile.path
+ )
+ aug.load!
+
+ unless aug.match("/augeas/files#{tmpfile.path}//error").empty?
+ error = aug.get("/augeas/files#{tmpfile.path}//error/message")
+ msg += " with error: #{error}"
+ raise Puppet::ParseError, (msg)
+ end
+
+ # Launch unit tests
+ tests = args[2] || []
+ aug.defvar('file', "/files#{tmpfile.path}")
+ tests.each do |t|
+ msg += " testing path #{t}"
+ raise Puppet::ParseError, (msg) unless aug.match(t).empty?
+ end
+ ensure
+ aug.close
+ tmpfile.unlink
+ end
+ end
+end
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/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/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/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_hash_spec.rb b/spec/unit/puppet/parser/functions/validate_hash_spec.rb
index 06d77a1..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,7 +35,7 @@ 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
diff --git a/spec/unit/puppet/parser/functions/validate_string_spec.rb b/spec/unit/puppet/parser/functions/validate_string_spec.rb
index 007b4ca..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
diff --git a/spec/unit/puppet/type/file_line_spec.rb b/spec/unit/puppet/type/file_line_spec.rb
index 0cd8a26..edc64bd 100644
--- a/spec/unit/puppet/type/file_line_spec.rb
+++ b/spec/unit/puppet/type/file_line_spec.rb
@@ -37,13 +37,13 @@ describe Puppet::Type.type(:file_line) do
file_line[:path].should == '/tmp/path'
end
it 'should not accept unqualified path' do
- expect { file_line[:path] = 'file' }.should raise_error(Puppet::Error, /File paths must be fully qualified/)
+ expect { file_line[:path] = 'file' }.to raise_error(Puppet::Error, /File paths must be fully qualified/)
end
it 'should require that a line is specified' do
- expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => '/tmp/file') }.should raise_error(Puppet::Error, /Both line and path are required attributes/)
+ expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => '/tmp/file') }.to raise_error(Puppet::Error, /Both line and path are required attributes/)
end
it 'should require that a file is specified' do
- expect { Puppet::Type.type(:file_line).new(:name => 'foo', :line => 'path') }.should raise_error(Puppet::Error, /Both line and path are required attributes/)
+ expect { Puppet::Type.type(:file_line).new(:name => 'foo', :line => 'path') }.to raise_error(Puppet::Error, /Both line and path are required attributes/)
end
it 'should default to ensure => present' do
file_line[:ensure].should eq :present