From b305bbeac7a0560a271f34026f936b88b88da477 Mon Sep 17 00:00:00 2001 From: Roman Barczyński Date: Thu, 3 Mar 2011 02:48:35 +0100 Subject: initial version --- load_vars.rb | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 load_vars.rb diff --git a/load_vars.rb b/load_vars.rb new file mode 100644 index 0000000..9befba9 --- /dev/null +++ b/load_vars.rb @@ -0,0 +1,56 @@ +# vim: set ts=2 sw=2 et : +# +# load_data loads varibles from external yaml file. +# +# EXAMPLE 1: +# data.yaml: +# -- +# host1.client.com: +# abc: def +# foo: bar +# test: other +# host2.client.com: +# abc: abc +# foo: baz +# test: other2 +# +# load_vars("/etc/puppet/data.yaml", $fqdn) +# will try to find matching $fqdn key in data.yaml +# and, if found, will add variables $abc $foo and $test +# +# +# EXAMPLE 2: +# data-host1.clent.com.yaml +# abc: def +# +# load_vars("/etc/puppet/data-$fqdn.yaml") +# will add variable $abc + +Puppet::Parser::Functions.newfunction :load_vars, :type => :statement do |args| + file = args[0] + data = {} + if args[1] + key = args[1] + end + + if FileTest.exist?(file) # file exists + + data = YAML.load_file(file) + raise ArgumentError, "Data in %s is not a hash" % file unless data.is_a?(Hash) + # data is a hash for sure + + if key + # if we have key then use it: + if data[key].is_a?(Hash) + data = data[key] + else + data = {} + end + end + + end + # add values from hash: + data.each do |param, value| + setvar(param, strinterp(value)) + end +end -- cgit v1.2.3 From 13098ff934b3ba88ef5e4ba9ce2b7232c32b9082 Mon Sep 17 00:00:00 2001 From: Roman Barczyński Date: Thu, 3 Mar 2011 03:09:55 +0100 Subject: cronrand - stateful (between puppet runs) randomizer eg. for cron minutes for equal load distribution --- cronrand.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 cronrand.rb diff --git a/cronrand.rb b/cronrand.rb new file mode 100644 index 0000000..3dd6810 --- /dev/null +++ b/cronrand.rb @@ -0,0 +1,31 @@ +# vim: set ts=2 sw=2 et : +# TODO: path should not be hardcoded here +# +# USAGE: +# $minutes = cronrand("puppet-run", $fqdn, 59) +# file { "puppet-cron": +# name => /etc/cron.d/puppet-run", +# content => "$minutes * * * * root /usr/sbin/puppetd --onetime --no-daemonize --logdest syslog > /dev/null 2>&1\n" +# } +# --- +# minutes will be chosen random and saved for each $fqdn, +# second puppet run on same host will create same content as first one. + +module Puppet::Parser::Functions + newfunction(:cronrand, :type => :rvalue) do |args| + job = args[0] + host = args[1] + minutes = (args[2].to_i < 60) ? args[2].to_i : 59 + filename = "/etc/puppet/modules/puppet/state/cronminutes-#{job}-#{host}" + value = 0 + + if FileTest.exists?(filename) + File.open(filename, 'r') { |fd| value = fd.gets.chomp.to_i } + else + value = rand(minutes) + File.open(filename, 'w') { |fd| fd.puts value } + end + value + end +end + -- cgit v1.2.3 From 20fcf0e610535681fa06e19a1aac61fabba30731 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Thu, 3 Mar 2011 02:37:26 +0000 Subject: Re-factor of the code to make it more Puppet-friendly and update of the short documentation in the comment lines. --- load_vars.rb | 109 +++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 61 insertions(+), 48 deletions(-) diff --git a/load_vars.rb b/load_vars.rb index 9befba9..0375166 100644 --- a/load_vars.rb +++ b/load_vars.rb @@ -1,56 +1,69 @@ -# vim: set ts=2 sw=2 et : -# -# load_data loads varibles from external yaml file. -# -# EXAMPLE 1: -# data.yaml: -# -- -# host1.client.com: -# abc: def -# foo: bar -# test: other -# host2.client.com: -# abc: abc -# foo: baz -# test: other2 -# -# load_vars("/etc/puppet/data.yaml", $fqdn) -# will try to find matching $fqdn key in data.yaml -# and, if found, will add variables $abc $foo and $test -# -# -# EXAMPLE 2: -# data-host1.clent.com.yaml -# abc: def -# -# load_vars("/etc/puppet/data-$fqdn.yaml") -# will add variable $abc - -Puppet::Parser::Functions.newfunction :load_vars, :type => :statement do |args| - file = args[0] - data = {} - if args[1] - key = args[1] - end +#!/usr/bin/env ruby +# +# load_vars.rb +# +# This script will allow for loading variables from an external YAML +# file and expose then for further use inside the Puppet manifest file ... +# +# For example: +# +# Given following content of the data.yaml file: +# +# --- +# host1.example.com: +# foo: bar +# baz: quux +# question: 42 +# host2.example.com: +# abc: def +# this: that +# darth: vader +# +# Then calling load_vars in Puppet manifest file as follows: +# +# load_vars("/etc/puppet/data.yaml", $fqdn) +# +# Will result in addition of variables $foo, $baz and $question +# for matching host name as per the variable $fqdn ... +# +# Another example which uses per-host file: +# +# Given following content of the file data-host1.example.com.yaml: +# +# --- +# foo: bar +# +# Then when we call load_vars like this: +# +# load_vars("/etc/puppet/data-$fqdn.yaml") +# +# This will result in a variable $foo being added and ready for use. +# - if FileTest.exist?(file) # file exists +module Puppet::Parser::Functions + newfunction(:load_vars, :type => :statement) do |args| + data = {} - data = YAML.load_file(file) - raise ArgumentError, "Data in %s is not a hash" % file unless data.is_a?(Hash) - # data is a hash for sure + file = args[0] + key = args[1] if args[1] - if key - # if we have key then use it: - if data[key].is_a?(Hash) - data = data[key] - else - data = {} + if File.exists?(file) + + begin + data = YAML.load_file(file) + rescue => error + raise(Puppet::ParseError, + "Unable to load data from the file `%s': %s" % file, error.to_s) end + + raise(Puppet::ParseError, + "Data in the file `%s' is not a hash" % file) unless data.is_a?(Hash) + + data = data[key] if key and data[key].is_a?(Hash) end - end - # add values from hash: - data.each do |param, value| - setvar(param, strinterp(value)) + data.each { |param, value| setvar(param, strinterp(value)) } end end + +# vim: set ts=2 sw=2 et -- cgit v1.2.3 From e566661cc7a11d4916c98f23cc0f63508fcc3aa2 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Thu, 3 Mar 2011 02:45:11 +0000 Subject: Fix to a type in the documentation. --- load_vars.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/load_vars.rb b/load_vars.rb index 0375166..0365cf9 100644 --- a/load_vars.rb +++ b/load_vars.rb @@ -3,7 +3,7 @@ # load_vars.rb # # This script will allow for loading variables from an external YAML -# file and expose then for further use inside the Puppet manifest file ... +# file and expose them for further use inside the Puppet manifest file ... # # For example: # -- cgit v1.2.3 From ab749e74149ee82cb903ed82dc8396657d1d5b83 Mon Sep 17 00:00:00 2001 From: Roman Barczyński Date: Thu, 3 Mar 2011 04:08:32 +0100 Subject: no need for "#! ruby", vim modeline must end with ":", and fixed not found key bug --- load_vars.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/load_vars.rb b/load_vars.rb index 0365cf9..07ee745 100644 --- a/load_vars.rb +++ b/load_vars.rb @@ -1,5 +1,3 @@ -#!/usr/bin/env ruby -# # load_vars.rb # # This script will allow for loading variables from an external YAML @@ -59,11 +57,11 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, "Data in the file `%s' is not a hash" % file) unless data.is_a?(Hash) - data = data[key] if key and data[key].is_a?(Hash) + data = ((data[key].is_a?(Hash)) ? data[key] : {}) if key end data.each { |param, value| setvar(param, strinterp(value)) } end end -# vim: set ts=2 sw=2 et +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 1064abcb6e7769cb9d51c20d330620ed1c06d6b9 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Thu, 3 Mar 2011 09:10:36 +0000 Subject: Re-factor. Removed not needed shebang for Ruby. --- load_vars.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/load_vars.rb b/load_vars.rb index 0365cf9..c7ebf6c 100644 --- a/load_vars.rb +++ b/load_vars.rb @@ -1,4 +1,3 @@ -#!/usr/bin/env ruby # # load_vars.rb # -- cgit v1.2.3 From 688fd8c1aa17e9b834b0e376ad5f6d16ec8ab370 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Thu, 3 Mar 2011 09:17:06 +0000 Subject: Fix. Given vim mode line should end with : in order to work. --- load_vars.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/load_vars.rb b/load_vars.rb index c7ebf6c..bebe4d8 100644 --- a/load_vars.rb +++ b/load_vars.rb @@ -65,4 +65,4 @@ module Puppet::Parser::Functions end end -# vim: set ts=2 sw=2 et +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From c5293e630c8c764c66721cbae5fa828b4f213f8b Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Thu, 3 Mar 2011 09:18:12 +0000 Subject: Fix. Should yield empty data structure when given key cannot be found in the hash upon parsing YAML file. --- load_vars.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/load_vars.rb b/load_vars.rb index bebe4d8..b39df27 100644 --- a/load_vars.rb +++ b/load_vars.rb @@ -58,7 +58,7 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, "Data in the file `%s' is not a hash" % file) unless data.is_a?(Hash) - data = data[key] if key and data[key].is_a?(Hash) + data = (key and data[key].is_a?(Hash) ? data[key] : {}) end data.each { |param, value| setvar(param, strinterp(value)) } -- cgit v1.2.3 From 19fd344a0fdb22ef6b3c6e80bc8fd4983e56d7cb Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Thu, 3 Mar 2011 13:14:56 +0100 Subject: Fix. The data structure should only be populated and/or set to be empty if and only if key is present. --- load_vars.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/load_vars.rb b/load_vars.rb index b39df27..4d0d14e 100644 --- a/load_vars.rb +++ b/load_vars.rb @@ -58,7 +58,7 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, "Data in the file `%s' is not a hash" % file) unless data.is_a?(Hash) - data = (key and data[key].is_a?(Hash) ? data[key] : {}) + data = ((data[key] and data[key].is_a?(Hash)) ? data[key] : {}) if key end data.each { |param, value| setvar(param, strinterp(value)) } -- cgit v1.2.3 From 49d208a13edce166811a71b1ada1d201e756c06a Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 11 Mar 2011 18:30:49 +0000 Subject: Small re-factor. --- load_vars.rb | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/load_vars.rb b/load_vars.rb index cc2495b..87effad 100644 --- a/load_vars.rb +++ b/load_vars.rb @@ -1,3 +1,4 @@ +# # load_vars.rb # # This script will allow for loading variables from an external YAML @@ -7,19 +8,19 @@ # # Given following content of the data.yaml file: # -# --- -# host1.example.com: -# foo: bar -# baz: quux -# question: 42 -# host2.example.com: -# abc: def -# this: that -# darth: vader +# --- +# host1.example.com: +# foo: bar +# baz: quux +# question: 42 +# host2.example.com: +# abc: def +# this: that +# darth: vader # # Then calling load_vars in Puppet manifest file as follows: # -# load_vars("/etc/puppet/data.yaml", $fqdn) +# load_vars("/etc/puppet/data.yaml", $fqdn) # # Will result in addition of variables $foo, $baz and $question # for matching host name as per the variable $fqdn ... @@ -28,12 +29,12 @@ # # Given following content of the file data-host1.example.com.yaml: # -# --- -# foo: bar +# --- +# foo: bar # # Then when we call load_vars like this: # -# load_vars("/etc/puppet/data-$fqdn.yaml") +# load_vars("/etc/puppet/data-$fqdn.yaml") # # This will result in a variable $foo being added and ready for use. # @@ -50,12 +51,12 @@ module Puppet::Parser::Functions begin data = YAML.load_file(file) rescue => error - raise(Puppet::ParseError, - "Unable to load data from the file `%s': %s" % file, error.to_s) + raise(Puppet::ParseError, "Unable to load data " + + "from the file `%s': %s" % file, error.to_s) end - raise(Puppet::ParseError, - "Data in the file `%s' is not a hash" % file) unless data.is_a?(Hash) + raise(Puppet::ParseError, "Data in the file `%s' " + + "is not a hash" % file) unless data.is_a?(Hash) data = ((data[key] and data[key].is_a?(Hash)) ? data[key] : {}) if key end -- cgit v1.2.3 From 0eea8ac101fdd6dbbe67c2406c3e742ae81a1c4c Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 12 Mar 2011 23:05:56 +0000 Subject: Check for the number of arguments given. --- load_vars.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/load_vars.rb b/load_vars.rb index 87effad..02e99a0 100644 --- a/load_vars.rb +++ b/load_vars.rb @@ -40,11 +40,19 @@ # module Puppet::Parser::Functions - newfunction(:load_vars, :type => :statement) do |args| + newfunction(:load_vars, :type => :statement) do |arguments| + + number_of_arguments = arguments.size + + if number_of_arguments < 2 + raise(Puppet::ParseError, "Wrong number of arguments " + + "given (#{number_of_arguments} for 2)") + end + data = {} - file = args[0] - key = args[1] if args[1] + file = arguments[0] + key = arguments[1] if arguments[1] if File.exists?(file) -- cgit v1.2.3 From 00a4c4ebcc67fc2efebc91090ece63e491fbd7b8 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 12 Mar 2011 23:15:02 +0000 Subject: Small re-factor. --- load_vars.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/load_vars.rb b/load_vars.rb index 02e99a0..ddfc0ef 100644 --- a/load_vars.rb +++ b/load_vars.rb @@ -42,12 +42,8 @@ module Puppet::Parser::Functions newfunction(:load_vars, :type => :statement) do |arguments| - number_of_arguments = arguments.size - - if number_of_arguments < 2 - raise(Puppet::ParseError, "Wrong number of arguments " + - "given (#{number_of_arguments} for 2)") - end + raise(Puppet::ParseError, "Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 data = {} -- cgit v1.2.3 From c73be8fdbd3ed726604f644a1cb66f0f99889baa Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sun, 13 Mar 2011 23:37:16 +0000 Subject: Changing the name of the file to reflect change in the function name. --- load_variables.rb | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ load_vars.rb | 72 ------------------------------------------------------- 2 files changed, 72 insertions(+), 72 deletions(-) create mode 100644 load_variables.rb delete mode 100644 load_vars.rb diff --git a/load_variables.rb b/load_variables.rb new file mode 100644 index 0000000..ddfc0ef --- /dev/null +++ b/load_variables.rb @@ -0,0 +1,72 @@ +# +# load_vars.rb +# +# This script will allow for loading variables from an external YAML +# file and expose them for further use inside the Puppet manifest file ... +# +# For example: +# +# Given following content of the data.yaml file: +# +# --- +# host1.example.com: +# foo: bar +# baz: quux +# question: 42 +# host2.example.com: +# abc: def +# this: that +# darth: vader +# +# Then calling load_vars in Puppet manifest file as follows: +# +# load_vars("/etc/puppet/data.yaml", $fqdn) +# +# Will result in addition of variables $foo, $baz and $question +# for matching host name as per the variable $fqdn ... +# +# Another example which uses per-host file: +# +# Given following content of the file data-host1.example.com.yaml: +# +# --- +# foo: bar +# +# Then when we call load_vars like this: +# +# load_vars("/etc/puppet/data-$fqdn.yaml") +# +# This will result in a variable $foo being added and ready for use. +# + +module Puppet::Parser::Functions + newfunction(:load_vars, :type => :statement) do |arguments| + + raise(Puppet::ParseError, "Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + data = {} + + file = arguments[0] + key = arguments[1] if arguments[1] + + if File.exists?(file) + + begin + data = YAML.load_file(file) + rescue => error + raise(Puppet::ParseError, "Unable to load data " + + "from the file `%s': %s" % file, error.to_s) + end + + raise(Puppet::ParseError, "Data in the file `%s' " + + "is not a hash" % file) unless data.is_a?(Hash) + + data = ((data[key] and data[key].is_a?(Hash)) ? data[key] : {}) if key + end + + data.each { |param, value| setvar(param, strinterp(value)) } + end +end + +# vim: set ts=2 sw=2 et : diff --git a/load_vars.rb b/load_vars.rb deleted file mode 100644 index ddfc0ef..0000000 --- a/load_vars.rb +++ /dev/null @@ -1,72 +0,0 @@ -# -# load_vars.rb -# -# This script will allow for loading variables from an external YAML -# file and expose them for further use inside the Puppet manifest file ... -# -# For example: -# -# Given following content of the data.yaml file: -# -# --- -# host1.example.com: -# foo: bar -# baz: quux -# question: 42 -# host2.example.com: -# abc: def -# this: that -# darth: vader -# -# Then calling load_vars in Puppet manifest file as follows: -# -# load_vars("/etc/puppet/data.yaml", $fqdn) -# -# Will result in addition of variables $foo, $baz and $question -# for matching host name as per the variable $fqdn ... -# -# Another example which uses per-host file: -# -# Given following content of the file data-host1.example.com.yaml: -# -# --- -# foo: bar -# -# Then when we call load_vars like this: -# -# load_vars("/etc/puppet/data-$fqdn.yaml") -# -# This will result in a variable $foo being added and ready for use. -# - -module Puppet::Parser::Functions - newfunction(:load_vars, :type => :statement) do |arguments| - - raise(Puppet::ParseError, "Wrong number of arguments " + - "given (#{arguments.size} for 2)") if arguments.size < 2 - - data = {} - - file = arguments[0] - key = arguments[1] if arguments[1] - - if File.exists?(file) - - begin - data = YAML.load_file(file) - rescue => error - raise(Puppet::ParseError, "Unable to load data " + - "from the file `%s': %s" % file, error.to_s) - end - - raise(Puppet::ParseError, "Data in the file `%s' " + - "is not a hash" % file) unless data.is_a?(Hash) - - data = ((data[key] and data[key].is_a?(Hash)) ? data[key] : {}) if key - end - - data.each { |param, value| setvar(param, strinterp(value)) } - end -end - -# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From a7081fd0277b8e433839f27a075e435ccb560094 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sun, 13 Mar 2011 23:39:43 +0000 Subject: Changing the way how function description and usage was given. Now documentation is exposed properly via Puppet itself. --- load_variables.rb | 83 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/load_variables.rb b/load_variables.rb index ddfc0ef..304bf00 100644 --- a/load_variables.rb +++ b/load_variables.rb @@ -1,46 +1,47 @@ # -# load_vars.rb -# -# This script will allow for loading variables from an external YAML -# file and expose them for further use inside the Puppet manifest file ... -# -# For example: -# -# Given following content of the data.yaml file: -# -# --- -# host1.example.com: -# foo: bar -# baz: quux -# question: 42 -# host2.example.com: -# abc: def -# this: that -# darth: vader -# -# Then calling load_vars in Puppet manifest file as follows: -# -# load_vars("/etc/puppet/data.yaml", $fqdn) -# -# Will result in addition of variables $foo, $baz and $question -# for matching host name as per the variable $fqdn ... -# -# Another example which uses per-host file: -# -# Given following content of the file data-host1.example.com.yaml: -# -# --- -# foo: bar -# -# Then when we call load_vars like this: -# -# load_vars("/etc/puppet/data-$fqdn.yaml") -# -# This will result in a variable $foo being added and ready for use. +# load_variables.rb # module Puppet::Parser::Functions - newfunction(:load_vars, :type => :statement) do |arguments| + newfunction(:load_variables, :type => :statement, :doc => <<-EOS +This function will allow for loading variables from an external YAML +file and expose them for further use inside the Puppet manifest file ... + +For example: + +Given following content of the data.yaml file: + + --- + host1.example.com: + foo: bar + baz: quux + question: 42 + host2.example.com: + abc: def + this: that + darth: vader + +Then calling load_variables in Puppet manifest file as follows: + + load_variables("/etc/puppet/data.yaml", $fqdn) + +Will result in addition of variables $foo, $baz and $question +for matching host name as per the variable $fqdn ... + +Another example which uses per-host file: + +Given following content of the file data-host1.example.com.yaml: + + --- + foo: bar + +Then when we call load_variables like this: + + load_variables("/etc/puppet/data-$fqdn.yaml") + +This will result in a variable $foo being added and ready for use. + EOS + ) do |arguments| raise(Puppet::ParseError, "Wrong number of arguments " + "given (#{arguments.size} for 2)") if arguments.size < 2 @@ -66,7 +67,7 @@ module Puppet::Parser::Functions end data.each { |param, value| setvar(param, strinterp(value)) } - end -end + end # def newfunction +end # module Puppet::Parser::Functions # vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 8e6bf2a0799476b5e29e98b4abe073f91127a4e4 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sun, 13 Mar 2011 23:56:44 +0000 Subject: First version. --- persistent_crontab_minutes.rb | 56 +++++++++++++++++++++++++++++++++++++++++++ unique_crontab_minutes.rb | 29 ++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 persistent_crontab_minutes.rb create mode 100644 unique_crontab_minutes.rb diff --git a/persistent_crontab_minutes.rb b/persistent_crontab_minutes.rb new file mode 100644 index 0000000..44c6328 --- /dev/null +++ b/persistent_crontab_minutes.rb @@ -0,0 +1,56 @@ +# +# persistent_crontab_minutes.rb +# + +require 'md5' + +module Puppet::Parser::Functions + newfunction(:persistent_crontab_minutes, :type => :rvalue) do |arguments| + + raise(Puppet::ParseError, "Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + value = 0 + + job = arguments[0] + host = arguments[1] + + environment = Puppet[:environment] + + # We select first directory that exists. This might not be the best idea ... + modules = Puppet[:modulepath].split(':').select { |i| File.exists?(i) }.first + + raise(Puppet::ParseError, "Unable to determine the storage " + + "directory for Puppet modules") unless modules + + # Prepare the file where we store current value ... + file = "/puppet/state/crontab/#{host}-#{job}.minutes" + file = File.join(modules, file) + + if FileTest.exists?(file) + File.open(file, 'r') { |f| value = f.read.to_i } + + raise(Puppet::ParseError, "The value for minutes in the file `%s' " + + "is out of the range from 0 to 59 inclusive") unless value < 60 + else + # + # Pick a random number based on the job and host name. This will yield + # the same value for exactly the same combination of the job and host name. + # + value = MD5.new(job_name + host).to_s.hex % 60 + + # Minutes are from 0 to 59 inclusive ... + value = value < 60 ? value : 59 + + File.open(file, 'w') { |f| f.write(value) } + end + + # Tell Puppet to keep an eye on this file ... + parser = Puppet::Parser::Parser.new(environment) + parser.watch_file(file) if File.exists?(file) + + return value + end # def newfunction +end # module Puppet::Parser::Functions + +# vim: set ts=2 sw=2 et : diff --git a/unique_crontab_minutes.rb b/unique_crontab_minutes.rb new file mode 100644 index 0000000..b3fb544 --- /dev/null +++ b/unique_crontab_minutes.rb @@ -0,0 +1,29 @@ +# +# random_crontab_minutes.rb +# + +require 'md5' + +module Puppet::Parser::Functions + newfunction(:random_crontab_minutes, :type => :rvalue) do |arguments| + + raise(Puppet::ParseError, "Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + job_name = arguments[0] + host = arguments[1] + + # + # Pick a random number based on the job and host name. This will yield + # the same value for exactly the same combination of the job and host name. + # + value = MD5.new(job_name + host).to_s.hex % 60 + + # Minutes are from 0 to 59 inclusive ... + value = value < 60 ? value : 59 + + return value + end # def newfunction +end # module Puppet::Parser::Functions + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From d629e15f29556c9352157339d6d8cdb00e6a0b02 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Mon, 14 Mar 2011 00:04:08 +0000 Subject: Adding README file ... --- README | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 README diff --git a/README b/README new file mode 100644 index 0000000..c3a69c3 --- /dev/null +++ b/README @@ -0,0 +1,3 @@ +Various functions to use within Puppet will be stored here. Most of them +are to be run by Puppet in order to extend its Domain Specific Language (DSL) +abilities and functionality. -- cgit v1.2.3 From c3ed006be2e2cbd22c0e24dd0f35e96a02e93dc5 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Mon, 14 Mar 2011 13:13:33 +0100 Subject: Changing the name of the file to reflect change in the function name. --- random_crontab_minutes.rb | 29 +++++++++++++++++++++++++++++ unique_crontab_minutes.rb | 29 ----------------------------- 2 files changed, 29 insertions(+), 29 deletions(-) create mode 100644 random_crontab_minutes.rb delete mode 100644 unique_crontab_minutes.rb diff --git a/random_crontab_minutes.rb b/random_crontab_minutes.rb new file mode 100644 index 0000000..b3fb544 --- /dev/null +++ b/random_crontab_minutes.rb @@ -0,0 +1,29 @@ +# +# random_crontab_minutes.rb +# + +require 'md5' + +module Puppet::Parser::Functions + newfunction(:random_crontab_minutes, :type => :rvalue) do |arguments| + + raise(Puppet::ParseError, "Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + job_name = arguments[0] + host = arguments[1] + + # + # Pick a random number based on the job and host name. This will yield + # the same value for exactly the same combination of the job and host name. + # + value = MD5.new(job_name + host).to_s.hex % 60 + + # Minutes are from 0 to 59 inclusive ... + value = value < 60 ? value : 59 + + return value + end # def newfunction +end # module Puppet::Parser::Functions + +# vim: set ts=2 sw=2 et : diff --git a/unique_crontab_minutes.rb b/unique_crontab_minutes.rb deleted file mode 100644 index b3fb544..0000000 --- a/unique_crontab_minutes.rb +++ /dev/null @@ -1,29 +0,0 @@ -# -# random_crontab_minutes.rb -# - -require 'md5' - -module Puppet::Parser::Functions - newfunction(:random_crontab_minutes, :type => :rvalue) do |arguments| - - raise(Puppet::ParseError, "Wrong number of arguments " + - "given (#{arguments.size} for 2)") if arguments.size < 2 - - job_name = arguments[0] - host = arguments[1] - - # - # Pick a random number based on the job and host name. This will yield - # the same value for exactly the same combination of the job and host name. - # - value = MD5.new(job_name + host).to_s.hex % 60 - - # Minutes are from 0 to 59 inclusive ... - value = value < 60 ? value : 59 - - return value - end # def newfunction -end # module Puppet::Parser::Functions - -# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From e1bb7393bb5e73afc1813bfe1c27f38c227a9dc3 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Wed, 23 Mar 2011 02:17:32 +0000 Subject: Create destination directory on-demand i.e. when missing ... Signed-off-by: Krzsysztof Wilczynski --- persistent_crontab_minutes.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/persistent_crontab_minutes.rb b/persistent_crontab_minutes.rb index 44c6328..5a76868 100644 --- a/persistent_crontab_minutes.rb +++ b/persistent_crontab_minutes.rb @@ -27,6 +27,11 @@ module Puppet::Parser::Functions file = "/puppet/state/crontab/#{host}-#{job}.minutes" file = File.join(modules, file) + # Get the directory portion from the file name ... + directory = File.dirname(file) + + FileUtils.mkdir_p(directory) unless File.directory?(directory) + if FileTest.exists?(file) File.open(file, 'r') { |f| value = f.read.to_i } -- cgit v1.2.3 From 55c94178ebec564cb27a1394a2fa0a970bf03621 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 23 Apr 2011 01:18:52 +0100 Subject: Minor changes. Added placeholder for :doc in Puppet's newfunction to fill later. --- load_variables.rb | 4 ++-- persistent_crontab_minutes.rb | 12 +++++++----- random_crontab_minutes.rb | 12 +++++++----- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/load_variables.rb b/load_variables.rb index 304bf00..a6ccc60 100644 --- a/load_variables.rb +++ b/load_variables.rb @@ -67,7 +67,7 @@ This will result in a variable $foo being added and ready for use. end data.each { |param, value| setvar(param, strinterp(value)) } - end # def newfunction -end # module Puppet::Parser::Functions + end +end # vim: set ts=2 sw=2 et : diff --git a/persistent_crontab_minutes.rb b/persistent_crontab_minutes.rb index 5a76868..cd80094 100644 --- a/persistent_crontab_minutes.rb +++ b/persistent_crontab_minutes.rb @@ -2,14 +2,16 @@ # persistent_crontab_minutes.rb # -require 'md5' - module Puppet::Parser::Functions - newfunction(:persistent_crontab_minutes, :type => :rvalue) do |arguments| + newfunction(:persistent_crontab_minutes, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| raise(Puppet::ParseError, "Wrong number of arguments " + "given (#{arguments.size} for 2)") if arguments.size < 2 + require 'md5' + value = 0 job = arguments[0] @@ -55,7 +57,7 @@ module Puppet::Parser::Functions parser.watch_file(file) if File.exists?(file) return value - end # def newfunction -end # module Puppet::Parser::Functions + end +end # vim: set ts=2 sw=2 et : diff --git a/random_crontab_minutes.rb b/random_crontab_minutes.rb index b3fb544..8ab29e1 100644 --- a/random_crontab_minutes.rb +++ b/random_crontab_minutes.rb @@ -2,14 +2,16 @@ # random_crontab_minutes.rb # -require 'md5' - module Puppet::Parser::Functions - newfunction(:random_crontab_minutes, :type => :rvalue) do |arguments| + newfunction(:random_crontab_minutes, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| raise(Puppet::ParseError, "Wrong number of arguments " + "given (#{arguments.size} for 2)") if arguments.size < 2 + require 'md5' + job_name = arguments[0] host = arguments[1] @@ -23,7 +25,7 @@ module Puppet::Parser::Functions value = value < 60 ? value : 59 return value - end # def newfunction -end # module Puppet::Parser::Functions + end +end # vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 399dd8cca8d527e40558cbb44eab1c1b5111d6c3 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 23 Apr 2011 01:19:43 +0100 Subject: First version. A function for Puppet that allows to retrieve a fact from Facter easily instead of resorting to awful "inline_template" use-and-abuse practices. Signed-off-by: Krzysztof Wilczynski --- fact.rb | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 fact.rb diff --git a/fact.rb b/fact.rb new file mode 100644 index 0000000..7d170c8 --- /dev/null +++ b/fact.rb @@ -0,0 +1,63 @@ +# +# fact.rb +# + +module Puppet::Parser::Functions + newfunction(:fact, :type => :rvalue, :doc => <<-EOS +This function will retrieve fact from Facter based on the fact name +and expose it for further use within Puppet manifest file ... + +For example: + +Given the following sample manifest: + + define partitions { + $result = split(fact("partitions_${name}"), ',') + + notice $result + + partition { $result: } + } + + define partition { + notice $name + } + + $available_disks = split($disks, ',') + + partitions { $available_disks: } + +This will produce the following: + + notice: Scope(Partitions[hda]): hda1 hda2 + notice: Scope(Partition[hda1]): hda1 + notice: Scope(Partition[hda2]): hda2 + + +Which allows you to avoid resorting to the following: + + $fact = "partitions_${name}" + $result = split(inline_template("<%= scope.lookupvar(fact) %>"), ',') + +Taking out the need for using "inline_template" in the "partitions" define above. + EOS + ) do |arguments| + + raise(Puppet::ParseError, "Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + fact = arguments[0] + + raise(Puppet::ParseError, 'You must provide fact name') if fact.empty? + + result = lookupvar(fact) # Get the value of interest from Facter ... + + if not result or result.empty? + raise(Puppet::ParseError, "Unable to retrieve fact `#{fact}'") + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 54b0d37bacb8b95f1b22aa97bd958ed798da9342 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 23 Apr 2011 01:42:21 +0100 Subject: We do not need cronrand.rb any more. Signed-off-by: Krzysztof Wilczynski --- cronrand.rb | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 cronrand.rb diff --git a/cronrand.rb b/cronrand.rb deleted file mode 100644 index 3dd6810..0000000 --- a/cronrand.rb +++ /dev/null @@ -1,31 +0,0 @@ -# vim: set ts=2 sw=2 et : -# TODO: path should not be hardcoded here -# -# USAGE: -# $minutes = cronrand("puppet-run", $fqdn, 59) -# file { "puppet-cron": -# name => /etc/cron.d/puppet-run", -# content => "$minutes * * * * root /usr/sbin/puppetd --onetime --no-daemonize --logdest syslog > /dev/null 2>&1\n" -# } -# --- -# minutes will be chosen random and saved for each $fqdn, -# second puppet run on same host will create same content as first one. - -module Puppet::Parser::Functions - newfunction(:cronrand, :type => :rvalue) do |args| - job = args[0] - host = args[1] - minutes = (args[2].to_i < 60) ? args[2].to_i : 59 - filename = "/etc/puppet/modules/puppet/state/cronminutes-#{job}-#{host}" - value = 0 - - if FileTest.exists?(filename) - File.open(filename, 'r') { |fd| value = fd.gets.chomp.to_i } - else - value = rand(minutes) - File.open(filename, 'w') { |fd| fd.puts value } - end - value - end -end - -- cgit v1.2.3 From 0b1aab2ed584aa79c14531e5c3d1b0c8a6c00511 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 23 Apr 2011 01:56:30 +0100 Subject: Small changes. Mainly to formatting of the help provided. Signed-off-by: Krzysztof Wilczynski --- fact.rb | 37 +++++++++++++++++++------------------ load_variables.rb | 26 +++++++++++++------------- 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/fact.rb b/fact.rb index 7d170c8..95213cc 100644 --- a/fact.rb +++ b/fact.rb @@ -4,42 +4,43 @@ module Puppet::Parser::Functions newfunction(:fact, :type => :rvalue, :doc => <<-EOS -This function will retrieve fact from Facter based on the fact name -and expose it for further use within Puppet manifest file ... +This function will retrieve fact from Facter based on the fact +name and expose it for further use within Puppet manifest file ... For example: Given the following sample manifest: - define partitions { - $result = split(fact("partitions_${name}"), ',') + define partitions { + $result = split(fact("partitions_${name}"), ',') - notice $result + notice $result - partition { $result: } - } + partition { $result: } + } - define partition { - notice $name - } + define partition { + notice $name + } - $available_disks = split($disks, ',') + $available_disks = split($disks, ',') - partitions { $available_disks: } + partitions { $available_disks: } This will produce the following: - notice: Scope(Partitions[hda]): hda1 hda2 - notice: Scope(Partition[hda1]): hda1 - notice: Scope(Partition[hda2]): hda2 + notice: Scope(Partitions[hda]): hda1 hda2 + notice: Scope(Partition[hda1]): hda1 + notice: Scope(Partition[hda2]): hda2 Which allows you to avoid resorting to the following: - $fact = "partitions_${name}" - $result = split(inline_template("<%= scope.lookupvar(fact) %>"), ',') + $fact = "partitions_${name}" + $result = split(inline_template("<%= scope.lookupvar(fact) %>"), ',') -Taking out the need for using "inline_template" in the "partitions" define above. +Phasing out the need for use and abuse of the infamous inline_template in the +partitions define given above. EOS ) do |arguments| diff --git a/load_variables.rb b/load_variables.rb index a6ccc60..a5eb269 100644 --- a/load_variables.rb +++ b/load_variables.rb @@ -11,19 +11,19 @@ For example: Given following content of the data.yaml file: - --- - host1.example.com: - foo: bar - baz: quux - question: 42 - host2.example.com: - abc: def - this: that - darth: vader + --- + host1.example.com: + foo: bar + baz: quux + question: 42 + host2.example.com: + abc: def + this: that + darth: vader Then calling load_variables in Puppet manifest file as follows: - load_variables("/etc/puppet/data.yaml", $fqdn) + load_variables("/etc/puppet/data.yaml", $fqdn) Will result in addition of variables $foo, $baz and $question for matching host name as per the variable $fqdn ... @@ -32,12 +32,12 @@ Another example which uses per-host file: Given following content of the file data-host1.example.com.yaml: - --- - foo: bar + --- + foo: bar Then when we call load_variables like this: - load_variables("/etc/puppet/data-$fqdn.yaml") + load_variables("/etc/puppet/data-$fqdn.yaml") This will result in a variable $foo being added and ready for use. EOS -- cgit v1.2.3 From 4a57da669c19a972968b8a88e09058844a52ba64 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 23 Apr 2011 02:55:08 +0100 Subject: Fix. It is better to promote good practice... Signed-off-by: Krzysztof Wilczynski --- load_variables.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/load_variables.rb b/load_variables.rb index a5eb269..95dcc90 100644 --- a/load_variables.rb +++ b/load_variables.rb @@ -37,7 +37,7 @@ Given following content of the file data-host1.example.com.yaml: Then when we call load_variables like this: - load_variables("/etc/puppet/data-$fqdn.yaml") + load_variables("/etc/puppet/data-${fqdn}.yaml") This will result in a variable $foo being added and ready for use. EOS -- cgit v1.2.3 From 92d4a4eb85d72253ee6310b44dd37aa71478ce11 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 23 Apr 2011 16:43:11 +0100 Subject: First version. Adding join to Puppet for use within the manifest files. Signed-off-by: Krzysztof Wilczynski --- join.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 join.rb diff --git a/join.rb b/join.rb new file mode 100644 index 0000000..b616e05 --- /dev/null +++ b/join.rb @@ -0,0 +1,25 @@ +# +# join.rb +# + +module Puppet::Parser::Functions + newfunction(:join, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + array = arguments[0] + + suffix = arguments[1] + prefix = arguments[2] + + if prefix and not prefix.empty? + result = prefix + array.join(suffix + prefix) + else + result = array.join(suffix) + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 908459c1ad7a14d338239e9ba428c56bbffee74c Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 23 Apr 2011 17:45:49 +0100 Subject: First version. Function that allows to collect selected indices from an array within Puppet manifest. More or less how array slicing works in Perl ... Signed-off-by: Krzysztof Wilczynski --- collect_indices.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 collect_indices.rb diff --git a/collect_indices.rb b/collect_indices.rb new file mode 100644 index 0000000..65abfce --- /dev/null +++ b/collect_indices.rb @@ -0,0 +1,27 @@ +# +# collect_indices.rb +# + +module Puppet::Parser::Functions + newfunction(:collect_indices, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + array = arguments.shift + indices = *arguments # Get them all ... Pokemon ... + + if not indices or indices.empty? + raise(Puppet::ParseError, 'You must provide indices to collect') + end + + # In Puppet numbers are often string-encoded ... + array = indices.collect { |i| array[i.to_i] }.compact + + return array + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From fa68d78b200befb286b3e18415dd7f820369f5e5 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 23 Apr 2011 18:11:08 +0100 Subject: Small changes. Added better error checking etc ... Signed-off-by: Krzysztof Wilczynski --- collect_indices.rb | 7 ++++++- join.rb | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/collect_indices.rb b/collect_indices.rb index 65abfce..e96c3e9 100644 --- a/collect_indices.rb +++ b/collect_indices.rb @@ -10,7 +10,12 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, "Wrong number of arguments " + "given (#{arguments.size} for 2)") if arguments.size < 2 - array = arguments.shift + array = arguments.shift + + if not array.is_a?(Array) + raise(Puppet::ParseError, 'Requires an array to work with') + end + indices = *arguments # Get them all ... Pokemon ... if not indices or indices.empty? diff --git a/join.rb b/join.rb index b616e05..4522d23 100644 --- a/join.rb +++ b/join.rb @@ -7,11 +7,26 @@ module Puppet::Parser::Functions EOS ) do |arguments| + # Technically we support three arguments but only first two are mandatory .... + raise(Puppet::ParseError, "Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + array = arguments[0] + if not array.is_a?(Array) + raise(Puppet::ParseError, 'Requires an array to work with') + end + suffix = arguments[1] prefix = arguments[2] + raise(Puppet::ParseError, 'You must provide suffix ' + + 'to join array elements with') if suffix.empty? + + if prefix and prefix.empty? + raise(Puppet::ParseError, 'You must provide prefix to add to join') + end + if prefix and not prefix.empty? result = prefix + array.join(suffix + prefix) else -- cgit v1.2.3 From eefd7a9fc4951b523419b207ea197a481878745d Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sun, 24 Apr 2011 01:35:49 +0100 Subject: Adding ability to specifiy range as the index when selecting indices to collect. Signed-off-by: Krzysztof Wilczynski --- collect_indices.rb | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/collect_indices.rb b/collect_indices.rb index e96c3e9..cd087b9 100644 --- a/collect_indices.rb +++ b/collect_indices.rb @@ -22,8 +22,28 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, 'You must provide indices to collect') end - # In Puppet numbers are often string-encoded ... - array = indices.collect { |i| array[i.to_i] }.compact + indices_list = [] + + indices.each do |i| + if m = i.match(/^(\d+)\-(\d+)$/) + start = m[1].to_i + stop = m[2].to_i + + raise(Puppet::ParseError, 'Stop index in given indices range ' + + 'is smaller than the start index') if start > stop + + (start .. stop).each { |i| indices_list << i.to_i } + else + if not i.match(/^\w+$/) + raise(Puppet::ParseError, 'Unknown format of given index') + end + + # In Puppet numbers are often string-encoded ... + indices_list << i.to_i + end + end + + array = indices_list.collect { |i| array[i] }.compact return array end -- cgit v1.2.3 From 4a72b0efde7069f76b142cc2349f4ec60001a50d Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sun, 24 Apr 2011 20:55:35 +0100 Subject: Adding more error checking ... Signed-off-by: Krzysztof Wilczynski --- collect_indices.rb | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/collect_indices.rb b/collect_indices.rb index cd087b9..8d981cb 100644 --- a/collect_indices.rb +++ b/collect_indices.rb @@ -10,7 +10,8 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, "Wrong number of arguments " + "given (#{arguments.size} for 2)") if arguments.size < 2 - array = arguments.shift + array = arguments.shift + array_size = array.size if not array.is_a?(Array) raise(Puppet::ParseError, 'Requires an array to work with') @@ -29,17 +30,29 @@ module Puppet::Parser::Functions start = m[1].to_i stop = m[2].to_i - raise(Puppet::ParseError, 'Stop index in given indices range ' + - 'is smaller than the start index') if start > stop + if start > stop + raise(Puppet::ParseError, 'Stop index in given indices range ' + + 'is smaller than the start index') + elsif stop > array_size - 1 # First element is at index 0 is it not? + raise(Puppet::ParseError, 'Stop index in given indices range ' + + 'exceeds array size') + end (start .. stop).each { |i| indices_list << i.to_i } else - if not i.match(/^\w+$/) + # Only positive numbers allowed ... + if not i.match(/^\d+$/) raise(Puppet::ParseError, 'Unknown format of given index') end # In Puppet numbers are often string-encoded ... - indices_list << i.to_i + i = i.to_i + + if i > array_size - 1 # Same story. First element is at index 0 ... + raise(Puppet::ParseError, 'Given index exceeds array size') + end + + indices_list << i end end -- cgit v1.2.3 From 61936fdeaaadb25e83ccb766e6b9ac872527a50d Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Mon, 25 Apr 2011 02:02:00 +0100 Subject: Updated error check and reporting. Also we now return empty string value i.e. "" instead of raising an exception when a particular fact is not present. We also make use of strinterp() explicitly to evaluate arguments passed to the function. Signed-off-by: Krzysztof Wilczynski --- fact.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/fact.rb b/fact.rb index 95213cc..2c22f60 100644 --- a/fact.rb +++ b/fact.rb @@ -44,17 +44,25 @@ partitions define given above. EOS ) do |arguments| - raise(Puppet::ParseError, "Wrong number of arguments " + + raise(Puppet::ParseError, "fact(): Wrong number of arguments " + "given (#{arguments.size} for 1)") if arguments.size < 1 fact = arguments[0] - raise(Puppet::ParseError, 'You must provide fact name') if fact.empty? + raise(Puppet::ParseError, 'fact(): You must provide ' + + 'fact name') if fact.empty? + fact = strinterp(fact) # Evaluate any interpolated variable names ... result = lookupvar(fact) # Get the value of interest from Facter ... if not result or result.empty? - raise(Puppet::ParseError, "Unable to retrieve fact `#{fact}'") + # + # Now this is a funny one ... Puppet does not have a concept of + # returning neither undef nor nil back for use within the Puppet DSL + # and empty string is as closest to actual undef as you we can get + # at this point in time ... + # + result = '' end return result -- cgit v1.2.3 From a64c20670818ad462f19325f6a2888b07b2f0a75 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Mon, 25 Apr 2011 02:06:50 +0100 Subject: Update to error reporting. Signed-off-by: Krzysztof Wilczynski --- collect_indices.rb | 24 ++++++++++++++---------- join.rb | 13 +++++++------ 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/collect_indices.rb b/collect_indices.rb index 8d981cb..39cf54a 100644 --- a/collect_indices.rb +++ b/collect_indices.rb @@ -7,20 +7,22 @@ module Puppet::Parser::Functions EOS ) do |arguments| - raise(Puppet::ParseError, "Wrong number of arguments " + - "given (#{arguments.size} for 2)") if arguments.size < 2 + raise(Puppet::ParseError, "collect_indices(): Wrong number of " + + "arguments given (#{arguments.size} for 2)") if arguments.size < 2 array = arguments.shift array_size = array.size if not array.is_a?(Array) - raise(Puppet::ParseError, 'Requires an array to work with') + raise(Puppet::ParseError, 'collect_indices(): Requires an array ' + + 'to work with') end indices = *arguments # Get them all ... Pokemon ... if not indices or indices.empty? - raise(Puppet::ParseError, 'You must provide indices to collect') + raise(Puppet::ParseError, 'collect_indices(): You must provide ' + + 'indices to collect') end indices_list = [] @@ -31,25 +33,27 @@ module Puppet::Parser::Functions stop = m[2].to_i if start > stop - raise(Puppet::ParseError, 'Stop index in given indices range ' + - 'is smaller than the start index') + raise(Puppet::ParseError, 'collect_indices(): Stop index in ' + + 'given indices range is smaller than the start index') elsif stop > array_size - 1 # First element is at index 0 is it not? - raise(Puppet::ParseError, 'Stop index in given indices range ' + - 'exceeds array size') + raise(Puppet::ParseError, 'collect_indices(): Stop index in ' + + 'given indices range exceeds array size') end (start .. stop).each { |i| indices_list << i.to_i } else # Only positive numbers allowed ... if not i.match(/^\d+$/) - raise(Puppet::ParseError, 'Unknown format of given index') + raise(Puppet::ParseError, 'collect_indices(): Unknown format ' + + 'of given index') end # In Puppet numbers are often string-encoded ... i = i.to_i if i > array_size - 1 # Same story. First element is at index 0 ... - raise(Puppet::ParseError, 'Given index exceeds array size') + raise(Puppet::ParseError, 'collect_indices(): Given index ' + + 'exceeds array size') end indices_list << i diff --git a/join.rb b/join.rb index 4522d23..1d2e55d 100644 --- a/join.rb +++ b/join.rb @@ -7,24 +7,25 @@ module Puppet::Parser::Functions EOS ) do |arguments| - # Technically we support three arguments but only first two are mandatory .... - raise(Puppet::ParseError, "Wrong number of arguments " + + # Technically we support three arguments but only first two are mandatory ... + raise(Puppet::ParseError, "join(): Wrong number of arguments " + "given (#{arguments.size} for 2)") if arguments.size < 2 array = arguments[0] if not array.is_a?(Array) - raise(Puppet::ParseError, 'Requires an array to work with') + raise(Puppet::ParseError, 'join(): Requires an array to work with') end suffix = arguments[1] - prefix = arguments[2] + prefix = arguments[2] if arguments[2] - raise(Puppet::ParseError, 'You must provide suffix ' + + raise(Puppet::ParseError, 'join(): You must provide suffix ' + 'to join array elements with') if suffix.empty? if prefix and prefix.empty? - raise(Puppet::ParseError, 'You must provide prefix to add to join') + raise(Puppet::ParseError, 'join(): You must provide prefix ' + + 'to add to join') end if prefix and not prefix.empty? -- cgit v1.2.3 From bb231bdfc2acca172203039d6a1e4852ab4809b0 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Mon, 25 Apr 2011 02:44:20 +0100 Subject: Updated help message. Signed-off-by: Krzysztof Wilczynski --- fact.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/fact.rb b/fact.rb index 2c22f60..806c330 100644 --- a/fact.rb +++ b/fact.rb @@ -33,14 +33,13 @@ This will produce the following: notice: Scope(Partition[hda1]): hda1 notice: Scope(Partition[hda2]): hda2 - Which allows you to avoid resorting to the following: $fact = "partitions_${name}" $result = split(inline_template("<%= scope.lookupvar(fact) %>"), ',') -Phasing out the need for use and abuse of the infamous inline_template in the -partitions define given above. +Phasing out the need for use and abuse of the infamous inline_template +in the partitions define given above. EOS ) do |arguments| -- cgit v1.2.3 From 914d5c2f38dd5c19d1b222c47ca6ba680277f6d6 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Mon, 25 Apr 2011 02:47:16 +0100 Subject: Added help accessible via the :doc functionality in Puppet's newfunction. Signed-off-by: Krzysztof Wilczynski --- join.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/join.rb b/join.rb index 1d2e55d..469763a 100644 --- a/join.rb +++ b/join.rb @@ -4,6 +4,39 @@ module Puppet::Parser::Functions newfunction(:join, :type => :rvalue, :doc => <<-EOS +This function will allow to concatenate elements of an array together +with a given suffix and optionally with prefix if such is given ... + +For example: + +Given the following sample manifest: + + define iterator { + notice $name + } + + $array = ['a', 'b', 'c'] + + $result = split(join($array, ',', 'letter_'), ',') + + notice $result + + iterator { $result: } + +This will produce the following: + + notice: Scope(Class[main]): letter_a letter_b letter_c + notice: Scope(Iterator[letter_a]): letter_a + notice: Scope(Iterator[letter_b]): letter_b + notice: Scope(Iterator[letter_c]): letter_c + +Which allows you to avoid resorting to the following: + + $result = split(inline_template("<%= array.collect { |i| \"letter_#{i}\" }.join(',') %>"), ',') + +Phasing out the need for use and abuse of the infamous inline_template +in the example above and which in this very case is extremely ugly as +we have to escape double-quotes to make Puppet parser not evaluate them. EOS ) do |arguments| -- cgit v1.2.3 From 87a825e6094ead24da36b64937bbd24ccd9d7d39 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Mon, 25 Apr 2011 03:19:10 +0100 Subject: First version. Improvment upon bool2num function found on the Internet. Signed-off-by: Krzysztof Wilczynski --- bool2num.rb | 36 ++++++++++++++++++++++++++++++++++++ load_variables.rb | 14 +++++++++----- 2 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 bool2num.rb diff --git a/bool2num.rb b/bool2num.rb new file mode 100644 index 0000000..c197b17 --- /dev/null +++ b/bool2num.rb @@ -0,0 +1,36 @@ +# +# bool2num.rb +# + +module Puppet::Parser::Functions + newfunction(:bool2num, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "bool2num(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + boolean = arguments[0] + + result = case boolean + # + # This is how undef looks like in Puppet ... + # We yield 0 (or false if you wish) in this case. + # + when /^$/, '' then '0' + when /^(1|t|true)$/ then '1' + when /^(0|f|false)$/ then '0' + # This is not likely to happen ... + when /^(undef|undefined)$/ then '0' + # We may get real boolean values as well ... + when true then '1' + when false then '0' + else + raise(Puppet::ParseError, 'bool2num(): Unknown type of boolean given') + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/load_variables.rb b/load_variables.rb index 95dcc90..a28c64b 100644 --- a/load_variables.rb +++ b/load_variables.rb @@ -43,8 +43,8 @@ This will result in a variable $foo being added and ready for use. EOS ) do |arguments| - raise(Puppet::ParseError, "Wrong number of arguments " + - "given (#{arguments.size} for 2)") if arguments.size < 2 + raise(Puppet::ParseError, "load_variables(): Wrong number of " + + "arguments given (#{arguments.size} for 2)") if arguments.size < 2 data = {} @@ -56,17 +56,21 @@ This will result in a variable $foo being added and ready for use. begin data = YAML.load_file(file) rescue => error - raise(Puppet::ParseError, "Unable to load data " + + raise(Puppet::ParseError, "load_variables(): Unable to load data " + "from the file `%s': %s" % file, error.to_s) end - raise(Puppet::ParseError, "Data in the file `%s' " + + raise(Puppet::ParseError, "load_variables(): Data in the file `%s' " + "is not a hash" % file) unless data.is_a?(Hash) data = ((data[key] and data[key].is_a?(Hash)) ? data[key] : {}) if key end - data.each { |param, value| setvar(param, strinterp(value)) } + data.each do |param, value| + value = strinterp(value) # Evaluate any interpolated variable names ... + + setvar(param, value) + end end end -- cgit v1.2.3 From 1a8617404c393c9efdbebe9d1d9b5c9c54e536de Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Mon, 25 Apr 2011 03:46:46 +0100 Subject: Minor update. Signed-off-by: Krzysztof Wilczynski --- collect_indices.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/collect_indices.rb b/collect_indices.rb index 39cf54a..947fb89 100644 --- a/collect_indices.rb +++ b/collect_indices.rb @@ -42,7 +42,7 @@ module Puppet::Parser::Functions (start .. stop).each { |i| indices_list << i.to_i } else - # Only positive numbers allowed ... + # Only positive numbers allowed in this case ... if not i.match(/^\d+$/) raise(Puppet::ParseError, 'collect_indices(): Unknown format ' + 'of given index') -- cgit v1.2.3 From 7e7c7ce2ee176017620987c36e4dd5abc2dd304b Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Mon, 25 Apr 2011 03:47:31 +0100 Subject: First version. Function opposite to the bool2num one. Converts number to appropriate boolean value. Signed-off-by: Krzysztof Wilczynski --- num2bool.rb | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 num2bool.rb diff --git a/num2bool.rb b/num2bool.rb new file mode 100644 index 0000000..0e6b7a5 --- /dev/null +++ b/num2bool.rb @@ -0,0 +1,36 @@ +# +# num2bool.rb +# + +module Puppet::Parser::Functions + newfunction(:num2bool, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "num2bool(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + number = arguments[0] + + # Only numbers allowed ... + if not number.match(/^\-?\d+$/) + raise(Puppet::ParseError, 'num2bool(): Requires a number to work with') + end + + result = case number + when /^0$/ + false + when /^\-?\d+$/ + # In Puppet numbers are often string-encoded ... + number = number.to_i + # We yield true for any positive number and false otherwise ... + number > 0 ? true : false + else + raise(Puppet::ParseError, 'num2bool(): Unknown number format given') + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 50cb2cd05afb44b02eda90be95139c01903f4090 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Mon, 25 Apr 2011 23:32:41 +0100 Subject: Small re-factor of fact function. Signed-off-by: Krzysztof Wilczynski --- fact.rb | 56 +++++++++++--------------------------------------------- 1 file changed, 11 insertions(+), 45 deletions(-) diff --git a/fact.rb b/fact.rb index 806c330..9522293 100644 --- a/fact.rb +++ b/fact.rb @@ -4,42 +4,6 @@ module Puppet::Parser::Functions newfunction(:fact, :type => :rvalue, :doc => <<-EOS -This function will retrieve fact from Facter based on the fact -name and expose it for further use within Puppet manifest file ... - -For example: - -Given the following sample manifest: - - define partitions { - $result = split(fact("partitions_${name}"), ',') - - notice $result - - partition { $result: } - } - - define partition { - notice $name - } - - $available_disks = split($disks, ',') - - partitions { $available_disks: } - -This will produce the following: - - notice: Scope(Partitions[hda]): hda1 hda2 - notice: Scope(Partition[hda1]): hda1 - notice: Scope(Partition[hda2]): hda2 - -Which allows you to avoid resorting to the following: - - $fact = "partitions_${name}" - $result = split(inline_template("<%= scope.lookupvar(fact) %>"), ',') - -Phasing out the need for use and abuse of the infamous inline_template -in the partitions define given above. EOS ) do |arguments| @@ -54,18 +18,20 @@ in the partitions define given above. fact = strinterp(fact) # Evaluate any interpolated variable names ... result = lookupvar(fact) # Get the value of interest from Facter ... - if not result or result.empty? - # - # Now this is a funny one ... Puppet does not have a concept of - # returning neither undef nor nil back for use within the Puppet DSL - # and empty string is as closest to actual undef as you we can get - # at this point in time ... - # - result = '' - end + # + # Now this is a funny one ... Puppet does not have a concept of + # returning neither undef nor nil back for use within the Puppet DSL + # and empty string is as closest to actual undef as you we can get + # at this point in time ... + # + result = (not result or result.empty?) ? '' : result return result end end # vim: set ts=2 sw=2 et : + +notice fact('interfaces') +notice fact('xyz') +notice fact('') -- cgit v1.2.3 From 28254bef6baa6ac9c1c26a2c58bdfd0548c7a553 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Mon, 25 Apr 2011 23:33:26 +0100 Subject: Changing name of join.rb to join_with_prefix.rb to make room for its simpler version. Signed-off-by: Krzysztof Wilczynski --- join.rb | 74 ----------------------------------------------------- join_with_prefix.rb | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 74 deletions(-) delete mode 100644 join.rb create mode 100644 join_with_prefix.rb diff --git a/join.rb b/join.rb deleted file mode 100644 index 469763a..0000000 --- a/join.rb +++ /dev/null @@ -1,74 +0,0 @@ -# -# join.rb -# - -module Puppet::Parser::Functions - newfunction(:join, :type => :rvalue, :doc => <<-EOS -This function will allow to concatenate elements of an array together -with a given suffix and optionally with prefix if such is given ... - -For example: - -Given the following sample manifest: - - define iterator { - notice $name - } - - $array = ['a', 'b', 'c'] - - $result = split(join($array, ',', 'letter_'), ',') - - notice $result - - iterator { $result: } - -This will produce the following: - - notice: Scope(Class[main]): letter_a letter_b letter_c - notice: Scope(Iterator[letter_a]): letter_a - notice: Scope(Iterator[letter_b]): letter_b - notice: Scope(Iterator[letter_c]): letter_c - -Which allows you to avoid resorting to the following: - - $result = split(inline_template("<%= array.collect { |i| \"letter_#{i}\" }.join(',') %>"), ',') - -Phasing out the need for use and abuse of the infamous inline_template -in the example above and which in this very case is extremely ugly as -we have to escape double-quotes to make Puppet parser not evaluate them. - EOS - ) do |arguments| - - # Technically we support three arguments but only first two are mandatory ... - raise(Puppet::ParseError, "join(): Wrong number of arguments " + - "given (#{arguments.size} for 2)") if arguments.size < 2 - - array = arguments[0] - - if not array.is_a?(Array) - raise(Puppet::ParseError, 'join(): Requires an array to work with') - end - - suffix = arguments[1] - prefix = arguments[2] if arguments[2] - - raise(Puppet::ParseError, 'join(): You must provide suffix ' + - 'to join array elements with') if suffix.empty? - - if prefix and prefix.empty? - raise(Puppet::ParseError, 'join(): You must provide prefix ' + - 'to add to join') - end - - if prefix and not prefix.empty? - result = prefix + array.join(suffix + prefix) - else - result = array.join(suffix) - end - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/join_with_prefix.rb b/join_with_prefix.rb new file mode 100644 index 0000000..469763a --- /dev/null +++ b/join_with_prefix.rb @@ -0,0 +1,74 @@ +# +# join.rb +# + +module Puppet::Parser::Functions + newfunction(:join, :type => :rvalue, :doc => <<-EOS +This function will allow to concatenate elements of an array together +with a given suffix and optionally with prefix if such is given ... + +For example: + +Given the following sample manifest: + + define iterator { + notice $name + } + + $array = ['a', 'b', 'c'] + + $result = split(join($array, ',', 'letter_'), ',') + + notice $result + + iterator { $result: } + +This will produce the following: + + notice: Scope(Class[main]): letter_a letter_b letter_c + notice: Scope(Iterator[letter_a]): letter_a + notice: Scope(Iterator[letter_b]): letter_b + notice: Scope(Iterator[letter_c]): letter_c + +Which allows you to avoid resorting to the following: + + $result = split(inline_template("<%= array.collect { |i| \"letter_#{i}\" }.join(',') %>"), ',') + +Phasing out the need for use and abuse of the infamous inline_template +in the example above and which in this very case is extremely ugly as +we have to escape double-quotes to make Puppet parser not evaluate them. + EOS + ) do |arguments| + + # Technically we support three arguments but only first two are mandatory ... + raise(Puppet::ParseError, "join(): Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + array = arguments[0] + + if not array.is_a?(Array) + raise(Puppet::ParseError, 'join(): Requires an array to work with') + end + + suffix = arguments[1] + prefix = arguments[2] if arguments[2] + + raise(Puppet::ParseError, 'join(): You must provide suffix ' + + 'to join array elements with') if suffix.empty? + + if prefix and prefix.empty? + raise(Puppet::ParseError, 'join(): You must provide prefix ' + + 'to add to join') + end + + if prefix and not prefix.empty? + result = prefix + array.join(suffix + prefix) + else + result = array.join(suffix) + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From f87fcecdfb9fac33ac13952a6546b8ed7f673909 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Tue, 26 Apr 2011 00:00:58 +0100 Subject: Small re-factor to fact function. Signed-off-by: Krzysztof Wilczynski --- fact.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/fact.rb b/fact.rb index 9522293..6e66ba6 100644 --- a/fact.rb +++ b/fact.rb @@ -24,14 +24,10 @@ module Puppet::Parser::Functions # and empty string is as closest to actual undef as you we can get # at this point in time ... # - result = (not result or result.empty?) ? '' : result + result = (result and not result.empty?) ? result : '' return result end end # vim: set ts=2 sw=2 et : - -notice fact('interfaces') -notice fact('xyz') -notice fact('') -- cgit v1.2.3 From 1b4ade7b2ad74f219a1712f6a28c22e593e7761f Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Tue, 26 Apr 2011 00:02:59 +0100 Subject: First version. Simple join function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- join.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 join.rb diff --git a/join.rb b/join.rb new file mode 100644 index 0000000..39615fd --- /dev/null +++ b/join.rb @@ -0,0 +1,28 @@ +# +# join.rb +# + +module Puppet::Parser::Functions + newfunction(:join, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + # Technically we support two arguments but only first is mandatory ... + raise(Puppet::ParseError, "join(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + if not array.is_a?(Array) + raise(Puppet::ParseError, 'join(): Requires an array to work with') + end + + suffix = arguments[1] if arguments[1] + + result = suffix ? array.join(suffix) : array.join + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From af5d789e152f60c62a52d20b6bb37bd193dd37c8 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Tue, 26 Apr 2011 00:04:10 +0100 Subject: First version. Simple prefix function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- prefix.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 prefix.rb diff --git a/prefix.rb b/prefix.rb new file mode 100644 index 0000000..bf3690d --- /dev/null +++ b/prefix.rb @@ -0,0 +1,29 @@ +# +# prefix.rb +# + +module Puppet::Parser::Functions + newfunction(:prefix, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + # Technically we support two arguments but only first is mandatory ... + raise(Puppet::ParseError, "prefix(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + if not array.is_a?(Array) + raise(Puppet::ParseError, 'prefix(): Requires an array to work with') + end + + prefix = arguments[1] if arguments[1] + + result = array.collect { |i| prefix ? prefix + i : i } + + return result + end +end + +# vim: set ts=2 sw=2 et : + -- cgit v1.2.3 From 738db6eb706c81d5b2a69692c77769432ec0c824 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Tue, 26 Apr 2011 00:05:20 +0100 Subject: Re-factor of the original function. Now its behaviour is either to join with prefix or just add prefix or simply join. This depends on its use. Signed-off-by: Krzysztof Wilczynski --- join_with_prefix.rb | 64 ++++++++++++----------------------------------------- 1 file changed, 14 insertions(+), 50 deletions(-) diff --git a/join_with_prefix.rb b/join_with_prefix.rb index 469763a..c0a2e4e 100644 --- a/join_with_prefix.rb +++ b/join_with_prefix.rb @@ -1,70 +1,34 @@ # -# join.rb +# join_with_prefix.rb # module Puppet::Parser::Functions - newfunction(:join, :type => :rvalue, :doc => <<-EOS -This function will allow to concatenate elements of an array together -with a given suffix and optionally with prefix if such is given ... - -For example: - -Given the following sample manifest: - - define iterator { - notice $name - } - - $array = ['a', 'b', 'c'] - - $result = split(join($array, ',', 'letter_'), ',') - - notice $result - - iterator { $result: } - -This will produce the following: - - notice: Scope(Class[main]): letter_a letter_b letter_c - notice: Scope(Iterator[letter_a]): letter_a - notice: Scope(Iterator[letter_b]): letter_b - notice: Scope(Iterator[letter_c]): letter_c - -Which allows you to avoid resorting to the following: - - $result = split(inline_template("<%= array.collect { |i| \"letter_#{i}\" }.join(',') %>"), ',') - -Phasing out the need for use and abuse of the infamous inline_template -in the example above and which in this very case is extremely ugly as -we have to escape double-quotes to make Puppet parser not evaluate them. + newfunction(:join_with_prefix, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| - # Technically we support three arguments but only first two are mandatory ... + # Technically we support three arguments but only first is mandatory ... raise(Puppet::ParseError, "join(): Wrong number of arguments " + - "given (#{arguments.size} for 2)") if arguments.size < 2 + "given (#{arguments.size} for 1)") if arguments.size < 1 array = arguments[0] if not array.is_a?(Array) - raise(Puppet::ParseError, 'join(): Requires an array to work with') + raise(Puppet::ParseError, 'join_with_prefix(): Requires an ' + + 'array to work with') end - suffix = arguments[1] - prefix = arguments[2] if arguments[2] - - raise(Puppet::ParseError, 'join(): You must provide suffix ' + - 'to join array elements with') if suffix.empty? + prefix = arguments[1] if arguments[1] + suffix = arguments[2] if arguments[2] - if prefix and prefix.empty? - raise(Puppet::ParseError, 'join(): You must provide prefix ' + - 'to add to join') - end - - if prefix and not prefix.empty? + if prefix and suffix result = prefix + array.join(suffix + prefix) - else + elsif prefix and not suffix + result = array.collect { |i| prefix ? prefix + i : i } + elsif suffix and not prefix result = array.join(suffix) + else + result = array.join end return result -- cgit v1.2.3 From de0e7adb66fd00a8b72b7e7746bd9844bca797d3 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Tue, 26 Apr 2011 00:09:26 +0100 Subject: First version. Simple empty function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- empty.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 empty.rb diff --git a/empty.rb b/empty.rb new file mode 100644 index 0000000..b83ece0 --- /dev/null +++ b/empty.rb @@ -0,0 +1,25 @@ +# +# empty.rb +# + +module Puppet::Parser::Functions + newfunction(:empty, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "empty(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + if not array.is_a?(Array) + raise(Puppet::ParseError, 'empty(): Requires an array to work with') + end + + result = array.empty? + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From a718b9b4a4eaf36ee68462573b9ca1537067ae9d Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Tue, 26 Apr 2011 00:13:31 +0100 Subject: First version. Simple count function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- count.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 count.rb diff --git a/count.rb b/count.rb new file mode 100644 index 0000000..c4a5977 --- /dev/null +++ b/count.rb @@ -0,0 +1,30 @@ +# +# count.rb +# + +# TODO(Krzysztof Wilczynski): We need to add support for regular expression ... + +module Puppet::Parser::Functions + newfunction(:count, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + # Technically we support two arguments but only first is mandatory ... + raise(Puppet::ParseError, "count(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + if not array.is_a?(Array) + raise(Puppet::ParseError, 'count(): Requires an array to work with') + end + + item = arguments[1] if arguments[1] + + result = item ? array.count(item) : array.count + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 19f6ca9d75940378db80b74b3dc0b90b31474549 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Tue, 26 Apr 2011 00:14:12 +0100 Subject: First version. Simple include function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- include.rb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 include.rb diff --git a/include.rb b/include.rb new file mode 100644 index 0000000..28024f7 --- /dev/null +++ b/include.rb @@ -0,0 +1,32 @@ +# +# include.rb +# + +# TODO(Krzysztof Wilczynski): We need to add support for regular expression ... + +module Puppet::Parser::Functions + newfunction(:include, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "include(): Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + array = arguments[0] + + if not array.is_a?(Array) + raise(Puppet::ParseError, 'include(): Requires an array to work with') + end + + item = arguments[1] + + raise(Puppet::ParseError, 'include(): You must provide item ' + + 'to search for within given array') if item.empty? + + result = array.include?(item) + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 4d6b350509ee84a9bd457c2eb0fa4d599f810bf5 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Tue, 26 Apr 2011 00:14:24 +0100 Subject: First version. Simple reverse function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- reverse.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 reverse.rb diff --git a/reverse.rb b/reverse.rb new file mode 100644 index 0000000..49ea80a --- /dev/null +++ b/reverse.rb @@ -0,0 +1,25 @@ +# +# reverse.rb +# + +module Puppet::Parser::Functions + newfunction(:reverse, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "reverse(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + if not array.is_a?(Array) + raise(Puppet::ParseError, 'reverse(): Requires an array to work with') + end + + result = array.reverse + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 45b5b472a119f86919481f9637488bcada02cc75 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Tue, 26 Apr 2011 00:14:38 +0100 Subject: First version. Simple shuffle function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- shuffle.rb | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 shuffle.rb diff --git a/shuffle.rb b/shuffle.rb new file mode 100644 index 0000000..d92a3cd --- /dev/null +++ b/shuffle.rb @@ -0,0 +1,35 @@ +# +# shuffle.rb +# + +module Puppet::Parser::Functions + newfunction(:shuffle, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "shuffle(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + if not array.is_a?(Array) + raise(Puppet::ParseError, 'shuffle(): Requires an array to work with') + end + + return [] if array.size == 0 + return array if array.size <= 1 + + list = array.clone + elements = list.size + + # Simple implementation of Fisher–Yates in-place shuffle ... + elements.times do |i| + j = rand(elements - i) + i + list[j], list[i] = list[i], list[j] + end + + return list + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From c9dcca03b5106a69a39e88ec1c0c53a7ffc121e7 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Tue, 26 Apr 2011 00:14:48 +0100 Subject: First version. Simple unique function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- unique.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 unique.rb diff --git a/unique.rb b/unique.rb new file mode 100644 index 0000000..03a5678 --- /dev/null +++ b/unique.rb @@ -0,0 +1,25 @@ +# +# unique.rb +# + +module Puppet::Parser::Functions + newfunction(:unique, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "unique(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + if not array.is_a?(Array) + raise(Puppet::ParseError, 'unique(): Requires an array to work with') + end + + result = array.uniq + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 528a16e3770b289c7bae315bf54d9ab07f8d6654 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Tue, 26 Apr 2011 01:29:53 +0100 Subject: First version. Simple size function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- size.rb | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 size.rb diff --git a/size.rb b/size.rb new file mode 100644 index 0000000..1aba882 --- /dev/null +++ b/size.rb @@ -0,0 +1,45 @@ +# +# size.rb +# + +module Puppet::Parser::Functions + newfunction(:size, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "size(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + item = arguments[0] + + if item.is_a?(String) + + begin + # + # Check whether your item is a numeric value or not ... + # This will take care about positive and/or negative numbers + # for both integer and floating-point values ... + # + # Please note that Puppet has no notion of hexadecimal + # nor octal numbers for at this point in time ... + # + Float(item) + + raise(Puppet::ParseError, 'size(): Requires either ' + + 'string or array to work with') + + rescue ArgumentError + result = item.size + end + + elsif item.is_a?(Array) + result = item.size + else + raise(Puppet::ParseError, 'size(): Unknown type given') + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 6bf04e1353b3a294aa7b7f5a0e70a5cc2743a3ee Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Tue, 26 Apr 2011 02:53:40 +0100 Subject: Small re-factor. We prefer our local clone of the array ... Signed-off-by: Krzysztof Wilczynski --- shuffle.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/shuffle.rb b/shuffle.rb index d92a3cd..37de723 100644 --- a/shuffle.rb +++ b/shuffle.rb @@ -16,19 +16,19 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, 'shuffle(): Requires an array to work with') end - return [] if array.size == 0 - return array if array.size <= 1 + result = array.clone + elements = result.size - list = array.clone - elements = list.size + return [] if result.size == 0 + return result if result.size <= 1 # Simple implementation of Fisher–Yates in-place shuffle ... elements.times do |i| j = rand(elements - i) + i - list[j], list[i] = list[i], list[j] + result[j], result[i] = result[i], result[j] end - return list + return result end end -- cgit v1.2.3 From 522557001849ab3cc0de9c8a095558c211b62d63 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Tue, 26 Apr 2011 02:54:18 +0100 Subject: First version. Simple delete_at function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- delete_at.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 delete_at.rb diff --git a/delete_at.rb b/delete_at.rb new file mode 100644 index 0000000..c33f65a --- /dev/null +++ b/delete_at.rb @@ -0,0 +1,42 @@ +# +# delete_at.rb +# + +module Puppet::Parser::Functions + newfunction(:delete_at, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "delete_at(): Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + array = arguments[0] + + if not array.is_a?(Array) + raise(Puppet::ParseError, 'delete_at(): Requires an array to work with') + end + + index = arguments[1] + + if not index.match(/^\d+$/) + raise(Puppet::ParseError, 'delete_at(): You must provide ' + + 'positive numeric index') + end + + result = array.clone + + # In Puppet numbers are often string-encoded ... + index = index.to_i + + if index > result.size - 1 # First element is at index 0 is it not? + raise(Puppet::ParseError, 'delete_at(): Given index ' + + 'exceeds array size') + end + + result.delete_at(index) # We ignore the element that got deleted ... + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 67ea75883f5b3346a20c85146cb177c2176f3cd5 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Tue, 26 Apr 2011 03:18:50 +0100 Subject: Changing name of collect_indices.rb to values_at.rb in order to be reasemble Ruby's Array#values_at more. Added support for ranges with hyphen, two and three dots notation. Signed-off-by: Krzysztof Wilczynski --- values_at.rb | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 values_at.rb diff --git a/values_at.rb b/values_at.rb new file mode 100644 index 0000000..c2851b4 --- /dev/null +++ b/values_at.rb @@ -0,0 +1,77 @@ +# +# values_at.rb +# + +module Puppet::Parser::Functions + newfunction(:values_at, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "values_at(): Wrong number of " + + "arguments given (#{arguments.size} for 2)") if arguments.size < 2 + + array = arguments.shift + + if not array.is_a?(Array) + raise(Puppet::ParseError, 'values_at(): Requires an array ' + + 'to work with') + end + + indices = *arguments # Get them all ... Pokemon ... + + if not indices or indices.empty? + raise(Puppet::ParseError, 'values_at(): You must provide ' + + 'at least one positive index to collect') + end + + result = [] + indices_list = [] + + indices.each do |i| + if m = i.match(/^(\d+)(\.\.\.?|\-)(\d+)$/) + start = m[1].to_i + stop = m[3].to_i + + type = m[2] + + if start > stop + raise(Puppet::ParseError, 'values_at(): Stop index in ' + + 'given indices range is smaller than the start index') + elsif stop > array.size - 1 # First element is at index 0 is it not? + raise(Puppet::ParseError, 'values_at(): Stop index in ' + + 'given indices range exceeds array size') + end + + range = case type + when /^(\.\.|\-)$/ then (start .. stop) + when /^(\.\.\.)$/ then (start ... stop) # Exclusive of last element ... + end + + range.each { |i| indices_list << i.to_i } + else + # Only positive numbers allowed in this case ... + if not i.match(/^\d+$/) + raise(Puppet::ParseError, 'values_at(): Unknown format ' + + 'of given index') + end + + # In Puppet numbers are often string-encoded ... + i = i.to_i + + if i > array.size - 1 # Same story. First element is at index 0 ... + raise(Puppet::ParseError, 'values_at(): Given index ' + + 'exceeds array size') + end + + indices_list << i + end + end + + # We remove nil values as they make no sense in Puppet DSL ... + result = indices_list.collect { |i| array[i] }.compact + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 6776d09c0a6ee3b7ba44711849b69674b00e783a Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Tue, 26 Apr 2011 03:55:04 +0100 Subject: Removing surplus files. Signed-off-by: Krzysztof Wilczynski --- collect_indices.rb | 69 ------------------------------------------------------ 1 file changed, 69 deletions(-) delete mode 100644 collect_indices.rb diff --git a/collect_indices.rb b/collect_indices.rb deleted file mode 100644 index 947fb89..0000000 --- a/collect_indices.rb +++ /dev/null @@ -1,69 +0,0 @@ -# -# collect_indices.rb -# - -module Puppet::Parser::Functions - newfunction(:collect_indices, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "collect_indices(): Wrong number of " + - "arguments given (#{arguments.size} for 2)") if arguments.size < 2 - - array = arguments.shift - array_size = array.size - - if not array.is_a?(Array) - raise(Puppet::ParseError, 'collect_indices(): Requires an array ' + - 'to work with') - end - - indices = *arguments # Get them all ... Pokemon ... - - if not indices or indices.empty? - raise(Puppet::ParseError, 'collect_indices(): You must provide ' + - 'indices to collect') - end - - indices_list = [] - - indices.each do |i| - if m = i.match(/^(\d+)\-(\d+)$/) - start = m[1].to_i - stop = m[2].to_i - - if start > stop - raise(Puppet::ParseError, 'collect_indices(): Stop index in ' + - 'given indices range is smaller than the start index') - elsif stop > array_size - 1 # First element is at index 0 is it not? - raise(Puppet::ParseError, 'collect_indices(): Stop index in ' + - 'given indices range exceeds array size') - end - - (start .. stop).each { |i| indices_list << i.to_i } - else - # Only positive numbers allowed in this case ... - if not i.match(/^\d+$/) - raise(Puppet::ParseError, 'collect_indices(): Unknown format ' + - 'of given index') - end - - # In Puppet numbers are often string-encoded ... - i = i.to_i - - if i > array_size - 1 # Same story. First element is at index 0 ... - raise(Puppet::ParseError, 'collect_indices(): Given index ' + - 'exceeds array size') - end - - indices_list << i - end - end - - array = indices_list.collect { |i| array[i] }.compact - - return array - end -end - -# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From c229a323a11c3d233eeac60754d1284464f2418b Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Tue, 26 Apr 2011 04:21:00 +0100 Subject: Changed comment line wording. Signed-off-by: Krzysztof Wilczynski --- size.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/size.rb b/size.rb index 1aba882..282fad2 100644 --- a/size.rb +++ b/size.rb @@ -21,7 +21,7 @@ module Puppet::Parser::Functions # for both integer and floating-point values ... # # Please note that Puppet has no notion of hexadecimal - # nor octal numbers for at this point in time ... + # nor octal numbers for its DSL at this point in time ... # Float(item) -- cgit v1.2.3 From 0e3c0385ef1553e6e613724a8b7f2a157481b172 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Tue, 26 Apr 2011 04:21:32 +0100 Subject: Removing string interpolation from fact.rb. I am not sure whether we should fiddle with this. The old behaviour e.g. evaluate when inside "" and don't do anything when inside '' is probably better choice in the end as people are used to it working that way. Signed-off-by: Krzysztof Wilczynski --- fact.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/fact.rb b/fact.rb index 6e66ba6..0c24867 100644 --- a/fact.rb +++ b/fact.rb @@ -15,7 +15,6 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, 'fact(): You must provide ' + 'fact name') if fact.empty? - fact = strinterp(fact) # Evaluate any interpolated variable names ... result = lookupvar(fact) # Get the value of interest from Facter ... # -- cgit v1.2.3 From cd4904b9d92d2cec87ef9332a64559fa16298535 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Tue, 26 Apr 2011 23:45:50 +0100 Subject: First version. Simple range function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- range.rb | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 range.rb diff --git a/range.rb b/range.rb new file mode 100644 index 0000000..0c513ef --- /dev/null +++ b/range.rb @@ -0,0 +1,56 @@ +# +# range.rb +# + +module Puppet::Parser::Functions + newfunction(:range, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "range(): Wrong number of " + + "arguments given (#{arguments.size} for 1)") if arguments.size < 1 + + if arguments.size > 1 + start = arguments[0] + stop = arguments[1] + + type = '..' # We select simplest type for Range available in Ruby ... + + elsif arguments.size > 0 + value = arguments[0] + + if m = value.match(/^(\w+)(\.\.\.?|\-)(\w+)$/) + start = m[1] + stop = m[3] + + type = m[2] + + elsif value.match(/^.+$/) + raise(Puppet::ParseError, 'range(): Unable to compute range ' + + 'from the value given') + else + raise(Puppet::ParseError, 'range(): Unknown format of range given') + end + end + + # Check whether we have numeric value if so then make it so ... + if start.match(/^\d+$/) + start = start.to_i + stop = stop.to_i + else + start = start.to_s + stop = stop.to_s + end + + range = case type + when /^(\.\.|\-)$/ then (start .. stop) + when /^(\.\.\.)$/ then (start ... stop) # Exclusive of last element ... + end + + result = range.collect { |i| i } # Get them all ... Pokemon ... + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 79191fc12751405cebaa56e98d56ae159508743f Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Wed, 27 Apr 2011 23:26:41 +0100 Subject: Changing name of the function from include to includes as it clashes with a core function from Puppet::Parser. I had no idea that you can over-write some of the that way. Oops. Signed-off-by: Krzysztof Wilczynski --- includes.rb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 includes.rb diff --git a/includes.rb b/includes.rb new file mode 100644 index 0000000..6207bf7 --- /dev/null +++ b/includes.rb @@ -0,0 +1,32 @@ +# +# include.rb +# + +# TODO(Krzysztof Wilczynski): We need to add support for regular expression ... + +module Puppet::Parser::Functions + newfunction(:includes, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "includes(): Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + array = arguments[0] + + if not array.is_a?(Array) + raise(Puppet::ParseError, 'includes(): Requires an array to work with') + end + + item = arguments[1] + + raise(Puppet::ParseError, 'includes(): You must provide item ' + + 'to search for within given array') if item.empty? + + result = array.include?(item) + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 0bd7550336df4d63bb957b742bb8caaa9f310355 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Thu, 28 Apr 2011 03:42:37 +0100 Subject: Minor changes. Signed-off-by: Krzysztof Wilczynski --- count.rb | 1 + empty.rb | 2 ++ includes.rb | 1 + range.rb | 1 + 4 files changed, 5 insertions(+) diff --git a/count.rb b/count.rb index c4a5977..9ea4033 100644 --- a/count.rb +++ b/count.rb @@ -3,6 +3,7 @@ # # TODO(Krzysztof Wilczynski): We need to add support for regular expression ... +# TODO(Krzysztof Wilczynski): Support for strings and hashes too .. module Puppet::Parser::Functions newfunction(:count, :type => :rvalue, :doc => <<-EOS diff --git a/empty.rb b/empty.rb index b83ece0..2e75c61 100644 --- a/empty.rb +++ b/empty.rb @@ -2,6 +2,8 @@ # empty.rb # +# TODO(Krzysztof Wilczynski): Support for hashes would be nice too ... + module Puppet::Parser::Functions newfunction(:empty, :type => :rvalue, :doc => <<-EOS EOS diff --git a/includes.rb b/includes.rb index 6207bf7..a491a76 100644 --- a/includes.rb +++ b/includes.rb @@ -3,6 +3,7 @@ # # TODO(Krzysztof Wilczynski): We need to add support for regular expression ... +# TODO(Krzysztof Wilczynski): Support for strings and hashes too ... module Puppet::Parser::Functions newfunction(:includes, :type => :rvalue, :doc => <<-EOS diff --git a/range.rb b/range.rb index 0c513ef..a6a9f22 100644 --- a/range.rb +++ b/range.rb @@ -7,6 +7,7 @@ module Puppet::Parser::Functions EOS ) do |arguments| + # We support more than one argument but at least one is mandatory ... raise(Puppet::ParseError, "range(): Wrong number of " + "arguments given (#{arguments.size} for 1)") if arguments.size < 1 -- cgit v1.2.3 From d495723d6f0b400755c749e68c0a8b8d49570848 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Thu, 28 Apr 2011 03:43:14 +0100 Subject: First version. Simple is_array function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- is_array.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 is_array.rb diff --git a/is_array.rb b/is_array.rb new file mode 100644 index 0000000..0b508fd --- /dev/null +++ b/is_array.rb @@ -0,0 +1,21 @@ +# +# is_array.rb +# + +module Puppet::Parser::Functions + newfunction(:is_array, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "is_array(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + type = arguments[0] + + result = type.is_a?(Array) + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From fc886ed7b7678d310b940b6f60f171e54439b483 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Thu, 28 Apr 2011 03:43:34 +0100 Subject: First version. Simple is_string function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- is_string.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 is_string.rb diff --git a/is_string.rb b/is_string.rb new file mode 100644 index 0000000..61037e3 --- /dev/null +++ b/is_string.rb @@ -0,0 +1,21 @@ +# +# is_string.rb +# + +module Puppet::Parser::Functions + newfunction(:is_string, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "is_string(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + type = arguments[0] + + result = type.is_a?(String) + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 31041c0e372a95c5d0737d0c70db187b2c8e1107 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Thu, 28 Apr 2011 03:44:11 +0100 Subject: First version. Simple time function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- time.rb | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 time.rb diff --git a/time.rb b/time.rb new file mode 100644 index 0000000..06c4586 --- /dev/null +++ b/time.rb @@ -0,0 +1,36 @@ +# +# time.rb +# + +module Puppet::Parser::Functions + newfunction(:time, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + # The Time Zone argument is optional ... + time_zone = arguments[0] if arguments[0] + + time = Time.new + + if time_zone and not time_zone.empty? + original_zone = ENV['TZ'] + + local_time = time.clone + local_time = local_time.utc + + ENV['TZ'] = time_zone + + time = local_time.localtime + + ENV['TZ'] = original_zone + end + + # Calling Time#to_i on a receiver changes it. Trust me I am the Doctor. + result = time.strftime('%s') + result = result.to_i + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From d25cd57dbece241db3642db81ab9c4672fc1eec1 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Thu, 28 Apr 2011 03:44:28 +0100 Subject: First version. Simple strftime function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- strftime.rb | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 strftime.rb diff --git a/strftime.rb b/strftime.rb new file mode 100644 index 0000000..b3cd6a6 --- /dev/null +++ b/strftime.rb @@ -0,0 +1,43 @@ +# +# strftime.rb +# + +module Puppet::Parser::Functions + newfunction(:strftime, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + # Technically we support two arguments but only first is mandatory ... + raise(Puppet::ParseError, "strftime(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + format = arguments[0] + + raise(Puppet::ParseError, 'strftime(): You must provide ' + + 'format for evaluation') if format.empty? + + # The Time Zone argument is optional ... + time_zone = arguments[1] if arguments[1] + + time = Time.new + + if time_zone and not time_zone.empty? + original_zone = ENV['TZ'] + + local_time = time.clone + local_time = local_time.utc + + ENV['TZ'] = time_zone + + time = local_time.localtime + + ENV['TZ'] = original_zone + end + + result = time.strftime(format) + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 177f0c730be495f3ae63ca2ced0ad3b5d0b8b3d3 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Thu, 28 Apr 2011 03:45:05 +0100 Subject: Removing old unused file. Signed-off-by: Krzysztof Wilczynski --- include.rb | 32 -------------------------------- 1 file changed, 32 deletions(-) delete mode 100644 include.rb diff --git a/include.rb b/include.rb deleted file mode 100644 index 28024f7..0000000 --- a/include.rb +++ /dev/null @@ -1,32 +0,0 @@ -# -# include.rb -# - -# TODO(Krzysztof Wilczynski): We need to add support for regular expression ... - -module Puppet::Parser::Functions - newfunction(:include, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "include(): Wrong number of arguments " + - "given (#{arguments.size} for 2)") if arguments.size < 2 - - array = arguments[0] - - if not array.is_a?(Array) - raise(Puppet::ParseError, 'include(): Requires an array to work with') - end - - item = arguments[1] - - raise(Puppet::ParseError, 'include(): You must provide item ' + - 'to search for within given array') if item.empty? - - result = array.include?(item) - - return result - end -end - -# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 1c07b8dba921f9ecc73fa29d9994b9ccf07e8538 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 03:09:52 +0100 Subject: First version. Simple abs function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- abs.rb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 abs.rb diff --git a/abs.rb b/abs.rb new file mode 100644 index 0000000..694bff9 --- /dev/null +++ b/abs.rb @@ -0,0 +1,32 @@ +# +# abs.rb +# + +module Puppet::Parser::Functions + newfunction(:abs, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "abs(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + + if value.is_a?(String) + if value.match(/^-?(?:\d+)(?:\.\d+){1}$/) + value = value.to_f + elsif value.match(/^-?\d+$/) + value = value.to_i + else + raise(Puppet::ParseError, 'abs(): Requires a numeric ' + + 'value to work with') + end + end + + result = value.abs + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 517227c75fe43489a25d4729ad9584b25193d843 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 04:18:29 +0100 Subject: Added TODO for future reference. Signed-off-by: Krzysztof Wilczynski --- unique.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/unique.rb b/unique.rb index 03a5678..617b451 100644 --- a/unique.rb +++ b/unique.rb @@ -2,6 +2,8 @@ # unique.rb # +# TODO(Krzysztof Wilczynski): Support for strings would be nice too ... + module Puppet::Parser::Functions newfunction(:unique, :type => :rvalue, :doc => <<-EOS EOS -- cgit v1.2.3 From b387d5ef602c4eacb4ad21f2f7a31a7e29b8c217 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 04:19:30 +0100 Subject: Changed wording from numeric to integer. Signed-off-by: Krzysztof Wilczynski --- range.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/range.rb b/range.rb index a6a9f22..68124e1 100644 --- a/range.rb +++ b/range.rb @@ -34,7 +34,7 @@ module Puppet::Parser::Functions end end - # Check whether we have numeric value if so then make it so ... + # Check whether we have integer value if so then make it so ... if start.match(/^\d+$/) start = start.to_i stop = stop.to_i -- cgit v1.2.3 From c3877c19843e10691f5dbc5d0f0f7e5368512abe Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 04:20:44 +0100 Subject: Added TODO for future reference. Signed-off-by: Krzysztof Wilczynski --- shuffle.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shuffle.rb b/shuffle.rb index 37de723..33c32bb 100644 --- a/shuffle.rb +++ b/shuffle.rb @@ -2,6 +2,8 @@ # shuffle.rb # +# TODO(Krzysztof Wilczynski): Support for strings would be nice too ... + module Puppet::Parser::Functions newfunction(:shuffle, :type => :rvalue, :doc => <<-EOS EOS -- cgit v1.2.3 From 606820a4c90d358b39b32f7f15b25cef496e08f7 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 04:21:20 +0100 Subject: Added TODO for future reference. Signed-off-by: Krzysztof Wilczynski --- reverse.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/reverse.rb b/reverse.rb index 49ea80a..6aa11f4 100644 --- a/reverse.rb +++ b/reverse.rb @@ -2,6 +2,8 @@ # reverse.rb # +# TODO(Krzysztof Wilczynski): Support for strings would be nice too ... + module Puppet::Parser::Functions newfunction(:reverse, :type => :rvalue, :doc => <<-EOS EOS -- cgit v1.2.3 From eab2a63f1bdd5478a9d6a5c5db71d6129cb79dcf Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 04:22:04 +0100 Subject: Added TODO for future reference. Signed-off-by: Krzysztof Wilczynski --- size.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/size.rb b/size.rb index 282fad2..aa4f4ad 100644 --- a/size.rb +++ b/size.rb @@ -2,6 +2,8 @@ # size.rb # +# TODO(Krzysztof Wilczynski): Support for hashes would be nice too ... + module Puppet::Parser::Functions newfunction(:size, :type => :rvalue, :doc => <<-EOS EOS -- cgit v1.2.3 From 65f98d2f09ab8fde4bcf81532d026c74f132e1e7 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 04:36:28 +0100 Subject: Adding relevant code. Signed-off-by: Krzysztof Wilczynski --- keys.rb | 25 +++++++++++++++++++++++++ values.rb | 25 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 keys.rb create mode 100644 values.rb diff --git a/keys.rb b/keys.rb new file mode 100644 index 0000000..7d50889 --- /dev/null +++ b/keys.rb @@ -0,0 +1,25 @@ +# +# keys.rb +# + +module Puppet::Parser::Functions + newfunction(:keys, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "keys(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + hash = arguments[0] + + if not hash.is_a?(Hash) + raise(Puppet::ParseError, 'keys(): Requires a hash to work with') + end + + result = hash.keys + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/values.rb b/values.rb new file mode 100644 index 0000000..8e58184 --- /dev/null +++ b/values.rb @@ -0,0 +1,25 @@ +# +# values.rb +# + +module Puppet::Parser::Functions + newfunction(:values, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "values(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + hash = arguments[0] + + if not hash.is_a?(Hash) + raise(Puppet::ParseError, 'values(): Requires a hash to work with') + end + + result = hash.values + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 1296b3fc28570ab553d765ba93ba130c6a1a0dcb Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 04:37:50 +0100 Subject: First version. Simple is_hash function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- is_hash.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 is_hash.rb diff --git a/is_hash.rb b/is_hash.rb new file mode 100644 index 0000000..259809c --- /dev/null +++ b/is_hash.rb @@ -0,0 +1,21 @@ +# +# is_hash.rb +# + +module Puppet::Parser::Functions + newfunction(:is_array, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "is_hash(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + type = arguments[0] + + result = type.is_a?(Hash) + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From e9d0b65807532903ff5ce8bd8f05149089044264 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 05:00:51 +0100 Subject: Adding support for string alongside arrays. Signed-off-by: Krzysztof Wilczynski --- shuffle.rb | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/shuffle.rb b/shuffle.rb index 33c32bb..c7abf30 100644 --- a/shuffle.rb +++ b/shuffle.rb @@ -2,8 +2,6 @@ # shuffle.rb # -# TODO(Krzysztof Wilczynski): Support for strings would be nice too ... - module Puppet::Parser::Functions newfunction(:shuffle, :type => :rvalue, :doc => <<-EOS EOS @@ -12,13 +10,23 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, "shuffle(): Wrong number of arguments " + "given (#{arguments.size} for 1)") if arguments.size < 1 - array = arguments[0] + value = arguments[0] + klass = value.class + + if not [Array, String].include?(klass) + raise(Puppet::ParseError, 'shuffle(): Requires either an ' + + 'array or string to work with') + end + + string_given = false + + result = value.clone - if not array.is_a?(Array) - raise(Puppet::ParseError, 'shuffle(): Requires an array to work with') + if value.is_a?(String) + result = result.split('') + string_given = true end - result = array.clone elements = result.size return [] if result.size == 0 @@ -30,6 +38,8 @@ module Puppet::Parser::Functions result[j], result[i] = result[i], result[j] end + result = string_given ? result.join : result + return result end end -- cgit v1.2.3 From f35fb819988408ad9a2e274bcdd268f7df177f52 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 15:19:31 +0100 Subject: First version. Simple type function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- type.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 type.rb diff --git a/type.rb b/type.rb new file mode 100644 index 0000000..389a056 --- /dev/null +++ b/type.rb @@ -0,0 +1,33 @@ +# +# type.rb +# + +module Puppet::Parser::Functions + newfunction(:type, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "type(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + + klass = value.class + + if not [Array, Bignum, Fixnum, Float, Hash, String].include?(klass) + raise(Puppet::ParseError, 'type(): Unknown type') + end + + klass = klass.to_s # Ugly ... + + # We note that Integer is the parent to Bignum and Fixnum ... + result = case klass + when /^(?:Big|Fix)num$/ then 'Integer' + else klass + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From b30ed04390647832002506039457fb93158d3cb8 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 17:27:50 +0100 Subject: First version. Simple capitalize function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- capitalize.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 capitalize.rb diff --git a/capitalize.rb b/capitalize.rb new file mode 100644 index 0000000..74d4f28 --- /dev/null +++ b/capitalize.rb @@ -0,0 +1,31 @@ +# +# capitalize.rb +# + +module Puppet::Parser::Functions + newfunction(:capitalize, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "capitalize(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + if not [Array, String].include?(klass) + raise(Puppet::ParseError, 'capitalize(): Requires either an ' + + 'array or string to work with') + end + + if value.is_a?(Array) + result = value.collect { |i| i.is_a?(String) ? i.capitalize : i } + else + result = value.capitalize + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From b3dd2207c3eefe4a74f86188330a0117b6f03da9 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 17:29:14 +0100 Subject: Small re-factor of shuffle function. It is more compact now. Signed-off-by: Krzysztof Wilczynski --- shuffle.rb | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/shuffle.rb b/shuffle.rb index c7abf30..2bba584 100644 --- a/shuffle.rb +++ b/shuffle.rb @@ -18,27 +18,25 @@ module Puppet::Parser::Functions 'array or string to work with') end - string_given = false - result = value.clone - if value.is_a?(String) - result = result.split('') - string_given = true - end - - elements = result.size + string_type = value.is_a?(String) ? true : false - return [] if result.size == 0 + # Check whether it makes sense to shuffle ... return result if result.size <= 1 + # We turn any string value into an array to be able to shuffle ... + result = string_type ? result.split('') : result + + elements = result.size + # Simple implementation of Fisher–Yates in-place shuffle ... elements.times do |i| j = rand(elements - i) + i result[j], result[i] = result[i], result[j] end - result = string_given ? result.join : result + result = string_type ? result.join : result return result end -- cgit v1.2.3 From 14b4ca8de2c8802bc599bb45aca5ecbba5ed98c9 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 17:30:04 +0100 Subject: Added support for strings to reverse. Signed-off-by: Krzysztof Wilczynski --- reverse.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/reverse.rb b/reverse.rb index 6aa11f4..86dee28 100644 --- a/reverse.rb +++ b/reverse.rb @@ -2,8 +2,6 @@ # reverse.rb # -# TODO(Krzysztof Wilczynski): Support for strings would be nice too ... - module Puppet::Parser::Functions newfunction(:reverse, :type => :rvalue, :doc => <<-EOS EOS @@ -12,13 +10,15 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, "reverse(): Wrong number of arguments " + "given (#{arguments.size} for 1)") if arguments.size < 1 - array = arguments[0] + value = arguments[0] + klass = value.class - if not array.is_a?(Array) - raise(Puppet::ParseError, 'reverse(): Requires an array to work with') + if not [Array, String].include?(klass) + raise(Puppet::ParseError, 'reverse(): Requires either an ' + + 'array or string to work with') end - result = array.reverse + result = value.reverse return result end -- cgit v1.2.3 From c025cce133904890716da2349e7f3abbc6903c9e Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 18:08:45 +0100 Subject: First version. Simple strip function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- strip.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 strip.rb diff --git a/strip.rb b/strip.rb new file mode 100644 index 0000000..1bb5c0a --- /dev/null +++ b/strip.rb @@ -0,0 +1,31 @@ +# +# strip.rb +# + +module Puppet::Parser::Functions + newfunction(:chop, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "strip(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + if not [Array, String].include?(klass) + raise(Puppet::ParseError, 'strip(): Requires either an ' + + 'array or string to work with') + end + + if value.is_a?(Array) + result = value.collect { |i| i.is_a?(String) ? i.strip : i } + else + result = value.strip + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From a29674f08fbb85838d4d9cee5faea6303ef35b9b Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 18:09:29 +0100 Subject: First version. Simple rstrip function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- rstrip.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 rstrip.rb diff --git a/rstrip.rb b/rstrip.rb new file mode 100644 index 0000000..b198488 --- /dev/null +++ b/rstrip.rb @@ -0,0 +1,31 @@ +# +# rstrip.rb +# + +module Puppet::Parser::Functions + newfunction(:rstrip, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "rstrip(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + if not [Array, String].include?(klass) + raise(Puppet::ParseError, 'rstrip(): Requires either an ' + + 'array or string to work with') + end + + if value.is_a?(Array) + result = value.collect { |i| i.is_a?(String) ? i.rstrip : i } + else + result = value.rstrip + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From a038f6a04111382f07d63ceb4f4624eec3dd7034 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 18:09:39 +0100 Subject: First version. Simple lstrip function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- lstrip.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 lstrip.rb diff --git a/lstrip.rb b/lstrip.rb new file mode 100644 index 0000000..7f205a8 --- /dev/null +++ b/lstrip.rb @@ -0,0 +1,31 @@ +# +# lstrip.rb +# + +module Puppet::Parser::Functions + newfunction(:lstrip, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "lstrip(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + if not [Array, String].include?(klass) + raise(Puppet::ParseError, 'lstrip(): Requires either an ' + + 'array or string to work with') + end + + if value.is_a?(Array) + result = value.collect { |i| i.is_a?(String) ? i.lstrip : i } + else + result = value.lstrip + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 020a2a23eedcfb4957ac55214d597ec911e9b454 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 18:09:57 +0100 Subject: First version. Simple chop function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- chop.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 chop.rb diff --git a/chop.rb b/chop.rb new file mode 100644 index 0000000..2f6b2d2 --- /dev/null +++ b/chop.rb @@ -0,0 +1,31 @@ +# +# chop.rb +# + +module Puppet::Parser::Functions + newfunction(:chop, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "chop(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + if not [Array, String].include?(klass) + raise(Puppet::ParseError, 'chop(): Requires either an ' + + 'array or string to work with') + end + + if value.is_a?(Array) + result = value.collect { |i| i.is_a?(String) ? i.chop : i } + else + result = value.chop + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 456a351ac2d7904c2c3bf4d106c1de679456325c Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 18:10:04 +0100 Subject: First version. Simple chomp function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- chomp.rb | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 chomp.rb diff --git a/chomp.rb b/chomp.rb new file mode 100644 index 0000000..bec3adf --- /dev/null +++ b/chomp.rb @@ -0,0 +1,31 @@ +# +# chomp.rb +# + +module Puppet::Parser::Functions + newfunction(:chomp, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "chomp(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + if not [Array, String].include?(klass) + raise(Puppet::ParseError, 'chomp(): Requires either an ' + + 'array or string to work with') + end + + if value.is_a?(Array) + result = value.collect { |i| i.is_a?(String) ? i.chomp : i } + else + result = value.chomp + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From af1ba5ce8a75cadf1de512ad01e6ccde0b20fd28 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 18:27:52 +0100 Subject: Adding support for strings and hashes to the function empty. Signed-off-by: Krzysztof Wilczynski --- empty.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/empty.rb b/empty.rb index 2e75c61..1db9b63 100644 --- a/empty.rb +++ b/empty.rb @@ -12,13 +12,15 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, "empty(): Wrong number of arguments " + "given (#{arguments.size} for 1)") if arguments.size < 1 - array = arguments[0] + value = arguments[0] + klass = value.class - if not array.is_a?(Array) - raise(Puppet::ParseError, 'empty(): Requires an array to work with') + if not [Array, Hash, String].include?(klass) + raise(Puppet::ParseError, 'empty(): Requires either an ' + + 'array, hash or string to work with') end - result = array.empty? + result = value.empty? return result end -- cgit v1.2.3 From aa2237bcb68cf6edd5d55aa2f754d00d011d3aa5 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 18:28:38 +0100 Subject: Make sure that we have string-encoded integer value. Signed-off-by: Krzysztof Wilczynski --- delete_at.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/delete_at.rb b/delete_at.rb index c33f65a..1869476 100644 --- a/delete_at.rb +++ b/delete_at.rb @@ -18,7 +18,7 @@ module Puppet::Parser::Functions index = arguments[1] - if not index.match(/^\d+$/) + if index.is_a?(String) and not index.match(/^\d+$/) raise(Puppet::ParseError, 'delete_at(): You must provide ' + 'positive numeric index') end -- cgit v1.2.3 From e0559c174ecd78d7b3f2cb589e6ab02a9c8ce561 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 18:40:07 +0100 Subject: Adding support for strings to the the function unique. Signed-off-by: Krzysztof Wilczynski --- unique.rb | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/unique.rb b/unique.rb index 617b451..ce7ea70 100644 --- a/unique.rb +++ b/unique.rb @@ -2,8 +2,6 @@ # unique.rb # -# TODO(Krzysztof Wilczynski): Support for strings would be nice too ... - module Puppet::Parser::Functions newfunction(:unique, :type => :rvalue, :doc => <<-EOS EOS @@ -12,13 +10,24 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, "unique(): Wrong number of arguments " + "given (#{arguments.size} for 1)") if arguments.size < 1 - array = arguments[0] + value = arguments[0] + klass = value.class - if not array.is_a?(Array) - raise(Puppet::ParseError, 'unique(): Requires an array to work with') + if not [Array, String].include?(klass) + raise(Puppet::ParseError, 'unique(): Requires either an ' + + 'array or string to work with') end - result = array.uniq + result = value.clone + + string_type = value.is_a?(String) ? true : false + + # We turn any string value into an array to be able to shuffle ... + result = string_type ? result.split('') : result + + result = result.uniq + + result = string_type ? result.join : result return result end -- cgit v1.2.3 From 085874afc35659909653eaf22a16ff37ebe72a73 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 18:42:54 +0100 Subject: Adding TODO for future reference. Signed-off-by: Krzysztof Wilczynski --- values_at.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/values_at.rb b/values_at.rb index c2851b4..cd7205d 100644 --- a/values_at.rb +++ b/values_at.rb @@ -2,6 +2,9 @@ # values_at.rb # +# TODO(Krzysztof Wilczynski): Support for hashes would be nice too ... +# TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ... + module Puppet::Parser::Functions newfunction(:values_at, :type => :rvalue, :doc => <<-EOS EOS -- cgit v1.2.3 From 5e9721983a6239338dea13b8efaef9f679bebb33 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 20:05:44 +0100 Subject: Added FalseClass and TrueClass to be identified as Boolean to the type function. Signed-off-by: Krzysztof Wilczynski --- type.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/type.rb b/type.rb index 389a056..ac1895c 100644 --- a/type.rb +++ b/type.rb @@ -14,15 +14,21 @@ module Puppet::Parser::Functions klass = value.class - if not [Array, Bignum, Fixnum, Float, Hash, String].include?(klass) + if not [Array, Bignum, Fixnum, FalseClass, + Float, Hash, String, TrueClass].include?(klass) + raise(Puppet::ParseError, 'type(): Unknown type') end klass = klass.to_s # Ugly ... + # # We note that Integer is the parent to Bignum and Fixnum ... + # Plus we say Boolean for FalseClass and TrueClass ... + # result = case klass - when /^(?:Big|Fix)num$/ then 'Integer' + when /^(?:Big|Fix)num$/ then 'Integer' + when /^(?:False|True)Class$/ then 'Boolean' else klass end -- cgit v1.2.3 From 07847be9b99913d3c94899b36da191e609c28a38 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 20:07:56 +0100 Subject: Added proper handling of both FalseClass and TrueClass. We also return real integer values now over string-encoded integers. Signed-off-by: Krzysztof Wilczynski --- bool2num.rb | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/bool2num.rb b/bool2num.rb index c197b17..eddd051 100644 --- a/bool2num.rb +++ b/bool2num.rb @@ -3,30 +3,38 @@ # module Puppet::Parser::Functions - newfunction(:bool2num, :type => :rvalue, :doc => <<-EOS + newfunction(:bool2number, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| - raise(Puppet::ParseError, "bool2num(): Wrong number of arguments " + + raise(Puppet::ParseError, "bool2number(): Wrong number of arguments " + "given (#{arguments.size} for 1)") if arguments.size < 1 - boolean = arguments[0] + value = arguments[0] + klass = value.class - result = case boolean - # - # This is how undef looks like in Puppet ... - # We yield 0 (or false if you wish) in this case. - # - when /^$/, '' then '0' - when /^(1|t|true)$/ then '1' - when /^(0|f|false)$/ then '0' - # This is not likely to happen ... - when /^(undef|undefined)$/ then '0' - # We may get real boolean values as well ... - when true then '1' - when false then '0' - else - raise(Puppet::ParseError, 'bool2num(): Unknown type of boolean given') + if not [FalseClass, String, TrueClass].include?(klass) + raise(Puppet::ParseError, 'bool2number(): Requires either an ' + + 'boolean or string to work with') + end + + if value.is_a?(String) + result = case value + # + # This is how undef looks like in Puppet ... + # We yield 0 (or false if you wish) in this case. + # + when /^$/, '' then 0 + when /^(1|t|y|true|yes)$/ then 1 + when /^(0|f|n|false|no)$/ then 0 + # This is not likely to happen ... + when /^(undef|undefined)$/ then 0 + else + raise(Puppet::ParseError, 'bool2number(): Unknown type of boolean given') + end + else + # We have real boolean values as well ... + result = value ? 1 : 0 end return result -- cgit v1.2.3 From 4c097b0a093b2363904fc3030af48edb9ecf11e9 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 20:10:53 +0100 Subject: Reverting name back to bool2num. Signed-off-by: Krzysztof Wilczynski --- bool2num.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bool2num.rb b/bool2num.rb index eddd051..b0dbc88 100644 --- a/bool2num.rb +++ b/bool2num.rb @@ -3,18 +3,18 @@ # module Puppet::Parser::Functions - newfunction(:bool2number, :type => :rvalue, :doc => <<-EOS + newfunction(:bool2num, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| - raise(Puppet::ParseError, "bool2number(): Wrong number of arguments " + + raise(Puppet::ParseError, "bool2num(): Wrong number of arguments " + "given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] klass = value.class if not [FalseClass, String, TrueClass].include?(klass) - raise(Puppet::ParseError, 'bool2number(): Requires either an ' + + raise(Puppet::ParseError, 'bool2num(): Requires either an ' + 'boolean or string to work with') end @@ -30,7 +30,7 @@ module Puppet::Parser::Functions # This is not likely to happen ... when /^(undef|undefined)$/ then 0 else - raise(Puppet::ParseError, 'bool2number(): Unknown type of boolean given') + raise(Puppet::ParseError, 'bool2num(): Unknown type of boolean given') end else # We have real boolean values as well ... -- cgit v1.2.3 From f3b6b11e85ba42af809c12e50f0172dc582a680a Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 20:13:27 +0100 Subject: First version. Simple str2bool function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- str2bool.rb | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 str2bool.rb diff --git a/str2bool.rb b/str2bool.rb new file mode 100644 index 0000000..02b5aae --- /dev/null +++ b/str2bool.rb @@ -0,0 +1,38 @@ +# +# str2bool.rb +# + +module Puppet::Parser::Functions + newfunction(:str2bool, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "str2bool(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + string = arguments[0] + + if not string.is_a?(String) + raise(Puppet::ParseError, 'str2bool(): Requires either a ' + + 'string to work with') + end + + result = case string + # + # This is how undef looks like in Puppet ... + # We yield false in this case. + # + when /^$/, '' then false + when /^(1|t|y|true|yes)$/ then true + when /^(0|f|n|false|no)$/ then false + # This is not likely to happen ... + when /^(undef|undefined)$/ then false + else + raise(Puppet::ParseError, 'str2bool(): Unknown type of boolean given') + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From b24c839f89058dd56e458420775a32ca46ea3259 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 20:47:39 +0100 Subject: Minor changes. Signed-off-by: Krzysztof Wilczynski --- bool2num.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bool2num.rb b/bool2num.rb index b0dbc88..9de7f50 100644 --- a/bool2num.rb +++ b/bool2num.rb @@ -19,6 +19,7 @@ module Puppet::Parser::Functions end if value.is_a?(String) + result = case value # # This is how undef looks like in Puppet ... @@ -32,6 +33,7 @@ module Puppet::Parser::Functions else raise(Puppet::ParseError, 'bool2num(): Unknown type of boolean given') end + else # We have real boolean values as well ... result = value ? 1 : 0 -- cgit v1.2.3 From 8454622e32e599d7e135e894f70ef2e8e42dcda6 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 20:58:28 +0100 Subject: Added check to ensure that given fact name is a string. Signed-off-by: Krzysztof Wilczynski --- fact.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fact.rb b/fact.rb index 0c24867..0f4114e 100644 --- a/fact.rb +++ b/fact.rb @@ -12,6 +12,10 @@ module Puppet::Parser::Functions fact = arguments[0] + if not fact.is_a?(String) + raise(Puppet::ParseError, 'fact(): Requires fact name to be a string') + end + raise(Puppet::ParseError, 'fact(): You must provide ' + 'fact name') if fact.empty? -- cgit v1.2.3 From 96777a0033b584a4f8940896cc2adf87560bfb9a Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 20:58:49 +0100 Subject: Minor change. Signed-off-by: Krzysztof Wilczynski --- abs.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/abs.rb b/abs.rb index 694bff9..aee68e4 100644 --- a/abs.rb +++ b/abs.rb @@ -23,6 +23,7 @@ module Puppet::Parser::Functions end end + # We have numeric value to handle ... result = value.abs return result -- cgit v1.2.3 From ab82eceba56077578418a57fe86503b135018ac3 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 21:02:17 +0100 Subject: Renaming from includes to member in order to avoid confusion and possibility of a clash with Puppet built-in function include. Signed-off-by: Krzysztof Wilczynski --- includes.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/includes.rb b/includes.rb index a491a76..73030d7 100644 --- a/includes.rb +++ b/includes.rb @@ -1,27 +1,27 @@ # -# include.rb +# member.rb # # TODO(Krzysztof Wilczynski): We need to add support for regular expression ... # TODO(Krzysztof Wilczynski): Support for strings and hashes too ... module Puppet::Parser::Functions - newfunction(:includes, :type => :rvalue, :doc => <<-EOS + newfunction(:member, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| - raise(Puppet::ParseError, "includes(): Wrong number of arguments " + + raise(Puppet::ParseError, "member(): Wrong number of arguments " + "given (#{arguments.size} for 2)") if arguments.size < 2 array = arguments[0] if not array.is_a?(Array) - raise(Puppet::ParseError, 'includes(): Requires an array to work with') + raise(Puppet::ParseError, 'member(): Requires an array to work with') end item = arguments[1] - raise(Puppet::ParseError, 'includes(): You must provide item ' + + raise(Puppet::ParseError, 'member(): You must provide item ' + 'to search for within given array') if item.empty? result = array.include?(item) -- cgit v1.2.3 From 1e788e891b83e6441dda6d7ebc2778b12e0ea619 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 21:03:11 +0100 Subject: Renaming the file from includes.rb to member.rb. Signed-off-by: Krzysztof Wilczynski --- member.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 member.rb diff --git a/member.rb b/member.rb new file mode 100644 index 0000000..73030d7 --- /dev/null +++ b/member.rb @@ -0,0 +1,33 @@ +# +# member.rb +# + +# TODO(Krzysztof Wilczynski): We need to add support for regular expression ... +# TODO(Krzysztof Wilczynski): Support for strings and hashes too ... + +module Puppet::Parser::Functions + newfunction(:member, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "member(): Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + array = arguments[0] + + if not array.is_a?(Array) + raise(Puppet::ParseError, 'member(): Requires an array to work with') + end + + item = arguments[1] + + raise(Puppet::ParseError, 'member(): You must provide item ' + + 'to search for within given array') if item.empty? + + result = array.include?(item) + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 94db34684d3cc3fff64fd3336a53fa1eb56fc83d Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 21:03:34 +0100 Subject: Removing old file includes.rb Signed-off-by: Krzysztof Wilczynski --- includes.rb | 33 --------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 includes.rb diff --git a/includes.rb b/includes.rb deleted file mode 100644 index 73030d7..0000000 --- a/includes.rb +++ /dev/null @@ -1,33 +0,0 @@ -# -# member.rb -# - -# TODO(Krzysztof Wilczynski): We need to add support for regular expression ... -# TODO(Krzysztof Wilczynski): Support for strings and hashes too ... - -module Puppet::Parser::Functions - newfunction(:member, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "member(): Wrong number of arguments " + - "given (#{arguments.size} for 2)") if arguments.size < 2 - - array = arguments[0] - - if not array.is_a?(Array) - raise(Puppet::ParseError, 'member(): Requires an array to work with') - end - - item = arguments[1] - - raise(Puppet::ParseError, 'member(): You must provide item ' + - 'to search for within given array') if item.empty? - - result = array.include?(item) - - return result - end -end - -# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From f4f47f6d5f38e9d72fd0fc4701e8cb2b78fc238e Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 23:15:05 +0100 Subject: Changed wording in the error message. Signed-off-by: Krzysztof Wilczynski --- abs.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/abs.rb b/abs.rb index aee68e4..c3e90d4 100644 --- a/abs.rb +++ b/abs.rb @@ -12,14 +12,15 @@ module Puppet::Parser::Functions value = arguments[0] + # Numbers in Puppet are often string-encoded ... if value.is_a?(String) if value.match(/^-?(?:\d+)(?:\.\d+){1}$/) value = value.to_f elsif value.match(/^-?\d+$/) value = value.to_i else - raise(Puppet::ParseError, 'abs(): Requires a numeric ' + - 'value to work with') + raise(Puppet::ParseError, 'abs(): Requires float or ' + + 'integer to work with') end end -- cgit v1.2.3 From 6d9e5efe380437420a53762e21d73dc1c8262f64 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 23:16:26 +0100 Subject: Minor changes. Signed-off-by: Krzysztof Wilczynski --- bool2num.rb | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/bool2num.rb b/bool2num.rb index 9de7f50..65c8295 100644 --- a/bool2num.rb +++ b/bool2num.rb @@ -13,23 +13,24 @@ module Puppet::Parser::Functions value = arguments[0] klass = value.class - if not [FalseClass, String, TrueClass].include?(klass) - raise(Puppet::ParseError, 'bool2num(): Requires either an ' + + # We can have either true or false, or string which resembles boolean ... + unless [FalseClass, TrueClass, String].include?(klass) + raise(Puppet::ParseError, 'bool2num(): Requires either ' + 'boolean or string to work with') end if value.is_a?(String) + # We consider all the yes, no, y, n and so on too ... result = case value # # This is how undef looks like in Puppet ... # We yield 0 (or false if you wish) in this case. # - when /^$/, '' then 0 - when /^(1|t|y|true|yes)$/ then 1 - when /^(0|f|n|false|no)$/ then 0 - # This is not likely to happen ... - when /^(undef|undefined)$/ then 0 + when /^$/, '' then 0 # Empty string will be false ... + when /^(1|t|y|true|yes)$/ then 1 + when /^(0|f|n|false|no)$/ then 0 + when /^(undef|undefined)$/ then 0 # This is not likely to happen ... else raise(Puppet::ParseError, 'bool2num(): Unknown type of boolean given') end -- cgit v1.2.3 From 726746649e38615b69aa372ec978b2ffa39b89d5 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 23:18:58 +0100 Subject: Small re-factor. Changed if not to unless for code clarity. Signed-off-by: Krzysztof Wilczynski --- capitalize.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/capitalize.rb b/capitalize.rb index 74d4f28..ac6cc50 100644 --- a/capitalize.rb +++ b/capitalize.rb @@ -13,12 +13,13 @@ module Puppet::Parser::Functions value = arguments[0] klass = value.class - if not [Array, String].include?(klass) - raise(Puppet::ParseError, 'capitalize(): Requires either an ' + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'capitalize(): Requires either ' + 'array or string to work with') end if value.is_a?(Array) + # Numbers in Puppet are often string-encoded ... result = value.collect { |i| i.is_a?(String) ? i.capitalize : i } else result = value.capitalize -- cgit v1.2.3 From 0ff8b00a64426acf96d0659d1cbc1f67bba842f4 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Fri, 29 Apr 2011 23:30:32 +0100 Subject: Small re-factor. Changed if not to unless for code clarity. Signed-off-by: Krzysztof Wilczynski --- chomp.rb | 5 +++-- chop.rb | 3 ++- count.rb | 4 ++-- fact.rb | 2 +- join.rb | 4 ++-- join_with_prefix.rb | 4 ++-- keys.rb | 4 ++-- lstrip.rb | 4 ++-- prefix.rb | 4 ++-- reverse.rb | 4 ++-- rstrip.rb | 4 ++-- str2bool.rb | 5 +++-- values.rb | 4 ++-- values_at.rb | 5 ++--- 14 files changed, 29 insertions(+), 27 deletions(-) diff --git a/chomp.rb b/chomp.rb index bec3adf..aba9e8d 100644 --- a/chomp.rb +++ b/chomp.rb @@ -13,12 +13,13 @@ module Puppet::Parser::Functions value = arguments[0] klass = value.class - if not [Array, String].include?(klass) - raise(Puppet::ParseError, 'chomp(): Requires either an ' + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'chomp(): Requires either ' + 'array or string to work with') end if value.is_a?(Array) + # Numbers in Puppet are often string-encoded ... result = value.collect { |i| i.is_a?(String) ? i.chomp : i } else result = value.chomp diff --git a/chop.rb b/chop.rb index 2f6b2d2..32ce040 100644 --- a/chop.rb +++ b/chop.rb @@ -13,12 +13,13 @@ module Puppet::Parser::Functions value = arguments[0] klass = value.class - if not [Array, String].include?(klass) + unless [Array, String].include?(klass) raise(Puppet::ParseError, 'chop(): Requires either an ' + 'array or string to work with') end if value.is_a?(Array) + # Numbers in Puppet are often string-encoded ... result = value.collect { |i| i.is_a?(String) ? i.chop : i } else result = value.chop diff --git a/count.rb b/count.rb index 9ea4033..e4bf22e 100644 --- a/count.rb +++ b/count.rb @@ -16,8 +16,8 @@ module Puppet::Parser::Functions array = arguments[0] - if not array.is_a?(Array) - raise(Puppet::ParseError, 'count(): Requires an array to work with') + unless array.is_a?(Array) + raise(Puppet::ParseError, 'count(): Requires array to work with') end item = arguments[1] if arguments[1] diff --git a/fact.rb b/fact.rb index 0f4114e..00aa572 100644 --- a/fact.rb +++ b/fact.rb @@ -12,7 +12,7 @@ module Puppet::Parser::Functions fact = arguments[0] - if not fact.is_a?(String) + unless fact.is_a?(String) raise(Puppet::ParseError, 'fact(): Requires fact name to be a string') end diff --git a/join.rb b/join.rb index 39615fd..4d20731 100644 --- a/join.rb +++ b/join.rb @@ -13,8 +13,8 @@ module Puppet::Parser::Functions array = arguments[0] - if not array.is_a?(Array) - raise(Puppet::ParseError, 'join(): Requires an array to work with') + unless array.is_a?(Array) + raise(Puppet::ParseError, 'join(): Requires array to work with') end suffix = arguments[1] if arguments[1] diff --git a/join_with_prefix.rb b/join_with_prefix.rb index c0a2e4e..8bf96d9 100644 --- a/join_with_prefix.rb +++ b/join_with_prefix.rb @@ -13,8 +13,8 @@ module Puppet::Parser::Functions array = arguments[0] - if not array.is_a?(Array) - raise(Puppet::ParseError, 'join_with_prefix(): Requires an ' + + unless array.is_a?(Array) + raise(Puppet::ParseError, 'join_with_prefix(): Requires ' + 'array to work with') end diff --git a/keys.rb b/keys.rb index 7d50889..3a92a47 100644 --- a/keys.rb +++ b/keys.rb @@ -12,8 +12,8 @@ module Puppet::Parser::Functions hash = arguments[0] - if not hash.is_a?(Hash) - raise(Puppet::ParseError, 'keys(): Requires a hash to work with') + unless hash.is_a?(Hash) + raise(Puppet::ParseError, 'keys(): Requires hash to work with') end result = hash.keys diff --git a/lstrip.rb b/lstrip.rb index 7f205a8..241b683 100644 --- a/lstrip.rb +++ b/lstrip.rb @@ -13,8 +13,8 @@ module Puppet::Parser::Functions value = arguments[0] klass = value.class - if not [Array, String].include?(klass) - raise(Puppet::ParseError, 'lstrip(): Requires either an ' + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'lstrip(): Requires either ' + 'array or string to work with') end diff --git a/prefix.rb b/prefix.rb index bf3690d..572ff4e 100644 --- a/prefix.rb +++ b/prefix.rb @@ -13,8 +13,8 @@ module Puppet::Parser::Functions array = arguments[0] - if not array.is_a?(Array) - raise(Puppet::ParseError, 'prefix(): Requires an array to work with') + unless array.is_a?(Array) + raise(Puppet::ParseError, 'prefix(): Requires array to work with') end prefix = arguments[1] if arguments[1] diff --git a/reverse.rb b/reverse.rb index 86dee28..79e9b93 100644 --- a/reverse.rb +++ b/reverse.rb @@ -13,8 +13,8 @@ module Puppet::Parser::Functions value = arguments[0] klass = value.class - if not [Array, String].include?(klass) - raise(Puppet::ParseError, 'reverse(): Requires either an ' + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'reverse(): Requires either ' + 'array or string to work with') end diff --git a/rstrip.rb b/rstrip.rb index b198488..56849d3 100644 --- a/rstrip.rb +++ b/rstrip.rb @@ -13,8 +13,8 @@ module Puppet::Parser::Functions value = arguments[0] klass = value.class - if not [Array, String].include?(klass) - raise(Puppet::ParseError, 'rstrip(): Requires either an ' + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'rstrip(): Requires either ' + 'array or string to work with') end diff --git a/str2bool.rb b/str2bool.rb index 02b5aae..5a52f25 100644 --- a/str2bool.rb +++ b/str2bool.rb @@ -12,11 +12,12 @@ module Puppet::Parser::Functions string = arguments[0] - if not string.is_a?(String) - raise(Puppet::ParseError, 'str2bool(): Requires either a ' + + unless string.is_a?(String) + raise(Puppet::ParseError, 'str2bool(): Requires either ' + 'string to work with') end + # We consider all the yes, no, y, n and so on too ... result = case string # # This is how undef looks like in Puppet ... diff --git a/values.rb b/values.rb index 8e58184..c1c4a77 100644 --- a/values.rb +++ b/values.rb @@ -12,8 +12,8 @@ module Puppet::Parser::Functions hash = arguments[0] - if not hash.is_a?(Hash) - raise(Puppet::ParseError, 'values(): Requires a hash to work with') + unless hash.is_a?(Hash) + raise(Puppet::ParseError, 'values(): Requires hash to work with') end result = hash.values diff --git a/values_at.rb b/values_at.rb index cd7205d..331af6a 100644 --- a/values_at.rb +++ b/values_at.rb @@ -15,9 +15,8 @@ module Puppet::Parser::Functions array = arguments.shift - if not array.is_a?(Array) - raise(Puppet::ParseError, 'values_at(): Requires an array ' + - 'to work with') + unless array.is_a?(Array) + raise(Puppet::ParseError, 'values_at(): Requires array to work with') end indices = *arguments # Get them all ... Pokemon ... -- cgit v1.2.3 From 0fabca9a6536dc82cea80a6fca996b8028450cb4 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 00:56:12 +0100 Subject: Adding support for hash and string to the function count. Signed-off-by: Krzysztof Wilczynski --- count.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/count.rb b/count.rb index e4bf22e..c4e2283 100644 --- a/count.rb +++ b/count.rb @@ -3,7 +3,7 @@ # # TODO(Krzysztof Wilczynski): We need to add support for regular expression ... -# TODO(Krzysztof Wilczynski): Support for strings and hashes too .. +# TODO(Krzysztof Wilczynski): Support for hash values would be nice too ... module Puppet::Parser::Functions newfunction(:count, :type => :rvalue, :doc => <<-EOS @@ -14,15 +14,20 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, "count(): Wrong number of arguments " + "given (#{arguments.size} for 1)") if arguments.size < 1 - array = arguments[0] + value = arguments[0] + klass = value.class - unless array.is_a?(Array) - raise(Puppet::ParseError, 'count(): Requires array to work with') + unless [Array, Hash, String].include?(klass) + raise(Puppet::ParseError, 'count(): Requires either ' + + 'array, hash or string to work with') end item = arguments[1] if arguments[1] - result = item ? array.count(item) : array.count + value = value.is_a?(Hash) ? value.keys : value + + # No item to look for and count? Then just return current size ... + result = item ? value.count(item) : value.size return result end -- cgit v1.2.3 From 555c50d73595478a8a422b83d073514f7dbed0f0 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 00:57:24 +0100 Subject: Moved to unless from if not for code clarity and changed wording of few error messages. Signed-off-by: Krzysztof Wilczynski --- delete_at.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/delete_at.rb b/delete_at.rb index 1869476..10190ba 100644 --- a/delete_at.rb +++ b/delete_at.rb @@ -12,25 +12,25 @@ module Puppet::Parser::Functions array = arguments[0] - if not array.is_a?(Array) - raise(Puppet::ParseError, 'delete_at(): Requires an array to work with') + unless array.is_a?(Array) + raise(Puppet::ParseError, 'delete_at(): Requires array to work with') end index = arguments[1] if index.is_a?(String) and not index.match(/^\d+$/) raise(Puppet::ParseError, 'delete_at(): You must provide ' + - 'positive numeric index') + 'non-negative numeric index') end result = array.clone - # In Puppet numbers are often string-encoded ... + # Numbers in Puppet are often string-encoded which is troublesome ... index = index.to_i if index > result.size - 1 # First element is at index 0 is it not? raise(Puppet::ParseError, 'delete_at(): Given index ' + - 'exceeds array size') + 'exceeds size of array given') end result.delete_at(index) # We ignore the element that got deleted ... -- cgit v1.2.3 From 551f4ce95b5a7b141d40e654026ff3b000481096 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 00:58:37 +0100 Subject: Moved to unless from if not and removed TODO. Signed-off-by: Krzysztof Wilczynski --- empty.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/empty.rb b/empty.rb index 1db9b63..e78ebf0 100644 --- a/empty.rb +++ b/empty.rb @@ -2,8 +2,6 @@ # empty.rb # -# TODO(Krzysztof Wilczynski): Support for hashes would be nice too ... - module Puppet::Parser::Functions newfunction(:empty, :type => :rvalue, :doc => <<-EOS EOS @@ -15,8 +13,8 @@ module Puppet::Parser::Functions value = arguments[0] klass = value.class - if not [Array, Hash, String].include?(klass) - raise(Puppet::ParseError, 'empty(): Requires either an ' + + unless [Array, Hash, String].include?(klass) + raise(Puppet::ParseError, 'empty(): Requires either ' + 'array, hash or string to work with') end -- cgit v1.2.3 From 21e39aaeac2fb545b80a47f55f5e77a2872f65fc Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 00:59:18 +0100 Subject: Moved to unless from if not plus removed surplus empty lines. Signed-off-by: Krzysztof Wilczynski --- unique.rb | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/unique.rb b/unique.rb index ce7ea70..a922c94 100644 --- a/unique.rb +++ b/unique.rb @@ -13,21 +13,19 @@ module Puppet::Parser::Functions value = arguments[0] klass = value.class - if not [Array, String].include?(klass) - raise(Puppet::ParseError, 'unique(): Requires either an ' + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'unique(): Requires either ' + 'array or string to work with') end result = value.clone - string_type = value.is_a?(String) ? true : false + string = value.is_a?(String) ? true : false # We turn any string value into an array to be able to shuffle ... - result = string_type ? result.split('') : result - - result = result.uniq - - result = string_type ? result.join : result + result = string ? result.split('') : result + result = result.uniq # Remove duplicates ... + result = string ? result.join : result return result end -- cgit v1.2.3 From 3b55113c7321376882f4d158bfcca380706330a2 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 01:00:10 +0100 Subject: Moved to unless from if not and changed wording of few error messages. Signed-off-by: Krzysztof Wilczynski --- type.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/type.rb b/type.rb index ac1895c..3051de6 100644 --- a/type.rb +++ b/type.rb @@ -14,17 +14,18 @@ module Puppet::Parser::Functions klass = value.class - if not [Array, Bignum, Fixnum, FalseClass, - Float, Hash, String, TrueClass].include?(klass) + # This should cover all the generic types present in Puppet at present ... + unless [Array, Bignum, Fixnum, FalseClass, + Float, Hash, String, TrueClass].include?(klass) - raise(Puppet::ParseError, 'type(): Unknown type') + raise(Puppet::ParseError, 'type(): Unknown type given') end klass = klass.to_s # Ugly ... # # We note that Integer is the parent to Bignum and Fixnum ... - # Plus we say Boolean for FalseClass and TrueClass ... + # Plus we claim name Boolean for FalseClass and TrueClass ... # result = case klass when /^(?:Big|Fix)num$/ then 'Integer' -- cgit v1.2.3 From 5fce8a7f54ffca674205b063dd52b8ad35137685 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 01:47:41 +0100 Subject: Added ability to flatten the resulting array in the function zip. This would allow for creating hashes on the fly from two arrays. Signed-off-by: Krzysztof Wilczynski --- zip.rb | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 zip.rb diff --git a/zip.rb b/zip.rb new file mode 100644 index 0000000..024d64a --- /dev/null +++ b/zip.rb @@ -0,0 +1,56 @@ +# +# zip.rb +# + +module Puppet::Parser::Functions + newfunction(:zip, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + # Technically we support three arguments but only first is mandatory ... + raise(Puppet::ParseError, "zip(): Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + a = arguments[0] + b = arguments[1] + + unless a.is_a?(Array) and b.is_a?(Array) + raise(Puppet::ParseError, 'zip(): Requires array to work with') + end + + flatten = arguments[2] if arguments[2] + + if flatten + klass = flatten.class + + # We can have either true or false, or string which resembles boolean ... + unless [FalseClass, TrueClass, String].include?(klass) + raise(Puppet::ParseError, 'zip(): Requires either ' + + 'boolean or string to work with') + end + + if flatten.is_a?(String) + # We consider all the yes, no, y, n and so on too ... + flatten = case flatten + # + # This is how undef looks like in Puppet ... + # 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 /^(undef|undefined)$/ then false # This is not likely to happen ... + else + raise(Puppet::ParseError, 'zip(): Unknown type of boolean given') + end + end + end + + result = a.zip(b) + result = flatten ? result.flatten : result + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From b26d5b2f3be95737ea2df63ffccea5354d37d81e Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 02:40:04 +0100 Subject: Now prefix will convert everything into string which is the same as join would do. Also function is now more robust in error detection. Signed-off-by: Krzysztof Wilczynski --- prefix.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/prefix.rb b/prefix.rb index 572ff4e..0e0cee2 100644 --- a/prefix.rb +++ b/prefix.rb @@ -19,11 +19,20 @@ module Puppet::Parser::Functions prefix = arguments[1] if arguments[1] - result = array.collect { |i| prefix ? prefix + i : i } + if prefix + unless prefix.is_a?(String) + raise(Puppet::ParseError, 'prefix(): Requires string to work with') + end + end + + # Turn everything into string same as join would do ... + result = array.collect do |i| + i = i.to_s + prefix ? prefix + i : i + end return result end end # vim: set ts=2 sw=2 et : - -- cgit v1.2.3 From 41798020d6620a58b0f5a9aa8fa467512aa3fecc Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 02:41:09 +0100 Subject: Function has now more robust error detection. Signed-off-by: Krzysztof Wilczynski --- join.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/join.rb b/join.rb index 4d20731..945556a 100644 --- a/join.rb +++ b/join.rb @@ -19,6 +19,12 @@ module Puppet::Parser::Functions suffix = arguments[1] if arguments[1] + if suffix + unless suffix.is_a?(String) + raise(Puppet::ParseError, 'join(): Requires string to work with') + end + end + result = suffix ? array.join(suffix) : array.join return result -- cgit v1.2.3 From 16eec26b0b2e671600ff048c54263adf032af5fd Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 02:43:13 +0100 Subject: Corrected function name from chop to strip. Moved to unless from if not to improve code clarity. Signed-off-by: Krzysztof Wilczynski --- strip.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/strip.rb b/strip.rb index 1bb5c0a..ebab20e 100644 --- a/strip.rb +++ b/strip.rb @@ -3,7 +3,7 @@ # module Puppet::Parser::Functions - newfunction(:chop, :type => :rvalue, :doc => <<-EOS + newfunction(:strip, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| @@ -13,8 +13,8 @@ module Puppet::Parser::Functions value = arguments[0] klass = value.class - if not [Array, String].include?(klass) - raise(Puppet::ParseError, 'strip(): Requires either an ' + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'strip(): Requires either ' + 'array or string to work with') end -- cgit v1.2.3 From 55df5ac566b2ba233c53b4865a183f13b5ec8160 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 02:44:49 +0100 Subject: Added comment line. Signed-off-by: Krzysztof Wilczynski --- time.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/time.rb b/time.rb index 06c4586..f7c1041 100644 --- a/time.rb +++ b/time.rb @@ -12,6 +12,7 @@ module Puppet::Parser::Functions time = Time.new + # There is probably a better way to handle Time Zone ... if time_zone and not time_zone.empty? original_zone = ENV['TZ'] -- cgit v1.2.3 From 20a8892c094d2dc5193f495261524c523d45e725 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 02:45:04 +0100 Subject: Added comment line. Signed-off-by: Krzysztof Wilczynski --- strftime.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/strftime.rb b/strftime.rb index b3cd6a6..c919320 100644 --- a/strftime.rb +++ b/strftime.rb @@ -21,6 +21,7 @@ module Puppet::Parser::Functions time = Time.new + # There is probably a better way to handle Time Zone ... if time_zone and not time_zone.empty? original_zone = ENV['TZ'] -- cgit v1.2.3 From 4b2a0a9e1f214085de5d9d1820ed5c94cb26325b Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 02:45:36 +0100 Subject: Small change to code formatting. Signed-off-by: Krzysztof Wilczynski --- str2bool.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/str2bool.rb b/str2bool.rb index 5a52f25..f3a6d6c 100644 --- a/str2bool.rb +++ b/str2bool.rb @@ -23,11 +23,10 @@ module Puppet::Parser::Functions # This is how undef looks like in Puppet ... # We yield false in this case. # - when /^$/, '' then false - when /^(1|t|y|true|yes)$/ then true - when /^(0|f|n|false|no)$/ then false - # This is not likely to happen ... - when /^(undef|undefined)$/ then false + 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 /^(undef|undefined)$/ then false # This is not likely to happen ... else raise(Puppet::ParseError, 'str2bool(): Unknown type of boolean given') end -- cgit v1.2.3 From 5da2005d0486838eac2b46afced1f94eda99820a Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 02:46:03 +0100 Subject: Moved to unless from if not to make code more clear. Plus a variable name change for simplicity. Signed-off-by: Krzysztof Wilczynski --- shuffle.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/shuffle.rb b/shuffle.rb index 2bba584..73e798c 100644 --- a/shuffle.rb +++ b/shuffle.rb @@ -13,20 +13,20 @@ module Puppet::Parser::Functions value = arguments[0] klass = value.class - if not [Array, String].include?(klass) - raise(Puppet::ParseError, 'shuffle(): Requires either an ' + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'shuffle(): Requires either ' + 'array or string to work with') end result = value.clone - string_type = value.is_a?(String) ? true : false + string = value.is_a?(String) ? true : false # Check whether it makes sense to shuffle ... return result if result.size <= 1 # We turn any string value into an array to be able to shuffle ... - result = string_type ? result.split('') : result + result = string ? result.split('') : result elements = result.size @@ -36,7 +36,7 @@ module Puppet::Parser::Functions result[j], result[i] = result[i], result[j] end - result = string_type ? result.join : result + result = string ? result.join : result return result end -- cgit v1.2.3 From 9ec50e7968bbd53c0e9d3b626ae46ce2eeba7cf1 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 02:47:20 +0100 Subject: Added TODO for future reference. Signed-off-by: Krzysztof Wilczynski --- range.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/range.rb b/range.rb index 68124e1..6afb50c 100644 --- a/range.rb +++ b/range.rb @@ -2,6 +2,8 @@ # range.rb # +# TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ... + module Puppet::Parser::Functions newfunction(:range, :type => :rvalue, :doc => <<-EOS EOS -- cgit v1.2.3 From 3c4c1c7c2010d8df0e5e43306cc7b407705d4053 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 02:48:25 +0100 Subject: Moved to unless from if not to improve code clarity. Added TODO for future reference. Changed wording of few error messages. Signed-off-by: Krzysztof Wilczynski --- num2bool.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/num2bool.rb b/num2bool.rb index 0e6b7a5..2baef62 100644 --- a/num2bool.rb +++ b/num2bool.rb @@ -2,6 +2,8 @@ # num2bool.rb # +# TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ... + module Puppet::Parser::Functions newfunction(:num2bool, :type => :rvalue, :doc => <<-EOS EOS @@ -13,20 +15,20 @@ module Puppet::Parser::Functions number = arguments[0] # Only numbers allowed ... - if not number.match(/^\-?\d+$/) - raise(Puppet::ParseError, 'num2bool(): Requires a number to work with') + unless number.match(/^\-?\d+$/) + raise(Puppet::ParseError, 'num2bool(): Requires integer to work with') end result = case number when /^0$/ false when /^\-?\d+$/ - # In Puppet numbers are often string-encoded ... + # Numbers in Puppet are often string-encoded which is troublesome ... number = number.to_i # We yield true for any positive number and false otherwise ... number > 0 ? true : false else - raise(Puppet::ParseError, 'num2bool(): Unknown number format given') + raise(Puppet::ParseError, 'num2bool(): Unknown numeric format given') end return result -- cgit v1.2.3 From 9d0e2447712dd3c01b59c723251eb8c02933e501 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 02:49:15 +0100 Subject: Moved to unless from if not to improve code clarity. Changed wording of few error messages. Signed-off-by: Krzysztof Wilczynski --- member.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/member.rb b/member.rb index 73030d7..bb43a37 100644 --- a/member.rb +++ b/member.rb @@ -15,14 +15,14 @@ module Puppet::Parser::Functions array = arguments[0] - if not array.is_a?(Array) - raise(Puppet::ParseError, 'member(): Requires an array to work with') + unless array.is_a?(Array) + raise(Puppet::ParseError, 'member(): Requires array to work with') end item = arguments[1] raise(Puppet::ParseError, 'member(): You must provide item ' + - 'to search for within given array') if item.empty? + 'to search for within array given') if item.empty? result = array.include?(item) -- cgit v1.2.3 From 872c7f3c42557787662bdd42a3d6f69051d09a88 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 02:50:38 +0100 Subject: Added body of the function flatten. Signed-off-by: Krzysztof Wilczynski --- flatten.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 flatten.rb diff --git a/flatten.rb b/flatten.rb new file mode 100644 index 0000000..6036f72 --- /dev/null +++ b/flatten.rb @@ -0,0 +1,25 @@ +# +# flatten.rb +# + +module Puppet::Parser::Functions + newfunction(:flatten, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "flatten(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + unless array.is_a?(Array) + raise(Puppet::ParseError, 'flatten(): Requires array to work with') + end + + result = array.flatten + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From db7a27cf5b0cb5667b17e2cbc686bff4be3966b7 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 02:51:17 +0100 Subject: Small re-factor. Remove not needed complexity. Signed-off-by: Krzysztof Wilczynski --- fact.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fact.rb b/fact.rb index 00aa572..27b7bb2 100644 --- a/fact.rb +++ b/fact.rb @@ -27,7 +27,7 @@ module Puppet::Parser::Functions # and empty string is as closest to actual undef as you we can get # at this point in time ... # - result = (result and not result.empty?) ? result : '' + result = result.empty? ? '' : result return result end -- cgit v1.2.3 From 4da6d8222eeca060ad91c9a49f965ec1150a37c2 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 02:54:47 +0100 Subject: Changed wording of the note in the comment. Signed-off-by: Krzysztof Wilczynski --- abs.rb | 2 +- capitalize.rb | 2 +- chomp.rb | 2 +- chop.rb | 2 +- downcase.rb | 32 ++++++++++++++++++++++++++++++++ lstrip.rb | 1 + upcase.rb | 32 ++++++++++++++++++++++++++++++++ 7 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 downcase.rb create mode 100644 upcase.rb diff --git a/abs.rb b/abs.rb index c3e90d4..0a554e4 100644 --- a/abs.rb +++ b/abs.rb @@ -12,7 +12,7 @@ module Puppet::Parser::Functions value = arguments[0] - # Numbers in Puppet are often string-encoded ... + # Numbers in Puppet are often string-encoded which is troublesome ... if value.is_a?(String) if value.match(/^-?(?:\d+)(?:\.\d+){1}$/) value = value.to_f diff --git a/capitalize.rb b/capitalize.rb index ac6cc50..f902cb3 100644 --- a/capitalize.rb +++ b/capitalize.rb @@ -19,7 +19,7 @@ module Puppet::Parser::Functions end if value.is_a?(Array) - # Numbers in Puppet are often string-encoded ... + # Numbers in Puppet are often string-encoded which is troublesome ... result = value.collect { |i| i.is_a?(String) ? i.capitalize : i } else result = value.capitalize diff --git a/chomp.rb b/chomp.rb index aba9e8d..e1d788a 100644 --- a/chomp.rb +++ b/chomp.rb @@ -19,7 +19,7 @@ module Puppet::Parser::Functions end if value.is_a?(Array) - # Numbers in Puppet are often string-encoded ... + # Numbers in Puppet are often string-encoded which is troublesome ... result = value.collect { |i| i.is_a?(String) ? i.chomp : i } else result = value.chomp diff --git a/chop.rb b/chop.rb index 32ce040..0f9af86 100644 --- a/chop.rb +++ b/chop.rb @@ -19,7 +19,7 @@ module Puppet::Parser::Functions end if value.is_a?(Array) - # Numbers in Puppet are often string-encoded ... + # Numbers in Puppet are often string-encoded which is troublesome ... result = value.collect { |i| i.is_a?(String) ? i.chop : i } else result = value.chop diff --git a/downcase.rb b/downcase.rb new file mode 100644 index 0000000..71f8480 --- /dev/null +++ b/downcase.rb @@ -0,0 +1,32 @@ +# +# downcase.rb +# + +module Puppet::Parser::Functions + newfunction(:downcase, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "downcase(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'downcase(): Requires either ' + + 'array or string to work with') + end + + if value.is_a?(Array) + # Numbers in Puppet are often string-encoded which is troublesome ... + result = value.collect { |i| i.is_a?(String) ? i.downcase : i } + else + result = value.downcase + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lstrip.rb b/lstrip.rb index 241b683..a7b2656 100644 --- a/lstrip.rb +++ b/lstrip.rb @@ -19,6 +19,7 @@ module Puppet::Parser::Functions end if value.is_a?(Array) + # Numbers in Puppet are often string-encoded which is troublesome ... result = value.collect { |i| i.is_a?(String) ? i.lstrip : i } else result = value.lstrip diff --git a/upcase.rb b/upcase.rb new file mode 100644 index 0000000..8a9769d --- /dev/null +++ b/upcase.rb @@ -0,0 +1,32 @@ +# +# upcase.rb +# + +module Puppet::Parser::Functions + newfunction(:upcase, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "upcase(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'upcase(): Requires either ' + + 'array or string to work with') + end + + if value.is_a?(Array) + # Numbers in Puppet are often string-encoded which is troublesome ... + result = value.collect { |i| i.is_a?(String) ? i.upcase : i } + else + result = value.upcase + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From f74ab047cd397f16f57e8a3516d5ef7cafcc24d7 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 02:57:48 +0100 Subject: Change boolean detecion from string to make entire function more robust. Signed-off-by: Krzysztof Wilczynski --- bool2num.rb | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/bool2num.rb b/bool2num.rb index 65c8295..b2989d0 100644 --- a/bool2num.rb +++ b/bool2num.rb @@ -20,26 +20,24 @@ module Puppet::Parser::Functions end if value.is_a?(String) - # We consider all the yes, no, y, n and so on too ... - result = case value + value = case value # # This is how undef looks like in Puppet ... # We yield 0 (or false if you wish) in this case. # - when /^$/, '' then 0 # Empty string will be false ... - when /^(1|t|y|true|yes)$/ then 1 - when /^(0|f|n|false|no)$/ then 0 - when /^(undef|undefined)$/ then 0 # This is not likely to happen ... + 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 /^(undef|undefined)$/ then false # This is not likely to happen ... else raise(Puppet::ParseError, 'bool2num(): Unknown type of boolean given') end - - else - # We have real boolean values as well ... - result = value ? 1 : 0 end + # We have real boolean values as well ... + result = value ? 1 : 0 + return result end end -- cgit v1.2.3 From 72b23cb2e5fba6241e438e2088cd310865d4457f Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 03:15:47 +0100 Subject: First version. Simple hash function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- hash.rb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 hash.rb diff --git a/hash.rb b/hash.rb new file mode 100644 index 0000000..7e63d43 --- /dev/null +++ b/hash.rb @@ -0,0 +1,32 @@ +# +# hash.rb +# + +module Puppet::Parser::Functions + newfunction(:hash, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "hash(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + unless array.is_a?(Array) + raise(Puppet::ParseError, 'hash(): Requires array to work with') + end + + result = {} + + begin + result = Hash[*array] + rescue Exception + raise(Puppet::ParseError, 'hash(): Unable to compute ' + + 'hash from array given') + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From b3be789b0da179e1011323e95468d3a1f2fd5083 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 03:16:12 +0100 Subject: Add number of stub code for future functions. Signed-off-by: Krzysztof Wilczynski --- date.rb | 12 ++++++++++++ delete.rb | 15 +++++++++++++++ grep.rb | 12 ++++++++++++ is_float.rb | 12 ++++++++++++ is_integer.rb | 12 ++++++++++++ is_numeric.rb | 12 ++++++++++++ is_valid_domain_name.rb | 12 ++++++++++++ is_valid_ip_address.rb | 12 ++++++++++++ is_valid_mac_address.rb | 12 ++++++++++++ is_valid_netmask.rb | 12 ++++++++++++ sort.rb | 12 ++++++++++++ 11 files changed, 135 insertions(+) create mode 100644 date.rb create mode 100644 delete.rb create mode 100644 grep.rb create mode 100644 is_float.rb create mode 100644 is_integer.rb create mode 100644 is_numeric.rb create mode 100644 is_valid_domain_name.rb create mode 100644 is_valid_ip_address.rb create mode 100644 is_valid_mac_address.rb create mode 100644 is_valid_netmask.rb create mode 100644 sort.rb diff --git a/date.rb b/date.rb new file mode 100644 index 0000000..ea11265 --- /dev/null +++ b/date.rb @@ -0,0 +1,12 @@ +# +# date.rb +# + +module Puppet::Parser::Functions + newfunction(:date, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/delete.rb b/delete.rb new file mode 100644 index 0000000..b8376d1 --- /dev/null +++ b/delete.rb @@ -0,0 +1,15 @@ +# +# delete.rb +# + +# TODO(Krzysztof Wilczynski): We need to add support for regular expression ... +# TODO(Krzysztof Wilczynski): Support for strings and hashes too ... + +module Puppet::Parser::Functions + newfunction(:delete, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/grep.rb b/grep.rb new file mode 100644 index 0000000..1663fe7 --- /dev/null +++ b/grep.rb @@ -0,0 +1,12 @@ +# +# grep.rb +# + +module Puppet::Parser::Functions + newfunction(:grep, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/is_float.rb b/is_float.rb new file mode 100644 index 0000000..2a5a923 --- /dev/null +++ b/is_float.rb @@ -0,0 +1,12 @@ +# +# is_float.rb +# + +module Puppet::Parser::Functions + newfunction(:is_float, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/is_integer.rb b/is_integer.rb new file mode 100644 index 0000000..44337f0 --- /dev/null +++ b/is_integer.rb @@ -0,0 +1,12 @@ +# +# is_integer.rb +# + +module Puppet::Parser::Functions + newfunction(:is_integer, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/is_numeric.rb b/is_numeric.rb new file mode 100644 index 0000000..7a64091 --- /dev/null +++ b/is_numeric.rb @@ -0,0 +1,12 @@ +# +# is_numeric.rb +# + +module Puppet::Parser::Functions + newfunction(:is_numeric, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/is_valid_domain_name.rb b/is_valid_domain_name.rb new file mode 100644 index 0000000..05d64be --- /dev/null +++ b/is_valid_domain_name.rb @@ -0,0 +1,12 @@ +# +# is_valid_doman_name.rb +# + +module Puppet::Parser::Functions + newfunction(:is_valid_doman_name, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/is_valid_ip_address.rb b/is_valid_ip_address.rb new file mode 100644 index 0000000..e6b68a8 --- /dev/null +++ b/is_valid_ip_address.rb @@ -0,0 +1,12 @@ +# +# is_valid_ip_address.rb +# + +module Puppet::Parser::Functions + newfunction(:is_valid_ip_address, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/is_valid_mac_address.rb b/is_valid_mac_address.rb new file mode 100644 index 0000000..5e354c9 --- /dev/null +++ b/is_valid_mac_address.rb @@ -0,0 +1,12 @@ +# +# is_valid_mac_address.rb +# + +module Puppet::Parser::Functions + newfunction(:is_valid_mac_address, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/is_valid_netmask.rb b/is_valid_netmask.rb new file mode 100644 index 0000000..2aeb374 --- /dev/null +++ b/is_valid_netmask.rb @@ -0,0 +1,12 @@ +# +# is_valid_netmask.rb +# + +module Puppet::Parser::Functions + newfunction(:is_valid_netmask, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/sort.rb b/sort.rb new file mode 100644 index 0000000..85e5ba0 --- /dev/null +++ b/sort.rb @@ -0,0 +1,12 @@ +# +# sort.rb +# + +module Puppet::Parser::Functions + newfunction(:sort, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 8733a57ffb6e831274e73eb03fe1662ffa6e04d7 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 03:44:59 +0100 Subject: Making sure that the function hash will also work on older Rubies. Signed-off-by: Krzysztof Wilczynski --- hash.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hash.rb b/hash.rb index 7e63d43..f0c01d4 100644 --- a/hash.rb +++ b/hash.rb @@ -19,6 +19,8 @@ module Puppet::Parser::Functions result = {} begin + # This is to make it compatible with older version of Ruby ... + array = array.flatten result = Hash[*array] rescue Exception raise(Puppet::ParseError, 'hash(): Unable to compute ' + -- cgit v1.2.3 From d38e399b47390f5bf0e7a5ff2669ab7e4f2fef3c Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 14:35:10 +0100 Subject: First version. Simple swapcase function to use within Puppet DSL. Signed-off-by: Krzysztof Wilczynski --- swapcase.rb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 swapcase.rb diff --git a/swapcase.rb b/swapcase.rb new file mode 100644 index 0000000..a0002a9 --- /dev/null +++ b/swapcase.rb @@ -0,0 +1,32 @@ +# +# swapcase.rb +# + +module Puppet::Parser::Functions + newfunction(:swapcase, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "swapcase(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'swapcase(): Requires either ' + + 'array or string to work with') + end + + if value.is_a?(Array) + # Numbers in Puppet are often string-encoded which is troublesome ... + result = value.collect { |i| i.is_a?(String) ? i.swapcase : i } + else + result = value.swapcase + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 781a8720577d0cafe2147f3a1b938db7bec31789 Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sat, 30 Apr 2011 14:36:25 +0100 Subject: Add number of stub code for future functions. Signed-off-by: Krzysztof Wilczynski --- load_json.rb | 12 ++++++++++++ load_yaml.rb | 12 ++++++++++++ rand.rb | 12 ++++++++++++ squeeze.rb | 12 ++++++++++++ 4 files changed, 48 insertions(+) create mode 100644 load_json.rb create mode 100644 load_yaml.rb create mode 100644 rand.rb create mode 100644 squeeze.rb diff --git a/load_json.rb b/load_json.rb new file mode 100644 index 0000000..9ec8c78 --- /dev/null +++ b/load_json.rb @@ -0,0 +1,12 @@ +# +# load_json.rb +# + +module Puppet::Parser::Functions + newfunction(:load_json, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/load_yaml.rb b/load_yaml.rb new file mode 100644 index 0000000..684a721 --- /dev/null +++ b/load_yaml.rb @@ -0,0 +1,12 @@ +# +# load_yaml.rb +# + +module Puppet::Parser::Functions + newfunction(:load_yaml, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/rand.rb b/rand.rb new file mode 100644 index 0000000..2cb9acb --- /dev/null +++ b/rand.rb @@ -0,0 +1,12 @@ +# +# rand.rb +# + +module Puppet::Parser::Functions + newfunction(:rand, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/squeeze.rb b/squeeze.rb new file mode 100644 index 0000000..a135bd3 --- /dev/null +++ b/squeeze.rb @@ -0,0 +1,12 @@ +# +# squeeze.rb +# + +module Puppet::Parser::Functions + newfunction(:squeeze, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 99a93d366f2e1efb977fcc8fe300d3d8357c8214 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Apr 2011 09:27:10 +0200 Subject: Convert to module format. --- .gitignore | 1 + Modulefile | 8 +++ README | 6 +- abs.rb | 34 ---------- bool2num.rb | 45 ------------ count.rb | 36 ---------- delete_at.rb | 42 ------------ empty.rb | 27 -------- fact.rb | 36 ---------- is_array.rb | 21 ------ is_hash.rb | 21 ------ is_string.rb | 21 ------ join.rb | 34 ---------- join_with_prefix.rb | 38 ----------- keys.rb | 25 ------- lib/puppet/parser/functions/abs.rb | 34 ++++++++++ lib/puppet/parser/functions/bool2num.rb | 45 ++++++++++++ lib/puppet/parser/functions/count.rb | 36 ++++++++++ lib/puppet/parser/functions/delete_at.rb | 42 ++++++++++++ lib/puppet/parser/functions/empty.rb | 27 ++++++++ lib/puppet/parser/functions/fact.rb | 36 ++++++++++ lib/puppet/parser/functions/is_array.rb | 21 ++++++ lib/puppet/parser/functions/is_hash.rb | 21 ++++++ lib/puppet/parser/functions/is_string.rb | 21 ++++++ lib/puppet/parser/functions/join.rb | 34 ++++++++++ lib/puppet/parser/functions/join_with_prefix.rb | 38 +++++++++++ lib/puppet/parser/functions/keys.rb | 25 +++++++ lib/puppet/parser/functions/load_variables.rb | 77 +++++++++++++++++++++ lib/puppet/parser/functions/member.rb | 33 +++++++++ lib/puppet/parser/functions/num2bool.rb | 38 +++++++++++ .../parser/functions/persistent_crontab_minutes.rb | 63 +++++++++++++++++ lib/puppet/parser/functions/prefix.rb | 38 +++++++++++ .../parser/functions/random_crontab_minutes.rb | 31 +++++++++ lib/puppet/parser/functions/range.rb | 59 ++++++++++++++++ lib/puppet/parser/functions/reverse.rb | 27 ++++++++ lib/puppet/parser/functions/shuffle.rb | 45 ++++++++++++ lib/puppet/parser/functions/size.rb | 47 +++++++++++++ lib/puppet/parser/functions/strftime.rb | 44 ++++++++++++ lib/puppet/parser/functions/time.rb | 37 ++++++++++ lib/puppet/parser/functions/unique.rb | 34 ++++++++++ lib/puppet/parser/functions/values.rb | 25 +++++++ lib/puppet/parser/functions/values_at.rb | 79 ++++++++++++++++++++++ load_variables.rb | 77 --------------------- num2bool.rb | 38 ----------- persistent_crontab_minutes.rb | 63 ----------------- prefix.rb | 38 ----------- random_crontab_minutes.rb | 31 --------- range.rb | 59 ---------------- reverse.rb | 27 -------- shuffle.rb | 45 ------------ size.rb | 47 ------------- strftime.rb | 44 ------------ time.rb | 37 ---------- unique.rb | 34 ---------- values.rb | 25 ------- values_at.rb | 79 ---------------------- 56 files changed, 1069 insertions(+), 1027 deletions(-) create mode 100644 .gitignore create mode 100644 Modulefile delete mode 100644 abs.rb delete mode 100644 bool2num.rb delete mode 100644 count.rb delete mode 100644 delete_at.rb delete mode 100644 empty.rb delete mode 100644 fact.rb delete mode 100644 is_array.rb delete mode 100644 is_hash.rb delete mode 100644 is_string.rb delete mode 100644 join.rb delete mode 100644 join_with_prefix.rb delete mode 100644 keys.rb create mode 100644 lib/puppet/parser/functions/abs.rb create mode 100644 lib/puppet/parser/functions/bool2num.rb create mode 100644 lib/puppet/parser/functions/count.rb create mode 100644 lib/puppet/parser/functions/delete_at.rb create mode 100644 lib/puppet/parser/functions/empty.rb create mode 100644 lib/puppet/parser/functions/fact.rb create mode 100644 lib/puppet/parser/functions/is_array.rb create mode 100644 lib/puppet/parser/functions/is_hash.rb create mode 100644 lib/puppet/parser/functions/is_string.rb create mode 100644 lib/puppet/parser/functions/join.rb create mode 100644 lib/puppet/parser/functions/join_with_prefix.rb create mode 100644 lib/puppet/parser/functions/keys.rb create mode 100644 lib/puppet/parser/functions/load_variables.rb create mode 100644 lib/puppet/parser/functions/member.rb create mode 100644 lib/puppet/parser/functions/num2bool.rb create mode 100644 lib/puppet/parser/functions/persistent_crontab_minutes.rb create mode 100644 lib/puppet/parser/functions/prefix.rb create mode 100644 lib/puppet/parser/functions/random_crontab_minutes.rb create mode 100644 lib/puppet/parser/functions/range.rb create mode 100644 lib/puppet/parser/functions/reverse.rb create mode 100644 lib/puppet/parser/functions/shuffle.rb create mode 100644 lib/puppet/parser/functions/size.rb create mode 100644 lib/puppet/parser/functions/strftime.rb create mode 100644 lib/puppet/parser/functions/time.rb create mode 100644 lib/puppet/parser/functions/unique.rb create mode 100644 lib/puppet/parser/functions/values.rb create mode 100644 lib/puppet/parser/functions/values_at.rb delete mode 100644 load_variables.rb delete mode 100644 num2bool.rb delete mode 100644 persistent_crontab_minutes.rb delete mode 100644 prefix.rb delete mode 100644 random_crontab_minutes.rb delete mode 100644 range.rb delete mode 100644 reverse.rb delete mode 100644 shuffle.rb delete mode 100644 size.rb delete mode 100644 strftime.rb delete mode 100644 time.rb delete mode 100644 unique.rb delete mode 100644 values.rb delete mode 100644 values_at.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..01d0a08 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +pkg/ diff --git a/Modulefile b/Modulefile new file mode 100644 index 0000000..9b7d1e1 --- /dev/null +++ b/Modulefile @@ -0,0 +1,8 @@ +name 'kwilczynski/puppet-functions' +version '0.0.1' +source 'https://github.com/kwilczynski/puppet-functions' +author 'Krzysztof Wilczynski' +license 'UNKNOWN' +summary 'A set of basic functions for puppet.' +description 'This module provides a set of basic functions for puppet that extend the standard library.' +project_page 'https://github.com/kwilczynski/puppet-functions' diff --git a/README b/README index c3a69c3..b01d803 100644 --- a/README +++ b/README @@ -1,3 +1,3 @@ -Various functions to use within Puppet will be stored here. Most of them -are to be run by Puppet in order to extend its Domain Specific Language (DSL) -abilities and functionality. +puppet-functions + +This is the puppet-functions module. diff --git a/abs.rb b/abs.rb deleted file mode 100644 index 0a554e4..0000000 --- a/abs.rb +++ /dev/null @@ -1,34 +0,0 @@ -# -# abs.rb -# - -module Puppet::Parser::Functions - newfunction(:abs, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "abs(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - value = arguments[0] - - # Numbers in Puppet are often string-encoded which is troublesome ... - if value.is_a?(String) - if value.match(/^-?(?:\d+)(?:\.\d+){1}$/) - value = value.to_f - elsif value.match(/^-?\d+$/) - value = value.to_i - else - raise(Puppet::ParseError, 'abs(): Requires float or ' + - 'integer to work with') - end - end - - # We have numeric value to handle ... - result = value.abs - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/bool2num.rb b/bool2num.rb deleted file mode 100644 index b2989d0..0000000 --- a/bool2num.rb +++ /dev/null @@ -1,45 +0,0 @@ -# -# bool2num.rb -# - -module Puppet::Parser::Functions - newfunction(:bool2num, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "bool2num(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - value = arguments[0] - klass = value.class - - # We can have either true or false, or string which resembles boolean ... - unless [FalseClass, TrueClass, String].include?(klass) - raise(Puppet::ParseError, 'bool2num(): Requires either ' + - 'boolean or string to work with') - end - - if value.is_a?(String) - # We consider all the yes, no, y, n and so on too ... - value = case value - # - # This is how undef looks like in Puppet ... - # We yield 0 (or false if you wish) 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 /^(undef|undefined)$/ then false # This is not likely to happen ... - else - raise(Puppet::ParseError, 'bool2num(): Unknown type of boolean given') - end - end - - # We have real boolean values as well ... - result = value ? 1 : 0 - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/count.rb b/count.rb deleted file mode 100644 index c4e2283..0000000 --- a/count.rb +++ /dev/null @@ -1,36 +0,0 @@ -# -# count.rb -# - -# TODO(Krzysztof Wilczynski): We need to add support for regular expression ... -# TODO(Krzysztof Wilczynski): Support for hash values would be nice too ... - -module Puppet::Parser::Functions - newfunction(:count, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - # Technically we support two arguments but only first is mandatory ... - raise(Puppet::ParseError, "count(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - value = arguments[0] - klass = value.class - - unless [Array, Hash, String].include?(klass) - raise(Puppet::ParseError, 'count(): Requires either ' + - 'array, hash or string to work with') - end - - item = arguments[1] if arguments[1] - - value = value.is_a?(Hash) ? value.keys : value - - # No item to look for and count? Then just return current size ... - result = item ? value.count(item) : value.size - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/delete_at.rb b/delete_at.rb deleted file mode 100644 index 10190ba..0000000 --- a/delete_at.rb +++ /dev/null @@ -1,42 +0,0 @@ -# -# delete_at.rb -# - -module Puppet::Parser::Functions - newfunction(:delete_at, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "delete_at(): Wrong number of arguments " + - "given (#{arguments.size} for 2)") if arguments.size < 2 - - array = arguments[0] - - unless array.is_a?(Array) - raise(Puppet::ParseError, 'delete_at(): Requires array to work with') - end - - index = arguments[1] - - if index.is_a?(String) and not index.match(/^\d+$/) - raise(Puppet::ParseError, 'delete_at(): You must provide ' + - 'non-negative numeric index') - end - - result = array.clone - - # Numbers in Puppet are often string-encoded which is troublesome ... - index = index.to_i - - if index > result.size - 1 # First element is at index 0 is it not? - raise(Puppet::ParseError, 'delete_at(): Given index ' + - 'exceeds size of array given') - end - - result.delete_at(index) # We ignore the element that got deleted ... - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/empty.rb b/empty.rb deleted file mode 100644 index e78ebf0..0000000 --- a/empty.rb +++ /dev/null @@ -1,27 +0,0 @@ -# -# empty.rb -# - -module Puppet::Parser::Functions - newfunction(:empty, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "empty(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - value = arguments[0] - klass = value.class - - unless [Array, Hash, String].include?(klass) - raise(Puppet::ParseError, 'empty(): Requires either ' + - 'array, hash or string to work with') - end - - result = value.empty? - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/fact.rb b/fact.rb deleted file mode 100644 index 27b7bb2..0000000 --- a/fact.rb +++ /dev/null @@ -1,36 +0,0 @@ -# -# fact.rb -# - -module Puppet::Parser::Functions - newfunction(:fact, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "fact(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - fact = arguments[0] - - unless fact.is_a?(String) - raise(Puppet::ParseError, 'fact(): Requires fact name to be a string') - end - - raise(Puppet::ParseError, 'fact(): You must provide ' + - 'fact name') if fact.empty? - - result = lookupvar(fact) # Get the value of interest from Facter ... - - # - # Now this is a funny one ... Puppet does not have a concept of - # returning neither undef nor nil back for use within the Puppet DSL - # and empty string is as closest to actual undef as you we can get - # at this point in time ... - # - result = result.empty? ? '' : result - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/is_array.rb b/is_array.rb deleted file mode 100644 index 0b508fd..0000000 --- a/is_array.rb +++ /dev/null @@ -1,21 +0,0 @@ -# -# is_array.rb -# - -module Puppet::Parser::Functions - newfunction(:is_array, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "is_array(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - type = arguments[0] - - result = type.is_a?(Array) - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/is_hash.rb b/is_hash.rb deleted file mode 100644 index 259809c..0000000 --- a/is_hash.rb +++ /dev/null @@ -1,21 +0,0 @@ -# -# is_hash.rb -# - -module Puppet::Parser::Functions - newfunction(:is_array, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "is_hash(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - type = arguments[0] - - result = type.is_a?(Hash) - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/is_string.rb b/is_string.rb deleted file mode 100644 index 61037e3..0000000 --- a/is_string.rb +++ /dev/null @@ -1,21 +0,0 @@ -# -# is_string.rb -# - -module Puppet::Parser::Functions - newfunction(:is_string, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "is_string(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - type = arguments[0] - - result = type.is_a?(String) - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/join.rb b/join.rb deleted file mode 100644 index 945556a..0000000 --- a/join.rb +++ /dev/null @@ -1,34 +0,0 @@ -# -# join.rb -# - -module Puppet::Parser::Functions - newfunction(:join, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - # Technically we support two arguments but only first is mandatory ... - raise(Puppet::ParseError, "join(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - array = arguments[0] - - unless array.is_a?(Array) - raise(Puppet::ParseError, 'join(): Requires array to work with') - end - - suffix = arguments[1] if arguments[1] - - if suffix - unless suffix.is_a?(String) - raise(Puppet::ParseError, 'join(): Requires string to work with') - end - end - - result = suffix ? array.join(suffix) : array.join - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/join_with_prefix.rb b/join_with_prefix.rb deleted file mode 100644 index 8bf96d9..0000000 --- a/join_with_prefix.rb +++ /dev/null @@ -1,38 +0,0 @@ -# -# join_with_prefix.rb -# - -module Puppet::Parser::Functions - newfunction(:join_with_prefix, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - # Technically we support three arguments but only first is mandatory ... - raise(Puppet::ParseError, "join(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - array = arguments[0] - - unless array.is_a?(Array) - raise(Puppet::ParseError, 'join_with_prefix(): Requires ' + - 'array to work with') - end - - prefix = arguments[1] if arguments[1] - suffix = arguments[2] if arguments[2] - - if prefix and suffix - result = prefix + array.join(suffix + prefix) - elsif prefix and not suffix - result = array.collect { |i| prefix ? prefix + i : i } - elsif suffix and not prefix - result = array.join(suffix) - else - result = array.join - end - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/keys.rb b/keys.rb deleted file mode 100644 index 3a92a47..0000000 --- a/keys.rb +++ /dev/null @@ -1,25 +0,0 @@ -# -# keys.rb -# - -module Puppet::Parser::Functions - newfunction(:keys, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "keys(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - hash = arguments[0] - - unless hash.is_a?(Hash) - raise(Puppet::ParseError, 'keys(): Requires hash to work with') - end - - result = hash.keys - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/abs.rb b/lib/puppet/parser/functions/abs.rb new file mode 100644 index 0000000..0a554e4 --- /dev/null +++ b/lib/puppet/parser/functions/abs.rb @@ -0,0 +1,34 @@ +# +# abs.rb +# + +module Puppet::Parser::Functions + newfunction(:abs, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "abs(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + + # Numbers in Puppet are often string-encoded which is troublesome ... + if value.is_a?(String) + if value.match(/^-?(?:\d+)(?:\.\d+){1}$/) + value = value.to_f + elsif value.match(/^-?\d+$/) + value = value.to_i + else + raise(Puppet::ParseError, 'abs(): Requires float or ' + + 'integer to work with') + end + end + + # We have numeric value to handle ... + result = value.abs + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/bool2num.rb b/lib/puppet/parser/functions/bool2num.rb new file mode 100644 index 0000000..b2989d0 --- /dev/null +++ b/lib/puppet/parser/functions/bool2num.rb @@ -0,0 +1,45 @@ +# +# bool2num.rb +# + +module Puppet::Parser::Functions + newfunction(:bool2num, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "bool2num(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + # We can have either true or false, or string which resembles boolean ... + unless [FalseClass, TrueClass, String].include?(klass) + raise(Puppet::ParseError, 'bool2num(): Requires either ' + + 'boolean or string to work with') + end + + if value.is_a?(String) + # We consider all the yes, no, y, n and so on too ... + value = case value + # + # This is how undef looks like in Puppet ... + # We yield 0 (or false if you wish) 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 /^(undef|undefined)$/ then false # This is not likely to happen ... + else + raise(Puppet::ParseError, 'bool2num(): Unknown type of boolean given') + end + end + + # We have real boolean values as well ... + result = value ? 1 : 0 + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/count.rb b/lib/puppet/parser/functions/count.rb new file mode 100644 index 0000000..c4e2283 --- /dev/null +++ b/lib/puppet/parser/functions/count.rb @@ -0,0 +1,36 @@ +# +# count.rb +# + +# TODO(Krzysztof Wilczynski): We need to add support for regular expression ... +# TODO(Krzysztof Wilczynski): Support for hash values would be nice too ... + +module Puppet::Parser::Functions + newfunction(:count, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + # Technically we support two arguments but only first is mandatory ... + raise(Puppet::ParseError, "count(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + unless [Array, Hash, String].include?(klass) + raise(Puppet::ParseError, 'count(): Requires either ' + + 'array, hash or string to work with') + end + + item = arguments[1] if arguments[1] + + value = value.is_a?(Hash) ? value.keys : value + + # No item to look for and count? Then just return current size ... + result = item ? value.count(item) : value.size + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/delete_at.rb b/lib/puppet/parser/functions/delete_at.rb new file mode 100644 index 0000000..10190ba --- /dev/null +++ b/lib/puppet/parser/functions/delete_at.rb @@ -0,0 +1,42 @@ +# +# delete_at.rb +# + +module Puppet::Parser::Functions + newfunction(:delete_at, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "delete_at(): Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + array = arguments[0] + + unless array.is_a?(Array) + raise(Puppet::ParseError, 'delete_at(): Requires array to work with') + end + + index = arguments[1] + + if index.is_a?(String) and not index.match(/^\d+$/) + raise(Puppet::ParseError, 'delete_at(): You must provide ' + + 'non-negative numeric index') + end + + result = array.clone + + # Numbers in Puppet are often string-encoded which is troublesome ... + index = index.to_i + + if index > result.size - 1 # First element is at index 0 is it not? + raise(Puppet::ParseError, 'delete_at(): Given index ' + + 'exceeds size of array given') + end + + result.delete_at(index) # We ignore the element that got deleted ... + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/empty.rb b/lib/puppet/parser/functions/empty.rb new file mode 100644 index 0000000..e78ebf0 --- /dev/null +++ b/lib/puppet/parser/functions/empty.rb @@ -0,0 +1,27 @@ +# +# empty.rb +# + +module Puppet::Parser::Functions + newfunction(:empty, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "empty(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + unless [Array, Hash, String].include?(klass) + raise(Puppet::ParseError, 'empty(): Requires either ' + + 'array, hash or string to work with') + end + + result = value.empty? + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/fact.rb b/lib/puppet/parser/functions/fact.rb new file mode 100644 index 0000000..27b7bb2 --- /dev/null +++ b/lib/puppet/parser/functions/fact.rb @@ -0,0 +1,36 @@ +# +# fact.rb +# + +module Puppet::Parser::Functions + newfunction(:fact, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "fact(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + fact = arguments[0] + + unless fact.is_a?(String) + raise(Puppet::ParseError, 'fact(): Requires fact name to be a string') + end + + raise(Puppet::ParseError, 'fact(): You must provide ' + + 'fact name') if fact.empty? + + result = lookupvar(fact) # Get the value of interest from Facter ... + + # + # Now this is a funny one ... Puppet does not have a concept of + # returning neither undef nor nil back for use within the Puppet DSL + # and empty string is as closest to actual undef as you we can get + # at this point in time ... + # + result = result.empty? ? '' : result + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_array.rb b/lib/puppet/parser/functions/is_array.rb new file mode 100644 index 0000000..0b508fd --- /dev/null +++ b/lib/puppet/parser/functions/is_array.rb @@ -0,0 +1,21 @@ +# +# is_array.rb +# + +module Puppet::Parser::Functions + newfunction(:is_array, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "is_array(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + type = arguments[0] + + result = type.is_a?(Array) + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_hash.rb b/lib/puppet/parser/functions/is_hash.rb new file mode 100644 index 0000000..259809c --- /dev/null +++ b/lib/puppet/parser/functions/is_hash.rb @@ -0,0 +1,21 @@ +# +# is_hash.rb +# + +module Puppet::Parser::Functions + newfunction(:is_array, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "is_hash(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + type = arguments[0] + + result = type.is_a?(Hash) + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_string.rb b/lib/puppet/parser/functions/is_string.rb new file mode 100644 index 0000000..61037e3 --- /dev/null +++ b/lib/puppet/parser/functions/is_string.rb @@ -0,0 +1,21 @@ +# +# is_string.rb +# + +module Puppet::Parser::Functions + newfunction(:is_string, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "is_string(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + type = arguments[0] + + result = type.is_a?(String) + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/join.rb b/lib/puppet/parser/functions/join.rb new file mode 100644 index 0000000..945556a --- /dev/null +++ b/lib/puppet/parser/functions/join.rb @@ -0,0 +1,34 @@ +# +# join.rb +# + +module Puppet::Parser::Functions + newfunction(:join, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + # Technically we support two arguments but only first is mandatory ... + raise(Puppet::ParseError, "join(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + unless array.is_a?(Array) + raise(Puppet::ParseError, 'join(): Requires array to work with') + end + + suffix = arguments[1] if arguments[1] + + if suffix + unless suffix.is_a?(String) + raise(Puppet::ParseError, 'join(): Requires string to work with') + end + end + + result = suffix ? array.join(suffix) : array.join + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/join_with_prefix.rb b/lib/puppet/parser/functions/join_with_prefix.rb new file mode 100644 index 0000000..8bf96d9 --- /dev/null +++ b/lib/puppet/parser/functions/join_with_prefix.rb @@ -0,0 +1,38 @@ +# +# join_with_prefix.rb +# + +module Puppet::Parser::Functions + newfunction(:join_with_prefix, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + # Technically we support three arguments but only first is mandatory ... + raise(Puppet::ParseError, "join(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + unless array.is_a?(Array) + raise(Puppet::ParseError, 'join_with_prefix(): Requires ' + + 'array to work with') + end + + prefix = arguments[1] if arguments[1] + suffix = arguments[2] if arguments[2] + + if prefix and suffix + result = prefix + array.join(suffix + prefix) + elsif prefix and not suffix + result = array.collect { |i| prefix ? prefix + i : i } + elsif suffix and not prefix + result = array.join(suffix) + else + result = array.join + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/keys.rb b/lib/puppet/parser/functions/keys.rb new file mode 100644 index 0000000..3a92a47 --- /dev/null +++ b/lib/puppet/parser/functions/keys.rb @@ -0,0 +1,25 @@ +# +# keys.rb +# + +module Puppet::Parser::Functions + newfunction(:keys, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "keys(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + hash = arguments[0] + + unless hash.is_a?(Hash) + raise(Puppet::ParseError, 'keys(): Requires hash to work with') + end + + result = hash.keys + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/load_variables.rb b/lib/puppet/parser/functions/load_variables.rb new file mode 100644 index 0000000..a28c64b --- /dev/null +++ b/lib/puppet/parser/functions/load_variables.rb @@ -0,0 +1,77 @@ +# +# load_variables.rb +# + +module Puppet::Parser::Functions + newfunction(:load_variables, :type => :statement, :doc => <<-EOS +This function will allow for loading variables from an external YAML +file and expose them for further use inside the Puppet manifest file ... + +For example: + +Given following content of the data.yaml file: + + --- + host1.example.com: + foo: bar + baz: quux + question: 42 + host2.example.com: + abc: def + this: that + darth: vader + +Then calling load_variables in Puppet manifest file as follows: + + load_variables("/etc/puppet/data.yaml", $fqdn) + +Will result in addition of variables $foo, $baz and $question +for matching host name as per the variable $fqdn ... + +Another example which uses per-host file: + +Given following content of the file data-host1.example.com.yaml: + + --- + foo: bar + +Then when we call load_variables like this: + + load_variables("/etc/puppet/data-${fqdn}.yaml") + +This will result in a variable $foo being added and ready for use. + EOS + ) do |arguments| + + raise(Puppet::ParseError, "load_variables(): Wrong number of " + + "arguments given (#{arguments.size} for 2)") if arguments.size < 2 + + data = {} + + file = arguments[0] + key = arguments[1] if arguments[1] + + if File.exists?(file) + + begin + data = YAML.load_file(file) + rescue => error + raise(Puppet::ParseError, "load_variables(): Unable to load data " + + "from the file `%s': %s" % file, error.to_s) + end + + raise(Puppet::ParseError, "load_variables(): Data in the file `%s' " + + "is not a hash" % file) unless data.is_a?(Hash) + + data = ((data[key] and data[key].is_a?(Hash)) ? data[key] : {}) if key + end + + data.each do |param, value| + value = strinterp(value) # Evaluate any interpolated variable names ... + + setvar(param, value) + end + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/member.rb b/lib/puppet/parser/functions/member.rb new file mode 100644 index 0000000..a491a76 --- /dev/null +++ b/lib/puppet/parser/functions/member.rb @@ -0,0 +1,33 @@ +# +# include.rb +# + +# TODO(Krzysztof Wilczynski): We need to add support for regular expression ... +# TODO(Krzysztof Wilczynski): Support for strings and hashes too ... + +module Puppet::Parser::Functions + newfunction(:includes, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "includes(): Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + array = arguments[0] + + if not array.is_a?(Array) + raise(Puppet::ParseError, 'includes(): Requires an array to work with') + end + + item = arguments[1] + + raise(Puppet::ParseError, 'includes(): You must provide item ' + + 'to search for within given array') if item.empty? + + result = array.include?(item) + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/num2bool.rb b/lib/puppet/parser/functions/num2bool.rb new file mode 100644 index 0000000..2baef62 --- /dev/null +++ b/lib/puppet/parser/functions/num2bool.rb @@ -0,0 +1,38 @@ +# +# num2bool.rb +# + +# TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ... + +module Puppet::Parser::Functions + newfunction(:num2bool, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "num2bool(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + number = arguments[0] + + # Only numbers allowed ... + unless number.match(/^\-?\d+$/) + raise(Puppet::ParseError, 'num2bool(): Requires integer to work with') + end + + result = case number + when /^0$/ + false + when /^\-?\d+$/ + # Numbers in Puppet are often string-encoded which is troublesome ... + number = number.to_i + # We yield true for any positive number and false otherwise ... + number > 0 ? true : false + else + raise(Puppet::ParseError, 'num2bool(): Unknown numeric format given') + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/persistent_crontab_minutes.rb b/lib/puppet/parser/functions/persistent_crontab_minutes.rb new file mode 100644 index 0000000..cd80094 --- /dev/null +++ b/lib/puppet/parser/functions/persistent_crontab_minutes.rb @@ -0,0 +1,63 @@ +# +# persistent_crontab_minutes.rb +# + +module Puppet::Parser::Functions + newfunction(:persistent_crontab_minutes, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + require 'md5' + + value = 0 + + job = arguments[0] + host = arguments[1] + + environment = Puppet[:environment] + + # We select first directory that exists. This might not be the best idea ... + modules = Puppet[:modulepath].split(':').select { |i| File.exists?(i) }.first + + raise(Puppet::ParseError, "Unable to determine the storage " + + "directory for Puppet modules") unless modules + + # Prepare the file where we store current value ... + file = "/puppet/state/crontab/#{host}-#{job}.minutes" + file = File.join(modules, file) + + # Get the directory portion from the file name ... + directory = File.dirname(file) + + FileUtils.mkdir_p(directory) unless File.directory?(directory) + + if FileTest.exists?(file) + File.open(file, 'r') { |f| value = f.read.to_i } + + raise(Puppet::ParseError, "The value for minutes in the file `%s' " + + "is out of the range from 0 to 59 inclusive") unless value < 60 + else + # + # Pick a random number based on the job and host name. This will yield + # the same value for exactly the same combination of the job and host name. + # + value = MD5.new(job_name + host).to_s.hex % 60 + + # Minutes are from 0 to 59 inclusive ... + value = value < 60 ? value : 59 + + File.open(file, 'w') { |f| f.write(value) } + end + + # Tell Puppet to keep an eye on this file ... + parser = Puppet::Parser::Parser.new(environment) + parser.watch_file(file) if File.exists?(file) + + return value + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/prefix.rb b/lib/puppet/parser/functions/prefix.rb new file mode 100644 index 0000000..0e0cee2 --- /dev/null +++ b/lib/puppet/parser/functions/prefix.rb @@ -0,0 +1,38 @@ +# +# prefix.rb +# + +module Puppet::Parser::Functions + newfunction(:prefix, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + # Technically we support two arguments but only first is mandatory ... + raise(Puppet::ParseError, "prefix(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + unless array.is_a?(Array) + raise(Puppet::ParseError, 'prefix(): Requires array to work with') + end + + prefix = arguments[1] if arguments[1] + + if prefix + unless prefix.is_a?(String) + raise(Puppet::ParseError, 'prefix(): Requires string to work with') + end + end + + # Turn everything into string same as join would do ... + result = array.collect do |i| + i = i.to_s + prefix ? prefix + i : i + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/random_crontab_minutes.rb b/lib/puppet/parser/functions/random_crontab_minutes.rb new file mode 100644 index 0000000..8ab29e1 --- /dev/null +++ b/lib/puppet/parser/functions/random_crontab_minutes.rb @@ -0,0 +1,31 @@ +# +# random_crontab_minutes.rb +# + +module Puppet::Parser::Functions + newfunction(:random_crontab_minutes, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + require 'md5' + + job_name = arguments[0] + host = arguments[1] + + # + # Pick a random number based on the job and host name. This will yield + # the same value for exactly the same combination of the job and host name. + # + value = MD5.new(job_name + host).to_s.hex % 60 + + # Minutes are from 0 to 59 inclusive ... + value = value < 60 ? value : 59 + + return value + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/range.rb b/lib/puppet/parser/functions/range.rb new file mode 100644 index 0000000..6afb50c --- /dev/null +++ b/lib/puppet/parser/functions/range.rb @@ -0,0 +1,59 @@ +# +# range.rb +# + +# TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ... + +module Puppet::Parser::Functions + newfunction(:range, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + # We support more than one argument but at least one is mandatory ... + raise(Puppet::ParseError, "range(): Wrong number of " + + "arguments given (#{arguments.size} for 1)") if arguments.size < 1 + + if arguments.size > 1 + start = arguments[0] + stop = arguments[1] + + type = '..' # We select simplest type for Range available in Ruby ... + + elsif arguments.size > 0 + value = arguments[0] + + if m = value.match(/^(\w+)(\.\.\.?|\-)(\w+)$/) + start = m[1] + stop = m[3] + + type = m[2] + + elsif value.match(/^.+$/) + raise(Puppet::ParseError, 'range(): Unable to compute range ' + + 'from the value given') + else + raise(Puppet::ParseError, 'range(): Unknown format of range given') + end + end + + # Check whether we have integer value if so then make it so ... + if start.match(/^\d+$/) + start = start.to_i + stop = stop.to_i + else + start = start.to_s + stop = stop.to_s + end + + range = case type + when /^(\.\.|\-)$/ then (start .. stop) + when /^(\.\.\.)$/ then (start ... stop) # Exclusive of last element ... + end + + result = range.collect { |i| i } # Get them all ... Pokemon ... + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/reverse.rb b/lib/puppet/parser/functions/reverse.rb new file mode 100644 index 0000000..79e9b93 --- /dev/null +++ b/lib/puppet/parser/functions/reverse.rb @@ -0,0 +1,27 @@ +# +# reverse.rb +# + +module Puppet::Parser::Functions + newfunction(:reverse, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "reverse(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'reverse(): Requires either ' + + 'array or string to work with') + end + + result = value.reverse + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/shuffle.rb b/lib/puppet/parser/functions/shuffle.rb new file mode 100644 index 0000000..73e798c --- /dev/null +++ b/lib/puppet/parser/functions/shuffle.rb @@ -0,0 +1,45 @@ +# +# shuffle.rb +# + +module Puppet::Parser::Functions + newfunction(:shuffle, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "shuffle(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'shuffle(): Requires either ' + + 'array or string to work with') + end + + result = value.clone + + string = value.is_a?(String) ? true : false + + # Check whether it makes sense to shuffle ... + return result if result.size <= 1 + + # We turn any string value into an array to be able to shuffle ... + result = string ? result.split('') : result + + elements = result.size + + # Simple implementation of Fisher–Yates in-place shuffle ... + elements.times do |i| + j = rand(elements - i) + i + result[j], result[i] = result[i], result[j] + end + + result = string ? result.join : result + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/size.rb b/lib/puppet/parser/functions/size.rb new file mode 100644 index 0000000..aa4f4ad --- /dev/null +++ b/lib/puppet/parser/functions/size.rb @@ -0,0 +1,47 @@ +# +# size.rb +# + +# TODO(Krzysztof Wilczynski): Support for hashes would be nice too ... + +module Puppet::Parser::Functions + newfunction(:size, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "size(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + item = arguments[0] + + if item.is_a?(String) + + begin + # + # Check whether your item is a numeric value or not ... + # This will take care about positive and/or negative numbers + # for both integer and floating-point values ... + # + # Please note that Puppet has no notion of hexadecimal + # nor octal numbers for its DSL at this point in time ... + # + Float(item) + + raise(Puppet::ParseError, 'size(): Requires either ' + + 'string or array to work with') + + rescue ArgumentError + result = item.size + end + + elsif item.is_a?(Array) + result = item.size + else + raise(Puppet::ParseError, 'size(): Unknown type given') + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/strftime.rb b/lib/puppet/parser/functions/strftime.rb new file mode 100644 index 0000000..c919320 --- /dev/null +++ b/lib/puppet/parser/functions/strftime.rb @@ -0,0 +1,44 @@ +# +# strftime.rb +# + +module Puppet::Parser::Functions + newfunction(:strftime, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + # Technically we support two arguments but only first is mandatory ... + raise(Puppet::ParseError, "strftime(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + format = arguments[0] + + raise(Puppet::ParseError, 'strftime(): You must provide ' + + 'format for evaluation') if format.empty? + + # The Time Zone argument is optional ... + time_zone = arguments[1] if arguments[1] + + time = Time.new + + # There is probably a better way to handle Time Zone ... + if time_zone and not time_zone.empty? + original_zone = ENV['TZ'] + + local_time = time.clone + local_time = local_time.utc + + ENV['TZ'] = time_zone + + time = local_time.localtime + + ENV['TZ'] = original_zone + end + + result = time.strftime(format) + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/time.rb b/lib/puppet/parser/functions/time.rb new file mode 100644 index 0000000..f7c1041 --- /dev/null +++ b/lib/puppet/parser/functions/time.rb @@ -0,0 +1,37 @@ +# +# time.rb +# + +module Puppet::Parser::Functions + newfunction(:time, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + # The Time Zone argument is optional ... + time_zone = arguments[0] if arguments[0] + + time = Time.new + + # There is probably a better way to handle Time Zone ... + if time_zone and not time_zone.empty? + original_zone = ENV['TZ'] + + local_time = time.clone + local_time = local_time.utc + + ENV['TZ'] = time_zone + + time = local_time.localtime + + ENV['TZ'] = original_zone + end + + # Calling Time#to_i on a receiver changes it. Trust me I am the Doctor. + result = time.strftime('%s') + result = result.to_i + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/unique.rb b/lib/puppet/parser/functions/unique.rb new file mode 100644 index 0000000..a922c94 --- /dev/null +++ b/lib/puppet/parser/functions/unique.rb @@ -0,0 +1,34 @@ +# +# unique.rb +# + +module Puppet::Parser::Functions + newfunction(:unique, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "unique(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'unique(): Requires either ' + + 'array or string to work with') + end + + result = value.clone + + string = value.is_a?(String) ? true : false + + # We turn any string value into an array to be able to shuffle ... + result = string ? result.split('') : result + result = result.uniq # Remove duplicates ... + result = string ? result.join : result + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/values.rb b/lib/puppet/parser/functions/values.rb new file mode 100644 index 0000000..c1c4a77 --- /dev/null +++ b/lib/puppet/parser/functions/values.rb @@ -0,0 +1,25 @@ +# +# values.rb +# + +module Puppet::Parser::Functions + newfunction(:values, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "values(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + hash = arguments[0] + + unless hash.is_a?(Hash) + raise(Puppet::ParseError, 'values(): Requires hash to work with') + end + + result = hash.values + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/values_at.rb b/lib/puppet/parser/functions/values_at.rb new file mode 100644 index 0000000..331af6a --- /dev/null +++ b/lib/puppet/parser/functions/values_at.rb @@ -0,0 +1,79 @@ +# +# values_at.rb +# + +# TODO(Krzysztof Wilczynski): Support for hashes would be nice too ... +# TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ... + +module Puppet::Parser::Functions + newfunction(:values_at, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "values_at(): Wrong number of " + + "arguments given (#{arguments.size} for 2)") if arguments.size < 2 + + array = arguments.shift + + unless array.is_a?(Array) + raise(Puppet::ParseError, 'values_at(): Requires array to work with') + end + + indices = *arguments # Get them all ... Pokemon ... + + if not indices or indices.empty? + raise(Puppet::ParseError, 'values_at(): You must provide ' + + 'at least one positive index to collect') + end + + result = [] + indices_list = [] + + indices.each do |i| + if m = i.match(/^(\d+)(\.\.\.?|\-)(\d+)$/) + start = m[1].to_i + stop = m[3].to_i + + type = m[2] + + if start > stop + raise(Puppet::ParseError, 'values_at(): Stop index in ' + + 'given indices range is smaller than the start index') + elsif stop > array.size - 1 # First element is at index 0 is it not? + raise(Puppet::ParseError, 'values_at(): Stop index in ' + + 'given indices range exceeds array size') + end + + range = case type + when /^(\.\.|\-)$/ then (start .. stop) + when /^(\.\.\.)$/ then (start ... stop) # Exclusive of last element ... + end + + range.each { |i| indices_list << i.to_i } + else + # Only positive numbers allowed in this case ... + if not i.match(/^\d+$/) + raise(Puppet::ParseError, 'values_at(): Unknown format ' + + 'of given index') + end + + # In Puppet numbers are often string-encoded ... + i = i.to_i + + if i > array.size - 1 # Same story. First element is at index 0 ... + raise(Puppet::ParseError, 'values_at(): Given index ' + + 'exceeds array size') + end + + indices_list << i + end + end + + # We remove nil values as they make no sense in Puppet DSL ... + result = indices_list.collect { |i| array[i] }.compact + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/load_variables.rb b/load_variables.rb deleted file mode 100644 index a28c64b..0000000 --- a/load_variables.rb +++ /dev/null @@ -1,77 +0,0 @@ -# -# load_variables.rb -# - -module Puppet::Parser::Functions - newfunction(:load_variables, :type => :statement, :doc => <<-EOS -This function will allow for loading variables from an external YAML -file and expose them for further use inside the Puppet manifest file ... - -For example: - -Given following content of the data.yaml file: - - --- - host1.example.com: - foo: bar - baz: quux - question: 42 - host2.example.com: - abc: def - this: that - darth: vader - -Then calling load_variables in Puppet manifest file as follows: - - load_variables("/etc/puppet/data.yaml", $fqdn) - -Will result in addition of variables $foo, $baz and $question -for matching host name as per the variable $fqdn ... - -Another example which uses per-host file: - -Given following content of the file data-host1.example.com.yaml: - - --- - foo: bar - -Then when we call load_variables like this: - - load_variables("/etc/puppet/data-${fqdn}.yaml") - -This will result in a variable $foo being added and ready for use. - EOS - ) do |arguments| - - raise(Puppet::ParseError, "load_variables(): Wrong number of " + - "arguments given (#{arguments.size} for 2)") if arguments.size < 2 - - data = {} - - file = arguments[0] - key = arguments[1] if arguments[1] - - if File.exists?(file) - - begin - data = YAML.load_file(file) - rescue => error - raise(Puppet::ParseError, "load_variables(): Unable to load data " + - "from the file `%s': %s" % file, error.to_s) - end - - raise(Puppet::ParseError, "load_variables(): Data in the file `%s' " + - "is not a hash" % file) unless data.is_a?(Hash) - - data = ((data[key] and data[key].is_a?(Hash)) ? data[key] : {}) if key - end - - data.each do |param, value| - value = strinterp(value) # Evaluate any interpolated variable names ... - - setvar(param, value) - end - end -end - -# vim: set ts=2 sw=2 et : diff --git a/num2bool.rb b/num2bool.rb deleted file mode 100644 index 2baef62..0000000 --- a/num2bool.rb +++ /dev/null @@ -1,38 +0,0 @@ -# -# num2bool.rb -# - -# TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ... - -module Puppet::Parser::Functions - newfunction(:num2bool, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "num2bool(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - number = arguments[0] - - # Only numbers allowed ... - unless number.match(/^\-?\d+$/) - raise(Puppet::ParseError, 'num2bool(): Requires integer to work with') - end - - result = case number - when /^0$/ - false - when /^\-?\d+$/ - # Numbers in Puppet are often string-encoded which is troublesome ... - number = number.to_i - # We yield true for any positive number and false otherwise ... - number > 0 ? true : false - else - raise(Puppet::ParseError, 'num2bool(): Unknown numeric format given') - end - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/persistent_crontab_minutes.rb b/persistent_crontab_minutes.rb deleted file mode 100644 index cd80094..0000000 --- a/persistent_crontab_minutes.rb +++ /dev/null @@ -1,63 +0,0 @@ -# -# persistent_crontab_minutes.rb -# - -module Puppet::Parser::Functions - newfunction(:persistent_crontab_minutes, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "Wrong number of arguments " + - "given (#{arguments.size} for 2)") if arguments.size < 2 - - require 'md5' - - value = 0 - - job = arguments[0] - host = arguments[1] - - environment = Puppet[:environment] - - # We select first directory that exists. This might not be the best idea ... - modules = Puppet[:modulepath].split(':').select { |i| File.exists?(i) }.first - - raise(Puppet::ParseError, "Unable to determine the storage " + - "directory for Puppet modules") unless modules - - # Prepare the file where we store current value ... - file = "/puppet/state/crontab/#{host}-#{job}.minutes" - file = File.join(modules, file) - - # Get the directory portion from the file name ... - directory = File.dirname(file) - - FileUtils.mkdir_p(directory) unless File.directory?(directory) - - if FileTest.exists?(file) - File.open(file, 'r') { |f| value = f.read.to_i } - - raise(Puppet::ParseError, "The value for minutes in the file `%s' " + - "is out of the range from 0 to 59 inclusive") unless value < 60 - else - # - # Pick a random number based on the job and host name. This will yield - # the same value for exactly the same combination of the job and host name. - # - value = MD5.new(job_name + host).to_s.hex % 60 - - # Minutes are from 0 to 59 inclusive ... - value = value < 60 ? value : 59 - - File.open(file, 'w') { |f| f.write(value) } - end - - # Tell Puppet to keep an eye on this file ... - parser = Puppet::Parser::Parser.new(environment) - parser.watch_file(file) if File.exists?(file) - - return value - end -end - -# vim: set ts=2 sw=2 et : diff --git a/prefix.rb b/prefix.rb deleted file mode 100644 index 0e0cee2..0000000 --- a/prefix.rb +++ /dev/null @@ -1,38 +0,0 @@ -# -# prefix.rb -# - -module Puppet::Parser::Functions - newfunction(:prefix, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - # Technically we support two arguments but only first is mandatory ... - raise(Puppet::ParseError, "prefix(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - array = arguments[0] - - unless array.is_a?(Array) - raise(Puppet::ParseError, 'prefix(): Requires array to work with') - end - - prefix = arguments[1] if arguments[1] - - if prefix - unless prefix.is_a?(String) - raise(Puppet::ParseError, 'prefix(): Requires string to work with') - end - end - - # Turn everything into string same as join would do ... - result = array.collect do |i| - i = i.to_s - prefix ? prefix + i : i - end - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/random_crontab_minutes.rb b/random_crontab_minutes.rb deleted file mode 100644 index 8ab29e1..0000000 --- a/random_crontab_minutes.rb +++ /dev/null @@ -1,31 +0,0 @@ -# -# random_crontab_minutes.rb -# - -module Puppet::Parser::Functions - newfunction(:random_crontab_minutes, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "Wrong number of arguments " + - "given (#{arguments.size} for 2)") if arguments.size < 2 - - require 'md5' - - job_name = arguments[0] - host = arguments[1] - - # - # Pick a random number based on the job and host name. This will yield - # the same value for exactly the same combination of the job and host name. - # - value = MD5.new(job_name + host).to_s.hex % 60 - - # Minutes are from 0 to 59 inclusive ... - value = value < 60 ? value : 59 - - return value - end -end - -# vim: set ts=2 sw=2 et : diff --git a/range.rb b/range.rb deleted file mode 100644 index 6afb50c..0000000 --- a/range.rb +++ /dev/null @@ -1,59 +0,0 @@ -# -# range.rb -# - -# TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ... - -module Puppet::Parser::Functions - newfunction(:range, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - # We support more than one argument but at least one is mandatory ... - raise(Puppet::ParseError, "range(): Wrong number of " + - "arguments given (#{arguments.size} for 1)") if arguments.size < 1 - - if arguments.size > 1 - start = arguments[0] - stop = arguments[1] - - type = '..' # We select simplest type for Range available in Ruby ... - - elsif arguments.size > 0 - value = arguments[0] - - if m = value.match(/^(\w+)(\.\.\.?|\-)(\w+)$/) - start = m[1] - stop = m[3] - - type = m[2] - - elsif value.match(/^.+$/) - raise(Puppet::ParseError, 'range(): Unable to compute range ' + - 'from the value given') - else - raise(Puppet::ParseError, 'range(): Unknown format of range given') - end - end - - # Check whether we have integer value if so then make it so ... - if start.match(/^\d+$/) - start = start.to_i - stop = stop.to_i - else - start = start.to_s - stop = stop.to_s - end - - range = case type - when /^(\.\.|\-)$/ then (start .. stop) - when /^(\.\.\.)$/ then (start ... stop) # Exclusive of last element ... - end - - result = range.collect { |i| i } # Get them all ... Pokemon ... - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/reverse.rb b/reverse.rb deleted file mode 100644 index 79e9b93..0000000 --- a/reverse.rb +++ /dev/null @@ -1,27 +0,0 @@ -# -# reverse.rb -# - -module Puppet::Parser::Functions - newfunction(:reverse, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "reverse(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - value = arguments[0] - klass = value.class - - unless [Array, String].include?(klass) - raise(Puppet::ParseError, 'reverse(): Requires either ' + - 'array or string to work with') - end - - result = value.reverse - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/shuffle.rb b/shuffle.rb deleted file mode 100644 index 73e798c..0000000 --- a/shuffle.rb +++ /dev/null @@ -1,45 +0,0 @@ -# -# shuffle.rb -# - -module Puppet::Parser::Functions - newfunction(:shuffle, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "shuffle(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - value = arguments[0] - klass = value.class - - unless [Array, String].include?(klass) - raise(Puppet::ParseError, 'shuffle(): Requires either ' + - 'array or string to work with') - end - - result = value.clone - - string = value.is_a?(String) ? true : false - - # Check whether it makes sense to shuffle ... - return result if result.size <= 1 - - # We turn any string value into an array to be able to shuffle ... - result = string ? result.split('') : result - - elements = result.size - - # Simple implementation of Fisher–Yates in-place shuffle ... - elements.times do |i| - j = rand(elements - i) + i - result[j], result[i] = result[i], result[j] - end - - result = string ? result.join : result - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/size.rb b/size.rb deleted file mode 100644 index aa4f4ad..0000000 --- a/size.rb +++ /dev/null @@ -1,47 +0,0 @@ -# -# size.rb -# - -# TODO(Krzysztof Wilczynski): Support for hashes would be nice too ... - -module Puppet::Parser::Functions - newfunction(:size, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "size(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - item = arguments[0] - - if item.is_a?(String) - - begin - # - # Check whether your item is a numeric value or not ... - # This will take care about positive and/or negative numbers - # for both integer and floating-point values ... - # - # Please note that Puppet has no notion of hexadecimal - # nor octal numbers for its DSL at this point in time ... - # - Float(item) - - raise(Puppet::ParseError, 'size(): Requires either ' + - 'string or array to work with') - - rescue ArgumentError - result = item.size - end - - elsif item.is_a?(Array) - result = item.size - else - raise(Puppet::ParseError, 'size(): Unknown type given') - end - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/strftime.rb b/strftime.rb deleted file mode 100644 index c919320..0000000 --- a/strftime.rb +++ /dev/null @@ -1,44 +0,0 @@ -# -# strftime.rb -# - -module Puppet::Parser::Functions - newfunction(:strftime, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - # Technically we support two arguments but only first is mandatory ... - raise(Puppet::ParseError, "strftime(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - format = arguments[0] - - raise(Puppet::ParseError, 'strftime(): You must provide ' + - 'format for evaluation') if format.empty? - - # The Time Zone argument is optional ... - time_zone = arguments[1] if arguments[1] - - time = Time.new - - # There is probably a better way to handle Time Zone ... - if time_zone and not time_zone.empty? - original_zone = ENV['TZ'] - - local_time = time.clone - local_time = local_time.utc - - ENV['TZ'] = time_zone - - time = local_time.localtime - - ENV['TZ'] = original_zone - end - - result = time.strftime(format) - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/time.rb b/time.rb deleted file mode 100644 index f7c1041..0000000 --- a/time.rb +++ /dev/null @@ -1,37 +0,0 @@ -# -# time.rb -# - -module Puppet::Parser::Functions - newfunction(:time, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - # The Time Zone argument is optional ... - time_zone = arguments[0] if arguments[0] - - time = Time.new - - # There is probably a better way to handle Time Zone ... - if time_zone and not time_zone.empty? - original_zone = ENV['TZ'] - - local_time = time.clone - local_time = local_time.utc - - ENV['TZ'] = time_zone - - time = local_time.localtime - - ENV['TZ'] = original_zone - end - - # Calling Time#to_i on a receiver changes it. Trust me I am the Doctor. - result = time.strftime('%s') - result = result.to_i - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/unique.rb b/unique.rb deleted file mode 100644 index a922c94..0000000 --- a/unique.rb +++ /dev/null @@ -1,34 +0,0 @@ -# -# unique.rb -# - -module Puppet::Parser::Functions - newfunction(:unique, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "unique(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - value = arguments[0] - klass = value.class - - unless [Array, String].include?(klass) - raise(Puppet::ParseError, 'unique(): Requires either ' + - 'array or string to work with') - end - - result = value.clone - - string = value.is_a?(String) ? true : false - - # We turn any string value into an array to be able to shuffle ... - result = string ? result.split('') : result - result = result.uniq # Remove duplicates ... - result = string ? result.join : result - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/values.rb b/values.rb deleted file mode 100644 index c1c4a77..0000000 --- a/values.rb +++ /dev/null @@ -1,25 +0,0 @@ -# -# values.rb -# - -module Puppet::Parser::Functions - newfunction(:values, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "values(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - hash = arguments[0] - - unless hash.is_a?(Hash) - raise(Puppet::ParseError, 'values(): Requires hash to work with') - end - - result = hash.values - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/values_at.rb b/values_at.rb deleted file mode 100644 index 331af6a..0000000 --- a/values_at.rb +++ /dev/null @@ -1,79 +0,0 @@ -# -# values_at.rb -# - -# TODO(Krzysztof Wilczynski): Support for hashes would be nice too ... -# TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ... - -module Puppet::Parser::Functions - newfunction(:values_at, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "values_at(): Wrong number of " + - "arguments given (#{arguments.size} for 2)") if arguments.size < 2 - - array = arguments.shift - - unless array.is_a?(Array) - raise(Puppet::ParseError, 'values_at(): Requires array to work with') - end - - indices = *arguments # Get them all ... Pokemon ... - - if not indices or indices.empty? - raise(Puppet::ParseError, 'values_at(): You must provide ' + - 'at least one positive index to collect') - end - - result = [] - indices_list = [] - - indices.each do |i| - if m = i.match(/^(\d+)(\.\.\.?|\-)(\d+)$/) - start = m[1].to_i - stop = m[3].to_i - - type = m[2] - - if start > stop - raise(Puppet::ParseError, 'values_at(): Stop index in ' + - 'given indices range is smaller than the start index') - elsif stop > array.size - 1 # First element is at index 0 is it not? - raise(Puppet::ParseError, 'values_at(): Stop index in ' + - 'given indices range exceeds array size') - end - - range = case type - when /^(\.\.|\-)$/ then (start .. stop) - when /^(\.\.\.)$/ then (start ... stop) # Exclusive of last element ... - end - - range.each { |i| indices_list << i.to_i } - else - # Only positive numbers allowed in this case ... - if not i.match(/^\d+$/) - raise(Puppet::ParseError, 'values_at(): Unknown format ' + - 'of given index') - end - - # In Puppet numbers are often string-encoded ... - i = i.to_i - - if i > array.size - 1 # Same story. First element is at index 0 ... - raise(Puppet::ParseError, 'values_at(): Given index ' + - 'exceeds array size') - end - - indices_list << i - end - end - - # We remove nil values as they make no sense in Puppet DSL ... - result = indices_list.collect { |i| array[i] }.compact - - return result - end -end - -# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 323cd874c5274408d5346dfeafbf530ea5debd91 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Apr 2011 09:55:55 +0200 Subject: Change README. --- README | 3 --- README.markdown | 3 +++ 2 files changed, 3 insertions(+), 3 deletions(-) delete mode 100644 README create mode 100644 README.markdown diff --git a/README b/README deleted file mode 100644 index b01d803..0000000 --- a/README +++ /dev/null @@ -1,3 +0,0 @@ -puppet-functions - -This is the puppet-functions module. diff --git a/README.markdown b/README.markdown new file mode 100644 index 0000000..66ff373 --- /dev/null +++ b/README.markdown @@ -0,0 +1,3 @@ +## puppet-functions module + +This is the puppet-functions module. It provides a library of various functions that extend the basic function library that is provided in Puppet. -- cgit v1.2.3 From 5c8f8f8edb3d50346de8b24467eaa46448e2cd8c Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Apr 2011 10:30:11 +0200 Subject: Added Apache license. --- LICENSE | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b6b37d9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,18 @@ +Puppet - Automating Configuration Management. + +Copyright (C) 2011 Krzysztof Wilczynski +Copyright (C) 2011 Puppet Labs Inc + +Puppet Labs can be contacted at: info@puppetlabs.com + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. -- cgit v1.2.3 From 352bac3703f012c88f2317307cc51d22dc80ebcc Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Apr 2011 16:51:47 +0200 Subject: Moved type into pluginsync compat area. --- type.rb | 40 ---------------------------------------- 1 file changed, 40 deletions(-) delete mode 100644 type.rb diff --git a/type.rb b/type.rb deleted file mode 100644 index 3051de6..0000000 --- a/type.rb +++ /dev/null @@ -1,40 +0,0 @@ -# -# type.rb -# - -module Puppet::Parser::Functions - newfunction(:type, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "type(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - value = arguments[0] - - klass = value.class - - # This should cover all the generic types present in Puppet at present ... - unless [Array, Bignum, Fixnum, FalseClass, - Float, Hash, String, TrueClass].include?(klass) - - raise(Puppet::ParseError, 'type(): Unknown type given') - end - - klass = klass.to_s # Ugly ... - - # - # We note that Integer is the parent to Bignum and Fixnum ... - # Plus we claim name Boolean for FalseClass and TrueClass ... - # - result = case klass - when /^(?:Big|Fix)num$/ then 'Integer' - when /^(?:False|True)Class$/ then 'Boolean' - else klass - end - - return result - end -end - -# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 09abea2d47b09b9e8933a1c27c7a454c77760e83 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Apr 2011 16:52:14 +0200 Subject: Moved type.rb --- lib/puppet/parser/functions/type.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 lib/puppet/parser/functions/type.rb diff --git a/lib/puppet/parser/functions/type.rb b/lib/puppet/parser/functions/type.rb new file mode 100644 index 0000000..389a056 --- /dev/null +++ b/lib/puppet/parser/functions/type.rb @@ -0,0 +1,33 @@ +# +# type.rb +# + +module Puppet::Parser::Functions + newfunction(:type, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "type(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + + klass = value.class + + if not [Array, Bignum, Fixnum, Float, Hash, String].include?(klass) + raise(Puppet::ParseError, 'type(): Unknown type') + end + + klass = klass.to_s # Ugly ... + + # We note that Integer is the parent to Bignum and Fixnum ... + result = case klass + when /^(?:Big|Fix)num$/ then 'Integer' + else klass + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From aafce9c99b779855e1e10e5a337848fa9f676a01 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Sat, 30 Apr 2011 16:00:49 +0200 Subject: Moved more functions into lib/puppet/parser/functions/ --- capitalize.rb | 32 ------------- chomp.rb | 32 ------------- chop.rb | 32 ------------- date.rb | 12 ----- delete.rb | 15 ------ downcase.rb | 32 ------------- flatten.rb | 25 ---------- grep.rb | 12 ----- hash.rb | 34 ------------- is_float.rb | 12 ----- is_integer.rb | 12 ----- is_numeric.rb | 12 ----- is_valid_domain_name.rb | 12 ----- is_valid_ip_address.rb | 12 ----- is_valid_mac_address.rb | 12 ----- is_valid_netmask.rb | 12 ----- lib/puppet/parser/functions/capitalize.rb | 32 +++++++++++++ lib/puppet/parser/functions/chomp.rb | 32 +++++++++++++ lib/puppet/parser/functions/chop.rb | 32 +++++++++++++ lib/puppet/parser/functions/date.rb | 12 +++++ lib/puppet/parser/functions/delete.rb | 15 ++++++ lib/puppet/parser/functions/downcase.rb | 32 +++++++++++++ lib/puppet/parser/functions/flatten.rb | 25 ++++++++++ lib/puppet/parser/functions/grep.rb | 12 +++++ lib/puppet/parser/functions/hash.rb | 34 +++++++++++++ lib/puppet/parser/functions/is_float.rb | 12 +++++ lib/puppet/parser/functions/is_integer.rb | 12 +++++ lib/puppet/parser/functions/is_numeric.rb | 12 +++++ .../parser/functions/is_valid_domain_name.rb | 12 +++++ lib/puppet/parser/functions/is_valid_ip_address.rb | 12 +++++ .../parser/functions/is_valid_mac_address.rb | 12 +++++ lib/puppet/parser/functions/is_valid_netmask.rb | 12 +++++ lib/puppet/parser/functions/load_json.rb | 12 +++++ lib/puppet/parser/functions/load_yaml.rb | 12 +++++ lib/puppet/parser/functions/lstrip.rb | 32 +++++++++++++ lib/puppet/parser/functions/member.rb | 14 +++--- lib/puppet/parser/functions/rand.rb | 12 +++++ lib/puppet/parser/functions/rstrip.rb | 31 ++++++++++++ lib/puppet/parser/functions/sort.rb | 12 +++++ lib/puppet/parser/functions/squeeze.rb | 12 +++++ lib/puppet/parser/functions/str2bool.rb | 38 +++++++++++++++ lib/puppet/parser/functions/strip.rb | 31 ++++++++++++ lib/puppet/parser/functions/swapcase.rb | 32 +++++++++++++ lib/puppet/parser/functions/upcase.rb | 32 +++++++++++++ lib/puppet/parser/functions/zip.rb | 56 ++++++++++++++++++++++ load_json.rb | 12 ----- load_yaml.rb | 12 ----- lstrip.rb | 32 ------------- member.rb | 33 ------------- rand.rb | 12 ----- rstrip.rb | 31 ------------ sort.rb | 12 ----- squeeze.rb | 12 ----- str2bool.rb | 38 --------------- strip.rb | 31 ------------ swapcase.rb | 32 ------------- upcase.rb | 32 ------------- zip.rb | 56 ---------------------- 58 files changed, 629 insertions(+), 662 deletions(-) delete mode 100644 capitalize.rb delete mode 100644 chomp.rb delete mode 100644 chop.rb delete mode 100644 date.rb delete mode 100644 delete.rb delete mode 100644 downcase.rb delete mode 100644 flatten.rb delete mode 100644 grep.rb delete mode 100644 hash.rb delete mode 100644 is_float.rb delete mode 100644 is_integer.rb delete mode 100644 is_numeric.rb delete mode 100644 is_valid_domain_name.rb delete mode 100644 is_valid_ip_address.rb delete mode 100644 is_valid_mac_address.rb delete mode 100644 is_valid_netmask.rb create mode 100644 lib/puppet/parser/functions/capitalize.rb create mode 100644 lib/puppet/parser/functions/chomp.rb create mode 100644 lib/puppet/parser/functions/chop.rb create mode 100644 lib/puppet/parser/functions/date.rb create mode 100644 lib/puppet/parser/functions/delete.rb create mode 100644 lib/puppet/parser/functions/downcase.rb create mode 100644 lib/puppet/parser/functions/flatten.rb create mode 100644 lib/puppet/parser/functions/grep.rb create mode 100644 lib/puppet/parser/functions/hash.rb create mode 100644 lib/puppet/parser/functions/is_float.rb create mode 100644 lib/puppet/parser/functions/is_integer.rb create mode 100644 lib/puppet/parser/functions/is_numeric.rb create mode 100644 lib/puppet/parser/functions/is_valid_domain_name.rb create mode 100644 lib/puppet/parser/functions/is_valid_ip_address.rb create mode 100644 lib/puppet/parser/functions/is_valid_mac_address.rb create mode 100644 lib/puppet/parser/functions/is_valid_netmask.rb create mode 100644 lib/puppet/parser/functions/load_json.rb create mode 100644 lib/puppet/parser/functions/load_yaml.rb create mode 100644 lib/puppet/parser/functions/lstrip.rb create mode 100644 lib/puppet/parser/functions/rand.rb create mode 100644 lib/puppet/parser/functions/rstrip.rb create mode 100644 lib/puppet/parser/functions/sort.rb create mode 100644 lib/puppet/parser/functions/squeeze.rb create mode 100644 lib/puppet/parser/functions/str2bool.rb create mode 100644 lib/puppet/parser/functions/strip.rb create mode 100644 lib/puppet/parser/functions/swapcase.rb create mode 100644 lib/puppet/parser/functions/upcase.rb create mode 100644 lib/puppet/parser/functions/zip.rb delete mode 100644 load_json.rb delete mode 100644 load_yaml.rb delete mode 100644 lstrip.rb delete mode 100644 member.rb delete mode 100644 rand.rb delete mode 100644 rstrip.rb delete mode 100644 sort.rb delete mode 100644 squeeze.rb delete mode 100644 str2bool.rb delete mode 100644 strip.rb delete mode 100644 swapcase.rb delete mode 100644 upcase.rb delete mode 100644 zip.rb diff --git a/capitalize.rb b/capitalize.rb deleted file mode 100644 index f902cb3..0000000 --- a/capitalize.rb +++ /dev/null @@ -1,32 +0,0 @@ -# -# capitalize.rb -# - -module Puppet::Parser::Functions - newfunction(:capitalize, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "capitalize(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - value = arguments[0] - klass = value.class - - unless [Array, String].include?(klass) - raise(Puppet::ParseError, 'capitalize(): Requires either ' + - 'array or string to work with') - end - - if value.is_a?(Array) - # Numbers in Puppet are often string-encoded which is troublesome ... - result = value.collect { |i| i.is_a?(String) ? i.capitalize : i } - else - result = value.capitalize - end - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/chomp.rb b/chomp.rb deleted file mode 100644 index e1d788a..0000000 --- a/chomp.rb +++ /dev/null @@ -1,32 +0,0 @@ -# -# chomp.rb -# - -module Puppet::Parser::Functions - newfunction(:chomp, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "chomp(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - value = arguments[0] - klass = value.class - - unless [Array, String].include?(klass) - raise(Puppet::ParseError, 'chomp(): Requires either ' + - 'array or string to work with') - end - - if value.is_a?(Array) - # Numbers in Puppet are often string-encoded which is troublesome ... - result = value.collect { |i| i.is_a?(String) ? i.chomp : i } - else - result = value.chomp - end - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/chop.rb b/chop.rb deleted file mode 100644 index 0f9af86..0000000 --- a/chop.rb +++ /dev/null @@ -1,32 +0,0 @@ -# -# chop.rb -# - -module Puppet::Parser::Functions - newfunction(:chop, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "chop(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - value = arguments[0] - klass = value.class - - unless [Array, String].include?(klass) - raise(Puppet::ParseError, 'chop(): Requires either an ' + - 'array or string to work with') - end - - if value.is_a?(Array) - # Numbers in Puppet are often string-encoded which is troublesome ... - result = value.collect { |i| i.is_a?(String) ? i.chop : i } - else - result = value.chop - end - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/date.rb b/date.rb deleted file mode 100644 index ea11265..0000000 --- a/date.rb +++ /dev/null @@ -1,12 +0,0 @@ -# -# date.rb -# - -module Puppet::Parser::Functions - newfunction(:date, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - end -end - -# vim: set ts=2 sw=2 et : diff --git a/delete.rb b/delete.rb deleted file mode 100644 index b8376d1..0000000 --- a/delete.rb +++ /dev/null @@ -1,15 +0,0 @@ -# -# delete.rb -# - -# TODO(Krzysztof Wilczynski): We need to add support for regular expression ... -# TODO(Krzysztof Wilczynski): Support for strings and hashes too ... - -module Puppet::Parser::Functions - newfunction(:delete, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - end -end - -# vim: set ts=2 sw=2 et : diff --git a/downcase.rb b/downcase.rb deleted file mode 100644 index 71f8480..0000000 --- a/downcase.rb +++ /dev/null @@ -1,32 +0,0 @@ -# -# downcase.rb -# - -module Puppet::Parser::Functions - newfunction(:downcase, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "downcase(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - value = arguments[0] - klass = value.class - - unless [Array, String].include?(klass) - raise(Puppet::ParseError, 'downcase(): Requires either ' + - 'array or string to work with') - end - - if value.is_a?(Array) - # Numbers in Puppet are often string-encoded which is troublesome ... - result = value.collect { |i| i.is_a?(String) ? i.downcase : i } - else - result = value.downcase - end - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/flatten.rb b/flatten.rb deleted file mode 100644 index 6036f72..0000000 --- a/flatten.rb +++ /dev/null @@ -1,25 +0,0 @@ -# -# flatten.rb -# - -module Puppet::Parser::Functions - newfunction(:flatten, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "flatten(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - array = arguments[0] - - unless array.is_a?(Array) - raise(Puppet::ParseError, 'flatten(): Requires array to work with') - end - - result = array.flatten - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/grep.rb b/grep.rb deleted file mode 100644 index 1663fe7..0000000 --- a/grep.rb +++ /dev/null @@ -1,12 +0,0 @@ -# -# grep.rb -# - -module Puppet::Parser::Functions - newfunction(:grep, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - end -end - -# vim: set ts=2 sw=2 et : diff --git a/hash.rb b/hash.rb deleted file mode 100644 index f0c01d4..0000000 --- a/hash.rb +++ /dev/null @@ -1,34 +0,0 @@ -# -# hash.rb -# - -module Puppet::Parser::Functions - newfunction(:hash, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "hash(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - array = arguments[0] - - unless array.is_a?(Array) - raise(Puppet::ParseError, 'hash(): Requires array to work with') - end - - result = {} - - begin - # This is to make it compatible with older version of Ruby ... - array = array.flatten - result = Hash[*array] - rescue Exception - raise(Puppet::ParseError, 'hash(): Unable to compute ' + - 'hash from array given') - end - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/is_float.rb b/is_float.rb deleted file mode 100644 index 2a5a923..0000000 --- a/is_float.rb +++ /dev/null @@ -1,12 +0,0 @@ -# -# is_float.rb -# - -module Puppet::Parser::Functions - newfunction(:is_float, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - end -end - -# vim: set ts=2 sw=2 et : diff --git a/is_integer.rb b/is_integer.rb deleted file mode 100644 index 44337f0..0000000 --- a/is_integer.rb +++ /dev/null @@ -1,12 +0,0 @@ -# -# is_integer.rb -# - -module Puppet::Parser::Functions - newfunction(:is_integer, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - end -end - -# vim: set ts=2 sw=2 et : diff --git a/is_numeric.rb b/is_numeric.rb deleted file mode 100644 index 7a64091..0000000 --- a/is_numeric.rb +++ /dev/null @@ -1,12 +0,0 @@ -# -# is_numeric.rb -# - -module Puppet::Parser::Functions - newfunction(:is_numeric, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - end -end - -# vim: set ts=2 sw=2 et : diff --git a/is_valid_domain_name.rb b/is_valid_domain_name.rb deleted file mode 100644 index 05d64be..0000000 --- a/is_valid_domain_name.rb +++ /dev/null @@ -1,12 +0,0 @@ -# -# is_valid_doman_name.rb -# - -module Puppet::Parser::Functions - newfunction(:is_valid_doman_name, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - end -end - -# vim: set ts=2 sw=2 et : diff --git a/is_valid_ip_address.rb b/is_valid_ip_address.rb deleted file mode 100644 index e6b68a8..0000000 --- a/is_valid_ip_address.rb +++ /dev/null @@ -1,12 +0,0 @@ -# -# is_valid_ip_address.rb -# - -module Puppet::Parser::Functions - newfunction(:is_valid_ip_address, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - end -end - -# vim: set ts=2 sw=2 et : diff --git a/is_valid_mac_address.rb b/is_valid_mac_address.rb deleted file mode 100644 index 5e354c9..0000000 --- a/is_valid_mac_address.rb +++ /dev/null @@ -1,12 +0,0 @@ -# -# is_valid_mac_address.rb -# - -module Puppet::Parser::Functions - newfunction(:is_valid_mac_address, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - end -end - -# vim: set ts=2 sw=2 et : diff --git a/is_valid_netmask.rb b/is_valid_netmask.rb deleted file mode 100644 index 2aeb374..0000000 --- a/is_valid_netmask.rb +++ /dev/null @@ -1,12 +0,0 @@ -# -# is_valid_netmask.rb -# - -module Puppet::Parser::Functions - newfunction(:is_valid_netmask, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/capitalize.rb b/lib/puppet/parser/functions/capitalize.rb new file mode 100644 index 0000000..f902cb3 --- /dev/null +++ b/lib/puppet/parser/functions/capitalize.rb @@ -0,0 +1,32 @@ +# +# capitalize.rb +# + +module Puppet::Parser::Functions + newfunction(:capitalize, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "capitalize(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'capitalize(): Requires either ' + + 'array or string to work with') + end + + if value.is_a?(Array) + # Numbers in Puppet are often string-encoded which is troublesome ... + result = value.collect { |i| i.is_a?(String) ? i.capitalize : i } + else + result = value.capitalize + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/chomp.rb b/lib/puppet/parser/functions/chomp.rb new file mode 100644 index 0000000..e1d788a --- /dev/null +++ b/lib/puppet/parser/functions/chomp.rb @@ -0,0 +1,32 @@ +# +# chomp.rb +# + +module Puppet::Parser::Functions + newfunction(:chomp, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "chomp(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'chomp(): Requires either ' + + 'array or string to work with') + end + + if value.is_a?(Array) + # Numbers in Puppet are often string-encoded which is troublesome ... + result = value.collect { |i| i.is_a?(String) ? i.chomp : i } + else + result = value.chomp + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/chop.rb b/lib/puppet/parser/functions/chop.rb new file mode 100644 index 0000000..0f9af86 --- /dev/null +++ b/lib/puppet/parser/functions/chop.rb @@ -0,0 +1,32 @@ +# +# chop.rb +# + +module Puppet::Parser::Functions + newfunction(:chop, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "chop(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'chop(): Requires either an ' + + 'array or string to work with') + end + + if value.is_a?(Array) + # Numbers in Puppet are often string-encoded which is troublesome ... + result = value.collect { |i| i.is_a?(String) ? i.chop : i } + else + result = value.chop + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/date.rb b/lib/puppet/parser/functions/date.rb new file mode 100644 index 0000000..ea11265 --- /dev/null +++ b/lib/puppet/parser/functions/date.rb @@ -0,0 +1,12 @@ +# +# date.rb +# + +module Puppet::Parser::Functions + newfunction(:date, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/delete.rb b/lib/puppet/parser/functions/delete.rb new file mode 100644 index 0000000..b8376d1 --- /dev/null +++ b/lib/puppet/parser/functions/delete.rb @@ -0,0 +1,15 @@ +# +# delete.rb +# + +# TODO(Krzysztof Wilczynski): We need to add support for regular expression ... +# TODO(Krzysztof Wilczynski): Support for strings and hashes too ... + +module Puppet::Parser::Functions + newfunction(:delete, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/downcase.rb b/lib/puppet/parser/functions/downcase.rb new file mode 100644 index 0000000..71f8480 --- /dev/null +++ b/lib/puppet/parser/functions/downcase.rb @@ -0,0 +1,32 @@ +# +# downcase.rb +# + +module Puppet::Parser::Functions + newfunction(:downcase, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "downcase(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'downcase(): Requires either ' + + 'array or string to work with') + end + + if value.is_a?(Array) + # Numbers in Puppet are often string-encoded which is troublesome ... + result = value.collect { |i| i.is_a?(String) ? i.downcase : i } + else + result = value.downcase + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/flatten.rb b/lib/puppet/parser/functions/flatten.rb new file mode 100644 index 0000000..6036f72 --- /dev/null +++ b/lib/puppet/parser/functions/flatten.rb @@ -0,0 +1,25 @@ +# +# flatten.rb +# + +module Puppet::Parser::Functions + newfunction(:flatten, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "flatten(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + unless array.is_a?(Array) + raise(Puppet::ParseError, 'flatten(): Requires array to work with') + end + + result = array.flatten + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/grep.rb b/lib/puppet/parser/functions/grep.rb new file mode 100644 index 0000000..1663fe7 --- /dev/null +++ b/lib/puppet/parser/functions/grep.rb @@ -0,0 +1,12 @@ +# +# grep.rb +# + +module Puppet::Parser::Functions + newfunction(:grep, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/hash.rb b/lib/puppet/parser/functions/hash.rb new file mode 100644 index 0000000..f0c01d4 --- /dev/null +++ b/lib/puppet/parser/functions/hash.rb @@ -0,0 +1,34 @@ +# +# hash.rb +# + +module Puppet::Parser::Functions + newfunction(:hash, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "hash(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + unless array.is_a?(Array) + raise(Puppet::ParseError, 'hash(): Requires array to work with') + end + + result = {} + + begin + # This is to make it compatible with older version of Ruby ... + array = array.flatten + result = Hash[*array] + rescue Exception + raise(Puppet::ParseError, 'hash(): Unable to compute ' + + 'hash from array given') + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_float.rb b/lib/puppet/parser/functions/is_float.rb new file mode 100644 index 0000000..2a5a923 --- /dev/null +++ b/lib/puppet/parser/functions/is_float.rb @@ -0,0 +1,12 @@ +# +# is_float.rb +# + +module Puppet::Parser::Functions + newfunction(:is_float, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_integer.rb b/lib/puppet/parser/functions/is_integer.rb new file mode 100644 index 0000000..44337f0 --- /dev/null +++ b/lib/puppet/parser/functions/is_integer.rb @@ -0,0 +1,12 @@ +# +# is_integer.rb +# + +module Puppet::Parser::Functions + newfunction(:is_integer, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_numeric.rb b/lib/puppet/parser/functions/is_numeric.rb new file mode 100644 index 0000000..7a64091 --- /dev/null +++ b/lib/puppet/parser/functions/is_numeric.rb @@ -0,0 +1,12 @@ +# +# is_numeric.rb +# + +module Puppet::Parser::Functions + newfunction(:is_numeric, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_valid_domain_name.rb b/lib/puppet/parser/functions/is_valid_domain_name.rb new file mode 100644 index 0000000..05d64be --- /dev/null +++ b/lib/puppet/parser/functions/is_valid_domain_name.rb @@ -0,0 +1,12 @@ +# +# is_valid_doman_name.rb +# + +module Puppet::Parser::Functions + newfunction(:is_valid_doman_name, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_valid_ip_address.rb b/lib/puppet/parser/functions/is_valid_ip_address.rb new file mode 100644 index 0000000..e6b68a8 --- /dev/null +++ b/lib/puppet/parser/functions/is_valid_ip_address.rb @@ -0,0 +1,12 @@ +# +# is_valid_ip_address.rb +# + +module Puppet::Parser::Functions + newfunction(:is_valid_ip_address, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_valid_mac_address.rb b/lib/puppet/parser/functions/is_valid_mac_address.rb new file mode 100644 index 0000000..5e354c9 --- /dev/null +++ b/lib/puppet/parser/functions/is_valid_mac_address.rb @@ -0,0 +1,12 @@ +# +# is_valid_mac_address.rb +# + +module Puppet::Parser::Functions + newfunction(:is_valid_mac_address, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_valid_netmask.rb b/lib/puppet/parser/functions/is_valid_netmask.rb new file mode 100644 index 0000000..2aeb374 --- /dev/null +++ b/lib/puppet/parser/functions/is_valid_netmask.rb @@ -0,0 +1,12 @@ +# +# is_valid_netmask.rb +# + +module Puppet::Parser::Functions + newfunction(:is_valid_netmask, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/load_json.rb b/lib/puppet/parser/functions/load_json.rb new file mode 100644 index 0000000..9ec8c78 --- /dev/null +++ b/lib/puppet/parser/functions/load_json.rb @@ -0,0 +1,12 @@ +# +# load_json.rb +# + +module Puppet::Parser::Functions + newfunction(:load_json, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/load_yaml.rb b/lib/puppet/parser/functions/load_yaml.rb new file mode 100644 index 0000000..684a721 --- /dev/null +++ b/lib/puppet/parser/functions/load_yaml.rb @@ -0,0 +1,12 @@ +# +# load_yaml.rb +# + +module Puppet::Parser::Functions + newfunction(:load_yaml, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/lstrip.rb b/lib/puppet/parser/functions/lstrip.rb new file mode 100644 index 0000000..a7b2656 --- /dev/null +++ b/lib/puppet/parser/functions/lstrip.rb @@ -0,0 +1,32 @@ +# +# lstrip.rb +# + +module Puppet::Parser::Functions + newfunction(:lstrip, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "lstrip(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'lstrip(): Requires either ' + + 'array or string to work with') + end + + if value.is_a?(Array) + # Numbers in Puppet are often string-encoded which is troublesome ... + result = value.collect { |i| i.is_a?(String) ? i.lstrip : i } + else + result = value.lstrip + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/member.rb b/lib/puppet/parser/functions/member.rb index a491a76..bb43a37 100644 --- a/lib/puppet/parser/functions/member.rb +++ b/lib/puppet/parser/functions/member.rb @@ -1,28 +1,28 @@ # -# include.rb +# member.rb # # TODO(Krzysztof Wilczynski): We need to add support for regular expression ... # TODO(Krzysztof Wilczynski): Support for strings and hashes too ... module Puppet::Parser::Functions - newfunction(:includes, :type => :rvalue, :doc => <<-EOS + newfunction(:member, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| - raise(Puppet::ParseError, "includes(): Wrong number of arguments " + + raise(Puppet::ParseError, "member(): Wrong number of arguments " + "given (#{arguments.size} for 2)") if arguments.size < 2 array = arguments[0] - if not array.is_a?(Array) - raise(Puppet::ParseError, 'includes(): Requires an array to work with') + unless array.is_a?(Array) + raise(Puppet::ParseError, 'member(): Requires array to work with') end item = arguments[1] - raise(Puppet::ParseError, 'includes(): You must provide item ' + - 'to search for within given array') if item.empty? + raise(Puppet::ParseError, 'member(): You must provide item ' + + 'to search for within array given') if item.empty? result = array.include?(item) diff --git a/lib/puppet/parser/functions/rand.rb b/lib/puppet/parser/functions/rand.rb new file mode 100644 index 0000000..2cb9acb --- /dev/null +++ b/lib/puppet/parser/functions/rand.rb @@ -0,0 +1,12 @@ +# +# rand.rb +# + +module Puppet::Parser::Functions + newfunction(:rand, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/rstrip.rb b/lib/puppet/parser/functions/rstrip.rb new file mode 100644 index 0000000..56849d3 --- /dev/null +++ b/lib/puppet/parser/functions/rstrip.rb @@ -0,0 +1,31 @@ +# +# rstrip.rb +# + +module Puppet::Parser::Functions + newfunction(:rstrip, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "rstrip(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'rstrip(): Requires either ' + + 'array or string to work with') + end + + if value.is_a?(Array) + result = value.collect { |i| i.is_a?(String) ? i.rstrip : i } + else + result = value.rstrip + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/sort.rb b/lib/puppet/parser/functions/sort.rb new file mode 100644 index 0000000..85e5ba0 --- /dev/null +++ b/lib/puppet/parser/functions/sort.rb @@ -0,0 +1,12 @@ +# +# sort.rb +# + +module Puppet::Parser::Functions + newfunction(:sort, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/squeeze.rb b/lib/puppet/parser/functions/squeeze.rb new file mode 100644 index 0000000..a135bd3 --- /dev/null +++ b/lib/puppet/parser/functions/squeeze.rb @@ -0,0 +1,12 @@ +# +# squeeze.rb +# + +module Puppet::Parser::Functions + newfunction(:squeeze, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/str2bool.rb b/lib/puppet/parser/functions/str2bool.rb new file mode 100644 index 0000000..f3a6d6c --- /dev/null +++ b/lib/puppet/parser/functions/str2bool.rb @@ -0,0 +1,38 @@ +# +# str2bool.rb +# + +module Puppet::Parser::Functions + newfunction(:str2bool, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "str2bool(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + string = arguments[0] + + unless string.is_a?(String) + raise(Puppet::ParseError, 'str2bool(): Requires either ' + + 'string to work with') + end + + # We consider all the yes, no, y, n and so on too ... + result = case string + # + # This is how undef looks like in Puppet ... + # 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 /^(undef|undefined)$/ then false # This is not likely to happen ... + else + raise(Puppet::ParseError, 'str2bool(): Unknown type of boolean given') + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/strip.rb b/lib/puppet/parser/functions/strip.rb new file mode 100644 index 0000000..ebab20e --- /dev/null +++ b/lib/puppet/parser/functions/strip.rb @@ -0,0 +1,31 @@ +# +# strip.rb +# + +module Puppet::Parser::Functions + newfunction(:strip, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "strip(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'strip(): Requires either ' + + 'array or string to work with') + end + + if value.is_a?(Array) + result = value.collect { |i| i.is_a?(String) ? i.strip : i } + else + result = value.strip + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/swapcase.rb b/lib/puppet/parser/functions/swapcase.rb new file mode 100644 index 0000000..a0002a9 --- /dev/null +++ b/lib/puppet/parser/functions/swapcase.rb @@ -0,0 +1,32 @@ +# +# swapcase.rb +# + +module Puppet::Parser::Functions + newfunction(:swapcase, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "swapcase(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'swapcase(): Requires either ' + + 'array or string to work with') + end + + if value.is_a?(Array) + # Numbers in Puppet are often string-encoded which is troublesome ... + result = value.collect { |i| i.is_a?(String) ? i.swapcase : i } + else + result = value.swapcase + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/upcase.rb b/lib/puppet/parser/functions/upcase.rb new file mode 100644 index 0000000..8a9769d --- /dev/null +++ b/lib/puppet/parser/functions/upcase.rb @@ -0,0 +1,32 @@ +# +# upcase.rb +# + +module Puppet::Parser::Functions + newfunction(:upcase, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + raise(Puppet::ParseError, "upcase(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + value = arguments[0] + klass = value.class + + unless [Array, String].include?(klass) + raise(Puppet::ParseError, 'upcase(): Requires either ' + + 'array or string to work with') + end + + if value.is_a?(Array) + # Numbers in Puppet are often string-encoded which is troublesome ... + result = value.collect { |i| i.is_a?(String) ? i.upcase : i } + else + result = value.upcase + end + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/zip.rb b/lib/puppet/parser/functions/zip.rb new file mode 100644 index 0000000..024d64a --- /dev/null +++ b/lib/puppet/parser/functions/zip.rb @@ -0,0 +1,56 @@ +# +# zip.rb +# + +module Puppet::Parser::Functions + newfunction(:zip, :type => :rvalue, :doc => <<-EOS + EOS + ) do |arguments| + + # Technically we support three arguments but only first is mandatory ... + raise(Puppet::ParseError, "zip(): Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + a = arguments[0] + b = arguments[1] + + unless a.is_a?(Array) and b.is_a?(Array) + raise(Puppet::ParseError, 'zip(): Requires array to work with') + end + + flatten = arguments[2] if arguments[2] + + if flatten + klass = flatten.class + + # We can have either true or false, or string which resembles boolean ... + unless [FalseClass, TrueClass, String].include?(klass) + raise(Puppet::ParseError, 'zip(): Requires either ' + + 'boolean or string to work with') + end + + if flatten.is_a?(String) + # We consider all the yes, no, y, n and so on too ... + flatten = case flatten + # + # This is how undef looks like in Puppet ... + # 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 /^(undef|undefined)$/ then false # This is not likely to happen ... + else + raise(Puppet::ParseError, 'zip(): Unknown type of boolean given') + end + end + end + + result = a.zip(b) + result = flatten ? result.flatten : result + + return result + end +end + +# vim: set ts=2 sw=2 et : diff --git a/load_json.rb b/load_json.rb deleted file mode 100644 index 9ec8c78..0000000 --- a/load_json.rb +++ /dev/null @@ -1,12 +0,0 @@ -# -# load_json.rb -# - -module Puppet::Parser::Functions - newfunction(:load_json, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - end -end - -# vim: set ts=2 sw=2 et : diff --git a/load_yaml.rb b/load_yaml.rb deleted file mode 100644 index 684a721..0000000 --- a/load_yaml.rb +++ /dev/null @@ -1,12 +0,0 @@ -# -# load_yaml.rb -# - -module Puppet::Parser::Functions - newfunction(:load_yaml, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lstrip.rb b/lstrip.rb deleted file mode 100644 index a7b2656..0000000 --- a/lstrip.rb +++ /dev/null @@ -1,32 +0,0 @@ -# -# lstrip.rb -# - -module Puppet::Parser::Functions - newfunction(:lstrip, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "lstrip(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - value = arguments[0] - klass = value.class - - unless [Array, String].include?(klass) - raise(Puppet::ParseError, 'lstrip(): Requires either ' + - 'array or string to work with') - end - - if value.is_a?(Array) - # Numbers in Puppet are often string-encoded which is troublesome ... - result = value.collect { |i| i.is_a?(String) ? i.lstrip : i } - else - result = value.lstrip - end - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/member.rb b/member.rb deleted file mode 100644 index bb43a37..0000000 --- a/member.rb +++ /dev/null @@ -1,33 +0,0 @@ -# -# member.rb -# - -# TODO(Krzysztof Wilczynski): We need to add support for regular expression ... -# TODO(Krzysztof Wilczynski): Support for strings and hashes too ... - -module Puppet::Parser::Functions - newfunction(:member, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "member(): Wrong number of arguments " + - "given (#{arguments.size} for 2)") if arguments.size < 2 - - array = arguments[0] - - unless array.is_a?(Array) - raise(Puppet::ParseError, 'member(): Requires array to work with') - end - - item = arguments[1] - - raise(Puppet::ParseError, 'member(): You must provide item ' + - 'to search for within array given') if item.empty? - - result = array.include?(item) - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/rand.rb b/rand.rb deleted file mode 100644 index 2cb9acb..0000000 --- a/rand.rb +++ /dev/null @@ -1,12 +0,0 @@ -# -# rand.rb -# - -module Puppet::Parser::Functions - newfunction(:rand, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - end -end - -# vim: set ts=2 sw=2 et : diff --git a/rstrip.rb b/rstrip.rb deleted file mode 100644 index 56849d3..0000000 --- a/rstrip.rb +++ /dev/null @@ -1,31 +0,0 @@ -# -# rstrip.rb -# - -module Puppet::Parser::Functions - newfunction(:rstrip, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "rstrip(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - value = arguments[0] - klass = value.class - - unless [Array, String].include?(klass) - raise(Puppet::ParseError, 'rstrip(): Requires either ' + - 'array or string to work with') - end - - if value.is_a?(Array) - result = value.collect { |i| i.is_a?(String) ? i.rstrip : i } - else - result = value.rstrip - end - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/sort.rb b/sort.rb deleted file mode 100644 index 85e5ba0..0000000 --- a/sort.rb +++ /dev/null @@ -1,12 +0,0 @@ -# -# sort.rb -# - -module Puppet::Parser::Functions - newfunction(:sort, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - end -end - -# vim: set ts=2 sw=2 et : diff --git a/squeeze.rb b/squeeze.rb deleted file mode 100644 index a135bd3..0000000 --- a/squeeze.rb +++ /dev/null @@ -1,12 +0,0 @@ -# -# squeeze.rb -# - -module Puppet::Parser::Functions - newfunction(:squeeze, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - end -end - -# vim: set ts=2 sw=2 et : diff --git a/str2bool.rb b/str2bool.rb deleted file mode 100644 index f3a6d6c..0000000 --- a/str2bool.rb +++ /dev/null @@ -1,38 +0,0 @@ -# -# str2bool.rb -# - -module Puppet::Parser::Functions - newfunction(:str2bool, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "str2bool(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - string = arguments[0] - - unless string.is_a?(String) - raise(Puppet::ParseError, 'str2bool(): Requires either ' + - 'string to work with') - end - - # We consider all the yes, no, y, n and so on too ... - result = case string - # - # This is how undef looks like in Puppet ... - # 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 /^(undef|undefined)$/ then false # This is not likely to happen ... - else - raise(Puppet::ParseError, 'str2bool(): Unknown type of boolean given') - end - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/strip.rb b/strip.rb deleted file mode 100644 index ebab20e..0000000 --- a/strip.rb +++ /dev/null @@ -1,31 +0,0 @@ -# -# strip.rb -# - -module Puppet::Parser::Functions - newfunction(:strip, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "strip(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - value = arguments[0] - klass = value.class - - unless [Array, String].include?(klass) - raise(Puppet::ParseError, 'strip(): Requires either ' + - 'array or string to work with') - end - - if value.is_a?(Array) - result = value.collect { |i| i.is_a?(String) ? i.strip : i } - else - result = value.strip - end - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/swapcase.rb b/swapcase.rb deleted file mode 100644 index a0002a9..0000000 --- a/swapcase.rb +++ /dev/null @@ -1,32 +0,0 @@ -# -# swapcase.rb -# - -module Puppet::Parser::Functions - newfunction(:swapcase, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "swapcase(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - value = arguments[0] - klass = value.class - - unless [Array, String].include?(klass) - raise(Puppet::ParseError, 'swapcase(): Requires either ' + - 'array or string to work with') - end - - if value.is_a?(Array) - # Numbers in Puppet are often string-encoded which is troublesome ... - result = value.collect { |i| i.is_a?(String) ? i.swapcase : i } - else - result = value.swapcase - end - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/upcase.rb b/upcase.rb deleted file mode 100644 index 8a9769d..0000000 --- a/upcase.rb +++ /dev/null @@ -1,32 +0,0 @@ -# -# upcase.rb -# - -module Puppet::Parser::Functions - newfunction(:upcase, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "upcase(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - value = arguments[0] - klass = value.class - - unless [Array, String].include?(klass) - raise(Puppet::ParseError, 'upcase(): Requires either ' + - 'array or string to work with') - end - - if value.is_a?(Array) - # Numbers in Puppet are often string-encoded which is troublesome ... - result = value.collect { |i| i.is_a?(String) ? i.upcase : i } - else - result = value.upcase - end - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/zip.rb b/zip.rb deleted file mode 100644 index 024d64a..0000000 --- a/zip.rb +++ /dev/null @@ -1,56 +0,0 @@ -# -# zip.rb -# - -module Puppet::Parser::Functions - newfunction(:zip, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - # Technically we support three arguments but only first is mandatory ... - raise(Puppet::ParseError, "zip(): Wrong number of arguments " + - "given (#{arguments.size} for 2)") if arguments.size < 2 - - a = arguments[0] - b = arguments[1] - - unless a.is_a?(Array) and b.is_a?(Array) - raise(Puppet::ParseError, 'zip(): Requires array to work with') - end - - flatten = arguments[2] if arguments[2] - - if flatten - klass = flatten.class - - # We can have either true or false, or string which resembles boolean ... - unless [FalseClass, TrueClass, String].include?(klass) - raise(Puppet::ParseError, 'zip(): Requires either ' + - 'boolean or string to work with') - end - - if flatten.is_a?(String) - # We consider all the yes, no, y, n and so on too ... - flatten = case flatten - # - # This is how undef looks like in Puppet ... - # 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 /^(undef|undefined)$/ then false # This is not likely to happen ... - else - raise(Puppet::ParseError, 'zip(): Unknown type of boolean given') - end - end - end - - result = a.zip(b) - result = flatten ? result.flatten : result - - return result - end -end - -# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From f71c3bd9e7b8826f331ae58d1a4de6acaad30883 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Thu, 5 May 2011 11:00:05 +0200 Subject: Added disclaimer and basic installation guide to README.markdown file. --- README.markdown | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 66ff373..71a0e6e 100644 --- a/README.markdown +++ b/README.markdown @@ -1,3 +1,19 @@ ## puppet-functions module -This is the puppet-functions module. It provides a library of various functions that extend the basic function library that is provided in Puppet. +### Overview + +This is the puppet-functions module. Here we are providing a library of various functions that extend the basic function library that is provided in Puppet. + +### Disclaimer + +Warning! While this software is written in the best interest of quality it has not been formally tested by our QA teams. Use at your own risk, but feel free to enjoy and perhaps improve it while you do. + +Please see the included Apache Software License for more legal details regarding warranty. + +### Installation + +From github, download the module into your modulepath on your Puppetmaster. If you are not sure where your module path is try this command: + + puppet --configprint modulepath + +Depending on the version of Puppet, you may need to restart the puppetmasterd (or Apache) process before the functions will work. -- cgit v1.2.3 From 5c42025cab843b7d1d39b5ab839cfa268462a10d Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Mon, 16 May 2011 21:40:01 +0100 Subject: Rename to puppetlabs-functions. --- Modulefile | 8 ++++---- README.markdown | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Modulefile b/Modulefile index 9b7d1e1..a886ae6 100644 --- a/Modulefile +++ b/Modulefile @@ -1,8 +1,8 @@ -name 'kwilczynski/puppet-functions' +name 'puppetlabs-functions' version '0.0.1' -source 'https://github.com/kwilczynski/puppet-functions' +source 'https://github.com/puppetlabs/puppetlabs-functions' author 'Krzysztof Wilczynski' -license 'UNKNOWN' +license 'Apache License 2.0' summary 'A set of basic functions for puppet.' description 'This module provides a set of basic functions for puppet that extend the standard library.' -project_page 'https://github.com/kwilczynski/puppet-functions' +project_page 'https://github.com/puppetlabs/puppetlabs-functions' diff --git a/README.markdown b/README.markdown index 71a0e6e..ec4267c 100644 --- a/README.markdown +++ b/README.markdown @@ -1,4 +1,4 @@ -## puppet-functions module +## puppetlabs-functions module ### Overview -- cgit v1.2.3 From e6b5a6dd0252f5491753abb3b71859644036f2fe Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Sun, 26 Jun 2011 14:33:53 +0200 Subject: Removed duplicate - is_hash is really now is_hash instead of is_array. --- lib/puppet/parser/functions/is_hash.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/puppet/parser/functions/is_hash.rb b/lib/puppet/parser/functions/is_hash.rb index 259809c..000306e 100644 --- a/lib/puppet/parser/functions/is_hash.rb +++ b/lib/puppet/parser/functions/is_hash.rb @@ -3,12 +3,12 @@ # module Puppet::Parser::Functions - newfunction(:is_array, :type => :rvalue, :doc => <<-EOS + newfunction(:is_hash, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| raise(Puppet::ParseError, "is_hash(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + "given (#{arguments.size} for 1)") if arguments.size != 1 type = arguments[0] -- cgit v1.2.3 From 157531cd290e53c9a174171bb29de293bade2ed4 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Wed, 29 Jun 2011 12:25:43 +0100 Subject: Copied function test scaffolding from puppet. --- Rakefile | 16 ++++++ spec/lib/puppet_spec/files.rb | 53 ++++++++++++++++++ spec/lib/puppet_spec/fixtures.rb | 28 ++++++++++ spec/lib/puppet_spec/matchers.rb | 87 +++++++++++++++++++++++++++++ spec/lib/puppet_spec/verbose.rb | 9 +++ spec/monkey_patches/alias_should_to_must.rb | 8 +++ spec/monkey_patches/publicize_methods.rb | 11 ++++ spec/spec.opts | 4 ++ spec/spec_helper.rb | 78 ++++++++++++++++++++++++++ 9 files changed, 294 insertions(+) create mode 100644 Rakefile create mode 100755 spec/lib/puppet_spec/files.rb create mode 100755 spec/lib/puppet_spec/fixtures.rb create mode 100644 spec/lib/puppet_spec/matchers.rb create mode 100755 spec/lib/puppet_spec/verbose.rb create mode 100755 spec/monkey_patches/alias_should_to_must.rb create mode 100755 spec/monkey_patches/publicize_methods.rb create mode 100644 spec/spec.opts create mode 100755 spec/spec_helper.rb diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..01b2a31 --- /dev/null +++ b/Rakefile @@ -0,0 +1,16 @@ +require 'rake' +require 'rspec/core/rake_task' + +task :default => [:test] + +desc 'Run RSpec' +RSpec::Core::RakeTask.new(:test) do |t| + t.pattern = 'spec/{unit}/**/*.rb' + t.rspec_opts = ['--color'] +end + +desc 'Generate code coverage' +RSpec::Core::RakeTask.new(:coverage) do |t| + t.rcov = true + t.rcov_opts = ['--exclude', 'spec'] +end diff --git a/spec/lib/puppet_spec/files.rb b/spec/lib/puppet_spec/files.rb new file mode 100755 index 0000000..30fb4fc --- /dev/null +++ b/spec/lib/puppet_spec/files.rb @@ -0,0 +1,53 @@ +require 'fileutils' +require 'tempfile' + +# A support module for testing files. +module PuppetSpec::Files + # This code exists only to support tests that run as root, pretty much. + # Once they have finally been eliminated this can all go... --daniel 2011-04-08 + if Puppet.features.posix? then + def self.in_tmp(path) + path =~ /^\/tmp/ or path =~ /^\/var\/folders/ + end + elsif Puppet.features.microsoft_windows? + def self.in_tmp(path) + tempdir = File.expand_path(File.join(Dir::LOCAL_APPDATA, "Temp")) + path =~ /^#{tempdir}/ + end + else + fail "Help! Can't find in_tmp for this platform" + end + + def self.cleanup + $global_tempfiles ||= [] + while path = $global_tempfiles.pop do + fail "Not deleting tmpfile #{path} outside regular tmpdir" unless in_tmp(path) + + begin + FileUtils.rm_r path, :secure => true + rescue Errno::ENOENT + # nothing to do + end + end + end + + def tmpfile(name) + # Generate a temporary file, just for the name... + source = Tempfile.new(name) + path = source.path + source.close! + + # ...record it for cleanup, + $global_tempfiles ||= [] + $global_tempfiles << File.expand_path(path) + + # ...and bam. + path + end + + def tmpdir(name) + path = tmpfile(name) + FileUtils.mkdir_p(path) + path + end +end diff --git a/spec/lib/puppet_spec/fixtures.rb b/spec/lib/puppet_spec/fixtures.rb new file mode 100755 index 0000000..7f6bc2a --- /dev/null +++ b/spec/lib/puppet_spec/fixtures.rb @@ -0,0 +1,28 @@ +module PuppetSpec::Fixtures + def fixtures(*rest) + File.join(PuppetSpec::FIXTURE_DIR, *rest) + end + def my_fixture_dir + callers = caller + while line = callers.shift do + next unless found = line.match(%r{/spec/(.*)_spec\.rb:}) + return fixtures(found[1]) + end + fail "sorry, I couldn't work out your path from the caller stack!" + end + def my_fixture(name) + file = File.join(my_fixture_dir, name) + unless File.readable? file then + fail Puppet::DevError, "fixture '#{name}' for #{my_fixture_dir} is not readable" + end + return file + end + def my_fixtures(glob = '*', flags = 0) + files = Dir.glob(File.join(my_fixture_dir, glob), flags) + unless files.length > 0 then + fail Puppet::DevError, "fixture '#{glob}' for #{my_fixture_dir} had no files!" + end + block_given? and files.each do |file| yield file end + files + end +end diff --git a/spec/lib/puppet_spec/matchers.rb b/spec/lib/puppet_spec/matchers.rb new file mode 100644 index 0000000..77f5803 --- /dev/null +++ b/spec/lib/puppet_spec/matchers.rb @@ -0,0 +1,87 @@ +require 'stringio' + +######################################################################## +# Backward compatibility for Jenkins outdated environment. +module RSpec + module Matchers + module BlockAliases + alias_method :to, :should unless method_defined? :to + alias_method :to_not, :should_not unless method_defined? :to_not + alias_method :not_to, :should_not unless method_defined? :not_to + end + end +end + + +######################################################################## +# Custom matchers... +RSpec::Matchers.define :have_matching_element do |expected| + match do |actual| + actual.any? { |item| item =~ expected } + end +end + + +RSpec::Matchers.define :exit_with do |expected| + actual = nil + match do |block| + begin + block.call + rescue SystemExit => e + actual = e.status + end + actual and actual == expected + end + failure_message_for_should do |block| + "expected exit with code #{expected} but " + + (actual.nil? ? " exit was not called" : "we exited with #{actual} instead") + end + failure_message_for_should_not do |block| + "expected that exit would not be called with #{expected}" + end + description do + "expect exit with #{expected}" + end +end + + +RSpec::Matchers.define :have_printed do |expected| + match do |block| + $stderr = $stdout = StringIO.new + + begin + block.call + ensure + $stdout.rewind + @actual = $stdout.read + + $stdout = STDOUT + $stderr = STDERR + end + + if @actual then + case expected + when String + @actual.include? expected + when Regexp + expected.match @actual + else + raise ArgumentError, "No idea how to match a #{@actual.class.name}" + end + end + end + + failure_message_for_should do |actual| + if actual.nil? then + "expected #{expected.inspect}, but nothing was printed" + else + "expected #{expected.inspect} to be printed; got:\n#{actual}" + end + end + + description do + "expect #{expected.inspect} to be printed" + end + + diffable +end diff --git a/spec/lib/puppet_spec/verbose.rb b/spec/lib/puppet_spec/verbose.rb new file mode 100755 index 0000000..d9834f2 --- /dev/null +++ b/spec/lib/puppet_spec/verbose.rb @@ -0,0 +1,9 @@ +# Support code for running stuff with warnings disabled. +module Kernel + def with_verbose_disabled + verbose, $VERBOSE = $VERBOSE, nil + result = yield + $VERBOSE = verbose + return result + end +end diff --git a/spec/monkey_patches/alias_should_to_must.rb b/spec/monkey_patches/alias_should_to_must.rb new file mode 100755 index 0000000..1a11117 --- /dev/null +++ b/spec/monkey_patches/alias_should_to_must.rb @@ -0,0 +1,8 @@ +require 'rspec' + +class Object + # This is necessary because the RAL has a 'should' + # method. + alias :must :should + alias :must_not :should_not +end diff --git a/spec/monkey_patches/publicize_methods.rb b/spec/monkey_patches/publicize_methods.rb new file mode 100755 index 0000000..b39e9c0 --- /dev/null +++ b/spec/monkey_patches/publicize_methods.rb @@ -0,0 +1,11 @@ +# Some monkey-patching to allow us to test private methods. +class Class + def publicize_methods(*methods) + saved_private_instance_methods = methods.empty? ? self.private_instance_methods : methods + + self.class_eval { public(*saved_private_instance_methods) } + yield + self.class_eval { private(*saved_private_instance_methods) } + end +end + diff --git a/spec/spec.opts b/spec/spec.opts new file mode 100644 index 0000000..425f0ed --- /dev/null +++ b/spec/spec.opts @@ -0,0 +1,4 @@ +--format +s +--colour +--backtrace diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100755 index 0000000..fdc04bc --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,78 @@ +dir = File.expand_path(File.dirname(__FILE__)) +$LOAD_PATH.unshift File.join(dir, 'lib') + +p dir + +# Don't want puppet getting the command line arguments for rake or autotest +ARGV.clear + +require 'puppet' +require 'mocha' +gem 'rspec', '>=2.0.0' +require 'rspec/expectations' + +# So everyone else doesn't have to include this base constant. +module PuppetSpec + FIXTURE_DIR = File.join(dir = File.expand_path(File.dirname(__FILE__)), "fixtures") unless defined?(FIXTURE_DIR) +end + +require 'pathname' +require 'tmpdir' + +require 'puppet_spec/verbose' +require 'puppet_spec/files' +require 'puppet_spec/fixtures' +require 'puppet_spec/matchers' +require 'monkey_patches/alias_should_to_must' +require 'monkey_patches/publicize_methods' + +Pathname.glob("#{dir}/shared_behaviours/**/*.rb") do |behaviour| + require behaviour.relative_path_from(Pathname.new(dir)) +end + +RSpec.configure do |config| + include PuppetSpec::Fixtures + + config.mock_with :mocha + + config.before :each do + GC.disable + + # these globals are set by Application + $puppet_application_mode = nil + $puppet_application_name = nil + + # REVISIT: I think this conceals other bad tests, but I don't have time to + # fully diagnose those right now. When you read this, please come tell me + # I suck for letting this float. --daniel 2011-04-21 + Signal.stubs(:trap) + + # Set the confdir and vardir to gibberish so that tests + # have to be correctly mocked. + Puppet[:confdir] = "/dev/null" + Puppet[:vardir] = "/dev/null" + + # Avoid opening ports to the outside world + Puppet.settings[:bindaddress] = "127.0.0.1" + + @logs = [] + Puppet::Util::Log.newdestination(Puppet::Test::LogCollector.new(@logs)) + + @log_level = Puppet::Util::Log.level + end + + config.after :each do + Puppet.settings.clear + Puppet::Node::Environment.clear + Puppet::Util::Storage.clear + Puppet::Util::ExecutionStub.reset + + PuppetSpec::Files.cleanup + + @logs.clear + Puppet::Util::Log.close_all + Puppet::Util::Log.level = @log_level + + GC.enable + end +end -- cgit v1.2.3 From e071b05ab631f4b73fed7178c52d0416f7629cb8 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Wed, 29 Jun 2011 12:30:07 +0100 Subject: Added kwalify function. --- README.markdown | 29 ++++++++++++++ lib/puppet/parser/functions/kwalify.rb | 35 +++++++++++++++++ spec/unit/parser/functions/kwalify_spec.rb | 61 ++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 lib/puppet/parser/functions/kwalify.rb create mode 100755 spec/unit/parser/functions/kwalify_spec.rb diff --git a/README.markdown b/README.markdown index ec4267c..68e559e 100644 --- a/README.markdown +++ b/README.markdown @@ -17,3 +17,32 @@ From github, download the module into your modulepath on your Puppetmaster. If y puppet --configprint modulepath Depending on the version of Puppet, you may need to restart the puppetmasterd (or Apache) process before the functions will work. + +## Functions + +### kwalify + +This function allows you to validate Puppet data structures using Kwalify +schemas as documented here: + +http://www.kuwata-lab.com/kwalify/ruby/users-guide.01.html + +To validate, create a schema in Puppet: + + $schema = { + 'type' => 'seq', + 'sequence' => [ + { 'type' => 'str' } + ] + } + +And create some content that you want validated: + + $document = ['a', 'b', 'c'] + +And then use the function to validate: + + kwalify($schema, $document) + +The function will throw an error and list all validation errors if there is a +problem otherwise it succeeds silently. diff --git a/lib/puppet/parser/functions/kwalify.rb b/lib/puppet/parser/functions/kwalify.rb new file mode 100644 index 0000000..7238f84 --- /dev/null +++ b/lib/puppet/parser/functions/kwalify.rb @@ -0,0 +1,35 @@ +# +# kwalify.rb +# + +require 'kwalify' + +module Puppet::Parser::Functions + newfunction(:kwalify, :type => :statement, :doc => <<-EOS +This function uses kwalify to validate Puppet data structures against Kwalify +schemas. + EOS + ) do |args| + + raise(Puppet::ParseError, "kwalify(): Wrong number of arguments " + + "given (#{args.size} for 2)") if args.size != 2 + + schema = args[0] + document = args[1] + + validator = Kwalify::Validator.new(schema) + + errors = validator.validate(document) + + if errors && !errors.empty? + error_out = [] + for e in errors + error_out << "[#{e.path}] #{e.message}" + end + raise(Puppet::ParseError, "Failed kwalify schema validation:\n" + error_out.join("\n")) + end + + end +end + +# vim: set ts=2 sw=2 et : diff --git a/spec/unit/parser/functions/kwalify_spec.rb b/spec/unit/parser/functions/kwalify_spec.rb new file mode 100755 index 0000000..b2afa12 --- /dev/null +++ b/spec/unit/parser/functions/kwalify_spec.rb @@ -0,0 +1,61 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the kwalify function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("kwalify").should == "function_kwalify" + end + + it "should raise a ParseError if there is less than 2 arguments" do + lambda { @scope.function_kwalify([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should validate a simple array schema" do + schema = { + 'type' => 'seq', + 'sequence' => [ + { 'type' => 'str' } + ] + } + document = ['a','b','c'] + @scope.function_kwalify([schema, document]) + end + + it "should not validate a simple array schema when invalid" do + schema = { + 'type' => 'seq', + 'sequence' => [ + { 'type' => 'str' } + ] + } + document = ['a','b',{'a' => 'b'}] + lambda { @scope.function_kwalify([schema, document]) }.should(raise_error(Puppet::ParseError)) + end + + it "should validate a hash schema" do + schema = { + 'type' => 'map', + 'mapping' => { + 'key1' => { + 'type' => 'str', + }, + 'key2' => { + 'type' => 'str', + }, + } + } + document = { + 'key1' => 'b', + 'key2' => 'c', + } + end + +end -- cgit v1.2.3 From 790818116e953118ba9eab5e5bef6d63f7bbc1fa Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Wed, 29 Jun 2011 21:21:55 +0100 Subject: Added tests for each function, fixing functions as we hit bugs. --- .gitignore | 1 + lib/puppet/parser/functions/date.rb | 6 +++++ lib/puppet/parser/functions/delete.rb | 6 +++++ lib/puppet/parser/functions/grep.rb | 6 +++++ lib/puppet/parser/functions/is_float.rb | 6 +++++ lib/puppet/parser/functions/is_integer.rb | 6 +++++ lib/puppet/parser/functions/is_numeric.rb | 6 +++++ .../parser/functions/is_valid_domain_name.rb | 10 +++++-- lib/puppet/parser/functions/is_valid_ip_address.rb | 6 +++++ .../parser/functions/is_valid_mac_address.rb | 6 +++++ lib/puppet/parser/functions/is_valid_netmask.rb | 6 +++++ lib/puppet/parser/functions/load_json.rb | 12 +++++++++ lib/puppet/parser/functions/load_yaml.rb | 10 +++++++ lib/puppet/parser/functions/rand.rb | 6 +++++ lib/puppet/parser/functions/sort.rb | 6 +++++ lib/puppet/parser/functions/squeeze.rb | 6 +++++ lib/puppet/parser/functions/time.rb | 5 ++++ spec/unit/parser/functions/abs_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/bool2num_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/capitalize_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/chomp_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/chop_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/count_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/date_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/delete_at_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/delete_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/downcase_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/empty_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/fact_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/flatten_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/grep_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/hash_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/is_array_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/is_float_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/is_hash_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/is_integer_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/is_numeric_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/is_string_spec.rb | 21 +++++++++++++++ .../parser/functions/is_valid_domain_name_spec.rb | 21 +++++++++++++++ .../parser/functions/is_valid_ip_address_spec.rb | 21 +++++++++++++++ .../parser/functions/is_valid_mac_address_spec.rb | 21 +++++++++++++++ .../unit/parser/functions/is_valid_netmask_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/join_spec.rb | 21 +++++++++++++++ .../unit/parser/functions/join_with_prefix_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/keys_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/kwalify_spec.rb | 1 + spec/unit/parser/functions/load_json_spec.rb | 29 ++++++++++++++++++++ spec/unit/parser/functions/load_variables_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/load_yaml_spec.rb | 31 ++++++++++++++++++++++ spec/unit/parser/functions/lstrip_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/member_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/num2bool_spec.rb | 21 +++++++++++++++ .../functions/persistent_crontab_minutes_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/prefix_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/rand_spec.rb | 21 +++++++++++++++ .../functions/random_crontab_minutes_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/range_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/reverse_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/rstrip_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/shuffle_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/size_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/sort_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/squeeze_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/str2bool_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/strftime_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/strip_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/swapcase_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/time_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/type_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/unique_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/upcase_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/values_at_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/values_spec.rb | 21 +++++++++++++++ spec/unit/parser/functions/zip_spec.rb | 21 +++++++++++++++ 74 files changed, 1303 insertions(+), 2 deletions(-) create mode 100755 spec/unit/parser/functions/abs_spec.rb create mode 100755 spec/unit/parser/functions/bool2num_spec.rb create mode 100755 spec/unit/parser/functions/capitalize_spec.rb create mode 100755 spec/unit/parser/functions/chomp_spec.rb create mode 100755 spec/unit/parser/functions/chop_spec.rb create mode 100755 spec/unit/parser/functions/count_spec.rb create mode 100755 spec/unit/parser/functions/date_spec.rb create mode 100755 spec/unit/parser/functions/delete_at_spec.rb create mode 100755 spec/unit/parser/functions/delete_spec.rb create mode 100755 spec/unit/parser/functions/downcase_spec.rb create mode 100755 spec/unit/parser/functions/empty_spec.rb create mode 100755 spec/unit/parser/functions/fact_spec.rb create mode 100755 spec/unit/parser/functions/flatten_spec.rb create mode 100755 spec/unit/parser/functions/grep_spec.rb create mode 100644 spec/unit/parser/functions/hash_spec.rb create mode 100644 spec/unit/parser/functions/is_array_spec.rb create mode 100644 spec/unit/parser/functions/is_float_spec.rb create mode 100644 spec/unit/parser/functions/is_hash_spec.rb create mode 100644 spec/unit/parser/functions/is_integer_spec.rb create mode 100644 spec/unit/parser/functions/is_numeric_spec.rb create mode 100644 spec/unit/parser/functions/is_string_spec.rb create mode 100644 spec/unit/parser/functions/is_valid_domain_name_spec.rb create mode 100644 spec/unit/parser/functions/is_valid_ip_address_spec.rb create mode 100644 spec/unit/parser/functions/is_valid_mac_address_spec.rb create mode 100644 spec/unit/parser/functions/is_valid_netmask_spec.rb create mode 100644 spec/unit/parser/functions/join_spec.rb create mode 100644 spec/unit/parser/functions/join_with_prefix_spec.rb create mode 100644 spec/unit/parser/functions/keys_spec.rb create mode 100644 spec/unit/parser/functions/load_json_spec.rb create mode 100644 spec/unit/parser/functions/load_variables_spec.rb create mode 100644 spec/unit/parser/functions/load_yaml_spec.rb create mode 100644 spec/unit/parser/functions/lstrip_spec.rb create mode 100644 spec/unit/parser/functions/member_spec.rb create mode 100644 spec/unit/parser/functions/num2bool_spec.rb create mode 100644 spec/unit/parser/functions/persistent_crontab_minutes_spec.rb create mode 100644 spec/unit/parser/functions/prefix_spec.rb create mode 100644 spec/unit/parser/functions/rand_spec.rb create mode 100644 spec/unit/parser/functions/random_crontab_minutes_spec.rb create mode 100644 spec/unit/parser/functions/range_spec.rb create mode 100644 spec/unit/parser/functions/reverse_spec.rb create mode 100644 spec/unit/parser/functions/rstrip_spec.rb create mode 100644 spec/unit/parser/functions/shuffle_spec.rb create mode 100644 spec/unit/parser/functions/size_spec.rb create mode 100644 spec/unit/parser/functions/sort_spec.rb create mode 100644 spec/unit/parser/functions/squeeze_spec.rb create mode 100644 spec/unit/parser/functions/str2bool_spec.rb create mode 100644 spec/unit/parser/functions/strftime_spec.rb create mode 100644 spec/unit/parser/functions/strip_spec.rb create mode 100644 spec/unit/parser/functions/swapcase_spec.rb create mode 100644 spec/unit/parser/functions/time_spec.rb create mode 100644 spec/unit/parser/functions/type_spec.rb create mode 100644 spec/unit/parser/functions/unique_spec.rb create mode 100644 spec/unit/parser/functions/upcase_spec.rb create mode 100644 spec/unit/parser/functions/values_at_spec.rb create mode 100644 spec/unit/parser/functions/values_spec.rb create mode 100644 spec/unit/parser/functions/zip_spec.rb diff --git a/.gitignore b/.gitignore index 01d0a08..7ad8fca 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ pkg/ +coverage/ diff --git a/lib/puppet/parser/functions/date.rb b/lib/puppet/parser/functions/date.rb index ea11265..4d0543e 100644 --- a/lib/puppet/parser/functions/date.rb +++ b/lib/puppet/parser/functions/date.rb @@ -6,6 +6,12 @@ module Puppet::Parser::Functions newfunction(:date, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_valid_netmask(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + end end diff --git a/lib/puppet/parser/functions/delete.rb b/lib/puppet/parser/functions/delete.rb index b8376d1..88f3448 100644 --- a/lib/puppet/parser/functions/delete.rb +++ b/lib/puppet/parser/functions/delete.rb @@ -9,6 +9,12 @@ module Puppet::Parser::Functions newfunction(:delete, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 2) then + raise(Puppet::ParseError, "is_valid_netmask(): Wrong number of arguments "+ + "given #{arguments.size} for 2") + end + end end diff --git a/lib/puppet/parser/functions/grep.rb b/lib/puppet/parser/functions/grep.rb index 1663fe7..8549218 100644 --- a/lib/puppet/parser/functions/grep.rb +++ b/lib/puppet/parser/functions/grep.rb @@ -6,6 +6,12 @@ module Puppet::Parser::Functions newfunction(:grep, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 2) then + raise(Puppet::ParseError, "grep(): Wrong number of arguments "+ + "given #{arguments.size} for 2") + end + end end diff --git a/lib/puppet/parser/functions/is_float.rb b/lib/puppet/parser/functions/is_float.rb index 2a5a923..39d097f 100644 --- a/lib/puppet/parser/functions/is_float.rb +++ b/lib/puppet/parser/functions/is_float.rb @@ -6,6 +6,12 @@ module Puppet::Parser::Functions newfunction(:is_float, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_float(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + end end diff --git a/lib/puppet/parser/functions/is_integer.rb b/lib/puppet/parser/functions/is_integer.rb index 44337f0..9813cf1 100644 --- a/lib/puppet/parser/functions/is_integer.rb +++ b/lib/puppet/parser/functions/is_integer.rb @@ -6,6 +6,12 @@ module Puppet::Parser::Functions newfunction(:is_integer, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_integer(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + end end diff --git a/lib/puppet/parser/functions/is_numeric.rb b/lib/puppet/parser/functions/is_numeric.rb index 7a64091..96e8674 100644 --- a/lib/puppet/parser/functions/is_numeric.rb +++ b/lib/puppet/parser/functions/is_numeric.rb @@ -6,6 +6,12 @@ module Puppet::Parser::Functions newfunction(:is_numeric, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_numeric(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + end end diff --git a/lib/puppet/parser/functions/is_valid_domain_name.rb b/lib/puppet/parser/functions/is_valid_domain_name.rb index 05d64be..c0b319c 100644 --- a/lib/puppet/parser/functions/is_valid_domain_name.rb +++ b/lib/puppet/parser/functions/is_valid_domain_name.rb @@ -1,11 +1,17 @@ # -# is_valid_doman_name.rb +# is_valid_domain_name.rb # module Puppet::Parser::Functions - newfunction(:is_valid_doman_name, :type => :rvalue, :doc => <<-EOS + newfunction(:is_valid_domain_name, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_valid_domain_name(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + end end diff --git a/lib/puppet/parser/functions/is_valid_ip_address.rb b/lib/puppet/parser/functions/is_valid_ip_address.rb index e6b68a8..e91dda8 100644 --- a/lib/puppet/parser/functions/is_valid_ip_address.rb +++ b/lib/puppet/parser/functions/is_valid_ip_address.rb @@ -6,6 +6,12 @@ module Puppet::Parser::Functions newfunction(:is_valid_ip_address, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_valid_ip_address(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + end end diff --git a/lib/puppet/parser/functions/is_valid_mac_address.rb b/lib/puppet/parser/functions/is_valid_mac_address.rb index 5e354c9..0b91d0d 100644 --- a/lib/puppet/parser/functions/is_valid_mac_address.rb +++ b/lib/puppet/parser/functions/is_valid_mac_address.rb @@ -6,6 +6,12 @@ module Puppet::Parser::Functions newfunction(:is_valid_mac_address, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_valid_mac_address(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + end end diff --git a/lib/puppet/parser/functions/is_valid_netmask.rb b/lib/puppet/parser/functions/is_valid_netmask.rb index 2aeb374..41e4843 100644 --- a/lib/puppet/parser/functions/is_valid_netmask.rb +++ b/lib/puppet/parser/functions/is_valid_netmask.rb @@ -6,6 +6,12 @@ module Puppet::Parser::Functions newfunction(:is_valid_netmask, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_valid_netmask(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + end end diff --git a/lib/puppet/parser/functions/load_json.rb b/lib/puppet/parser/functions/load_json.rb index 9ec8c78..7c3f187 100644 --- a/lib/puppet/parser/functions/load_json.rb +++ b/lib/puppet/parser/functions/load_json.rb @@ -6,6 +6,18 @@ module Puppet::Parser::Functions newfunction(:load_json, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "load_json(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + + json = arguments[0] + + require 'json' + + JSON.load(json) + end end diff --git a/lib/puppet/parser/functions/load_yaml.rb b/lib/puppet/parser/functions/load_yaml.rb index 684a721..1bc2f36 100644 --- a/lib/puppet/parser/functions/load_yaml.rb +++ b/lib/puppet/parser/functions/load_yaml.rb @@ -6,6 +6,16 @@ module Puppet::Parser::Functions newfunction(:load_yaml, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "load_yaml(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + + require 'yaml' + + YAML::load(arguments[0]) + end end diff --git a/lib/puppet/parser/functions/rand.rb b/lib/puppet/parser/functions/rand.rb index 2cb9acb..6d870dc 100644 --- a/lib/puppet/parser/functions/rand.rb +++ b/lib/puppet/parser/functions/rand.rb @@ -6,6 +6,12 @@ module Puppet::Parser::Functions newfunction(:rand, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 0) and (arguments.size != 1) then + raise(Puppet::ParseError, "rand(): Wrong number of arguments "+ + "given #{arguments.size} for 0 or 1") + end + end end diff --git a/lib/puppet/parser/functions/sort.rb b/lib/puppet/parser/functions/sort.rb index 85e5ba0..974141c 100644 --- a/lib/puppet/parser/functions/sort.rb +++ b/lib/puppet/parser/functions/sort.rb @@ -6,6 +6,12 @@ module Puppet::Parser::Functions newfunction(:sort, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 0) then + raise(Puppet::ParseError, "sort(): Wrong number of arguments "+ + "given #{arguments.size} for 0") + end + end end diff --git a/lib/puppet/parser/functions/squeeze.rb b/lib/puppet/parser/functions/squeeze.rb index a135bd3..02eb00c 100644 --- a/lib/puppet/parser/functions/squeeze.rb +++ b/lib/puppet/parser/functions/squeeze.rb @@ -6,6 +6,12 @@ module Puppet::Parser::Functions newfunction(:squeeze, :type => :rvalue, :doc => <<-EOS EOS ) do |arguments| + + if (arguments.size != 2) then + raise(Puppet::ParseError, "squeeze(): Wrong number of arguments "+ + "given #{arguments.size} for 2") + end + end end diff --git a/lib/puppet/parser/functions/time.rb b/lib/puppet/parser/functions/time.rb index f7c1041..e1f5b6f 100644 --- a/lib/puppet/parser/functions/time.rb +++ b/lib/puppet/parser/functions/time.rb @@ -10,6 +10,11 @@ module Puppet::Parser::Functions # The Time Zone argument is optional ... time_zone = arguments[0] if arguments[0] + if (arguments.size != 0) and (arguments.size != 1) then + raise(Puppet::ParseError, "time(): Wrong number of arguments "+ + "given #{arguments.size} for 0 or 1") + end + time = Time.new # There is probably a better way to handle Time Zone ... diff --git a/spec/unit/parser/functions/abs_spec.rb b/spec/unit/parser/functions/abs_spec.rb new file mode 100755 index 0000000..cd2902a --- /dev/null +++ b/spec/unit/parser/functions/abs_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the abs function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("abs").should == "function_abs" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_abs([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/bool2num_spec.rb b/spec/unit/parser/functions/bool2num_spec.rb new file mode 100755 index 0000000..a2585e9 --- /dev/null +++ b/spec/unit/parser/functions/bool2num_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the bool2num function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("bool2num").should == "function_bool2num" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_bool2num([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/capitalize_spec.rb b/spec/unit/parser/functions/capitalize_spec.rb new file mode 100755 index 0000000..bb1fe2a --- /dev/null +++ b/spec/unit/parser/functions/capitalize_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the capitalize function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("capitalize").should == "function_capitalize" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_capitalize([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/chomp_spec.rb b/spec/unit/parser/functions/chomp_spec.rb new file mode 100755 index 0000000..150a7c8 --- /dev/null +++ b/spec/unit/parser/functions/chomp_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the chomp function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("chomp").should == "function_chomp" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_chomp([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/chop_spec.rb b/spec/unit/parser/functions/chop_spec.rb new file mode 100755 index 0000000..ae636cb --- /dev/null +++ b/spec/unit/parser/functions/chop_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the chop function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("chop").should == "function_chop" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_chop([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/count_spec.rb b/spec/unit/parser/functions/count_spec.rb new file mode 100755 index 0000000..28617c9 --- /dev/null +++ b/spec/unit/parser/functions/count_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the count function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("count").should == "function_count" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_count([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/date_spec.rb b/spec/unit/parser/functions/date_spec.rb new file mode 100755 index 0000000..dcba4af --- /dev/null +++ b/spec/unit/parser/functions/date_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the date function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("date").should == "function_date" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_date([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/delete_at_spec.rb b/spec/unit/parser/functions/delete_at_spec.rb new file mode 100755 index 0000000..a0b5b06 --- /dev/null +++ b/spec/unit/parser/functions/delete_at_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the delete_at function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("delete_at").should == "function_delete_at" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_delete_at([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/delete_spec.rb b/spec/unit/parser/functions/delete_spec.rb new file mode 100755 index 0000000..b0729ab --- /dev/null +++ b/spec/unit/parser/functions/delete_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the delete function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("delete").should == "function_delete" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_delete([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/downcase_spec.rb b/spec/unit/parser/functions/downcase_spec.rb new file mode 100755 index 0000000..162291c --- /dev/null +++ b/spec/unit/parser/functions/downcase_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the downcase function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("downcase").should == "function_downcase" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_downcase([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/empty_spec.rb b/spec/unit/parser/functions/empty_spec.rb new file mode 100755 index 0000000..beaf45c --- /dev/null +++ b/spec/unit/parser/functions/empty_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the empty function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("empty").should == "function_empty" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_empty([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/fact_spec.rb b/spec/unit/parser/functions/fact_spec.rb new file mode 100755 index 0000000..c013ae0 --- /dev/null +++ b/spec/unit/parser/functions/fact_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the fact function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("fact").should == "function_fact" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_fact([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/flatten_spec.rb b/spec/unit/parser/functions/flatten_spec.rb new file mode 100755 index 0000000..7af23c1 --- /dev/null +++ b/spec/unit/parser/functions/flatten_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the flatten function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("flatten").should == "function_flatten" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_flatten([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/grep_spec.rb b/spec/unit/parser/functions/grep_spec.rb new file mode 100755 index 0000000..1c949da --- /dev/null +++ b/spec/unit/parser/functions/grep_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the grep function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("grep").should == "function_grep" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_grep([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/hash_spec.rb b/spec/unit/parser/functions/hash_spec.rb new file mode 100644 index 0000000..09b0d50 --- /dev/null +++ b/spec/unit/parser/functions/hash_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the hash function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("hash").should == "function_hash" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_hash([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/is_array_spec.rb b/spec/unit/parser/functions/is_array_spec.rb new file mode 100644 index 0000000..b2843b0 --- /dev/null +++ b/spec/unit/parser/functions/is_array_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_array function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_array").should == "function_is_array" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_array([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/is_float_spec.rb b/spec/unit/parser/functions/is_float_spec.rb new file mode 100644 index 0000000..e3dc8fc --- /dev/null +++ b/spec/unit/parser/functions/is_float_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_float function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_float").should == "function_is_float" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_float([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/is_hash_spec.rb b/spec/unit/parser/functions/is_hash_spec.rb new file mode 100644 index 0000000..66dfdeb --- /dev/null +++ b/spec/unit/parser/functions/is_hash_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_hash function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_hash").should == "function_is_hash" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_hash([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/is_integer_spec.rb b/spec/unit/parser/functions/is_integer_spec.rb new file mode 100644 index 0000000..131251c --- /dev/null +++ b/spec/unit/parser/functions/is_integer_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_integer function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_integer").should == "function_is_integer" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_integer([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/is_numeric_spec.rb b/spec/unit/parser/functions/is_numeric_spec.rb new file mode 100644 index 0000000..3a49f5d --- /dev/null +++ b/spec/unit/parser/functions/is_numeric_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_numeric function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_numeric").should == "function_is_numeric" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_numeric([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/is_string_spec.rb b/spec/unit/parser/functions/is_string_spec.rb new file mode 100644 index 0000000..8c0061e --- /dev/null +++ b/spec/unit/parser/functions/is_string_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_string function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_string").should == "function_is_string" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_string([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/is_valid_domain_name_spec.rb b/spec/unit/parser/functions/is_valid_domain_name_spec.rb new file mode 100644 index 0000000..5cea285 --- /dev/null +++ b/spec/unit/parser/functions/is_valid_domain_name_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_valid_domain_name function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_valid_domain_name").should == "function_is_valid_domain_name" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_valid_domain_name([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/is_valid_ip_address_spec.rb b/spec/unit/parser/functions/is_valid_ip_address_spec.rb new file mode 100644 index 0000000..fa803dd --- /dev/null +++ b/spec/unit/parser/functions/is_valid_ip_address_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_valid_ip_address function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_valid_ip_address").should == "function_is_valid_ip_address" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_valid_ip_address([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/is_valid_mac_address_spec.rb b/spec/unit/parser/functions/is_valid_mac_address_spec.rb new file mode 100644 index 0000000..f2a8389 --- /dev/null +++ b/spec/unit/parser/functions/is_valid_mac_address_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_valid_mac_address function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_valid_mac_address").should == "function_is_valid_mac_address" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_valid_mac_address([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/is_valid_netmask_spec.rb b/spec/unit/parser/functions/is_valid_netmask_spec.rb new file mode 100644 index 0000000..97cbb7c --- /dev/null +++ b/spec/unit/parser/functions/is_valid_netmask_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_valid_netmask function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_valid_netmask").should == "function_is_valid_netmask" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_valid_netmask([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/join_spec.rb b/spec/unit/parser/functions/join_spec.rb new file mode 100644 index 0000000..a7dc0e5 --- /dev/null +++ b/spec/unit/parser/functions/join_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the join function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("join").should == "function_join" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_join([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/join_with_prefix_spec.rb b/spec/unit/parser/functions/join_with_prefix_spec.rb new file mode 100644 index 0000000..0182d8c --- /dev/null +++ b/spec/unit/parser/functions/join_with_prefix_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the join_with_prefix function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("join_with_prefix").should == "function_join_with_prefix" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_join_with_prefix([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/keys_spec.rb b/spec/unit/parser/functions/keys_spec.rb new file mode 100644 index 0000000..13dc260 --- /dev/null +++ b/spec/unit/parser/functions/keys_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the keys function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("keys").should == "function_keys" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_keys([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/kwalify_spec.rb b/spec/unit/parser/functions/kwalify_spec.rb index b2afa12..abdd529 100755 --- a/spec/unit/parser/functions/kwalify_spec.rb +++ b/spec/unit/parser/functions/kwalify_spec.rb @@ -56,6 +56,7 @@ describe "the kwalify function" do 'key1' => 'b', 'key2' => 'c', } + @scope.function_kwalify([schema, document]) end end diff --git a/spec/unit/parser/functions/load_json_spec.rb b/spec/unit/parser/functions/load_json_spec.rb new file mode 100644 index 0000000..73a4566 --- /dev/null +++ b/spec/unit/parser/functions/load_json_spec.rb @@ -0,0 +1,29 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the load_json function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("load_json").should == "function_load_json" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_load_json([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should convert JSON to a data structure" do + json = <<-EOS +["aaa","bbb","ccc"] +EOS + result = @scope.function_load_json([json]) + result.should(eq(['aaa','bbb','ccc'])) + end + +end diff --git a/spec/unit/parser/functions/load_variables_spec.rb b/spec/unit/parser/functions/load_variables_spec.rb new file mode 100644 index 0000000..dc29e61 --- /dev/null +++ b/spec/unit/parser/functions/load_variables_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the load_variables function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("load_variables").should == "function_load_variables" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_load_variables([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/load_yaml_spec.rb b/spec/unit/parser/functions/load_yaml_spec.rb new file mode 100644 index 0000000..2498d12 --- /dev/null +++ b/spec/unit/parser/functions/load_yaml_spec.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the load_yaml function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("load_yaml").should == "function_load_yaml" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_load_yaml([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should convert YAML to a data structure" do + yaml = <<-EOS +- aaa +- bbb +- ccc +EOS + result = @scope.function_load_yaml([yaml]) + result.should(eq(['aaa','bbb','ccc'])) + end + +end diff --git a/spec/unit/parser/functions/lstrip_spec.rb b/spec/unit/parser/functions/lstrip_spec.rb new file mode 100644 index 0000000..9726675 --- /dev/null +++ b/spec/unit/parser/functions/lstrip_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the lstrip function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("lstrip").should == "function_lstrip" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_lstrip([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/member_spec.rb b/spec/unit/parser/functions/member_spec.rb new file mode 100644 index 0000000..39b684f --- /dev/null +++ b/spec/unit/parser/functions/member_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the member function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("member").should == "function_member" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_member([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/num2bool_spec.rb b/spec/unit/parser/functions/num2bool_spec.rb new file mode 100644 index 0000000..fbd25c8 --- /dev/null +++ b/spec/unit/parser/functions/num2bool_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the num2bool function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("num2bool").should == "function_num2bool" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_num2bool([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/persistent_crontab_minutes_spec.rb b/spec/unit/parser/functions/persistent_crontab_minutes_spec.rb new file mode 100644 index 0000000..1d8cbe7 --- /dev/null +++ b/spec/unit/parser/functions/persistent_crontab_minutes_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the persistent_crontab_minutes function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("persistent_crontab_minutes").should == "function_persistent_crontab_minutes" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_persistent_crontab_minutes([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/prefix_spec.rb b/spec/unit/parser/functions/prefix_spec.rb new file mode 100644 index 0000000..9ede439 --- /dev/null +++ b/spec/unit/parser/functions/prefix_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the prefix function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("prefix").should == "function_prefix" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_prefix([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/rand_spec.rb b/spec/unit/parser/functions/rand_spec.rb new file mode 100644 index 0000000..818d1c5 --- /dev/null +++ b/spec/unit/parser/functions/rand_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the rand function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("rand").should == "function_rand" + end + + it "should raise a ParseError if there is not 0 or 1 arguments" do + lambda { @scope.function_rand(['a','b']) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/random_crontab_minutes_spec.rb b/spec/unit/parser/functions/random_crontab_minutes_spec.rb new file mode 100644 index 0000000..b47b3ae --- /dev/null +++ b/spec/unit/parser/functions/random_crontab_minutes_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the random_crontab_minutes function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("random_crontab_minutes").should == "function_random_crontab_minutes" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_random_crontab_minutes([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/range_spec.rb b/spec/unit/parser/functions/range_spec.rb new file mode 100644 index 0000000..23310bc --- /dev/null +++ b/spec/unit/parser/functions/range_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the range function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("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)) + end + +end diff --git a/spec/unit/parser/functions/reverse_spec.rb b/spec/unit/parser/functions/reverse_spec.rb new file mode 100644 index 0000000..27aa2cf --- /dev/null +++ b/spec/unit/parser/functions/reverse_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the reverse function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("reverse").should == "function_reverse" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_reverse([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/rstrip_spec.rb b/spec/unit/parser/functions/rstrip_spec.rb new file mode 100644 index 0000000..a6e73ec --- /dev/null +++ b/spec/unit/parser/functions/rstrip_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the rstrip function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("rstrip").should == "function_rstrip" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_rstrip([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/shuffle_spec.rb b/spec/unit/parser/functions/shuffle_spec.rb new file mode 100644 index 0000000..cf063c6 --- /dev/null +++ b/spec/unit/parser/functions/shuffle_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the shuffle function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("shuffle").should == "function_shuffle" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_shuffle([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/size_spec.rb b/spec/unit/parser/functions/size_spec.rb new file mode 100644 index 0000000..0d1f0c4 --- /dev/null +++ b/spec/unit/parser/functions/size_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the size function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("size").should == "function_size" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_size([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/sort_spec.rb b/spec/unit/parser/functions/sort_spec.rb new file mode 100644 index 0000000..ae62d5f --- /dev/null +++ b/spec/unit/parser/functions/sort_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the sort function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("sort").should == "function_sort" + end + + it "should raise a ParseError if there is not 0 arguments" do + lambda { @scope.function_sort(['']) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/squeeze_spec.rb b/spec/unit/parser/functions/squeeze_spec.rb new file mode 100644 index 0000000..da8965c --- /dev/null +++ b/spec/unit/parser/functions/squeeze_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the squeeze function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("squeeze").should == "function_squeeze" + end + + it "should raise a ParseError if there is less than 2 arguments" do + lambda { @scope.function_squeeze([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/str2bool_spec.rb b/spec/unit/parser/functions/str2bool_spec.rb new file mode 100644 index 0000000..6a1ac95 --- /dev/null +++ b/spec/unit/parser/functions/str2bool_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the str2bool function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("str2bool").should == "function_str2bool" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_str2bool([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/strftime_spec.rb b/spec/unit/parser/functions/strftime_spec.rb new file mode 100644 index 0000000..e377954 --- /dev/null +++ b/spec/unit/parser/functions/strftime_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the strftime function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("strftime").should == "function_strftime" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_strftime([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/strip_spec.rb b/spec/unit/parser/functions/strip_spec.rb new file mode 100644 index 0000000..ca06845 --- /dev/null +++ b/spec/unit/parser/functions/strip_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the strip function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("strip").should == "function_strip" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_strip([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/swapcase_spec.rb b/spec/unit/parser/functions/swapcase_spec.rb new file mode 100644 index 0000000..7c5ff30 --- /dev/null +++ b/spec/unit/parser/functions/swapcase_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the swapcase function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("swapcase").should == "function_swapcase" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_swapcase([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/time_spec.rb b/spec/unit/parser/functions/time_spec.rb new file mode 100644 index 0000000..8bf5982 --- /dev/null +++ b/spec/unit/parser/functions/time_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the time function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("time").should == "function_time" + end + + it "should raise a ParseError if there is more than 2 arguments" do + lambda { @scope.function_time(['','']) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/type_spec.rb b/spec/unit/parser/functions/type_spec.rb new file mode 100644 index 0000000..4109da1 --- /dev/null +++ b/spec/unit/parser/functions/type_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the type function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("type").should == "function_type" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_type([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/unique_spec.rb b/spec/unit/parser/functions/unique_spec.rb new file mode 100644 index 0000000..fe7b3ca --- /dev/null +++ b/spec/unit/parser/functions/unique_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the unique function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("unique").should == "function_unique" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_unique([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/upcase_spec.rb b/spec/unit/parser/functions/upcase_spec.rb new file mode 100644 index 0000000..39884eb --- /dev/null +++ b/spec/unit/parser/functions/upcase_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the upcase function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("upcase").should == "function_upcase" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_upcase([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/values_at_spec.rb b/spec/unit/parser/functions/values_at_spec.rb new file mode 100644 index 0000000..af5dc25 --- /dev/null +++ b/spec/unit/parser/functions/values_at_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the values_at function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("values_at").should == "function_values_at" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_values_at([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/values_spec.rb b/spec/unit/parser/functions/values_spec.rb new file mode 100644 index 0000000..2c313a0 --- /dev/null +++ b/spec/unit/parser/functions/values_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the values function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("values").should == "function_values" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_values([]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/parser/functions/zip_spec.rb b/spec/unit/parser/functions/zip_spec.rb new file mode 100644 index 0000000..3d0392a --- /dev/null +++ b/spec/unit/parser/functions/zip_spec.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the zip function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("zip").should == "function_zip" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_zip([]) }.should( raise_error(Puppet::ParseError)) + end + +end -- cgit v1.2.3 From ff56d9917e52b1a0d14478af74411e81e3633e4f Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Wed, 29 Jun 2011 21:59:18 +0100 Subject: New abs test. --- spec/unit/parser/functions/abs_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/unit/parser/functions/abs_spec.rb b/spec/unit/parser/functions/abs_spec.rb index cd2902a..6bf0b41 100755 --- a/spec/unit/parser/functions/abs_spec.rb +++ b/spec/unit/parser/functions/abs_spec.rb @@ -18,4 +18,9 @@ describe "the abs function" do lambda { @scope.function_abs([]) }.should( raise_error(Puppet::ParseError)) end + it "should convert a negative number into a positive" do + result = @scope.function_abs([-34]) + result.should(eq(34)) + end + end -- cgit v1.2.3 From 464fb1f41b9c7197fcaade6831b80d6390829ec7 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Wed, 29 Jun 2011 23:37:37 +0100 Subject: Add some more functional tests. --- lib/puppet/parser/functions/date.rb | 2 ++ lib/puppet/parser/functions/delete.rb | 6 +++++ lib/puppet/parser/functions/fact.rb | 36 --------------------------- lib/puppet/parser/functions/grep.rb | 5 ++++ spec/unit/parser/functions/delete_at_spec.rb | 5 ++++ spec/unit/parser/functions/delete_spec.rb | 5 ++++ spec/unit/parser/functions/downcase_spec.rb | 5 ++++ spec/unit/parser/functions/empty_spec.rb | 5 ++++ spec/unit/parser/functions/fact_spec.rb | 21 ---------------- spec/unit/parser/functions/flatten_spec.rb | 5 ++++ spec/unit/parser/functions/grep_spec.rb | 5 ++++ spec/unit/parser/functions/hash_spec.rb | 5 ++++ spec/unit/parser/functions/is_array_spec.rb | 10 ++++++++ spec/unit/parser/functions/is_float_spec.rb | 15 +++++++++++ spec/unit/parser/functions/is_hash_spec.rb | 15 +++++++++++ spec/unit/parser/functions/is_integer_spec.rb | 15 +++++++++++ 16 files changed, 103 insertions(+), 57 deletions(-) delete mode 100644 lib/puppet/parser/functions/fact.rb delete mode 100755 spec/unit/parser/functions/fact_spec.rb diff --git a/lib/puppet/parser/functions/date.rb b/lib/puppet/parser/functions/date.rb index 4d0543e..bc62e60 100644 --- a/lib/puppet/parser/functions/date.rb +++ b/lib/puppet/parser/functions/date.rb @@ -12,6 +12,8 @@ module Puppet::Parser::Functions "given #{arguments.size} for 1") end + # TODO: stubbed + end end diff --git a/lib/puppet/parser/functions/delete.rb b/lib/puppet/parser/functions/delete.rb index 88f3448..0d208b5 100644 --- a/lib/puppet/parser/functions/delete.rb +++ b/lib/puppet/parser/functions/delete.rb @@ -15,6 +15,12 @@ module Puppet::Parser::Functions "given #{arguments.size} for 2") end + a = arguments[0] + item = arguments[1] + + a.delete(item) + a + end end diff --git a/lib/puppet/parser/functions/fact.rb b/lib/puppet/parser/functions/fact.rb deleted file mode 100644 index 27b7bb2..0000000 --- a/lib/puppet/parser/functions/fact.rb +++ /dev/null @@ -1,36 +0,0 @@ -# -# fact.rb -# - -module Puppet::Parser::Functions - newfunction(:fact, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "fact(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - fact = arguments[0] - - unless fact.is_a?(String) - raise(Puppet::ParseError, 'fact(): Requires fact name to be a string') - end - - raise(Puppet::ParseError, 'fact(): You must provide ' + - 'fact name') if fact.empty? - - result = lookupvar(fact) # Get the value of interest from Facter ... - - # - # Now this is a funny one ... Puppet does not have a concept of - # returning neither undef nor nil back for use within the Puppet DSL - # and empty string is as closest to actual undef as you we can get - # at this point in time ... - # - result = result.empty? ? '' : result - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/grep.rb b/lib/puppet/parser/functions/grep.rb index 8549218..2caaa6f 100644 --- a/lib/puppet/parser/functions/grep.rb +++ b/lib/puppet/parser/functions/grep.rb @@ -12,6 +12,11 @@ module Puppet::Parser::Functions "given #{arguments.size} for 2") end + a = arguments[0] + pattern = Regexp.new(arguments[1]) + + a.grep(pattern) + end end diff --git a/spec/unit/parser/functions/delete_at_spec.rb b/spec/unit/parser/functions/delete_at_spec.rb index a0b5b06..27db0c8 100755 --- a/spec/unit/parser/functions/delete_at_spec.rb +++ b/spec/unit/parser/functions/delete_at_spec.rb @@ -18,4 +18,9 @@ describe "the delete_at function" do lambda { @scope.function_delete_at([]) }.should( raise_error(Puppet::ParseError)) end + it "should delete an item at specified location from an array" do + result = @scope.function_delete_at([['a','b','c'],1]) + result.should(eq(['a','c'])) + end + end diff --git a/spec/unit/parser/functions/delete_spec.rb b/spec/unit/parser/functions/delete_spec.rb index b0729ab..fab3230 100755 --- a/spec/unit/parser/functions/delete_spec.rb +++ b/spec/unit/parser/functions/delete_spec.rb @@ -18,4 +18,9 @@ describe "the delete function" do lambda { @scope.function_delete([]) }.should( raise_error(Puppet::ParseError)) end + it "should delete an item from an array" do + result = @scope.function_delete([['a','b','c'],'b']) + result.should(eq(['a','c'])) + end + end diff --git a/spec/unit/parser/functions/downcase_spec.rb b/spec/unit/parser/functions/downcase_spec.rb index 162291c..0d53220 100755 --- a/spec/unit/parser/functions/downcase_spec.rb +++ b/spec/unit/parser/functions/downcase_spec.rb @@ -18,4 +18,9 @@ describe "the downcase function" do lambda { @scope.function_downcase([]) }.should( raise_error(Puppet::ParseError)) end + it "should downcase a string" do + result = @scope.function_downcase(["ASFD"]) + result.should(eq("asfd")) + end + end diff --git a/spec/unit/parser/functions/empty_spec.rb b/spec/unit/parser/functions/empty_spec.rb index beaf45c..6c0fe69 100755 --- a/spec/unit/parser/functions/empty_spec.rb +++ b/spec/unit/parser/functions/empty_spec.rb @@ -18,4 +18,9 @@ describe "the empty function" do lambda { @scope.function_empty([]) }.should( raise_error(Puppet::ParseError)) end + it "should return a true for an empty string" do + result = @scope.function_empty(['']) + result.should(eq(true)) + end + end diff --git a/spec/unit/parser/functions/fact_spec.rb b/spec/unit/parser/functions/fact_spec.rb deleted file mode 100755 index c013ae0..0000000 --- a/spec/unit/parser/functions/fact_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the fact function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("fact").should == "function_fact" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_fact([]) }.should( raise_error(Puppet::ParseError)) - end - -end diff --git a/spec/unit/parser/functions/flatten_spec.rb b/spec/unit/parser/functions/flatten_spec.rb index 7af23c1..91cf4ef 100755 --- a/spec/unit/parser/functions/flatten_spec.rb +++ b/spec/unit/parser/functions/flatten_spec.rb @@ -18,4 +18,9 @@ describe "the flatten function" do lambda { @scope.function_flatten([]) }.should( raise_error(Puppet::ParseError)) end + it "should flatten a complex data structure" do + result = @scope.function_flatten([["a","b",["c",["d","e"],"f","g"]]]) + result.should(eq(["a","b","c","d","e","f","g"])) + end + end diff --git a/spec/unit/parser/functions/grep_spec.rb b/spec/unit/parser/functions/grep_spec.rb index 1c949da..b1f647c 100755 --- a/spec/unit/parser/functions/grep_spec.rb +++ b/spec/unit/parser/functions/grep_spec.rb @@ -18,4 +18,9 @@ describe "the grep function" do lambda { @scope.function_grep([]) }.should( raise_error(Puppet::ParseError)) end + it "should grep contents from an array" do + result = @scope.function_grep([["aaabbb","bbbccc","dddeee"], "bbb"]) + result.should(eq(["aaabbb","bbbccc"])) + end + end diff --git a/spec/unit/parser/functions/hash_spec.rb b/spec/unit/parser/functions/hash_spec.rb index 09b0d50..6d3d48c 100644 --- a/spec/unit/parser/functions/hash_spec.rb +++ b/spec/unit/parser/functions/hash_spec.rb @@ -18,4 +18,9 @@ describe "the hash function" do lambda { @scope.function_hash([]) }.should( raise_error(Puppet::ParseError)) end + it "should convert an array to a hash" do + result = @scope.function_hash([['a',1,'b',2,'c',3]]) + result.should(eq({'a'=>1,'b'=>2,'c'=>3})) + end + end diff --git a/spec/unit/parser/functions/is_array_spec.rb b/spec/unit/parser/functions/is_array_spec.rb index b2843b0..15b563c 100644 --- a/spec/unit/parser/functions/is_array_spec.rb +++ b/spec/unit/parser/functions/is_array_spec.rb @@ -18,4 +18,14 @@ describe "the is_array function" do lambda { @scope.function_is_array([]) }.should( raise_error(Puppet::ParseError)) end + it "should return true if passed an array" do + result = @scope.function_is_array([[1,2,3]]) + result.should(eq(true)) + end + + it "should return false if passed a hash" do + result = @scope.function_is_array([{'a'=>1}]) + result.should(eq(false)) + end + end diff --git a/spec/unit/parser/functions/is_float_spec.rb b/spec/unit/parser/functions/is_float_spec.rb index e3dc8fc..3d58017 100644 --- a/spec/unit/parser/functions/is_float_spec.rb +++ b/spec/unit/parser/functions/is_float_spec.rb @@ -18,4 +18,19 @@ describe "the is_float function" do lambda { @scope.function_is_float([]) }.should( raise_error(Puppet::ParseError)) end + it "should return true if a float" do + result = @scope.function_is_float([0.12]) + result.should(eq(true)) + end + + it "should return false if a string" do + result = @scope.function_is_float(["asdf"]) + result.should(eq(false)) + end + + it "should return false if not an integer" do + result = @scope.function_is_float([3]) + result.should(eq(false)) + end + end diff --git a/spec/unit/parser/functions/is_hash_spec.rb b/spec/unit/parser/functions/is_hash_spec.rb index 66dfdeb..94364f5 100644 --- a/spec/unit/parser/functions/is_hash_spec.rb +++ b/spec/unit/parser/functions/is_hash_spec.rb @@ -18,4 +18,19 @@ describe "the is_hash function" do lambda { @scope.function_is_hash([]) }.should( raise_error(Puppet::ParseError)) end + it "should return true if passed a hash" do + result = @scope.function_is_hash([{"a"=>1,"b"=>2}]) + result.should(eq(true)) + end + + it "should return false if passed an array" do + result = @scope.function_is_hash([["a","b"]]) + result.should(eq(false)) + end + + it "should return false if passed a string" do + result = @scope.function_is_hash(["asdf"]) + result.should(eq(false)) + end + end diff --git a/spec/unit/parser/functions/is_integer_spec.rb b/spec/unit/parser/functions/is_integer_spec.rb index 131251c..596bd33 100644 --- a/spec/unit/parser/functions/is_integer_spec.rb +++ b/spec/unit/parser/functions/is_integer_spec.rb @@ -18,4 +18,19 @@ describe "the is_integer function" do lambda { @scope.function_is_integer([]) }.should( raise_error(Puppet::ParseError)) end + it "should return true if an integer" do + result = @scope.function_is_integer([3]) + result.should(eq(true)) + end + + it "should return false if a float" do + result = @scope.function_is_integer([3.2]) + result.should(eq(false)) + end + + it "should return false if a string" do + result = @scope.function_is_integer(["asdf"]) + result.should(eq(false)) + end + end -- cgit v1.2.3 From c7c8647634df07f0de0e662360eb4567f7c20770 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Wed, 29 Jun 2011 23:39:23 +0100 Subject: Move require inside function for kwalify. --- lib/puppet/parser/functions/kwalify.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/puppet/parser/functions/kwalify.rb b/lib/puppet/parser/functions/kwalify.rb index 7238f84..49b9aeb 100644 --- a/lib/puppet/parser/functions/kwalify.rb +++ b/lib/puppet/parser/functions/kwalify.rb @@ -2,8 +2,6 @@ # kwalify.rb # -require 'kwalify' - module Puppet::Parser::Functions newfunction(:kwalify, :type => :statement, :doc => <<-EOS This function uses kwalify to validate Puppet data structures against Kwalify @@ -17,6 +15,8 @@ schemas. schema = args[0] document = args[1] + require 'kwalify' + validator = Kwalify::Validator.new(schema) errors = validator.validate(document) -- cgit v1.2.3 From 1abf4b62fc8e97c7096c9da3d350e3e6268c5c72 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Thu, 30 Jun 2011 01:00:32 +0200 Subject: Few more tests. --- lib/puppet/parser/functions/date.rb | 2 +- spec/unit/parser/functions/bool2num_spec.rb | 10 ++++++++++ spec/unit/parser/functions/capitalize_spec.rb | 5 +++++ spec/unit/parser/functions/chomp_spec.rb | 5 +++++ spec/unit/parser/functions/chop_spec.rb | 5 +++++ spec/unit/parser/functions/count_spec.rb | 5 +++++ 6 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/puppet/parser/functions/date.rb b/lib/puppet/parser/functions/date.rb index bc62e60..0439bd1 100644 --- a/lib/puppet/parser/functions/date.rb +++ b/lib/puppet/parser/functions/date.rb @@ -8,7 +8,7 @@ module Puppet::Parser::Functions ) do |arguments| if (arguments.size != 1) then - raise(Puppet::ParseError, "is_valid_netmask(): Wrong number of arguments "+ + raise(Puppet::ParseError, "date(): Wrong number of arguments "+ "given #{arguments.size} for 1") end diff --git a/spec/unit/parser/functions/bool2num_spec.rb b/spec/unit/parser/functions/bool2num_spec.rb index a2585e9..d5da18c 100755 --- a/spec/unit/parser/functions/bool2num_spec.rb +++ b/spec/unit/parser/functions/bool2num_spec.rb @@ -18,4 +18,14 @@ describe "the bool2num function" do lambda { @scope.function_bool2num([]) }.should( raise_error(Puppet::ParseError)) end + it "should convert true to 1" do + result = @scope.function_bool2num([true]) + result.should(eq(1)) + end + + it "should convert false to 0" do + result = @scope.function_bool2num([false]) + result.should(eq(0)) + end + end diff --git a/spec/unit/parser/functions/capitalize_spec.rb b/spec/unit/parser/functions/capitalize_spec.rb index bb1fe2a..1c45821 100755 --- a/spec/unit/parser/functions/capitalize_spec.rb +++ b/spec/unit/parser/functions/capitalize_spec.rb @@ -18,4 +18,9 @@ describe "the capitalize function" do lambda { @scope.function_capitalize([]) }.should( raise_error(Puppet::ParseError)) end + it "should capitalize the beginning of a string" do + result = @scope.function_capitalize(["abc"]) + result.should(eq("Abc")) + end + end diff --git a/spec/unit/parser/functions/chomp_spec.rb b/spec/unit/parser/functions/chomp_spec.rb index 150a7c8..0592115 100755 --- a/spec/unit/parser/functions/chomp_spec.rb +++ b/spec/unit/parser/functions/chomp_spec.rb @@ -18,4 +18,9 @@ describe "the chomp function" do lambda { @scope.function_chomp([]) }.should( raise_error(Puppet::ParseError)) end + it "should chomp the end of a string" do + result = @scope.function_chomp(["abc\n"]) + result.should(eq("abc")) + end + end diff --git a/spec/unit/parser/functions/chop_spec.rb b/spec/unit/parser/functions/chop_spec.rb index ae636cb..0c456a8 100755 --- a/spec/unit/parser/functions/chop_spec.rb +++ b/spec/unit/parser/functions/chop_spec.rb @@ -18,4 +18,9 @@ describe "the chop function" do lambda { @scope.function_chop([]) }.should( raise_error(Puppet::ParseError)) end + it "should chop the end of a string" do + result = @scope.function_chop(["asdf\n"]) + result.should(eq("asdf")) + end + end diff --git a/spec/unit/parser/functions/count_spec.rb b/spec/unit/parser/functions/count_spec.rb index 28617c9..62d005a 100755 --- a/spec/unit/parser/functions/count_spec.rb +++ b/spec/unit/parser/functions/count_spec.rb @@ -18,4 +18,9 @@ describe "the count function" do lambda { @scope.function_count([]) }.should( raise_error(Puppet::ParseError)) end + it "should return the size of an array" do + result = @scope.function_count([['a','c','b']]) + result.should(eq(3)) + end + end -- cgit v1.2.3 From 07ee33455439d960b9996e8110e011437181b42a Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 1 Jul 2011 21:09:02 +0200 Subject: Added validate_resource function and examples on how to use it (and kwalify as well) --- examples/kwalify-1.pp | 9 ++++++ examples/kwalify-2.pp | 24 +++++++++++++++ examples/validate_resource-1.pp | 23 ++++++++++++++ examples/validate_resource-1.schema | 39 ++++++++++++++++++++++++ examples/validate_resource-2.pp | 17 +++++++++++ examples/validate_resource-2.schema | 26 ++++++++++++++++ lib/puppet/parser/functions/validate_resource.rb | 36 ++++++++++++++++++++++ 7 files changed, 174 insertions(+) create mode 100644 examples/kwalify-1.pp create mode 100644 examples/kwalify-2.pp create mode 100644 examples/validate_resource-1.pp create mode 100644 examples/validate_resource-1.schema create mode 100644 examples/validate_resource-2.pp create mode 100644 examples/validate_resource-2.schema create mode 100644 lib/puppet/parser/functions/validate_resource.rb diff --git a/examples/kwalify-1.pp b/examples/kwalify-1.pp new file mode 100644 index 0000000..852e46c --- /dev/null +++ b/examples/kwalify-1.pp @@ -0,0 +1,9 @@ +$schema = { + 'type' => 'seq', + 'sequence' => [ + { 'type' => 'str', 'enum' => ['asdf','fdsa'] } + ] +} +$document = ['a', 'b', 'c'] + +kwalify($schema, $document) diff --git a/examples/kwalify-2.pp b/examples/kwalify-2.pp new file mode 100644 index 0000000..3f4ec33 --- /dev/null +++ b/examples/kwalify-2.pp @@ -0,0 +1,24 @@ +$schema = { + 'type' => 'map', + 'mapping' => { + 'name' => { + 'type' => 'str', + 'required' => true, + }, + 'email' => { + 'type' => 'str', + 'pattern' => '/@/', + }, + 'age' => { + 'type' => 'str', + 'pattern' => '/^\d+$/', + }, + } +} +$document = { + 'name' => 'foo', + 'email' => 'foo@mail.com', + 'age' => 20, +} + +kwalify($schema, $document) diff --git a/examples/validate_resource-1.pp b/examples/validate_resource-1.pp new file mode 100644 index 0000000..c701b8d --- /dev/null +++ b/examples/validate_resource-1.pp @@ -0,0 +1,23 @@ +define fooresource( + $color, + $type, + $somenumber, + $map + ) { + + validate_resource() + + # ... do something ... + +} + +fooresource { "example1": + color => "blue", + type => "circle", + somenumber => 5, + map => { + a => 1, + b => 2, + c => 3, + } +} diff --git a/examples/validate_resource-1.schema b/examples/validate_resource-1.schema new file mode 100644 index 0000000..c540db5 --- /dev/null +++ b/examples/validate_resource-1.schema @@ -0,0 +1,39 @@ +type: map +mapping: + "title": + type: str + required: yes + "name": + type: str + required: yes + "caller_module_name": + type: str + required: yes + "module_name": + type: str + required: yes + "color": + type: str + required: yes + "type": + type: str + required: yes + "somenumber": + type: str + required: yes + pattern: /^\d+$/ + "map": + type: map + mapping: + "a": + type: str + required: yes + pattern: /^\d+$/ + "b": + type: str + required: yes + pattern: /^\d+$/ + "c": + type: str + required: yes + pattern: /^\d+$/ diff --git a/examples/validate_resource-2.pp b/examples/validate_resource-2.pp new file mode 100644 index 0000000..b53b109 --- /dev/null +++ b/examples/validate_resource-2.pp @@ -0,0 +1,17 @@ +class foo ( + $a, + $b, + $c + ) { + + validate_resource() + + # ... do something ... + +} + +class { "foo": + a => "1", + b => "foobaz", + c => ['a','b','c'] +} diff --git a/examples/validate_resource-2.schema b/examples/validate_resource-2.schema new file mode 100644 index 0000000..b516945 --- /dev/null +++ b/examples/validate_resource-2.schema @@ -0,0 +1,26 @@ +type: map +mapping: + "title": + type: str + required: yes + "name": + type: str + required: yes + "caller_module_name": + type: str + required: yes + "module_name": + type: str + required: yes + "a": + type: str + required: yes + "b": + type: str + required: yes + pattern: /^foo/ + "c": + type: seq + required: yes + sequence: + - type: str diff --git a/lib/puppet/parser/functions/validate_resource.rb b/lib/puppet/parser/functions/validate_resource.rb new file mode 100644 index 0000000..bbb5a54 --- /dev/null +++ b/lib/puppet/parser/functions/validate_resource.rb @@ -0,0 +1,36 @@ +# +# validate_resource +# + +module Puppet::Parser::Functions + newfunction(:validate_resource, :type => :statement, :doc => <<-EOS + EOS + ) do |arguments| + + require 'kwalify' + + if (arguments.size != 0) then + raise(Puppet::ParseError, "validate_resource(): Wrong number of arguments "+ + "given #{arguments.size} for 0") + end + + + classhash = to_hash(recursive=false) + sourcepath = source.file + schemapath = sourcepath.gsub(/\.(rb|pp)$/, ".schema") + schema = Kwalify::Yaml.load_file(schemapath) + validator = Kwalify::Validator.new(schema) + errors = validator.validate(classhash) + + if errors && !errors.empty? + error_output = "Resource validation failed:\n" + for e in errors + error_output += "[#{e.path}] #{e.message}\n" + end + raise(Puppet::ParseError, error_output) + end + + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From fde64f37c98291f4a5b9ee1fcf634288e42b730a Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Sun, 24 Jul 2011 00:39:17 +0100 Subject: (#1) - fleshed out some more tests. --- lib/puppet/parser/functions/sort.rb | 6 ++++-- lib/puppet/parser/functions/squeeze.rb | 21 +++++++++++++++++++-- .../parser/functions/is_valid_domain_name_spec.rb | 10 ++++++++++ .../parser/functions/is_valid_ip_address_spec.rb | 10 ++++++++++ .../parser/functions/is_valid_mac_address_spec.rb | 10 ++++++++++ spec/unit/parser/functions/join_spec.rb | 5 +++++ spec/unit/parser/functions/join_with_prefix_spec.rb | 5 +++++ spec/unit/parser/functions/keys_spec.rb | 5 +++++ spec/unit/parser/functions/lstrip_spec.rb | 5 +++++ spec/unit/parser/functions/member_spec.rb | 10 ++++++++++ spec/unit/parser/functions/num2bool_spec.rb | 5 +++++ spec/unit/parser/functions/prefix_spec.rb | 5 +++++ spec/unit/parser/functions/range_spec.rb | 10 ++++++++++ spec/unit/parser/functions/reverse_spec.rb | 5 +++++ spec/unit/parser/functions/rstrip_spec.rb | 10 ++++++++++ spec/unit/parser/functions/size_spec.rb | 10 ++++++++++ spec/unit/parser/functions/sort_spec.rb | 9 +++++++-- spec/unit/parser/functions/squeeze_spec.rb | 10 ++++++++++ spec/unit/parser/functions/str2bool_spec.rb | 10 ++++++++++ spec/unit/parser/functions/strip_spec.rb | 5 +++++ spec/unit/parser/functions/swapcase_spec.rb | 5 +++++ spec/unit/parser/functions/type_spec.rb | 5 +++++ spec/unit/parser/functions/unique_spec.rb | 5 +++++ spec/unit/parser/functions/upcase_spec.rb | 5 +++++ spec/unit/parser/functions/values_at_spec.rb | 5 +++++ spec/unit/parser/functions/values_spec.rb | 5 +++++ spec/unit/parser/functions/zip_spec.rb | 5 +++++ 27 files changed, 195 insertions(+), 6 deletions(-) diff --git a/lib/puppet/parser/functions/sort.rb b/lib/puppet/parser/functions/sort.rb index 974141c..54a4018 100644 --- a/lib/puppet/parser/functions/sort.rb +++ b/lib/puppet/parser/functions/sort.rb @@ -7,11 +7,13 @@ module Puppet::Parser::Functions EOS ) do |arguments| - if (arguments.size != 0) then + if (arguments.size != 1) then raise(Puppet::ParseError, "sort(): Wrong number of arguments "+ - "given #{arguments.size} for 0") + "given #{arguments.size} for 1") end + arguments[0].sort + end end diff --git a/lib/puppet/parser/functions/squeeze.rb b/lib/puppet/parser/functions/squeeze.rb index 02eb00c..9a1dd20 100644 --- a/lib/puppet/parser/functions/squeeze.rb +++ b/lib/puppet/parser/functions/squeeze.rb @@ -7,9 +7,26 @@ module Puppet::Parser::Functions EOS ) do |arguments| - if (arguments.size != 2) then + if ((arguments.size != 2) and (arguments.size != 1)) then raise(Puppet::ParseError, "squeeze(): Wrong number of arguments "+ - "given #{arguments.size} for 2") + "given #{arguments.size} for 2 or 1") + end + + item = arguments[0] + squeezeval = arguments[1] + + if item.is_a?(Array) then + if squeezeval then + item.collect { |i| i.squeeze(squeezeval) } + else + item.collect { |i| i.squeeze } + end + else + if squeezeval then + item.squeeze(squeezeval) + else + item.squeeze + end end end diff --git a/spec/unit/parser/functions/is_valid_domain_name_spec.rb b/spec/unit/parser/functions/is_valid_domain_name_spec.rb index 5cea285..f03f6e8 100644 --- a/spec/unit/parser/functions/is_valid_domain_name_spec.rb +++ b/spec/unit/parser/functions/is_valid_domain_name_spec.rb @@ -18,4 +18,14 @@ describe "the is_valid_domain_name function" do lambda { @scope.function_is_valid_domain_name([]) }.should( raise_error(Puppet::ParseError)) end + it "should return true if a valid domain name" do + result = @scope.function_is_valid_domain_name("foo.bar.com") + result.should(eq(true)) + end + + it "should return false if not a valid domain name" do + result = @scope.function_is_valid_domain_name("not valid") + result.should(eq(false)) + end + end diff --git a/spec/unit/parser/functions/is_valid_ip_address_spec.rb b/spec/unit/parser/functions/is_valid_ip_address_spec.rb index fa803dd..ee53ee1 100644 --- a/spec/unit/parser/functions/is_valid_ip_address_spec.rb +++ b/spec/unit/parser/functions/is_valid_ip_address_spec.rb @@ -18,4 +18,14 @@ describe "the is_valid_ip_address function" do lambda { @scope.function_is_valid_ip_address([]) }.should( raise_error(Puppet::ParseError)) end + it "should return true if an IP address" do + result = @scope.function_is_valid_ip_address("1.2.3.4") + result.should(eq(true)) + end + + it "should return false if not valid" do + result = @scope.function_is_valid_ip_address("asdf") + result.should(eq(false)) + end + end diff --git a/spec/unit/parser/functions/is_valid_mac_address_spec.rb b/spec/unit/parser/functions/is_valid_mac_address_spec.rb index f2a8389..c4eb468 100644 --- a/spec/unit/parser/functions/is_valid_mac_address_spec.rb +++ b/spec/unit/parser/functions/is_valid_mac_address_spec.rb @@ -18,4 +18,14 @@ describe "the is_valid_mac_address function" do lambda { @scope.function_is_valid_mac_address([]) }.should( raise_error(Puppet::ParseError)) end + it "should return true if a valid mac address" do + result = @scope.function_is_valid_mac_address("00:a0:1f:12:7f:a0") + result.should(eq(true)) + end + + it "should return false if not valid" do + result = @scope.function_is_valid_mac_address("not valid") + result.should(eq(false)) + end + end diff --git a/spec/unit/parser/functions/join_spec.rb b/spec/unit/parser/functions/join_spec.rb index a7dc0e5..1b3dec8 100644 --- a/spec/unit/parser/functions/join_spec.rb +++ b/spec/unit/parser/functions/join_spec.rb @@ -18,4 +18,9 @@ describe "the join function" do lambda { @scope.function_join([]) }.should( raise_error(Puppet::ParseError)) end + it "should join an array into a string" do + result = @scope.function_join([["a","b","c"], ":"]) + result.should(eq("a:b:c")) + end + end diff --git a/spec/unit/parser/functions/join_with_prefix_spec.rb b/spec/unit/parser/functions/join_with_prefix_spec.rb index 0182d8c..70e6965 100644 --- a/spec/unit/parser/functions/join_with_prefix_spec.rb +++ b/spec/unit/parser/functions/join_with_prefix_spec.rb @@ -18,4 +18,9 @@ describe "the join_with_prefix function" do lambda { @scope.function_join_with_prefix([]) }.should( raise_error(Puppet::ParseError)) end + it "should join an array into a string" do + result = @scope.function_join_with_prefix([["a","b","c"], ":", "p"]) + result.should(eq("pa:pb:pc")) + end + end diff --git a/spec/unit/parser/functions/keys_spec.rb b/spec/unit/parser/functions/keys_spec.rb index 13dc260..927be96 100644 --- a/spec/unit/parser/functions/keys_spec.rb +++ b/spec/unit/parser/functions/keys_spec.rb @@ -18,4 +18,9 @@ describe "the keys function" do lambda { @scope.function_keys([]) }.should( raise_error(Puppet::ParseError)) end + it "should return an array of keys when given a hash" do + result = @scope.function_keys([{'a'=>1, 'b' => 2}]) + result.should(eq(['a','b'])) + end + end diff --git a/spec/unit/parser/functions/lstrip_spec.rb b/spec/unit/parser/functions/lstrip_spec.rb index 9726675..ac331fa 100644 --- a/spec/unit/parser/functions/lstrip_spec.rb +++ b/spec/unit/parser/functions/lstrip_spec.rb @@ -18,4 +18,9 @@ describe "the lstrip function" do lambda { @scope.function_lstrip([]) }.should( raise_error(Puppet::ParseError)) end + it "should lstrip a string" do + result = @scope.function_lstrip([" asdf"]) + result.should(eq('asdf')) + end + end diff --git a/spec/unit/parser/functions/member_spec.rb b/spec/unit/parser/functions/member_spec.rb index 39b684f..2cebc0d 100644 --- a/spec/unit/parser/functions/member_spec.rb +++ b/spec/unit/parser/functions/member_spec.rb @@ -18,4 +18,14 @@ describe "the member function" do lambda { @scope.function_member([]) }.should( raise_error(Puppet::ParseError)) end + it "should return true if a member is in an array" do + result = @scope.function_member([["a","b","c"], "a"]) + result.should(eq(true)) + end + + it "should return false if a member is not in an array" do + result = @scope.function_member([["a","b","c"], "d"]) + result.should(eq(false)) + end + end diff --git a/spec/unit/parser/functions/num2bool_spec.rb b/spec/unit/parser/functions/num2bool_spec.rb index fbd25c8..2f6435d 100644 --- a/spec/unit/parser/functions/num2bool_spec.rb +++ b/spec/unit/parser/functions/num2bool_spec.rb @@ -18,4 +18,9 @@ describe "the num2bool function" do lambda { @scope.function_num2bool([]) }.should( raise_error(Puppet::ParseError)) end + it "should return true if 1" do + result = @scope.function_num2bool(["1"]) + result.should(eq(true)) + end + end diff --git a/spec/unit/parser/functions/prefix_spec.rb b/spec/unit/parser/functions/prefix_spec.rb index 9ede439..a0cbcab 100644 --- a/spec/unit/parser/functions/prefix_spec.rb +++ b/spec/unit/parser/functions/prefix_spec.rb @@ -18,4 +18,9 @@ describe "the prefix function" do lambda { @scope.function_prefix([]) }.should( raise_error(Puppet::ParseError)) end + it "should return a prefixed array" do + result = @scope.function_prefix([['a','b','c'], 'p']) + result.should(eq(['pa','pb','pc'])) + end + end diff --git a/spec/unit/parser/functions/range_spec.rb b/spec/unit/parser/functions/range_spec.rb index 23310bc..8c2446a 100644 --- a/spec/unit/parser/functions/range_spec.rb +++ b/spec/unit/parser/functions/range_spec.rb @@ -18,4 +18,14 @@ describe "the range function" do lambda { @scope.function_range([]) }.should( raise_error(Puppet::ParseError)) end + it "should return a letter range" do + result = @scope.function_range(["a","d"]) + 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])) + end + end diff --git a/spec/unit/parser/functions/reverse_spec.rb b/spec/unit/parser/functions/reverse_spec.rb index 27aa2cf..4fa50e4 100644 --- a/spec/unit/parser/functions/reverse_spec.rb +++ b/spec/unit/parser/functions/reverse_spec.rb @@ -18,4 +18,9 @@ describe "the reverse function" do lambda { @scope.function_reverse([]) }.should( raise_error(Puppet::ParseError)) end + it "should reverse a string" do + result = @scope.function_reverse(["asdfghijkl"]) + result.should(eq('lkjihgfdsa')) + end + end diff --git a/spec/unit/parser/functions/rstrip_spec.rb b/spec/unit/parser/functions/rstrip_spec.rb index a6e73ec..af8cc12 100644 --- a/spec/unit/parser/functions/rstrip_spec.rb +++ b/spec/unit/parser/functions/rstrip_spec.rb @@ -18,4 +18,14 @@ describe "the rstrip function" do lambda { @scope.function_rstrip([]) }.should( raise_error(Puppet::ParseError)) end + it "should rstrip a string" do + result = @scope.function_rstrip(["asdf "]) + result.should(eq('asdf')) + end + + it "should rstrip each element in an array" do + result = @scope.function_rstrip([["a ","b ", "c "]]) + result.should(eq(['a','b','c'])) + end + end diff --git a/spec/unit/parser/functions/size_spec.rb b/spec/unit/parser/functions/size_spec.rb index 0d1f0c4..ccaa335 100644 --- a/spec/unit/parser/functions/size_spec.rb +++ b/spec/unit/parser/functions/size_spec.rb @@ -18,4 +18,14 @@ describe "the size function" do lambda { @scope.function_size([]) }.should( raise_error(Puppet::ParseError)) end + it "should return the size of a string" do + result = @scope.function_size(["asdf"]) + result.should(eq(4)) + end + + it "should return the size of an array" do + result = @scope.function_size([["a","b","c"]]) + result.should(eq(3)) + end + end diff --git a/spec/unit/parser/functions/sort_spec.rb b/spec/unit/parser/functions/sort_spec.rb index ae62d5f..da5db7a 100644 --- a/spec/unit/parser/functions/sort_spec.rb +++ b/spec/unit/parser/functions/sort_spec.rb @@ -14,8 +14,13 @@ describe "the sort function" do Puppet::Parser::Functions.function("sort").should == "function_sort" end - it "should raise a ParseError if there is not 0 arguments" do - lambda { @scope.function_sort(['']) }.should( raise_error(Puppet::ParseError)) + it "should raise a ParseError if there is not 1 arguments" do + lambda { @scope.function_sort(['','']) }.should( raise_error(Puppet::ParseError)) + end + + it "should sort an array" do + result = @scope.function_sort([["a","c","b"]]) + result.should(eq(['a','b','c'])) end end diff --git a/spec/unit/parser/functions/squeeze_spec.rb b/spec/unit/parser/functions/squeeze_spec.rb index da8965c..9355ad2 100644 --- a/spec/unit/parser/functions/squeeze_spec.rb +++ b/spec/unit/parser/functions/squeeze_spec.rb @@ -18,4 +18,14 @@ describe "the squeeze function" do lambda { @scope.function_squeeze([]) }.should( raise_error(Puppet::ParseError)) end + it "should squeeze a string" do + result = @scope.function_squeeze(["aaabbbbcccc"]) + result.should(eq('abc')) + end + + it "should squeeze all elements in an array" do + result = @scope.function_squeeze([["aaabbbbcccc","dddfff"]]) + result.should(eq(['abc','df'])) + end + end diff --git a/spec/unit/parser/functions/str2bool_spec.rb b/spec/unit/parser/functions/str2bool_spec.rb index 6a1ac95..d7f0ac9 100644 --- a/spec/unit/parser/functions/str2bool_spec.rb +++ b/spec/unit/parser/functions/str2bool_spec.rb @@ -18,4 +18,14 @@ describe "the str2bool function" do lambda { @scope.function_str2bool([]) }.should( raise_error(Puppet::ParseError)) end + it "should convert string 'true' to true" do + result = @scope.function_str2bool(["true"]) + result.should(eq(true)) + end + + it "should convert string 'undef' to false" do + result = @scope.function_str2bool(["undef"]) + result.should(eq(false)) + end + end diff --git a/spec/unit/parser/functions/strip_spec.rb b/spec/unit/parser/functions/strip_spec.rb index ca06845..48a52dd 100644 --- a/spec/unit/parser/functions/strip_spec.rb +++ b/spec/unit/parser/functions/strip_spec.rb @@ -18,4 +18,9 @@ describe "the strip function" do lambda { @scope.function_strip([]) }.should( raise_error(Puppet::ParseError)) end + it "should strip a string" do + result = @scope.function_strip([" ab cd "]) + result.should(eq('ab cd')) + end + end diff --git a/spec/unit/parser/functions/swapcase_spec.rb b/spec/unit/parser/functions/swapcase_spec.rb index 7c5ff30..2686054 100644 --- a/spec/unit/parser/functions/swapcase_spec.rb +++ b/spec/unit/parser/functions/swapcase_spec.rb @@ -18,4 +18,9 @@ describe "the swapcase function" do lambda { @scope.function_swapcase([]) }.should( raise_error(Puppet::ParseError)) end + it "should swapcase a string" do + result = @scope.function_swapcase(["aaBBccDD"]) + result.should(eq('AAbbCCdd')) + end + end diff --git a/spec/unit/parser/functions/type_spec.rb b/spec/unit/parser/functions/type_spec.rb index 4109da1..fddb87d 100644 --- a/spec/unit/parser/functions/type_spec.rb +++ b/spec/unit/parser/functions/type_spec.rb @@ -18,4 +18,9 @@ describe "the type function" do lambda { @scope.function_type([]) }.should( raise_error(Puppet::ParseError)) end + it "should return a type when given a string" do + result = @scope.function_type(["aaabbbbcccc"]) + result.should(eq('String')) + end + end diff --git a/spec/unit/parser/functions/unique_spec.rb b/spec/unit/parser/functions/unique_spec.rb index fe7b3ca..7b8b08d 100644 --- a/spec/unit/parser/functions/unique_spec.rb +++ b/spec/unit/parser/functions/unique_spec.rb @@ -18,4 +18,9 @@ describe "the unique function" do lambda { @scope.function_unique([]) }.should( raise_error(Puppet::ParseError)) end + it "should remove duplicate elements in a string" do + result = @scope.function_squeeze([["aabbc"]]) + result.should(eq(['abc'])) + end + end diff --git a/spec/unit/parser/functions/upcase_spec.rb b/spec/unit/parser/functions/upcase_spec.rb index 39884eb..10e4c8a 100644 --- a/spec/unit/parser/functions/upcase_spec.rb +++ b/spec/unit/parser/functions/upcase_spec.rb @@ -18,4 +18,9 @@ describe "the upcase function" do lambda { @scope.function_upcase([]) }.should( raise_error(Puppet::ParseError)) end + it "should upcase a string" do + result = @scope.function_upcase(["abc"]) + result.should(eq('ABC')) + end + end diff --git a/spec/unit/parser/functions/values_at_spec.rb b/spec/unit/parser/functions/values_at_spec.rb index af5dc25..ec8730b 100644 --- a/spec/unit/parser/functions/values_at_spec.rb +++ b/spec/unit/parser/functions/values_at_spec.rb @@ -18,4 +18,9 @@ describe "the values_at function" do lambda { @scope.function_values_at([]) }.should( raise_error(Puppet::ParseError)) end + it "should return a value at from an array" do + result = @scope.function_values_at([['a','b','c'],"1"]) + result.should(eq(['b'])) + end + end diff --git a/spec/unit/parser/functions/values_spec.rb b/spec/unit/parser/functions/values_spec.rb index 2c313a0..92f1311 100644 --- a/spec/unit/parser/functions/values_spec.rb +++ b/spec/unit/parser/functions/values_spec.rb @@ -18,4 +18,9 @@ describe "the values function" do lambda { @scope.function_values([]) }.should( raise_error(Puppet::ParseError)) end + it "should return values from a hash" do + result = @scope.function_values([{'a'=>'1','b'=>'2','c'=>'3'}]) + result.should(eq(['1','2','3'])) + end + end diff --git a/spec/unit/parser/functions/zip_spec.rb b/spec/unit/parser/functions/zip_spec.rb index 3d0392a..074f4df 100644 --- a/spec/unit/parser/functions/zip_spec.rb +++ b/spec/unit/parser/functions/zip_spec.rb @@ -18,4 +18,9 @@ describe "the zip function" do lambda { @scope.function_zip([]) }.should( raise_error(Puppet::ParseError)) end + it "should be able to zip an array" do + result = @scope.function_zip([['1','2','3'],['4','5','6']]) + result.should(eq([["1", "4"], ["2", "5"], ["3", "6"]])) + end + end -- cgit v1.2.3 From a55930368afe2e8177608472653c4696eccec92f Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Thu, 28 Jul 2011 15:38:19 +0100 Subject: (#2) - Added is_float and is_integer functionality. --- lib/puppet/parser/functions/is_float.rb | 8 ++++++++ lib/puppet/parser/functions/is_integer.rb | 8 ++++++++ spec/unit/parser/functions/is_float_spec.rb | 4 ++-- spec/unit/parser/functions/is_integer_spec.rb | 4 ++-- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/puppet/parser/functions/is_float.rb b/lib/puppet/parser/functions/is_float.rb index 39d097f..8aed848 100644 --- a/lib/puppet/parser/functions/is_float.rb +++ b/lib/puppet/parser/functions/is_float.rb @@ -12,6 +12,14 @@ module Puppet::Parser::Functions "given #{arguments.size} for 1") end + value = arguments[0] + + if value != value.to_f.to_s then + return false + else + return true + end + end end diff --git a/lib/puppet/parser/functions/is_integer.rb b/lib/puppet/parser/functions/is_integer.rb index 9813cf1..9dd1cee 100644 --- a/lib/puppet/parser/functions/is_integer.rb +++ b/lib/puppet/parser/functions/is_integer.rb @@ -12,6 +12,14 @@ module Puppet::Parser::Functions "given #{arguments.size} for 1") end + value = arguments[0] + + if value != value.to_i.to_s then + return false + else + return true + end + end end diff --git a/spec/unit/parser/functions/is_float_spec.rb b/spec/unit/parser/functions/is_float_spec.rb index 3d58017..35c0dd6 100644 --- a/spec/unit/parser/functions/is_float_spec.rb +++ b/spec/unit/parser/functions/is_float_spec.rb @@ -19,7 +19,7 @@ describe "the is_float function" do end it "should return true if a float" do - result = @scope.function_is_float([0.12]) + result = @scope.function_is_float(["0.12"]) result.should(eq(true)) end @@ -29,7 +29,7 @@ describe "the is_float function" do end it "should return false if not an integer" do - result = @scope.function_is_float([3]) + result = @scope.function_is_float(["3"]) result.should(eq(false)) end diff --git a/spec/unit/parser/functions/is_integer_spec.rb b/spec/unit/parser/functions/is_integer_spec.rb index 596bd33..faf6f2d 100644 --- a/spec/unit/parser/functions/is_integer_spec.rb +++ b/spec/unit/parser/functions/is_integer_spec.rb @@ -19,12 +19,12 @@ describe "the is_integer function" do end it "should return true if an integer" do - result = @scope.function_is_integer([3]) + result = @scope.function_is_integer(["3"]) result.should(eq(true)) end it "should return false if a float" do - result = @scope.function_is_integer([3.2]) + result = @scope.function_is_integer(["3.2"]) result.should(eq(false)) end -- cgit v1.2.3 From 7efd6ec5819775226d669c15b58649d375f72959 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Thu, 28 Jul 2011 15:44:26 +0100 Subject: (#1) - added new test for upcase. --- spec/unit/parser/functions/upcase_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/unit/parser/functions/upcase_spec.rb b/spec/unit/parser/functions/upcase_spec.rb index 10e4c8a..5d18846 100644 --- a/spec/unit/parser/functions/upcase_spec.rb +++ b/spec/unit/parser/functions/upcase_spec.rb @@ -23,4 +23,9 @@ describe "the upcase function" do result.should(eq('ABC')) end + it "should do nothing if a string is already upcase" do + result = @scope.function_upcase(["ABC"]) + result.should(eq('ABC')) + end + end -- cgit v1.2.3 From 635ed82e5cae38b0ba82098c340cbeed70d483c3 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Thu, 28 Jul 2011 18:10:16 +0100 Subject: (#2) - unstubbed is_valid_ip_address --- lib/puppet/parser/functions/is_valid_ip_address.rb | 13 +++++++++++++ .../parser/functions/is_valid_ip_address_spec.rb | 20 +++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/puppet/parser/functions/is_valid_ip_address.rb b/lib/puppet/parser/functions/is_valid_ip_address.rb index e91dda8..4f45890 100644 --- a/lib/puppet/parser/functions/is_valid_ip_address.rb +++ b/lib/puppet/parser/functions/is_valid_ip_address.rb @@ -7,11 +7,24 @@ module Puppet::Parser::Functions EOS ) do |arguments| + require 'ipaddr' + if (arguments.size != 1) then raise(Puppet::ParseError, "is_valid_ip_address(): Wrong number of arguments "+ "given #{arguments.size} for 1") end + begin + ip = IPAddr.new(arguments[0]) + rescue ArgumentError + return false + end + + if ip.ipv4? or ip.ipv6? then + return true + else + return false + end end end diff --git a/spec/unit/parser/functions/is_valid_ip_address_spec.rb b/spec/unit/parser/functions/is_valid_ip_address_spec.rb index ee53ee1..2883aaa 100644 --- a/spec/unit/parser/functions/is_valid_ip_address_spec.rb +++ b/spec/unit/parser/functions/is_valid_ip_address_spec.rb @@ -18,14 +18,28 @@ describe "the is_valid_ip_address function" do lambda { @scope.function_is_valid_ip_address([]) }.should( raise_error(Puppet::ParseError)) end - it "should return true if an IP address" do - result = @scope.function_is_valid_ip_address("1.2.3.4") + it "should return true if an IPv4 address" do + result = @scope.function_is_valid_ip_address(["1.2.3.4"]) + result.should(eq(true)) + end + + it "should return true if a full IPv6 address" do + result = @scope.function_is_valid_ip_address(["fe80:0000:cd12:d123:e2f8:47ff:fe09:dd74"]) + result.should(eq(true)) + end + + it "should return true if a compressed IPv6 address" do + result = @scope.function_is_valid_ip_address(["fe00::1"]) result.should(eq(true)) end it "should return false if not valid" do - result = @scope.function_is_valid_ip_address("asdf") + result = @scope.function_is_valid_ip_address(["asdf"]) result.should(eq(false)) end + it "should return false if IP octets out of range" do + result = @scope.function_is_valid_ip_address(["1.1.1.300"]) + result.should(eq(false)) + end end -- cgit v1.2.3 From 313df566bf5cfcbef73fc8182ccb07ddf2f13feb Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Thu, 28 Jul 2011 21:03:33 +0100 Subject: (#2) unstub is_numeric function. --- lib/puppet/parser/functions/is_numeric.rb | 8 ++++++++ spec/unit/parser/functions/is_numeric_spec.rb | 17 ++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/puppet/parser/functions/is_numeric.rb b/lib/puppet/parser/functions/is_numeric.rb index 96e8674..c2c0fb5 100644 --- a/lib/puppet/parser/functions/is_numeric.rb +++ b/lib/puppet/parser/functions/is_numeric.rb @@ -12,6 +12,14 @@ module Puppet::Parser::Functions "given #{arguments.size} for 1") end + value = arguments[0] + + if value == value.to_f.to_s or value == value.to_i.to_s then + return true + else + return false + end + end end diff --git a/spec/unit/parser/functions/is_numeric_spec.rb b/spec/unit/parser/functions/is_numeric_spec.rb index 3a49f5d..2191b7b 100644 --- a/spec/unit/parser/functions/is_numeric_spec.rb +++ b/spec/unit/parser/functions/is_numeric_spec.rb @@ -14,8 +14,23 @@ describe "the is_numeric function" do Puppet::Parser::Functions.function("is_numeric").should == "function_is_numeric" end - it "should raise a ParseError if there is less than 1 arguments" do + it "should raise a ParseError if there is less than 1 argument" do lambda { @scope.function_is_numeric([]) }.should( raise_error(Puppet::ParseError)) end + it "should return true if an integer" do + result = @scope.function_is_numeric(["3"]) + result.should(eq(true)) + end + + it "should return true if a float" do + result = @scope.function_is_numeric(["3.2"]) + result.should(eq(true)) + end + + it "should return false if a string" do + result = @scope.function_is_numeric(["asdf"]) + result.should(eq(false)) + end + end -- cgit v1.2.3 From 1a7bd1ae8321bfc3d2ae87f1d5184828abf56916 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Thu, 28 Jul 2011 21:10:33 +0100 Subject: Remove is_valid_netmask instead of unstubbing. Doesn't seem like a sensible function on its own. --- lib/puppet/parser/functions/is_valid_netmask.rb | 18 ------------------ spec/unit/parser/functions/is_valid_netmask_spec.rb | 21 --------------------- 2 files changed, 39 deletions(-) delete mode 100644 lib/puppet/parser/functions/is_valid_netmask.rb delete mode 100644 spec/unit/parser/functions/is_valid_netmask_spec.rb diff --git a/lib/puppet/parser/functions/is_valid_netmask.rb b/lib/puppet/parser/functions/is_valid_netmask.rb deleted file mode 100644 index 41e4843..0000000 --- a/lib/puppet/parser/functions/is_valid_netmask.rb +++ /dev/null @@ -1,18 +0,0 @@ -# -# is_valid_netmask.rb -# - -module Puppet::Parser::Functions - newfunction(:is_valid_netmask, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - if (arguments.size != 1) then - raise(Puppet::ParseError, "is_valid_netmask(): Wrong number of arguments "+ - "given #{arguments.size} for 1") - end - - end -end - -# vim: set ts=2 sw=2 et : diff --git a/spec/unit/parser/functions/is_valid_netmask_spec.rb b/spec/unit/parser/functions/is_valid_netmask_spec.rb deleted file mode 100644 index 97cbb7c..0000000 --- a/spec/unit/parser/functions/is_valid_netmask_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the is_valid_netmask function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("is_valid_netmask").should == "function_is_valid_netmask" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_is_valid_netmask([]) }.should( raise_error(Puppet::ParseError)) - end - -end -- cgit v1.2.3 From a47853502d7681edd8173e05249ce43d44bede7c Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Thu, 28 Jul 2011 21:15:43 +0100 Subject: Removed load_variables. load_yaml is sufficient to solve this problem on its own. --- lib/puppet/parser/functions/load_variables.rb | 77 ----------------------- spec/unit/parser/functions/load_variables_spec.rb | 21 ------- 2 files changed, 98 deletions(-) delete mode 100644 lib/puppet/parser/functions/load_variables.rb delete mode 100644 spec/unit/parser/functions/load_variables_spec.rb diff --git a/lib/puppet/parser/functions/load_variables.rb b/lib/puppet/parser/functions/load_variables.rb deleted file mode 100644 index a28c64b..0000000 --- a/lib/puppet/parser/functions/load_variables.rb +++ /dev/null @@ -1,77 +0,0 @@ -# -# load_variables.rb -# - -module Puppet::Parser::Functions - newfunction(:load_variables, :type => :statement, :doc => <<-EOS -This function will allow for loading variables from an external YAML -file and expose them for further use inside the Puppet manifest file ... - -For example: - -Given following content of the data.yaml file: - - --- - host1.example.com: - foo: bar - baz: quux - question: 42 - host2.example.com: - abc: def - this: that - darth: vader - -Then calling load_variables in Puppet manifest file as follows: - - load_variables("/etc/puppet/data.yaml", $fqdn) - -Will result in addition of variables $foo, $baz and $question -for matching host name as per the variable $fqdn ... - -Another example which uses per-host file: - -Given following content of the file data-host1.example.com.yaml: - - --- - foo: bar - -Then when we call load_variables like this: - - load_variables("/etc/puppet/data-${fqdn}.yaml") - -This will result in a variable $foo being added and ready for use. - EOS - ) do |arguments| - - raise(Puppet::ParseError, "load_variables(): Wrong number of " + - "arguments given (#{arguments.size} for 2)") if arguments.size < 2 - - data = {} - - file = arguments[0] - key = arguments[1] if arguments[1] - - if File.exists?(file) - - begin - data = YAML.load_file(file) - rescue => error - raise(Puppet::ParseError, "load_variables(): Unable to load data " + - "from the file `%s': %s" % file, error.to_s) - end - - raise(Puppet::ParseError, "load_variables(): Data in the file `%s' " + - "is not a hash" % file) unless data.is_a?(Hash) - - data = ((data[key] and data[key].is_a?(Hash)) ? data[key] : {}) if key - end - - data.each do |param, value| - value = strinterp(value) # Evaluate any interpolated variable names ... - - setvar(param, value) - end - end -end - -# vim: set ts=2 sw=2 et : diff --git a/spec/unit/parser/functions/load_variables_spec.rb b/spec/unit/parser/functions/load_variables_spec.rb deleted file mode 100644 index dc29e61..0000000 --- a/spec/unit/parser/functions/load_variables_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the load_variables function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("load_variables").should == "function_load_variables" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_load_variables([]) }.should( raise_error(Puppet::ParseError)) - end - -end -- cgit v1.2.3 From 4915eff575801e73ab15a77b500eb2e0d42b579c Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Thu, 28 Jul 2011 21:23:53 +0100 Subject: Removed crontab functions instead of unstubbing them. --- .../parser/functions/persistent_crontab_minutes.rb | 63 ---------------------- .../parser/functions/random_crontab_minutes.rb | 31 ----------- .../functions/persistent_crontab_minutes_spec.rb | 21 -------- .../functions/random_crontab_minutes_spec.rb | 21 -------- 4 files changed, 136 deletions(-) delete mode 100644 lib/puppet/parser/functions/persistent_crontab_minutes.rb delete mode 100644 lib/puppet/parser/functions/random_crontab_minutes.rb delete mode 100644 spec/unit/parser/functions/persistent_crontab_minutes_spec.rb delete mode 100644 spec/unit/parser/functions/random_crontab_minutes_spec.rb diff --git a/lib/puppet/parser/functions/persistent_crontab_minutes.rb b/lib/puppet/parser/functions/persistent_crontab_minutes.rb deleted file mode 100644 index cd80094..0000000 --- a/lib/puppet/parser/functions/persistent_crontab_minutes.rb +++ /dev/null @@ -1,63 +0,0 @@ -# -# persistent_crontab_minutes.rb -# - -module Puppet::Parser::Functions - newfunction(:persistent_crontab_minutes, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "Wrong number of arguments " + - "given (#{arguments.size} for 2)") if arguments.size < 2 - - require 'md5' - - value = 0 - - job = arguments[0] - host = arguments[1] - - environment = Puppet[:environment] - - # We select first directory that exists. This might not be the best idea ... - modules = Puppet[:modulepath].split(':').select { |i| File.exists?(i) }.first - - raise(Puppet::ParseError, "Unable to determine the storage " + - "directory for Puppet modules") unless modules - - # Prepare the file where we store current value ... - file = "/puppet/state/crontab/#{host}-#{job}.minutes" - file = File.join(modules, file) - - # Get the directory portion from the file name ... - directory = File.dirname(file) - - FileUtils.mkdir_p(directory) unless File.directory?(directory) - - if FileTest.exists?(file) - File.open(file, 'r') { |f| value = f.read.to_i } - - raise(Puppet::ParseError, "The value for minutes in the file `%s' " + - "is out of the range from 0 to 59 inclusive") unless value < 60 - else - # - # Pick a random number based on the job and host name. This will yield - # the same value for exactly the same combination of the job and host name. - # - value = MD5.new(job_name + host).to_s.hex % 60 - - # Minutes are from 0 to 59 inclusive ... - value = value < 60 ? value : 59 - - File.open(file, 'w') { |f| f.write(value) } - end - - # Tell Puppet to keep an eye on this file ... - parser = Puppet::Parser::Parser.new(environment) - parser.watch_file(file) if File.exists?(file) - - return value - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/random_crontab_minutes.rb b/lib/puppet/parser/functions/random_crontab_minutes.rb deleted file mode 100644 index 8ab29e1..0000000 --- a/lib/puppet/parser/functions/random_crontab_minutes.rb +++ /dev/null @@ -1,31 +0,0 @@ -# -# random_crontab_minutes.rb -# - -module Puppet::Parser::Functions - newfunction(:random_crontab_minutes, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - raise(Puppet::ParseError, "Wrong number of arguments " + - "given (#{arguments.size} for 2)") if arguments.size < 2 - - require 'md5' - - job_name = arguments[0] - host = arguments[1] - - # - # Pick a random number based on the job and host name. This will yield - # the same value for exactly the same combination of the job and host name. - # - value = MD5.new(job_name + host).to_s.hex % 60 - - # Minutes are from 0 to 59 inclusive ... - value = value < 60 ? value : 59 - - return value - end -end - -# vim: set ts=2 sw=2 et : diff --git a/spec/unit/parser/functions/persistent_crontab_minutes_spec.rb b/spec/unit/parser/functions/persistent_crontab_minutes_spec.rb deleted file mode 100644 index 1d8cbe7..0000000 --- a/spec/unit/parser/functions/persistent_crontab_minutes_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the persistent_crontab_minutes function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("persistent_crontab_minutes").should == "function_persistent_crontab_minutes" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_persistent_crontab_minutes([]) }.should( raise_error(Puppet::ParseError)) - end - -end diff --git a/spec/unit/parser/functions/random_crontab_minutes_spec.rb b/spec/unit/parser/functions/random_crontab_minutes_spec.rb deleted file mode 100644 index b47b3ae..0000000 --- a/spec/unit/parser/functions/random_crontab_minutes_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the random_crontab_minutes function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("random_crontab_minutes").should == "function_random_crontab_minutes" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_random_crontab_minutes([]) }.should( raise_error(Puppet::ParseError)) - end - -end -- cgit v1.2.3 From 7d6ae5d57ce45ff1293f02b90c816a7f938a3af6 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Thu, 28 Jul 2011 21:30:02 +0100 Subject: Count functionality overlaps with size - so removing it. --- lib/puppet/parser/functions/count.rb | 36 -------------------------------- spec/unit/parser/functions/count_spec.rb | 26 ----------------------- 2 files changed, 62 deletions(-) delete mode 100644 lib/puppet/parser/functions/count.rb delete mode 100755 spec/unit/parser/functions/count_spec.rb diff --git a/lib/puppet/parser/functions/count.rb b/lib/puppet/parser/functions/count.rb deleted file mode 100644 index c4e2283..0000000 --- a/lib/puppet/parser/functions/count.rb +++ /dev/null @@ -1,36 +0,0 @@ -# -# count.rb -# - -# TODO(Krzysztof Wilczynski): We need to add support for regular expression ... -# TODO(Krzysztof Wilczynski): Support for hash values would be nice too ... - -module Puppet::Parser::Functions - newfunction(:count, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - # Technically we support two arguments but only first is mandatory ... - raise(Puppet::ParseError, "count(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - value = arguments[0] - klass = value.class - - unless [Array, Hash, String].include?(klass) - raise(Puppet::ParseError, 'count(): Requires either ' + - 'array, hash or string to work with') - end - - item = arguments[1] if arguments[1] - - value = value.is_a?(Hash) ? value.keys : value - - # No item to look for and count? Then just return current size ... - result = item ? value.count(item) : value.size - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/spec/unit/parser/functions/count_spec.rb b/spec/unit/parser/functions/count_spec.rb deleted file mode 100755 index 62d005a..0000000 --- a/spec/unit/parser/functions/count_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the count function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("count").should == "function_count" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_count([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return the size of an array" do - result = @scope.function_count([['a','c','b']]) - result.should(eq(3)) - end - -end -- cgit v1.2.3 From ce48eb6e7a76f74e1f61c76ac3b185031c40772b Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 00:10:31 +0100 Subject: Allow sort for strings. --- lib/puppet/parser/functions/sort.rb | 8 +++++++- spec/unit/parser/functions/sort_spec.rb | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/puppet/parser/functions/sort.rb b/lib/puppet/parser/functions/sort.rb index 54a4018..49ca20a 100644 --- a/lib/puppet/parser/functions/sort.rb +++ b/lib/puppet/parser/functions/sort.rb @@ -12,7 +12,13 @@ module Puppet::Parser::Functions "given #{arguments.size} for 1") end - arguments[0].sort + value = arguments[0] + + if value.is_a?(Array) then + value.sort + elsif value.is_a?(String) then + value.split("").sort.to_s + end end end diff --git a/spec/unit/parser/functions/sort_spec.rb b/spec/unit/parser/functions/sort_spec.rb index da5db7a..fbe3073 100644 --- a/spec/unit/parser/functions/sort_spec.rb +++ b/spec/unit/parser/functions/sort_spec.rb @@ -23,4 +23,9 @@ describe "the sort function" do result.should(eq(['a','b','c'])) end + it "should sort a string" do + result = @scope.function_sort(["acb"]) + result.should(eq('abc')) + end + end -- cgit v1.2.3 From 4080c0534e76cf68b935344c75b2382f0184a226 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 17:55:45 +0100 Subject: (#2) unstub is_valid_mac_address. --- lib/puppet/parser/functions/is_valid_mac_address.rb | 8 ++++++++ spec/unit/parser/functions/is_valid_mac_address_spec.rb | 9 +++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/puppet/parser/functions/is_valid_mac_address.rb b/lib/puppet/parser/functions/is_valid_mac_address.rb index 0b91d0d..a00d874 100644 --- a/lib/puppet/parser/functions/is_valid_mac_address.rb +++ b/lib/puppet/parser/functions/is_valid_mac_address.rb @@ -12,6 +12,14 @@ module Puppet::Parser::Functions "given #{arguments.size} for 1") end + mac = arguments[0] + + if /^[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}$/.match(mac) then + return true + else + return false + end + end end diff --git a/spec/unit/parser/functions/is_valid_mac_address_spec.rb b/spec/unit/parser/functions/is_valid_mac_address_spec.rb index c4eb468..62e6fee 100644 --- a/spec/unit/parser/functions/is_valid_mac_address_spec.rb +++ b/spec/unit/parser/functions/is_valid_mac_address_spec.rb @@ -19,12 +19,17 @@ describe "the is_valid_mac_address function" do end it "should return true if a valid mac address" do - result = @scope.function_is_valid_mac_address("00:a0:1f:12:7f:a0") + result = @scope.function_is_valid_mac_address(["00:a0:1f:12:7f:a0"]) result.should(eq(true)) end + it "should return false if octets are out of range" do + result = @scope.function_is_valid_mac_address(["00:a0:1f:12:7f:g0"]) + result.should(eq(false)) + end + it "should return false if not valid" do - result = @scope.function_is_valid_mac_address("not valid") + result = @scope.function_is_valid_mac_address(["not valid"]) result.should(eq(false)) end -- cgit v1.2.3 From db7e06e301b689efcd421fd56fb1c23e3595e173 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 18:00:32 +0100 Subject: Removed join_with_prefix. --- lib/puppet/parser/functions/join_with_prefix.rb | 38 ---------------------- .../unit/parser/functions/join_with_prefix_spec.rb | 26 --------------- 2 files changed, 64 deletions(-) delete mode 100644 lib/puppet/parser/functions/join_with_prefix.rb delete mode 100644 spec/unit/parser/functions/join_with_prefix_spec.rb diff --git a/lib/puppet/parser/functions/join_with_prefix.rb b/lib/puppet/parser/functions/join_with_prefix.rb deleted file mode 100644 index 8bf96d9..0000000 --- a/lib/puppet/parser/functions/join_with_prefix.rb +++ /dev/null @@ -1,38 +0,0 @@ -# -# join_with_prefix.rb -# - -module Puppet::Parser::Functions - newfunction(:join_with_prefix, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - # Technically we support three arguments but only first is mandatory ... - raise(Puppet::ParseError, "join(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - array = arguments[0] - - unless array.is_a?(Array) - raise(Puppet::ParseError, 'join_with_prefix(): Requires ' + - 'array to work with') - end - - prefix = arguments[1] if arguments[1] - suffix = arguments[2] if arguments[2] - - if prefix and suffix - result = prefix + array.join(suffix + prefix) - elsif prefix and not suffix - result = array.collect { |i| prefix ? prefix + i : i } - elsif suffix and not prefix - result = array.join(suffix) - else - result = array.join - end - - return result - end -end - -# vim: set ts=2 sw=2 et : diff --git a/spec/unit/parser/functions/join_with_prefix_spec.rb b/spec/unit/parser/functions/join_with_prefix_spec.rb deleted file mode 100644 index 70e6965..0000000 --- a/spec/unit/parser/functions/join_with_prefix_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the join_with_prefix function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("join_with_prefix").should == "function_join_with_prefix" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_join_with_prefix([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should join an array into a string" do - result = @scope.function_join_with_prefix([["a","b","c"], ":", "p"]) - result.should(eq("pa:pb:pc")) - end - -end -- cgit v1.2.3 From 62520a2df03acde975d8d7bd96805e767a9611f4 Mon Sep 17 00:00:00 2001 From: James Turnbull Date: Sat, 30 Jul 2011 04:22:30 +1000 Subject: Added doc strings for first five functions --- lib/puppet/parser/functions/abs.rb | 2 ++ lib/puppet/parser/functions/bool2num.rb | 4 ++++ lib/puppet/parser/functions/capitalize.rb | 2 ++ lib/puppet/parser/functions/chomp.rb | 5 ++++- lib/puppet/parser/functions/chop.rb | 7 ++++++- 5 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/puppet/parser/functions/abs.rb b/lib/puppet/parser/functions/abs.rb index 0a554e4..ade5462 100644 --- a/lib/puppet/parser/functions/abs.rb +++ b/lib/puppet/parser/functions/abs.rb @@ -4,6 +4,8 @@ module Puppet::Parser::Functions newfunction(:abs, :type => :rvalue, :doc => <<-EOS + Returns the absolute value of a number, for example -34.56 becomes + 34.56. Takes a single integer and float value as an argument. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/bool2num.rb b/lib/puppet/parser/functions/bool2num.rb index b2989d0..9a07a8a 100644 --- a/lib/puppet/parser/functions/bool2num.rb +++ b/lib/puppet/parser/functions/bool2num.rb @@ -4,6 +4,10 @@ module Puppet::Parser::Functions newfunction(:bool2num, :type => :rvalue, :doc => <<-EOS + Converts a boolean to a number. Converts the values: + false, f, 0, n, and no to 0 + true, t, 1, y, and yes to 1 + Requires a single boolean or string as an input. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/capitalize.rb b/lib/puppet/parser/functions/capitalize.rb index f902cb3..640d00b 100644 --- a/lib/puppet/parser/functions/capitalize.rb +++ b/lib/puppet/parser/functions/capitalize.rb @@ -4,6 +4,8 @@ module Puppet::Parser::Functions newfunction(:capitalize, :type => :rvalue, :doc => <<-EOS + Capitalizes the first letter of a string or array of strings. + Requires either a single string or an array as an input. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/chomp.rb b/lib/puppet/parser/functions/chomp.rb index e1d788a..c99d139 100644 --- a/lib/puppet/parser/functions/chomp.rb +++ b/lib/puppet/parser/functions/chomp.rb @@ -3,7 +3,10 @@ # module Puppet::Parser::Functions - newfunction(:chomp, :type => :rvalue, :doc => <<-EOS + newfunction(:chomp, :type => :rvalue, :doc => <<-'EOS' + Removes the record separator from the end of a string or an array of + strings, for example `hello\n` becomes `hello`. + Requires a single string or array as an input. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/chop.rb b/lib/puppet/parser/functions/chop.rb index 0f9af86..636b990 100644 --- a/lib/puppet/parser/functions/chop.rb +++ b/lib/puppet/parser/functions/chop.rb @@ -3,7 +3,12 @@ # module Puppet::Parser::Functions - newfunction(:chop, :type => :rvalue, :doc => <<-EOS + newfunction(:chop, :type => :rvalue, :doc => <<-'EOS' + Returns a new string with the last character removed. If the string ends + with `\r\n`, both characters are removed. Applying chop to an empty + string returns an empty string. If you wish to merely remove record + separators then you should use the `chomp` function. + Requires a string or array of strings as input. EOS ) do |arguments| -- cgit v1.2.3 From 56a402e6542105ec63c5071e685d5af93fd4d0f3 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 20:08:31 +0100 Subject: (#2) unstub is_valid_domain_name --- .../parser/functions/is_valid_domain_name.rb | 8 +++++ .../parser/functions/is_valid_domain_name_spec.rb | 35 ++++++++++++++++++---- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/lib/puppet/parser/functions/is_valid_domain_name.rb b/lib/puppet/parser/functions/is_valid_domain_name.rb index c0b319c..99d6f86 100644 --- a/lib/puppet/parser/functions/is_valid_domain_name.rb +++ b/lib/puppet/parser/functions/is_valid_domain_name.rb @@ -12,6 +12,14 @@ module Puppet::Parser::Functions "given #{arguments.size} for 1") end + domain = arguments[0] + + if domain =~ /^(([a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])\.?$/ then + return true + else + return false + end + end end diff --git a/spec/unit/parser/functions/is_valid_domain_name_spec.rb b/spec/unit/parser/functions/is_valid_domain_name_spec.rb index f03f6e8..3339447 100644 --- a/spec/unit/parser/functions/is_valid_domain_name_spec.rb +++ b/spec/unit/parser/functions/is_valid_domain_name_spec.rb @@ -19,13 +19,38 @@ describe "the is_valid_domain_name function" do end it "should return true if a valid domain name" do - result = @scope.function_is_valid_domain_name("foo.bar.com") - result.should(eq(true)) + result = @scope.function_is_valid_domain_name(["foo.bar.com"]) + result.should(be_true) end - it "should return false if not a valid domain name" do - result = @scope.function_is_valid_domain_name("not valid") - result.should(eq(false)) + it "should allow domain parts to start with numbers" do + result = @scope.function_is_valid_domain_name(["3foo.2bar.com"]) + result.should(be_true) + end + + it "should allow domain to end with a dot" do + result = @scope.function_is_valid_domain_name(["3foo.2bar.com."]) + result.should(be_true) + end + + it "should allow a single part domain" do + result = @scope.function_is_valid_domain_name(["orange"]) + result.should(be_true) + end + + it "should return false if domain parts start with hyphens" do + result = @scope.function_is_valid_domain_name(["-3foo.2bar.com"]) + result.should(be_false) + end + + it "should return true if domain contains hyphens" do + result = @scope.function_is_valid_domain_name(["3foo-bar.2bar-fuzz.com"]) + result.should(be_true) + end + + it "should return false if domain name contains spaces" do + result = @scope.function_is_valid_domain_name(["not valid"]) + result.should(be_false) end end -- cgit v1.2.3 From 18e5302614bce168ac07e35dc23b058064e9e41c Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 20:11:47 +0100 Subject: (#2) fix is_string finally so it also makes sure numbers return false. --- lib/puppet/parser/functions/is_string.rb | 4 ++++ spec/unit/parser/functions/is_string_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/puppet/parser/functions/is_string.rb b/lib/puppet/parser/functions/is_string.rb index 61037e3..8a02a10 100644 --- a/lib/puppet/parser/functions/is_string.rb +++ b/lib/puppet/parser/functions/is_string.rb @@ -14,6 +14,10 @@ module Puppet::Parser::Functions result = type.is_a?(String) + if result and (type == type.to_f.to_s or type == type.to_i.to_s) then + return false + end + return result end end diff --git a/spec/unit/parser/functions/is_string_spec.rb b/spec/unit/parser/functions/is_string_spec.rb index 8c0061e..4f3f5fd 100644 --- a/spec/unit/parser/functions/is_string_spec.rb +++ b/spec/unit/parser/functions/is_string_spec.rb @@ -18,4 +18,24 @@ describe "the is_string function" do lambda { @scope.function_is_string([]) }.should( raise_error(Puppet::ParseError)) end + it "should return true if a string" do + result = @scope.function_is_string(["asdf"]) + result.should(eq(true)) + end + + it "should return false if an integer" do + result = @scope.function_is_string(["3"]) + result.should(eq(false)) + end + + it "should return false if a float" do + result = @scope.function_is_string(["3.23"]) + result.should(eq(false)) + end + + it "should return false if an array" do + result = @scope.function_is_string([["a","b","c"]]) + result.should(eq(false)) + end + end -- cgit v1.2.3 From aa023c1e5d23b2a5a66dbe9a36d0e8765a8d8cff Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 20:56:40 +0100 Subject: Removed date stub since this functinality is available in strftime anyway. --- lib/puppet/parser/functions/date.rb | 20 -------------------- spec/unit/parser/functions/date_spec.rb | 21 --------------------- 2 files changed, 41 deletions(-) delete mode 100644 lib/puppet/parser/functions/date.rb delete mode 100755 spec/unit/parser/functions/date_spec.rb diff --git a/lib/puppet/parser/functions/date.rb b/lib/puppet/parser/functions/date.rb deleted file mode 100644 index 0439bd1..0000000 --- a/lib/puppet/parser/functions/date.rb +++ /dev/null @@ -1,20 +0,0 @@ -# -# date.rb -# - -module Puppet::Parser::Functions - newfunction(:date, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - if (arguments.size != 1) then - raise(Puppet::ParseError, "date(): Wrong number of arguments "+ - "given #{arguments.size} for 1") - end - - # TODO: stubbed - - end -end - -# vim: set ts=2 sw=2 et : diff --git a/spec/unit/parser/functions/date_spec.rb b/spec/unit/parser/functions/date_spec.rb deleted file mode 100755 index dcba4af..0000000 --- a/spec/unit/parser/functions/date_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the date function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("date").should == "function_date" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_date([]) }.should( raise_error(Puppet::ParseError)) - end - -end -- cgit v1.2.3 From 6827ad804f2eb44839f9a3e99f40f1bb2284f491 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 20:57:10 +0100 Subject: (#1) provide some more detailed tests for a number of functions. --- spec/unit/parser/functions/abs_spec.rb | 7 ++++++- spec/unit/parser/functions/downcase_spec.rb | 5 +++++ spec/unit/parser/functions/empty_spec.rb | 5 +++++ spec/unit/parser/functions/flatten_spec.rb | 5 +++++ spec/unit/parser/functions/is_array_spec.rb | 5 +++++ spec/unit/parser/functions/is_float_spec.rb | 2 +- spec/unit/parser/functions/num2bool_spec.rb | 7 ++++++- spec/unit/parser/functions/shuffle_spec.rb | 10 ++++++++++ spec/unit/parser/functions/strftime_spec.rb | 15 +++++++++++++++ spec/unit/parser/functions/time_spec.rb | 15 +++++++++++++++ spec/unit/parser/functions/type_spec.rb | 12 +++++++++++- 11 files changed, 84 insertions(+), 4 deletions(-) diff --git a/spec/unit/parser/functions/abs_spec.rb b/spec/unit/parser/functions/abs_spec.rb index 6bf0b41..65ba2e8 100755 --- a/spec/unit/parser/functions/abs_spec.rb +++ b/spec/unit/parser/functions/abs_spec.rb @@ -19,8 +19,13 @@ describe "the abs function" do end it "should convert a negative number into a positive" do - result = @scope.function_abs([-34]) + result = @scope.function_abs(["-34"]) result.should(eq(34)) end + it "should do nothing with a positive number" do + result = @scope.function_abs(["5678"]) + result.should(eq(5678)) + end + end diff --git a/spec/unit/parser/functions/downcase_spec.rb b/spec/unit/parser/functions/downcase_spec.rb index 0d53220..0bccd5f 100755 --- a/spec/unit/parser/functions/downcase_spec.rb +++ b/spec/unit/parser/functions/downcase_spec.rb @@ -23,4 +23,9 @@ describe "the downcase function" do result.should(eq("asfd")) end + it "should do nothing to a string that is already downcase" do + result = @scope.function_downcase(["asdf asdf"]) + result.should(eq("asdf asdf")) + end + end diff --git a/spec/unit/parser/functions/empty_spec.rb b/spec/unit/parser/functions/empty_spec.rb index 6c0fe69..cb0021f 100755 --- a/spec/unit/parser/functions/empty_spec.rb +++ b/spec/unit/parser/functions/empty_spec.rb @@ -23,4 +23,9 @@ describe "the empty function" do result.should(eq(true)) end + it "should return a false for a non-empty string" do + result = @scope.function_empty(['asdf']) + result.should(eq(false)) + end + end diff --git a/spec/unit/parser/functions/flatten_spec.rb b/spec/unit/parser/functions/flatten_spec.rb index 91cf4ef..7bedeb2 100755 --- a/spec/unit/parser/functions/flatten_spec.rb +++ b/spec/unit/parser/functions/flatten_spec.rb @@ -23,4 +23,9 @@ describe "the flatten function" do result.should(eq(["a","b","c","d","e","f","g"])) end + it "should do nothing to a structure that is already flat" do + result = @scope.function_flatten([["a","b","c","d"]]) + result.should(eq(["a","b","c","d"])) + end + end diff --git a/spec/unit/parser/functions/is_array_spec.rb b/spec/unit/parser/functions/is_array_spec.rb index 15b563c..537595c 100644 --- a/spec/unit/parser/functions/is_array_spec.rb +++ b/spec/unit/parser/functions/is_array_spec.rb @@ -28,4 +28,9 @@ describe "the is_array function" do result.should(eq(false)) end + it "should return false if passed a string" do + result = @scope.function_is_array(["asdf"]) + result.should(eq(false)) + end + end diff --git a/spec/unit/parser/functions/is_float_spec.rb b/spec/unit/parser/functions/is_float_spec.rb index 35c0dd6..55ba8cf 100644 --- a/spec/unit/parser/functions/is_float_spec.rb +++ b/spec/unit/parser/functions/is_float_spec.rb @@ -28,7 +28,7 @@ describe "the is_float function" do result.should(eq(false)) end - it "should return false if not an integer" do + it "should return false if an integer" do result = @scope.function_is_float(["3"]) result.should(eq(false)) end diff --git a/spec/unit/parser/functions/num2bool_spec.rb b/spec/unit/parser/functions/num2bool_spec.rb index 2f6435d..6585273 100644 --- a/spec/unit/parser/functions/num2bool_spec.rb +++ b/spec/unit/parser/functions/num2bool_spec.rb @@ -20,7 +20,12 @@ describe "the num2bool function" do it "should return true if 1" do result = @scope.function_num2bool(["1"]) - result.should(eq(true)) + result.should(be_true) + end + + it "should return false if 0" do + result = @scope.function_num2bool(["0"]) + result.should(be_false) end end diff --git a/spec/unit/parser/functions/shuffle_spec.rb b/spec/unit/parser/functions/shuffle_spec.rb index cf063c6..936c2fd 100644 --- a/spec/unit/parser/functions/shuffle_spec.rb +++ b/spec/unit/parser/functions/shuffle_spec.rb @@ -18,4 +18,14 @@ describe "the shuffle function" do lambda { @scope.function_shuffle([]) }.should( raise_error(Puppet::ParseError)) end + it "should shuffle a string and the result should be the same size" do + result = @scope.function_shuffle(["asdf"]) + result.size.should(eq(4)) + end + + it "should shuffle a string but the sorted contents should still be the same" do + result = @scope.function_shuffle(["adfs"]) + result.split("").sort.to_s.should(eq("adfs")) + end + end diff --git a/spec/unit/parser/functions/strftime_spec.rb b/spec/unit/parser/functions/strftime_spec.rb index e377954..f7a2cd9 100644 --- a/spec/unit/parser/functions/strftime_spec.rb +++ b/spec/unit/parser/functions/strftime_spec.rb @@ -18,4 +18,19 @@ describe "the strftime function" do lambda { @scope.function_strftime([]) }.should( raise_error(Puppet::ParseError)) end + it "using %s should be higher then when I wrote this test" do + result = @scope.function_strftime(["%s"]) + result.to_i.should(be > 1311953157) + end + + it "using %s should be lower then 1.5 trillion" do + result = @scope.function_strftime(["%s"]) + result.to_i.should(be < 1500000000) + end + + it "should return a date when given %Y-%m-%d" do + result = @scope.function_strftime(["%Y-%m-%d"]) + result.should =~ /^\d{4}-\d{2}-\d{2}$/ + end + end diff --git a/spec/unit/parser/functions/time_spec.rb b/spec/unit/parser/functions/time_spec.rb index 8bf5982..666e8e0 100644 --- a/spec/unit/parser/functions/time_spec.rb +++ b/spec/unit/parser/functions/time_spec.rb @@ -18,4 +18,19 @@ describe "the time function" do lambda { @scope.function_time(['','']) }.should( raise_error(Puppet::ParseError)) end + it "should return a number" do + result = @scope.function_time([]) + result.class.should(eq(Fixnum)) + end + + it "should be higher then when I wrote this test" do + result = @scope.function_time([]) + result.should(be > 1311953157) + end + + it "should be lower then 1.5 trillion" do + result = @scope.function_time([]) + result.should(be < 1500000000) + end + end diff --git a/spec/unit/parser/functions/type_spec.rb b/spec/unit/parser/functions/type_spec.rb index fddb87d..36c3823 100644 --- a/spec/unit/parser/functions/type_spec.rb +++ b/spec/unit/parser/functions/type_spec.rb @@ -18,9 +18,19 @@ describe "the type function" do lambda { @scope.function_type([]) }.should( raise_error(Puppet::ParseError)) end - it "should return a type when given a string" do + it "should return String when given a string" do result = @scope.function_type(["aaabbbbcccc"]) result.should(eq('String')) end + it "should return Array when given an array" do + result = @scope.function_type([["aaabbbbcccc","asdf"]]) + result.should(eq('Array')) + end + + it "should return Hash when given a hash" do + result = @scope.function_type([{"a"=>1,"b"=>2}]) + result.should(eq('Hash')) + end + end -- cgit v1.2.3 From 284843bd968acd3c361907aadc7921e1eda94822 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 21:17:19 +0100 Subject: Some improvements to values_at tests. --- spec/unit/parser/functions/values_at_spec.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/spec/unit/parser/functions/values_at_spec.rb b/spec/unit/parser/functions/values_at_spec.rb index ec8730b..6c45316 100644 --- a/spec/unit/parser/functions/values_at_spec.rb +++ b/spec/unit/parser/functions/values_at_spec.rb @@ -18,9 +18,28 @@ describe "the values_at function" do lambda { @scope.function_values_at([]) }.should( raise_error(Puppet::ParseError)) end + it "should raise a ParseError if you try to use a range where stop is greater then start" do + lambda { @scope.function_values_at([['a','b'],["3-1"]]) }.should( raise_error(Puppet::ParseError)) + end + it "should return a value at from an array" do result = @scope.function_values_at([['a','b','c'],"1"]) result.should(eq(['b'])) end + it "should return a value at from an array when passed a range" do + result = @scope.function_values_at([['a','b','c'],"0-1"]) + result.should(eq(['a','b'])) + end + + it "should return chosen values from an array when passed number of indexes" do + result = @scope.function_values_at([['a','b','c'],["0","2"]]) + result.should(eq(['a','c'])) + end + + it "should return chosen values from an array when passed ranges and multiple indexes" do + result = @scope.function_values_at([['a','b','c','d','e','f','g'],["0","2","4-5"]]) + result.should(eq(['a','c','e','f'])) + end + end -- cgit v1.2.3 From f9634b7f9b03be86e25dc740cdcda754a0d2bf7d Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 22:08:09 +0100 Subject: Remove rand. --- lib/puppet/parser/functions/rand.rb | 18 ------------------ spec/unit/parser/functions/rand_spec.rb | 21 --------------------- 2 files changed, 39 deletions(-) delete mode 100644 lib/puppet/parser/functions/rand.rb delete mode 100644 spec/unit/parser/functions/rand_spec.rb diff --git a/lib/puppet/parser/functions/rand.rb b/lib/puppet/parser/functions/rand.rb deleted file mode 100644 index 6d870dc..0000000 --- a/lib/puppet/parser/functions/rand.rb +++ /dev/null @@ -1,18 +0,0 @@ -# -# rand.rb -# - -module Puppet::Parser::Functions - newfunction(:rand, :type => :rvalue, :doc => <<-EOS - EOS - ) do |arguments| - - if (arguments.size != 0) and (arguments.size != 1) then - raise(Puppet::ParseError, "rand(): Wrong number of arguments "+ - "given #{arguments.size} for 0 or 1") - end - - end -end - -# vim: set ts=2 sw=2 et : diff --git a/spec/unit/parser/functions/rand_spec.rb b/spec/unit/parser/functions/rand_spec.rb deleted file mode 100644 index 818d1c5..0000000 --- a/spec/unit/parser/functions/rand_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the rand function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("rand").should == "function_rand" - end - - it "should raise a ParseError if there is not 0 or 1 arguments" do - lambda { @scope.function_rand(['a','b']) }.should( raise_error(Puppet::ParseError)) - end - -end -- cgit v1.2.3 From 19313b43ea04066a88a0b78e83650ac52785e2e9 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 22:18:56 +0100 Subject: (#3) Apply missing documentation to more functions. --- lib/puppet/parser/functions/num2bool.rb | 2 + lib/puppet/parser/functions/prefix.rb | 7 +++ lib/puppet/parser/functions/range.rb | 12 +++++ lib/puppet/parser/functions/reverse.rb | 1 + lib/puppet/parser/functions/rstrip.rb | 1 + lib/puppet/parser/functions/shuffle.rb | 1 + lib/puppet/parser/functions/size.rb | 1 + lib/puppet/parser/functions/sort.rb | 1 + lib/puppet/parser/functions/squeeze.rb | 1 + lib/puppet/parser/functions/str2bool.rb | 3 ++ lib/puppet/parser/functions/strftime.rb | 63 ++++++++++++++++++++++++ lib/puppet/parser/functions/strip.rb | 8 +++ lib/puppet/parser/functions/swapcase.rb | 7 +++ lib/puppet/parser/functions/time.rb | 7 +++ lib/puppet/parser/functions/type.rb | 19 ++++++- lib/puppet/parser/functions/unique.rb | 17 +++++++ lib/puppet/parser/functions/upcase.rb | 9 ++++ lib/puppet/parser/functions/validate_resource.rb | 5 ++ lib/puppet/parser/functions/values.rb | 14 ++++++ lib/puppet/parser/functions/values_at.rb | 25 ++++++++-- lib/puppet/parser/functions/zip.rb | 9 ++++ spec/unit/parser/functions/type_spec.rb | 22 ++++++--- spec/unit/parser/functions/unique_spec.rb | 9 +++- spec/unit/parser/functions/values_spec.rb | 4 ++ 24 files changed, 235 insertions(+), 13 deletions(-) diff --git a/lib/puppet/parser/functions/num2bool.rb b/lib/puppet/parser/functions/num2bool.rb index 2baef62..874db22 100644 --- a/lib/puppet/parser/functions/num2bool.rb +++ b/lib/puppet/parser/functions/num2bool.rb @@ -6,6 +6,8 @@ module Puppet::Parser::Functions newfunction(:num2bool, :type => :rvalue, :doc => <<-EOS +This function converts a number into a true boolean. Zero becomes false. Numbers +higher then 0 become true. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/prefix.rb b/lib/puppet/parser/functions/prefix.rb index 0e0cee2..4593976 100644 --- a/lib/puppet/parser/functions/prefix.rb +++ b/lib/puppet/parser/functions/prefix.rb @@ -4,6 +4,13 @@ module Puppet::Parser::Functions newfunction(:prefix, :type => :rvalue, :doc => <<-EOS +This function applies a prefix to all elements in an array. + +*Examles:* + + prefix(['a','b','c'], 'p') + +Will return: ['pa','pb','pc'] EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/range.rb b/lib/puppet/parser/functions/range.rb index 6afb50c..6e85422 100644 --- a/lib/puppet/parser/functions/range.rb +++ b/lib/puppet/parser/functions/range.rb @@ -6,6 +6,18 @@ module Puppet::Parser::Functions newfunction(:range, :type => :rvalue, :doc => <<-EOS +When given range in the form of (start, stop) it will extrapolate a range as +an array. + +*Examples:* + + range("0", "9") + +Will return: [0,1,2,3,4,5,6,7,8,9] + + range("a", "c") + +Will return: ["a","b","c"] EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/reverse.rb b/lib/puppet/parser/functions/reverse.rb index 79e9b93..fe04869 100644 --- a/lib/puppet/parser/functions/reverse.rb +++ b/lib/puppet/parser/functions/reverse.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:reverse, :type => :rvalue, :doc => <<-EOS +Reverses the order of a string or array. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/rstrip.rb b/lib/puppet/parser/functions/rstrip.rb index 56849d3..29b0998 100644 --- a/lib/puppet/parser/functions/rstrip.rb +++ b/lib/puppet/parser/functions/rstrip.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:rstrip, :type => :rvalue, :doc => <<-EOS +Strips leading spaces to the right of the string. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/shuffle.rb b/lib/puppet/parser/functions/shuffle.rb index 73e798c..18134ab 100644 --- a/lib/puppet/parser/functions/shuffle.rb +++ b/lib/puppet/parser/functions/shuffle.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:shuffle, :type => :rvalue, :doc => <<-EOS +Randomizes the order of a string or array elements. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/size.rb b/lib/puppet/parser/functions/size.rb index aa4f4ad..cc207e3 100644 --- a/lib/puppet/parser/functions/size.rb +++ b/lib/puppet/parser/functions/size.rb @@ -6,6 +6,7 @@ module Puppet::Parser::Functions newfunction(:size, :type => :rvalue, :doc => <<-EOS +Returns the number of elements in a string or array. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/sort.rb b/lib/puppet/parser/functions/sort.rb index 49ca20a..3785496 100644 --- a/lib/puppet/parser/functions/sort.rb +++ b/lib/puppet/parser/functions/sort.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:sort, :type => :rvalue, :doc => <<-EOS +Sorts strings and arrays lexically. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/squeeze.rb b/lib/puppet/parser/functions/squeeze.rb index 9a1dd20..65c174a 100644 --- a/lib/puppet/parser/functions/squeeze.rb +++ b/lib/puppet/parser/functions/squeeze.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:squeeze, :type => :rvalue, :doc => <<-EOS +Returns a new string where runs of the same character that occur in this set are replaced by a single character. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/str2bool.rb b/lib/puppet/parser/functions/str2bool.rb index f3a6d6c..c320da6 100644 --- a/lib/puppet/parser/functions/str2bool.rb +++ b/lib/puppet/parser/functions/str2bool.rb @@ -4,6 +4,9 @@ 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'. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/strftime.rb b/lib/puppet/parser/functions/strftime.rb index c919320..0b52ade 100644 --- a/lib/puppet/parser/functions/strftime.rb +++ b/lib/puppet/parser/functions/strftime.rb @@ -4,6 +4,69 @@ module Puppet::Parser::Functions newfunction(:strftime, :type => :rvalue, :doc => <<-EOS +This function returns formatted time. + +*Examples:* + +To return the time since epoch: + + strftime("%s") + +To return the date: + + strftime("%Y-%m-%d") + +*Format meaning:* + + %a - The abbreviated weekday name (``Sun'') + %A - The full weekday name (``Sunday'') + %b - The abbreviated month name (``Jan'') + %B - The full month name (``January'') + %c - The preferred local date and time representation + %C - Century (20 in 2009) + %d - Day of the month (01..31) + %D - Date (%m/%d/%y) + %e - Day of the month, blank-padded ( 1..31) + %F - Equivalent to %Y-%m-%d (the ISO 8601 date format) + %h - Equivalent to %b + %H - Hour of the day, 24-hour clock (00..23) + %I - Hour of the day, 12-hour clock (01..12) + %j - Day of the year (001..366) + %k - hour, 24-hour clock, blank-padded ( 0..23) + %l - hour, 12-hour clock, blank-padded ( 0..12) + %L - Millisecond of the second (000..999) + %m - Month of the year (01..12) + %M - Minute of the hour (00..59) + %n - Newline (\n) + %N - Fractional seconds digits, default is 9 digits (nanosecond) + %3N millisecond (3 digits) + %6N microsecond (6 digits) + %9N nanosecond (9 digits) + %p - Meridian indicator (``AM'' or ``PM'') + %P - Meridian indicator (``am'' or ``pm'') + %r - time, 12-hour (same as %I:%M:%S %p) + %R - time, 24-hour (%H:%M) + %s - Number of seconds since 1970-01-01 00:00:00 UTC. + %S - Second of the minute (00..60) + %t - Tab character (\t) + %T - time, 24-hour (%H:%M:%S) + %u - Day of the week as a decimal, Monday being 1. (1..7) + %U - Week number of the current year, + starting with the first Sunday as the first + day of the first week (00..53) + %v - VMS date (%e-%b-%Y) + %V - Week number of year according to ISO 8601 (01..53) + %W - Week number of the current year, + starting with the first Monday as the first + day of the first week (00..53) + %w - Day of the week (Sunday is 0, 0..6) + %x - Preferred representation for the date alone, no time + %X - Preferred representation for the time alone, no date + %y - Year without a century (00..99) + %Y - Year with century + %z - Time zone as hour offset from UTC (e.g. +0900) + %Z - Time zone name + %% - Literal ``%'' character EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/strip.rb b/lib/puppet/parser/functions/strip.rb index ebab20e..5f4630d 100644 --- a/lib/puppet/parser/functions/strip.rb +++ b/lib/puppet/parser/functions/strip.rb @@ -4,6 +4,14 @@ module Puppet::Parser::Functions newfunction(:strip, :type => :rvalue, :doc => <<-EOS +This function removes leading and trailing whitespace from a string or from +every string inside an array. + +*Examples:* + + strip(" aaa ") + +Would result in: "aaa" EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/swapcase.rb b/lib/puppet/parser/functions/swapcase.rb index a0002a9..b9e6632 100644 --- a/lib/puppet/parser/functions/swapcase.rb +++ b/lib/puppet/parser/functions/swapcase.rb @@ -4,6 +4,13 @@ module Puppet::Parser::Functions newfunction(:swapcase, :type => :rvalue, :doc => <<-EOS +This function will swap the existing case of a string. + +*Examples:* + + swapcase("aBcD") + +Would result in: "AbCd" EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/time.rb b/lib/puppet/parser/functions/time.rb index e1f5b6f..0cddaf8 100644 --- a/lib/puppet/parser/functions/time.rb +++ b/lib/puppet/parser/functions/time.rb @@ -4,6 +4,13 @@ module Puppet::Parser::Functions newfunction(:time, :type => :rvalue, :doc => <<-EOS +This function will return the current time since epoch as an integer. + +*Examples:* + + time() + +Will return something like: 1311972653 EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/type.rb b/lib/puppet/parser/functions/type.rb index 389a056..de6087e 100644 --- a/lib/puppet/parser/functions/type.rb +++ b/lib/puppet/parser/functions/type.rb @@ -4,6 +4,13 @@ module Puppet::Parser::Functions newfunction(:type, :type => :rvalue, :doc => <<-EOS +Returns the type when passed a variable. Type can be one of: + +* string +* array +* hash +* float +* integer EOS ) do |arguments| @@ -22,11 +29,19 @@ module Puppet::Parser::Functions # We note that Integer is the parent to Bignum and Fixnum ... result = case klass - when /^(?:Big|Fix)num$/ then 'Integer' + when /^(?:Big|Fix)num$/ then 'integer' else klass end - return result + if result == "String" then + if value == value.to_i.to_s then + result = "Integer" + elsif value == value.to_f.to_s then + result = "Float" + end + end + + return result.downcase end end diff --git a/lib/puppet/parser/functions/unique.rb b/lib/puppet/parser/functions/unique.rb index a922c94..8844a74 100644 --- a/lib/puppet/parser/functions/unique.rb +++ b/lib/puppet/parser/functions/unique.rb @@ -4,6 +4,23 @@ module Puppet::Parser::Functions newfunction(:unique, :type => :rvalue, :doc => <<-EOS +This function will remove duplicates from strings and arrays. + +*Examples:* + + unique("aabbcc") + +Will return: + + abc + +You can also use this with arrays: + + unique(["a","a","b","b","c","c"]) + +This returns: + + ["a","b","c"] EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/upcase.rb b/lib/puppet/parser/functions/upcase.rb index 8a9769d..fe6cadc 100644 --- a/lib/puppet/parser/functions/upcase.rb +++ b/lib/puppet/parser/functions/upcase.rb @@ -4,6 +4,15 @@ module Puppet::Parser::Functions newfunction(:upcase, :type => :rvalue, :doc => <<-EOS +Converts a string or an array of strings to uppercase. + +*Examples:* + + upcase("abcd") + +Will return: + + ASDF EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/validate_resource.rb b/lib/puppet/parser/functions/validate_resource.rb index bbb5a54..d71ef3f 100644 --- a/lib/puppet/parser/functions/validate_resource.rb +++ b/lib/puppet/parser/functions/validate_resource.rb @@ -4,6 +4,11 @@ module Puppet::Parser::Functions newfunction(:validate_resource, :type => :statement, :doc => <<-EOS +This function when placed at the beginning of a class, will go looking for a +valid kwalify schema by replacing the extension of the file with '.schema'. + +It will then validate the arguments passed to the function using that kwalify +schema. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/values.rb b/lib/puppet/parser/functions/values.rb index c1c4a77..1606756 100644 --- a/lib/puppet/parser/functions/values.rb +++ b/lib/puppet/parser/functions/values.rb @@ -4,6 +4,20 @@ module Puppet::Parser::Functions newfunction(:values, :type => :rvalue, :doc => <<-EOS +When given a hash this function will return the values of that hash. + +*Examples:* + + $hash = { + 'a' => 1, + 'b' => 2, + 'c' => 3, + } + values($hash) + +This example would return: + + [1,2,3] EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/values_at.rb b/lib/puppet/parser/functions/values_at.rb index 331af6a..7f1de8e 100644 --- a/lib/puppet/parser/functions/values_at.rb +++ b/lib/puppet/parser/functions/values_at.rb @@ -2,11 +2,30 @@ # values_at.rb # -# TODO(Krzysztof Wilczynski): Support for hashes would be nice too ... -# TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ... - module Puppet::Parser::Functions newfunction(:values_at, :type => :rvalue, :doc => <<-EOS +Finds value inside an array based on location. + +The first argument is the array you want to analyze, and the second element can +be a combination of: + +* A single numeric index +* A range in the form of 'start-stop' (eg. 4-9) +* An array combining the above + +*Examples*: + + values_at(['a','b','c'], 2) + +Would return ['c']. + + values_at(['a','b','c'], ["0-1"]) + +Would return ['a','b']. + + values_at(['a','b','c','d','e'], [0, "2-3"]) + +Would return ['a','c','d']. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/zip.rb b/lib/puppet/parser/functions/zip.rb index 024d64a..2b56e9c 100644 --- a/lib/puppet/parser/functions/zip.rb +++ b/lib/puppet/parser/functions/zip.rb @@ -4,6 +4,15 @@ module Puppet::Parser::Functions newfunction(:zip, :type => :rvalue, :doc => <<-EOS +Takes one element from first array and merges corresponding elements from second array. This generates a sequence of n-element arrays, where n is one more than the count of arguments. + +*Example:* + + zip(['1','2','3'],['4','5','6']) + +Would result in: + + ["1", "4"], ["2", "5"], ["3", "6"] EOS ) do |arguments| diff --git a/spec/unit/parser/functions/type_spec.rb b/spec/unit/parser/functions/type_spec.rb index 36c3823..53071f3 100644 --- a/spec/unit/parser/functions/type_spec.rb +++ b/spec/unit/parser/functions/type_spec.rb @@ -18,19 +18,29 @@ describe "the type function" do lambda { @scope.function_type([]) }.should( raise_error(Puppet::ParseError)) end - it "should return String when given a string" do + it "should return string when given a string" do result = @scope.function_type(["aaabbbbcccc"]) - result.should(eq('String')) + result.should(eq('string')) end - it "should return Array when given an array" do + it "should return array when given an array" do result = @scope.function_type([["aaabbbbcccc","asdf"]]) - result.should(eq('Array')) + result.should(eq('array')) end - it "should return Hash when given a hash" do + it "should return hash when given a hash" do result = @scope.function_type([{"a"=>1,"b"=>2}]) - result.should(eq('Hash')) + result.should(eq('hash')) + end + + it "should return integer when given an integer" do + result = @scope.function_type(["1"]) + result.should(eq('integer')) + end + + it "should return float when given a float" do + result = @scope.function_type(["1.34"]) + result.should(eq('float')) end end diff --git a/spec/unit/parser/functions/unique_spec.rb b/spec/unit/parser/functions/unique_spec.rb index 7b8b08d..627dc33 100644 --- a/spec/unit/parser/functions/unique_spec.rb +++ b/spec/unit/parser/functions/unique_spec.rb @@ -19,8 +19,13 @@ describe "the unique function" do end it "should remove duplicate elements in a string" do - result = @scope.function_squeeze([["aabbc"]]) - result.should(eq(['abc'])) + result = @scope.function_unique(["aabbc"]) + result.should(eq('abc')) + end + + it "should remove duplicate elements in an array" do + result = @scope.function_unique([["a","a","b","b","c"]]) + result.should(eq(['a','b','c'])) end end diff --git a/spec/unit/parser/functions/values_spec.rb b/spec/unit/parser/functions/values_spec.rb index 92f1311..f6eb5b6 100644 --- a/spec/unit/parser/functions/values_spec.rb +++ b/spec/unit/parser/functions/values_spec.rb @@ -23,4 +23,8 @@ describe "the values function" do result.should(eq(['1','2','3'])) end + it "should return values from a hash" do + lambda { @scope.function_values([['a','b','c']]) }.should( raise_error(Puppet::ParseError)) + end + end -- cgit v1.2.3 From a1cae426f1d6b8f2c19184ec8aac3ebc47d97744 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 29 Jul 2011 23:09:30 +0100 Subject: (#3) Provide documentation for remaining functions. --- lib/puppet/parser/functions/delete.rb | 9 ++++++++- lib/puppet/parser/functions/delete_at.rb | 7 +++++++ lib/puppet/parser/functions/downcase.rb | 1 + lib/puppet/parser/functions/empty.rb | 1 + lib/puppet/parser/functions/flatten.rb | 8 ++++++++ lib/puppet/parser/functions/grep.rb | 10 ++++++++++ lib/puppet/parser/functions/hash.rb | 7 +++++++ lib/puppet/parser/functions/is_array.rb | 1 + lib/puppet/parser/functions/is_float.rb | 1 + lib/puppet/parser/functions/is_hash.rb | 1 + lib/puppet/parser/functions/is_integer.rb | 1 + lib/puppet/parser/functions/is_numeric.rb | 1 + lib/puppet/parser/functions/is_string.rb | 1 + lib/puppet/parser/functions/is_valid_domain_name.rb | 1 + lib/puppet/parser/functions/is_valid_ip_address.rb | 1 + lib/puppet/parser/functions/is_valid_mac_address.rb | 1 + lib/puppet/parser/functions/join.rb | 7 +++++++ lib/puppet/parser/functions/keys.rb | 1 + lib/puppet/parser/functions/load_json.rb | 2 ++ lib/puppet/parser/functions/load_yaml.rb | 2 ++ lib/puppet/parser/functions/lstrip.rb | 1 + lib/puppet/parser/functions/member.rb | 11 +++++++++++ lib/puppet/parser/functions/type.rb | 4 +++- spec/unit/parser/functions/type_spec.rb | 5 +++++ 24 files changed, 83 insertions(+), 2 deletions(-) diff --git a/lib/puppet/parser/functions/delete.rb b/lib/puppet/parser/functions/delete.rb index 0d208b5..ab8f75b 100644 --- a/lib/puppet/parser/functions/delete.rb +++ b/lib/puppet/parser/functions/delete.rb @@ -7,11 +7,18 @@ module Puppet::Parser::Functions newfunction(:delete, :type => :rvalue, :doc => <<-EOS +Deletes a selected element from an array. + +*Examples:* + + delete(['a','b','c'], 'b') + +Would return: ['a','c'] EOS ) do |arguments| if (arguments.size != 2) then - raise(Puppet::ParseError, "is_valid_netmask(): Wrong number of arguments "+ + raise(Puppet::ParseError, "delete(): Wrong number of arguments "+ "given #{arguments.size} for 2") end diff --git a/lib/puppet/parser/functions/delete_at.rb b/lib/puppet/parser/functions/delete_at.rb index 10190ba..3eb4b53 100644 --- a/lib/puppet/parser/functions/delete_at.rb +++ b/lib/puppet/parser/functions/delete_at.rb @@ -4,6 +4,13 @@ module Puppet::Parser::Functions newfunction(:delete_at, :type => :rvalue, :doc => <<-EOS +Deletes a determined indexed value from an array. + +*Examples:* + + delete_at(['a','b','c'], 1) + +Would return: ['a','c'] EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/downcase.rb b/lib/puppet/parser/functions/downcase.rb index 71f8480..4066d21 100644 --- a/lib/puppet/parser/functions/downcase.rb +++ b/lib/puppet/parser/functions/downcase.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:downcase, :type => :rvalue, :doc => <<-EOS +Converts the case of a string or all strings in an array to lower case. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/empty.rb b/lib/puppet/parser/functions/empty.rb index e78ebf0..80ebb86 100644 --- a/lib/puppet/parser/functions/empty.rb +++ b/lib/puppet/parser/functions/empty.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:empty, :type => :rvalue, :doc => <<-EOS +Returns true if the variable is empty. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/flatten.rb b/lib/puppet/parser/functions/flatten.rb index 6036f72..781da78 100644 --- a/lib/puppet/parser/functions/flatten.rb +++ b/lib/puppet/parser/functions/flatten.rb @@ -4,6 +4,14 @@ module Puppet::Parser::Functions newfunction(:flatten, :type => :rvalue, :doc => <<-EOS +This function flattens any deeply nested arrays and returns a single flat array +as a result. + +*Examples:* + + flatten(['a', ['b', ['c']]]) + +Would return: ['a','b','c'] EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/grep.rb b/lib/puppet/parser/functions/grep.rb index 2caaa6f..ceba9ec 100644 --- a/lib/puppet/parser/functions/grep.rb +++ b/lib/puppet/parser/functions/grep.rb @@ -4,6 +4,16 @@ module Puppet::Parser::Functions newfunction(:grep, :type => :rvalue, :doc => <<-EOS +This function searches through an array and returns any elements that match +the provided regular expression. + +*Examples:* + + grep(['aaa','bbb','ccc','aaaddd'], 'aaa') + +Would return: + + ['aaa','aaaddd'] EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/hash.rb b/lib/puppet/parser/functions/hash.rb index f0c01d4..453ba1e 100644 --- a/lib/puppet/parser/functions/hash.rb +++ b/lib/puppet/parser/functions/hash.rb @@ -4,6 +4,13 @@ module Puppet::Parser::Functions newfunction(:hash, :type => :rvalue, :doc => <<-EOS +This function converts and array into a hash. + +*Examples:* + + hash(['a',1,'b',2,'c',3]) + +Would return: {'a'=>1,'b'=>2,'c'=>3} EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/is_array.rb b/lib/puppet/parser/functions/is_array.rb index 0b508fd..b39e184 100644 --- a/lib/puppet/parser/functions/is_array.rb +++ b/lib/puppet/parser/functions/is_array.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:is_array, :type => :rvalue, :doc => <<-EOS +Returns true if the variable passed to this function is an array. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/is_float.rb b/lib/puppet/parser/functions/is_float.rb index 8aed848..2fc05ba 100644 --- a/lib/puppet/parser/functions/is_float.rb +++ b/lib/puppet/parser/functions/is_float.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:is_float, :type => :rvalue, :doc => <<-EOS +Returns true if the variable passed to this function is a float. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/is_hash.rb b/lib/puppet/parser/functions/is_hash.rb index 000306e..ad907f0 100644 --- a/lib/puppet/parser/functions/is_hash.rb +++ b/lib/puppet/parser/functions/is_hash.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:is_hash, :type => :rvalue, :doc => <<-EOS +Returns true if the variable passed to this function is a hash. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/is_integer.rb b/lib/puppet/parser/functions/is_integer.rb index 9dd1cee..8ee34f6 100644 --- a/lib/puppet/parser/functions/is_integer.rb +++ b/lib/puppet/parser/functions/is_integer.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:is_integer, :type => :rvalue, :doc => <<-EOS +Returns true if the variable returned to this string is an integer. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/is_numeric.rb b/lib/puppet/parser/functions/is_numeric.rb index c2c0fb5..ce13ece 100644 --- a/lib/puppet/parser/functions/is_numeric.rb +++ b/lib/puppet/parser/functions/is_numeric.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:is_numeric, :type => :rvalue, :doc => <<-EOS +Returns true if the variable passed to this function is a number. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/is_string.rb b/lib/puppet/parser/functions/is_string.rb index 8a02a10..f5bef04 100644 --- a/lib/puppet/parser/functions/is_string.rb +++ b/lib/puppet/parser/functions/is_string.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:is_string, :type => :rvalue, :doc => <<-EOS +Returns true if the variable passed to this function is a string. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/is_valid_domain_name.rb b/lib/puppet/parser/functions/is_valid_domain_name.rb index 99d6f86..7dd9c0b 100644 --- a/lib/puppet/parser/functions/is_valid_domain_name.rb +++ b/lib/puppet/parser/functions/is_valid_domain_name.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:is_valid_domain_name, :type => :rvalue, :doc => <<-EOS +Returns true if the string passed to this function is a valid IP address. Support for IPv4 and IPv6 address types is included. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/is_valid_ip_address.rb b/lib/puppet/parser/functions/is_valid_ip_address.rb index 4f45890..604d938 100644 --- a/lib/puppet/parser/functions/is_valid_ip_address.rb +++ b/lib/puppet/parser/functions/is_valid_ip_address.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:is_valid_ip_address, :type => :rvalue, :doc => <<-EOS +Returns true if the string passed to this function is a valid IP address. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/is_valid_mac_address.rb b/lib/puppet/parser/functions/is_valid_mac_address.rb index a00d874..53199b6 100644 --- a/lib/puppet/parser/functions/is_valid_mac_address.rb +++ b/lib/puppet/parser/functions/is_valid_mac_address.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:is_valid_mac_address, :type => :rvalue, :doc => <<-EOS +Returns true if the string passed to this function is a valid mac address. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/join.rb b/lib/puppet/parser/functions/join.rb index 945556a..005a46e 100644 --- a/lib/puppet/parser/functions/join.rb +++ b/lib/puppet/parser/functions/join.rb @@ -4,6 +4,13 @@ module Puppet::Parser::Functions newfunction(:join, :type => :rvalue, :doc => <<-EOS +This function joins an array into a string using a seperator. + +*Examples:* + + join(['a','b','c'], ",") + +Would result in: "a,b,c" EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/keys.rb b/lib/puppet/parser/functions/keys.rb index 3a92a47..f0d13b6 100644 --- a/lib/puppet/parser/functions/keys.rb +++ b/lib/puppet/parser/functions/keys.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:keys, :type => :rvalue, :doc => <<-EOS +Returns the keys of a hash as an array. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/load_json.rb b/lib/puppet/parser/functions/load_json.rb index 7c3f187..333cf11 100644 --- a/lib/puppet/parser/functions/load_json.rb +++ b/lib/puppet/parser/functions/load_json.rb @@ -4,6 +4,8 @@ module Puppet::Parser::Functions newfunction(:load_json, :type => :rvalue, :doc => <<-EOS +This function accepts JSON as a string and converts into the correct Puppet +structure. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/load_yaml.rb b/lib/puppet/parser/functions/load_yaml.rb index 1bc2f36..2683277 100644 --- a/lib/puppet/parser/functions/load_yaml.rb +++ b/lib/puppet/parser/functions/load_yaml.rb @@ -4,6 +4,8 @@ module Puppet::Parser::Functions newfunction(:load_yaml, :type => :rvalue, :doc => <<-EOS +This function accepts YAML as a string and converts it into the correct +Puppet structure. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/lstrip.rb b/lib/puppet/parser/functions/lstrip.rb index a7b2656..3a64de3 100644 --- a/lib/puppet/parser/functions/lstrip.rb +++ b/lib/puppet/parser/functions/lstrip.rb @@ -4,6 +4,7 @@ module Puppet::Parser::Functions newfunction(:lstrip, :type => :rvalue, :doc => <<-EOS +Strips leading spaces to the left of a string. EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/member.rb b/lib/puppet/parser/functions/member.rb index bb43a37..43d76af 100644 --- a/lib/puppet/parser/functions/member.rb +++ b/lib/puppet/parser/functions/member.rb @@ -7,6 +7,17 @@ module Puppet::Parser::Functions newfunction(:member, :type => :rvalue, :doc => <<-EOS +This function determines if a variable is a member of an array. + +*Examples:* + + member(['a','b'], 'b') + +Would return: true + + member(['a','b'], 'c') + +Would return: false EOS ) do |arguments| diff --git a/lib/puppet/parser/functions/type.rb b/lib/puppet/parser/functions/type.rb index de6087e..8d85f11 100644 --- a/lib/puppet/parser/functions/type.rb +++ b/lib/puppet/parser/functions/type.rb @@ -11,6 +11,7 @@ Returns the type when passed a variable. Type can be one of: * hash * float * integer +* boolean EOS ) do |arguments| @@ -21,7 +22,7 @@ Returns the type when passed a variable. Type can be one of: klass = value.class - if not [Array, Bignum, Fixnum, Float, Hash, String].include?(klass) + if not [TrueClass, FalseClass, Array, Bignum, Fixnum, Float, Hash, String].include?(klass) raise(Puppet::ParseError, 'type(): Unknown type') end @@ -30,6 +31,7 @@ Returns the type when passed a variable. Type can be one of: # We note that Integer is the parent to Bignum and Fixnum ... result = case klass when /^(?:Big|Fix)num$/ then 'integer' + when /^(?:True|False)Class$/ then 'boolean' else klass end diff --git a/spec/unit/parser/functions/type_spec.rb b/spec/unit/parser/functions/type_spec.rb index 53071f3..e3c28ed 100644 --- a/spec/unit/parser/functions/type_spec.rb +++ b/spec/unit/parser/functions/type_spec.rb @@ -43,4 +43,9 @@ describe "the type function" do result.should(eq('float')) end + it "should return boolean when given a boolean" do + result = @scope.function_type([true]) + result.should(eq('boolean')) + end + end -- cgit v1.2.3 From 35fefe186546427963d8c8a446fa98875d65ccad Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Sat, 30 Jul 2011 00:44:02 +0100 Subject: Fix some ruby 1.9.2 issues. --- lib/puppet/parser/functions/sort.rb | 2 +- lib/puppet/parser/functions/values_at.rb | 2 +- spec/unit/parser/functions/shuffle_spec.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/puppet/parser/functions/sort.rb b/lib/puppet/parser/functions/sort.rb index 3785496..cefbe54 100644 --- a/lib/puppet/parser/functions/sort.rb +++ b/lib/puppet/parser/functions/sort.rb @@ -18,7 +18,7 @@ Sorts strings and arrays lexically. if value.is_a?(Array) then value.sort elsif value.is_a?(String) then - value.split("").sort.to_s + value.split("").sort.join("") end end diff --git a/lib/puppet/parser/functions/values_at.rb b/lib/puppet/parser/functions/values_at.rb index 7f1de8e..d3e69d9 100644 --- a/lib/puppet/parser/functions/values_at.rb +++ b/lib/puppet/parser/functions/values_at.rb @@ -38,7 +38,7 @@ Would return ['a','c','d']. raise(Puppet::ParseError, 'values_at(): Requires array to work with') end - indices = *arguments # Get them all ... Pokemon ... + indices = [arguments.shift].flatten() # Get them all ... Pokemon ... if not indices or indices.empty? raise(Puppet::ParseError, 'values_at(): You must provide ' + diff --git a/spec/unit/parser/functions/shuffle_spec.rb b/spec/unit/parser/functions/shuffle_spec.rb index 936c2fd..f04fda5 100644 --- a/spec/unit/parser/functions/shuffle_spec.rb +++ b/spec/unit/parser/functions/shuffle_spec.rb @@ -25,7 +25,7 @@ describe "the shuffle function" do it "should shuffle a string but the sorted contents should still be the same" do result = @scope.function_shuffle(["adfs"]) - result.split("").sort.to_s.should(eq("adfs")) + result.split("").sort.join("").should(eq("adfs")) end end -- cgit v1.2.3 From 681a1c7971d78c53dc9a0747ae4d983ff6b0d670 Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 5 Aug 2011 08:25:03 +0100 Subject: Prep for stdlib merge * Renamed load_yaml & load_json to parseyaml & parsejson * Renamed is_valid_* functions and remove the 'valid_' --- lib/puppet/parser/functions/is_domain_name.rb | 27 +++++++++++ lib/puppet/parser/functions/is_ip_address.rb | 32 +++++++++++++ lib/puppet/parser/functions/is_mac_address.rb | 27 +++++++++++ .../parser/functions/is_valid_domain_name.rb | 27 ----------- lib/puppet/parser/functions/is_valid_ip_address.rb | 32 ------------- .../parser/functions/is_valid_mac_address.rb | 27 ----------- lib/puppet/parser/functions/load_json.rb | 26 ---------- lib/puppet/parser/functions/load_yaml.rb | 24 ---------- lib/puppet/parser/functions/parsejson.rb | 26 ++++++++++ lib/puppet/parser/functions/parseyaml.rb | 24 ++++++++++ spec/unit/parser/functions/is_domain_name_spec.rb | 56 ++++++++++++++++++++++ spec/unit/parser/functions/is_ip_address_spec.rb | 45 +++++++++++++++++ spec/unit/parser/functions/is_mac_address_spec.rb | 36 ++++++++++++++ .../parser/functions/is_valid_domain_name_spec.rb | 56 ---------------------- .../parser/functions/is_valid_ip_address_spec.rb | 45 ----------------- .../parser/functions/is_valid_mac_address_spec.rb | 36 -------------- spec/unit/parser/functions/load_json_spec.rb | 29 ----------- spec/unit/parser/functions/load_yaml_spec.rb | 31 ------------ spec/unit/parser/functions/parsejson_spec.rb | 29 +++++++++++ spec/unit/parser/functions/parseyaml_spec.rb | 31 ++++++++++++ 20 files changed, 333 insertions(+), 333 deletions(-) create mode 100644 lib/puppet/parser/functions/is_domain_name.rb create mode 100644 lib/puppet/parser/functions/is_ip_address.rb create mode 100644 lib/puppet/parser/functions/is_mac_address.rb delete mode 100644 lib/puppet/parser/functions/is_valid_domain_name.rb delete mode 100644 lib/puppet/parser/functions/is_valid_ip_address.rb delete mode 100644 lib/puppet/parser/functions/is_valid_mac_address.rb delete mode 100644 lib/puppet/parser/functions/load_json.rb delete mode 100644 lib/puppet/parser/functions/load_yaml.rb create mode 100644 lib/puppet/parser/functions/parsejson.rb create mode 100644 lib/puppet/parser/functions/parseyaml.rb create mode 100644 spec/unit/parser/functions/is_domain_name_spec.rb create mode 100644 spec/unit/parser/functions/is_ip_address_spec.rb create mode 100644 spec/unit/parser/functions/is_mac_address_spec.rb delete mode 100644 spec/unit/parser/functions/is_valid_domain_name_spec.rb delete mode 100644 spec/unit/parser/functions/is_valid_ip_address_spec.rb delete mode 100644 spec/unit/parser/functions/is_valid_mac_address_spec.rb delete mode 100644 spec/unit/parser/functions/load_json_spec.rb delete mode 100644 spec/unit/parser/functions/load_yaml_spec.rb create mode 100644 spec/unit/parser/functions/parsejson_spec.rb create mode 100644 spec/unit/parser/functions/parseyaml_spec.rb diff --git a/lib/puppet/parser/functions/is_domain_name.rb b/lib/puppet/parser/functions/is_domain_name.rb new file mode 100644 index 0000000..4e92939 --- /dev/null +++ b/lib/puppet/parser/functions/is_domain_name.rb @@ -0,0 +1,27 @@ +# +# is_domain_name.rb +# + +module Puppet::Parser::Functions + newfunction(:is_domain_name, :type => :rvalue, :doc => <<-EOS +Returns true if the string passed to this function is a valid IP address. Support for IPv4 and IPv6 address types is included. + EOS + ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_domain_name(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + + domain = arguments[0] + + if domain =~ /^(([a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])\.?$/ then + return true + else + return false + end + + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_ip_address.rb b/lib/puppet/parser/functions/is_ip_address.rb new file mode 100644 index 0000000..b4a9a15 --- /dev/null +++ b/lib/puppet/parser/functions/is_ip_address.rb @@ -0,0 +1,32 @@ +# +# is_ip_address.rb +# + +module Puppet::Parser::Functions + newfunction(:is_ip_address, :type => :rvalue, :doc => <<-EOS +Returns true if the string passed to this function is a valid IP address. + EOS + ) do |arguments| + + require 'ipaddr' + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_ip_address(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + + begin + ip = IPAddr.new(arguments[0]) + rescue ArgumentError + return false + end + + if ip.ipv4? or ip.ipv6? then + return true + else + return false + end + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_mac_address.rb b/lib/puppet/parser/functions/is_mac_address.rb new file mode 100644 index 0000000..1b3088a --- /dev/null +++ b/lib/puppet/parser/functions/is_mac_address.rb @@ -0,0 +1,27 @@ +# +# is_mac_address.rb +# + +module Puppet::Parser::Functions + newfunction(:is_mac_address, :type => :rvalue, :doc => <<-EOS +Returns true if the string passed to this function is a valid mac address. + EOS + ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_mac_address(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + + mac = arguments[0] + + if /^[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}$/.match(mac) then + return true + else + return false + end + + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_valid_domain_name.rb b/lib/puppet/parser/functions/is_valid_domain_name.rb deleted file mode 100644 index 7dd9c0b..0000000 --- a/lib/puppet/parser/functions/is_valid_domain_name.rb +++ /dev/null @@ -1,27 +0,0 @@ -# -# is_valid_domain_name.rb -# - -module Puppet::Parser::Functions - newfunction(:is_valid_domain_name, :type => :rvalue, :doc => <<-EOS -Returns true if the string passed to this function is a valid IP address. Support for IPv4 and IPv6 address types is included. - EOS - ) do |arguments| - - if (arguments.size != 1) then - raise(Puppet::ParseError, "is_valid_domain_name(): Wrong number of arguments "+ - "given #{arguments.size} for 1") - end - - domain = arguments[0] - - if domain =~ /^(([a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])\.?$/ then - return true - else - return false - end - - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_valid_ip_address.rb b/lib/puppet/parser/functions/is_valid_ip_address.rb deleted file mode 100644 index 604d938..0000000 --- a/lib/puppet/parser/functions/is_valid_ip_address.rb +++ /dev/null @@ -1,32 +0,0 @@ -# -# is_valid_ip_address.rb -# - -module Puppet::Parser::Functions - newfunction(:is_valid_ip_address, :type => :rvalue, :doc => <<-EOS -Returns true if the string passed to this function is a valid IP address. - EOS - ) do |arguments| - - require 'ipaddr' - - if (arguments.size != 1) then - raise(Puppet::ParseError, "is_valid_ip_address(): Wrong number of arguments "+ - "given #{arguments.size} for 1") - end - - begin - ip = IPAddr.new(arguments[0]) - rescue ArgumentError - return false - end - - if ip.ipv4? or ip.ipv6? then - return true - else - return false - end - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/is_valid_mac_address.rb b/lib/puppet/parser/functions/is_valid_mac_address.rb deleted file mode 100644 index 53199b6..0000000 --- a/lib/puppet/parser/functions/is_valid_mac_address.rb +++ /dev/null @@ -1,27 +0,0 @@ -# -# is_valid_mac_address.rb -# - -module Puppet::Parser::Functions - newfunction(:is_valid_mac_address, :type => :rvalue, :doc => <<-EOS -Returns true if the string passed to this function is a valid mac address. - EOS - ) do |arguments| - - if (arguments.size != 1) then - raise(Puppet::ParseError, "is_valid_mac_address(): Wrong number of arguments "+ - "given #{arguments.size} for 1") - end - - mac = arguments[0] - - if /^[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}:[a-fA-F0-9]{1,2}$/.match(mac) then - return true - else - return false - end - - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/load_json.rb b/lib/puppet/parser/functions/load_json.rb deleted file mode 100644 index 333cf11..0000000 --- a/lib/puppet/parser/functions/load_json.rb +++ /dev/null @@ -1,26 +0,0 @@ -# -# load_json.rb -# - -module Puppet::Parser::Functions - newfunction(:load_json, :type => :rvalue, :doc => <<-EOS -This function accepts JSON as a string and converts into the correct Puppet -structure. - EOS - ) do |arguments| - - if (arguments.size != 1) then - raise(Puppet::ParseError, "load_json(): Wrong number of arguments "+ - "given #{arguments.size} for 1") - end - - json = arguments[0] - - require 'json' - - JSON.load(json) - - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/load_yaml.rb b/lib/puppet/parser/functions/load_yaml.rb deleted file mode 100644 index 2683277..0000000 --- a/lib/puppet/parser/functions/load_yaml.rb +++ /dev/null @@ -1,24 +0,0 @@ -# -# load_yaml.rb -# - -module Puppet::Parser::Functions - newfunction(:load_yaml, :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, "load_yaml(): Wrong number of arguments "+ - "given #{arguments.size} for 1") - end - - require 'yaml' - - YAML::load(arguments[0]) - - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/parsejson.rb b/lib/puppet/parser/functions/parsejson.rb new file mode 100644 index 0000000..cf3b12c --- /dev/null +++ b/lib/puppet/parser/functions/parsejson.rb @@ -0,0 +1,26 @@ +# +# parsejson.rb +# + +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 + ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "parsejson(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + + json = arguments[0] + + require 'json' + + JSON.load(json) + + end +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/parseyaml.rb b/lib/puppet/parser/functions/parseyaml.rb new file mode 100644 index 0000000..e8ac8a4 --- /dev/null +++ b/lib/puppet/parser/functions/parseyaml.rb @@ -0,0 +1,24 @@ +# +# parseyaml.rb +# + +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 + + require 'yaml' + + YAML::load(arguments[0]) + + end +end + +# vim: set ts=2 sw=2 et : diff --git a/spec/unit/parser/functions/is_domain_name_spec.rb b/spec/unit/parser/functions/is_domain_name_spec.rb new file mode 100644 index 0000000..ec7c7f5 --- /dev/null +++ b/spec/unit/parser/functions/is_domain_name_spec.rb @@ -0,0 +1,56 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_domain_name function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_domain_name").should == "function_is_domain_name" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_domain_name([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return true if a valid domain name" do + result = @scope.function_is_domain_name(["foo.bar.com"]) + result.should(be_true) + end + + it "should allow domain parts to start with numbers" do + result = @scope.function_is_domain_name(["3foo.2bar.com"]) + result.should(be_true) + end + + it "should allow domain to end with a dot" do + result = @scope.function_is_domain_name(["3foo.2bar.com."]) + result.should(be_true) + end + + it "should allow a single part domain" do + result = @scope.function_is_domain_name(["orange"]) + result.should(be_true) + end + + it "should return false if domain parts start with hyphens" do + result = @scope.function_is_domain_name(["-3foo.2bar.com"]) + result.should(be_false) + end + + it "should return true if domain contains hyphens" do + result = @scope.function_is_domain_name(["3foo-bar.2bar-fuzz.com"]) + result.should(be_true) + end + + it "should return false if domain name contains spaces" do + result = @scope.function_is_domain_name(["not valid"]) + result.should(be_false) + end + +end diff --git a/spec/unit/parser/functions/is_ip_address_spec.rb b/spec/unit/parser/functions/is_ip_address_spec.rb new file mode 100644 index 0000000..98ce828 --- /dev/null +++ b/spec/unit/parser/functions/is_ip_address_spec.rb @@ -0,0 +1,45 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_ip_address function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_ip_address").should == "function_is_ip_address" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_ip_address([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return true if an IPv4 address" do + result = @scope.function_is_ip_address(["1.2.3.4"]) + result.should(eq(true)) + end + + it "should return true if a full IPv6 address" do + result = @scope.function_is_ip_address(["fe80:0000:cd12:d123:e2f8:47ff:fe09:dd74"]) + result.should(eq(true)) + end + + it "should return true if a compressed IPv6 address" do + result = @scope.function_is_ip_address(["fe00::1"]) + result.should(eq(true)) + end + + it "should return false if not valid" do + result = @scope.function_is_ip_address(["asdf"]) + result.should(eq(false)) + end + + it "should return false if IP octets out of range" do + result = @scope.function_is_ip_address(["1.1.1.300"]) + result.should(eq(false)) + end +end diff --git a/spec/unit/parser/functions/is_mac_address_spec.rb b/spec/unit/parser/functions/is_mac_address_spec.rb new file mode 100644 index 0000000..c9b9637 --- /dev/null +++ b/spec/unit/parser/functions/is_mac_address_spec.rb @@ -0,0 +1,36 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_mac_address function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_mac_address").should == "function_is_mac_address" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_mac_address([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return true if a valid mac address" do + result = @scope.function_is_mac_address(["00:a0:1f:12:7f:a0"]) + result.should(eq(true)) + end + + it "should return false if octets are out of range" do + result = @scope.function_is_mac_address(["00:a0:1f:12:7f:g0"]) + result.should(eq(false)) + end + + it "should return false if not valid" do + result = @scope.function_is_mac_address(["not valid"]) + result.should(eq(false)) + end + +end diff --git a/spec/unit/parser/functions/is_valid_domain_name_spec.rb b/spec/unit/parser/functions/is_valid_domain_name_spec.rb deleted file mode 100644 index 3339447..0000000 --- a/spec/unit/parser/functions/is_valid_domain_name_spec.rb +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the is_valid_domain_name function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("is_valid_domain_name").should == "function_is_valid_domain_name" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_is_valid_domain_name([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return true if a valid domain name" do - result = @scope.function_is_valid_domain_name(["foo.bar.com"]) - result.should(be_true) - end - - it "should allow domain parts to start with numbers" do - result = @scope.function_is_valid_domain_name(["3foo.2bar.com"]) - result.should(be_true) - end - - it "should allow domain to end with a dot" do - result = @scope.function_is_valid_domain_name(["3foo.2bar.com."]) - result.should(be_true) - end - - it "should allow a single part domain" do - result = @scope.function_is_valid_domain_name(["orange"]) - result.should(be_true) - end - - it "should return false if domain parts start with hyphens" do - result = @scope.function_is_valid_domain_name(["-3foo.2bar.com"]) - result.should(be_false) - end - - it "should return true if domain contains hyphens" do - result = @scope.function_is_valid_domain_name(["3foo-bar.2bar-fuzz.com"]) - result.should(be_true) - end - - it "should return false if domain name contains spaces" do - result = @scope.function_is_valid_domain_name(["not valid"]) - result.should(be_false) - end - -end diff --git a/spec/unit/parser/functions/is_valid_ip_address_spec.rb b/spec/unit/parser/functions/is_valid_ip_address_spec.rb deleted file mode 100644 index 2883aaa..0000000 --- a/spec/unit/parser/functions/is_valid_ip_address_spec.rb +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the is_valid_ip_address function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("is_valid_ip_address").should == "function_is_valid_ip_address" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_is_valid_ip_address([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return true if an IPv4 address" do - result = @scope.function_is_valid_ip_address(["1.2.3.4"]) - result.should(eq(true)) - end - - it "should return true if a full IPv6 address" do - result = @scope.function_is_valid_ip_address(["fe80:0000:cd12:d123:e2f8:47ff:fe09:dd74"]) - result.should(eq(true)) - end - - it "should return true if a compressed IPv6 address" do - result = @scope.function_is_valid_ip_address(["fe00::1"]) - result.should(eq(true)) - end - - it "should return false if not valid" do - result = @scope.function_is_valid_ip_address(["asdf"]) - result.should(eq(false)) - end - - it "should return false if IP octets out of range" do - result = @scope.function_is_valid_ip_address(["1.1.1.300"]) - result.should(eq(false)) - end -end diff --git a/spec/unit/parser/functions/is_valid_mac_address_spec.rb b/spec/unit/parser/functions/is_valid_mac_address_spec.rb deleted file mode 100644 index 62e6fee..0000000 --- a/spec/unit/parser/functions/is_valid_mac_address_spec.rb +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the is_valid_mac_address function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("is_valid_mac_address").should == "function_is_valid_mac_address" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_is_valid_mac_address([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return true if a valid mac address" do - result = @scope.function_is_valid_mac_address(["00:a0:1f:12:7f:a0"]) - result.should(eq(true)) - end - - it "should return false if octets are out of range" do - result = @scope.function_is_valid_mac_address(["00:a0:1f:12:7f:g0"]) - result.should(eq(false)) - end - - it "should return false if not valid" do - result = @scope.function_is_valid_mac_address(["not valid"]) - result.should(eq(false)) - end - -end diff --git a/spec/unit/parser/functions/load_json_spec.rb b/spec/unit/parser/functions/load_json_spec.rb deleted file mode 100644 index 73a4566..0000000 --- a/spec/unit/parser/functions/load_json_spec.rb +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the load_json function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("load_json").should == "function_load_json" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_load_json([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should convert JSON to a data structure" do - json = <<-EOS -["aaa","bbb","ccc"] -EOS - result = @scope.function_load_json([json]) - result.should(eq(['aaa','bbb','ccc'])) - end - -end diff --git a/spec/unit/parser/functions/load_yaml_spec.rb b/spec/unit/parser/functions/load_yaml_spec.rb deleted file mode 100644 index 2498d12..0000000 --- a/spec/unit/parser/functions/load_yaml_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the load_yaml function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("load_yaml").should == "function_load_yaml" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_load_yaml([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should convert YAML to a data structure" do - yaml = <<-EOS -- aaa -- bbb -- ccc -EOS - result = @scope.function_load_yaml([yaml]) - result.should(eq(['aaa','bbb','ccc'])) - end - -end diff --git a/spec/unit/parser/functions/parsejson_spec.rb b/spec/unit/parser/functions/parsejson_spec.rb new file mode 100644 index 0000000..26eea36 --- /dev/null +++ b/spec/unit/parser/functions/parsejson_spec.rb @@ -0,0 +1,29 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the parsejson function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("parsejson").should == "function_parsejson" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_parsejson([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should convert JSON to a data structure" do + json = <<-EOS +["aaa","bbb","ccc"] +EOS + result = @scope.function_parsejson([json]) + result.should(eq(['aaa','bbb','ccc'])) + end + +end diff --git a/spec/unit/parser/functions/parseyaml_spec.rb b/spec/unit/parser/functions/parseyaml_spec.rb new file mode 100644 index 0000000..f9cb049 --- /dev/null +++ b/spec/unit/parser/functions/parseyaml_spec.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the parseyaml function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("parseyaml").should == "function_parseyaml" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_parseyaml([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should convert YAML to a data structure" do + yaml = <<-EOS +- aaa +- bbb +- ccc +EOS + result = @scope.function_parseyaml([yaml]) + result.should(eq(['aaa','bbb','ccc'])) + end + +end -- cgit v1.2.3 From 1b73a66fc67af0e33fa41aacf50654d4a7a4903c Mon Sep 17 00:00:00 2001 From: Ken Barber Date: Fri, 5 Aug 2011 08:46:38 +0100 Subject: * Moved kwalify to puppetlabs-kwalify project * Re-arranged tests in line with puppetlabs-stdlib --- README.markdown | 27 +--------- examples/kwalify-1.pp | 9 ---- examples/kwalify-2.pp | 24 --------- examples/validate_resource-1.pp | 23 -------- examples/validate_resource-1.schema | 39 -------------- examples/validate_resource-2.pp | 17 ------ examples/validate_resource-2.schema | 26 --------- lib/puppet/parser/functions/kwalify.rb | 35 ------------ lib/puppet/parser/functions/validate_resource.rb | 41 -------------- spec/unit/parser/functions/abs_spec.rb | 31 ----------- spec/unit/parser/functions/bool2num_spec.rb | 31 ----------- spec/unit/parser/functions/capitalize_spec.rb | 26 --------- spec/unit/parser/functions/chomp_spec.rb | 26 --------- spec/unit/parser/functions/chop_spec.rb | 26 --------- spec/unit/parser/functions/delete_at_spec.rb | 26 --------- spec/unit/parser/functions/delete_spec.rb | 26 --------- spec/unit/parser/functions/downcase_spec.rb | 31 ----------- spec/unit/parser/functions/empty_spec.rb | 31 ----------- spec/unit/parser/functions/flatten_spec.rb | 31 ----------- spec/unit/parser/functions/grep_spec.rb | 26 --------- spec/unit/parser/functions/hash_spec.rb | 26 --------- spec/unit/parser/functions/is_array_spec.rb | 36 ------------- spec/unit/parser/functions/is_domain_name_spec.rb | 56 ------------------- spec/unit/parser/functions/is_float_spec.rb | 36 ------------- spec/unit/parser/functions/is_hash_spec.rb | 36 ------------- spec/unit/parser/functions/is_integer_spec.rb | 36 ------------- spec/unit/parser/functions/is_ip_address_spec.rb | 45 ---------------- spec/unit/parser/functions/is_mac_address_spec.rb | 36 ------------- spec/unit/parser/functions/is_numeric_spec.rb | 36 ------------- spec/unit/parser/functions/is_string_spec.rb | 41 -------------- spec/unit/parser/functions/join_spec.rb | 26 --------- spec/unit/parser/functions/keys_spec.rb | 26 --------- spec/unit/parser/functions/kwalify_spec.rb | 62 ---------------------- spec/unit/parser/functions/lstrip_spec.rb | 26 --------- spec/unit/parser/functions/member_spec.rb | 31 ----------- spec/unit/parser/functions/num2bool_spec.rb | 31 ----------- spec/unit/parser/functions/parsejson_spec.rb | 29 ---------- spec/unit/parser/functions/parseyaml_spec.rb | 31 ----------- spec/unit/parser/functions/prefix_spec.rb | 26 --------- spec/unit/parser/functions/range_spec.rb | 31 ----------- spec/unit/parser/functions/reverse_spec.rb | 26 --------- spec/unit/parser/functions/rstrip_spec.rb | 31 ----------- spec/unit/parser/functions/shuffle_spec.rb | 31 ----------- spec/unit/parser/functions/size_spec.rb | 31 ----------- spec/unit/parser/functions/sort_spec.rb | 31 ----------- spec/unit/parser/functions/squeeze_spec.rb | 31 ----------- spec/unit/parser/functions/str2bool_spec.rb | 31 ----------- spec/unit/parser/functions/strftime_spec.rb | 36 ------------- spec/unit/parser/functions/strip_spec.rb | 26 --------- spec/unit/parser/functions/swapcase_spec.rb | 26 --------- spec/unit/parser/functions/time_spec.rb | 36 ------------- spec/unit/parser/functions/type_spec.rb | 51 ------------------ spec/unit/parser/functions/unique_spec.rb | 31 ----------- spec/unit/parser/functions/upcase_spec.rb | 31 ----------- spec/unit/parser/functions/values_at_spec.rb | 45 ---------------- spec/unit/parser/functions/values_spec.rb | 30 ----------- spec/unit/parser/functions/zip_spec.rb | 26 --------- spec/unit/puppet/parser/functions/abs_spec.rb | 31 +++++++++++ spec/unit/puppet/parser/functions/bool2num_spec.rb | 31 +++++++++++ .../puppet/parser/functions/capitalize_spec.rb | 26 +++++++++ spec/unit/puppet/parser/functions/chomp_spec.rb | 26 +++++++++ spec/unit/puppet/parser/functions/chop_spec.rb | 26 +++++++++ .../unit/puppet/parser/functions/delete_at_spec.rb | 26 +++++++++ spec/unit/puppet/parser/functions/delete_spec.rb | 26 +++++++++ spec/unit/puppet/parser/functions/downcase_spec.rb | 31 +++++++++++ spec/unit/puppet/parser/functions/empty_spec.rb | 31 +++++++++++ spec/unit/puppet/parser/functions/flatten_spec.rb | 31 +++++++++++ spec/unit/puppet/parser/functions/grep_spec.rb | 26 +++++++++ spec/unit/puppet/parser/functions/hash_spec.rb | 26 +++++++++ spec/unit/puppet/parser/functions/is_array_spec.rb | 36 +++++++++++++ .../puppet/parser/functions/is_domain_name_spec.rb | 56 +++++++++++++++++++ spec/unit/puppet/parser/functions/is_float_spec.rb | 36 +++++++++++++ spec/unit/puppet/parser/functions/is_hash_spec.rb | 36 +++++++++++++ .../puppet/parser/functions/is_integer_spec.rb | 36 +++++++++++++ .../puppet/parser/functions/is_ip_address_spec.rb | 45 ++++++++++++++++ .../puppet/parser/functions/is_mac_address_spec.rb | 36 +++++++++++++ .../puppet/parser/functions/is_numeric_spec.rb | 36 +++++++++++++ .../unit/puppet/parser/functions/is_string_spec.rb | 41 ++++++++++++++ spec/unit/puppet/parser/functions/join_spec.rb | 26 +++++++++ spec/unit/puppet/parser/functions/keys_spec.rb | 26 +++++++++ spec/unit/puppet/parser/functions/lstrip_spec.rb | 26 +++++++++ spec/unit/puppet/parser/functions/member_spec.rb | 31 +++++++++++ spec/unit/puppet/parser/functions/num2bool_spec.rb | 31 +++++++++++ .../unit/puppet/parser/functions/parsejson_spec.rb | 29 ++++++++++ .../unit/puppet/parser/functions/parseyaml_spec.rb | 31 +++++++++++ spec/unit/puppet/parser/functions/prefix_spec.rb | 26 +++++++++ spec/unit/puppet/parser/functions/range_spec.rb | 31 +++++++++++ spec/unit/puppet/parser/functions/reverse_spec.rb | 26 +++++++++ spec/unit/puppet/parser/functions/rstrip_spec.rb | 31 +++++++++++ spec/unit/puppet/parser/functions/shuffle_spec.rb | 31 +++++++++++ spec/unit/puppet/parser/functions/size_spec.rb | 31 +++++++++++ spec/unit/puppet/parser/functions/sort_spec.rb | 31 +++++++++++ spec/unit/puppet/parser/functions/squeeze_spec.rb | 31 +++++++++++ spec/unit/puppet/parser/functions/str2bool_spec.rb | 31 +++++++++++ spec/unit/puppet/parser/functions/strftime_spec.rb | 36 +++++++++++++ spec/unit/puppet/parser/functions/strip_spec.rb | 26 +++++++++ spec/unit/puppet/parser/functions/swapcase_spec.rb | 26 +++++++++ spec/unit/puppet/parser/functions/time_spec.rb | 36 +++++++++++++ spec/unit/puppet/parser/functions/type_spec.rb | 51 ++++++++++++++++++ spec/unit/puppet/parser/functions/unique_spec.rb | 31 +++++++++++ spec/unit/puppet/parser/functions/upcase_spec.rb | 31 +++++++++++ .../unit/puppet/parser/functions/values_at_spec.rb | 45 ++++++++++++++++ spec/unit/puppet/parser/functions/values_spec.rb | 30 +++++++++++ spec/unit/puppet/parser/functions/zip_spec.rb | 26 +++++++++ 104 files changed, 1503 insertions(+), 1804 deletions(-) delete mode 100644 examples/kwalify-1.pp delete mode 100644 examples/kwalify-2.pp delete mode 100644 examples/validate_resource-1.pp delete mode 100644 examples/validate_resource-1.schema delete mode 100644 examples/validate_resource-2.pp delete mode 100644 examples/validate_resource-2.schema delete mode 100644 lib/puppet/parser/functions/kwalify.rb delete mode 100644 lib/puppet/parser/functions/validate_resource.rb delete mode 100755 spec/unit/parser/functions/abs_spec.rb delete mode 100755 spec/unit/parser/functions/bool2num_spec.rb delete mode 100755 spec/unit/parser/functions/capitalize_spec.rb delete mode 100755 spec/unit/parser/functions/chomp_spec.rb delete mode 100755 spec/unit/parser/functions/chop_spec.rb delete mode 100755 spec/unit/parser/functions/delete_at_spec.rb delete mode 100755 spec/unit/parser/functions/delete_spec.rb delete mode 100755 spec/unit/parser/functions/downcase_spec.rb delete mode 100755 spec/unit/parser/functions/empty_spec.rb delete mode 100755 spec/unit/parser/functions/flatten_spec.rb delete mode 100755 spec/unit/parser/functions/grep_spec.rb delete mode 100644 spec/unit/parser/functions/hash_spec.rb delete mode 100644 spec/unit/parser/functions/is_array_spec.rb delete mode 100644 spec/unit/parser/functions/is_domain_name_spec.rb delete mode 100644 spec/unit/parser/functions/is_float_spec.rb delete mode 100644 spec/unit/parser/functions/is_hash_spec.rb delete mode 100644 spec/unit/parser/functions/is_integer_spec.rb delete mode 100644 spec/unit/parser/functions/is_ip_address_spec.rb delete mode 100644 spec/unit/parser/functions/is_mac_address_spec.rb delete mode 100644 spec/unit/parser/functions/is_numeric_spec.rb delete mode 100644 spec/unit/parser/functions/is_string_spec.rb delete mode 100644 spec/unit/parser/functions/join_spec.rb delete mode 100644 spec/unit/parser/functions/keys_spec.rb delete mode 100755 spec/unit/parser/functions/kwalify_spec.rb delete mode 100644 spec/unit/parser/functions/lstrip_spec.rb delete mode 100644 spec/unit/parser/functions/member_spec.rb delete mode 100644 spec/unit/parser/functions/num2bool_spec.rb delete mode 100644 spec/unit/parser/functions/parsejson_spec.rb delete mode 100644 spec/unit/parser/functions/parseyaml_spec.rb delete mode 100644 spec/unit/parser/functions/prefix_spec.rb delete mode 100644 spec/unit/parser/functions/range_spec.rb delete mode 100644 spec/unit/parser/functions/reverse_spec.rb delete mode 100644 spec/unit/parser/functions/rstrip_spec.rb delete mode 100644 spec/unit/parser/functions/shuffle_spec.rb delete mode 100644 spec/unit/parser/functions/size_spec.rb delete mode 100644 spec/unit/parser/functions/sort_spec.rb delete mode 100644 spec/unit/parser/functions/squeeze_spec.rb delete mode 100644 spec/unit/parser/functions/str2bool_spec.rb delete mode 100644 spec/unit/parser/functions/strftime_spec.rb delete mode 100644 spec/unit/parser/functions/strip_spec.rb delete mode 100644 spec/unit/parser/functions/swapcase_spec.rb delete mode 100644 spec/unit/parser/functions/time_spec.rb delete mode 100644 spec/unit/parser/functions/type_spec.rb delete mode 100644 spec/unit/parser/functions/unique_spec.rb delete mode 100644 spec/unit/parser/functions/upcase_spec.rb delete mode 100644 spec/unit/parser/functions/values_at_spec.rb delete mode 100644 spec/unit/parser/functions/values_spec.rb delete mode 100644 spec/unit/parser/functions/zip_spec.rb create mode 100755 spec/unit/puppet/parser/functions/abs_spec.rb create mode 100755 spec/unit/puppet/parser/functions/bool2num_spec.rb create mode 100755 spec/unit/puppet/parser/functions/capitalize_spec.rb create mode 100755 spec/unit/puppet/parser/functions/chomp_spec.rb create mode 100755 spec/unit/puppet/parser/functions/chop_spec.rb create mode 100755 spec/unit/puppet/parser/functions/delete_at_spec.rb create mode 100755 spec/unit/puppet/parser/functions/delete_spec.rb create mode 100755 spec/unit/puppet/parser/functions/downcase_spec.rb create mode 100755 spec/unit/puppet/parser/functions/empty_spec.rb create mode 100755 spec/unit/puppet/parser/functions/flatten_spec.rb create mode 100755 spec/unit/puppet/parser/functions/grep_spec.rb create mode 100644 spec/unit/puppet/parser/functions/hash_spec.rb create mode 100644 spec/unit/puppet/parser/functions/is_array_spec.rb create mode 100644 spec/unit/puppet/parser/functions/is_domain_name_spec.rb create mode 100644 spec/unit/puppet/parser/functions/is_float_spec.rb create mode 100644 spec/unit/puppet/parser/functions/is_hash_spec.rb create mode 100644 spec/unit/puppet/parser/functions/is_integer_spec.rb create mode 100644 spec/unit/puppet/parser/functions/is_ip_address_spec.rb create mode 100644 spec/unit/puppet/parser/functions/is_mac_address_spec.rb create mode 100644 spec/unit/puppet/parser/functions/is_numeric_spec.rb create mode 100644 spec/unit/puppet/parser/functions/is_string_spec.rb create mode 100644 spec/unit/puppet/parser/functions/join_spec.rb create mode 100644 spec/unit/puppet/parser/functions/keys_spec.rb create mode 100644 spec/unit/puppet/parser/functions/lstrip_spec.rb create mode 100644 spec/unit/puppet/parser/functions/member_spec.rb create mode 100644 spec/unit/puppet/parser/functions/num2bool_spec.rb create mode 100644 spec/unit/puppet/parser/functions/parsejson_spec.rb create mode 100644 spec/unit/puppet/parser/functions/parseyaml_spec.rb create mode 100644 spec/unit/puppet/parser/functions/prefix_spec.rb create mode 100644 spec/unit/puppet/parser/functions/range_spec.rb create mode 100644 spec/unit/puppet/parser/functions/reverse_spec.rb create mode 100644 spec/unit/puppet/parser/functions/rstrip_spec.rb create mode 100644 spec/unit/puppet/parser/functions/shuffle_spec.rb create mode 100644 spec/unit/puppet/parser/functions/size_spec.rb create mode 100644 spec/unit/puppet/parser/functions/sort_spec.rb create mode 100644 spec/unit/puppet/parser/functions/squeeze_spec.rb create mode 100644 spec/unit/puppet/parser/functions/str2bool_spec.rb create mode 100644 spec/unit/puppet/parser/functions/strftime_spec.rb create mode 100644 spec/unit/puppet/parser/functions/strip_spec.rb create mode 100644 spec/unit/puppet/parser/functions/swapcase_spec.rb create mode 100644 spec/unit/puppet/parser/functions/time_spec.rb create mode 100644 spec/unit/puppet/parser/functions/type_spec.rb create mode 100644 spec/unit/puppet/parser/functions/unique_spec.rb create mode 100644 spec/unit/puppet/parser/functions/upcase_spec.rb create mode 100644 spec/unit/puppet/parser/functions/values_at_spec.rb create mode 100644 spec/unit/puppet/parser/functions/values_spec.rb create mode 100644 spec/unit/puppet/parser/functions/zip_spec.rb diff --git a/README.markdown b/README.markdown index 68e559e..a15338f 100644 --- a/README.markdown +++ b/README.markdown @@ -20,29 +20,4 @@ Depending on the version of Puppet, you may need to restart the puppetmasterd (o ## Functions -### kwalify - -This function allows you to validate Puppet data structures using Kwalify -schemas as documented here: - -http://www.kuwata-lab.com/kwalify/ruby/users-guide.01.html - -To validate, create a schema in Puppet: - - $schema = { - 'type' => 'seq', - 'sequence' => [ - { 'type' => 'str' } - ] - } - -And create some content that you want validated: - - $document = ['a', 'b', 'c'] - -And then use the function to validate: - - kwalify($schema, $document) - -The function will throw an error and list all validation errors if there is a -problem otherwise it succeeds silently. +TODO diff --git a/examples/kwalify-1.pp b/examples/kwalify-1.pp deleted file mode 100644 index 852e46c..0000000 --- a/examples/kwalify-1.pp +++ /dev/null @@ -1,9 +0,0 @@ -$schema = { - 'type' => 'seq', - 'sequence' => [ - { 'type' => 'str', 'enum' => ['asdf','fdsa'] } - ] -} -$document = ['a', 'b', 'c'] - -kwalify($schema, $document) diff --git a/examples/kwalify-2.pp b/examples/kwalify-2.pp deleted file mode 100644 index 3f4ec33..0000000 --- a/examples/kwalify-2.pp +++ /dev/null @@ -1,24 +0,0 @@ -$schema = { - 'type' => 'map', - 'mapping' => { - 'name' => { - 'type' => 'str', - 'required' => true, - }, - 'email' => { - 'type' => 'str', - 'pattern' => '/@/', - }, - 'age' => { - 'type' => 'str', - 'pattern' => '/^\d+$/', - }, - } -} -$document = { - 'name' => 'foo', - 'email' => 'foo@mail.com', - 'age' => 20, -} - -kwalify($schema, $document) diff --git a/examples/validate_resource-1.pp b/examples/validate_resource-1.pp deleted file mode 100644 index c701b8d..0000000 --- a/examples/validate_resource-1.pp +++ /dev/null @@ -1,23 +0,0 @@ -define fooresource( - $color, - $type, - $somenumber, - $map - ) { - - validate_resource() - - # ... do something ... - -} - -fooresource { "example1": - color => "blue", - type => "circle", - somenumber => 5, - map => { - a => 1, - b => 2, - c => 3, - } -} diff --git a/examples/validate_resource-1.schema b/examples/validate_resource-1.schema deleted file mode 100644 index c540db5..0000000 --- a/examples/validate_resource-1.schema +++ /dev/null @@ -1,39 +0,0 @@ -type: map -mapping: - "title": - type: str - required: yes - "name": - type: str - required: yes - "caller_module_name": - type: str - required: yes - "module_name": - type: str - required: yes - "color": - type: str - required: yes - "type": - type: str - required: yes - "somenumber": - type: str - required: yes - pattern: /^\d+$/ - "map": - type: map - mapping: - "a": - type: str - required: yes - pattern: /^\d+$/ - "b": - type: str - required: yes - pattern: /^\d+$/ - "c": - type: str - required: yes - pattern: /^\d+$/ diff --git a/examples/validate_resource-2.pp b/examples/validate_resource-2.pp deleted file mode 100644 index b53b109..0000000 --- a/examples/validate_resource-2.pp +++ /dev/null @@ -1,17 +0,0 @@ -class foo ( - $a, - $b, - $c - ) { - - validate_resource() - - # ... do something ... - -} - -class { "foo": - a => "1", - b => "foobaz", - c => ['a','b','c'] -} diff --git a/examples/validate_resource-2.schema b/examples/validate_resource-2.schema deleted file mode 100644 index b516945..0000000 --- a/examples/validate_resource-2.schema +++ /dev/null @@ -1,26 +0,0 @@ -type: map -mapping: - "title": - type: str - required: yes - "name": - type: str - required: yes - "caller_module_name": - type: str - required: yes - "module_name": - type: str - required: yes - "a": - type: str - required: yes - "b": - type: str - required: yes - pattern: /^foo/ - "c": - type: seq - required: yes - sequence: - - type: str diff --git a/lib/puppet/parser/functions/kwalify.rb b/lib/puppet/parser/functions/kwalify.rb deleted file mode 100644 index 49b9aeb..0000000 --- a/lib/puppet/parser/functions/kwalify.rb +++ /dev/null @@ -1,35 +0,0 @@ -# -# kwalify.rb -# - -module Puppet::Parser::Functions - newfunction(:kwalify, :type => :statement, :doc => <<-EOS -This function uses kwalify to validate Puppet data structures against Kwalify -schemas. - EOS - ) do |args| - - raise(Puppet::ParseError, "kwalify(): Wrong number of arguments " + - "given (#{args.size} for 2)") if args.size != 2 - - schema = args[0] - document = args[1] - - require 'kwalify' - - validator = Kwalify::Validator.new(schema) - - errors = validator.validate(document) - - if errors && !errors.empty? - error_out = [] - for e in errors - error_out << "[#{e.path}] #{e.message}" - end - raise(Puppet::ParseError, "Failed kwalify schema validation:\n" + error_out.join("\n")) - end - - end -end - -# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/validate_resource.rb b/lib/puppet/parser/functions/validate_resource.rb deleted file mode 100644 index d71ef3f..0000000 --- a/lib/puppet/parser/functions/validate_resource.rb +++ /dev/null @@ -1,41 +0,0 @@ -# -# validate_resource -# - -module Puppet::Parser::Functions - newfunction(:validate_resource, :type => :statement, :doc => <<-EOS -This function when placed at the beginning of a class, will go looking for a -valid kwalify schema by replacing the extension of the file with '.schema'. - -It will then validate the arguments passed to the function using that kwalify -schema. - EOS - ) do |arguments| - - require 'kwalify' - - if (arguments.size != 0) then - raise(Puppet::ParseError, "validate_resource(): Wrong number of arguments "+ - "given #{arguments.size} for 0") - end - - - classhash = to_hash(recursive=false) - sourcepath = source.file - schemapath = sourcepath.gsub(/\.(rb|pp)$/, ".schema") - schema = Kwalify::Yaml.load_file(schemapath) - validator = Kwalify::Validator.new(schema) - errors = validator.validate(classhash) - - if errors && !errors.empty? - error_output = "Resource validation failed:\n" - for e in errors - error_output += "[#{e.path}] #{e.message}\n" - end - raise(Puppet::ParseError, error_output) - end - - end -end - -# vim: set ts=2 sw=2 et : diff --git a/spec/unit/parser/functions/abs_spec.rb b/spec/unit/parser/functions/abs_spec.rb deleted file mode 100755 index 65ba2e8..0000000 --- a/spec/unit/parser/functions/abs_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the abs function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("abs").should == "function_abs" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_abs([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should convert a negative number into a positive" do - result = @scope.function_abs(["-34"]) - result.should(eq(34)) - end - - it "should do nothing with a positive number" do - result = @scope.function_abs(["5678"]) - result.should(eq(5678)) - end - -end diff --git a/spec/unit/parser/functions/bool2num_spec.rb b/spec/unit/parser/functions/bool2num_spec.rb deleted file mode 100755 index d5da18c..0000000 --- a/spec/unit/parser/functions/bool2num_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the bool2num function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("bool2num").should == "function_bool2num" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_bool2num([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should convert true to 1" do - result = @scope.function_bool2num([true]) - result.should(eq(1)) - end - - it "should convert false to 0" do - result = @scope.function_bool2num([false]) - result.should(eq(0)) - end - -end diff --git a/spec/unit/parser/functions/capitalize_spec.rb b/spec/unit/parser/functions/capitalize_spec.rb deleted file mode 100755 index 1c45821..0000000 --- a/spec/unit/parser/functions/capitalize_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the capitalize function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("capitalize").should == "function_capitalize" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_capitalize([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should capitalize the beginning of a string" do - result = @scope.function_capitalize(["abc"]) - result.should(eq("Abc")) - end - -end diff --git a/spec/unit/parser/functions/chomp_spec.rb b/spec/unit/parser/functions/chomp_spec.rb deleted file mode 100755 index 0592115..0000000 --- a/spec/unit/parser/functions/chomp_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the chomp function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("chomp").should == "function_chomp" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_chomp([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should chomp the end of a string" do - result = @scope.function_chomp(["abc\n"]) - result.should(eq("abc")) - end - -end diff --git a/spec/unit/parser/functions/chop_spec.rb b/spec/unit/parser/functions/chop_spec.rb deleted file mode 100755 index 0c456a8..0000000 --- a/spec/unit/parser/functions/chop_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the chop function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("chop").should == "function_chop" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_chop([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should chop the end of a string" do - result = @scope.function_chop(["asdf\n"]) - result.should(eq("asdf")) - end - -end diff --git a/spec/unit/parser/functions/delete_at_spec.rb b/spec/unit/parser/functions/delete_at_spec.rb deleted file mode 100755 index 27db0c8..0000000 --- a/spec/unit/parser/functions/delete_at_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the delete_at function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("delete_at").should == "function_delete_at" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_delete_at([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should delete an item at specified location from an array" do - result = @scope.function_delete_at([['a','b','c'],1]) - result.should(eq(['a','c'])) - end - -end diff --git a/spec/unit/parser/functions/delete_spec.rb b/spec/unit/parser/functions/delete_spec.rb deleted file mode 100755 index fab3230..0000000 --- a/spec/unit/parser/functions/delete_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the delete function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("delete").should == "function_delete" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_delete([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should delete an item from an array" do - result = @scope.function_delete([['a','b','c'],'b']) - result.should(eq(['a','c'])) - end - -end diff --git a/spec/unit/parser/functions/downcase_spec.rb b/spec/unit/parser/functions/downcase_spec.rb deleted file mode 100755 index 0bccd5f..0000000 --- a/spec/unit/parser/functions/downcase_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the downcase function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("downcase").should == "function_downcase" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_downcase([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should downcase a string" do - result = @scope.function_downcase(["ASFD"]) - result.should(eq("asfd")) - end - - it "should do nothing to a string that is already downcase" do - result = @scope.function_downcase(["asdf asdf"]) - result.should(eq("asdf asdf")) - end - -end diff --git a/spec/unit/parser/functions/empty_spec.rb b/spec/unit/parser/functions/empty_spec.rb deleted file mode 100755 index cb0021f..0000000 --- a/spec/unit/parser/functions/empty_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the empty function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("empty").should == "function_empty" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_empty([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return a true for an empty string" do - result = @scope.function_empty(['']) - result.should(eq(true)) - end - - it "should return a false for a non-empty string" do - result = @scope.function_empty(['asdf']) - result.should(eq(false)) - end - -end diff --git a/spec/unit/parser/functions/flatten_spec.rb b/spec/unit/parser/functions/flatten_spec.rb deleted file mode 100755 index 7bedeb2..0000000 --- a/spec/unit/parser/functions/flatten_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the flatten function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("flatten").should == "function_flatten" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_flatten([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should flatten a complex data structure" do - result = @scope.function_flatten([["a","b",["c",["d","e"],"f","g"]]]) - result.should(eq(["a","b","c","d","e","f","g"])) - end - - it "should do nothing to a structure that is already flat" do - result = @scope.function_flatten([["a","b","c","d"]]) - result.should(eq(["a","b","c","d"])) - end - -end diff --git a/spec/unit/parser/functions/grep_spec.rb b/spec/unit/parser/functions/grep_spec.rb deleted file mode 100755 index b1f647c..0000000 --- a/spec/unit/parser/functions/grep_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the grep function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("grep").should == "function_grep" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_grep([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should grep contents from an array" do - result = @scope.function_grep([["aaabbb","bbbccc","dddeee"], "bbb"]) - result.should(eq(["aaabbb","bbbccc"])) - end - -end diff --git a/spec/unit/parser/functions/hash_spec.rb b/spec/unit/parser/functions/hash_spec.rb deleted file mode 100644 index 6d3d48c..0000000 --- a/spec/unit/parser/functions/hash_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the hash function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("hash").should == "function_hash" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_hash([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should convert an array to a hash" do - result = @scope.function_hash([['a',1,'b',2,'c',3]]) - result.should(eq({'a'=>1,'b'=>2,'c'=>3})) - end - -end diff --git a/spec/unit/parser/functions/is_array_spec.rb b/spec/unit/parser/functions/is_array_spec.rb deleted file mode 100644 index 537595c..0000000 --- a/spec/unit/parser/functions/is_array_spec.rb +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the is_array function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("is_array").should == "function_is_array" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_is_array([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return true if passed an array" do - result = @scope.function_is_array([[1,2,3]]) - result.should(eq(true)) - end - - it "should return false if passed a hash" do - result = @scope.function_is_array([{'a'=>1}]) - result.should(eq(false)) - end - - it "should return false if passed a string" do - result = @scope.function_is_array(["asdf"]) - result.should(eq(false)) - end - -end diff --git a/spec/unit/parser/functions/is_domain_name_spec.rb b/spec/unit/parser/functions/is_domain_name_spec.rb deleted file mode 100644 index ec7c7f5..0000000 --- a/spec/unit/parser/functions/is_domain_name_spec.rb +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the is_domain_name function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("is_domain_name").should == "function_is_domain_name" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_is_domain_name([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return true if a valid domain name" do - result = @scope.function_is_domain_name(["foo.bar.com"]) - result.should(be_true) - end - - it "should allow domain parts to start with numbers" do - result = @scope.function_is_domain_name(["3foo.2bar.com"]) - result.should(be_true) - end - - it "should allow domain to end with a dot" do - result = @scope.function_is_domain_name(["3foo.2bar.com."]) - result.should(be_true) - end - - it "should allow a single part domain" do - result = @scope.function_is_domain_name(["orange"]) - result.should(be_true) - end - - it "should return false if domain parts start with hyphens" do - result = @scope.function_is_domain_name(["-3foo.2bar.com"]) - result.should(be_false) - end - - it "should return true if domain contains hyphens" do - result = @scope.function_is_domain_name(["3foo-bar.2bar-fuzz.com"]) - result.should(be_true) - end - - it "should return false if domain name contains spaces" do - result = @scope.function_is_domain_name(["not valid"]) - result.should(be_false) - end - -end diff --git a/spec/unit/parser/functions/is_float_spec.rb b/spec/unit/parser/functions/is_float_spec.rb deleted file mode 100644 index 55ba8cf..0000000 --- a/spec/unit/parser/functions/is_float_spec.rb +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the is_float function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("is_float").should == "function_is_float" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_is_float([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return true if a float" do - result = @scope.function_is_float(["0.12"]) - result.should(eq(true)) - end - - it "should return false if a string" do - result = @scope.function_is_float(["asdf"]) - result.should(eq(false)) - end - - it "should return false if an integer" do - result = @scope.function_is_float(["3"]) - result.should(eq(false)) - end - -end diff --git a/spec/unit/parser/functions/is_hash_spec.rb b/spec/unit/parser/functions/is_hash_spec.rb deleted file mode 100644 index 94364f5..0000000 --- a/spec/unit/parser/functions/is_hash_spec.rb +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the is_hash function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("is_hash").should == "function_is_hash" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_is_hash([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return true if passed a hash" do - result = @scope.function_is_hash([{"a"=>1,"b"=>2}]) - result.should(eq(true)) - end - - it "should return false if passed an array" do - result = @scope.function_is_hash([["a","b"]]) - result.should(eq(false)) - end - - it "should return false if passed a string" do - result = @scope.function_is_hash(["asdf"]) - result.should(eq(false)) - end - -end diff --git a/spec/unit/parser/functions/is_integer_spec.rb b/spec/unit/parser/functions/is_integer_spec.rb deleted file mode 100644 index faf6f2d..0000000 --- a/spec/unit/parser/functions/is_integer_spec.rb +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the is_integer function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("is_integer").should == "function_is_integer" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_is_integer([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return true if an integer" do - result = @scope.function_is_integer(["3"]) - result.should(eq(true)) - end - - it "should return false if a float" do - result = @scope.function_is_integer(["3.2"]) - result.should(eq(false)) - end - - it "should return false if a string" do - result = @scope.function_is_integer(["asdf"]) - result.should(eq(false)) - end - -end diff --git a/spec/unit/parser/functions/is_ip_address_spec.rb b/spec/unit/parser/functions/is_ip_address_spec.rb deleted file mode 100644 index 98ce828..0000000 --- a/spec/unit/parser/functions/is_ip_address_spec.rb +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the is_ip_address function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("is_ip_address").should == "function_is_ip_address" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_is_ip_address([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return true if an IPv4 address" do - result = @scope.function_is_ip_address(["1.2.3.4"]) - result.should(eq(true)) - end - - it "should return true if a full IPv6 address" do - result = @scope.function_is_ip_address(["fe80:0000:cd12:d123:e2f8:47ff:fe09:dd74"]) - result.should(eq(true)) - end - - it "should return true if a compressed IPv6 address" do - result = @scope.function_is_ip_address(["fe00::1"]) - result.should(eq(true)) - end - - it "should return false if not valid" do - result = @scope.function_is_ip_address(["asdf"]) - result.should(eq(false)) - end - - it "should return false if IP octets out of range" do - result = @scope.function_is_ip_address(["1.1.1.300"]) - result.should(eq(false)) - end -end diff --git a/spec/unit/parser/functions/is_mac_address_spec.rb b/spec/unit/parser/functions/is_mac_address_spec.rb deleted file mode 100644 index c9b9637..0000000 --- a/spec/unit/parser/functions/is_mac_address_spec.rb +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the is_mac_address function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("is_mac_address").should == "function_is_mac_address" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_is_mac_address([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return true if a valid mac address" do - result = @scope.function_is_mac_address(["00:a0:1f:12:7f:a0"]) - result.should(eq(true)) - end - - it "should return false if octets are out of range" do - result = @scope.function_is_mac_address(["00:a0:1f:12:7f:g0"]) - result.should(eq(false)) - end - - it "should return false if not valid" do - result = @scope.function_is_mac_address(["not valid"]) - result.should(eq(false)) - end - -end diff --git a/spec/unit/parser/functions/is_numeric_spec.rb b/spec/unit/parser/functions/is_numeric_spec.rb deleted file mode 100644 index 2191b7b..0000000 --- a/spec/unit/parser/functions/is_numeric_spec.rb +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the is_numeric function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("is_numeric").should == "function_is_numeric" - end - - it "should raise a ParseError if there is less than 1 argument" do - lambda { @scope.function_is_numeric([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return true if an integer" do - result = @scope.function_is_numeric(["3"]) - result.should(eq(true)) - end - - it "should return true if a float" do - result = @scope.function_is_numeric(["3.2"]) - result.should(eq(true)) - end - - it "should return false if a string" do - result = @scope.function_is_numeric(["asdf"]) - result.should(eq(false)) - end - -end diff --git a/spec/unit/parser/functions/is_string_spec.rb b/spec/unit/parser/functions/is_string_spec.rb deleted file mode 100644 index 4f3f5fd..0000000 --- a/spec/unit/parser/functions/is_string_spec.rb +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the is_string function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("is_string").should == "function_is_string" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_is_string([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return true if a string" do - result = @scope.function_is_string(["asdf"]) - result.should(eq(true)) - end - - it "should return false if an integer" do - result = @scope.function_is_string(["3"]) - result.should(eq(false)) - end - - it "should return false if a float" do - result = @scope.function_is_string(["3.23"]) - result.should(eq(false)) - end - - it "should return false if an array" do - result = @scope.function_is_string([["a","b","c"]]) - result.should(eq(false)) - end - -end diff --git a/spec/unit/parser/functions/join_spec.rb b/spec/unit/parser/functions/join_spec.rb deleted file mode 100644 index 1b3dec8..0000000 --- a/spec/unit/parser/functions/join_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the join function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("join").should == "function_join" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_join([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should join an array into a string" do - result = @scope.function_join([["a","b","c"], ":"]) - result.should(eq("a:b:c")) - end - -end diff --git a/spec/unit/parser/functions/keys_spec.rb b/spec/unit/parser/functions/keys_spec.rb deleted file mode 100644 index 927be96..0000000 --- a/spec/unit/parser/functions/keys_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the keys function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("keys").should == "function_keys" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_keys([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return an array of keys when given a hash" do - result = @scope.function_keys([{'a'=>1, 'b' => 2}]) - result.should(eq(['a','b'])) - end - -end diff --git a/spec/unit/parser/functions/kwalify_spec.rb b/spec/unit/parser/functions/kwalify_spec.rb deleted file mode 100755 index abdd529..0000000 --- a/spec/unit/parser/functions/kwalify_spec.rb +++ /dev/null @@ -1,62 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the kwalify function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("kwalify").should == "function_kwalify" - end - - it "should raise a ParseError if there is less than 2 arguments" do - lambda { @scope.function_kwalify([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should validate a simple array schema" do - schema = { - 'type' => 'seq', - 'sequence' => [ - { 'type' => 'str' } - ] - } - document = ['a','b','c'] - @scope.function_kwalify([schema, document]) - end - - it "should not validate a simple array schema when invalid" do - schema = { - 'type' => 'seq', - 'sequence' => [ - { 'type' => 'str' } - ] - } - document = ['a','b',{'a' => 'b'}] - lambda { @scope.function_kwalify([schema, document]) }.should(raise_error(Puppet::ParseError)) - end - - it "should validate a hash schema" do - schema = { - 'type' => 'map', - 'mapping' => { - 'key1' => { - 'type' => 'str', - }, - 'key2' => { - 'type' => 'str', - }, - } - } - document = { - 'key1' => 'b', - 'key2' => 'c', - } - @scope.function_kwalify([schema, document]) - end - -end diff --git a/spec/unit/parser/functions/lstrip_spec.rb b/spec/unit/parser/functions/lstrip_spec.rb deleted file mode 100644 index ac331fa..0000000 --- a/spec/unit/parser/functions/lstrip_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the lstrip function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("lstrip").should == "function_lstrip" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_lstrip([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should lstrip a string" do - result = @scope.function_lstrip([" asdf"]) - result.should(eq('asdf')) - end - -end diff --git a/spec/unit/parser/functions/member_spec.rb b/spec/unit/parser/functions/member_spec.rb deleted file mode 100644 index 2cebc0d..0000000 --- a/spec/unit/parser/functions/member_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the member function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("member").should == "function_member" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_member([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return true if a member is in an array" do - result = @scope.function_member([["a","b","c"], "a"]) - result.should(eq(true)) - end - - it "should return false if a member is not in an array" do - result = @scope.function_member([["a","b","c"], "d"]) - result.should(eq(false)) - end - -end diff --git a/spec/unit/parser/functions/num2bool_spec.rb b/spec/unit/parser/functions/num2bool_spec.rb deleted file mode 100644 index 6585273..0000000 --- a/spec/unit/parser/functions/num2bool_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the num2bool function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("num2bool").should == "function_num2bool" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_num2bool([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return true if 1" do - result = @scope.function_num2bool(["1"]) - result.should(be_true) - end - - it "should return false if 0" do - result = @scope.function_num2bool(["0"]) - result.should(be_false) - end - -end diff --git a/spec/unit/parser/functions/parsejson_spec.rb b/spec/unit/parser/functions/parsejson_spec.rb deleted file mode 100644 index 26eea36..0000000 --- a/spec/unit/parser/functions/parsejson_spec.rb +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the parsejson function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("parsejson").should == "function_parsejson" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_parsejson([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should convert JSON to a data structure" do - json = <<-EOS -["aaa","bbb","ccc"] -EOS - result = @scope.function_parsejson([json]) - result.should(eq(['aaa','bbb','ccc'])) - end - -end diff --git a/spec/unit/parser/functions/parseyaml_spec.rb b/spec/unit/parser/functions/parseyaml_spec.rb deleted file mode 100644 index f9cb049..0000000 --- a/spec/unit/parser/functions/parseyaml_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the parseyaml function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("parseyaml").should == "function_parseyaml" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_parseyaml([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should convert YAML to a data structure" do - yaml = <<-EOS -- aaa -- bbb -- ccc -EOS - result = @scope.function_parseyaml([yaml]) - result.should(eq(['aaa','bbb','ccc'])) - end - -end diff --git a/spec/unit/parser/functions/prefix_spec.rb b/spec/unit/parser/functions/prefix_spec.rb deleted file mode 100644 index a0cbcab..0000000 --- a/spec/unit/parser/functions/prefix_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the prefix function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("prefix").should == "function_prefix" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_prefix([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return a prefixed array" do - result = @scope.function_prefix([['a','b','c'], 'p']) - result.should(eq(['pa','pb','pc'])) - end - -end diff --git a/spec/unit/parser/functions/range_spec.rb b/spec/unit/parser/functions/range_spec.rb deleted file mode 100644 index 8c2446a..0000000 --- a/spec/unit/parser/functions/range_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the range function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("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)) - end - - it "should return a letter range" do - result = @scope.function_range(["a","d"]) - 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])) - end - -end diff --git a/spec/unit/parser/functions/reverse_spec.rb b/spec/unit/parser/functions/reverse_spec.rb deleted file mode 100644 index 4fa50e4..0000000 --- a/spec/unit/parser/functions/reverse_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the reverse function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("reverse").should == "function_reverse" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_reverse([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should reverse a string" do - result = @scope.function_reverse(["asdfghijkl"]) - result.should(eq('lkjihgfdsa')) - end - -end diff --git a/spec/unit/parser/functions/rstrip_spec.rb b/spec/unit/parser/functions/rstrip_spec.rb deleted file mode 100644 index af8cc12..0000000 --- a/spec/unit/parser/functions/rstrip_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the rstrip function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("rstrip").should == "function_rstrip" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_rstrip([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should rstrip a string" do - result = @scope.function_rstrip(["asdf "]) - result.should(eq('asdf')) - end - - it "should rstrip each element in an array" do - result = @scope.function_rstrip([["a ","b ", "c "]]) - result.should(eq(['a','b','c'])) - end - -end diff --git a/spec/unit/parser/functions/shuffle_spec.rb b/spec/unit/parser/functions/shuffle_spec.rb deleted file mode 100644 index f04fda5..0000000 --- a/spec/unit/parser/functions/shuffle_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the shuffle function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("shuffle").should == "function_shuffle" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_shuffle([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should shuffle a string and the result should be the same size" do - result = @scope.function_shuffle(["asdf"]) - result.size.should(eq(4)) - end - - it "should shuffle a string but the sorted contents should still be the same" do - result = @scope.function_shuffle(["adfs"]) - result.split("").sort.join("").should(eq("adfs")) - end - -end diff --git a/spec/unit/parser/functions/size_spec.rb b/spec/unit/parser/functions/size_spec.rb deleted file mode 100644 index ccaa335..0000000 --- a/spec/unit/parser/functions/size_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the size function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("size").should == "function_size" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_size([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return the size of a string" do - result = @scope.function_size(["asdf"]) - result.should(eq(4)) - end - - it "should return the size of an array" do - result = @scope.function_size([["a","b","c"]]) - result.should(eq(3)) - end - -end diff --git a/spec/unit/parser/functions/sort_spec.rb b/spec/unit/parser/functions/sort_spec.rb deleted file mode 100644 index fbe3073..0000000 --- a/spec/unit/parser/functions/sort_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the sort function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("sort").should == "function_sort" - end - - it "should raise a ParseError if there is not 1 arguments" do - lambda { @scope.function_sort(['','']) }.should( raise_error(Puppet::ParseError)) - end - - it "should sort an array" do - result = @scope.function_sort([["a","c","b"]]) - result.should(eq(['a','b','c'])) - end - - it "should sort a string" do - result = @scope.function_sort(["acb"]) - result.should(eq('abc')) - end - -end diff --git a/spec/unit/parser/functions/squeeze_spec.rb b/spec/unit/parser/functions/squeeze_spec.rb deleted file mode 100644 index 9355ad2..0000000 --- a/spec/unit/parser/functions/squeeze_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the squeeze function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("squeeze").should == "function_squeeze" - end - - it "should raise a ParseError if there is less than 2 arguments" do - lambda { @scope.function_squeeze([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should squeeze a string" do - result = @scope.function_squeeze(["aaabbbbcccc"]) - result.should(eq('abc')) - end - - it "should squeeze all elements in an array" do - result = @scope.function_squeeze([["aaabbbbcccc","dddfff"]]) - result.should(eq(['abc','df'])) - end - -end diff --git a/spec/unit/parser/functions/str2bool_spec.rb b/spec/unit/parser/functions/str2bool_spec.rb deleted file mode 100644 index d7f0ac9..0000000 --- a/spec/unit/parser/functions/str2bool_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the str2bool function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("str2bool").should == "function_str2bool" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_str2bool([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should convert string 'true' to true" do - result = @scope.function_str2bool(["true"]) - result.should(eq(true)) - end - - it "should convert string 'undef' to false" do - result = @scope.function_str2bool(["undef"]) - result.should(eq(false)) - end - -end diff --git a/spec/unit/parser/functions/strftime_spec.rb b/spec/unit/parser/functions/strftime_spec.rb deleted file mode 100644 index f7a2cd9..0000000 --- a/spec/unit/parser/functions/strftime_spec.rb +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the strftime function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("strftime").should == "function_strftime" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_strftime([]) }.should( raise_error(Puppet::ParseError)) - end - - it "using %s should be higher then when I wrote this test" do - result = @scope.function_strftime(["%s"]) - result.to_i.should(be > 1311953157) - end - - it "using %s should be lower then 1.5 trillion" do - result = @scope.function_strftime(["%s"]) - result.to_i.should(be < 1500000000) - end - - it "should return a date when given %Y-%m-%d" do - result = @scope.function_strftime(["%Y-%m-%d"]) - result.should =~ /^\d{4}-\d{2}-\d{2}$/ - end - -end diff --git a/spec/unit/parser/functions/strip_spec.rb b/spec/unit/parser/functions/strip_spec.rb deleted file mode 100644 index 48a52dd..0000000 --- a/spec/unit/parser/functions/strip_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the strip function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("strip").should == "function_strip" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_strip([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should strip a string" do - result = @scope.function_strip([" ab cd "]) - result.should(eq('ab cd')) - end - -end diff --git a/spec/unit/parser/functions/swapcase_spec.rb b/spec/unit/parser/functions/swapcase_spec.rb deleted file mode 100644 index 2686054..0000000 --- a/spec/unit/parser/functions/swapcase_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the swapcase function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("swapcase").should == "function_swapcase" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_swapcase([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should swapcase a string" do - result = @scope.function_swapcase(["aaBBccDD"]) - result.should(eq('AAbbCCdd')) - end - -end diff --git a/spec/unit/parser/functions/time_spec.rb b/spec/unit/parser/functions/time_spec.rb deleted file mode 100644 index 666e8e0..0000000 --- a/spec/unit/parser/functions/time_spec.rb +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the time function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("time").should == "function_time" - end - - it "should raise a ParseError if there is more than 2 arguments" do - lambda { @scope.function_time(['','']) }.should( raise_error(Puppet::ParseError)) - end - - it "should return a number" do - result = @scope.function_time([]) - result.class.should(eq(Fixnum)) - end - - it "should be higher then when I wrote this test" do - result = @scope.function_time([]) - result.should(be > 1311953157) - end - - it "should be lower then 1.5 trillion" do - result = @scope.function_time([]) - result.should(be < 1500000000) - end - -end diff --git a/spec/unit/parser/functions/type_spec.rb b/spec/unit/parser/functions/type_spec.rb deleted file mode 100644 index e3c28ed..0000000 --- a/spec/unit/parser/functions/type_spec.rb +++ /dev/null @@ -1,51 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the type function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("type").should == "function_type" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_type([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return string when given a string" do - result = @scope.function_type(["aaabbbbcccc"]) - result.should(eq('string')) - end - - it "should return array when given an array" do - result = @scope.function_type([["aaabbbbcccc","asdf"]]) - result.should(eq('array')) - end - - it "should return hash when given a hash" do - result = @scope.function_type([{"a"=>1,"b"=>2}]) - result.should(eq('hash')) - end - - it "should return integer when given an integer" do - result = @scope.function_type(["1"]) - result.should(eq('integer')) - end - - it "should return float when given a float" do - result = @scope.function_type(["1.34"]) - result.should(eq('float')) - end - - it "should return boolean when given a boolean" do - result = @scope.function_type([true]) - result.should(eq('boolean')) - end - -end diff --git a/spec/unit/parser/functions/unique_spec.rb b/spec/unit/parser/functions/unique_spec.rb deleted file mode 100644 index 627dc33..0000000 --- a/spec/unit/parser/functions/unique_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the unique function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("unique").should == "function_unique" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_unique([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should remove duplicate elements in a string" do - result = @scope.function_unique(["aabbc"]) - result.should(eq('abc')) - end - - it "should remove duplicate elements in an array" do - result = @scope.function_unique([["a","a","b","b","c"]]) - result.should(eq(['a','b','c'])) - end - -end diff --git a/spec/unit/parser/functions/upcase_spec.rb b/spec/unit/parser/functions/upcase_spec.rb deleted file mode 100644 index 5d18846..0000000 --- a/spec/unit/parser/functions/upcase_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the upcase function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("upcase").should == "function_upcase" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_upcase([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should upcase a string" do - result = @scope.function_upcase(["abc"]) - result.should(eq('ABC')) - end - - it "should do nothing if a string is already upcase" do - result = @scope.function_upcase(["ABC"]) - result.should(eq('ABC')) - end - -end diff --git a/spec/unit/parser/functions/values_at_spec.rb b/spec/unit/parser/functions/values_at_spec.rb deleted file mode 100644 index 6c45316..0000000 --- a/spec/unit/parser/functions/values_at_spec.rb +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the values_at function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("values_at").should == "function_values_at" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_values_at([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should raise a ParseError if you try to use a range where stop is greater then start" do - lambda { @scope.function_values_at([['a','b'],["3-1"]]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return a value at from an array" do - result = @scope.function_values_at([['a','b','c'],"1"]) - result.should(eq(['b'])) - end - - it "should return a value at from an array when passed a range" do - result = @scope.function_values_at([['a','b','c'],"0-1"]) - result.should(eq(['a','b'])) - end - - it "should return chosen values from an array when passed number of indexes" do - result = @scope.function_values_at([['a','b','c'],["0","2"]]) - result.should(eq(['a','c'])) - end - - it "should return chosen values from an array when passed ranges and multiple indexes" do - result = @scope.function_values_at([['a','b','c','d','e','f','g'],["0","2","4-5"]]) - result.should(eq(['a','c','e','f'])) - end - -end diff --git a/spec/unit/parser/functions/values_spec.rb b/spec/unit/parser/functions/values_spec.rb deleted file mode 100644 index f6eb5b6..0000000 --- a/spec/unit/parser/functions/values_spec.rb +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the values function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("values").should == "function_values" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_values([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return values from a hash" do - result = @scope.function_values([{'a'=>'1','b'=>'2','c'=>'3'}]) - result.should(eq(['1','2','3'])) - end - - it "should return values from a hash" do - lambda { @scope.function_values([['a','b','c']]) }.should( raise_error(Puppet::ParseError)) - end - -end diff --git a/spec/unit/parser/functions/zip_spec.rb b/spec/unit/parser/functions/zip_spec.rb deleted file mode 100644 index 074f4df..0000000 --- a/spec/unit/parser/functions/zip_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env rspec -require 'spec_helper' - -describe "the zip function" do - before :all do - Puppet::Parser::Functions.autoloader.loadall - end - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("zip").should == "function_zip" - end - - it "should raise a ParseError if there is less than 1 arguments" do - lambda { @scope.function_zip([]) }.should( raise_error(Puppet::ParseError)) - end - - it "should be able to zip an array" do - result = @scope.function_zip([['1','2','3'],['4','5','6']]) - result.should(eq([["1", "4"], ["2", "5"], ["3", "6"]])) - end - -end diff --git a/spec/unit/puppet/parser/functions/abs_spec.rb b/spec/unit/puppet/parser/functions/abs_spec.rb new file mode 100755 index 0000000..65ba2e8 --- /dev/null +++ b/spec/unit/puppet/parser/functions/abs_spec.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the abs function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("abs").should == "function_abs" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_abs([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should convert a negative number into a positive" do + result = @scope.function_abs(["-34"]) + result.should(eq(34)) + end + + it "should do nothing with a positive number" do + result = @scope.function_abs(["5678"]) + result.should(eq(5678)) + end + +end diff --git a/spec/unit/puppet/parser/functions/bool2num_spec.rb b/spec/unit/puppet/parser/functions/bool2num_spec.rb new file mode 100755 index 0000000..d5da18c --- /dev/null +++ b/spec/unit/puppet/parser/functions/bool2num_spec.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the bool2num function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("bool2num").should == "function_bool2num" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_bool2num([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should convert true to 1" do + result = @scope.function_bool2num([true]) + result.should(eq(1)) + end + + it "should convert false to 0" do + result = @scope.function_bool2num([false]) + result.should(eq(0)) + end + +end diff --git a/spec/unit/puppet/parser/functions/capitalize_spec.rb b/spec/unit/puppet/parser/functions/capitalize_spec.rb new file mode 100755 index 0000000..1c45821 --- /dev/null +++ b/spec/unit/puppet/parser/functions/capitalize_spec.rb @@ -0,0 +1,26 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the capitalize function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("capitalize").should == "function_capitalize" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_capitalize([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should capitalize the beginning of a string" do + result = @scope.function_capitalize(["abc"]) + result.should(eq("Abc")) + end + +end diff --git a/spec/unit/puppet/parser/functions/chomp_spec.rb b/spec/unit/puppet/parser/functions/chomp_spec.rb new file mode 100755 index 0000000..0592115 --- /dev/null +++ b/spec/unit/puppet/parser/functions/chomp_spec.rb @@ -0,0 +1,26 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the chomp function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("chomp").should == "function_chomp" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_chomp([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should chomp the end of a string" do + result = @scope.function_chomp(["abc\n"]) + result.should(eq("abc")) + end + +end diff --git a/spec/unit/puppet/parser/functions/chop_spec.rb b/spec/unit/puppet/parser/functions/chop_spec.rb new file mode 100755 index 0000000..0c456a8 --- /dev/null +++ b/spec/unit/puppet/parser/functions/chop_spec.rb @@ -0,0 +1,26 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the chop function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("chop").should == "function_chop" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_chop([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should chop the end of a string" do + result = @scope.function_chop(["asdf\n"]) + result.should(eq("asdf")) + end + +end diff --git a/spec/unit/puppet/parser/functions/delete_at_spec.rb b/spec/unit/puppet/parser/functions/delete_at_spec.rb new file mode 100755 index 0000000..27db0c8 --- /dev/null +++ b/spec/unit/puppet/parser/functions/delete_at_spec.rb @@ -0,0 +1,26 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the delete_at function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("delete_at").should == "function_delete_at" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_delete_at([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should delete an item at specified location from an array" do + result = @scope.function_delete_at([['a','b','c'],1]) + result.should(eq(['a','c'])) + end + +end diff --git a/spec/unit/puppet/parser/functions/delete_spec.rb b/spec/unit/puppet/parser/functions/delete_spec.rb new file mode 100755 index 0000000..fab3230 --- /dev/null +++ b/spec/unit/puppet/parser/functions/delete_spec.rb @@ -0,0 +1,26 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the delete function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("delete").should == "function_delete" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_delete([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should delete an item from an array" do + result = @scope.function_delete([['a','b','c'],'b']) + result.should(eq(['a','c'])) + end + +end diff --git a/spec/unit/puppet/parser/functions/downcase_spec.rb b/spec/unit/puppet/parser/functions/downcase_spec.rb new file mode 100755 index 0000000..0bccd5f --- /dev/null +++ b/spec/unit/puppet/parser/functions/downcase_spec.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the downcase function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("downcase").should == "function_downcase" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_downcase([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should downcase a string" do + result = @scope.function_downcase(["ASFD"]) + result.should(eq("asfd")) + end + + it "should do nothing to a string that is already downcase" do + result = @scope.function_downcase(["asdf asdf"]) + result.should(eq("asdf asdf")) + end + +end diff --git a/spec/unit/puppet/parser/functions/empty_spec.rb b/spec/unit/puppet/parser/functions/empty_spec.rb new file mode 100755 index 0000000..cb0021f --- /dev/null +++ b/spec/unit/puppet/parser/functions/empty_spec.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the empty function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("empty").should == "function_empty" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_empty([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return a true for an empty string" do + result = @scope.function_empty(['']) + result.should(eq(true)) + end + + it "should return a false for a non-empty string" do + result = @scope.function_empty(['asdf']) + result.should(eq(false)) + end + +end diff --git a/spec/unit/puppet/parser/functions/flatten_spec.rb b/spec/unit/puppet/parser/functions/flatten_spec.rb new file mode 100755 index 0000000..7bedeb2 --- /dev/null +++ b/spec/unit/puppet/parser/functions/flatten_spec.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the flatten function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("flatten").should == "function_flatten" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_flatten([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should flatten a complex data structure" do + result = @scope.function_flatten([["a","b",["c",["d","e"],"f","g"]]]) + result.should(eq(["a","b","c","d","e","f","g"])) + end + + it "should do nothing to a structure that is already flat" do + result = @scope.function_flatten([["a","b","c","d"]]) + result.should(eq(["a","b","c","d"])) + end + +end diff --git a/spec/unit/puppet/parser/functions/grep_spec.rb b/spec/unit/puppet/parser/functions/grep_spec.rb new file mode 100755 index 0000000..b1f647c --- /dev/null +++ b/spec/unit/puppet/parser/functions/grep_spec.rb @@ -0,0 +1,26 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the grep function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("grep").should == "function_grep" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_grep([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should grep contents from an array" do + result = @scope.function_grep([["aaabbb","bbbccc","dddeee"], "bbb"]) + result.should(eq(["aaabbb","bbbccc"])) + end + +end diff --git a/spec/unit/puppet/parser/functions/hash_spec.rb b/spec/unit/puppet/parser/functions/hash_spec.rb new file mode 100644 index 0000000..6d3d48c --- /dev/null +++ b/spec/unit/puppet/parser/functions/hash_spec.rb @@ -0,0 +1,26 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the hash function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("hash").should == "function_hash" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_hash([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should convert an array to a hash" do + result = @scope.function_hash([['a',1,'b',2,'c',3]]) + result.should(eq({'a'=>1,'b'=>2,'c'=>3})) + end + +end diff --git a/spec/unit/puppet/parser/functions/is_array_spec.rb b/spec/unit/puppet/parser/functions/is_array_spec.rb new file mode 100644 index 0000000..537595c --- /dev/null +++ b/spec/unit/puppet/parser/functions/is_array_spec.rb @@ -0,0 +1,36 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_array function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_array").should == "function_is_array" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_array([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return true if passed an array" do + result = @scope.function_is_array([[1,2,3]]) + result.should(eq(true)) + end + + it "should return false if passed a hash" do + result = @scope.function_is_array([{'a'=>1}]) + result.should(eq(false)) + end + + it "should return false if passed a string" do + result = @scope.function_is_array(["asdf"]) + result.should(eq(false)) + end + +end diff --git a/spec/unit/puppet/parser/functions/is_domain_name_spec.rb b/spec/unit/puppet/parser/functions/is_domain_name_spec.rb new file mode 100644 index 0000000..ec7c7f5 --- /dev/null +++ b/spec/unit/puppet/parser/functions/is_domain_name_spec.rb @@ -0,0 +1,56 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_domain_name function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_domain_name").should == "function_is_domain_name" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_domain_name([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return true if a valid domain name" do + result = @scope.function_is_domain_name(["foo.bar.com"]) + result.should(be_true) + end + + it "should allow domain parts to start with numbers" do + result = @scope.function_is_domain_name(["3foo.2bar.com"]) + result.should(be_true) + end + + it "should allow domain to end with a dot" do + result = @scope.function_is_domain_name(["3foo.2bar.com."]) + result.should(be_true) + end + + it "should allow a single part domain" do + result = @scope.function_is_domain_name(["orange"]) + result.should(be_true) + end + + it "should return false if domain parts start with hyphens" do + result = @scope.function_is_domain_name(["-3foo.2bar.com"]) + result.should(be_false) + end + + it "should return true if domain contains hyphens" do + result = @scope.function_is_domain_name(["3foo-bar.2bar-fuzz.com"]) + result.should(be_true) + end + + it "should return false if domain name contains spaces" do + result = @scope.function_is_domain_name(["not valid"]) + result.should(be_false) + end + +end diff --git a/spec/unit/puppet/parser/functions/is_float_spec.rb b/spec/unit/puppet/parser/functions/is_float_spec.rb new file mode 100644 index 0000000..55ba8cf --- /dev/null +++ b/spec/unit/puppet/parser/functions/is_float_spec.rb @@ -0,0 +1,36 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_float function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_float").should == "function_is_float" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_float([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return true if a float" do + result = @scope.function_is_float(["0.12"]) + result.should(eq(true)) + end + + it "should return false if a string" do + result = @scope.function_is_float(["asdf"]) + result.should(eq(false)) + end + + it "should return false if an integer" do + result = @scope.function_is_float(["3"]) + result.should(eq(false)) + end + +end diff --git a/spec/unit/puppet/parser/functions/is_hash_spec.rb b/spec/unit/puppet/parser/functions/is_hash_spec.rb new file mode 100644 index 0000000..94364f5 --- /dev/null +++ b/spec/unit/puppet/parser/functions/is_hash_spec.rb @@ -0,0 +1,36 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_hash function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_hash").should == "function_is_hash" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_hash([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return true if passed a hash" do + result = @scope.function_is_hash([{"a"=>1,"b"=>2}]) + result.should(eq(true)) + end + + it "should return false if passed an array" do + result = @scope.function_is_hash([["a","b"]]) + result.should(eq(false)) + end + + it "should return false if passed a string" do + result = @scope.function_is_hash(["asdf"]) + result.should(eq(false)) + end + +end diff --git a/spec/unit/puppet/parser/functions/is_integer_spec.rb b/spec/unit/puppet/parser/functions/is_integer_spec.rb new file mode 100644 index 0000000..faf6f2d --- /dev/null +++ b/spec/unit/puppet/parser/functions/is_integer_spec.rb @@ -0,0 +1,36 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_integer function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_integer").should == "function_is_integer" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_integer([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return true if an integer" do + result = @scope.function_is_integer(["3"]) + result.should(eq(true)) + end + + it "should return false if a float" do + result = @scope.function_is_integer(["3.2"]) + result.should(eq(false)) + end + + it "should return false if a string" do + result = @scope.function_is_integer(["asdf"]) + result.should(eq(false)) + end + +end diff --git a/spec/unit/puppet/parser/functions/is_ip_address_spec.rb b/spec/unit/puppet/parser/functions/is_ip_address_spec.rb new file mode 100644 index 0000000..98ce828 --- /dev/null +++ b/spec/unit/puppet/parser/functions/is_ip_address_spec.rb @@ -0,0 +1,45 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_ip_address function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_ip_address").should == "function_is_ip_address" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_ip_address([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return true if an IPv4 address" do + result = @scope.function_is_ip_address(["1.2.3.4"]) + result.should(eq(true)) + end + + it "should return true if a full IPv6 address" do + result = @scope.function_is_ip_address(["fe80:0000:cd12:d123:e2f8:47ff:fe09:dd74"]) + result.should(eq(true)) + end + + it "should return true if a compressed IPv6 address" do + result = @scope.function_is_ip_address(["fe00::1"]) + result.should(eq(true)) + end + + it "should return false if not valid" do + result = @scope.function_is_ip_address(["asdf"]) + result.should(eq(false)) + end + + it "should return false if IP octets out of range" do + result = @scope.function_is_ip_address(["1.1.1.300"]) + result.should(eq(false)) + end +end diff --git a/spec/unit/puppet/parser/functions/is_mac_address_spec.rb b/spec/unit/puppet/parser/functions/is_mac_address_spec.rb new file mode 100644 index 0000000..c9b9637 --- /dev/null +++ b/spec/unit/puppet/parser/functions/is_mac_address_spec.rb @@ -0,0 +1,36 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_mac_address function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_mac_address").should == "function_is_mac_address" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_mac_address([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return true if a valid mac address" do + result = @scope.function_is_mac_address(["00:a0:1f:12:7f:a0"]) + result.should(eq(true)) + end + + it "should return false if octets are out of range" do + result = @scope.function_is_mac_address(["00:a0:1f:12:7f:g0"]) + result.should(eq(false)) + end + + it "should return false if not valid" do + result = @scope.function_is_mac_address(["not valid"]) + result.should(eq(false)) + end + +end diff --git a/spec/unit/puppet/parser/functions/is_numeric_spec.rb b/spec/unit/puppet/parser/functions/is_numeric_spec.rb new file mode 100644 index 0000000..2191b7b --- /dev/null +++ b/spec/unit/puppet/parser/functions/is_numeric_spec.rb @@ -0,0 +1,36 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_numeric function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_numeric").should == "function_is_numeric" + end + + it "should raise a ParseError if there is less than 1 argument" do + lambda { @scope.function_is_numeric([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return true if an integer" do + result = @scope.function_is_numeric(["3"]) + result.should(eq(true)) + end + + it "should return true if a float" do + result = @scope.function_is_numeric(["3.2"]) + result.should(eq(true)) + end + + it "should return false if a string" do + result = @scope.function_is_numeric(["asdf"]) + result.should(eq(false)) + end + +end diff --git a/spec/unit/puppet/parser/functions/is_string_spec.rb b/spec/unit/puppet/parser/functions/is_string_spec.rb new file mode 100644 index 0000000..4f3f5fd --- /dev/null +++ b/spec/unit/puppet/parser/functions/is_string_spec.rb @@ -0,0 +1,41 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the is_string function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("is_string").should == "function_is_string" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_is_string([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return true if a string" do + result = @scope.function_is_string(["asdf"]) + result.should(eq(true)) + end + + it "should return false if an integer" do + result = @scope.function_is_string(["3"]) + result.should(eq(false)) + end + + it "should return false if a float" do + result = @scope.function_is_string(["3.23"]) + result.should(eq(false)) + end + + it "should return false if an array" do + result = @scope.function_is_string([["a","b","c"]]) + result.should(eq(false)) + end + +end diff --git a/spec/unit/puppet/parser/functions/join_spec.rb b/spec/unit/puppet/parser/functions/join_spec.rb new file mode 100644 index 0000000..1b3dec8 --- /dev/null +++ b/spec/unit/puppet/parser/functions/join_spec.rb @@ -0,0 +1,26 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the join function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("join").should == "function_join" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_join([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should join an array into a string" do + result = @scope.function_join([["a","b","c"], ":"]) + result.should(eq("a:b:c")) + end + +end diff --git a/spec/unit/puppet/parser/functions/keys_spec.rb b/spec/unit/puppet/parser/functions/keys_spec.rb new file mode 100644 index 0000000..927be96 --- /dev/null +++ b/spec/unit/puppet/parser/functions/keys_spec.rb @@ -0,0 +1,26 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the keys function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("keys").should == "function_keys" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_keys([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return an array of keys when given a hash" do + result = @scope.function_keys([{'a'=>1, 'b' => 2}]) + result.should(eq(['a','b'])) + end + +end diff --git a/spec/unit/puppet/parser/functions/lstrip_spec.rb b/spec/unit/puppet/parser/functions/lstrip_spec.rb new file mode 100644 index 0000000..ac331fa --- /dev/null +++ b/spec/unit/puppet/parser/functions/lstrip_spec.rb @@ -0,0 +1,26 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the lstrip function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("lstrip").should == "function_lstrip" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_lstrip([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should lstrip a string" do + result = @scope.function_lstrip([" asdf"]) + result.should(eq('asdf')) + end + +end diff --git a/spec/unit/puppet/parser/functions/member_spec.rb b/spec/unit/puppet/parser/functions/member_spec.rb new file mode 100644 index 0000000..2cebc0d --- /dev/null +++ b/spec/unit/puppet/parser/functions/member_spec.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the member function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("member").should == "function_member" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_member([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return true if a member is in an array" do + result = @scope.function_member([["a","b","c"], "a"]) + result.should(eq(true)) + end + + it "should return false if a member is not in an array" do + result = @scope.function_member([["a","b","c"], "d"]) + result.should(eq(false)) + end + +end diff --git a/spec/unit/puppet/parser/functions/num2bool_spec.rb b/spec/unit/puppet/parser/functions/num2bool_spec.rb new file mode 100644 index 0000000..6585273 --- /dev/null +++ b/spec/unit/puppet/parser/functions/num2bool_spec.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the num2bool function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("num2bool").should == "function_num2bool" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_num2bool([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return true if 1" do + result = @scope.function_num2bool(["1"]) + result.should(be_true) + end + + it "should return false if 0" do + result = @scope.function_num2bool(["0"]) + result.should(be_false) + end + +end diff --git a/spec/unit/puppet/parser/functions/parsejson_spec.rb b/spec/unit/puppet/parser/functions/parsejson_spec.rb new file mode 100644 index 0000000..26eea36 --- /dev/null +++ b/spec/unit/puppet/parser/functions/parsejson_spec.rb @@ -0,0 +1,29 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the parsejson function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("parsejson").should == "function_parsejson" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_parsejson([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should convert JSON to a data structure" do + json = <<-EOS +["aaa","bbb","ccc"] +EOS + result = @scope.function_parsejson([json]) + result.should(eq(['aaa','bbb','ccc'])) + end + +end diff --git a/spec/unit/puppet/parser/functions/parseyaml_spec.rb b/spec/unit/puppet/parser/functions/parseyaml_spec.rb new file mode 100644 index 0000000..f9cb049 --- /dev/null +++ b/spec/unit/puppet/parser/functions/parseyaml_spec.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the parseyaml function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("parseyaml").should == "function_parseyaml" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_parseyaml([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should convert YAML to a data structure" do + yaml = <<-EOS +- aaa +- bbb +- ccc +EOS + result = @scope.function_parseyaml([yaml]) + result.should(eq(['aaa','bbb','ccc'])) + end + +end diff --git a/spec/unit/puppet/parser/functions/prefix_spec.rb b/spec/unit/puppet/parser/functions/prefix_spec.rb new file mode 100644 index 0000000..a0cbcab --- /dev/null +++ b/spec/unit/puppet/parser/functions/prefix_spec.rb @@ -0,0 +1,26 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the prefix function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("prefix").should == "function_prefix" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_prefix([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return a prefixed array" do + result = @scope.function_prefix([['a','b','c'], 'p']) + result.should(eq(['pa','pb','pc'])) + end + +end diff --git a/spec/unit/puppet/parser/functions/range_spec.rb b/spec/unit/puppet/parser/functions/range_spec.rb new file mode 100644 index 0000000..8c2446a --- /dev/null +++ b/spec/unit/puppet/parser/functions/range_spec.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the range function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("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)) + end + + it "should return a letter range" do + result = @scope.function_range(["a","d"]) + 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])) + end + +end diff --git a/spec/unit/puppet/parser/functions/reverse_spec.rb b/spec/unit/puppet/parser/functions/reverse_spec.rb new file mode 100644 index 0000000..4fa50e4 --- /dev/null +++ b/spec/unit/puppet/parser/functions/reverse_spec.rb @@ -0,0 +1,26 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the reverse function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("reverse").should == "function_reverse" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_reverse([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should reverse a string" do + result = @scope.function_reverse(["asdfghijkl"]) + result.should(eq('lkjihgfdsa')) + end + +end diff --git a/spec/unit/puppet/parser/functions/rstrip_spec.rb b/spec/unit/puppet/parser/functions/rstrip_spec.rb new file mode 100644 index 0000000..af8cc12 --- /dev/null +++ b/spec/unit/puppet/parser/functions/rstrip_spec.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the rstrip function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("rstrip").should == "function_rstrip" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_rstrip([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should rstrip a string" do + result = @scope.function_rstrip(["asdf "]) + result.should(eq('asdf')) + end + + it "should rstrip each element in an array" do + result = @scope.function_rstrip([["a ","b ", "c "]]) + result.should(eq(['a','b','c'])) + end + +end diff --git a/spec/unit/puppet/parser/functions/shuffle_spec.rb b/spec/unit/puppet/parser/functions/shuffle_spec.rb new file mode 100644 index 0000000..f04fda5 --- /dev/null +++ b/spec/unit/puppet/parser/functions/shuffle_spec.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the shuffle function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("shuffle").should == "function_shuffle" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_shuffle([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should shuffle a string and the result should be the same size" do + result = @scope.function_shuffle(["asdf"]) + result.size.should(eq(4)) + end + + it "should shuffle a string but the sorted contents should still be the same" do + result = @scope.function_shuffle(["adfs"]) + result.split("").sort.join("").should(eq("adfs")) + end + +end diff --git a/spec/unit/puppet/parser/functions/size_spec.rb b/spec/unit/puppet/parser/functions/size_spec.rb new file mode 100644 index 0000000..ccaa335 --- /dev/null +++ b/spec/unit/puppet/parser/functions/size_spec.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the size function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("size").should == "function_size" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_size([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return the size of a string" do + result = @scope.function_size(["asdf"]) + result.should(eq(4)) + end + + it "should return the size of an array" do + result = @scope.function_size([["a","b","c"]]) + result.should(eq(3)) + end + +end diff --git a/spec/unit/puppet/parser/functions/sort_spec.rb b/spec/unit/puppet/parser/functions/sort_spec.rb new file mode 100644 index 0000000..fbe3073 --- /dev/null +++ b/spec/unit/puppet/parser/functions/sort_spec.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the sort function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("sort").should == "function_sort" + end + + it "should raise a ParseError if there is not 1 arguments" do + lambda { @scope.function_sort(['','']) }.should( raise_error(Puppet::ParseError)) + end + + it "should sort an array" do + result = @scope.function_sort([["a","c","b"]]) + result.should(eq(['a','b','c'])) + end + + it "should sort a string" do + result = @scope.function_sort(["acb"]) + result.should(eq('abc')) + end + +end diff --git a/spec/unit/puppet/parser/functions/squeeze_spec.rb b/spec/unit/puppet/parser/functions/squeeze_spec.rb new file mode 100644 index 0000000..9355ad2 --- /dev/null +++ b/spec/unit/puppet/parser/functions/squeeze_spec.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the squeeze function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("squeeze").should == "function_squeeze" + end + + it "should raise a ParseError if there is less than 2 arguments" do + lambda { @scope.function_squeeze([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should squeeze a string" do + result = @scope.function_squeeze(["aaabbbbcccc"]) + result.should(eq('abc')) + end + + it "should squeeze all elements in an array" do + result = @scope.function_squeeze([["aaabbbbcccc","dddfff"]]) + result.should(eq(['abc','df'])) + end + +end diff --git a/spec/unit/puppet/parser/functions/str2bool_spec.rb b/spec/unit/puppet/parser/functions/str2bool_spec.rb new file mode 100644 index 0000000..d7f0ac9 --- /dev/null +++ b/spec/unit/puppet/parser/functions/str2bool_spec.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the str2bool function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("str2bool").should == "function_str2bool" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_str2bool([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should convert string 'true' to true" do + result = @scope.function_str2bool(["true"]) + result.should(eq(true)) + end + + it "should convert string 'undef' to false" do + result = @scope.function_str2bool(["undef"]) + result.should(eq(false)) + end + +end diff --git a/spec/unit/puppet/parser/functions/strftime_spec.rb b/spec/unit/puppet/parser/functions/strftime_spec.rb new file mode 100644 index 0000000..f7a2cd9 --- /dev/null +++ b/spec/unit/puppet/parser/functions/strftime_spec.rb @@ -0,0 +1,36 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the strftime function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("strftime").should == "function_strftime" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_strftime([]) }.should( raise_error(Puppet::ParseError)) + end + + it "using %s should be higher then when I wrote this test" do + result = @scope.function_strftime(["%s"]) + result.to_i.should(be > 1311953157) + end + + it "using %s should be lower then 1.5 trillion" do + result = @scope.function_strftime(["%s"]) + result.to_i.should(be < 1500000000) + end + + it "should return a date when given %Y-%m-%d" do + result = @scope.function_strftime(["%Y-%m-%d"]) + result.should =~ /^\d{4}-\d{2}-\d{2}$/ + end + +end diff --git a/spec/unit/puppet/parser/functions/strip_spec.rb b/spec/unit/puppet/parser/functions/strip_spec.rb new file mode 100644 index 0000000..48a52dd --- /dev/null +++ b/spec/unit/puppet/parser/functions/strip_spec.rb @@ -0,0 +1,26 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the strip function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("strip").should == "function_strip" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_strip([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should strip a string" do + result = @scope.function_strip([" ab cd "]) + result.should(eq('ab cd')) + end + +end diff --git a/spec/unit/puppet/parser/functions/swapcase_spec.rb b/spec/unit/puppet/parser/functions/swapcase_spec.rb new file mode 100644 index 0000000..2686054 --- /dev/null +++ b/spec/unit/puppet/parser/functions/swapcase_spec.rb @@ -0,0 +1,26 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the swapcase function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("swapcase").should == "function_swapcase" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_swapcase([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should swapcase a string" do + result = @scope.function_swapcase(["aaBBccDD"]) + result.should(eq('AAbbCCdd')) + end + +end diff --git a/spec/unit/puppet/parser/functions/time_spec.rb b/spec/unit/puppet/parser/functions/time_spec.rb new file mode 100644 index 0000000..666e8e0 --- /dev/null +++ b/spec/unit/puppet/parser/functions/time_spec.rb @@ -0,0 +1,36 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the time function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("time").should == "function_time" + end + + it "should raise a ParseError if there is more than 2 arguments" do + lambda { @scope.function_time(['','']) }.should( raise_error(Puppet::ParseError)) + end + + it "should return a number" do + result = @scope.function_time([]) + result.class.should(eq(Fixnum)) + end + + it "should be higher then when I wrote this test" do + result = @scope.function_time([]) + result.should(be > 1311953157) + end + + it "should be lower then 1.5 trillion" do + result = @scope.function_time([]) + result.should(be < 1500000000) + end + +end diff --git a/spec/unit/puppet/parser/functions/type_spec.rb b/spec/unit/puppet/parser/functions/type_spec.rb new file mode 100644 index 0000000..e3c28ed --- /dev/null +++ b/spec/unit/puppet/parser/functions/type_spec.rb @@ -0,0 +1,51 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the type function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("type").should == "function_type" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_type([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return string when given a string" do + result = @scope.function_type(["aaabbbbcccc"]) + result.should(eq('string')) + end + + it "should return array when given an array" do + result = @scope.function_type([["aaabbbbcccc","asdf"]]) + result.should(eq('array')) + end + + it "should return hash when given a hash" do + result = @scope.function_type([{"a"=>1,"b"=>2}]) + result.should(eq('hash')) + end + + it "should return integer when given an integer" do + result = @scope.function_type(["1"]) + result.should(eq('integer')) + end + + it "should return float when given a float" do + result = @scope.function_type(["1.34"]) + result.should(eq('float')) + end + + it "should return boolean when given a boolean" do + result = @scope.function_type([true]) + result.should(eq('boolean')) + end + +end diff --git a/spec/unit/puppet/parser/functions/unique_spec.rb b/spec/unit/puppet/parser/functions/unique_spec.rb new file mode 100644 index 0000000..627dc33 --- /dev/null +++ b/spec/unit/puppet/parser/functions/unique_spec.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the unique function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("unique").should == "function_unique" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_unique([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should remove duplicate elements in a string" do + result = @scope.function_unique(["aabbc"]) + result.should(eq('abc')) + end + + it "should remove duplicate elements in an array" do + result = @scope.function_unique([["a","a","b","b","c"]]) + result.should(eq(['a','b','c'])) + end + +end diff --git a/spec/unit/puppet/parser/functions/upcase_spec.rb b/spec/unit/puppet/parser/functions/upcase_spec.rb new file mode 100644 index 0000000..5d18846 --- /dev/null +++ b/spec/unit/puppet/parser/functions/upcase_spec.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the upcase function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("upcase").should == "function_upcase" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_upcase([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should upcase a string" do + result = @scope.function_upcase(["abc"]) + result.should(eq('ABC')) + end + + it "should do nothing if a string is already upcase" do + result = @scope.function_upcase(["ABC"]) + result.should(eq('ABC')) + end + +end diff --git a/spec/unit/puppet/parser/functions/values_at_spec.rb b/spec/unit/puppet/parser/functions/values_at_spec.rb new file mode 100644 index 0000000..6c45316 --- /dev/null +++ b/spec/unit/puppet/parser/functions/values_at_spec.rb @@ -0,0 +1,45 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the values_at function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("values_at").should == "function_values_at" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_values_at([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should raise a ParseError if you try to use a range where stop is greater then start" do + lambda { @scope.function_values_at([['a','b'],["3-1"]]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return a value at from an array" do + result = @scope.function_values_at([['a','b','c'],"1"]) + result.should(eq(['b'])) + end + + it "should return a value at from an array when passed a range" do + result = @scope.function_values_at([['a','b','c'],"0-1"]) + result.should(eq(['a','b'])) + end + + it "should return chosen values from an array when passed number of indexes" do + result = @scope.function_values_at([['a','b','c'],["0","2"]]) + result.should(eq(['a','c'])) + end + + it "should return chosen values from an array when passed ranges and multiple indexes" do + result = @scope.function_values_at([['a','b','c','d','e','f','g'],["0","2","4-5"]]) + result.should(eq(['a','c','e','f'])) + end + +end diff --git a/spec/unit/puppet/parser/functions/values_spec.rb b/spec/unit/puppet/parser/functions/values_spec.rb new file mode 100644 index 0000000..f6eb5b6 --- /dev/null +++ b/spec/unit/puppet/parser/functions/values_spec.rb @@ -0,0 +1,30 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the values function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("values").should == "function_values" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_values([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should return values from a hash" do + result = @scope.function_values([{'a'=>'1','b'=>'2','c'=>'3'}]) + result.should(eq(['1','2','3'])) + end + + it "should return values from a hash" do + lambda { @scope.function_values([['a','b','c']]) }.should( raise_error(Puppet::ParseError)) + end + +end diff --git a/spec/unit/puppet/parser/functions/zip_spec.rb b/spec/unit/puppet/parser/functions/zip_spec.rb new file mode 100644 index 0000000..074f4df --- /dev/null +++ b/spec/unit/puppet/parser/functions/zip_spec.rb @@ -0,0 +1,26 @@ +#!/usr/bin/env rspec +require 'spec_helper' + +describe "the zip function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("zip").should == "function_zip" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_zip([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should be able to zip an array" do + result = @scope.function_zip([['1','2','3'],['4','5','6']]) + result.should(eq([["1", "4"], ["2", "5"], ["3", "6"]])) + end + +end -- cgit v1.2.3