summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.markdown99
-rw-r--r--lib/facter/root_home.rb13
-rw-r--r--lib/puppet/functions/is_a.rb32
-rw-r--r--lib/puppet/parser/functions/convert_base.rb35
-rw-r--r--lib/puppet/parser/functions/intersection.rb6
-rw-r--r--lib/puppet/parser/functions/parsejson.rb25
-rw-r--r--lib/puppet/parser/functions/parseyaml.rb22
-rw-r--r--lib/puppet/parser/functions/str2bool.rb8
-rw-r--r--lib/puppet/parser/functions/try_get_value.rb77
-rw-r--r--lib/puppet/parser/functions/union.rb17
-rw-r--r--lib/puppet/parser/functions/validate_re.rb11
-rw-r--r--spec/acceptance/is_a_spec.rb30
-rwxr-xr-xspec/acceptance/parsejson_spec.rb23
-rwxr-xr-xspec/acceptance/parseyaml_spec.rb25
-rwxr-xr-xspec/acceptance/try_get_value_spec.rb47
-rwxr-xr-xspec/acceptance/union_spec.rb5
-rw-r--r--spec/fixtures/lsuser/root2
-rwxr-xr-xspec/functions/base64_spec.rb1
-rw-r--r--spec/functions/convert_base_spec.rb24
-rw-r--r--spec/functions/is_a_spec.rb25
-rwxr-xr-xspec/functions/parsejson_spec.rb65
-rwxr-xr-xspec/functions/parseyaml_spec.rb83
-rwxr-xr-xspec/functions/str2bool_spec.rb4
-rw-r--r--spec/functions/try_get_value_spec.rb100
-rw-r--r--spec/functions/type_of_spec.rb4
-rwxr-xr-xspec/functions/union_spec.rb9
-rwxr-xr-xspec/functions/validate_re_spec.rb15
-rwxr-xr-xspec/spec_helper_acceptance.rb6
-rwxr-xr-xspec/unit/facter/root_home_spec.rb13
29 files changed, 758 insertions, 68 deletions
diff --git a/README.markdown b/README.markdown
index f949dca..27dd0a7 100644
--- a/README.markdown
+++ b/README.markdown
@@ -155,6 +155,12 @@ Appends the contents of multiple arrays onto the first array given. For example:
* `concat(['1','2','3'],'4',['5','6','7'])` returns ['1','2','3','4','5','6','7'].
*Type*: rvalue.
+#### `convert_base`
+
+Converts a given integer or base 10 string representing an integer to a specified base, as a string. For example:
+ * `convert_base(5, 2)` results in: '101'
+ * `convert_base('254', '16')` results in: 'fe'
+
#### `count`
If called with only an array, it counts the number of elements that are **not** nil/undef. If called with a second argument, counts the number of elements in an array that matches the second argument. *Type*: rvalue.
@@ -218,7 +224,7 @@ Converts the case of a string or of all strings in an array to lowercase. *Type*
#### `empty`
-Returns 'true' if the variable is empty. *Type*: rvalue.
+Returns true if the argument is an array or hash that contains no elements, or an empty string. *Type*: rvalue.
#### `ensure_packages`
@@ -397,6 +403,29 @@ Converts an array into a hash. For example, `hash(['a',1,'b',2,'c',3])` returns
Returns an array an intersection of two. For example, `intersection(["a","b","c"],["b","c","d"])` returns ["b","c"]. *Type*: rvalue.
+#### `is_a`
+
+Boolean check to determine whether a variable is of a given data type. This is equivalent to the `=~` type checks.
+
+ ~~~
+ foo = 3
+ $bar = [1,2,3]
+ $baz = 'A string!'
+
+ if $foo.is_a(Integer) {
+ notify { 'foo!': }
+ }
+ if $bar.is_a(Array) {
+ notify { 'bar!': }
+ }
+ if $baz.is_a(String) {
+ notify { 'baz!': }
+ }
+ ~~~
+
+See the documentation for "The Puppet Type System" for more information about types.
+See the `assert_type()` function for flexible ways to assert the type of a value.
+
#### `is_array`
Returns 'true' if the variable passed to this function is an array. *Type*: rvalue.
@@ -515,10 +544,12 @@ Converts a number or a string representation of a number into a true boolean. Ze
#### `parsejson`
Converts a string of JSON into the correct Puppet structure. *Type*: rvalue.
+The optional second argument will be returned if the data was not correct.
#### `parseyaml`
Converts a string of YAML into the correct Puppet structure. *Type*: rvalue.
+The optional second argument will be returned if the data was not correct.
#### `pick`
@@ -532,10 +563,10 @@ From a list of values, returns the first value that is not undefined or an empty
#### `prefix`
-Applies a prefix to all elements in an array, or to the keys in a hash.
+Applies a prefix to all elements in an array, or to the keys in a hash.
For example:
* `prefix(['a','b','c'], 'p')` returns ['pa','pb','pc']
-* `prefix({'a'=>'b','b'=>'c','c'=>'d'}, 'p')` returns {'pa'=>'b','pb'=>'c','pc'=>'d'}.
+* `prefix({'a'=>'b','b'=>'c','c'=>'d'}, 'p')` returns {'pa'=>'b','pb'=>'c','pc'=>'d'}.
*Type*: rvalue.
@@ -573,6 +604,8 @@ The second argument to this function is which type of hash to use. It will be co
The third argument to this function is the salt to use.
+*Type*: rvalue.
+
**Note:** this uses the Puppet master's implementation of crypt(3). If your environment contains several different operating systems, ensure that they are compatible before using this function.
#### `range`
@@ -615,7 +648,7 @@ Returns a new string where runs of the same character that occur in this set are
#### `str2bool`
-Converts a string to a boolean. This attempts to convert strings that contain values such as '1', 't', 'y', and 'yes' to 'true' and strings that contain values such as '0', 'f', 'n', and 'no' to 'false'. *Type*: rvalue.
+Converts a string to a boolean regardless of case. This attempts to convert strings that contain values such as '1', 't', 'y', 'Y', 'YES','yes', and 'TRUE' to 'true' and strings that contain values such as '0', 'f','F', 'N','n', 'NO','FALSE', and 'no' to 'false'. *Type*: rvalue.
#### `str2saltedsha512`
@@ -694,6 +727,55 @@ Returns the current Unix epoch time as an integer. For example, `time()` returns
Converts the argument into bytes, for example "4 kB" becomes "4096". Takes a single string value as an argument. *Type*: rvalue.
+#### `try_get_value`
+
+*Type*: rvalue.
+
+Looks up into a complex structure of arrays and hashes to extract a value by
+its path in the structure. The path is a string of hash keys or array indexes
+starting with zero, separated by the path separator character (default "/").
+The function will go down the structure by each path component and will try to
+return the value at the end of the path.
+
+In addition to the required "path" argument the function accepts the default
+argument. It will be returned if the path is not correct, no value was found or
+a any other error have occurred. And the last argument can set the path
+separator character.
+
+```ruby
+$data = {
+ 'a' => {
+ 'b' => [
+ 'b1',
+ 'b2',
+ 'b3',
+ ]
+ }
+}
+
+$value = try_get_value($data, 'a/b/2')
+# $value = 'b3'
+
+# with all possible options
+$value = try_get_value($data, 'a/b/2', 'not_found', '/')
+# $value = 'b3'
+
+# using the default value
+$value = try_get_value($data, 'a/b/c/d', 'not_found')
+# $value = 'not_found'
+
+# using custom separator
+$value = try_get_value($data, 'a|b', [], '|')
+# $value = ['b1','b2','b3']
+```
+
+1. **$data** The data structure we are working with.
+2. **'a/b/2'** The path string.
+3. **'not_found'** The default value. It will be returned if nothing is found.
+ (optional, defaults to *undef*)
+4. **'/'** The path separator character.
+ (optional, defaults to *'/'*)
+
#### `type3x`
Returns a string description of the type when passed a value. Type can be a string, array, hash, float, integer, or boolean. This function will be removed when Puppet 3 support is dropped and the new type system can be used. *Type*: rvalue.
@@ -704,7 +786,7 @@ Returns the literal type when passed a value. Requires the new parser. Useful fo
#### `union`
-Returns a union of two arrays, without duplicates. For example, `union(["a","b","c"],["b","c","d"])` returns ["a","b","c","d"].
+Returns a union of two or more arrays, without duplicates. For example, `union(["a","b","c"],["b","c","d"])` returns ["a","b","c","d"]. *Type*: rvalue.
#### `unique`
@@ -972,6 +1054,13 @@ test, and the second argument should be a stringified regular expression (withou
validate_re($::puppetversion, '^2.7', 'The $puppetversion fact value does not match 2.7')
~~~
+ Note: Compilation will also abort, if the first argument is not a String. Always use
+ quotes to force stringification:
+
+ ~~~
+ validate_re("${::operatingsystemmajrelease}", '^[57]$')
+ ~~~
+
*Type*: statement.
#### `validate_slength`
diff --git a/lib/facter/root_home.rb b/lib/facter/root_home.rb
index b4f87ff..ee3ffa8 100644
--- a/lib/facter/root_home.rb
+++ b/lib/facter/root_home.rb
@@ -30,3 +30,16 @@ Facter.add(:root_home) do
hash['dir'].strip
end
end
+
+Facter.add(:root_home) do
+ confine :kernel => :aix
+ root_home = nil
+ setcode do
+ str = Facter::Util::Resolution.exec("lsuser -C -a home root")
+ str && str.split("\n").each do |line|
+ next if line =~ /^#/
+ root_home = line.split(/:/)[1]
+ end
+ root_home
+ end
+end
diff --git a/lib/puppet/functions/is_a.rb b/lib/puppet/functions/is_a.rb
new file mode 100644
index 0000000..da98b03
--- /dev/null
+++ b/lib/puppet/functions/is_a.rb
@@ -0,0 +1,32 @@
+# Boolean check to determine whether a variable is of a given data type. This is equivalent to the `=~` type checks.
+#
+# @example how to check a data type
+# # check a data type
+# foo = 3
+# $bar = [1,2,3]
+# $baz = 'A string!'
+#
+# if $foo.is_a(Integer) {
+# notify { 'foo!': }
+# }
+# if $bar.is_a(Array) {
+# notify { 'bar!': }
+# }
+# if $baz.is_a(String) {
+# notify { 'baz!': }
+# }
+#
+# See the documentation for "The Puppet Type System" for more information about types.
+# See the `assert_type()` function for flexible ways to assert the type of a value.
+#
+Puppet::Functions.create_function(:is_a) do
+ dispatch :is_a do
+ param 'Any', :value
+ param 'Type', :type
+ end
+
+ def is_a(value, type)
+ # See puppet's lib/puppet/pops/evaluator/evaluator_impl.rb eval_MatchExpression
+ Puppet::Pops::Types::TypeCalculator.instance?(type, value)
+ end
+end
diff --git a/lib/puppet/parser/functions/convert_base.rb b/lib/puppet/parser/functions/convert_base.rb
new file mode 100644
index 0000000..0fcbafe
--- /dev/null
+++ b/lib/puppet/parser/functions/convert_base.rb
@@ -0,0 +1,35 @@
+module Puppet::Parser::Functions
+
+ newfunction(:convert_base, :type => :rvalue, :arity => 2, :doc => <<-'ENDHEREDOC') do |args|
+
+ Converts a given integer or base 10 string representing an integer to a specified base, as a string.
+
+ Usage:
+
+ $binary_repr = convert_base(5, 2) # $binary_repr is now set to "101"
+ $hex_repr = convert_base("254", "16") # $hex_repr is now set to "fe"
+
+ ENDHEREDOC
+
+ raise Puppet::ParseError, ("convert_base(): First argument must be either a string or an integer") unless (args[0].is_a?(Integer) or args[0].is_a?(String))
+ raise Puppet::ParseError, ("convert_base(): Second argument must be either a string or an integer") unless (args[1].is_a?(Integer) or args[1].is_a?(String))
+
+ if args[0].is_a?(String)
+ raise Puppet::ParseError, ("convert_base(): First argument must be an integer or a string corresponding to an integer in base 10") unless args[0] =~ /^[0-9]+$/
+ end
+
+ if args[1].is_a?(String)
+ raise Puppet::ParseError, ("convert_base(): First argument must be an integer or a string corresponding to an integer in base 10") unless args[1] =~ /^[0-9]+$/
+ end
+
+ number_to_convert = args[0]
+ new_base = args[1]
+
+ number_to_convert = number_to_convert.to_i()
+ new_base = new_base.to_i()
+
+ raise Puppet::ParseError, ("convert_base(): base must be at least 2 and must not be greater than 36") unless new_base >= 2 and new_base <= 36
+
+ return number_to_convert.to_s(new_base)
+ end
+end
diff --git a/lib/puppet/parser/functions/intersection.rb b/lib/puppet/parser/functions/intersection.rb
index 48f02e9..bfbb4ba 100644
--- a/lib/puppet/parser/functions/intersection.rb
+++ b/lib/puppet/parser/functions/intersection.rb
@@ -4,13 +4,13 @@
module Puppet::Parser::Functions
newfunction(:intersection, :type => :rvalue, :doc => <<-EOS
-This function returns an array an intersection of two.
+This function returns an array of the intersection of two.
*Examples:*
- intersection(["a","b","c"],["b","c","d"])
+ intersection(["a","b","c"],["b","c","d"]) # returns ["b","c"]
+ intersection(["a","b","c"],[1,2,3,4]) # returns [] (true, when evaluated as a Boolean)
-Would return: ["b","c"]
EOS
) do |arguments|
diff --git a/lib/puppet/parser/functions/parsejson.rb b/lib/puppet/parser/functions/parsejson.rb
index a9a16a4..b4af40e 100644
--- a/lib/puppet/parser/functions/parsejson.rb
+++ b/lib/puppet/parser/functions/parsejson.rb
@@ -4,20 +4,25 @@
module Puppet::Parser::Functions
newfunction(:parsejson, :type => :rvalue, :doc => <<-EOS
-This function accepts JSON as a string and converts into the correct Puppet
-structure.
- EOS
+This function accepts JSON as a string and converts it into the correct
+Puppet structure.
+
+The optional second argument can be used to pass a default value that will
+be returned if the parsing of YAML string have failed.
+ EOS
) do |arguments|
+ raise ArgumentError, 'Wrong number of arguments. 1 or 2 arguments should be provided.' unless arguments.length >= 1
- if (arguments.size != 1) then
- raise(Puppet::ParseError, "parsejson(): Wrong number of arguments "+
- "given #{arguments.size} for 1")
+ begin
+ PSON::load(arguments[0]) || arguments[1]
+ rescue Exception => e
+ if arguments[1]
+ arguments[1]
+ else
+ raise e
+ end
end
- json = arguments[0]
-
- # PSON is natively available in puppet
- PSON.load(json)
end
end
diff --git a/lib/puppet/parser/functions/parseyaml.rb b/lib/puppet/parser/functions/parseyaml.rb
index 53d54fa..66d0413 100644
--- a/lib/puppet/parser/functions/parseyaml.rb
+++ b/lib/puppet/parser/functions/parseyaml.rb
@@ -6,17 +6,23 @@ module Puppet::Parser::Functions
newfunction(:parseyaml, :type => :rvalue, :doc => <<-EOS
This function accepts YAML as a string and converts it into the correct
Puppet structure.
- EOS
- ) do |arguments|
-
- if (arguments.size != 1) then
- raise(Puppet::ParseError, "parseyaml(): Wrong number of arguments "+
- "given #{arguments.size} for 1")
- end
+The optional second argument can be used to pass a default value that will
+be returned if the parsing of YAML string have failed.
+ EOS
+ ) do |arguments|
+ raise ArgumentError, 'Wrong number of arguments. 1 or 2 arguments should be provided.' unless arguments.length >= 1
require 'yaml'
- YAML::load(arguments[0])
+ begin
+ YAML::load(arguments[0]) || arguments[1]
+ rescue Exception => e
+ if arguments[1]
+ arguments[1]
+ else
+ raise e
+ end
+ end
end
end
diff --git a/lib/puppet/parser/functions/str2bool.rb b/lib/puppet/parser/functions/str2bool.rb
index 446732e..8def131 100644
--- a/lib/puppet/parser/functions/str2bool.rb
+++ b/lib/puppet/parser/functions/str2bool.rb
@@ -5,8 +5,8 @@
module Puppet::Parser::Functions
newfunction(:str2bool, :type => :rvalue, :doc => <<-EOS
This converts a string to a boolean. This attempt to convert strings that
-contain things like: y, 1, t, true to 'true' and strings that contain things
-like: 0, f, n, false, no to 'false'.
+contain things like: Y,y, 1, T,t, TRUE,true to 'true' and strings that contain things
+like: 0, F,f, N,n, false, FALSE, no to 'false'.
EOS
) do |arguments|
@@ -32,8 +32,8 @@ like: 0, f, n, false, no to 'false'.
# We yield false in this case.
#
when /^$/, '' then false # Empty string will be false ...
- when /^(1|t|y|true|yes)$/ then true
- when /^(0|f|n|false|no)$/ then false
+ when /^(1|t|y|true|yes)$/i then true
+ when /^(0|f|n|false|no)$/i then false
when /^(undef|undefined)$/ then false # This is not likely to happen ...
else
raise(Puppet::ParseError, 'str2bool(): Unknown type of boolean given')
diff --git a/lib/puppet/parser/functions/try_get_value.rb b/lib/puppet/parser/functions/try_get_value.rb
new file mode 100644
index 0000000..0c19fd9
--- /dev/null
+++ b/lib/puppet/parser/functions/try_get_value.rb
@@ -0,0 +1,77 @@
+module Puppet::Parser::Functions
+ newfunction(
+ :try_get_value,
+ :type => :rvalue,
+ :arity => -2,
+ :doc => <<-eos
+Looks up into a complex structure of arrays and hashes and returns a value
+or the default value if nothing was found.
+
+Key can contain slashes to describe path components. The function will go down
+the structure and try to extract the required value.
+
+$data = {
+ 'a' => {
+ 'b' => [
+ 'b1',
+ 'b2',
+ 'b3',
+ ]
+ }
+}
+
+$value = try_get_value($data, 'a/b/2', 'not_found', '/')
+=> $value = 'b3'
+
+a -> first hash key
+b -> second hash key
+2 -> array index starting with 0
+
+not_found -> (optional) will be returned if there is no value or the path did not match. Defaults to nil.
+/ -> (optional) path delimiter. Defaults to '/'.
+
+In addition to the required "key" argument, "try_get_value" accepts default
+argument. It will be returned if no value was found or a path component is
+missing. And the fourth argument can set a variable path separator.
+ eos
+ ) do |args|
+ path_lookup = lambda do |data, path, default|
+ debug "Try_get_value: #{path.inspect} from: #{data.inspect}"
+ if data.nil?
+ debug "Try_get_value: no data, return default: #{default.inspect}"
+ break default
+ end
+ unless path.is_a? Array
+ debug "Try_get_value: wrong path, return default: #{default.inspect}"
+ break default
+ end
+ unless path.any?
+ debug "Try_get_value: value found, return data: #{data.inspect}"
+ break data
+ end
+ unless data.is_a? Hash or data.is_a? Array
+ debug "Try_get_value: incorrect data, return default: #{default.inspect}"
+ break default
+ end
+
+ key = path.shift
+ if data.is_a? Array
+ begin
+ key = Integer key
+ rescue ArgumentError
+ debug "Try_get_value: non-numeric path for an array, return default: #{default.inspect}"
+ break default
+ end
+ end
+ path_lookup.call data[key], path, default
+ end
+
+ data = args[0]
+ path = args[1] || ''
+ default = args[2]
+ separator = args[3] || '/'
+
+ path = path.split separator
+ path_lookup.call data, path, default
+ end
+end
diff --git a/lib/puppet/parser/functions/union.rb b/lib/puppet/parser/functions/union.rb
index c91bb80..6c5bb83 100644
--- a/lib/puppet/parser/functions/union.rb
+++ b/lib/puppet/parser/functions/union.rb
@@ -4,7 +4,7 @@
module Puppet::Parser::Functions
newfunction(:union, :type => :rvalue, :doc => <<-EOS
-This function returns a union of two arrays.
+This function returns a union of two or more arrays.
*Examples:*
@@ -14,20 +14,15 @@ Would return: ["a","b","c","d"]
EOS
) do |arguments|
- # Two arguments are required
+ # Check that 2 or more arguments have been given ...
raise(Puppet::ParseError, "union(): Wrong number of arguments " +
- "given (#{arguments.size} for 2)") if arguments.size != 2
+ "given (#{arguments.size} for < 2)") if arguments.size < 2
- first = arguments[0]
- second = arguments[1]
-
- unless first.is_a?(Array) && second.is_a?(Array)
- raise(Puppet::ParseError, 'union(): Requires 2 arrays')
+ arguments.each do |argument|
+ raise(Puppet::ParseError, 'union(): Every parameter must be an array') unless argument.is_a?(Array)
end
- result = first | second
-
- return result
+ arguments.reduce(:|)
end
end
diff --git a/lib/puppet/parser/functions/validate_re.rb b/lib/puppet/parser/functions/validate_re.rb
index ca25a70..efee7f8 100644
--- a/lib/puppet/parser/functions/validate_re.rb
+++ b/lib/puppet/parser/functions/validate_re.rb
@@ -23,16 +23,23 @@ module Puppet::Parser::Functions
validate_re($::puppetversion, '^2.7', 'The $puppetversion fact value does not match 2.7')
+ Note: Compilation will also abort, if the first argument is not a String. Always use
+ quotes to force stringification:
+
+ validate_re("${::operatingsystemmajrelease}", '^[57]$')
+
ENDHEREDOC
if (args.length < 2) or (args.length > 3) then
- raise Puppet::ParseError, ("validate_re(): wrong number of arguments (#{args.length}; must be 2 or 3)")
+ raise Puppet::ParseError, "validate_re(): wrong number of arguments (#{args.length}; must be 2 or 3)"
end
+ raise Puppet::ParseError, "validate_re(): input needs to be a String, not a #{args[0].class}" unless args[0].is_a? String
+
msg = args[2] || "validate_re(): #{args[0].inspect} does not match #{args[1].inspect}"
# We're using a flattened array here because we can't call String#any? in
# Ruby 1.9 like we can in Ruby 1.8
- raise Puppet::ParseError, (msg) unless [args[1]].flatten.any? do |re_str|
+ raise Puppet::ParseError, msg unless [args[1]].flatten.any? do |re_str|
args[0] =~ Regexp.compile(re_str)
end
diff --git a/spec/acceptance/is_a_spec.rb b/spec/acceptance/is_a_spec.rb
new file mode 100644
index 0000000..355fd83
--- /dev/null
+++ b/spec/acceptance/is_a_spec.rb
@@ -0,0 +1,30 @@
+#! /usr/bin/env ruby -S rspec
+require 'spec_helper_acceptance'
+
+if get_puppet_version =~ /^4/
+ describe 'is_a function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
+ it 'should match a string' do
+ pp = <<-EOS
+ if 'hello world'.is_a(String) {
+ notify { 'output correct': }
+ }
+ EOS
+
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).to match(/Notice: output correct/)
+ end
+ end
+
+ it 'should not match a integer as string' do
+ pp = <<-EOS
+ if 5.is_a(String) {
+ notify { 'output wrong': }
+ }
+ EOS
+
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).not_to match(/Notice: output wrong/)
+ end
+ end
+ end
+end
diff --git a/spec/acceptance/parsejson_spec.rb b/spec/acceptance/parsejson_spec.rb
index 5097810..d0e3de8 100755
--- a/spec/acceptance/parsejson_spec.rb
+++ b/spec/acceptance/parsejson_spec.rb
@@ -16,10 +16,23 @@ describe 'parsejson function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('o
end
end
end
+
describe 'failure' do
it 'raises error on incorrect json' do
pp = <<-EOS
$a = '{"hunter": "washere", "tests": "passing",}'
+ $ao = parsejson($a, 'tests are using the default value')
+ notice(inline_template('a is <%= @ao.inspect %>'))
+ EOS
+
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).to match(/tests are using the default value/)
+ end
+ end
+
+ it 'raises error on incorrect json' do
+ pp = <<-EOS
+ $a = '{"hunter": "washere", "tests": "passing",}'
$ao = parsejson($a)
notice(inline_template('a is <%= @ao.inspect %>'))
EOS
@@ -29,6 +42,14 @@ describe 'parsejson function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('o
end
end
- it 'raises error on incorrect number of arguments'
+ it 'raises error on incorrect number of arguments' do
+ pp = <<-EOS
+ $o = parsejson()
+ EOS
+
+ apply_manifest(pp, :expect_failures => true) do |r|
+ expect(r.stderr).to match(/wrong number of arguments/i)
+ end
+ end
end
end
diff --git a/spec/acceptance/parseyaml_spec.rb b/spec/acceptance/parseyaml_spec.rb
index 5819837..64511f1 100755
--- a/spec/acceptance/parseyaml_spec.rb
+++ b/spec/acceptance/parseyaml_spec.rb
@@ -16,7 +16,21 @@ describe 'parseyaml function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('o
end
end
end
+
describe 'failure' do
+ it 'returns the default value on incorrect yaml' do
+ pp = <<-EOS
+ $a = "---\nhunter: washere\ntests: passing\n:"
+ $o = parseyaml($a, {'tests' => 'using the default value'})
+ $tests = $o['tests']
+ notice(inline_template('tests are <%= @tests.inspect %>'))
+ EOS
+
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).to match(/tests are "using the default value"/)
+ end
+ end
+
it 'raises error on incorrect yaml' do
pp = <<-EOS
$a = "---\nhunter: washere\ntests: passing\n:"
@@ -30,6 +44,15 @@ describe 'parseyaml function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('o
end
end
- it 'raises error on incorrect number of arguments'
+
+ it 'raises error on incorrect number of arguments' do
+ pp = <<-EOS
+ $o = parseyaml()
+ EOS
+
+ apply_manifest(pp, :expect_failures => true) do |r|
+ expect(r.stderr).to match(/wrong number of arguments/i)
+ end
+ end
end
end
diff --git a/spec/acceptance/try_get_value_spec.rb b/spec/acceptance/try_get_value_spec.rb
new file mode 100755
index 0000000..c0bf38a
--- /dev/null
+++ b/spec/acceptance/try_get_value_spec.rb
@@ -0,0 +1,47 @@
+#! /usr/bin/env ruby -S rspec
+require 'spec_helper_acceptance'
+
+describe 'try_get_value function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
+ describe 'success' do
+ it 'gets a value' do
+ pp = <<-EOS
+ $data = {
+ 'a' => { 'b' => 'passing'}
+ }
+
+ $tests = try_get_value($data, 'a/b')
+ notice(inline_template('tests are <%= @tests.inspect %>'))
+ EOS
+
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).to match(/tests are "passing"/)
+ end
+ end
+ end
+ describe 'failure' do
+ it 'uses a default value' do
+ pp = <<-EOS
+ $data = {
+ 'a' => { 'b' => 'passing'}
+ }
+
+ $tests = try_get_value($data, 'c/d', 'using the default value')
+ notice(inline_template('tests are <%= @tests.inspect %>'))
+ EOS
+
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).to match(/using the default value/)
+ end
+ end
+
+ it 'raises error on incorrect number of arguments' do
+ pp = <<-EOS
+ $o = try_get_value()
+ EOS
+
+ apply_manifest(pp, :expect_failures => true) do |r|
+ expect(r.stderr).to match(/wrong number of arguments/i)
+ end
+ end
+ end
+end
diff --git a/spec/acceptance/union_spec.rb b/spec/acceptance/union_spec.rb
index 6db8d0c..160fd7b 100755
--- a/spec/acceptance/union_spec.rb
+++ b/spec/acceptance/union_spec.rb
@@ -6,9 +6,10 @@ describe 'union function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('opera
it 'unions arrays' do
pp = <<-EOS
$a = ["the","public"]
- $b = ["art","galleries"]
+ $b = ["art"]
+ $c = ["galleries"]
# Anagram: Large picture halls, I bet
- $o = union($a,$b)
+ $o = union($a,$b,$c)
notice(inline_template('union is <%= @o.inspect %>'))
EOS
diff --git a/spec/fixtures/lsuser/root b/spec/fixtures/lsuser/root
new file mode 100644
index 0000000..afd59ca
--- /dev/null
+++ b/spec/fixtures/lsuser/root
@@ -0,0 +1,2 @@
+#name:home
+root:/root
diff --git a/spec/functions/base64_spec.rb b/spec/functions/base64_spec.rb
index 42512b3..c529e9e 100755
--- a/spec/functions/base64_spec.rb
+++ b/spec/functions/base64_spec.rb
@@ -10,6 +10,7 @@ describe 'base64' do
it { is_expected.to run.with_params("encode", 2).and_raise_error(Puppet::ParseError, /second argument must be a string/) }
it { is_expected.to run.with_params("encode", "thestring").and_return("dGhlc3RyaW5n\n") }
+ it { is_expected.to run.with_params("encode", "a very long string that will cause the base64 encoder to produce output with multiple lines").and_return("YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0\nIGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5l\ncw==\n") }
it { is_expected.to run.with_params("decode", "dGhlc3RyaW5n").and_return("thestring") }
it { is_expected.to run.with_params("decode", "dGhlc3RyaW5n\n").and_return("thestring") }
end
diff --git a/spec/functions/convert_base_spec.rb b/spec/functions/convert_base_spec.rb
new file mode 100644
index 0000000..8ab2284
--- /dev/null
+++ b/spec/functions/convert_base_spec.rb
@@ -0,0 +1,24 @@
+require 'spec_helper'
+
+describe 'convert_base' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(ArgumentError) }
+ it { is_expected.to run.with_params("asdf").and_raise_error(ArgumentError) }
+ it { is_expected.to run.with_params("asdf","moo","cow").and_raise_error(ArgumentError) }
+ it { is_expected.to run.with_params(["1"],"2").and_raise_error(Puppet::ParseError, /argument must be either a string or an integer/) }
+ it { is_expected.to run.with_params("1",["2"]).and_raise_error(Puppet::ParseError, /argument must be either a string or an integer/) }
+ it { is_expected.to run.with_params("1",1).and_raise_error(Puppet::ParseError, /base must be at least 2 and must not be greater than 36/) }
+ it { is_expected.to run.with_params("1",37).and_raise_error(Puppet::ParseError, /base must be at least 2 and must not be greater than 36/) }
+
+ it "should raise a ParseError if argument 1 is a string that does not correspond to an integer in base 10" do
+ is_expected.to run.with_params("ten",6).and_raise_error(Puppet::ParseError, /argument must be an integer or a string corresponding to an integer in base 10/)
+ end
+
+ it "should raise a ParseError if argument 2 is a string and does not correspond to an integer in base 10" do
+ is_expected.to run.with_params(100,"hex").and_raise_error(Puppet::ParseError, /argument must be an integer or a string corresponding to an integer in base 10/)
+ end
+
+ it { is_expected.to run.with_params("11",'16').and_return('b') }
+ it { is_expected.to run.with_params("35",'36').and_return('z') }
+ it { is_expected.to run.with_params(5, 2).and_return('101') }
+end
diff --git a/spec/functions/is_a_spec.rb b/spec/functions/is_a_spec.rb
new file mode 100644
index 0000000..8dec13f
--- /dev/null
+++ b/spec/functions/is_a_spec.rb
@@ -0,0 +1,25 @@
+require 'spec_helper'
+
+if ENV["FUTURE_PARSER"] == 'yes'
+ describe 'type_of' do
+ pending 'teach rspec-puppet to load future-only functions under 3.7.5' do
+ it { is_expected.not_to eq(nil) }
+ end
+ end
+end
+
+if Puppet.version.to_f >= 4.0
+ describe 'is_a' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(ArgumentError) }
+ it { is_expected.to run.with_params('', '').and_raise_error(ArgumentError) }
+
+ it 'succeeds when comparing a string and a string' do
+ is_expected.to run.with_params('hello world', String).and_return(true)
+ end
+
+ it 'fails when comparing an integer and a string' do
+ is_expected.to run.with_params(5, String).and_return(false)
+ end
+ end
+end
diff --git a/spec/functions/parsejson_spec.rb b/spec/functions/parsejson_spec.rb
index 436566e..a01f1f6 100755
--- a/spec/functions/parsejson_spec.rb
+++ b/spec/functions/parsejson_spec.rb
@@ -1,9 +1,64 @@
require 'spec_helper'
describe 'parsejson' do
- it { is_expected.not_to eq(nil) }
- it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
- it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
- it { is_expected.to run.with_params('["one"').and_raise_error(PSON::ParserError) }
- it { is_expected.to run.with_params('["one", "two", "three"]').and_return(['one', 'two', 'three']) }
+ it 'should exist' do
+ is_expected.not_to eq(nil)
+ end
+
+ it 'should raise an error if called without any arguments' do
+ is_expected.to run.with_params().
+ and_raise_error(/wrong number of arguments/i)
+ end
+
+ context 'with correct JSON data' do
+
+ it 'should be able to parse a JSON data with a Hash' do
+ is_expected.to run.with_params('{"a":"1","b":"2"}').
+ and_return({'a'=>'1', 'b'=>'2'})
+ end
+
+ it 'should be able to parse a JSON data with an Array' do
+ is_expected.to run.with_params('["a","b","c"]').
+ and_return(['a', 'b', 'c'])
+ end
+
+ it 'should be able to parse empty JSON values' do
+ is_expected.to run.with_params('[]').
+ and_return([])
+ is_expected.to run.with_params('{}').
+ and_return({})
+ end
+
+ it 'should be able to parse a JSON data with a mixed structure' do
+ is_expected.to run.with_params('{"a":"1","b":2,"c":{"d":[true,false]}}').
+ and_return({'a' =>'1', 'b' => 2, 'c' => { 'd' => [true, false] } })
+ end
+
+ it 'should not return the default value if the data was parsed correctly' do
+ is_expected.to run.with_params('{"a":"1"}', 'default_value').
+ and_return({'a' => '1'})
+ end
+
+ end
+
+ context 'with incorrect JSON data' do
+ it 'should raise an error with invalid JSON and no default' do
+ is_expected.to run.with_params('').
+ and_raise_error(PSON::ParserError)
+ end
+
+ it 'should support a structure for a default value' do
+ is_expected.to run.with_params('', {'a' => '1'}).
+ and_return({'a' => '1'})
+ end
+
+ ['', 1, 1.2, nil, true, false, [], {}, :yaml].each do |value|
+ it "should return the default value for an incorrect #{value.inspect} (#{value.class}) parameter" do
+ is_expected.to run.with_params(value, 'default_value').
+ and_return('default_value')
+ end
+ end
+
+ end
+
end
diff --git a/spec/functions/parseyaml_spec.rb b/spec/functions/parseyaml_spec.rb
index fb635f8..fa947ca 100755
--- a/spec/functions/parseyaml_spec.rb
+++ b/spec/functions/parseyaml_spec.rb
@@ -1,14 +1,81 @@
require 'spec_helper'
describe 'parseyaml' do
- it { is_expected.not_to eq(nil) }
- it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
- it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
- it { is_expected.to run.with_params('["one", "two", "three"]').and_return(['one', 'two', 'three']) }
- context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do
- it { is_expected.to run.with_params('["one"').and_raise_error(Psych::SyntaxError) }
+ it 'should exist' do
+ is_expected.not_to eq(nil)
end
- context 'when running on ruby 1.8.7, which does not have Psych', :if => RUBY_VERSION == '1.8.7' do
- it { is_expected.to run.with_params('["one"').and_raise_error(ArgumentError) }
+
+ it 'should raise an error if called without any arguments' do
+ is_expected.to run.with_params().
+ and_raise_error(/wrong number of arguments/i)
end
+
+ context 'with correct YAML data' do
+ it 'should be able to parse a YAML data with a String' do
+ is_expected.to run.with_params('--- just a string').
+ and_return('just a string')
+ is_expected.to run.with_params('just a string').
+ and_return('just a string')
+ end
+
+ it 'should be able to parse a YAML data with a Hash' do
+ is_expected.to run.with_params("---\na: '1'\nb: '2'\n").
+ and_return({'a' => '1', 'b' => '2'})
+ end
+
+ it 'should be able to parse a YAML data with an Array' do
+ is_expected.to run.with_params("---\n- a\n- b\n- c\n").
+ and_return(['a', 'b', 'c'])
+ end
+
+ it 'should be able to parse a YAML data with a mixed structure' do
+ is_expected.to run.with_params("---\na: '1'\nb: 2\nc:\n d:\n - :a\n - true\n - false\n").
+ and_return({'a' => '1', 'b' => 2, 'c' => {'d' => [:a, true, false]}})
+ end
+
+ it 'should not return the default value if the data was parsed correctly' do
+ is_expected.to run.with_params("---\na: '1'\n", 'default_value').
+ and_return({'a' => '1'})
+ end
+
+ end
+
+ context 'on a modern ruby', :unless => RUBY_VERSION == '1.8.7' do
+ it 'should raise an error with invalid YAML and no default' do
+ is_expected.to run.with_params('["one"').
+ and_raise_error(Psych::SyntaxError)
+ end
+ end
+
+ context 'when running on ruby 1.8.7, which does not have Psych', :if => RUBY_VERSION == '1.8.7' do
+ it 'should raise an error with invalid YAML and no default' do
+ is_expected.to run.with_params('["one"').
+ and_raise_error(ArgumentError)
+ end
+ end
+
+ context 'with incorrect YAML data' do
+ it 'should support a structure for a default value' do
+ is_expected.to run.with_params('', {'a' => '1'}).
+ and_return({'a' => '1'})
+ end
+
+ [1, 1.2, nil, true, false, [], {}, :yaml].each do |value|
+ it "should return the default value for an incorrect #{value.inspect} (#{value.class}) parameter" do
+ is_expected.to run.with_params(value, 'default_value').
+ and_return('default_value')
+ end
+ end
+
+ context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do
+ ['---', '...', '*8', ''].each do |value|
+ it "should return the default value for an incorrect #{value.inspect} string parameter" do
+ is_expected.to run.with_params(value, 'default_value').
+ and_return('default_value')
+ end
+ end
+ end
+
+ end
+
end
diff --git a/spec/functions/str2bool_spec.rb b/spec/functions/str2bool_spec.rb
index 3b439b2..7d8c47c 100755
--- a/spec/functions/str2bool_spec.rb
+++ b/spec/functions/str2bool_spec.rb
@@ -10,13 +10,13 @@ describe 'str2bool' do
it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /Unknown type of boolean given/) }
describe 'when testing values that mean "true"' do
- [ '1', 't', 'y', 'true', 'yes', true ].each do |value|
+ [ 'TRUE','1', 't', 'y', 'true', 'yes', true ].each do |value|
it { is_expected.to run.with_params(value).and_return(true) }
end
end
describe 'when testing values that mean "false"' do
- [ '', '0', 'f', 'n', 'false', 'no', false, 'undef', 'undefined' ].each do |value|
+ [ 'FALSE','', '0', 'f', 'n', 'false', 'no', false, 'undef', 'undefined' ].each do |value|
it { is_expected.to run.with_params(value).and_return(false) }
end
end
diff --git a/spec/functions/try_get_value_spec.rb b/spec/functions/try_get_value_spec.rb
new file mode 100644
index 0000000..38c0efd
--- /dev/null
+++ b/spec/functions/try_get_value_spec.rb
@@ -0,0 +1,100 @@
+require 'spec_helper'
+
+describe 'try_get_value' do
+
+ let(:data) do
+ {
+ 'a' => {
+ 'g' => '2',
+ 'e' => [
+ 'f0',
+ 'f1',
+ {
+ 'x' => {
+ 'y' => 'z'
+ }
+ },
+ 'f3',
+ ]
+ },
+ 'b' => true,
+ 'c' => false,
+ 'd' => '1',
+ }
+ end
+
+ context 'single values' do
+ it 'should exist' do
+ is_expected.not_to eq(nil)
+ end
+
+ it 'should be able to return a single value' do
+ is_expected.to run.with_params('test').and_return('test')
+ end
+
+ it 'should use the default value if data is a single value and path is present' do
+ is_expected.to run.with_params('test', 'path', 'default').and_return('default')
+ end
+
+ it 'should return default if there is no data' do
+ is_expected.to run.with_params(nil, nil, 'default').and_return('default')
+ end
+
+ it 'should be able to use data structures as default values' do
+ is_expected.to run.with_params('test', 'path', data).and_return(data)
+ end
+ end
+
+ context 'structure values' do
+ it 'should be able to extracts a single hash value' do
+ is_expected.to run.with_params(data, 'd', 'default').and_return('1')
+ end
+
+ it 'should be able to extract a deeply nested hash value' do
+ is_expected.to run.with_params(data, 'a/g', 'default').and_return('2')
+ end
+
+ it 'should return the default value if the path is not found' do
+ is_expected.to run.with_params(data, 'missing', 'default').and_return('default')
+ end
+
+ it 'should return the default value if the path is too long' do
+ is_expected.to run.with_params(data, 'a/g/c/d', 'default').and_return('default')
+ end
+
+ it 'should support an array index in the path' do
+ is_expected.to run.with_params(data, 'a/e/1', 'default').and_return('f1')
+ end
+
+ it 'should return the default value if an array index is not a number' do
+ is_expected.to run.with_params(data, 'a/b/c', 'default').and_return('default')
+ end
+
+ it 'should return the default value if and index is out of array length' do
+ is_expected.to run.with_params(data, 'a/e/5', 'default').and_return('default')
+ end
+
+ it 'should be able to path though both arrays and hashes' do
+ is_expected.to run.with_params(data, 'a/e/2/x/y', 'default').and_return('z')
+ end
+
+ it 'should be able to return "true" value' do
+ is_expected.to run.with_params(data, 'b', 'default').and_return(true)
+ is_expected.to run.with_params(data, 'm', true).and_return(true)
+ end
+
+ it 'should be able to return "false" value' do
+ is_expected.to run.with_params(data, 'c', 'default').and_return(false)
+ is_expected.to run.with_params(data, 'm', false).and_return(false)
+ end
+
+ it 'should return "nil" if value is not found and no default value is provided' do
+ is_expected.to run.with_params(data, 'a/1').and_return(nil)
+ end
+
+ it 'should be able to use a custom path separator' do
+ is_expected.to run.with_params(data, 'a::g', 'default', '::').and_return('2')
+ is_expected.to run.with_params(data, 'a::c', 'default', '::').and_return('default')
+ end
+ end
+end
diff --git a/spec/functions/type_of_spec.rb b/spec/functions/type_of_spec.rb
index f770990..cc9ef78 100644
--- a/spec/functions/type_of_spec.rb
+++ b/spec/functions/type_of_spec.rb
@@ -2,7 +2,9 @@ require 'spec_helper'
if ENV["FUTURE_PARSER"] == 'yes'
describe 'type_of' do
- pending 'teach rspec-puppet to load future-only functions under 3.7.5'
+ pending 'teach rspec-puppet to load future-only functions under 3.7.5' do
+ it { is_expected.not_to eq(nil) }
+ end
end
end
diff --git a/spec/functions/union_spec.rb b/spec/functions/union_spec.rb
index 970e1fe..cfd38b6 100755
--- a/spec/functions/union_spec.rb
+++ b/spec/functions/union_spec.rb
@@ -5,10 +5,9 @@ describe 'union' do
it { is_expected.not_to eq(nil) }
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
- it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
- it { is_expected.to run.with_params('one', []).and_raise_error(Puppet::ParseError, /Requires 2 arrays/) }
- it { is_expected.to run.with_params([], 'two').and_raise_error(Puppet::ParseError, /Requires 2 arrays/) }
- it { is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError, /Requires 2 arrays/) }
+ it { is_expected.to run.with_params('one', []).and_raise_error(Puppet::ParseError, /Every parameter must be an array/) }
+ it { is_expected.to run.with_params([], 'two').and_raise_error(Puppet::ParseError, /Every parameter must be an array/) }
+ it { is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError, /Every parameter must be an array/) }
end
it { is_expected.to run.with_params([], []).and_return([]) }
@@ -19,5 +18,7 @@ describe 'union' do
it { is_expected.to run.with_params(['one', 'two', 'three'], ['two', 'three']).and_return(['one', 'two', 'three']) }
it { is_expected.to run.with_params(['one', 'two', 'two', 'three'], ['two', 'three']).and_return(['one', 'two', 'three']) }
it { is_expected.to run.with_params(['one', 'two', 'three'], ['two', 'two', 'three']).and_return(['one', 'two', 'three']) }
+ it { is_expected.to run.with_params(['one', 'two'], ['two', 'three'], ['one', 'three']).and_return(['one', 'two', 'three']) }
+ it { is_expected.to run.with_params(['one', 'two'], ['three', 'four'], ['one', 'two', 'three'], ['four']).and_return(['one', 'two', 'three', 'four']) }
it 'should not confuse types' do is_expected.to run.with_params(['1', '2', '3'], [1, 2]).and_return(['1', '2', '3', 1, 2]) end
end
diff --git a/spec/functions/validate_re_spec.rb b/spec/functions/validate_re_spec.rb
index 42b1049..3f90143 100755
--- a/spec/functions/validate_re_spec.rb
+++ b/spec/functions/validate_re_spec.rb
@@ -41,6 +41,21 @@ describe 'validate_re' do
it { is_expected.to run.with_params('notone', '^one').and_raise_error(Puppet::ParseError, /does not match/) }
it { is_expected.to run.with_params('notone', [ '^one', '^two' ]).and_raise_error(Puppet::ParseError, /does not match/) }
it { is_expected.to run.with_params('notone', [ '^one', '^two' ], 'custom error').and_raise_error(Puppet::ParseError, /custom error/) }
+
+ describe 'non-string inputs' do
+ [
+ 1, # Fixnum
+ 3.14, # Float
+ nil, # NilClass
+ true, # TrueClass
+ false, # FalseClass
+ ["10"], # Array
+ :key, # Symbol
+ {:key=>"val"}, # Hash
+ ].each do |input|
+ it { is_expected.to run.with_params(input, '.*').and_raise_error(Puppet::ParseError, /needs to be a String/) }
+ end
+ end
end
end
end
diff --git a/spec/spec_helper_acceptance.rb b/spec/spec_helper_acceptance.rb
index f784112..eda0d1a 100755
--- a/spec/spec_helper_acceptance.rb
+++ b/spec/spec_helper_acceptance.rb
@@ -33,9 +33,13 @@ def is_future_parser_enabled?
return false
end
+def get_puppet_version
+ (on default, puppet('--version')).output.chomp
+end
+
RSpec.shared_context "with faked facts" do
let(:facts_d) do
- puppet_version = (on default, puppet('--version')).output.chomp
+ puppet_version = get_puppet_version
if fact('osfamily') =~ /windows/i
if fact('kernelmajversion').to_f < 6.0
'C:/Documents and Settings/All Users/Application Data/PuppetLabs/facter/facts.d'
diff --git a/spec/unit/facter/root_home_spec.rb b/spec/unit/facter/root_home_spec.rb
index 98fe141..ac5160a 100755
--- a/spec/unit/facter/root_home_spec.rb
+++ b/spec/unit/facter/root_home_spec.rb
@@ -49,4 +49,17 @@ describe 'root_home', :type => :fact do
end
end
+ context "aix" do
+ before do
+ Facter.fact(:kernel).stubs(:value).returns("AIX")
+ Facter.fact(:osfamily).stubs(:value).returns("AIX")
+ end
+ let(:expected_root_home) { "/root" }
+ sample_lsuser = File.read(fixtures('lsuser','root'))
+
+ it "should return /root" do
+ Facter::Util::Resolution.stubs(:exec).with("lsuser -C -a home root").returns(sample_lsuser)
+ expect(Facter.fact(:root_home).value).to eq(expected_root_home)
+ end
+ end
end