summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.markdown29
-rw-r--r--lib/puppet/parser/functions/delete_undef_values.rb34
-rw-r--r--lib/puppet/parser/functions/delete_values.rb26
-rw-r--r--lib/puppet/parser/functions/ensure_resource.rb2
-rw-r--r--lib/puppet/parser/functions/range.rb10
-rw-r--r--lib/puppet/parser/functions/uriescape.rb3
-rw-r--r--lib/puppet/parser/functions/validate_ipv4_address.rb48
-rw-r--r--lib/puppet/parser/functions/validate_ipv6_address.rb49
-rw-r--r--lib/puppet/parser/functions/validate_slength.rb71
-rw-r--r--lib/puppet/provider/file_line/ruby.rb5
-rw-r--r--lib/puppet/type/file_line.rb5
-rw-r--r--spec/unit/puppet/parser/functions/delete_undef_values_spec.rb29
-rw-r--r--spec/unit/puppet/parser/functions/delete_values_spec.rb30
-rw-r--r--spec/unit/puppet/parser/functions/range_spec.rb68
-rw-r--r--spec/unit/puppet/parser/functions/uriescape_spec.rb4
-rw-r--r--spec/unit/puppet/parser/functions/validate_ipv4_address_spec.rb64
-rw-r--r--spec/unit/puppet/parser/functions/validate_ipv6_address_spec.rb67
-rwxr-xr-xspec/unit/puppet/parser/functions/validate_slength_spec.rb71
-rw-r--r--spec/unit/puppet/provider/file_line/ruby_spec.rb33
19 files changed, 570 insertions, 78 deletions
diff --git a/README.markdown b/README.markdown
index 0e40f51..1e51e1d 100644
--- a/README.markdown
+++ b/README.markdown
@@ -204,6 +204,33 @@ Would return: ['a','c']
- *Type*: rvalue
+delete_values
+-------------
+Deletes all instances of a given value from a hash.
+
+*Examples:*
+
+ delete_values({'a'=>'A','b'=>'B','c'=>'C','B'=>'D'}, 'B')
+
+Would return: {'a'=>'A','c'=>'C','B'=>'D'}
+
+
+delete_undef_values
+-------------------
+Deletes all instances of the undef value from an array or hash.
+
+*Examples:*
+
+ $hash = delete_undef_values({a=>'A', b=>'', c=>undef, d => false})
+
+Would return: {a => 'A', b => '', d => false}
+
+ $array = delete_undef_values(['A','',undef,false])
+
+Would return: ['A','',false]
+
+- *Type*: rvalue
+
difference
----------
This function returns the difference between two arrays.
@@ -258,7 +285,7 @@ resource.
This example only creates the resource if it does not already exist:
- ensure_resource('user, 'dan', {'ensure' => 'present' })
+ ensure_resource('user', 'dan', {'ensure' => 'present' })
If the resource already exists but does not match the specified parameters,
this function will attempt to recreate the resource leading to a duplicate
diff --git a/lib/puppet/parser/functions/delete_undef_values.rb b/lib/puppet/parser/functions/delete_undef_values.rb
new file mode 100644
index 0000000..4eecab2
--- /dev/null
+++ b/lib/puppet/parser/functions/delete_undef_values.rb
@@ -0,0 +1,34 @@
+module Puppet::Parser::Functions
+ newfunction(:delete_undef_values, :type => :rvalue, :doc => <<-EOS
+Returns a copy of input hash or array with all undefs deleted.
+
+*Examples:*
+
+ $hash = delete_undef_values({a=>'A', b=>'', c=>undef, d => false})
+
+Would return: {a => 'A', b => '', d => false}
+
+ $array = delete_undef_values(['A','',undef,false])
+
+Would return: ['A','',false]
+
+ EOS
+ ) do |args|
+
+ raise(Puppet::ParseError,
+ "delete_undef_values(): Wrong number of arguments given " +
+ "(#{args.size})") if args.size < 1
+
+ result = args[0]
+ if result.is_a?(Hash)
+ result.delete_if {|key, val| val.equal? :undef}
+ elsif result.is_a?(Array)
+ result.delete :undef
+ else
+ raise(Puppet::ParseError,
+ "delete_undef_values(): Wrong argument type #{args[0].class} " +
+ "for first argument")
+ end
+ result
+ end
+end
diff --git a/lib/puppet/parser/functions/delete_values.rb b/lib/puppet/parser/functions/delete_values.rb
new file mode 100644
index 0000000..17b9d37
--- /dev/null
+++ b/lib/puppet/parser/functions/delete_values.rb
@@ -0,0 +1,26 @@
+module Puppet::Parser::Functions
+ newfunction(:delete_values, :type => :rvalue, :doc => <<-EOS
+Deletes all instances of a given value from a hash.
+
+*Examples:*
+
+ delete_values({'a'=>'A','b'=>'B','c'=>'C','B'=>'D'}, 'B')
+
+Would return: {'a'=>'A','c'=>'C','B'=>'D'}
+
+ EOS
+ ) do |arguments|
+
+ raise(Puppet::ParseError,
+ "delete_values(): Wrong number of arguments given " +
+ "(#{arguments.size} of 2)") if arguments.size != 2
+
+ hash, item = arguments
+
+ if not hash.is_a?(Hash)
+ raise(TypeError, "delete_values(): First argument must be a Hash. " + \
+ "Given an argument of class #{hash.class}.")
+ end
+ hash.delete_if { |key, val| item == val }
+ end
+end
diff --git a/lib/puppet/parser/functions/ensure_resource.rb b/lib/puppet/parser/functions/ensure_resource.rb
index a9a1733..05e5593 100644
--- a/lib/puppet/parser/functions/ensure_resource.rb
+++ b/lib/puppet/parser/functions/ensure_resource.rb
@@ -13,7 +13,7 @@ resource.
This example only creates the resource if it does not already exist:
- ensure_resource('user, 'dan', {'ensure' => 'present' })
+ ensure_resource('user', 'dan', {'ensure' => 'present' })
If the resource already exists but does not match the specified parameters,
this function will attempt to recreate the resource leading to a duplicate
diff --git a/lib/puppet/parser/functions/range.rb b/lib/puppet/parser/functions/range.rb
index 825617b..0849491 100644
--- a/lib/puppet/parser/functions/range.rb
+++ b/lib/puppet/parser/functions/range.rb
@@ -27,6 +27,13 @@ Will return: ["a","b","c"]
range("host01", "host10")
Will return: ["host01", "host02", ..., "host09", "host10"]
+
+Passing a third argument will cause the generated range to step by that
+interval, e.g.
+
+ range("0", "9", "2")
+
+Will return: [0,2,4,6,8]
EOS
) do |arguments|
@@ -37,6 +44,7 @@ Will return: ["host01", "host02", ..., "host09", "host10"]
if arguments.size > 1
start = arguments[0]
stop = arguments[1]
+ step = arguments[2].nil? ? 1 : arguments[2].to_i.abs
type = '..' # We select simplest type for Range available in Ruby ...
@@ -71,7 +79,7 @@ Will return: ["host01", "host02", ..., "host09", "host10"]
when /^(\.\.\.)$/ then (start ... stop) # Exclusive of last element ...
end
- result = range.collect { |i| i } # Get them all ... Pokemon ...
+ result = range.step(step).collect { |i| i } # Get them all ... Pokemon ...
return result
end
diff --git a/lib/puppet/parser/functions/uriescape.rb b/lib/puppet/parser/functions/uriescape.rb
index 67b93a6..0d81de5 100644
--- a/lib/puppet/parser/functions/uriescape.rb
+++ b/lib/puppet/parser/functions/uriescape.rb
@@ -15,7 +15,6 @@ module Puppet::Parser::Functions
value = arguments[0]
klass = value.class
- unsafe = ":/?#[]@!$&'()*+,;= "
unless [Array, String].include?(klass)
raise(Puppet::ParseError, 'uriescape(): Requires either ' +
@@ -26,7 +25,7 @@ module Puppet::Parser::Functions
# Numbers in Puppet are often string-encoded which is troublesome ...
result = value.collect { |i| i.is_a?(String) ? URI.escape(i,unsafe) : i }
else
- result = URI.escape(value,unsafe)
+ result = URI.escape(value)
end
return result
diff --git a/lib/puppet/parser/functions/validate_ipv4_address.rb b/lib/puppet/parser/functions/validate_ipv4_address.rb
new file mode 100644
index 0000000..fc02748
--- /dev/null
+++ b/lib/puppet/parser/functions/validate_ipv4_address.rb
@@ -0,0 +1,48 @@
+module Puppet::Parser::Functions
+
+ newfunction(:validate_ipv4_address, :doc => <<-ENDHEREDOC
+ Validate that all values passed are valid IPv4 addresses.
+ Fail compilation if any value fails this check.
+
+ The following values will pass:
+
+ $my_ip = "1.2.3.4"
+ validate_ipv4_address($my_ip)
+ validate_bool("8.8.8.8", "172.16.0.1", $my_ip)
+
+ The following values will fail, causing compilation to abort:
+
+ $some_array = [ 1, true, false, "garbage string", "3ffe:505:2" ]
+ validate_ipv4_address($some_array)
+
+ ENDHEREDOC
+ ) do |args|
+
+ require "ipaddr"
+ rescuable_exceptions = [ ArgumentError ]
+
+ if defined?(IPAddr::InvalidAddressError)
+ rescuable_exceptions << IPAddr::InvalidAddressError
+ end
+
+ unless args.length > 0 then
+ raise Puppet::ParseError, ("validate_ipv4_address(): wrong number of arguments (#{args.length}; must be > 0)")
+ end
+
+ args.each do |arg|
+ unless arg.is_a?(String)
+ raise Puppet::ParseError, "#{arg.inspect} is not a string."
+ end
+
+ begin
+ unless IPAddr.new(arg).ipv4?
+ raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address."
+ end
+ rescue *rescuable_exceptions
+ raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv4 address."
+ end
+ end
+
+ end
+
+end
diff --git a/lib/puppet/parser/functions/validate_ipv6_address.rb b/lib/puppet/parser/functions/validate_ipv6_address.rb
new file mode 100644
index 0000000..b0f2558
--- /dev/null
+++ b/lib/puppet/parser/functions/validate_ipv6_address.rb
@@ -0,0 +1,49 @@
+module Puppet::Parser::Functions
+
+ newfunction(:validate_ipv6_address, :doc => <<-ENDHEREDOC
+ Validate that all values passed are valid IPv6 addresses.
+ Fail compilation if any value fails this check.
+
+ The following values will pass:
+
+ $my_ip = "3ffe:505:2"
+ validate_ipv6_address(1)
+ validate_ipv6_address($my_ip)
+ validate_bool("fe80::baf6:b1ff:fe19:7507", $my_ip)
+
+ The following values will fail, causing compilation to abort:
+
+ $some_array = [ true, false, "garbage string", "1.2.3.4" ]
+ validate_ipv6_address($some_array)
+
+ ENDHEREDOC
+ ) do |args|
+
+ require "ipaddr"
+ rescuable_exceptions = [ ArgumentError ]
+
+ if defined?(IPAddr::InvalidAddressError)
+ rescuable_exceptions << IPAddr::InvalidAddressError
+ end
+
+ unless args.length > 0 then
+ raise Puppet::ParseError, ("validate_ipv6_address(): wrong number of arguments (#{args.length}; must be > 0)")
+ end
+
+ args.each do |arg|
+ unless arg.is_a?(String)
+ raise Puppet::ParseError, "#{arg.inspect} is not a string."
+ end
+
+ begin
+ unless IPAddr.new(arg).ipv6?
+ raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv6 address."
+ end
+ rescue *rescuable_exceptions
+ raise Puppet::ParseError, "#{arg.inspect} is not a valid IPv6 address."
+ end
+ end
+
+ end
+
+end
diff --git a/lib/puppet/parser/functions/validate_slength.rb b/lib/puppet/parser/functions/validate_slength.rb
index fdcc0a2..7d534f3 100644
--- a/lib/puppet/parser/functions/validate_slength.rb
+++ b/lib/puppet/parser/functions/validate_slength.rb
@@ -2,51 +2,70 @@ module Puppet::Parser::Functions
newfunction(:validate_slength, :doc => <<-'ENDHEREDOC') do |args|
Validate that the first argument is a string (or an array of strings), and
- less/equal to than the length of the second argument. It fails if the first
- argument is not a string or array of strings, and if arg 2 is not convertable
- to a number.
+ less/equal to than the length of the second argument. An optional third
+ parameter can be given a the minimum length. It fails if the first
+ argument is not a string or array of strings, and if arg 2 and arg 3 are
+ not convertable to a number.
The following values will pass:
validate_slength("discombobulate",17)
validate_slength(["discombobulate","moo"],17)
+ validate_slength(["discombobulate","moo"],17,3)
The following valueis will not:
validate_slength("discombobulate",1)
validate_slength(["discombobulate","thermometer"],5)
+ validate_slength(["discombobulate","moo"],17,10)
ENDHEREDOC
- raise Puppet::ParseError, ("validate_slength(): Wrong number of arguments (#{args.length}; must be = 2)") unless args.length == 2
+ raise Puppet::ParseError, "validate_slength(): Wrong number of arguments (#{args.length}; must be 2 or 3)" unless args.length == 2 or args.length == 3
- unless (args[0].is_a?(String) or args[0].is_a?(Array))
- raise Puppet::ParseError, ("validate_slength(): please pass a string, or an array of strings - what you passed didn't work for me at all - #{args[0].class}")
- end
+ input, max_length, min_length = *args
begin
- max_length = args[1].to_i
- rescue NoMethodError => e
- raise Puppet::ParseError, ("validate_slength(): Couldn't convert whatever you passed as the length parameter to an integer - sorry: " + e.message )
+ max_length = Integer(max_length)
+ raise ArgumentError if max_length <= 0
+ rescue ArgumentError, TypeError
+ raise Puppet::ParseError, "validate_slength(): Expected second argument to be a positive Numeric, got #{max_length}:#{max_length.class}"
+ end
+
+ if min_length
+ begin
+ min_length = Integer(min_length)
+ raise ArgumentError if min_length < 0
+ rescue ArgumentError, TypeError
+ raise Puppet::ParseError, "validate_slength(): Expected third argument to be unset or a positive Numeric, got #{min_length}:#{min_length.class}"
+ end
+ else
+ min_length = 0
+ end
+
+ if min_length > max_length
+ raise Puppet::ParseError, "validate_slength(): Expected second argument to be larger than third argument"
+ end
+
+ validator = lambda do |str|
+ unless str.length <= max_length and str.length >= min_length
+ raise Puppet::ParseError, "validate_slength(): Expected length of #{input.inspect} to be between #{min_length} and #{max_length}, was #{input.length}"
+ end
end
- raise Puppet::ParseError, ("validate_slength(): please pass a positive number as max_length") unless max_length > 0
-
- case args[0]
- when String
- raise Puppet::ParseError, ("validate_slength(): #{args[0].inspect} is #{args[0].length} characters. It should have been less than or equal to #{max_length} characters") unless args[0].length <= max_length
- when Array
- args[0].each do |arg|
- if arg.is_a?(String)
- unless ( arg.is_a?(String) and arg.length <= max_length )
- raise Puppet::ParseError, ("validate_slength(): #{arg.inspect} is #{arg.length} characters. It should have been less than or equal to #{max_length} characters")
- end
- else
- raise Puppet::ParseError, ("validate_slength(): #{arg.inspect} is not a string, it's a #{arg.class}")
- end
+ case input
+ when String
+ validator.call(input)
+ when Array
+ input.each_with_index do |arg, pos|
+ if arg.is_a? String
+ validator.call(arg)
+ else
+ raise Puppet::ParseError, "validate_slength(): Expected element at array position #{pos} to be a String, got #{arg.class}"
end
- else
- raise Puppet::ParseError, ("validate_slength(): please pass a string, or an array of strings - what you passed didn't work for me at all - #{args[0].class}")
+ end
+ else
+ raise Puppet::ParseError, "validate_slength(): Expected first argument to be a String or Array, got #{input.class}"
end
end
end
diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb
index a3219d3..3cb9f6e 100644
--- a/lib/puppet/provider/file_line/ruby.rb
+++ b/lib/puppet/provider/file_line/ruby.rb
@@ -1,4 +1,3 @@
-
Puppet::Type.type(:file_line).provide(:ruby) do
def exists?
@@ -35,8 +34,8 @@ Puppet::Type.type(:file_line).provide(:ruby) do
def handle_create_with_match()
regex = resource[:match] ? Regexp.new(resource[:match]) : nil
match_count = lines.select { |l| regex.match(l) }.size
- if match_count > 1
- raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'"
+ if match_count > 1 && resource[:multiple].to_s != 'true'
+ raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'"
end
File.open(resource[:path], 'w') do |fh|
lines.each do |l|
diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb
index f71a4bc..14946bb 100644
--- a/lib/puppet/type/file_line.rb
+++ b/lib/puppet/type/file_line.rb
@@ -37,6 +37,11 @@ Puppet::Type.newtype(:file_line) do
'if a match is found, we replace that line rather than adding a new line.'
end
+ newparam(:multiple) do
+ desc 'An optional value to determine if match can change multiple lines.'
+ newvalues(true, false)
+ end
+
newparam(:line) do
desc 'The line to be appended to the file located by the path parameter.'
end
diff --git a/spec/unit/puppet/parser/functions/delete_undef_values_spec.rb b/spec/unit/puppet/parser/functions/delete_undef_values_spec.rb
new file mode 100644
index 0000000..0536641
--- /dev/null
+++ b/spec/unit/puppet/parser/functions/delete_undef_values_spec.rb
@@ -0,0 +1,29 @@
+#! /usr/bin/env ruby -S rspec
+require 'spec_helper'
+
+describe "the delete_undef_values function" do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+
+ it "should exist" do
+ Puppet::Parser::Functions.function("delete_undef_values").should == "function_delete_undef_values"
+ end
+
+ it "should raise a ParseError if there is less than 1 argument" do
+ lambda { scope.function_delete_undef_values([]) }.should( raise_error(Puppet::ParseError))
+ end
+
+ it "should raise a ParseError if the argument is not Array nor Hash" do
+ lambda { scope.function_delete_undef_values(['']) }.should( raise_error(Puppet::ParseError))
+ lambda { scope.function_delete_undef_values([nil]) }.should( raise_error(Puppet::ParseError))
+ end
+
+ it "should delete all undef items from Array and only these" do
+ result = scope.function_delete_undef_values([['a',:undef,'c','undef']])
+ result.should(eq(['a','c','undef']))
+ end
+
+ it "should delete all undef items from Hash and only these" do
+ result = scope.function_delete_undef_values([{'a'=>'A','b'=>:undef,'c'=>'C','d'=>'undef'}])
+ result.should(eq({'a'=>'A','c'=>'C','d'=>'undef'}))
+ end
+end
diff --git a/spec/unit/puppet/parser/functions/delete_values_spec.rb b/spec/unit/puppet/parser/functions/delete_values_spec.rb
new file mode 100644
index 0000000..e15c366
--- /dev/null
+++ b/spec/unit/puppet/parser/functions/delete_values_spec.rb
@@ -0,0 +1,30 @@
+#! /usr/bin/env ruby -S rspec
+require 'spec_helper'
+
+describe "the delete_values function" do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+
+ it "should exist" do
+ Puppet::Parser::Functions.function("delete_values").should == "function_delete_values"
+ end
+
+ it "should raise a ParseError if there are fewer than 2 arguments" do
+ lambda { scope.function_delete_values([]) }.should( raise_error(Puppet::ParseError))
+ end
+
+ it "should raise a ParseError if there are greater than 2 arguments" do
+ lambda { scope.function_delete([[], 'foo', 'bar']) }.should( raise_error(Puppet::ParseError))
+ end
+
+ it "should raise a TypeError if the argument is not a hash" do
+ lambda { scope.function_delete_values([1,'bar']) }.should( raise_error(TypeError))
+ lambda { scope.function_delete_values(['foo','bar']) }.should( raise_error(TypeError))
+ lambda { scope.function_delete_values([[],'bar']) }.should( raise_error(TypeError))
+ end
+
+ it "should delete all instances of a value from a hash" do
+ result = scope.function_delete_values([{ 'a'=>'A', 'b'=>'B', 'B'=>'C', 'd'=>'B' },'B'])
+ result.should(eq({ 'a'=>'A', 'B'=>'C' }))
+ end
+
+end
diff --git a/spec/unit/puppet/parser/functions/range_spec.rb b/spec/unit/puppet/parser/functions/range_spec.rb
index 42751f4..0e1ad37 100644
--- a/spec/unit/puppet/parser/functions/range_spec.rb
+++ b/spec/unit/puppet/parser/functions/range_spec.rb
@@ -4,31 +4,67 @@ require 'spec_helper'
describe "the range function" do
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
- it "should exist" do
+ it "exists" do
Puppet::Parser::Functions.function("range").should == "function_range"
end
- it "should raise a ParseError if there is less than 1 arguments" do
- lambda { scope.function_range([]) }.should( raise_error(Puppet::ParseError))
+ it "raises a ParseError if there is less than 1 arguments" do
+ expect { scope.function_range([]) }.to raise_error Puppet::ParseError, /Wrong number of arguments.*0 for 1/
end
- it "should return a letter range" do
- result = scope.function_range(["a","d"])
- result.should(eq(['a','b','c','d']))
- end
+ describe 'with a letter range' do
+ it "returns a letter range" do
+ result = scope.function_range(["a","d"])
+ result.should eq ['a','b','c','d']
+ end
+
+ it "returns a letter range given a step of 1" do
+ result = scope.function_range(["a","d","1"])
+ result.should eq ['a','b','c','d']
+ end
- it "should return a number range" do
- result = scope.function_range(["1","4"])
- result.should(eq([1,2,3,4]))
+ it "returns a stepped letter range" do
+ result = scope.function_range(["a","d","2"])
+ result.should eq ['a','c']
+ end
+
+ it "returns a stepped letter range given a negative step" do
+ result = scope.function_range(["a","d","-2"])
+ result.should eq ['a','c']
+ end
end
- it "should work with padded hostname like strings" do
- expected = ("host01".."host10").to_a
- scope.function_range(["host01","host10"]).should eq expected
+ describe 'with a number range' do
+ it "returns a number range" do
+ result = scope.function_range(["1","4"])
+ result.should eq [1,2,3,4]
+ end
+
+ it "returns a number range given a step of 1" do
+ result = scope.function_range(["1","4","1"])
+ result.should eq [1,2,3,4]
+ end
+
+ it "returns a stepped number range" do
+ result = scope.function_range(["1","4","2"])
+ result.should eq [1,3]
+ end
+
+ it "returns a stepped number range given a negative step" do
+ result = scope.function_range(["1","4","-2"])
+ result.should eq [1,3]
+ end
end
- it "should coerce zero padded digits to integers" do
- expected = (0..10).to_a
- scope.function_range(["00", "10"]).should eq expected
+ describe 'with a numeric-like string range' do
+ it "works with padded hostname like strings" do
+ expected = ("host01".."host10").to_a
+ scope.function_range(["host01","host10"]).should eq expected
+ end
+
+ it "coerces zero padded digits to integers" do
+ expected = (0..10).to_a
+ scope.function_range(["00", "10"]).should eq expected
+ end
end
end
diff --git a/spec/unit/puppet/parser/functions/uriescape_spec.rb b/spec/unit/puppet/parser/functions/uriescape_spec.rb
index 371de46..7211c88 100644
--- a/spec/unit/puppet/parser/functions/uriescape_spec.rb
+++ b/spec/unit/puppet/parser/functions/uriescape_spec.rb
@@ -13,8 +13,8 @@ describe "the uriescape function" do
end
it "should uriescape a string" do
- result = scope.function_uriescape([":/?#[]@!$&'()*+,;= "])
- result.should(eq('%3A%2F%3F%23%5B%5D%40%21%24%26%27%28%29%2A%2B%2C%3B%3D%20'))
+ result = scope.function_uriescape([":/?#[]@!$&'()*+,;= \"{}"])
+ result.should(eq(':/?%23[]@!$&\'()*+,;=%20%22%7B%7D'))
end
it "should do nothing if a string is already safe" do
diff --git a/spec/unit/puppet/parser/functions/validate_ipv4_address_spec.rb b/spec/unit/puppet/parser/functions/validate_ipv4_address_spec.rb
new file mode 100644
index 0000000..85536d3
--- /dev/null
+++ b/spec/unit/puppet/parser/functions/validate_ipv4_address_spec.rb
@@ -0,0 +1,64 @@
+#! /usr/bin/env/ruby -S rspec
+
+require "spec_helper"
+
+describe Puppet::Parser::Functions.function(:validate_ipv4_address) do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+
+ describe "when calling validate_ipv4_address from puppet" do
+ describe "when given IPv4 address strings" do
+ it "should compile with one argument" do
+ Puppet[:code] = "validate_ipv4_address('1.2.3.4')"
+ scope.compiler.compile
+ end
+
+ it "should compile with multiple arguments" do
+ Puppet[:code] = "validate_ipv4_address('1.2.3.4', '5.6.7.8')"
+ scope.compiler.compile
+ end
+ end
+
+ describe "when given an IPv6 address" do
+ it "should not compile" do
+ Puppet[:code] = "validate_ipv4_address('3ffe:505')"
+ expect {
+ scope.compiler.compile
+ }.to raise_error(Puppet::ParseError, /not a valid IPv4 address/)
+ end
+ end
+
+ describe "when given other strings" do
+ it "should not compile" do
+ Puppet[:code] = "validate_ipv4_address('hello', 'world')"
+ expect {
+ scope.compiler.compile
+ }.to raise_error(Puppet::ParseError, /not a valid IPv4 address/)
+ end
+ end
+
+ describe "when given numbers" do
+ it "should not compile" do
+ Puppet[:code] = "validate_ipv4_address(1, 2)"
+ expect {
+ scope.compiler.compile
+ }.to raise_error(Puppet::ParseError, /is not a valid IPv4 address/)
+ end
+ end
+
+ describe "when given booleans" do
+ it "should not compile" do
+ Puppet[:code] = "validate_ipv4_address(true, false)"
+ expect {
+ scope.compiler.compile
+ }.to raise_error(Puppet::ParseError, /is not a string/)
+ end
+ end
+
+ it "should not compile when no arguments are passed" do
+ Puppet[:code] = "validate_ipv4_address()"
+ expect {
+ scope.compiler.compile
+ }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
+ end
+ end
+end
diff --git a/spec/unit/puppet/parser/functions/validate_ipv6_address_spec.rb b/spec/unit/puppet/parser/functions/validate_ipv6_address_spec.rb
new file mode 100644
index 0000000..1fe5304
--- /dev/null
+++ b/spec/unit/puppet/parser/functions/validate_ipv6_address_spec.rb
@@ -0,0 +1,67 @@
+#! /usr/bin/env/ruby -S rspec
+
+require "spec_helper"
+
+describe Puppet::Parser::Functions.function(:validate_ipv6_address) do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+
+ describe "when calling validate_ipv6_address from puppet" do
+ describe "when given IPv6 address strings" do
+ it "should compile with one argument" do
+ Puppet[:code] = "validate_ipv6_address('3ffe:0505:0002::')"
+ scope.compiler.compile
+ end
+
+ it "should compile with multiple arguments" do
+ Puppet[:code] = "validate_ipv6_address('3ffe:0505:0002::', '3ffe:0505:0001::')"
+ scope.compiler.compile
+ end
+ end
+
+ describe "when given an ipv4 address" do
+ it "should not compile" do
+ Puppet[:code] = "validate_ipv6_address('1.2.3.4')"
+ expect {
+ scope.compiler.compile
+ }.to raise_error(Puppet::ParseError, /not a valid IPv6 address/)
+ end
+ end
+
+ describe "when given other strings" do
+ it "should not compile" do
+ Puppet[:code] = "validate_ipv6_address('hello', 'world')"
+ expect {
+ scope.compiler.compile
+ }.to raise_error(Puppet::ParseError, /not a valid IPv6 address/)
+ end
+ end
+
+ # 1.8.7 is EOL'd and also absolutely insane about ipv6
+ unless RUBY_VERSION == '1.8.7'
+ describe "when given numbers" do
+ it "should not compile" do
+ Puppet[:code] = "validate_ipv6_address(1, 2)"
+ expect {
+ scope.compiler.compile
+ }.to raise_error(Puppet::ParseError, /not a valid IPv6 address/)
+ end
+ end
+ end
+
+ describe "when given booleans" do
+ it "should not compile" do
+ Puppet[:code] = "validate_ipv6_address(true, false)"
+ expect {
+ scope.compiler.compile
+ }.to raise_error(Puppet::ParseError, /is not a string/)
+ end
+ end
+
+ it "should not compile when no arguments are passed" do
+ Puppet[:code] = "validate_ipv6_address()"
+ expect {
+ scope.compiler.compile
+ }.to raise_error(Puppet::ParseError, /wrong number of arguments/)
+ end
+ end
+end
diff --git a/spec/unit/puppet/parser/functions/validate_slength_spec.rb b/spec/unit/puppet/parser/functions/validate_slength_spec.rb
index b363e7a..851835f 100755
--- a/spec/unit/puppet/parser/functions/validate_slength_spec.rb
+++ b/spec/unit/puppet/parser/functions/validate_slength_spec.rb
@@ -9,40 +9,59 @@ describe "the validate_slength function" do
Puppet::Parser::Functions.function("validate_slength").should == "function_validate_slength"
end
- it "should raise a ParseError if there is less than 2 arguments" do
- expect { scope.function_validate_slength([]) }.to(raise_error(Puppet::ParseError))
- expect { scope.function_validate_slength(["asdf"]) }.to(raise_error(Puppet::ParseError))
- end
+ describe "validating the input argument types" do
+ it "raises an error if there are less than two arguments" do
+ expect { scope.function_validate_slength([]) }.to raise_error Puppet::ParseError, /Wrong number of arguments/
+ end
- it "should raise a ParseError if argument 2 doesn't convert to a fixnum" do
- expect { scope.function_validate_slength(["moo",["2"]]) }.to(raise_error(Puppet::ParseError, /Couldn't convert whatever you passed/))
- end
+ it "raises an error if there are more than three arguments" do
+ expect { scope.function_validate_slength(['input', 1, 2, 3]) }.to raise_error Puppet::ParseError, /Wrong number of arguments/
+ end
- it "should raise a ParseError if argument 2 converted, but to 0, e.g. a string" do
- expect { scope.function_validate_slength(["moo","monkey"]) }.to(raise_error(Puppet::ParseError, /please pass a positive number as max_length/))
- end
+ it "raises an error if the first argument is not a string" do
+ expect { scope.function_validate_slength([Object.new, 2, 1]) }.to raise_error Puppet::ParseError, /Expected first argument.*got .*Object/
+ end
- it "should raise a ParseError if argument 2 converted, but to 0" do
- expect { scope.function_validate_slength(["moo","0"]) }.to(raise_error(Puppet::ParseError, /please pass a positive number as max_length/))
- end
+ it "raises an error if the second argument cannot be cast to an Integer" do
+ expect { scope.function_validate_slength(['input', Object.new]) }.to raise_error Puppet::ParseError, /Expected second argument.*got .*Object/
+ end
- it "should fail if string greater then size" do
- expect { scope.function_validate_slength(["test", 2]) }.to(raise_error(Puppet::ParseError, /It should have been less than or equal to/))
- end
+ it "raises an error if the third argument cannot be cast to an Integer" do
+ expect { scope.function_validate_slength(['input', 1, Object.new]) }.to raise_error Puppet::ParseError, /Expected third argument.*got .*Object/
+ end
- it "should fail if you pass an array of something other than strings" do
- expect { scope.function_validate_slength([["moo",["moo"],Hash.new["moo" => 7]], 7]) }.to(raise_error(Puppet::ParseError, /is not a string, it's a/))
+ it "raises an error if the second argument is smaller than the third argument" do
+ expect { scope.function_validate_slength(['input', 1, 2]) }.to raise_error Puppet::ParseError, /Expected second argument to be larger than third argument/
+ end
end
- it "should fail if you pass something other than a string or array" do
- expect { scope.function_validate_slength([Hash.new["moo" => "7"],6]) }.to(raise_error(Puppet::ParseError, /please pass a string, or an array of strings/))
- end
+ describe "validating the input string length" do
+ describe "when the input is a string" do
+ it "fails validation if the string is larger than the max length" do
+ expect { scope.function_validate_slength(['input', 1]) }.to raise_error Puppet::ParseError, /Expected length .* between 0 and 1, was 5/
+ end
- it "should not fail if string is smaller or equal to size" do
- expect { scope.function_validate_slength(["test", 5]) }.to_not(raise_error(Puppet::ParseError))
- end
+ it "fails validation if the string is less than the min length" do
+ expect { scope.function_validate_slength(['input', 10, 6]) }.to raise_error Puppet::ParseError, /Expected length .* between 6 and 10, was 5/
+ end
+
+ it "doesn't raise an error if the string is under the max length" do
+ scope.function_validate_slength(['input', 10])
+ end
+
+ it "doesn't raise an error if the string is equal to the max length" do
+ scope.function_validate_slength(['input', 5])
+ end
+
+ it "doesn't raise an error if the string is equal to the min length" do
+ scope.function_validate_slength(['input', 10, 5])
+ end
+ end
- it "should not fail if array of string is are all smaller or equal to size" do
- expect { scope.function_validate_slength([["moo","foo","bar"], 5]) }.to_not(raise_error(Puppet::ParseError))
+ describe "when the input is an array" do
+ it "fails validation if one of the array elements is not a string" do
+ expect { scope.function_validate_slength([["a", "b", Object.new], 2]) }.to raise_error Puppet::ParseError, /Expected element at array position 2 .*String, got .*Object/
+ end
+ end
end
end
diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb
index 7857d39..648c05b 100644
--- a/spec/unit/puppet/provider/file_line/ruby_spec.rb
+++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb
@@ -61,6 +61,39 @@ describe provider_class do
File.read(@tmpfile).should eql("foo1\nfoo=blah\nfoo2\nfoo=baz")
end
+ it 'should replace all lines that matches' do
+ @resource = Puppet::Type::File_line.new(
+ {
+ :name => 'foo',
+ :path => @tmpfile,
+ :line => 'foo = bar',
+ :match => '^foo\s*=.*$',
+ :multiple => true
+ }
+ )
+ @provider = provider_class.new(@resource)
+ File.open(@tmpfile, 'w') do |fh|
+ fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz")
+ end
+ @provider.exists?.should be_nil
+ @provider.create
+ File.read(@tmpfile).chomp.should eql("foo1\nfoo = bar\nfoo2\nfoo = bar")
+ end
+
+ it 'should raise an error with invalid values' do
+ expect {
+ @resource = Puppet::Type::File_line.new(
+ {
+ :name => 'foo',
+ :path => @tmpfile,
+ :line => 'foo = bar',
+ :match => '^foo\s*=.*$',
+ :multiple => 'asgadga'
+ }
+ )
+ }.to raise_error(Puppet::Error, /Invalid value "asgadga"\. Valid values are true, false\./)
+ end
+
it 'should replace a line that matches' do
File.open(@tmpfile, 'w') do |fh|
fh.write("foo1\nfoo=blah\nfoo2")