summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.fixtures.yml3
-rw-r--r--.gitignore2
-rw-r--r--.travis.yml3
-rw-r--r--CHANGELOG.md37
-rw-r--r--CONTRIBUTING.md22
-rw-r--r--Gemfile11
-rw-r--r--README.markdown192
-rw-r--r--lib/facter/facter_dot_d.rb6
-rw-r--r--lib/puppet/functions/type_of.rb17
-rw-r--r--lib/puppet/parser/functions/basename.rb34
-rw-r--r--lib/puppet/parser/functions/ceiling.rb25
-rw-r--r--lib/puppet/parser/functions/concat.rb18
-rw-r--r--lib/puppet/parser/functions/delete.rb27
-rw-r--r--lib/puppet/parser/functions/ensure_resource.rb3
-rw-r--r--lib/puppet/parser/functions/fqdn_rotate.rb16
-rw-r--r--lib/puppet/parser/functions/is_domain_name.rb8
-rw-r--r--lib/puppet/parser/functions/prefix.rb23
-rw-r--r--lib/puppet/parser/functions/type.rb43
-rw-r--r--lib/puppet/parser/functions/type3x.rb51
-rw-r--r--lib/puppet/parser/functions/upcase.rb17
-rw-r--r--lib/puppet/parser/functions/uriescape.rb2
-rw-r--r--lib/puppet/parser/functions/validate_absolute_path.rb61
-rw-r--r--lib/puppet/parser/functions/validate_cmd.rb21
-rw-r--r--lib/puppet/type/file_line.rb7
-rw-r--r--metadata.json8
-rwxr-xr-xspec/acceptance/ceiling_spec.rb39
-rwxr-xr-xspec/acceptance/concat_spec.rb22
-rwxr-xr-xspec/acceptance/fqdn_rotate_spec.rb2
-rwxr-xr-xspec/functions/ceiling_spec.rb39
-rwxr-xr-xspec/functions/concat_spec.rb17
-rwxr-xr-xspec/functions/delete_spec.rb31
-rwxr-xr-xspec/functions/fqdn_rotate_spec.rb17
-rwxr-xr-xspec/functions/is_domain_name_spec.rb7
-rwxr-xr-xspec/functions/prefix_spec.rb5
-rw-r--r--spec/functions/type3x_spec.rb43
-rwxr-xr-xspec/functions/type_spec.rb5
-rwxr-xr-xspec/functions/upcase_spec.rb27
-rwxr-xr-xspec/functions/uriescape_spec.rb7
-rwxr-xr-xspec/functions/validate_absolute_path_spec.rb38
-rwxr-xr-xspec/functions/validate_cmd_spec.rb81
-rw-r--r--spec/unit/puppet/functions/type_of_spec.rb33
-rwxr-xr-xspec/unit/puppet/parser/functions/basename_spec.rb46
-rwxr-xr-xspec/unit/puppet/type/file_line_spec.rb4
43 files changed, 849 insertions, 271 deletions
diff --git a/.fixtures.yml b/.fixtures.yml
new file mode 100644
index 0000000..37b7377
--- /dev/null
+++ b/.fixtures.yml
@@ -0,0 +1,3 @@
+fixtures:
+ symlinks:
+ stdlib: "#{source_dir}"
diff --git a/.gitignore b/.gitignore
index b5b7a00..b5db85e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,3 +5,5 @@ spec/fixtures/
.vagrant/
.bundle/
coverage/
+.idea/
+*.iml
diff --git a/.travis.yml b/.travis.yml
index 3ed1153..503e184 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,6 +1,7 @@
---
+sudo: false
language: ruby
-bundler_args: --without development
+bundler_args: --without system_tests
script: "bundle exec rake validate && bundle exec rake lint && bundle exec rake spec SPEC_OPTS='--color --format documentation'"
matrix:
fast_finish: true
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2fb73db..1385695 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,40 @@
+##2015-01-14 - Supported Release 4.6.0
+###Summary
+
+Improved functionality and preparing for Puppet Next with new parser
+
+####Features
+- MODULES-444: concat can now take more than two arrays
+- basename function added to have Ruby File.basename functionality
+- delete function can now take an array of items to remove
+- MODULES-1473: deprecate type function in favor of type_of
+
+####Bugfixes
+- Several test case fixes
+- Ensure_resource is more verbose on debug mode
+
+##2015-01-14 - Supported Release 4.5.1
+###Summary
+
+This release changes the temporary facter_dot_d cache locations outside of the /tmp directory due to a possible security vunerability. CVE-2015-1029
+
+####Bugfixes
+- Facter_dot_d cache will now be stored in puppet libdir instead of tmp
+
+##2014-12-15 - Supported Release 4.5.0
+###Summary
+
+This release improves functionality of the member function and adds improved future parser support.
+
+####Features
+- MODULES-1329: Update member() to allow the variable to be an array.
+- Sync .travis.yml, Gemfile, Rakefile, and CONTRIBUTING.md via modulesync
+
+####Bugfixes
+- Fix range() to work with numeric ranges with the future parser
+- Accurately express SLES support in metadata.json (was missing 10SP4 and 12)
+- Don't require `line` to match the `match` parameter
+
##2014-11-10 - Supported Release 4.4.0
###Summary
This release has an overhauled readme, new private manifest function, and fixes many future parser bugs.
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index e128847..f1cbde4 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -41,11 +41,9 @@ Checklist (and a short version for the impatient)
* Pre-requisites:
- - Sign the [Contributor License Agreement](https://cla.puppetlabs.com/)
-
- Make sure you have a [GitHub account](https://github.com/join)
- - [Create a ticket](http://projects.puppetlabs.com/projects/modules/issues/new), or [watch the ticket](http://projects.puppetlabs.com/projects/modules/issues) you are patching for.
+ - [Create a ticket](https://tickets.puppetlabs.com/secure/CreateIssue!default.jspa), or [watch the ticket](https://tickets.puppetlabs.com/browse/) you are patching for.
* Preferred method:
@@ -94,17 +92,7 @@ The long version
whitespace or other "whitespace errors". You can do this by
running "git diff --check" on your changes before you commit.
- 2. Sign the Contributor License Agreement
-
- Before we can accept your changes, we do need a signed Puppet
- Labs Contributor License Agreement (CLA).
-
- You can access the CLA via the [Contributor License Agreement link](https://cla.puppetlabs.com/)
-
- If you have any questions about the CLA, please feel free to
- contact Puppet Labs via email at cla-submissions@puppetlabs.com.
-
- 3. Sending your patches
+ 2. Sending your patches
To submit your changes via a GitHub pull request, we _highly_
recommend that you have them on a topic branch, instead of
@@ -124,7 +112,7 @@ The long version
in order to open a pull request.
- 4. Update the related GitHub issue.
+ 3. Update the related GitHub issue.
If there is a GitHub issue associated with the change you
submitted, then you should update the ticket to include the
@@ -220,14 +208,12 @@ review.
Additional Resources
====================
-* [Getting additional help](http://projects.puppetlabs.com/projects/puppet/wiki/Getting_Help)
+* [Getting additional help](http://puppetlabs.com/community/get-help)
* [Writing tests](http://projects.puppetlabs.com/projects/puppet/wiki/Development_Writing_Tests)
* [Patchwork](https://patchwork.puppetlabs.com)
-* [Contributor License Agreement](https://projects.puppetlabs.com/contributor_licenses/sign)
-
* [General GitHub documentation](http://help.github.com/)
* [GitHub pull request documentation](http://help.github.com/send-pull-requests/)
diff --git a/Gemfile b/Gemfile
index 74a16f3..fa2a31c 100644
--- a/Gemfile
+++ b/Gemfile
@@ -12,6 +12,7 @@ end
group :development, :unit_tests do
gem 'rake', '~> 10.1.0', :require => false
+ gem 'rspec', '~> 3.1.0', :require => false
gem 'rspec-puppet', :require => false
gem 'puppetlabs_spec_helper', :require => false
gem 'puppet-lint', :require => false
@@ -24,8 +25,14 @@ group :system_tests do
gem 'serverspec', :require => false
end
-ENV['GEM_PUPPET_VERSION'] ||= ENV['PUPPET_GEM_VERSION']
-puppetversion = ENV['GEM_PUPPET_VERSION']
+facterversion = ENV['GEM_FACTER_VERSION'] || ENV['FACTER_GEM_VERSION']
+if facterversion
+ gem 'facter', *location_for(facterversion)
+else
+ gem 'facter', :require => false
+end
+
+puppetversion = ENV['GEM_PUPPET_VERSION'] || ENV['PUPPET_GEM_VERSION']
if puppetversion
gem 'puppet', *location_for(puppetversion)
else
diff --git a/README.markdown b/README.markdown
index 69c5d3d..cbe42b4 100644
--- a/README.markdown
+++ b/README.markdown
@@ -1,7 +1,5 @@
#stdlib
-[![Build Status](https://travis-ci.org/puppetlabs/puppetlabs-stdlib.png?branch=master)](https://travis-ci.org/puppetlabs/puppetlabs-stdlib)
-
####Table of Contents
1. [Overview](#overview)
@@ -14,7 +12,7 @@
##Overview
-Adds a standard library of resources for Puppet modules.
+Adds a standard library of resources for Puppet modules.
##Module Description
@@ -28,19 +26,21 @@ modules. Puppet modules make heavy use of this standard library. The stdlib modu
* Types
* Providers
+> *Note:* As of version 3.7, Puppet Enterprise no longer includes the stdlib module. If you're running Puppet Enterprise, you should install the most recent release of stdlib for compatibility with Puppet modules.
+
##Setup
-Installing the stdlib module adds the functions, facts, and resources of this standard library to Puppet.
+Installing the stdlib module adds the functions, facts, and resources of this standard library to Puppet.
##Usage
-After you've installed stdlib, all of its functions, facts, and resources are available for module use or development.
+After you've installed stdlib, all of its functions, facts, and resources are available for module use or development.
-If you want to use a standardized set of run stages for Puppet, `include stdlib` in your manifest.
+If you want to use a standardized set of run stages for Puppet, `include stdlib` in your manifest.
-##Reference
+## Reference
-### Classes
+### Classes
#### Public Classes
@@ -74,6 +74,29 @@ If you want to use a standardized set of run stages for Puppet, `include stdlib`
}
```
+### Resources
+
+* `file_line`: This resource ensures that a given line, including whitespace at the beginning and end, is contained within a file. If the line is not contained in the given file, Puppet will add the line. Multiple resources can be declared to manage multiple lines in the same file. You can also use match to replace existing lines.
+
+ ```
+ file_line { 'sudo_rule':
+ path => '/etc/sudoers',
+ line => '%sudo ALL=(ALL) ALL',
+ }
+ file_line { 'sudo_rule_nopw':
+ path => '/etc/sudoers',
+ line => '%sudonopw ALL=(ALL) NOPASSWD: ALL',
+ }
+ ```
+
+ * `after`: Specify the line after which Puppet will add any new lines. (Existing lines are added in place.) Optional.
+ * `ensure`: Ensures whether the resource is present. Valid values are 'present', 'absent'.
+ * `line`: The line to be added to the file located by the `path` parameter.
+ * `match`: A regular expression to run against existing lines in the file; if a match is found, we replace that line rather than adding a new line. Optional.
+ * `multiple`: Determine if match can change multiple lines. Valid values are 'true', 'false'. Optional.
+ * `name`: An arbitrary name used as the identity of the resource.
+ * `path`: The file in which Puppet will ensure the line specified by the line parameter.
+
### Functions
* `abs`: Returns the absolute value of a number; for example, '-34.56' becomes '34.56'. Takes a single integer and float value as an argument. *Type*: rvalue
@@ -84,6 +107,13 @@ If you want to use a standardized set of run stages for Puppet, `include stdlib`
Requires an action ('encode', 'decode') and either a plain or base64-encoded
string. *Type*: rvalue
+* `basename`: Returns the `basename` of a path (optionally stripping an extension). For example:
+ * ('/path/to/a/file.ext') returns 'file.ext'
+ * ('relative/path/file.ext') returns 'file.ext'
+ * ('/path/to/a/file.ext', '.ext') returns 'file'
+
+ *Type*: rvalue
+
* `bool2num`: Converts a boolean to a number. Converts values:
* 'false', 'f', '0', 'n', and 'no' to 0.
* 'true', 't', '1', 'y', and 'yes' to 1.
@@ -92,12 +122,17 @@ string. *Type*: rvalue
* `capitalize`: Capitalizes the first letter of a string or array of strings.
Requires either a single string or an array as an input. *Type*: rvalue
+* `ceiling`: Returns the smallest integer greater than or equal to the argument.
+Takes a single numeric value as an argument. *Type*: rvalue
+
* `chomp`: 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. *Type*: rvalue
* `chop`: 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 want to merely remove record separators, then you should use the `chomp` function. Requires a string or an array of strings as input. *Type*: rvalue
-* `concat`: Appends the contents of array 2 onto array 1. For example, `concat(['1','2','3'],'4')` results in: ['1','2','3','4']. *Type*: rvalue
+* `concat`: Appends the contents of multiple arrays onto array 1. For example:
+ * `concat(['1','2','3'],'4')` results in: ['1','2','3','4'].
+ * `concat(['1','2','3'],'4',['5','6','7'])` results in: ['1','2','3','4','5','6','7'].
* `count`: Takes an array as first argument and an optional second argument. Count the number of elements in array that matches second argument. If called with only an array, it counts the number of elements that are **not** nil/undef. *Type*: rvalue
@@ -112,11 +147,11 @@ strings; for example, 'hello\n' becomes 'hello'. Requires a single string or arr
user { 'dan': ensure => present, }
}
```
-
+
*Type*: rvalue
* `delete`: Deletes all instances of a given element from an array, substring from a
-string, or key from a hash. For example, `delete(['a','b','c','b'], 'b')` returns ['a','c']; `delete('abracadabra', 'bra')` returns 'acada'. *Type*: rvalue
+string, or key from a hash. For example, `delete(['a','b','c','b'], 'b')` returns ['a','c']; `delete('abracadabra', 'bra')` returns 'acada'. `delete({'a' => 1,'b' => 2,'c' => 3},['b','c'])` returns {'a'=> 1} *Type*: rvalue
* `delete_at`: Deletes a determined indexed value from an array. For example, `delete_at(['a','b','c'], 1)` returns ['a','c']. *Type*: rvalue
@@ -156,25 +191,6 @@ also appear in the second array. For example, `difference(["a","b","c"],["b","c"
*Type*: statement
-* `file_line`: This resource ensures that a given line is contained within a file. You can also use match to replace existing lines.
-
- *Example:*
-
- ```
- file_line { 'sudo_rule':
- path => '/etc/sudoers',
- line => '%sudo ALL=(ALL) ALL',
- }
-
- file_line { 'change_mount':
- path => '/etc/fstab',
- line => '10.0.0.1:/vol/data /opt/data nfs defaults 0 0',
- match => '^172.16.17.2:/vol/old',
- }
- ```
-
- *Type*: resource
-
* `flatten`: This function flattens any deeply nested arrays and returns a single flat array as a result. For example, `flatten(['a', ['b', ['c']]])` returns ['a','b','c']. *Type*: rvalue
* `floor`: Returns the largest integer less than or equal to the argument.
@@ -239,7 +255,7 @@ returns the value of the resource's parameter. For example, the following code r
has_interface_with("macaddress", "x:x:x:x:x:x")
has_interface_with("ipaddress", "127.0.0.1") => true
```
-
+
If no kind is given, then the presence of the interface is checked:
```
@@ -265,7 +281,7 @@ returns the value of the resource's parameter. For example, the following code r
notice('this will be printed')
}
```
-
+
*Type*: rvalue
* `hash`: This function converts an array into a hash. For example, `hash(['a',1,'b',2,'c',3])` returns {'a'=>1,'b'=>2,'c'=>3}. *Type*: rvalue
@@ -312,12 +328,12 @@ returns the value of the resource's parameter. For example, the following code r
* `max`: Returns the highest value of all arguments. Requires at least one argument. *Type*: rvalue
-* `member`: This function determines if a variable is a member of an array. For example, `member(['a','b'], 'b')` returns 'true', while `member(['a','b'], 'c')` returns 'false'. *Type*: rvalue
+* `member`: This function determines if a variable is a member of an array. The variable can be either a string, array, or fixnum. For example, `member(['a','b'], 'b')` and `member(['a','b','c'], ['b','c'])` return 'true', while `member(['a','b'], 'c')` and `member(['a','b','c'], ['c','d'])` return 'false'. *Type*: rvalue
* `merge`: Merges two or more hashes together and returns the resulting hash.
*Example*:
-
+
```
$hash1 = {'one' => 1, 'two' => 2}
$hash2 = {'two' => 'dos', 'three' => 'tres'}
@@ -325,7 +341,7 @@ returns the value of the resource's parameter. For example, the following code r
# The resulting hash is equivalent to:
# $merged_hash = {'one' => 1, 'two' => 'dos', 'three' => 'tres'}
```
-
+
When there is a duplicate key, the key in the rightmost hash "wins." *Type*: rvalue
* `min`: Returns the lowest value of all arguments. Requires at least one argument. *Type*: rvalue
@@ -341,10 +357,10 @@ returns the value of the resource's parameter. For example, the following code r
```
$real_jenkins_version = pick($::jenkins_version, '1.449')
```
-
+
*Type*: rvalue
-* `prefix`: This function applies a prefix to all elements in an array. For example, `prefix(['a','b','c'], 'p')` returns ['pa','pb','pc']. *Type*: rvalue
+* `prefix`: This function applies a prefix to all elements in an array or to the keys in a hash. For example, `prefix(['a','b','c'], 'p')` returns ['pa','pb','pc'], and `prefix({'a'=>'b','b'=>'c','c'=>'d'}, 'p')` returns {'pa'=>'b','pb'=>'c','pc'=>'d'}. *Type*: rvalue
* `private`: This function sets the current class or definition as private.
@@ -353,9 +369,9 @@ Calling the class or definition from outside the current module will fail. For e
```
Class foo::bar is private
```
-
+
You can specify the error message you want to use:
-
+
```
private("You're not supposed to do that!")
```
@@ -364,8 +380,10 @@ Calling the class or definition from outside the current module will fail. For e
* `range`: When given range in the form of '(start, stop)', `range` extrapolates a range as an array. For example, `range("0", "9")` returns [0,1,2,3,4,5,6,7,8,9]. Zero-padded strings are converted to integers automatically, so `range("00", "09")` returns [0,1,2,3,4,5,6,7,8,9].
- Non-integer strings are accepted; `range("a", "c")` returns ["a","b","c"], and `range("host01", "host10")` returns ["host01", "host02", ..., "host09", "host10"].
-
+ Non-integer strings are accepted; `range("a", "c")` returns ["a","b","c"], and `range("host01", "host10")` returns ["host01", "host02", ..., "host09", "host10"].
+
+ Passing a third argument will cause the generated range to step by that interval, e.g. `range("0", "9", "2")` returns ["0","2","4","6","8"]
+
*Type*: rvalue
* `reject`: This function searches through an array and rejects all elements that match the provided regular expression. For example, `reject(['aaa','bbb','ccc','aaaddd'], 'aaa')` returns ['bbb','ccc']. *Type*: rvalue
@@ -390,7 +408,7 @@ manifests as a valid password attribute. *Type*: rvalue
* `strftime`: This function returns formatted time. For example, `strftime("%s")` returns the time since epoch, and `strftime("%Y=%m-%d")` returns the date. *Type*: rvalue
*Format:*
-
+
* `%a`: The abbreviated weekday name ('Sun')
* `%A`: The full weekday name ('Sunday')
* `%b`: The abbreviated month name ('Jan')
@@ -437,7 +455,6 @@ manifests as a valid password attribute. *Type*: rvalue
* `%Z`: Time zone name
* `%%`: Literal '%' character
-
* `strip`: This function removes leading and trailing whitespace from a string or from every string inside an array. For example, `strip(" aaa ")` results in "aaa". *Type*: rvalue
* `suffix`: This function applies a suffix to all elements in an array. For example, `suffix(['a','b','c'], 'p')` returns ['ap','bp','cp']. *Type*: rvalue
@@ -449,7 +466,12 @@ manifests as a valid password attribute. *Type*: rvalue
* `to_bytes`: Converts the argument into bytes, for example 4 kB becomes 4096.
Takes a single string value as an argument. *Type*: rvalue
-* `type`: Returns the type when passed a variable. Type can be a string, array, hash, float, integer, or boolean. *Type*: rvalue
+* `type3x`: Returns a string description of the type when passed a value. Type can be a string, array, hash, float, integer, or boolean. This function will be removed when puppet 3 support is dropped and the new type system may be used. *Type*: rvalue
+
+* `type_of`: Returns the literal type when passed a value. Requires the new
+ parser. Useful for comparison of types with `<=` such as in `if
+ type_of($some_value) <= Array[String] { ... }` (which is equivalent to `if
+ $some_value =~ Array[String] { ... }`) *Type*: rvalue
* `union`: This function returns a union of two arrays. For example, `union(["a","b","c"],["b","c","d"])` returns ["a","b","c","d"].
@@ -457,33 +479,40 @@ Takes a single string value as an argument. *Type*: rvalue
You can also use this with arrays. For example, `unique(["a","a","b","b","c","c"])` returns ["a","b","c"]. *Type*: rvalue
-* `upcase`: Converts a string or an array of strings to uppercase. For example, `upcase("abcd")` returns 'ABCD'. *Type*: rvalue
+* `upcase`: Converts an object, array or hash of objects that respond to upcase to uppercase. For example, `upcase("abcd")` returns 'ABCD'. *Type*: rvalue
* `uriescape`: Urlencodes a string or array of strings. Requires either a single string or an array as an input. *Type*: rvalue
-* `validate_absolute_path`: Validate that the string represents an absolute path in the filesystem. This function works for Windows and Unix-style paths.
- The following values will pass:
+* `validate_absolute_path`: Validate the string represents an absolute path in the filesystem. This function works for Windows and Unix style paths.
- ```
- $my_path = "C:/Program Files (x86)/Puppet Labs/Puppet"
- validate_absolute_path($my_path)
- $my_path2 = "/var/lib/puppet"
- validate_absolute_path($my_path2)
- ```
+ The following values will pass:
- The following values will fail, causing compilation to abort:
+ ```
+ $my_path = 'C:/Program Files (x86)/Puppet Labs/Puppet'
+ validate_absolute_path($my_path)
+ $my_path2 = '/var/lib/puppet'
+ validate_absolute_path($my_path2)
+ $my_path3 = ['C:/Program Files (x86)/Puppet Labs/Puppet','C:/Program Files/Puppet Labs/Puppet']
+ validate_absolute_path($my_path3)
+ $my_path4 = ['/var/lib/puppet','/usr/share/puppet']
+ validate_absolute_path($my_path4)
+ ```
- ```
- validate_absolute_path(true)
- validate_absolute_path([ 'var/lib/puppet', '/var/foo' ])
- validate_absolute_path([ '/var/lib/puppet', 'var/foo' ])
- $undefined = undef
- validate_absolute_path($undefined)
- ```
-
- *Type*: statement
+ The following values will fail, causing compilation to abort:
+
+ ```
+ validate_absolute_path(true)
+ validate_absolute_path('../var/lib/puppet')
+ validate_absolute_path('var/lib/puppet')
+ validate_absolute_path([ 'var/lib/puppet', '/var/foo' ])
+ validate_absolute_path([ '/var/lib/puppet', 'var/foo' ])
+ $undefined = undef
+ validate_absolute_path($undefined)
+ ```
-* `validate_array`: Validate that all passed values are array data structures. Abort catalog compilation if any value fails this check.
+ *Type*: statement
+
+* `validate_array`: Validate that all passed values are array data structures. Abort catalog compilation if any value fails this check.
The following values will pass:
@@ -513,13 +542,13 @@ The first argument of this function should be the string to test, and the second
```
validate_augeas($passwdcontent, 'Passwd.lns', ['$file/foo'])
```
-
+
To ensure that no users use the '/bin/barsh' shell:
```
validate_augeas($passwdcontent, 'Passwd.lns', ['$file/*[shell="/bin/barsh"]']
```
-
+
You can pass a fourth argument as the error message raised and shown to the user:
```
@@ -531,13 +560,13 @@ The first argument of this function should be the string to test, and the second
* `validate_bool`: Validate that all passed values are either true or false. Abort catalog compilation if any value fails this check.
The following values will pass:
-
+
```
$iamtrue = true
validate_bool(true)
validate_bool(true, true, false, $iamtrue)
```
-
+
The following values will fail, causing compilation to abort:
```
@@ -549,14 +578,19 @@ The first argument of this function should be the string to test, and the second
*Type*: statement
-* `validate_cmd`: Performs validation of a string with an external command. The first argument of this function should be the string to test, and the second argument should be the path to a test command taking a file as last argument. If the command, launched against a tempfile containing the passed string, returns a non-null value, compilation aborts with a parse error.
+* `validate_cmd`: Performs validation of a string with an external command. The first argument of this function should be a string to test, and the second argument should be a path to a test command taking a % as a placeholder for the file path (will default to the end of the command if no % placeholder given). If the command, launched against a tempfile containing the passed string, returns a non-null value, compilation will abort with a parse error.
- You can pass a third argument as the error message raised and shown to the user:
+If a third argument is specified, this will be the error message raised and seen by the user.
```
+ # Defaults to end of path
validate_cmd($sudoerscontent, '/usr/sbin/visudo -c -f', 'Visudo failed to validate sudoers content')
```
-
+ ```
+ # % as file location
+ validate_cmd($haproxycontent, '/usr/sbin/haproxy -f % -c', 'Haproxy failed to validate config content')
+ ```
+
*Type*: statement
* `validate_hash`: Validates that all passed values are hash data structures. Abort catalog compilation if any value fails this check.
@@ -576,7 +610,7 @@ The first argument of this function should be the string to test, and the second
$undefined = undef
validate_hash($undefined)
```
-
+
*Type*: statement
* `validate_integer`: Validate that the first argument is an integer (or an array of integers). Abort catalog compilation if any of the checks fail.
@@ -669,8 +703,8 @@ of the regular expressions match the string passed in, compilation aborts with a
validate_re('one', [ '^two', '^three' ])
```
- To set the error message:
-
+ To set the error message:
+
```
validate_re($::puppetversion, '^2.7', 'The $puppetversion fact value does not match 2.7')
```
@@ -680,19 +714,19 @@ of the regular expressions match the string passed in, compilation aborts with a
* `validate_slength`: Validates that the first argument is a string (or an array of strings), and is less than or equal to the length of the second argument. It fails if the first argument is not a string or array of strings, or if arg 2 is not convertable to a number.
The following values pass:
-
+
```
validate_slength("discombobulate",17)
validate_slength(["discombobulate","moo"],17)
```
-
+
The following values fail:
```
validate_slength("discombobulate",1)
validate_slength(["discombobulate","thermometer"],5)
```
-
+
*Type*: statement
* `validate_string`: Validates that all passed values are string data structures. Aborts catalog compilation if any value fails this check.
@@ -746,9 +780,11 @@ of the regular expressions match the string passed in, compilation aborts with a
##Limitations
+As of Puppet Enterprise version 3.7, the stdlib module is no longer included in PE. PE users should install the most recent release of stdlib for compatibility with Puppet modules.
+
###Version Compatibility
-Versions | Puppet 2.6 | Puppet 2.7 | Puppet 3.x | Puppet 4.x |
+Versions | Puppet 2.6 | Puppet 2.7 | Puppet 3.x | Puppet 4.x |
:---------------|:-----:|:---:|:---:|:----:
**stdlib 2.x** | **yes** | **yes** | no | no
**stdlib 3.x** | no | **yes** | **yes** | no
diff --git a/lib/facter/facter_dot_d.rb b/lib/facter/facter_dot_d.rb
index 2c096b0..b058437 100644
--- a/lib/facter/facter_dot_d.rb
+++ b/lib/facter/facter_dot_d.rb
@@ -15,7 +15,7 @@
class Facter::Util::DotD
require 'yaml'
- def initialize(dir="/etc/facts.d", cache_file="/tmp/facts_cache.yml")
+ def initialize(dir="/etc/facts.d", cache_file=File.join(Puppet[:libdir], "facts_dot_d.cache"))
@dir = dir
@cache_file = cache_file
@cache = nil
@@ -23,7 +23,7 @@ class Facter::Util::DotD
end
def entries
- Dir.entries(@dir).reject{|f| f =~ /^\.|\.ttl$/}.sort.map {|f| File.join(@dir, f) }
+ Dir.entries(@dir).reject { |f| f =~ /^\.|\.ttl$/ }.sort.map { |f| File.join(@dir, f) }
rescue
[]
end
@@ -113,7 +113,7 @@ class Facter::Util::DotD
def cache_save!
cache = load_cache
- File.open(@cache_file, "w", 0600) {|f| f.write(YAML.dump(cache)) }
+ File.open(@cache_file, "w", 0600) { |f| f.write(YAML.dump(cache)) }
rescue
end
diff --git a/lib/puppet/functions/type_of.rb b/lib/puppet/functions/type_of.rb
new file mode 100644
index 0000000..02cdd4d
--- /dev/null
+++ b/lib/puppet/functions/type_of.rb
@@ -0,0 +1,17 @@
+# Returns the type when passed a value.
+#
+# @example how to compare values' types
+# # compare the types of two values
+# if type_of($first_value) != type_of($second_value) { fail("first_value and second_value are different types") }
+# @example how to compare against an abstract type
+# unless type_of($first_value) <= Numeric { fail("first_value must be Numeric") }
+# unless type_of{$first_value) <= Collection[1] { fail("first_value must be an Array or Hash, and contain at least one element") }
+#
+# See the documentation for "The Puppet Type System" for more information about types.
+# See the `assert_type()` function for flexible ways to assert the type of a value.
+#
+Puppet::Functions.create_function(:type_of) do
+ def type_of(value)
+ Puppet::Pops::Types::TypeCalculator.infer_set(value)
+ end
+end
diff --git a/lib/puppet/parser/functions/basename.rb b/lib/puppet/parser/functions/basename.rb
new file mode 100644
index 0000000..f7e4438
--- /dev/null
+++ b/lib/puppet/parser/functions/basename.rb
@@ -0,0 +1,34 @@
+module Puppet::Parser::Functions
+ newfunction(:basename, :type => :rvalue, :doc => <<-EOS
+ Strips directory (and optional suffix) from a filename
+ EOS
+ ) do |arguments|
+
+ if arguments.size < 1 then
+ raise(Puppet::ParseError, "basename(): No arguments given")
+ elsif arguments.size > 2 then
+ raise(Puppet::ParseError, "basename(): Too many arguments given (#{arguments.size})")
+ else
+
+ unless arguments[0].is_a?(String)
+ raise(Puppet::ParseError, 'basename(): Requires string as first argument')
+ end
+
+ if arguments.size == 1 then
+ rv = File.basename(arguments[0])
+ elsif arguments.size == 2 then
+
+ unless arguments[1].is_a?(String)
+ raise(Puppet::ParseError, 'basename(): Requires string as second argument')
+ end
+
+ rv = File.basename(arguments[0], arguments[1])
+ end
+
+ end
+
+ return rv
+ end
+end
+
+# vim: set ts=2 sw=2 et :
diff --git a/lib/puppet/parser/functions/ceiling.rb b/lib/puppet/parser/functions/ceiling.rb
new file mode 100644
index 0000000..5f3b10b
--- /dev/null
+++ b/lib/puppet/parser/functions/ceiling.rb
@@ -0,0 +1,25 @@
+module Puppet::Parser::Functions
+ newfunction(:ceiling, :type => :rvalue, :doc => <<-EOS
+ Returns the smallest integer greater or equal to the argument.
+ Takes a single numeric value as an argument.
+ EOS
+ ) do |arguments|
+
+ raise(Puppet::ParseError, "ceiling(): Wrong number of arguments " +
+ "given (#{arguments.size} for 1)") if arguments.size != 1
+
+ begin
+ arg = Float(arguments[0])
+ rescue TypeError, ArgumentError => e
+ raise(Puppet::ParseError, "ceiling(): Wrong argument type " +
+ "given (#{arguments[0]} for Numeric)")
+ end
+
+ raise(Puppet::ParseError, "ceiling(): Wrong argument type " +
+ "given (#{arg.class} for Numeric)") if arg.is_a?(Numeric) == false
+
+ arg.ceil
+ end
+end
+
+# vim: set ts=2 sw=2 et :
diff --git a/lib/puppet/parser/functions/concat.rb b/lib/puppet/parser/functions/concat.rb
index 0d35b07..618e62d 100644
--- a/lib/puppet/parser/functions/concat.rb
+++ b/lib/puppet/parser/functions/concat.rb
@@ -4,31 +4,35 @@
module Puppet::Parser::Functions
newfunction(:concat, :type => :rvalue, :doc => <<-EOS
-Appends the contents of array 2 onto array 1.
+Appends the contents of multiple arrays into array 1.
*Example:*
- concat(['1','2','3'],['4','5','6'])
+ concat(['1','2','3'],['4','5','6'],['7','8','9'])
Would result in:
- ['1','2','3','4','5','6']
+ ['1','2','3','4','5','6','7','8','9']
EOS
) do |arguments|
- # Check that 2 arguments have been given ...
+ # Check that more than 2 arguments have been given ...
raise(Puppet::ParseError, "concat(): Wrong number of arguments " +
- "given (#{arguments.size} for 2)") if arguments.size != 2
+ "given (#{arguments.size} for < 2)") if arguments.size < 2
a = arguments[0]
- b = arguments[1]
# Check that the first parameter is an array
unless a.is_a?(Array)
raise(Puppet::ParseError, 'concat(): Requires array to work with')
end
- result = a + Array(b)
+ result = a
+ arguments.shift
+
+ arguments.each do |x|
+ result = result + Array(x)
+ end
return result
end
diff --git a/lib/puppet/parser/functions/delete.rb b/lib/puppet/parser/functions/delete.rb
index d03a293..f548b44 100644
--- a/lib/puppet/parser/functions/delete.rb
+++ b/lib/puppet/parser/functions/delete.rb
@@ -17,27 +17,30 @@ string, or key from a hash.
delete({'a'=>1,'b'=>2,'c'=>3}, 'b')
Would return: {'a'=>1,'c'=>3}
+ delete({'a'=>1,'b'=>2,'c'=>3}, ['b','c'])
+ Would return: {'a'=>1}
+
delete('abracadabra', 'bra')
Would return: 'acada'
- EOS
+ EOS
) do |arguments|
if (arguments.size != 2) then
raise(Puppet::ParseError, "delete(): Wrong number of arguments "+
- "given #{arguments.size} for 2.")
+ "given #{arguments.size} for 2.")
end
collection = arguments[0].dup
- item = arguments[1]
-
- case collection
- when Array, Hash
- collection.delete item
- when String
- collection.gsub! item, ''
- else
- raise(TypeError, "delete(): First argument must be an Array, " +
- "String, or Hash. Given an argument of class #{collection.class}.")
+ Array(arguments[1]).each do |item|
+ case collection
+ when Array, Hash
+ collection.delete item
+ when String
+ collection.gsub! item, ''
+ else
+ raise(TypeError, "delete(): First argument must be an Array, " +
+ "String, or Hash. Given an argument of class #{collection.class}.")
+ end
end
collection
end
diff --git a/lib/puppet/parser/functions/ensure_resource.rb b/lib/puppet/parser/functions/ensure_resource.rb
index 05e5593..1ba6a44 100644
--- a/lib/puppet/parser/functions/ensure_resource.rb
+++ b/lib/puppet/parser/functions/ensure_resource.rb
@@ -36,8 +36,9 @@ ENDOFDOC
items.each do |item|
Puppet::Parser::Functions.function(:defined_with_params)
if function_defined_with_params(["#{type}[#{item}]", params])
- Puppet.debug("Resource #{type}[#{item}] not created because it already exists")
+ Puppet.debug("Resource #{type}[#{item}] with params #{params} not created because it already exists")
else
+ Puppet.debug("Create new resource #{type}[#{item}] with params #{params}")
Puppet::Parser::Functions.function(:create_resources)
function_create_resources([type.capitalize, { item => params }])
end
diff --git a/lib/puppet/parser/functions/fqdn_rotate.rb b/lib/puppet/parser/functions/fqdn_rotate.rb
index 7f4d37d..cf22d36 100644
--- a/lib/puppet/parser/functions/fqdn_rotate.rb
+++ b/lib/puppet/parser/functions/fqdn_rotate.rb
@@ -31,8 +31,20 @@ Rotates an array a random number of times based on a nodes fqdn.
elements = result.size
- srand(Digest::MD5.hexdigest([lookupvar('::fqdn'),arguments].join(':')).hex)
- rand(elements).times {
+ seed = Digest::MD5.hexdigest([lookupvar('::fqdn'),arguments].join(':')).hex
+ # deterministic_rand() was added in Puppet 3.2.0; reimplement if necessary
+ if Puppet::Util.respond_to?(:deterministic_rand)
+ offset = Puppet::Util.deterministic_rand(seed, elements).to_i
+ else
+ if defined?(Random) == 'constant' && Random.class == Class
+ offset = Random.new(seed).rand(elements)
+ else
+ srand(seed)
+ offset = rand(elements)
+ srand()
+ end
+ end
+ offset.times {
result.push result.shift
}
diff --git a/lib/puppet/parser/functions/is_domain_name.rb b/lib/puppet/parser/functions/is_domain_name.rb
index b3fee96..2860ded 100644
--- a/lib/puppet/parser/functions/is_domain_name.rb
+++ b/lib/puppet/parser/functions/is_domain_name.rb
@@ -13,16 +13,16 @@ Returns true if the string passed to this function is a syntactically correct do
"given #{arguments.size} for 1")
end
- domain = arguments[0]
+ # Only allow string types
+ return false unless arguments[0].is_a?(String)
+
+ domain = arguments[0].dup
# Limits (rfc1035, 3.1)
domain_max_length=255
label_min_length=1
label_max_length=63
- # Only allow string types
- return false unless domain.is_a?(String)
-
# Allow ".", it is the top level domain
return true if domain == '.'
diff --git a/lib/puppet/parser/functions/prefix.rb b/lib/puppet/parser/functions/prefix.rb
index d02286a..ac1c58a 100644
--- a/lib/puppet/parser/functions/prefix.rb
+++ b/lib/puppet/parser/functions/prefix.rb
@@ -4,7 +4,7 @@
module Puppet::Parser::Functions
newfunction(:prefix, :type => :rvalue, :doc => <<-EOS
-This function applies a prefix to all elements in an array.
+This function applies a prefix to all elements in an array or a hash.
*Examples:*
@@ -18,10 +18,10 @@ Will return: ['pa','pb','pc']
raise(Puppet::ParseError, "prefix(): Wrong number of arguments " +
"given (#{arguments.size} for 1)") if arguments.size < 1
- array = arguments[0]
+ enumerable = arguments[0]
- unless array.is_a?(Array)
- raise Puppet::ParseError, "prefix(): expected first argument to be an Array, got #{array.inspect}"
+ unless enumerable.is_a?(Array) or enumerable.is_a?(Hash)
+ raise Puppet::ParseError, "prefix(): expected first argument to be an Array or a Hash, got #{enumerable.inspect}"
end
prefix = arguments[1] if arguments[1]
@@ -32,10 +32,17 @@ Will return: ['pa','pb','pc']
end
end
- # Turn everything into string same as join would do ...
- result = array.collect do |i|
- i = i.to_s
- prefix ? prefix + i : i
+ if enumerable.is_a?(Array)
+ # Turn everything into string same as join would do ...
+ result = enumerable.collect do |i|
+ i = i.to_s
+ prefix ? prefix + i : i
+ end
+ else
+ result = Hash[enumerable.map do |k,v|
+ k = k.to_s
+ [ prefix ? prefix + k : k, v ]
+ end]
end
return result
diff --git a/lib/puppet/parser/functions/type.rb b/lib/puppet/parser/functions/type.rb
index 8d85f11..016529b 100644
--- a/lib/puppet/parser/functions/type.rb
+++ b/lib/puppet/parser/functions/type.rb
@@ -4,46 +4,15 @@
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
-* boolean
+ DEPRECATED: This function will cease to function on Puppet 4; please use type3x() before upgrading to puppet 4 for backwards-compatibility, or migrate to the new parser's typing system.
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 [TrueClass, FalseClass, Array, Bignum, Fixnum, Float, Hash, String].include?(klass)
- raise(Puppet::ParseError, 'type(): Unknown type')
- end
-
- klass = klass.to_s # Ugly ...
+ ) do |args|
- # 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
+ warning("type() DEPRECATED: This function will cease to function on Puppet 4; please use type3x() before upgrading to puppet 4 for backwards-compatibility, or migrate to the new parser's typing system.")
+ if ! Puppet::Parser::Functions.autoloader.loaded?(:type3x)
+ Puppet::Parser::Functions.autoloader.load(:type3x)
end
-
- 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
+ function_type3x(args + [false])
end
end
diff --git a/lib/puppet/parser/functions/type3x.rb b/lib/puppet/parser/functions/type3x.rb
new file mode 100644
index 0000000..0800b4a
--- /dev/null
+++ b/lib/puppet/parser/functions/type3x.rb
@@ -0,0 +1,51 @@
+#
+# type3x.rb
+#
+
+module Puppet::Parser::Functions
+ newfunction(:type3x, :type => :rvalue, :doc => <<-EOS
+DEPRECATED: This function will be removed when puppet 3 support is dropped; please migrate to the new parser's typing system.
+
+Returns the type when passed a value. Type can be one of:
+
+* string
+* array
+* hash
+* float
+* integer
+* boolean
+ EOS
+ ) do |args|
+ raise(Puppet::ParseError, "type3x(): Wrong number of arguments " +
+ "given (#{args.size} for 1)") if args.size < 1
+
+ value = args[0]
+
+ klass = value.class
+
+ if not [TrueClass, FalseClass, Array, Bignum, Fixnum, Float, Hash, String].include?(klass)
+ raise(Puppet::ParseError, 'type3x(): 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'
+ when /^(?:True|False)Class$/ then 'boolean'
+ else klass
+ end
+
+ 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
+
+# vim: set ts=2 sw=2 et :
diff --git a/lib/puppet/parser/functions/upcase.rb b/lib/puppet/parser/functions/upcase.rb
index 4302b29..0226a88 100644
--- a/lib/puppet/parser/functions/upcase.rb
+++ b/lib/puppet/parser/functions/upcase.rb
@@ -13,22 +13,27 @@ Converts a string or an array of strings to uppercase.
Will return:
ASDF
- EOS
+ EOS
) do |arguments|
raise(Puppet::ParseError, "upcase(): Wrong number of arguments " +
- "given (#{arguments.size} for 1)") if arguments.size < 1
+ "given (#{arguments.size} for 1)") if arguments.size != 1
value = arguments[0]
- unless value.is_a?(Array) || value.is_a?(String)
- raise(Puppet::ParseError, 'upcase(): Requires either ' +
- 'array or string to work with')
+ unless value.is_a?(Array) || value.is_a?(Hash) || value.respond_to?(:upcase)
+ raise(Puppet::ParseError, 'upcase(): Requires an ' +
+ 'array, hash or object that responds to upcase in order to work')
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 }
+ result = value.collect { |i| function_upcase([i]) }
+ elsif value.is_a?(Hash)
+ result = {}
+ value.each_pair do |k, v|
+ result[function_upcase([k])] = function_upcase([v])
+ end
else
result = value.upcase
end
diff --git a/lib/puppet/parser/functions/uriescape.rb b/lib/puppet/parser/functions/uriescape.rb
index a486eee..45bbed2 100644
--- a/lib/puppet/parser/functions/uriescape.rb
+++ b/lib/puppet/parser/functions/uriescape.rb
@@ -22,7 +22,7 @@ module Puppet::Parser::Functions
if value.is_a?(Array)
# Numbers in Puppet are often string-encoded which is troublesome ...
- result = value.collect { |i| i.is_a?(String) ? URI.escape(i,unsafe) : i }
+ result = value.collect { |i| i.is_a?(String) ? URI.escape(i) : i }
else
result = URI.escape(value)
end
diff --git a/lib/puppet/parser/functions/validate_absolute_path.rb b/lib/puppet/parser/functions/validate_absolute_path.rb
index fe27974..b696680 100644
--- a/lib/puppet/parser/functions/validate_absolute_path.rb
+++ b/lib/puppet/parser/functions/validate_absolute_path.rb
@@ -5,15 +5,20 @@ module Puppet::Parser::Functions
The following values will pass:
- $my_path = "C:/Program Files (x86)/Puppet Labs/Puppet"
+ $my_path = 'C:/Program Files (x86)/Puppet Labs/Puppet'
validate_absolute_path($my_path)
- $my_path2 = "/var/lib/puppet"
+ $my_path2 = '/var/lib/puppet'
validate_absolute_path($my_path2)
-
+ $my_path3 = ['C:/Program Files (x86)/Puppet Labs/Puppet','C:/Program Files/Puppet Labs/Puppet']
+ validate_absolute_path($my_path3)
+ $my_path4 = ['/var/lib/puppet','/usr/share/puppet']
+ validate_absolute_path($my_path4)
The following values will fail, causing compilation to abort:
validate_absolute_path(true)
+ validate_absolute_path('../var/lib/puppet')
+ validate_absolute_path('var/lib/puppet')
validate_absolute_path([ 'var/lib/puppet', '/var/foo' ])
validate_absolute_path([ '/var/lib/puppet', 'var/foo' ])
$undefined = undef
@@ -28,28 +33,36 @@ module Puppet::Parser::Functions
end
args.each do |arg|
- # This logic was borrowed from
- # [lib/puppet/file_serving/base.rb](https://github.com/puppetlabs/puppet/blob/master/lib/puppet/file_serving/base.rb)
-
- # Puppet 2.7 and beyond will have Puppet::Util.absolute_path? Fall back to a back-ported implementation otherwise.
- if Puppet::Util.respond_to?(:absolute_path?) then
- unless Puppet::Util.absolute_path?(arg, :posix) or Puppet::Util.absolute_path?(arg, :windows)
- raise Puppet::ParseError, ("#{arg.inspect} is not an absolute path.")
+ # put arg to candidate var to be able to replace it
+ candidates = arg
+ # if arg is just a string with a path to test, convert it to an array
+ # to avoid test code duplication
+ unless arg.is_a?(Array) then
+ candidates = Array.new(1,arg)
+ end
+ # iterate over all pathes within the candidates array
+ candidates.each do |path|
+ # This logic was borrowed from
+ # [lib/puppet/file_serving/base.rb](https://github.com/puppetlabs/puppet/blob/master/lib/puppet/file_serving/base.rb)
+ # Puppet 2.7 and beyond will have Puppet::Util.absolute_path? Fall back to a back-ported implementation otherwise.
+ if Puppet::Util.respond_to?(:absolute_path?) then
+ unless Puppet::Util.absolute_path?(path, :posix) or Puppet::Util.absolute_path?(path, :windows)
+ raise Puppet::ParseError, ("#{path.inspect} is not an absolute path.")
+ end
+ else
+ # This code back-ported from 2.7.x's lib/puppet/util.rb Puppet::Util.absolute_path?
+ # Determine in a platform-specific way whether a path is absolute. This
+ # defaults to the local platform if none is specified.
+ # Escape once for the string literal, and once for the regex.
+ slash = '[\\\\/]'
+ name = '[^\\\\/]+'
+ regexes = {
+ :windows => %r!^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))!i,
+ :posix => %r!^/!,
+ }
+ rval = (!!(path =~ regexes[:posix])) || (!!(path =~ regexes[:windows]))
+ rval or raise Puppet::ParseError, ("#{path.inspect} is not an absolute path.")
end
- else
- # This code back-ported from 2.7.x's lib/puppet/util.rb Puppet::Util.absolute_path?
- # Determine in a platform-specific way whether a path is absolute. This
- # defaults to the local platform if none is specified.
- # Escape once for the string literal, and once for the regex.
- slash = '[\\\\/]'
- name = '[^\\\\/]+'
- regexes = {
- :windows => %r!^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))!i,
- :posix => %r!^/!,
- }
-
- rval = (!!(arg =~ regexes[:posix])) || (!!(arg =~ regexes[:windows]))
- rval or raise Puppet::ParseError, ("#{arg.inspect} is not an absolute path.")
end
end
end
diff --git a/lib/puppet/parser/functions/validate_cmd.rb b/lib/puppet/parser/functions/validate_cmd.rb
index c6136a5..5df3c60 100644
--- a/lib/puppet/parser/functions/validate_cmd.rb
+++ b/lib/puppet/parser/functions/validate_cmd.rb
@@ -6,9 +6,9 @@ module Puppet::Parser::Functions
Perform validation of a string with an external command.
The first argument of this function should be a string to
test, and the second argument should be a path to a test command
- taking a file as last argument. If the command, launched against
- a tempfile containing the passed string, returns a non-null value,
- compilation will abort with a parse error.
+ taking a % as a placeholder for the file path (will default to the end).
+ If the command, launched against a tempfile containing the passed string,
+ returns a non-null value, compilation will abort with a parse error.
If a third argument is specified, this will be the error message raised and
seen by the user.
@@ -17,8 +17,12 @@ module Puppet::Parser::Functions
Example:
+ # Defaults to end of path
validate_cmd($sudoerscontent, '/usr/sbin/visudo -c -f', 'Visudo failed to validate sudoers content')
+ # % as file location
+ validate_cmd($haproxycontent, '/usr/sbin/haproxy -f % -c', 'Haproxy failed to validate config content')
+
ENDHEREDOC
if (args.length < 2) or (args.length > 3) then
raise Puppet::ParseError, ("validate_cmd(): wrong number of arguments (#{args.length}; must be 2 or 3)")
@@ -34,10 +38,17 @@ module Puppet::Parser::Functions
begin
tmpfile.write(content)
tmpfile.close
+
+ if checkscript =~ /\s%(\s|$)/
+ check_with_correct_location = checkscript.gsub(/%/,tmpfile.path)
+ else
+ check_with_correct_location = "#{checkscript} #{tmpfile.path}"
+ end
+
if Puppet::Util::Execution.respond_to?('execute')
- Puppet::Util::Execution.execute("#{checkscript} #{tmpfile.path}")
+ Puppet::Util::Execution.execute(check_with_correct_location)
else
- Puppet::Util.execute("#{checkscript} #{tmpfile.path}")
+ Puppet::Util.execute(check_with_correct_location)
end
rescue Puppet::ExecutionFailure => detail
msg += "\n#{detail}"
diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb
index 9dbe43c..df263e6 100644
--- a/lib/puppet/type/file_line.rb
+++ b/lib/puppet/type/file_line.rb
@@ -71,12 +71,5 @@ Puppet::Type.newtype(:file_line) do
unless self[:line] and self[:path]
raise(Puppet::Error, "Both line and path are required attributes")
end
-
- if (self[:match])
- unless Regexp.new(self[:match]).match(self[:line])
- raise(Puppet::Error, "When providing a 'match' parameter, the value must be a regex that matches against the value of your 'line' parameter")
- end
- end
-
end
end
diff --git a/metadata.json b/metadata.json
index f049cfb..27def9c 100644
--- a/metadata.json
+++ b/metadata.json
@@ -1,10 +1,10 @@
{
"name": "puppetlabs-stdlib",
- "version": "4.4.0",
+ "version": "4.5.1",
"author": "puppetlabs",
- "summary": "Puppet Module Standard Library",
- "license": "Apache 2.0",
- "source": "git://github.com/puppetlabs/puppetlabs-stdlib",
+ "summary": "Standard library of resources for Puppet modules.",
+ "license": "Apache-2.0",
+ "source": "https://github.com/puppetlabs/puppetlabs-stdlib",
"project_page": "https://github.com/puppetlabs/puppetlabs-stdlib",
"issues_url": "https://tickets.puppetlabs.com/browse/MODULES",
"operatingsystem_support": [
diff --git a/spec/acceptance/ceiling_spec.rb b/spec/acceptance/ceiling_spec.rb
new file mode 100755
index 0000000..557986e
--- /dev/null
+++ b/spec/acceptance/ceiling_spec.rb
@@ -0,0 +1,39 @@
+#! /usr/bin/env ruby -S rspec
+require 'spec_helper_acceptance'
+
+describe 'ceiling function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
+ describe 'success' do
+ it 'ceilings floats' do
+ pp = <<-EOS
+ $a = 12.8
+ $b = 13
+ $o = ceiling($a)
+ if $o == $b {
+ notify { 'output correct': }
+ }
+ EOS
+
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).to match(/Notice: output correct/)
+ end
+ end
+ it 'ceilings integers' do
+ pp = <<-EOS
+ $a = 7
+ $b = 7
+ $o = ceiling($a)
+ if $o == $b {
+ notify { 'output correct': }
+ }
+ EOS
+
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).to match(/Notice: output correct/)
+ end
+ end
+ end
+ describe 'failure' do
+ it 'handles improper argument counts'
+ it 'handles non-numbers'
+ end
+end
diff --git a/spec/acceptance/concat_spec.rb b/spec/acceptance/concat_spec.rb
index 7bda365..06b649f 100755
--- a/spec/acceptance/concat_spec.rb
+++ b/spec/acceptance/concat_spec.rb
@@ -14,5 +14,27 @@ describe 'concat function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('oper
apply_manifest(pp, :catch_failures => true)
end
+ it 'should concat arrays and primitives to array' do
+ pp = <<-EOS
+ $output = concat(['1','2','3'],'4','5','6',['7','8','9'])
+ validate_array($output)
+ if size($output) != 9 {
+ fail("${output} should have 9 elements.")
+ }
+ EOS
+
+ apply_manifest(pp, :catch_failures => true)
+ end
+ it 'should concat multiple arrays to one' do
+ pp = <<-EOS
+ $output = concat(['1','2','3'],['4','5','6'],['7','8','9'])
+ validate_array($output)
+ if size($output) != 9 {
+ fail("${output} should have 9 elements.")
+ }
+ EOS
+
+ apply_manifest(pp, :catch_failures => true)
+ end
end
end
diff --git a/spec/acceptance/fqdn_rotate_spec.rb b/spec/acceptance/fqdn_rotate_spec.rb
index c37b35a..753068b 100755
--- a/spec/acceptance/fqdn_rotate_spec.rb
+++ b/spec/acceptance/fqdn_rotate_spec.rb
@@ -21,7 +21,7 @@ describe 'fqdn_rotate function', :unless => UNSUPPORTED_PLATFORMS.include?(fact(
after :each do
shell("if [ -f '#{facts_d}/fqdn.txt' ] ; then rm '#{facts_d}/fqdn.txt' ; fi")
end
- before :all do
+ before :each do
#No need to create on windows, PE creates by default
if fact('osfamily') !~ /windows/i
shell("mkdir -p '#{facts_d}'")
diff --git a/spec/functions/ceiling_spec.rb b/spec/functions/ceiling_spec.rb
new file mode 100755
index 0000000..814aa7c
--- /dev/null
+++ b/spec/functions/ceiling_spec.rb
@@ -0,0 +1,39 @@
+#! /usr/bin/env ruby -S rspec
+
+require 'spec_helper'
+
+describe "the ceiling function" do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+
+ it "should exist" do
+ expect(Puppet::Parser::Functions.function("ceiling")).to eq("function_ceiling")
+ end
+
+ it "should raise a ParseError if there is less than 1 argument" do
+ expect { scope.function_ceiling([]) }.to( raise_error(Puppet::ParseError, /Wrong number of arguments/))
+ end
+
+ it "should should raise a ParseError if input isn't numeric (eg. String)" do
+ expect { scope.function_ceiling(["foo"]) }.to( raise_error(Puppet::ParseError, /Wrong argument type/))
+ end
+
+ it "should should raise a ParseError if input isn't numeric (eg. Boolean)" do
+ expect { scope.function_ceiling([true]) }.to( raise_error(Puppet::ParseError, /Wrong argument type/))
+ end
+
+ it "should return an integer when a numeric type is passed" do
+ result = scope.function_ceiling([12.4])
+ expect(result.is_a?(Integer)).to(eq(true))
+ end
+
+ it "should return the input when an integer is passed" do
+ result = scope.function_ceiling([7])
+ expect(result).to(eq(7))
+ end
+
+ it "should return the smallest integer greater than or equal to the input" do
+ result = scope.function_ceiling([3.8])
+ expect(result).to(eq(4))
+ end
+end
+
diff --git a/spec/functions/concat_spec.rb b/spec/functions/concat_spec.rb
index 49cb2ad..49fa6bb 100755
--- a/spec/functions/concat_spec.rb
+++ b/spec/functions/concat_spec.rb
@@ -4,14 +4,19 @@ require 'spec_helper'
describe "the concat function" do
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
- it "should raise a ParseError if the client does not provide two arguments" do
+ it "should raise a ParseError if the client does not provide at least two arguments" do
expect { scope.function_concat([]) }.to(raise_error(Puppet::ParseError))
+ expect { scope.function_concat([[1]]) }.to(raise_error(Puppet::ParseError))
end
it "should raise a ParseError if the first parameter is not an array" do
expect { scope.function_concat([1, []])}.to(raise_error(Puppet::ParseError))
end
+ it "should not raise a ParseError if the client provides more than two arguments" do
+ expect { scope.function_concat([[1],[2],[3]]) }.not_to raise_error
+ end
+
it "should be able to concat an array" do
result = scope.function_concat([['1','2','3'],['4','5','6']])
expect(result).to(eq(['1','2','3','4','5','6']))
@@ -32,4 +37,14 @@ describe "the concat function" do
result = scope.function_concat([array_original,['4','5','6']])
array_original.should(eq(['1','2','3']))
end
+
+ it "should be able to concat multiple arrays" do
+ result = scope.function_concat([['1','2','3'],['4','5','6'],['7','8','9']])
+ expect(result).to(eq(['1','2','3','4','5','6','7','8','9']))
+ end
+
+ it "should be able to concat mix of primitives and arrays to a final array" do
+ result = scope.function_concat([['1','2','3'],'4',['5','6','7']])
+ expect(result).to(eq(['1','2','3','4','5','6','7']))
+ end
end
diff --git a/spec/functions/delete_spec.rb b/spec/functions/delete_spec.rb
index 39b3176..c8edd78 100755
--- a/spec/functions/delete_spec.rb
+++ b/spec/functions/delete_spec.rb
@@ -9,48 +9,53 @@ describe "the delete function" do
end
it "should raise a ParseError if there are fewer than 2 arguments" do
- expect { scope.function_delete([]) }.to( raise_error(Puppet::ParseError))
+ expect { scope.function_delete([]) }.to(raise_error(Puppet::ParseError))
end
it "should raise a ParseError if there are greater than 2 arguments" do
- expect { scope.function_delete([[], 'foo', 'bar']) }.to( raise_error(Puppet::ParseError))
+ expect { scope.function_delete([[], 'foo', 'bar']) }.to(raise_error(Puppet::ParseError))
end
it "should raise a TypeError if a number is passed as the first argument" do
- expect { scope.function_delete([1, 'bar']) }.to( raise_error(TypeError))
+ expect { scope.function_delete([1, 'bar']) }.to(raise_error(TypeError))
end
it "should delete all instances of an element from an array" do
- result = scope.function_delete([['a','b','c','b'],'b'])
- expect(result).to(eq(['a','c']))
+ result = scope.function_delete([['a', 'b', 'c', 'b'], 'b'])
+ expect(result).to(eq(['a', 'c']))
end
it "should delete all instances of a substring from a string" do
- result = scope.function_delete(['foobarbabarz','bar'])
+ result = scope.function_delete(['foobarbabarz', 'bar'])
expect(result).to(eq('foobaz'))
end
it "should delete a key from a hash" do
- result = scope.function_delete([{ 'a' => 1, 'b' => 2, 'c' => 3 },'b'])
- expect(result).to(eq({ 'a' => 1, 'c' => 3 }))
+ result = scope.function_delete([{'a' => 1, 'b' => 2, 'c' => 3}, 'b'])
+ expect(result).to(eq({'a' => 1, 'c' => 3}))
+ end
+
+ it 'should accept an array of items to delete' do
+ result = scope.function_delete([{'a' => 1, 'b' => 2, 'c' => 3}, ['b', 'c']])
+ expect(result).to(eq({'a' => 1}))
end
it "should not change origin array passed as argument" do
- origin_array = ['a','b','c','d']
+ origin_array = ['a', 'b', 'c', 'd']
result = scope.function_delete([origin_array, 'b'])
- expect(origin_array).to(eq(['a','b','c','d']))
+ expect(origin_array).to(eq(['a', 'b', 'c', 'd']))
end
it "should not change the origin string passed as argument" do
origin_string = 'foobarbabarz'
- result = scope.function_delete([origin_string,'bar'])
+ result = scope.function_delete([origin_string, 'bar'])
expect(origin_string).to(eq('foobarbabarz'))
end
it "should not change origin hash passed as argument" do
- origin_hash = { 'a' => 1, 'b' => 2, 'c' => 3 }
+ origin_hash = {'a' => 1, 'b' => 2, 'c' => 3}
result = scope.function_delete([origin_hash, 'b'])
- expect(origin_hash).to(eq({ 'a' => 1, 'b' => 2, 'c' => 3 }))
+ expect(origin_hash).to(eq({'a' => 1, 'b' => 2, 'c' => 3}))
end
end
diff --git a/spec/functions/fqdn_rotate_spec.rb b/spec/functions/fqdn_rotate_spec.rb
index 40057d4..673a8a3 100755
--- a/spec/functions/fqdn_rotate_spec.rb
+++ b/spec/functions/fqdn_rotate_spec.rb
@@ -40,4 +40,21 @@ describe "the fqdn_rotate function" do
result = scope.function_fqdn_rotate([value])
result.size.should(eq(4))
end
+
+ it "should use the Puppet::Util.deterministic_rand function if available" do
+ scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1")
+ if Puppet::Util.respond_to?(:deterministic_rand)
+ Puppet::Util.expects(:deterministic_rand).with(113646079810780526294648115052177588845,4)
+ end
+ scope.function_fqdn_rotate(["asdf"])
+ end
+
+ it "should not leave the global seed in a deterministic state" do
+ scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1").twice
+ scope.function_fqdn_rotate(["asdf"])
+ rand1 = rand()
+ scope.function_fqdn_rotate(["asdf"])
+ rand2 = rand()
+ expect(rand1).not_to eql(rand2)
+ end
end
diff --git a/spec/functions/is_domain_name_spec.rb b/spec/functions/is_domain_name_spec.rb
index 4d05f5c..5ab8369 100755
--- a/spec/functions/is_domain_name_spec.rb
+++ b/spec/functions/is_domain_name_spec.rb
@@ -61,4 +61,11 @@ describe "the is_domain_name function" do
result = scope.function_is_domain_name(["not valid"])
expect(result).to(be_falsey)
end
+
+ # Values obtained from Facter values will be frozen strings
+ # in newer versions of Facter:
+ it "should not throw an exception if passed a frozen string" do
+ result = scope.function_is_domain_name(["my.domain.name".freeze])
+ expect(result).to(be_truthy)
+ end
end
diff --git a/spec/functions/prefix_spec.rb b/spec/functions/prefix_spec.rb
index 34cac53..aec8a7d 100755
--- a/spec/functions/prefix_spec.rb
+++ b/spec/functions/prefix_spec.rb
@@ -25,4 +25,9 @@ describe "the prefix function" do
result = scope.function_prefix([['a','b','c'], 'p'])
expect(result).to(eq(['pa','pb','pc']))
end
+
+ it "returns a prefixed hash" do
+ result = scope.function_prefix([{'a' => 'b','b' => 'c','c' => 'd'}, 'p'])
+ expect(result).to(eq({'pa'=>'b','pb'=>'c','pc'=>'d'}))
+ end
end
diff --git a/spec/functions/type3x_spec.rb b/spec/functions/type3x_spec.rb
new file mode 100644
index 0000000..d21236a
--- /dev/null
+++ b/spec/functions/type3x_spec.rb
@@ -0,0 +1,43 @@
+#! /usr/bin/env ruby -S rspec
+require 'spec_helper'
+
+describe "the type3x function" do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+ it "should exist" do
+ expect(Puppet::Parser::Functions.function("type3x")).to eq("function_type3x")
+ end
+
+ it "should raise a ParseError if there is less than 1 arguments" do
+ expect { scope.function_type3x([]) }.to( raise_error(Puppet::ParseError))
+ end
+
+ it "should return string when given a string" do
+ result = scope.function_type3x(["aaabbbbcccc"])
+ expect(result).to(eq('string'))
+ end
+
+ it "should return array when given an array" do
+ result = scope.function_type3x([["aaabbbbcccc","asdf"]])
+ expect(result).to(eq('array'))
+ end
+
+ it "should return hash when given a hash" do
+ result = scope.function_type3x([{"a"=>1,"b"=>2}])
+ expect(result).to(eq('hash'))
+ end
+
+ it "should return integer when given an integer" do
+ result = scope.function_type3x(["1"])
+ expect(result).to(eq('integer'))
+ end
+
+ it "should return float when given a float" do
+ result = scope.function_type3x(["1.34"])
+ expect(result).to(eq('float'))
+ end
+
+ it "should return boolean when given a boolean" do
+ result = scope.function_type3x([true])
+ expect(result).to(eq('boolean'))
+ end
+end
diff --git a/spec/functions/type_spec.rb b/spec/functions/type_spec.rb
index 9dfe9d7..b683fcf 100755
--- a/spec/functions/type_spec.rb
+++ b/spec/functions/type_spec.rb
@@ -7,8 +7,9 @@ describe "the type function" do
expect(Puppet::Parser::Functions.function("type")).to eq("function_type")
end
- it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_type([]) }.to( raise_error(Puppet::ParseError))
+ it "should give a deprecation warning when called" do
+ scope.expects(:warning).with("type() DEPRECATED: This function will cease to function on Puppet 4; please use type3x() before upgrading to puppet 4 for backwards-compatibility, or migrate to the new parser's typing system.")
+ scope.function_type(["aoeu"])
end
it "should return string when given a string" do
diff --git a/spec/functions/upcase_spec.rb b/spec/functions/upcase_spec.rb
index 3cf8b05..0689099 100755
--- a/spec/functions/upcase_spec.rb
+++ b/spec/functions/upcase_spec.rb
@@ -9,7 +9,7 @@ describe "the upcase function" do
end
it "should raise a ParseError if there is less than 1 arguments" do
- expect { scope.function_upcase([]) }.to( raise_error(Puppet::ParseError))
+ expect { scope.function_upcase([]) }.to(raise_error(Puppet::ParseError))
end
it "should upcase a string" do
@@ -30,4 +30,29 @@ describe "the upcase function" do
result = scope.function_upcase([value])
result.should(eq('ABC'))
end
+
+ it 'should accept hashes and return uppercase' do
+ expect(
+ scope.function_upcase([{'test' => %w(this that and other thing)}])
+ ).to eq({'TEST' => %w(THIS THAT AND OTHER THING)})
+ end
+
+ if :test.respond_to?(:upcase)
+ it 'should accept hashes of symbols' do
+ expect(
+ scope.function_upcase([{:test => [:this, :that, :other]}])
+ ).to eq({:TEST => [:THIS, :THAT, :OTHER]})
+ end
+ it 'should return upcase symbol' do
+ expect(
+ scope.function_upcase([:test])
+ ).to eq(:TEST)
+ end
+ it 'should return mixed objects in upcease' do
+ expect(
+ scope.function_upcase([[:test, 'woot']])
+ ).to eq([:TEST, 'WOOT'])
+
+ end
+ end
end
diff --git a/spec/functions/uriescape_spec.rb b/spec/functions/uriescape_spec.rb
index 2321e5a..d0f37de 100755
--- a/spec/functions/uriescape_spec.rb
+++ b/spec/functions/uriescape_spec.rb
@@ -17,6 +17,13 @@ describe "the uriescape function" do
expect(result).to(eq(':/?%23[]@!$&\'()*+,;=%20%22%7B%7D'))
end
+ it "should uriescape an array of strings, while not touching up nonstrings" do
+ teststring = ":/?#[]@!$&'()*+,;= \"{}"
+ expectstring = ':/?%23[]@!$&\'()*+,;=%20%22%7B%7D'
+ result = scope.function_uriescape([[teststring, teststring, 1]])
+ expect(result).to(eq([expectstring, expectstring, 1]))
+ end
+
it "should do nothing if a string is already safe" do
result = scope.function_uriescape(["ABCdef"])
expect(result).to(eq('ABCdef'))
diff --git a/spec/functions/validate_absolute_path_spec.rb b/spec/functions/validate_absolute_path_spec.rb
index 342ae84..36c836b 100755
--- a/spec/functions/validate_absolute_path_spec.rb
+++ b/spec/functions/validate_absolute_path_spec.rb
@@ -39,6 +39,11 @@ describe Puppet::Parser::Functions.function(:validate_absolute_path) do
expect { subject.call [path] }.not_to raise_error
end
end
+ valid_paths do
+ it "validate_absolute_path(#{valid_paths.inspect}) should not fail" do
+ expect { subject.call [valid_paths] }.not_to raise_error
+ end
+ end
end
context "Puppet without mocking" do
@@ -47,6 +52,11 @@ describe Puppet::Parser::Functions.function(:validate_absolute_path) do
expect { subject.call [path] }.not_to raise_error
end
end
+ valid_paths do
+ it "validate_absolute_path(#{valid_paths.inspect}) should not fail" do
+ expect { subject.call [valid_paths] }.not_to raise_error
+ end
+ end
end
end
@@ -55,6 +65,7 @@ describe Puppet::Parser::Functions.function(:validate_absolute_path) do
[
nil,
[ nil ],
+ [ nil, nil ],
{ 'foo' => 'bar' },
{ },
'',
@@ -66,19 +77,28 @@ describe Puppet::Parser::Functions.function(:validate_absolute_path) do
end
context 'Relative paths' do
- %w{
- relative1
- .
- ..
- ./foo
- ../foo
- etc/puppetlabs/puppet
- opt/puppet/bin
- }.each do |path|
+ def self.rel_paths
+ %w{
+ relative1
+ .
+ ..
+ ./foo
+ ../foo
+ etc/puppetlabs/puppet
+ opt/puppet/bin
+ }
+ end
+ rel_paths.each do |path|
it "validate_absolute_path(#{path.inspect}) should fail" do
expect { subject.call [path] }.to raise_error Puppet::ParseError
end
end
+ rel_paths do
+ it "validate_absolute_path(#{rel_paths.inspect}) should fail" do
+ expect { subject.call [rel_paths] }.to raise_error Puppet::ParseError
+ end
+ end
end
end
end
+
diff --git a/spec/functions/validate_cmd_spec.rb b/spec/functions/validate_cmd_spec.rb
index a6e68df..7cb9782 100755
--- a/spec/functions/validate_cmd_spec.rb
+++ b/spec/functions/validate_cmd_spec.rb
@@ -12,37 +12,74 @@ describe Puppet::Parser::Functions.function(:validate_cmd) do
scope.method(function_name)
end
- describe "with an explicit failure message" do
- it "prints the failure message on error" do
- expect {
- subject.call ['', '/bin/false', 'failure message!']
- }.to raise_error Puppet::ParseError, /failure message!/
+ context 'with no % placeholder' do
+ describe "with an explicit failure message" do
+ it "prints the failure message on error" do
+ expect {
+ subject.call ['', '/bin/false', 'failure message!']
+ }.to raise_error Puppet::ParseError, /failure message!/
+ end
end
- end
- describe "on validation failure" do
- it "includes the command error output" do
- expect {
- subject.call ['', "#{TOUCHEXE} /cant/touch/this"]
- }.to raise_error Puppet::ParseError, /(cannot touch|o such file or)/
+ describe "on validation failure" do
+ it "includes the command error output" do
+ expect {
+ subject.call ['', "#{TOUCHEXE} /cant/touch/this"]
+ }.to raise_error Puppet::ParseError, /(cannot touch|o such file or)/
+ end
+
+ it "includes the command return value" do
+ expect {
+ subject.call ['', '/cant/run/this']
+ }.to raise_error Puppet::ParseError, /returned 1\b/
+ end
end
- it "includes the command return value" do
- expect {
- subject.call ['', '/cant/run/this']
- }.to raise_error Puppet::ParseError, /returned 1\b/
+ describe "when performing actual validation" do
+ it "can positively validate file content" do
+ expect { subject.call ["non-empty", "#{TESTEXE} -s"] }.to_not raise_error
+ end
+
+ it "can negatively validate file content" do
+ expect {
+ subject.call ["", "#{TESTEXE} -s"]
+ }.to raise_error Puppet::ParseError, /failed to validate.*test -s/
+ end
end
end
- describe "when performing actual validation" do
- it "can positively validate file content" do
- expect { subject.call ["non-empty", "#{TESTEXE} -s"] }.to_not raise_error
+ context 'with % placeholder' do
+ describe "with an explicit failure message" do
+ it "prints the failure message on error" do
+ expect {
+ subject.call ['', '/bin/false % -f', 'failure message!']
+ }.to raise_error Puppet::ParseError, /failure message!/
+ end
end
+ describe "on validation failure" do
+ it "includes the command error output" do
+ expect {
+ subject.call ['', "#{TOUCHEXE} /cant/touch/this"]
+ }.to raise_error Puppet::ParseError, /(cannot touch|o such file or)/
+ end
+
+ it "includes the command return value" do
+ expect {
+ subject.call ['', '/cant/run/this % -z']
+ }.to raise_error Puppet::ParseError, /Execution of '\/cant\/run\/this .+ -z' returned 1/
+ end
+ end
+
+ describe "when performing actual validation" do
+ it "can positively validate file content" do
+ expect { subject.call ["non-empty", "#{TESTEXE} -s %"] }.to_not raise_error
+ end
- it "can negatively validate file content" do
- expect {
- subject.call ["", "#{TESTEXE} -s"]
- }.to raise_error Puppet::ParseError, /failed to validate.*test -s/
+ it "can negatively validate file content" do
+ expect {
+ subject.call ["", "#{TESTEXE} -s %"]
+ }.to raise_error Puppet::ParseError, /failed to validate.*test -s/
+ end
end
end
end
diff --git a/spec/unit/puppet/functions/type_of_spec.rb b/spec/unit/puppet/functions/type_of_spec.rb
new file mode 100644
index 0000000..8afb624
--- /dev/null
+++ b/spec/unit/puppet/functions/type_of_spec.rb
@@ -0,0 +1,33 @@
+#! /usr/bin/env ruby -S rspec
+
+require 'spec_helper'
+
+if ENV["FUTURE_PARSER"] == 'yes' or Puppet.version >= "4"
+ require 'puppet/pops'
+ require 'puppet/loaders'
+
+ describe 'the type_of function' do
+ before(:all) do
+ loaders = Puppet::Pops::Loaders.new(Puppet::Node::Environment.create(:testing, [File.join(fixtures, "modules")]))
+ Puppet.push_context({:loaders => loaders}, "test-examples")
+ end
+
+ after(:all) do
+ Puppet::Pops::Loaders.clear
+ Puppet::pop_context()
+ end
+
+ let(:func) do
+ # Load the function from the environment modulepath's modules (ie, fixtures)
+ Puppet.lookup(:loaders).private_environment_loader.load(:function, 'type_of')
+ end
+
+ it 'gives the type of a string' do
+ expect(func.call({}, 'hello world')).to be_kind_of(Puppet::Pops::Types::PStringType)
+ end
+
+ it 'gives the type of an integer' do
+ expect(func.call({}, 5)).to be_kind_of(Puppet::Pops::Types::PIntegerType)
+ end
+ end
+end
diff --git a/spec/unit/puppet/parser/functions/basename_spec.rb b/spec/unit/puppet/parser/functions/basename_spec.rb
new file mode 100755
index 0000000..8a2d0dc
--- /dev/null
+++ b/spec/unit/puppet/parser/functions/basename_spec.rb
@@ -0,0 +1,46 @@
+#! /usr/bin/env ruby -S rspec
+require 'spec_helper'
+
+describe "the basename function" do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+
+ it "should exist" do
+ Puppet::Parser::Functions.function("basename").should == "function_basename"
+ end
+
+ it "should raise a ParseError if there is less than 1 argument" do
+ lambda { scope.function_basename([]) }.should( raise_error(Puppet::ParseError))
+ end
+
+ it "should raise a ParseError if there are more than 2 arguments" do
+ lambda { scope.function_basename(['a', 'b', 'c']) }.should( raise_error(Puppet::ParseError))
+ end
+
+ it "should return basename for an absolute path" do
+ result = scope.function_basename(['/path/to/a/file.ext'])
+ result.should(eq('file.ext'))
+ end
+
+ it "should return basename for a relative path" do
+ result = scope.function_basename(['path/to/a/file.ext'])
+ result.should(eq('file.ext'))
+ end
+
+ it "should strip extention when extension specified (absolute path)" do
+ result = scope.function_basename(['/path/to/a/file.ext', '.ext'])
+ result.should(eq('file'))
+ end
+
+ it "should strip extention when extension specified (relative path)" do
+ result = scope.function_basename(['path/to/a/file.ext', '.ext'])
+ result.should(eq('file'))
+ end
+
+ it "should complain about non-string first argument" do
+ lambda { scope.function_basename([[]]) }.should( raise_error(Puppet::ParseError))
+ end
+
+ it "should complain about non-string second argument" do
+ lambda { scope.function_basename(['/path/to/a/file.ext', []]) }.should( raise_error(Puppet::ParseError))
+ end
+end
diff --git a/spec/unit/puppet/type/file_line_spec.rb b/spec/unit/puppet/type/file_line_spec.rb
index 9ef49ef..410d0bf 100755
--- a/spec/unit/puppet/type/file_line_spec.rb
+++ b/spec/unit/puppet/type/file_line_spec.rb
@@ -15,14 +15,14 @@ describe Puppet::Type.type(:file_line) do
file_line[:match] = '^foo.*$'
expect(file_line[:match]).to eq('^foo.*$')
end
- it 'should not accept a match regex that does not match the specified line' do
+ it 'should accept a match regex that does not match the specified line' do
expect {
Puppet::Type.type(:file_line).new(
:name => 'foo',
:path => '/my/path',
:line => 'foo=bar',
:match => '^bar=blah$'
- )}.to raise_error(Puppet::Error, /the value must be a regex that matches/)
+ )}.not_to raise_error
end
it 'should accept a match regex that does match the specified line' do
expect {