From 76d0ec471d074a07cc2cece3963dd9c8c2d18aeb Mon Sep 17 00:00:00 2001 From: Emerson Prado Date: Fri, 3 Feb 2017 17:00:03 -0200 Subject: Include routine to converge ensure values 'present' and 'installed' If user declares ensure_package concurrently with ensure values 'present' and 'installed', function fails as if values were different Change causes function to interpret ensure => 'installed' as 'present', effectively elliminating the error Also works if user doesn't specify ensure value, since 'present' is the default --- lib/puppet/parser/functions/ensure_packages.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/puppet/parser/functions/ensure_packages.rb b/lib/puppet/parser/functions/ensure_packages.rb index 439af1e..93dd4fb 100644 --- a/lib/puppet/parser/functions/ensure_packages.rb +++ b/lib/puppet/parser/functions/ensure_packages.rb @@ -20,6 +20,9 @@ third argument to the ensure_resource() function. if arguments[0].is_a?(Hash) if arguments[1] defaults = { 'ensure' => 'present' }.merge(arguments[1]) + if defaults['ensure'] == 'installed' + defaults['ensure'] = 'present' + end else defaults = { 'ensure' => 'present' } end @@ -31,6 +34,9 @@ third argument to the ensure_resource() function. if arguments[1] defaults = { 'ensure' => 'present' }.merge(arguments[1]) + if defaults['ensure'] == 'installed' + defaults['ensure'] = 'present' + end else defaults = { 'ensure' => 'present' } end -- cgit v1.2.3 From 530d2ef184d19b965a4c9db2c415c26b80f9d31b Mon Sep 17 00:00:00 2001 From: Emerson Prado Date: Fri, 3 Feb 2017 17:05:52 -0200 Subject: Add spec test for present + installed convergence --- spec/functions/ensure_packages_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spec/functions/ensure_packages_spec.rb b/spec/functions/ensure_packages_spec.rb index 5d97684..357a6e1 100755 --- a/spec/functions/ensure_packages_spec.rb +++ b/spec/functions/ensure_packages_spec.rb @@ -41,4 +41,16 @@ describe 'ensure_packages' do it { expect(lambda { catalogue }).to contain_package('foo').with({'provider' => 'rpm', 'ensure' => 'present'}) } it { expect(lambda { catalogue }).to contain_package('bar').with({'provider' => 'gem', 'ensure' => 'present'}) } end + + context 'given a catalog with "package { puppet: ensure => present }"' do + let(:pre_condition) { 'package { puppet: ensure => present }' } + + describe 'after running ensure_package("puppet", { "ensure" => "installed" })' do + before { subject.call(['puppet', { "ensure" => "installed" }]) } + + # this lambda is required due to strangeness within rspec-puppet's expectation handling + it { expect(lambda { catalogue }).to contain_package('puppet').with_ensure('present') } + end + end + end -- cgit v1.2.3 From c4d84d49069dc0b2bcf834f5aec93ed9f7a65bcb Mon Sep 17 00:00:00 2001 From: Hunter Haugen Date: Thu, 13 Apr 2017 16:36:59 -0700 Subject: (maint) Stdlib::Compat::Integer accepts numbers with newlines apparently --- spec/aliases/integer_spec.rb | 4 +++- spec/functions/is_integer_spec.rb | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/spec/aliases/integer_spec.rb b/spec/aliases/integer_spec.rb index bd00c2a..8cb4658 100644 --- a/spec/aliases/integer_spec.rb +++ b/spec/aliases/integer_spec.rb @@ -8,6 +8,8 @@ if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 '3', -3, '-3', + "123\nfoo", + "foo\n123", ].each do |value| describe value.inspect do let(:params) {{ value: value }} @@ -17,7 +19,7 @@ if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 end describe 'rejects other values' do - [ true, 'true', false, 'false', 'iAmAString', '1test', '1 test', 'test 1', 'test 1 test', {}, { 'key' => 'value' }, { 1=> 2 }, '', :undef , 'x', 3.7, '3.7',-3.7, '-342.2315e-12' ].each do |value| + [ "foo\nbar", true, 'true', false, 'false', 'iAmAString', '1test', '1 test', 'test 1', 'test 1 test', {}, { 'key' => 'value' }, { 1=> 2 }, '', :undef , 'x', 3.7, '3.7',-3.7, '-342.2315e-12' ].each do |value| describe value.inspect do let(:params) {{ value: value }} it { is_expected.to compile.and_raise_error(/parameter 'value' expects a value of type Integer, Pattern(\[.*\]+)?, or Array/) } diff --git a/spec/functions/is_integer_spec.rb b/spec/functions/is_integer_spec.rb index b296830..8118ef4 100755 --- a/spec/functions/is_integer_spec.rb +++ b/spec/functions/is_integer_spec.rb @@ -11,6 +11,8 @@ describe 'is_integer' do it { is_expected.to run.with_params('3').and_return(true) } it { is_expected.to run.with_params(-3).and_return(true) } it { is_expected.to run.with_params('-3').and_return(true) } + it { is_expected.to run.with_params("123\nfoo").and_return(true) } + it { is_expected.to run.with_params("foo\n123").and_return(true) } it { is_expected.to run.with_params(3.7).and_return(false) } it { is_expected.to run.with_params('3.7').and_return(false) } @@ -24,6 +26,7 @@ describe 'is_integer' do it { is_expected.to run.with_params(true).and_return(false) } it { is_expected.to run.with_params(false).and_return(false) } it { is_expected.to run.with_params('0001234').and_return(false) } + it { is_expected.to run.with_params("foo\nbar").and_return(false) } context 'Checking for deprecation warning' do after(:all) do -- cgit v1.2.3 From dd94758d9315502798df22b96fbc12bbde9fb884 Mon Sep 17 00:00:00 2001 From: David Gillies Date: Fri, 16 Jun 2017 14:40:46 -0700 Subject: Fix headers in CHANGELOG.md so that headers render correctly --- CHANGELOG.md | 96 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4930a9c..4b3a5a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,7 +57,7 @@ This release introduces multiple new functions, a new fact and the addition of U - Addition of FQDN UUID generation function. - Addition of Ubuntu Xenial to OS Support. -####Bugfixes +#### Bugfixes - Ensure_packages now works with Ruby < 2.0. - Updated the documentation of str2bool function. @@ -143,11 +143,11 @@ Special thanks to [Voxpupuli's](https://voxpupuli.org/) Igor Galić for donating * Puppet 4.5.0 (PE 2016.2) has a number of improvements around data types - especially error handling - that make working with them much nicer. ## Supported Release 4.12.0 -###Summary +### Summary This release provides several new functions, bugfixes, modulesync changes, and some documentation updates. -####Features +#### Features - Adds `clamp`. This function keeps values within a specified range. - Adds `validate_x509_rsa_key_pair`. This function validates an x509 RSA certificate and key pair. - Adds `dig`. This function performs a deep lookup in nested hashes or arrays. @@ -159,7 +159,7 @@ This release provides several new functions, bugfixes, modulesync changes, and s - Apply modulesync changes. - Add validate_email_address function. -####Bugfixes +#### Bugfixes - Fixes `fqdn_rand_string` tests, since Puppet 4.4.0 and later have a higher `fqdn_rand` ceiling. - (MODULES-3152) Adds a check to `package_provider` to prevent failures if Gem is not installed. - Fixes to README.md. @@ -168,39 +168,39 @@ This release provides several new functions, bugfixes, modulesync changes, and s - Fixes concat with Hash arguments. ## Supported Release 4.11.0 -###Summary +### Summary Provides a validate_absolute_paths and Debian 8 support. There is a fix to the is_package_provider fact and a test improvement. -####Features +#### Features - Adds new parser called is_absolute_path - Supports Debian 8 -####Bugfixes +#### Bugfixes - Allow package_provider fact to resolve on PE 3.x -####Improvements +#### Improvements - ensures that the test passes independently of changes to rubygems for ensure_resource -##2015-12-15 - Supported Release 4.10.0 -###Summary +## 2015-12-15 - Supported Release 4.10.0 +### Summary Includes the addition of several new functions and considerable improvements to the existing functions, tests and documentation. Includes some bug fixes which includes compatibility, test and fact issues. -####Features +#### Features - Adds service_provider fact - Adds is_a() function - Adds package_provider fact - Adds validate_ip_address function - Adds seeded_rand function -####Bugfixes +#### Bugfixes - Fix backwards compatibility from an improvement to the parseyaml function - Renaming of load_module_metadata test to include _spec.rb - Fix root_home fact on AIX 5.x, now '-c' rather than '-C' - Fixed Gemfile to work with ruby 1.8.7 -####Improvements +#### Improvements - (MODULES-2462) Improvement of parseyaml function - Improvement of str2bool function - Improvement to readme @@ -215,26 +215,26 @@ Includes the addition of several new functions and considerable improvements to - Improvement to naming convention in validate_ipv4_address function ## Supported Release 4.9.1 -###Summary +### Summary Small release for support of newer PE versions. This increments the version of PE in the metadata.json file. -##2015-09-08 - Supported Release 4.9.0 -###Summary +## 2015-09-08 - Supported Release 4.9.0 +### Summary This release adds new features including the new functions dos2unix, unix2dos, try_get_value, convert_base as well as other features and improvements. -####Features +#### Features - (MODULES-2370) allow `match` parameter to influence `ensure => absent` behavior - (MODULES-2410) Add new functions dos2unix and unix2dos - (MODULE-2456) Modify union to accept more than two arrays - Adds a convert_base function, which can convert numbers between bases - Add a new function "try_get_value" -####Bugfixes +#### Bugfixes - n/a -####Improvements +#### Improvements - (MODULES-2478) Support root_home fact on AIX through "lsuser" command - Acceptance test improvements - Unit test improvements @@ -253,15 +253,15 @@ This release adds a function for reading metadata.json from any module, and expa - Fix various docs typos - Fix `file_line` resource on puppet < 3.3 -##2015-06-22 - Supported Release 4.7.0 -###Summary +## 2015-06-22 - Supported Release 4.7.0 +### Summary Adds Solaris 12 support along with improved Puppet 4 support. There are significant test improvements, and some minor fixes. -####Features +#### Features - Add support for Solaris 12 -####Bugfixes +#### Bugfixes - Fix for AIO Puppet 4 - Fix time for ruby 1.8.7 - Specify rspec-puppet version @@ -271,7 +271,7 @@ Adds Solaris 12 support along with improved Puppet 4 support. There are signific - catch and rescue from looking up non-existent facts - Use puppet_install_helper, for Puppet 4 -####Improvements +#### Improvements - Enforce support for Puppet 4 testing - fqdn_rotate/fqdn_rand_string acceptance tests and implementation - Simplify mac address regex @@ -282,12 +282,12 @@ Adds Solaris 12 support along with improved Puppet 4 support. There are signific - Add validate_slength optional 3rd arg - Move tests directory to examples directory -##2015-04-14 - Supported Release 4.6.0 -###Summary +## 2015-04-14 - Supported Release 4.6.0 +### Summary Adds functions and function argument abilities, and improves compatibility with the new puppet parser -####Features +#### Features - MODULES-444: `concat()` can now take more than two arrays - `basename()` added to have Ruby File.basename functionality - `delete()` can now take an array of items to remove @@ -304,7 +304,7 @@ Adds functions and function argument abilities, and improves compatibility with - Adds `validate_integer()` - Adds `validate_numeric()` (like `validate_integer()` but also accepts floats) -####Bugfixes +#### Bugfixes - Fix seeding of `fqdn_rotate()` - `ensure_resource()` is more verbose on debug mode - Stricter argument checking for `dirname()` @@ -312,37 +312,37 @@ Adds functions and function argument abilities, and improves compatibility with - Fix `uriescape()` when called with array - Fix `file_line` resource when using the `after` attribute with `match` -##2015-01-14 - Supported Release 4.5.1 -###Summary +## 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 +#### Bugfixes - Facter_dot_d cache will now be stored in puppet libdir instead of tmp -##2014-12-15 - Supported Release 4.5.0 -###Summary +## 2014-12-15 - Supported Release 4.5.0 +### Summary This release improves functionality of the member function and adds improved future parser support. -####Features +#### Features - MODULES-1329: Update member() to allow the variable to be an array. - Sync .travis.yml, Gemfile, Rakefile, and CONTRIBUTING.md via modulesync -####Bugfixes +#### 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 +## 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. -####Features +#### Features - All new shiny README - New `private()` function for making private manifests (yay!) -####Bugfixes +#### Bugfixes - Code reuse in `bool2num()` and `zip()` - Fix many functions to handle `generate()` no longer returning a string on new puppets - `concat()` no longer modifies the first argument (whoops) @@ -354,20 +354,20 @@ This release has an overhauled readme, new private manifest function, and fixes - Fix `file_line` matching on older rubies -##2014-07-15 - Supported Release 4.3.2 -###Summary +## 2014-07-15 - Supported Release 4.3.2 +### Summary This release merely updates metadata.json so the module can be uninstalled and upgraded via the puppet module command. -##2014-07-14 - Supported Release 4.3.1 +## 2014-07-14 - Supported Release 4.3.1 ### Summary This supported release updates the metadata.json to work around upgrade behavior of the PMT. #### Bugfixes - Synchronize metadata.json with PMT-generated metadata to pass checksums -##2014-06-27 - Supported Release 4.3.0 +## 2014-06-27 - Supported Release 4.3.0 ### Summary This release is the first supported release of the stdlib 4 series. It remains backwards-compatible with the stdlib 3 series. It adds two new functions, one bugfix, and many testing updates. @@ -379,7 +379,7 @@ backwards-compatible with the stdlib 3 series. It adds two new functions, one bu #### Bugfixes - Fix `has_interface_with()` when interfaces fact is nil -##2014-06-04 - Release 4.2.2 +## 2014-06-04 - Release 4.2.2 ### Summary This release adds PE3.3 support in the metadata and fixes a few tests. @@ -423,14 +423,14 @@ This release adds many new functions and fixes, and continues to be backwards co - Add/update unit & acceptance tests. -##2014-03-04 - Supported Release - 3.2.1 -###Summary +## 2014-03-04 - Supported Release - 3.2.1 +### Summary This is a supported release -####Bugfixes +#### Bugfixes - Fixed `is_integer`/`is_float`/`is_numeric` for checking the value of arithmatic expressions. -####Known bugs +#### Known bugs * No known bugs --- -- cgit v1.2.3 From 30c0aeb693e218d6d416a9ada323740cd7b643a1 Mon Sep 17 00:00:00 2001 From: Hunter Haugen Date: Mon, 26 Jun 2017 14:25:51 -0700 Subject: (MODULES-5144) Prep for puppet 5 --- metadata.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metadata.json b/metadata.json index 0bd0184..f2b0204 100644 --- a/metadata.json +++ b/metadata.json @@ -101,7 +101,7 @@ "requirements": [ { "name": "puppet", - "version_requirement": ">= 4.7.0 < 5.0.0" + "version_requirement": ">= 4.7.0 < 6.0.0" } ], "description": "Standard Library for Puppet Modules", -- cgit v1.2.3 From ec6cd56576953c74b48ebbba477d89e138e91ace Mon Sep 17 00:00:00 2001 From: Reid Vandewiele Date: Wed, 28 Jun 2017 13:37:55 -0700 Subject: (MODULES-5113) Make line support Sensitive Do this by making it a property. The idea is to get rid of the warning that appears when a Sensitive value is passed to the line attribute. --- lib/puppet/type/file_line.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb index e82b246..3d691bf 100644 --- a/lib/puppet/type/file_line.rb +++ b/lib/puppet/type/file_line.rb @@ -104,8 +104,16 @@ Puppet::Type.newtype(:file_line) do ' This is also takes a regex.' end - newparam(:line) do + # The line property never changes; the type only ever performs a create() or + # destroy(). line is a property in order to allow it to correctly handle + # Sensitive type values. Because it is a property which will never change, + # it should never be considered out of sync. + newproperty(:line) do desc 'The line to be appended to the file or used to replace matches found by the match attribute.' + + def retrieve + @resource[:line] + end end newparam(:path) do -- cgit v1.2.3 From 740ca7dc8053be93e43392ca61b2f308c0596d19 Mon Sep 17 00:00:00 2001 From: Reid Vandewiele Date: Fri, 7 Apr 2017 15:13:59 -0700 Subject: (FACT-932) Add new function, fact() The fact() function allows dot-notation reference to facts. It is an alternative to using $facts directly with array-indexing. Array-indexing is often onerous to use since it doesn't align with how structured facts are accessed elsewhere in the ecosystem and if any element in a multi-step path doesn't exist, array indexing can cause a compilation failure. Example usage: fact('os.family') --- lib/puppet/functions/fact.rb | 58 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 lib/puppet/functions/fact.rb diff --git a/lib/puppet/functions/fact.rb b/lib/puppet/functions/fact.rb new file mode 100644 index 0000000..dfb048b --- /dev/null +++ b/lib/puppet/functions/fact.rb @@ -0,0 +1,58 @@ +# Digs into the facts hash using dot-notation +# +# Example usage: +# +# fact('osfamily') +# fact('os.architecture') +# +# Array indexing: +# +# fact('mountpoints."/dev".options.1') +# +# Fact containing a "." in the name: +# +# fact('vmware."VRA.version"') +# +Puppet::Functions.create_function(:fact) do + dispatch :fact do + param 'String', :fact_name + end + + def to_dot_syntax(array_path) + array_path.map do |string| + string.include?('.') ? %Q{"#{string}"} : string + end.join('.') + end + + def fact(fact_name) + facts = closure_scope['facts'] + + # Transform the dot-notation string into an array of paths to walk. Make + # sure to correctly extract double-quoted values containing dots as single + # elements in the path. + path = fact_name.scan(/([^."]+)|(?:")([^"]+)(?:")/).map {|x| x.compact.first } + + walked_path = [] + path.reduce(facts) do |d, k| + return nil if d.nil? || k.nil? + + case + when d.is_a?(Array) + begin + result = d[Integer(k)] + rescue ArgumentError => e + Puppet.warning("fact request for #{fact_name} returning nil: '#{to_dot_syntax(walked_path)}' is an array; cannot index to '#{k}'") + result = nil + end + when d.is_a?(Hash) + result = d[k] + else + Puppet.warning("fact request for #{fact_name} returning nil: '#{to_dot_syntax(walked_path)}' is not a collection; cannot walk to '#{k}'") + result = nil + end + + walked_path << k + result + end + end +end -- cgit v1.2.3 From 409a974095a3f5b637e091494b5d14b451c5de78 Mon Sep 17 00:00:00 2001 From: Reid Vandewiele Date: Fri, 30 Jun 2017 14:01:41 -0700 Subject: (FACT-932) Allow use of fact() on other hashes Because sometimes people want to use an alternative data set, but treat it like it's a set of facts. --- lib/puppet/functions/fact.rb | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/puppet/functions/fact.rb b/lib/puppet/functions/fact.rb index dfb048b..48d0d3c 100644 --- a/lib/puppet/functions/fact.rb +++ b/lib/puppet/functions/fact.rb @@ -18,22 +18,27 @@ Puppet::Functions.create_function(:fact) do param 'String', :fact_name end - def to_dot_syntax(array_path) - array_path.map do |string| - string.include?('.') ? %Q{"#{string}"} : string - end.join('.') + dispatch :alternative do + param 'Hash', :fact_hash + param 'String', :fact_name end def fact(fact_name) - facts = closure_scope['facts'] + dot_dig(closure_scope['facts'], fact_name) + end + + def alternative(alternative_hash, fact_name) + dot_dig(alternative_hash, fact_name) + end + def dot_dig(fact_hash, fact_name) # Transform the dot-notation string into an array of paths to walk. Make # sure to correctly extract double-quoted values containing dots as single # elements in the path. path = fact_name.scan(/([^."]+)|(?:")([^"]+)(?:")/).map {|x| x.compact.first } walked_path = [] - path.reduce(facts) do |d, k| + path.reduce(fact_hash) do |d, k| return nil if d.nil? || k.nil? case @@ -55,4 +60,10 @@ Puppet::Functions.create_function(:fact) do result end end + + def to_dot_syntax(array_path) + array_path.map do |string| + string.include?('.') ? %Q{"#{string}"} : string + end.join('.') + end end -- cgit v1.2.3 From 58131d8ff1886352e964aafef58ffeb65c5d2cdd Mon Sep 17 00:00:00 2001 From: tphoney Date: Mon, 3 Jul 2017 17:08:09 +0100 Subject: (MODULES-5003) file_line fix all broken lines --- lib/puppet/provider/file_line/ruby.rb | 11 +++++-- spec/unit/puppet/provider/file_line/ruby_spec.rb | 37 ++++++++++++++++++------ 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index 42f287a..1d27424 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -1,16 +1,21 @@ Puppet::Type.type(:file_line).provide(:ruby) do def exists? + found = true if resource[:replace].to_s != 'true' and count_matches(match_regex) > 0 - true + found = true else lines.find do |line| if resource[:ensure].to_s == 'absent' and resource[:match_for_absence].to_s == 'true' - line.chomp =~ Regexp.new(resource[:match]) + found = line.chomp =~ Regexp.new(resource[:match]) else - line.chomp == resource[:line].chomp + found = line.chomp == resource[:line].chomp + end + if found == false then + break end end end + found end def create diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb index 1f41f62..0e12aa2 100755 --- a/spec/unit/puppet/provider/file_line/ruby_spec.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -29,7 +29,7 @@ describe provider_class do File.open(tmpfile, 'w') do |fh| fh.write('foo1') end - expect(provider.exists?).to be_nil + expect(provider.exists?).to eql (false) end it 'should append to an existing file when creating' do provider.create @@ -69,7 +69,7 @@ describe provider_class do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo2") end - expect(@provider.exists?).to be_nil + expect(@provider.exists?).to eql (false) @provider.create expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo2\nfoo = bar") end @@ -112,7 +112,7 @@ describe provider_class do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz") end - expect(@provider.exists?).to be_nil + expect(@provider.exists?).to eql(false) expect { @provider.create }.to raise_error(Puppet::Error, /More than one line.*matches/) expect(File.read(@tmpfile)).to eql("foo1\nfoo=blah\nfoo2\nfoo=baz") end @@ -131,11 +131,30 @@ describe provider_class do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz") end - expect(@provider.exists?).to be_nil + expect(@provider.exists?).to eql(false) @provider.create expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2\nfoo = bar") end + it 'should replace all lines that match, even when some lines are correct' do + @resource = Puppet::Type::File_line.new( + { + :name => 'neil', + :path => @tmpfile, + :line => "\thard\tcore\t0\n", + :match => '^[ \t]hard[ \t]+core[ \t]+.*', + :multiple => true, + } + ) + @provider = provider_class.new(@resource) + File.open(@tmpfile, 'w') do |fh| + fh.write("\thard\tcore\t90\n\thard\tcore\t0\n") + end + expect(@provider.exists?).to eql(false) + @provider.create + expect(File.read(@tmpfile).chomp).to eql("\thard\tcore\t0\n\thard\tcore\t0") + end + it 'should raise an error with invalid values' do expect { @resource = Puppet::Type::File_line.new( @@ -154,7 +173,7 @@ describe provider_class do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo=blah\nfoo2") end - expect(@provider.exists?).to be_nil + expect(@provider.exists?).to eql(false) @provider.create expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2") end @@ -162,7 +181,7 @@ describe provider_class do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo2") end - expect(@provider.exists?).to be_nil + expect(@provider.exists?).to eql(false) @provider.create expect(File.read(@tmpfile)).to eql("foo1\nfoo2\nfoo = bar\n") end @@ -170,7 +189,7 @@ describe provider_class do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo = bar\nfoo2") end - expect(@provider.exists?).to be_truthy + expect(@provider.exists?).to eql(false) @provider.create expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2") end @@ -274,7 +293,7 @@ describe provider_class do } ) @provider = provider_class.new(@resource) - expect(@provider.exists?).to be_nil + expect(@provider.exists?).to eql (false) @provider.create expect(File.read(@tmpfile).chomp).to eql("foo1\ninserted = line\nfoo = blah\nfoo2\nfoo1\ninserted = line\nfoo = baz") end @@ -367,7 +386,7 @@ describe provider_class do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo\nfoo2") end - expect(@provider.exists?).to be_truthy + expect(@provider.exists?).to be_nil end it 'should remove one line if it matches' do -- cgit v1.2.3 From 0f35700487368357adec8a535b5c50437b208264 Mon Sep 17 00:00:00 2001 From: Reid Vandewiele Date: Mon, 3 Jul 2017 11:33:15 -0700 Subject: Revert "Allow use of fact() on other hashes" This reverts commit 409a974095a3f5b637e091494b5d14b451c5de78. After thinking about this, use case, and function naming, I think it's probably best for now to keep things simple and let `fact()` be single-purpose for looking up facts with no potentially confusing extensions. The only use case where the name makes sense is where it's being used on the `$facts` hash, and I think we'd rather in that circumstance promote the raw use of `fact()`. --- lib/puppet/functions/fact.rb | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/lib/puppet/functions/fact.rb b/lib/puppet/functions/fact.rb index 48d0d3c..dfb048b 100644 --- a/lib/puppet/functions/fact.rb +++ b/lib/puppet/functions/fact.rb @@ -18,27 +18,22 @@ Puppet::Functions.create_function(:fact) do param 'String', :fact_name end - dispatch :alternative do - param 'Hash', :fact_hash - param 'String', :fact_name + def to_dot_syntax(array_path) + array_path.map do |string| + string.include?('.') ? %Q{"#{string}"} : string + end.join('.') end def fact(fact_name) - dot_dig(closure_scope['facts'], fact_name) - end - - def alternative(alternative_hash, fact_name) - dot_dig(alternative_hash, fact_name) - end + facts = closure_scope['facts'] - def dot_dig(fact_hash, fact_name) # Transform the dot-notation string into an array of paths to walk. Make # sure to correctly extract double-quoted values containing dots as single # elements in the path. path = fact_name.scan(/([^."]+)|(?:")([^"]+)(?:")/).map {|x| x.compact.first } walked_path = [] - path.reduce(fact_hash) do |d, k| + path.reduce(facts) do |d, k| return nil if d.nil? || k.nil? case @@ -60,10 +55,4 @@ Puppet::Functions.create_function(:fact) do result end end - - def to_dot_syntax(array_path) - array_path.map do |string| - string.include?('.') ? %Q{"#{string}"} : string - end.join('.') - end end -- cgit v1.2.3 From d85094eb73a898361e909ed2da8fcf466f1bb750 Mon Sep 17 00:00:00 2001 From: tphoney Date: Thu, 6 Jul 2017 10:58:02 +0100 Subject: (MODULES-5186) - removing whitespace from EOL --- README.md | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index c5a7471..61318ea 100644 --- a/README.md +++ b/README.md @@ -178,7 +178,7 @@ Default value: 'present'. Sets the line to be added to the file located by the `path` parameter. Values: String. - + ##### `match` Specifies a regular expression to compare against existing lines in the file; if a match is found, it is replaced rather than adding a new line. A regex comparison is performed against the line value, and if it does not match, an exception is raised. @@ -207,7 +207,7 @@ Default value: `false`. ##### `name` -Specifies the name to use as the identity of the resource. If you want the resource namevar to differ from the supplied `title` of the resource, specify it with `name`. +Specifies the name to use as the identity of the resource. If you want the resource namevar to differ from the supplied `title` of the resource, specify it with `name`. Values: String. @@ -217,7 +217,7 @@ Default value: The value of the title. **Required.** -Specifies the file in which Puppet ensures the line specified by `line`. +Specifies the file in which Puppet ensures the line specified by `line`. Value: String specifying an absolute path to the file. @@ -311,7 +311,7 @@ C:/whatever Matches paths on Windows operating systems. -Acceptable input example: +Acceptable input example: ```shell C:\\WINDOWS\\System32 @@ -468,9 +468,9 @@ Converts a Boolean to a number. Converts values: * `false`, 'f', '0', 'n', and 'no' to 0. * `true`, 't', '1', 'y', and 'yes' to 1. - + Argument: a single Boolean or string as an input. - + *Type*: rvalue. #### `bool2str` @@ -538,8 +538,8 @@ Keeps value within the range [Min, X, Max] by sort based on integer value (param * `clamp('24', [575, 187])` returns 187. * `clamp(16, 88, 661)` returns 88. * `clamp([4, 3, '99'])` returns 4. - -Arguments: strings, arrays, or numerics. + +Arguments: strings, arrays, or numerics. *Type*: rvalue. @@ -690,7 +690,7 @@ Other settings in Puppet affect the stdlib `deprecation` function: Specifies whether or not to log deprecation warnings. This is especially useful for automated tests to avoid flooding your logs before you are ready to migrate. This variable is Boolean, with the following effects: - + * `true`: Functions log a warning. * `false`: No warnings are logged. * No value set: Puppet 4 emits warnings, but Puppet 3 does not. @@ -1253,7 +1253,7 @@ Joins an array into a string using a separator. For example, `join(['a','b','c'] #### `join_keys_to_values` -Joins each key of a hash to that key's corresponding value with a separator, returning the result as strings. +Joins each key of a hash to that key's corresponding value with a separator, returning the result as strings. If a value is an array, the key is prefixed to each element. The return value is a flattened array. @@ -1407,7 +1407,7 @@ Arguments: * The YAML string to convert, as a first argument. * Optionally, the result to return if conversion fails, as a second error. -*Type*: rvalue. +*Type*: rvalue. #### `pick` @@ -1696,7 +1696,7 @@ For example, `time()` returns something like '1311972653'. Converts the argument into bytes. -For example, "4 kB" becomes "4096". +For example, "4 kB" becomes "4096". Arguments: A single string. @@ -1708,7 +1708,7 @@ Arguments: A single string. Retrieves a value within multiple layers of hashes and arrays. -Arguments: +Arguments: * A string containing a path, as the first argument. Provide this argument as a string of hash keys or array indexes starting with zero and separated by the path separator character (default "/"). This function goes through the structure by each path component and tries to return the value at the end of the path. @@ -1819,7 +1819,7 @@ For example, `upcase('abcd')` returns 'ABCD'. #### `uriescape` -URLEncodes a string or array of strings. +URLEncodes a string or array of strings. Arguments: Either a single string or an array of strings. @@ -1915,7 +1915,7 @@ validate_augeas($sudoerscontent, 'Sudoers.lns', [], 'Failed to validate sudoers **Deprecated. Will be removed in a future version of stdlib. See [`validate_legacy`](#validate_legacy).** -Validates that all passed values are either `true` or `false`. +Validates that all passed values are either `true` or `false`. Terminates catalog compilation if any value fails this check. The following values pass: @@ -1992,7 +1992,7 @@ Arguments: * An integer or an array of integers, as the first argument. * Optionally, a maximum, as the second argument. (All elements of) the first argument must be equal to or less than this maximum. -* Optionally, a minimum, as the third argument. (All elements of) the first argument must be equal to or greater than than this maximum. +* Optionally, a minimum, as the third argument. (All elements of) the first argument must be equal to or greater than than this maximum. This function fails if the first argument is not an integer or array of integers, or if the second or third arguments are not convertable to an integer. However, if (and only if) a minimum is given, the second argument may be an empty string or `undef`, which serves as a placeholder to ensure the minimum check. @@ -2046,7 +2046,7 @@ validate_integer(1, 3, true) **Deprecated. Will be removed in a future version of stdlib. See [`validate_legacy`](#validate_legacy).** -Validates that the argument is an IP address, regardless of whether it is an IPv4 or an IPv6 address. It also validates IP address with netmask. +Validates that the argument is an IP address, regardless of whether it is an IPv4 or an IPv6 address. It also validates IP address with netmask. Arguments: A string specifying an IP address. @@ -2117,7 +2117,7 @@ The deprecation messages you get can vary, depending on the modules and data tha The `validate_legacy` function helps you move from Puppet 3 style validation to Puppet 4 validation without breaking functionality your module's users depend on. -Moving to Puppet 4 type validation allows much better defined type checking using [data types](https://docs.puppet.com/puppet/latest/reference/lang_data.html). Many of Puppet 3's `validate_*` functions have surprising holes in their validation. For example, [validate_numeric](#validate_numeric) allows not only numbers, but also arrays of numbers or strings that look like numbers, without giving you any control over the specifics. +Moving to Puppet 4 type validation allows much better defined type checking using [data types](https://docs.puppet.com/puppet/latest/reference/lang_data.html). Many of Puppet 3's `validate_*` functions have surprising holes in their validation. For example, [validate_numeric](#validate_numeric) allows not only numbers, but also arrays of numbers or strings that look like numbers, without giving you any control over the specifics. For each parameter of your classes and defined types, choose a new Puppet 4 data type to use. In most cases, the new data type allows a different set of values than the original `validate_*` function. The situation then looks like this: -- cgit v1.2.3 From 085496fd19c21bf2d02b8ad871d7cab850b92b93 Mon Sep 17 00:00:00 2001 From: tphoney Date: Thu, 6 Jul 2017 12:02:58 +0100 Subject: (MODULES-5186) move test to correct location --- spec/functions/ensure_resources_spec.rb | 22 ++++++++++++++++++++++ spec/unit/ensure_resources_spec.rb | 22 ---------------------- 2 files changed, 22 insertions(+), 22 deletions(-) create mode 100644 spec/functions/ensure_resources_spec.rb delete mode 100644 spec/unit/ensure_resources_spec.rb diff --git a/spec/functions/ensure_resources_spec.rb b/spec/functions/ensure_resources_spec.rb new file mode 100644 index 0000000..aea723e --- /dev/null +++ b/spec/functions/ensure_resources_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe 'test::ensure_resources', type: :class do + let(:params) {{ resource_type: 'user', title_hash: title_param, attributes_hash: {'ensure' => 'present'} }} + + describe 'given a title hash of multiple resources' do + + let(:title_param) { {'dan' => { 'gid' => 'mygroup', 'uid' => '600' }, 'alex' => { 'gid' => 'mygroup', 'uid' => '700'}} } + + it { is_expected.to compile } + it { is_expected.to contain_user('dan').with({ 'gid' => 'mygroup', 'uid' => '600', 'ensure' => 'present'}) } + it { is_expected.to contain_user('alex').with({ 'gid' => 'mygroup', 'uid' => '700', 'ensure' => 'present'}) } + end + + describe 'given a title hash of a single resource' do + + let(:title_param) { {'dan' => { 'gid' => 'mygroup', 'uid' => '600' }} } + + it { is_expected.to compile } + it { is_expected.to contain_user('dan').with({ 'gid' => 'mygroup', 'uid' => '600', 'ensure' => 'present'}) } + end +end diff --git a/spec/unit/ensure_resources_spec.rb b/spec/unit/ensure_resources_spec.rb deleted file mode 100644 index aea723e..0000000 --- a/spec/unit/ensure_resources_spec.rb +++ /dev/null @@ -1,22 +0,0 @@ -require 'spec_helper' - -describe 'test::ensure_resources', type: :class do - let(:params) {{ resource_type: 'user', title_hash: title_param, attributes_hash: {'ensure' => 'present'} }} - - describe 'given a title hash of multiple resources' do - - let(:title_param) { {'dan' => { 'gid' => 'mygroup', 'uid' => '600' }, 'alex' => { 'gid' => 'mygroup', 'uid' => '700'}} } - - it { is_expected.to compile } - it { is_expected.to contain_user('dan').with({ 'gid' => 'mygroup', 'uid' => '600', 'ensure' => 'present'}) } - it { is_expected.to contain_user('alex').with({ 'gid' => 'mygroup', 'uid' => '700', 'ensure' => 'present'}) } - end - - describe 'given a title hash of a single resource' do - - let(:title_param) { {'dan' => { 'gid' => 'mygroup', 'uid' => '600' }} } - - it { is_expected.to compile } - it { is_expected.to contain_user('dan').with({ 'gid' => 'mygroup', 'uid' => '600', 'ensure' => 'present'}) } - end -end -- cgit v1.2.3 From 700b735893b77f6ebc6420f72f1e474d5053f12b Mon Sep 17 00:00:00 2001 From: tphoney Date: Fri, 7 Jul 2017 15:28:52 +0100 Subject: (MODULES-5186) dont run this test on windows --- spec/unit/puppet/provider/file_line/ruby_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb index 1f41f62..3d28687 100755 --- a/spec/unit/puppet/provider/file_line/ruby_spec.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -2,7 +2,8 @@ require 'spec_helper' require 'tempfile' provider_class = Puppet::Type.type(:file_line).provider(:ruby) -describe provider_class do +# These tests fail on windows when run as part of the rake task. Individually they pass +describe provider_class, :unless => Puppet::Util::Platform.windows? do context "when adding" do let :tmpfile do tmp = Tempfile.new('tmp') -- cgit v1.2.3 From 3eee345aa2cb697e8791069ffe96299db6a28f8e Mon Sep 17 00:00:00 2001 From: Eric Putnam Date: Thu, 6 Jul 2017 14:43:15 -0700 Subject: (MODULES-5187) mysnc puppet 5 and ruby 2.4 --- .travis.yml | 6 +++--- appveyor.yml | 22 +++++++++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4981b25..0c6f904 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,10 +22,10 @@ matrix: script: bundle exec rake beaker services: docker sudo: required - - rvm: 2.3.1 + - rvm: 2.4.0 bundler_args: --without system_tests - env: PUPPET_GEM_VERSION="~> 4.0" - - rvm: 2.1.7 + env: PUPPET_GEM_VERSION="~> 5.0" + - rvm: 2.1.9 bundler_args: --without system_tests env: PUPPET_GEM_VERSION="~> 4.0" notifications: diff --git a/appveyor.yml b/appveyor.yml index c87ed7c..7e05880 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -14,16 +14,28 @@ environment: RUBY_VER: 21 - PUPPET_GEM_VERSION: ~> 4.0 RUBY_VER: 21-x64 - - PUPPET_GEM_VERSION: ~> 4.0 - RUBY_VER: 23 - - PUPPET_GEM_VERSION: ~> 4.0 - RUBY_VER: 23-x64 - - PUPPET_GEM_VERSION: 4.2.3 + - PUPPET_GEM_VERSION: ~> 5.0 + RUBY_VER: 24 + - PUPPET_GEM_VERSION: ~> 5.0 + RUBY_VER: 24-x64 + - PUPPET_GEM_VERSION: 4.7.1 RUBY_VER: 21-x64 matrix: fast_finish: true install: - SET PATH=C:\Ruby%RUBY_VER%\bin;%PATH% +- ps: | + # AppVeyor appears to have OpenSSL headers available already + # which msys2 would normally install with: + # pacman -S mingw-w64-x86_64-openssl --noconfirm + # + if ( $(ruby --version) -match "^ruby\s+2\.4" ) { + Write-Output "Building OpenSSL gem ~> 2.0.4 to fix Ruby 2.4 / AppVeyor issue" + gem install openssl --version '~> 2.0.4' --no-ri --no-rdoc + } + + gem list openssl + ruby -ropenssl -e 'puts \"OpenSSL Version - #{OpenSSL::OPENSSL_VERSION}\"; puts \"OpenSSL Library Version - #{OpenSSL::OPENSSL_LIBRARY_VERSION}\"' - bundle install --jobs 4 --retry 2 --without system_tests - type Gemfile.lock build: off -- cgit v1.2.3 From fe7ccd8b89556cc6cc1f5ea7f58b5ac2aedb23ec Mon Sep 17 00:00:00 2001 From: Frank de Jong Date: Sat, 8 Jul 2017 09:43:40 +0200 Subject: Add validate_domain_name function --- README.md | 26 +++++++++++++++ .../parser/functions/validate_domain_name.rb | 39 ++++++++++++++++++++++ spec/functions/validate_domain_name_spec.rb | 35 +++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 lib/puppet/parser/functions/validate_domain_name.rb create mode 100644 spec/functions/validate_domain_name_spec.rb diff --git a/README.md b/README.md index 61318ea..1647fcf 100644 --- a/README.md +++ b/README.md @@ -1958,6 +1958,32 @@ validate_cmd($haproxycontent, '/usr/sbin/haproxy -f % -c', 'Haproxy failed to va *Type*: statement. +#### `validate_domain_name` + +**Deprecated. Will be removed in a future version of stdlib. See [`validate_legacy`](#validate_legacy).** + +Validate that all values passed are syntactically correct domain names. Aborts catalog compilation if any value fails this check. + +The following values pass: + +~~~ +$my_domain_name = 'server.domain.tld' +validate_domain_name($my_domain_name) +validate_domain_name('domain.tld', 'puppet.com', $my_domain_name) +~~~ + +The following values fail, causing compilation to abort: + +~~~ +validate_domain_name(1) +validate_domain_name(true) +validate_domain_name('invalid domain') +validate_domain_name('-foo.example.com') +validate_domain_name('www.example.2com') +~~~ + +*Type*: statement. + #### `validate_hash` **Deprecated. Will be removed in a future version of stdlib. See [`validate_legacy`](#validate_legacy).** diff --git a/lib/puppet/parser/functions/validate_domain_name.rb b/lib/puppet/parser/functions/validate_domain_name.rb new file mode 100644 index 0000000..c3fad78 --- /dev/null +++ b/lib/puppet/parser/functions/validate_domain_name.rb @@ -0,0 +1,39 @@ +module Puppet::Parser::Functions + newfunction(:validate_domain_name, :doc => <<-ENDHEREDOC + Validate that all values passed are syntactically correct domain names. + Fail compilation if any value fails this check. + + The following values will pass: + + $my_domain_name = 'server.domain.tld' + validate_domain_name($my_domain_name) + validate_domain_name('domain.tld', 'puppet.com', $my_domain_name) + + The following values will fail, causing compilation to abort: + + validate_domain_name(1) + validate_domain_name(true) + validate_domain_name('invalid domain') + validate_domain_name('-foo.example.com') + validate_domain_name('www.example.2com') + + ENDHEREDOC + ) do |args| + + rescuable_exceptions = [ArgumentError] + + if args.empty? + raise Puppet::ParseError, "validate_domain_name(): wrong number of arguments (#{args.length}; must be > 0)" + end + + args.each do |arg| + raise Puppet::ParseError, "#{arg.inspect} is not a string." unless arg.is_a?(String) + + begin + raise Puppet::ParseError, "#{arg.inspect} is not a syntactically correct domain name" unless function_is_domain_name([arg]) + rescue *rescuable_exceptions + raise Puppet::ParseError, "#{arg.inspect} is not a syntactically correct domain name" + end + end + end +end diff --git a/spec/functions/validate_domain_name_spec.rb b/spec/functions/validate_domain_name_spec.rb new file mode 100644 index 0000000..69fcae4 --- /dev/null +++ b/spec/functions/validate_domain_name_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' + +describe 'validate_domain_name' do + describe 'signature validation' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } + end + + describe 'valid inputs' do + it { is_expected.to run.with_params('com', 'com.') } + it { is_expected.to run.with_params('x.com', 'x.com.') } + it { is_expected.to run.with_params('foo.example.com', 'foo.example.com.') } + it { is_expected.to run.with_params('2foo.example.com', '2foo.example.com.') } + it { is_expected.to run.with_params('www.2foo.example.com', 'www.2foo.example.com.') } + it { is_expected.to run.with_params('domain.tld', 'puppet.com') } + end + + describe 'invalid inputs' do + it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, /is not a string/) } + it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /is not a string/) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /is not a string/) } + it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /is not a string/) } + + it { is_expected.to run.with_params('foo.example.com', []).and_raise_error(Puppet::ParseError, /is not a string/) } + it { is_expected.to run.with_params('foo.example.com', {}).and_raise_error(Puppet::ParseError, /is not a string/) } + it { is_expected.to run.with_params('foo.example.com', 1).and_raise_error(Puppet::ParseError, /is not a string/) } + it { is_expected.to run.with_params('foo.example.com', true).and_raise_error(Puppet::ParseError, /is not a string/) } + + it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, /is not a syntactically correct domain name/) } + it { is_expected.to run.with_params('invalid domain').and_raise_error(Puppet::ParseError, /is not a syntactically correct domain name/) } + it { is_expected.to run.with_params('-foo.example.com').and_raise_error(Puppet::ParseError, /is not a syntactically correct domain name/) } + it { is_expected.to run.with_params('www.example.2com').and_raise_error(Puppet::ParseError, /is not a syntactically correct domain name/) } + it { is_expected.to run.with_params('192.168.1.1').and_raise_error(Puppet::ParseError, /is not a syntactically correct domain name/) } + end +end -- cgit v1.2.3 From 715c40dc9083068598387ab67396136e87eb553d Mon Sep 17 00:00:00 2001 From: Eric Putnam Date: Tue, 11 Jul 2017 15:43:37 -0700 Subject: (MODULES-5187) fix spec tests for puppet 5 changes --- spec/aliases/integer_spec.rb | 6 +++++- spec/functions/merge_spec.rb | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/spec/aliases/integer_spec.rb b/spec/aliases/integer_spec.rb index 8cb4658..aec9fd6 100644 --- a/spec/aliases/integer_spec.rb +++ b/spec/aliases/integer_spec.rb @@ -22,7 +22,11 @@ if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 [ "foo\nbar", true, 'true', false, 'false', 'iAmAString', '1test', '1 test', 'test 1', 'test 1 test', {}, { 'key' => 'value' }, { 1=> 2 }, '', :undef , 'x', 3.7, '3.7',-3.7, '-342.2315e-12' ].each do |value| describe value.inspect do let(:params) {{ value: value }} - it { is_expected.to compile.and_raise_error(/parameter 'value' expects a value of type Integer, Pattern(\[.*\]+)?, or Array/) } + if Gem::Version.new(Puppet.version) >= Gem::Version.new('5.0.0') + it { is_expected.to compile.and_raise_error(/parameter 'value' expects a Stdlib::Compat::Integer = Variant\[Integer, Pattern\[.*\], Array\[.*\]\] value/) } + else + it { is_expected.to compile.and_raise_error(/parameter 'value' expects a value of type Integer, Pattern(\[.*\]+)?, or Array/) } + end end end end diff --git a/spec/functions/merge_spec.rb b/spec/functions/merge_spec.rb index 7b53363..3b2e3ef 100755 --- a/spec/functions/merge_spec.rb +++ b/spec/functions/merge_spec.rb @@ -4,7 +4,7 @@ describe 'merge' do it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } it { is_expected.to run.with_params({}, 'two').and_raise_error(Puppet::ParseError, /unexpected argument type String/) } - it { is_expected.to run.with_params({}, 1).and_raise_error(Puppet::ParseError, /unexpected argument type Fixnum/) } + it { is_expected.to run.with_params({}, 1).and_raise_error(Puppet::ParseError, /unexpected argument type (Fixnum|Integer)/) } it { pending 'should not special case this' is_expected.to run.with_params({}).and_return({}) -- cgit v1.2.3 From 01d3004f0a6a1c662aea90edd5b1524139706c61 Mon Sep 17 00:00:00 2001 From: tphoney Date: Thu, 13 Jul 2017 16:12:08 +0100 Subject: (FM-6239) rewrite of test following std patterns --- spec/functions/ensure_resources_spec.rb | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/spec/functions/ensure_resources_spec.rb b/spec/functions/ensure_resources_spec.rb index aea723e..7cca671 100644 --- a/spec/functions/ensure_resources_spec.rb +++ b/spec/functions/ensure_resources_spec.rb @@ -1,22 +1,25 @@ require 'spec_helper' -describe 'test::ensure_resources', type: :class do - let(:params) {{ resource_type: 'user', title_hash: title_param, attributes_hash: {'ensure' => 'present'} }} +describe 'ensure_resources' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params().and_raise_error(ArgumentError, /Must specify a type/) } + it { is_expected.to run.with_params('type').and_raise_error(ArgumentError, /Must specify a title/) } describe 'given a title hash of multiple resources' do + before { subject.call(['user', {'dan' => { 'gid' => 'mygroup', 'uid' => '600' }, 'alex' => { 'gid' => 'mygroup', 'uid' => '700'}}, {'ensure' => 'present'}]) } - let(:title_param) { {'dan' => { 'gid' => 'mygroup', 'uid' => '600' }, 'alex' => { 'gid' => 'mygroup', 'uid' => '700'}} } - - it { is_expected.to compile } - it { is_expected.to contain_user('dan').with({ 'gid' => 'mygroup', 'uid' => '600', 'ensure' => 'present'}) } - it { is_expected.to contain_user('alex').with({ 'gid' => 'mygroup', 'uid' => '700', 'ensure' => 'present'}) } + # this lambda is required due to strangeness within rspec-puppet's expectation handling + it { expect(lambda { catalogue }).to contain_user('dan').with_ensure('present') } + it { expect(lambda { catalogue }).to contain_user('alex').with_ensure('present') } + it { expect(lambda { catalogue }).to contain_user('dan').with({ 'gid' => 'mygroup', 'uid' => '600'}) } + it { expect(lambda { catalogue }).to contain_user('alex').with({ 'gid' => 'mygroup', 'uid' => '700'}) } end describe 'given a title hash of a single resource' do + before { subject.call(['user', {'dan' => { 'gid' => 'mygroup', 'uid' => '600' }}, {'ensure' => 'present'}]) } - let(:title_param) { {'dan' => { 'gid' => 'mygroup', 'uid' => '600' }} } - - it { is_expected.to compile } - it { is_expected.to contain_user('dan').with({ 'gid' => 'mygroup', 'uid' => '600', 'ensure' => 'present'}) } + # this lambda is required due to strangeness within rspec-puppet's expectation handling + it { expect(lambda { catalogue }).to contain_user('dan').with_ensure('present') } + it { expect(lambda { catalogue }).to contain_user('dan').with({ 'gid' => 'mygroup', 'uid' => '600'}) } end end -- cgit v1.2.3 From 81d7d35fd78917ee1a5e66cb7459ea341bc452ea Mon Sep 17 00:00:00 2001 From: tkishel Date: Thu, 13 Jul 2017 15:16:04 -0700 Subject: (MODULES-5003) file_line_does_not_change_multiple_lines_when_one_matches The exists? method is called to determine the need to call the create and destroy methods. When the ensure, match, and multiple attributes are defined, the exists? method needs to return false unless all the lines that match the match attribute equal the line attribute. The first commit is minimal, and implements this change. The second commit normalizes the code, which I needed for comprehension. --- lib/puppet/provider/file_line/ruby.rb | 27 +++++++++++++----------- spec/unit/puppet/provider/file_line/ruby_spec.rb | 4 ++-- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index 1d27424..aa99986 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -1,18 +1,21 @@ Puppet::Type.type(:file_line).provide(:ruby) do def exists? - found = true - if resource[:replace].to_s != 'true' and count_matches(match_regex) > 0 - found = true + found = false + lines_count = 0 + lines.each do |line| + found = line.chomp == resource[:line] + if found + lines_count += 1 + end + end + if resource[:match] == nil + found = lines_count > 0 else - lines.find do |line| - if resource[:ensure].to_s == 'absent' and resource[:match_for_absence].to_s == 'true' - found = line.chomp =~ Regexp.new(resource[:match]) - else - found = line.chomp == resource[:line].chomp - end - if found == false then - break - end + match_count = count_matches(match_regex) + if resource[:replace].to_s == 'true' + found = lines_count > 0 && lines_count == match_count + else + found = match_count > 0 end end found diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb index dcf8509..ab6edf9 100755 --- a/spec/unit/puppet/provider/file_line/ruby_spec.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -190,7 +190,7 @@ describe provider_class, :unless => Puppet::Util::Platform.windows? do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo = bar\nfoo2") end - expect(@provider.exists?).to eql(false) + expect(@provider.exists?).to eql(true) @provider.create expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2") end @@ -387,7 +387,7 @@ describe provider_class, :unless => Puppet::Util::Platform.windows? do File.open(@tmpfile, 'w') do |fh| fh.write("foo1\nfoo\nfoo2") end - expect(@provider.exists?).to be_nil + expect(@provider.exists?).to eql (true) end it 'should remove one line if it matches' do -- cgit v1.2.3 From 4d764068ffe456366a9c823e15581e9a9649898e Mon Sep 17 00:00:00 2001 From: tkishel Date: Thu, 13 Jul 2017 15:30:54 -0700 Subject: (MODULES-5003) file_line_does_not_change_multiple_lines_when_one_matches The exists? method is called to determine the need to call the create and destroy methods. When the ensure, match, and multiple attributes are defined, the exists? method needs to return false unless all the lines that match the match attribute equal the line attribute. The first commit is minimal, and implements this change. The second commit normalizes the code, which I needed for comprehension. --- lib/puppet/provider/file_line/ruby.rb | 79 ++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index aa99986..2f6c8ef 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -11,7 +11,7 @@ Puppet::Type.type(:file_line).provide(:ruby) do if resource[:match] == nil found = lines_count > 0 else - match_count = count_matches(match_regex) + match_count = count_matches(new_match_regex) if resource[:replace].to_s == 'true' found = lines_count > 0 && lines_count == match_count else @@ -22,19 +22,19 @@ Puppet::Type.type(:file_line).provide(:ruby) do end def create - unless resource[:replace].to_s != 'true' and count_matches(match_regex) > 0 + unless resource[:replace].to_s != 'true' && count_matches(new_match_regex) > 0 if resource[:match] handle_create_with_match elsif resource[:after] handle_create_with_after else - append_line + handle_append_line end end end def destroy - if resource[:match_for_absence].to_s == 'true' and resource[:match] + if resource[:match_for_absence].to_s == 'true' && resource[:match] handle_destroy_with_match else handle_destroy_line @@ -42,6 +42,7 @@ Puppet::Type.type(:file_line).provide(:ruby) do end private + def lines # If this type is ever used with very large files, we should # write this in a different way, using a temp @@ -56,25 +57,34 @@ Puppet::Type.type(:file_line).provide(:ruby) do end end - def match_regex + def new_after_regex + resource[:after] ? Regexp.new(resource[:after]) : nil + end + + def new_match_regex resource[:match] ? Regexp.new(resource[:match]) : nil end + def count_matches(regex) + lines.select{ |line| line.match(regex) }.size + end + def handle_create_with_match() - regex_after = resource[:after] ? Regexp.new(resource[:after]) : nil - match_count = count_matches(match_regex) + after_regex = new_after_regex + match_regex = new_match_regex + match_count = count_matches(new_match_regex) if match_count > 1 && resource[:multiple].to_s != 'true' raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'" end File.open(resource[:path], 'w') do |fh| - lines.each do |l| - fh.puts(match_regex.match(l) ? resource[:line] : l) - if (match_count == 0 and regex_after) - if regex_after.match(l) + lines.each do |line| + fh.puts(match_regex.match(line) ? resource[:line] : line) + if match_count == 0 && after_regex + if after_regex.match(line) fh.puts(resource[:line]) - match_count += 1 #Increment match_count to indicate that the new line has been inserted. + match_count += 1 # Increment match_count to indicate that the new line has been inserted. end end end @@ -86,32 +96,29 @@ Puppet::Type.type(:file_line).provide(:ruby) do end def handle_create_with_after - regex = Regexp.new(resource[:after]) - count = count_matches(regex) + after_regex = new_after_regex + after_count = count_matches(after_regex) - if count > 1 && resource[:multiple].to_s != 'true' - raise Puppet::Error, "#{count} lines match pattern '#{resource[:after]}' in file '#{resource[:path]}'. One or no line must match the pattern." + if after_count > 1 && resource[:multiple].to_s != 'true' + raise Puppet::Error, "#{after_count} lines match pattern '#{resource[:after]}' in file '#{resource[:path]}'. One or no line must match the pattern." end - File.open(resource[:path], 'w') do |fh| - lines.each do |l| - fh.puts(l) - if regex.match(l) then + File.open(resource[:path],'w') do |fh| + lines.each do |line| + fh.puts(line) + if after_regex.match(line) fh.puts(resource[:line]) end end - end - if (count == 0) # append the line to the end of the file - append_line + if (after_count == 0) + fh.puts(resource[:line]) + end end end - def count_matches(regex) - lines.select{|l| l.match(regex)}.size - end - def handle_destroy_with_match + match_regex = new_match_regex match_count = count_matches(match_regex) if match_count > 1 && resource[:multiple].to_s != 'true' raise Puppet::Error, "More than one line in file '#{resource[:path]}' matches pattern '#{resource[:match]}'" @@ -119,27 +126,23 @@ Puppet::Type.type(:file_line).provide(:ruby) do local_lines = lines File.open(resource[:path],'w') do |fh| - fh.write(local_lines.reject{|l| match_regex.match(l) }.join('')) + fh.write(local_lines.reject{ |line| match_regex.match(line) }.join('')) end end def handle_destroy_line local_lines = lines File.open(resource[:path],'w') do |fh| - fh.write(local_lines.reject{|l| l.chomp == resource[:line] }.join('')) + fh.write(local_lines.reject{ |line| line.chomp == resource[:line] }.join('')) end end - ## - # append the line to the file. - # - # @api private - def append_line - File.open(resource[:path], 'w') do |fh| - lines.each do |l| - fh.puts(l) + def handle_append_line + File.open(resource[:path],'w') do |fh| + lines.each do |line| + fh.puts(line) end - fh.puts resource[:line] + fh.puts(resource[:line]) end end end -- cgit v1.2.3 From 33922a4ec7a8c204a17e0c3017eea21faa220f39 Mon Sep 17 00:00:00 2001 From: tphoney Date: Fri, 14 Jul 2017 10:14:04 +0100 Subject: FM-6239 clean up for puppet 5.tests/deprecation --- lib/puppet/parser/functions/unique.rb | 4 +++ spec/aliases/absolutepath_spec.rb | 2 +- spec/aliases/float_spec.rb | 2 +- spec/aliases/numeric_spec.rb | 2 +- spec/aliases/string_spec.rb | 2 +- spec/functions/deep_merge_spec.rb | 2 +- spec/functions/strftime_spec.rb | 4 +-- spec/functions/unique_spec.rb | 46 ++++++++++++++++++----------------- 8 files changed, 35 insertions(+), 29 deletions(-) diff --git a/lib/puppet/parser/functions/unique.rb b/lib/puppet/parser/functions/unique.rb index b57431d..1e2a895 100644 --- a/lib/puppet/parser/functions/unique.rb +++ b/lib/puppet/parser/functions/unique.rb @@ -24,6 +24,10 @@ This returns: EOS ) do |arguments| + if Puppet::Util::Package.versioncmp(Puppet.version, '5.0.0') >= 0 + function_deprecation([:unique, 'This method is deprecated, please use the core puppet unique function. There is further documentation for the function in the release notes of Puppet 5.0.']) + end + raise(Puppet::ParseError, "unique(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1 value = arguments[0] diff --git a/spec/aliases/absolutepath_spec.rb b/spec/aliases/absolutepath_spec.rb index cd442f2..ff23dc0 100644 --- a/spec/aliases/absolutepath_spec.rb +++ b/spec/aliases/absolutepath_spec.rb @@ -40,7 +40,7 @@ if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 ].each do |value| describe value.inspect do let(:params) {{ value: value }} - it { is_expected.to compile.and_raise_error(/parameter 'value' expects a match for Variant/) } + it { is_expected.to compile.and_raise_error(/parameter 'value' expects a match for.*Variant/) } end end end diff --git a/spec/aliases/float_spec.rb b/spec/aliases/float_spec.rb index 66079c6..84e1934 100644 --- a/spec/aliases/float_spec.rb +++ b/spec/aliases/float_spec.rb @@ -20,7 +20,7 @@ if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 [ true, 'true', false, 'false', 'iAmAString', '1test', '1 test', 'test 1', 'test 1 test', {}, { 'key' => 'value' }, { 1=> 2 }, '', :undef , 'x', 3, '3', -3, '-3'].each do |value| describe value.inspect do let(:params) {{ value: value }} - it { is_expected.to compile.and_raise_error(/parameter 'value' expects a value of type Float or Pattern(\[.*\]+)?/) } + it { is_expected.to compile.and_raise_error(/parameter 'value' expects.*Float.*Pattern/) } end end end diff --git a/spec/aliases/numeric_spec.rb b/spec/aliases/numeric_spec.rb index bc17f4f..09c28ec 100644 --- a/spec/aliases/numeric_spec.rb +++ b/spec/aliases/numeric_spec.rb @@ -24,7 +24,7 @@ if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 [ true, 'true', false, 'false', 'iAmAString', '1test', '1 test', 'test 1', 'test 1 test', {}, { 'key' => 'value' }, { 1=> 2 }, '', :undef , 'x' ].each do |value| describe value.inspect do let(:params) {{ value: value }} - it { is_expected.to compile.and_raise_error(/parameter 'value' expects a value of type Numeric, Pattern(\[.*\]+)?, or Array/) } + it { is_expected.to compile.and_raise_error(/parameter 'value' expects.*Numeric.*Pattern.*Array/) } end end end diff --git a/spec/aliases/string_spec.rb b/spec/aliases/string_spec.rb index 3ea1967..4fc8ce6 100644 --- a/spec/aliases/string_spec.rb +++ b/spec/aliases/string_spec.rb @@ -24,7 +24,7 @@ if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 ].each do |value| describe value.inspect do let(:params) {{ value: value }} - it { is_expected.to compile.and_raise_error(/parameter 'value' expects a (?:value of type Undef or )?String/) } + it { is_expected.to compile.and_raise_error(/parameter 'value' expects a (?:value of type Undef or )?.*String/) } end end end diff --git a/spec/functions/deep_merge_spec.rb b/spec/functions/deep_merge_spec.rb index c91a07e..819e025 100755 --- a/spec/functions/deep_merge_spec.rb +++ b/spec/functions/deep_merge_spec.rb @@ -4,7 +4,7 @@ describe 'deep_merge' do it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } it { is_expected.to run.with_params({ 'key' => 'value' }).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } it { is_expected.to run.with_params({}, '2').and_raise_error(Puppet::ParseError, /unexpected argument type String/) } - it { is_expected.to run.with_params({}, 2).and_raise_error(Puppet::ParseError, /unexpected argument type Fixnum/) } + it { is_expected.to run.with_params({}, 2).and_raise_error(Puppet::ParseError, /unexpected argument/) } it { is_expected.to run.with_params({}, '').and_return({}) } it { is_expected.to run.with_params({}, {}).and_return({}) } it { is_expected.to run.with_params({}, {}, {}).and_return({}) } diff --git a/spec/functions/strftime_spec.rb b/spec/functions/strftime_spec.rb index e76774a..41cda6a 100755 --- a/spec/functions/strftime_spec.rb +++ b/spec/functions/strftime_spec.rb @@ -14,9 +14,9 @@ describe 'strftime' do expect(result.to_i).to(be > 1311953157) end - it "using %s should be lower then 1.5 trillion" do + it "using %s should be greater than 1.5 trillion" do result = scope.function_strftime(["%s"]) - expect(result.to_i).to(be < 1500000000) + expect(result.to_i).to(be > 1500000000) end it "should return a date when given %Y-%m-%d" do diff --git a/spec/functions/unique_spec.rb b/spec/functions/unique_spec.rb index 7955acb..76932ec 100755 --- a/spec/functions/unique_spec.rb +++ b/spec/functions/unique_spec.rb @@ -1,29 +1,31 @@ require 'spec_helper' describe 'unique' do - describe 'signature validation' do - it { is_expected.not_to eq(nil) } - it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } - it { - pending("Current implementation ignores parameters after the first.") - is_expected.to run.with_params([], 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) - } - it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) } - it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) } - it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) } - end + if Puppet.version.to_f < 5.0 + describe 'signature validation' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } + it { + pending("Current implementation ignores parameters after the first.") + is_expected.to run.with_params([], 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) + } + it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) } + it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /Requires either array or string to work/) } + end - context 'when called with an array' do - it { is_expected.to run.with_params([]).and_return([]) } - it { is_expected.to run.with_params(['a']).and_return(['a']) } - it { is_expected.to run.with_params(['a', 'b', 'a']).and_return(['a', 'b']) } - it { is_expected.to run.with_params(['ã', 'ъ', 'ã']).and_return(['ã', 'ъ']) } - end + context 'when called with an array' do + it { is_expected.to run.with_params([]).and_return([]) } + it { is_expected.to run.with_params(['a']).and_return(['a']) } + it { is_expected.to run.with_params(['a', 'b', 'a']).and_return(['a', 'b']) } + it { is_expected.to run.with_params(['ã', 'ъ', 'ã']).and_return(['ã', 'ъ']) } + end - context 'when called with a string' do - it { is_expected.to run.with_params('').and_return('') } - it { is_expected.to run.with_params('a').and_return('a') } - it { is_expected.to run.with_params('aaba').and_return('ab') } - it { is_expected.to run.with_params('ããъã').and_return('ãъ') } + context 'when called with a string' do + it { is_expected.to run.with_params('').and_return('') } + it { is_expected.to run.with_params('a').and_return('a') } + it { is_expected.to run.with_params('aaba').and_return('ab') } + it { is_expected.to run.with_params('ããъã').and_return('ãъ') } + end end end -- cgit v1.2.3 From c9370f4e3ae97b1e752713102a98af2979b1e9be Mon Sep 17 00:00:00 2001 From: Tim Meusel Date: Sat, 15 Jul 2017 11:21:08 +0200 Subject: add type for MAC address --- types/mac.pp | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 types/mac.pp diff --git a/types/mac.pp b/types/mac.pp new file mode 100644 index 0000000..4103574 --- /dev/null +++ b/types/mac.pp @@ -0,0 +1,2 @@ +# A type for a MAC address +type Stdlib::MAC = Pattern[/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/] -- cgit v1.2.3 From 42d4ea7af9197f77a3062727eb166c719ca6296a Mon Sep 17 00:00:00 2001 From: Eric Putnam Date: Fri, 7 Jul 2017 17:30:17 -0700 Subject: (MODULES-4908) adds support for sensitive data type to pw_hash Also includes a small fix to integer_spec so tests pass. --- lib/puppet/parser/functions/pw_hash.rb | 7 +++++++ spec/aliases/integer_spec.rb | 2 +- spec/functions/pw_hash_spec.rb | 8 ++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/puppet/parser/functions/pw_hash.rb b/lib/puppet/parser/functions/pw_hash.rb index d99ee5b..0deeb3a 100644 --- a/lib/puppet/parser/functions/pw_hash.rb +++ b/lib/puppet/parser/functions/pw_hash.rb @@ -27,6 +27,13 @@ Puppet::Parser::Functions::newfunction( environment contains several different operating systems, ensure that they are compatible before using this function.") do |args| raise ArgumentError, "pw_hash(): wrong number of arguments (#{args.size} for 3)" if args.size != 3 + args.map! do |arg| + if arg.is_a? Puppet::Pops::Types::PSensitiveType::Sensitive + arg.unwrap + else + arg + end + end raise ArgumentError, "pw_hash(): first argument must be a string" unless args[0].is_a? String or args[0].nil? raise ArgumentError, "pw_hash(): second argument must be a string" unless args[1].is_a? String hashes = { 'md5' => '1', diff --git a/spec/aliases/integer_spec.rb b/spec/aliases/integer_spec.rb index aec9fd6..9cf0357 100644 --- a/spec/aliases/integer_spec.rb +++ b/spec/aliases/integer_spec.rb @@ -22,7 +22,7 @@ if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 [ "foo\nbar", true, 'true', false, 'false', 'iAmAString', '1test', '1 test', 'test 1', 'test 1 test', {}, { 'key' => 'value' }, { 1=> 2 }, '', :undef , 'x', 3.7, '3.7',-3.7, '-342.2315e-12' ].each do |value| describe value.inspect do let(:params) {{ value: value }} - if Gem::Version.new(Puppet.version) >= Gem::Version.new('5.0.0') + if Puppet::Util::Package.versioncmp(Puppet.version, '5.0.0') >= 0 it { is_expected.to compile.and_raise_error(/parameter 'value' expects a Stdlib::Compat::Integer = Variant\[Integer, Pattern\[.*\], Array\[.*\]\] value/) } else it { is_expected.to compile.and_raise_error(/parameter 'value' expects a value of type Integer, Pattern(\[.*\]+)?, or Array/) } diff --git a/spec/functions/pw_hash_spec.rb b/spec/functions/pw_hash_spec.rb index df5348c..9e03464 100644 --- a/spec/functions/pw_hash_spec.rb +++ b/spec/functions/pw_hash_spec.rb @@ -65,5 +65,13 @@ describe 'pw_hash' do it { is_expected.to run.with_params('password', 'sha-256', 'salt').and_return('$5$salt$Gcm6FsVtF/Qa77ZKD.iwsJlCVPY0XSMgLJL0Hnww/c1') } it { is_expected.to run.with_params('password', 'sha-512', 'salt').and_return('$6$salt$IxDD3jeSOb5eB1CX5LBsqZFVkJdido3OUILO5Ifz5iwMuTS4XMS130MTSuDDl3aCI6WouIL9AjRbLCelDCy.g.') } end + + if Puppet::Util::Package.versioncmp(Puppet.version, '4.7.0') >= 0 + describe 'when arguments are sensitive' do + it { is_expected.to run.with_params(Puppet::Pops::Types::PSensitiveType::Sensitive.new('password'), 'md5', 'salt').and_return('$1$salt$qJH7.N4xYta3aEG/dfqo/0') } + it { is_expected.to run.with_params(Puppet::Pops::Types::PSensitiveType::Sensitive.new('password'), 'md5', Puppet::Pops::Types::PSensitiveType::Sensitive.new('salt')).and_return('$1$salt$qJH7.N4xYta3aEG/dfqo/0') } + it { is_expected.to run.with_params('password', 'md5', Puppet::Pops::Types::PSensitiveType::Sensitive.new('salt')).and_return('$1$salt$qJH7.N4xYta3aEG/dfqo/0') } + end + end end end -- cgit v1.2.3 From 703fa777e1daee97e2aea529064d773662ef1fe0 Mon Sep 17 00:00:00 2001 From: "asif.shaikh" Date: Wed, 1 Feb 2017 20:14:27 -0800 Subject: Add new file_line option append_on_no_match --- README.md | 12 ++++++++++++ lib/puppet/provider/file_line/ruby.rb | 4 +++- lib/puppet/type/file_line.rb | 8 +++++++- spec/unit/puppet/provider/file_line/ruby_spec.rb | 18 ++++++++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 61318ea..5e298d5 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,18 @@ file_line { 'bashrc_proxy': In the example above, `match` looks for a line beginning with 'export' followed by 'HTTP_PROXY' and replaces it with the value in line. +Match Example: + + file_line { 'bashrc_proxy': + ensure => present, + path => '/etc/bashrc', + line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128', + match => '^export\ HTTP_PROXY\=', + append_on_no_match => false, + } + +In this code example, `match` looks for a line beginning with export followed by HTTP_PROXY and replaces it with the value in line. If a match is not found, then no changes are made to the file. + Match Example with `ensure => absent`: ```puppet diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb index 2f6c8ef..16f2709 100644 --- a/lib/puppet/provider/file_line/ruby.rb +++ b/lib/puppet/provider/file_line/ruby.rb @@ -12,7 +12,9 @@ Puppet::Type.type(:file_line).provide(:ruby) do found = lines_count > 0 else match_count = count_matches(new_match_regex) - if resource[:replace].to_s == 'true' + if resource[:append_on_no_match].to_s == 'false' + found = true + elsif resource[:replace].to_s == 'true' found = lines_count > 0 && lines_count == match_count else found = match_count > 0 diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb index 3d691bf..b2357b8 100644 --- a/lib/puppet/type/file_line.rb +++ b/lib/puppet/type/file_line.rb @@ -58,7 +58,7 @@ Puppet::Type.newtype(:file_line) do encoding => "iso-8859-1", } - Files with special characters that are not valid UTF-8 will give the + Files with special characters that are not valid UTF-8 will give the error message "invalid byte sequence in UTF-8". In this case, determine the correct file encoding and specify the correct encoding using the encoding attribute, the value of which needs to be a valid Ruby character @@ -136,6 +136,12 @@ Puppet::Type.newtype(:file_line) do defaultto 'UTF-8' end + newparam(:append_on_no_match) do + desc 'If true, append line if match is not found. If false, do not append line if a match is not found' + newvalues(true, false) + defaultto true + end + # Autorequire the file resource if it's being managed autorequire(:file) do self[:path] diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb index ab6edf9..dcca4a4 100755 --- a/spec/unit/puppet/provider/file_line/ruby_spec.rb +++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -194,6 +194,24 @@ describe provider_class, :unless => Puppet::Util::Platform.windows? do @provider.create expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2") end + + it 'should not add line after no matches found' do + @resource = Puppet::Type::File_line.new( + { + :name => 'foo', + :path => @tmpfile, + :line => 'inserted = line', + :match => '^foo3$', + :append_on_no_match => false, + } + ) + @provider = provider_class.new(@resource) + File.open(@tmpfile, 'w') do |fh| + fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz") + end + expect(@provider.exists?).to be true + expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = blah\nfoo2\nfoo = baz") + end end describe 'using after' do -- cgit v1.2.3 From 39ea2c0bc49d812f41dcbbed6635e25607f80970 Mon Sep 17 00:00:00 2001 From: Nick Walker Date: Tue, 28 Mar 2017 13:16:26 -0700 Subject: Add a round function to complement ceiling and floor --- README.md | 4 ++++ functions/round.pp | 9 +++++++++ spec/functions/round_spec.rb | 11 +++++++++++ 3 files changed, 24 insertions(+) create mode 100644 functions/round.pp create mode 100755 spec/functions/round_spec.rb diff --git a/README.md b/README.md index 61318ea..b92c226 100644 --- a/README.md +++ b/README.md @@ -1509,6 +1509,10 @@ For example, `reject(['aaa','bbb','ccc','aaaddd'], 'aaa')` returns ['bbb','ccc'] Reverses the order of a string or array. +#### `stdlib::round` + + Rounds a number to the nearest integer + *Type*: rvalue. #### `rstrip` diff --git a/functions/round.pp b/functions/round.pp new file mode 100644 index 0000000..6bad92d --- /dev/null +++ b/functions/round.pp @@ -0,0 +1,9 @@ +function stdlib::round( + Numeric $input, +) { + if $input >= 0 { + Integer( $input + 0.5 ) + } else { + Integer( $input - 0.5 ) + } +} diff --git a/spec/functions/round_spec.rb b/spec/functions/round_spec.rb new file mode 100755 index 0000000..5aa801c --- /dev/null +++ b/spec/functions/round_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe 'stdlib::round' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params(34.3).and_return(34) } + it { is_expected.to run.with_params(-34.3).and_return(-34) } + it { is_expected.to run.with_params(34.5).and_return(35) } + it { is_expected.to run.with_params(-34.5).and_return(-35) } + it { is_expected.to run.with_params(34.7).and_return(35) } + it { is_expected.to run.with_params(-34.7).and_return(-35) } +end -- cgit v1.2.3 From 32a6fb29b05d0df233a83982d48b7448d97f48eb Mon Sep 17 00:00:00 2001 From: Reid Vandewiele Date: Fri, 21 Jul 2017 12:16:36 -0700 Subject: Update README for fact() function --- README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/README.md b/README.md index b92c226..2b1e005 100644 --- a/README.md +++ b/README.md @@ -895,6 +895,31 @@ userlist: ensure_resources('user', hiera_hash('userlist'), {'ensure' => 'present'}) ``` +#### `fact` + +Return the value of a given fact. Supports the use of dot-notation for referring to structured facts. If a fact requested does not exist, returns Undef. + +Example usage: + +```puppet +fact('kernel') +fact('osfamily') +fact('os.architecture') +``` + +Array indexing: + +```puppet +$first_processor = fact('processors.models.0') +$second_processor = fact('processors.models.1') +``` + +Fact containing a "." in the fact name: + +```puppet +fact('vmware."VRA.version"') +``` + #### `flatten` Flattens deeply nested arrays and returns a single flat array as a result. -- cgit v1.2.3 From d773497f95d6dd24a9a94a0faf13336d8de41d8e Mon Sep 17 00:00:00 2001 From: Tim Meusel Date: Thu, 27 Jul 2017 01:08:31 +0200 Subject: add documentation --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 61318ea..6848b92 100644 --- a/README.md +++ b/README.md @@ -289,6 +289,10 @@ Unacceptable input example: httds://notquiteright.org ``` +#### `Stdlib::MAC` + +Matches MAC addresses defined in [RFC5342](https://tools.ietf.org/html/rfc5342). + #### `Stdlib::Unixpath` Matches paths on Unix operating systems. -- cgit v1.2.3 From 772a2d2f406b65ef65161a04455865b37a1c8456 Mon Sep 17 00:00:00 2001 From: Eric Putnam Date: Thu, 27 Jul 2017 09:24:52 -0700 Subject: (maint) move/rewrite round() as ruby function --- README.md | 2 +- functions/round.pp | 9 --------- lib/puppet/parser/functions/round.rb | 33 +++++++++++++++++++++++++++++++++ spec/functions/round_spec.rb | 5 ++++- 4 files changed, 38 insertions(+), 11 deletions(-) delete mode 100644 functions/round.pp create mode 100644 lib/puppet/parser/functions/round.rb diff --git a/README.md b/README.md index 629b252..fd5aecb 100644 --- a/README.md +++ b/README.md @@ -1546,7 +1546,7 @@ For example, `reject(['aaa','bbb','ccc','aaaddd'], 'aaa')` returns ['bbb','ccc'] Reverses the order of a string or array. -#### `stdlib::round` +#### `round` Rounds a number to the nearest integer diff --git a/functions/round.pp b/functions/round.pp deleted file mode 100644 index 6bad92d..0000000 --- a/functions/round.pp +++ /dev/null @@ -1,9 +0,0 @@ -function stdlib::round( - Numeric $input, -) { - if $input >= 0 { - Integer( $input + 0.5 ) - } else { - Integer( $input - 0.5 ) - } -} diff --git a/lib/puppet/parser/functions/round.rb b/lib/puppet/parser/functions/round.rb new file mode 100644 index 0000000..489c301 --- /dev/null +++ b/lib/puppet/parser/functions/round.rb @@ -0,0 +1,33 @@ +# +# round.rb +# + +module Puppet::Parser::Functions + newfunction(:round, :type => :rvalue, :doc => <<-EOS + Rounds a number to the nearest integer + + *Examples:* + + round(2.9) + + returns: 3 + + round(2.4) + + returns: 2 + + EOS + ) do |args| + + raise Puppet::ParseError, "round(): Wrong number of arguments given #{args.size} for 1" if args.size != 1 + raise Puppet::ParseError, "round(): Expected a Numeric, got #{args[0].class}" unless args[0].is_a? Numeric + + value = args[0] + + if value >= 0 + Integer(value + 0.5) + else + Integer(value - 0.5) + end + end +end diff --git a/spec/functions/round_spec.rb b/spec/functions/round_spec.rb index 5aa801c..8b13478 100755 --- a/spec/functions/round_spec.rb +++ b/spec/functions/round_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe 'stdlib::round' do +describe 'round' do it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params(34.3).and_return(34) } it { is_expected.to run.with_params(-34.3).and_return(-34) } @@ -8,4 +8,7 @@ describe 'stdlib::round' do it { is_expected.to run.with_params(-34.5).and_return(-35) } it { is_expected.to run.with_params(34.7).and_return(35) } it { is_expected.to run.with_params(-34.7).and_return(-35) } + it { is_expected.to run.with_params("test").and_raise_error Puppet::ParseError } + it { is_expected.to run.with_params("test", "best").and_raise_error Puppet::ParseError } + it { is_expected.to run.with_params(3, 4).and_raise_error Puppet::ParseError } end -- cgit v1.2.3 From 220449a6027fb13e10664a117c6f17331820d32d Mon Sep 17 00:00:00 2001 From: Glenn Sarti Date: Thu, 27 Jul 2017 15:19:18 -0700 Subject: (maint) modulesync 915cde70e20 --- Gemfile | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Gemfile b/Gemfile index 46cb2ea..a9f0161 100644 --- a/Gemfile +++ b/Gemfile @@ -33,13 +33,13 @@ ruby_version_segments = Gem::Version.new(RUBY_VERSION.dup).segments minor_version = "#{ruby_version_segments[0]}.#{ruby_version_segments[1]}" group :development do - gem "puppet-module-posix-default-r#{minor_version}", :require => false, :platforms => "ruby" - gem "puppet-module-win-default-r#{minor_version}", :require => false, :platforms => ["mswin", "mingw", "x64_mingw"] - gem "puppet-module-posix-dev-r#{minor_version}", :require => false, :platforms => "ruby" - gem "puppet-module-win-dev-r#{minor_version}", :require => false, :platforms => ["mswin", "mingw", "x64_mingw"] - gem "json_pure", '<= 2.0.1', :require => false if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.0.0') - gem "fast_gettext", '1.1.0', :require => false if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.1.0') - gem "fast_gettext", :require => false if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.1.0') + gem "puppet-module-posix-default-r#{minor_version}", :require => false, :platforms => "ruby" + gem "puppet-module-win-default-r#{minor_version}", :require => false, :platforms => ["mswin", "mingw", "x64_mingw"] + gem "puppet-module-posix-dev-r#{minor_version}", :require => false, :platforms => "ruby" + gem "puppet-module-win-dev-r#{minor_version}", '0.0.7', :require => false, :platforms => ["mswin", "mingw", "x64_mingw"] + gem "json_pure", '<= 2.0.1', :require => false if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.0.0') + gem "fast_gettext", '1.1.0', :require => false if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.1.0') + gem "fast_gettext", :require => false if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.1.0') end group :system_tests do @@ -50,6 +50,7 @@ group :system_tests do gem "beaker-rspec", *location_for(ENV['BEAKER_RSPEC_VERSION']) gem "beaker-hostgenerator", *location_for(ENV['BEAKER_HOSTGENERATOR_VERSION']) gem "beaker-abs", *location_for(ENV['BEAKER_ABS_VERSION'] || '~> 0.1') + gem "puppet-blacksmith", '~> 3.4', :require => false end gem 'puppet', *location_for(ENV['PUPPET_GEM_VERSION']) -- cgit v1.2.3 -- cgit v1.2.3 From 30533970553e0d68854a2db5c3f6c87f86a0e8ff Mon Sep 17 00:00:00 2001 From: tphoney Date: Wed, 2 Aug 2017 12:42:16 +0100 Subject: MODULES-5382 Add documentation for email functions --- README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/README.md b/README.md index f8762cd..7813f19 100644 --- a/README.md +++ b/README.md @@ -1208,6 +1208,13 @@ Returns `true` if the string passed to this function is a syntactically correct *Type*: rvalue. +#### `is_email_address` + +Returns true if the string passed to this function is a valid email address. + +*Type*: rvalue. + + #### `is_float` **Deprecated. Will be removed in a future version of stdlib. See [`validate_legacy`](#validate_legacy).** @@ -2029,6 +2036,27 @@ validate_domain_name('www.example.2com') *Type*: statement. +#### `validate_email_address` + +Validate that all values passed are valid email addresses. Fail compilation if any value fails this check. + +The following values will pass: + +~~~ +$my_email = "waldo@gmail.com" +validate_email_address($my_email) +validate_email_address("bob@gmail.com", "alice@gmail.com", $my_email) +~~~ + +The following values will fail, causing compilation to abort: + +~~~ +$some_array = [ 'bad_email@/d/efdf.com' ] +validate_email_address($some_array) +~~~ + +*Type*: statement. + #### `validate_hash` **Deprecated. Will be removed in a future version of stdlib. See [`validate_legacy`](#validate_legacy).** -- cgit v1.2.3 From 2d97667b3ed252df709b21abe53f2c837ad2f7a8 Mon Sep 17 00:00:00 2001 From: Eric Putnam Date: Wed, 9 Aug 2017 11:13:51 -0700 Subject: (maint) revert puppet version requirement puppet version requirement lower bound was moved to 4.7.0 without a major bump. this returns the lower bound to 2.7 but also pushes the upper bound to include puppet 5. --- metadata.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metadata.json b/metadata.json index f2b0204..66c47c1 100644 --- a/metadata.json +++ b/metadata.json @@ -101,7 +101,7 @@ "requirements": [ { "name": "puppet", - "version_requirement": ">= 4.7.0 < 6.0.0" + "version_requirement": ">=2.7.20 < 6.0.0" } ], "description": "Standard Library for Puppet Modules", -- cgit v1.2.3