diff options
30 files changed, 501 insertions, 240 deletions
@@ -21,7 +21,7 @@ group :development, :unit_tests do gem 'puppet_facts' gem 'puppet-blacksmith', '>= 3.4.0' gem 'puppetlabs_spec_helper', '>= 1.2.1' - gem 'rspec-puppet', '>= 2.3.2' + gem 'rspec-puppet', '>= 2.3.2', :git => 'https://github.com/rodjek/rspec-puppet.git', :branch => 'fb27c533e2664057fba4b73d0bd902a946abfce0' gem 'rspec-puppet-facts' gem 'simplecov' gem 'parallel_tests' diff --git a/README.markdown b/README.markdown index 26fe55f..667da33 100644 --- a/README.markdown +++ b/README.markdown @@ -304,7 +304,7 @@ Deletes all instances of the undef value from an array or hash. For example, `$h #### `deprecation` -Function to print deprecation warnings, Logs a warning once for a given key. The uniqueness key - can appear once. The msg is the message text including any positional information that is formatted by the user/caller of the method It is affected by the puppet setting 'strict', which can be set to :error (outputs as an error message), :off (no message / error is displayed) and :warning (default, outputs a warning) *Type*: String, String. +A function to print deprecation warnings, logs a warning once for a given key. The uniqueness key - can appear once. The msg is the message text including any positional information that is formatted by the user/caller of the method. It is affected by the puppet setting 'strict', which can be set to :error (outputs as an error message), :off (no message / error is displayed) and :warning (default, outputs a warning). In Puppet version less than 4.0 an environment variable can be set to decide whether or not to log deprecation warnings (ENV[STDLIB_LOG_DEPRECATION]). If this environment variable is set to true, the functions will log a warning. *Type*: String, String. #### `difference` @@ -1370,6 +1370,62 @@ The following values will fail, causing compilation to abort: #### `validate_legacy` +This function supports updating modules to upgrade from Puppet 3 style argument validation (using the stdlib `validate_*` functions) to Puppet 4 data types, without breaking the world. + +##### For module users + +You are probably here, because you're receiving loads of deprecation warnings about `validate_*` functions. If you're still on Puppet 3, use the options described in [`deprecation`](#deprecation) to silence the messages for now, and avoid upgrading to module versions which already have dropped Puppet 3 support. If you're already running Puppet 4 (shipped in Puppet Enterprise 2015.2 or later), please read on to understand the required steps to fix those issues. + +The `validate_*` functions were the only way on Puppet 3 to easily check the types of class and define arguments. Additionally some of the functions provided additional helpers like [validate_numeric](#validate_numeric), which would not only allow numbers, but also arrays of numbers. Puppet 4 now allows much better defined type checking using [data types](https://docs.puppet.com/puppet/latest/reference/lang_data.html), without such surprises. To avoid breaking the world for people depending on those surprises, `validate_legacy` provides a way to make those edge-cases visible, and assist people with getting onto the clearer Puppet 4 syntax. + +Depending on the current state of development of the modules you use and the data you feed them, you'll encounter different messages: + +* "Notice: Accepting previously invalid value for target type '<type>'": Nothing to worry, you're already using values allowed by the new type, that would have been invalid by the old validation function. This is informational only. +* "Warning: This method is deprecated, please use the stdlib validate_legacy function": The module you're using hasn't yet upgraded to validate_legacy, use the options from [deprecation()](#deprecation) to silence the warnings for now, or submit a patch with you module's developer. See below for details. +* "Warning: validate_legacy(<function>) expected <type> value, got <actual type>": Your code is passing a value to the module that was accepted by the Puppet v3 style validation, but that will not be accepted by the next version of the module. Most often this can be fixed by removing quotes from numbers, or booleans. +* "Error: Evaluation Error: Error while evaluating a Resource Statement, Evaluation Error: Error while evaluating a Function Call, validate_legacy(<function>) expected <type> value, got <actual type>": Your code is passing a value that is not acceptable to either the new, or the old style validation. + + + + +##### For module developers + +Many `validate_*` functions have surprising holes in their validation. For example, [validate_numeric](#validate_numeric) would not only allow numbers, but also arrays of numbers, or strings that look like numbers, without giving you any control over the specifics. Contrast that to Puppet 4 [data types](https://docs.puppet.com/puppet/latest/reference/lang_data.html) which allow you to choose between `Numeric`, `Array[Numeric]`, or `Optional[Numeric]`. To get from here to there, without leaving your users behind, the validate_legacy function will provide you with a tool to make this migration as painless as possible. + +To start out, for each parameter of your classes and defines, you'll have to decide on a new Puppet 4 data type to use. In most cases the new data type will allow a different set of values than the original `validate_*` function (which is the point of the whole exercise). The situation then looks like this: + +| | `validate_` pass | `validate_` fail | +| ------------ | ---------------- | ---------------- | +| matches type | pass | pass, notice | +| fails type | pass, deprecated | fail | + +The code after the validation will still have to deal with all possible values for now, but users of your code can now change their manifests to only pass values that will match the new type. + +To effect those checks, given a class like this: +~~~ +class example($value) { + validate_numeric($value) +~~~ +which should only accept numbers, the resulting validation code looks like this: +~~~ +class example( + Variant[Stdlib::Compat::Numeric, Numeric] $value +) { + validate_legacy(Numeric, 'validate_numeric', $value) +~~~ + +The type of `$value` is defined as `Variant[Stdlib::Compat::Numeric, Numeric]`, which will allow any `Numeric` (the new type), and all values previously accepted by validate_numeric (through `Stdlib::Compat::Numeric`). For each `validate_*` function in stdlib, there is a matching `Stdlib::Compat::*` type that will allow the appropriate set of values through. See the documentation in the `types/` directory in the stdlib source code for caveats. + +The call to `validate_legacy` will take care of triggering the correct log or fail message for you. It requires the new type, the previous validation function name, and all arguments to that function. + +If you were previously still supporting Puppet 3, this is a breaking change. You'll need to update your `metadata.json` to not support Puppet 3 anymore in the `requirements` section, and bump the major version of your module. This should still pass all previously existing tests your module has. Do not forget to add more tests for the newly possible values. This is also a good time to start calling [`deprecation()`](#deprecation) for all parameters you always wanted to get rid of, or add additional constraints on your parameters. + +After releasing this version, you can at any time release another breaking change release where you remove all compat types, and all calls to `validate_legacy`. At this time you can also go through your code and remove all leftovers dealing with the previously possible values. + +At all times note in the CHANGELOG and the README at what stage of the process your modules currently are. + +##### Reference + Validates a value against both a specified type and a deprecated validation function. Silently passes if both pass, errors if one validation passes and the other does not, fails if both validations return false. Arguments include the type to check the value against, the full name of the previous validation function, the value itself to be checked, and an unspecified amount of arguments needed for the previous validation function. @@ -1381,7 +1437,7 @@ Example: #### `validate_numeric` -Validates that the first argument is a numeric value (or an array of numeric values). Aborts catalog compilation if any of the checks fail. +Validates that the first argument is a numeric value (or an array or string of numeric values). Aborts catalog compilation if any of the checks fail. The second argument is optional and passes a maximum. (All elements of) the first argument has to be less or equal to this max. diff --git a/lib/puppet/functions/deprecation.rb b/lib/puppet/functions/deprecation.rb index 3b84ae5..30aeb1d 100644 --- a/lib/puppet/functions/deprecation.rb +++ b/lib/puppet/functions/deprecation.rb @@ -1,5 +1,4 @@ # Function to print deprecation warnings, Logs a warning once for a given key. The uniqueness key - can appear once. The msg is the message text including any positional information that is formatted by the user/caller of the method It is affected by the puppet setting 'strict', which can be set to :error (outputs as an error message), :off (no message / error is displayed) and :warning (default, outputs a warning) *Type*: String, String. -# Puppet::Functions.create_function(:deprecation) do dispatch :deprecation do @@ -9,13 +8,16 @@ Puppet::Functions.create_function(:deprecation) do def deprecation(key, message) # depending on configuration setting of strict + caller_infos = caller.first.split(":") case Puppet.settings[:strict] when :off # do nothing when :error - fail("deprecation. #{key}. #{message}") + err_message = "#{message} : #{caller_infos[0]} : #{caller_infos[1]}" + fail("deprecation. #{key}. #{err_message}") else - Puppet.warn_once('deprecation', key, message) + err_message = "#{message} : #{caller_infos[0]} : #{caller_infos[1]}" + Puppet.deprecation_warning(err_message, key) end end end diff --git a/lib/puppet/functions/validate_legacy.rb b/lib/puppet/functions/validate_legacy.rb index 9d7d012..0ba6dd8 100644 --- a/lib/puppet/functions/validate_legacy.rb +++ b/lib/puppet/functions/validate_legacy.rb @@ -2,46 +2,49 @@ Puppet::Functions.create_function(:validate_legacy, Puppet::Functions::InternalF # The function checks a value against both the target_type (new) and the previous_validation function (old). dispatch :validate_legacy do + scope_param param 'Type', :target_type - param 'String', :previous_validation - param 'NotUndef', :value - optional_param 'Any', :args + param 'String', :function_name + param 'Any', :value + optional_repeated_param 'Any', :args end + dispatch :validate_legacy_s do scope_param param 'String', :type_string - param 'String', :previous_validation - param 'NotUndef', :value + param 'String', :function_name + param 'Any', :value optional_repeated_param 'Any', :args end def validate_legacy_s(scope, type_string, *args) t = Puppet::Pops::Types::TypeParser.new.parse(type_string, scope) - validate_legacy(t, *args) + validate_legacy(scope, t, *args) end - def validate_legacy(target_type, previous_validation, value, *prev_args) + def validate_legacy(scope, target_type, function_name, value, *prev_args) if assert_type(target_type, value) - if previous_validation(previous_validation, value, *prev_args) + if previous_validation(scope, function_name, value, *prev_args) # Silently passes else - Puppet.warn("Accepting previously invalid value for target_type '#{target_type}'") + Puppet.notice("Accepting previously invalid value for target type '#{target_type}'") end else + caller_infos = caller.first.split(":") inferred_type = Puppet::Pops::Types::TypeCalculator.infer_set(value) - error_msg = Puppet::Pops::Types::TypeMismatchDescriber.new.describe_mismatch(previous_validation, target_type, inferred_type) - if previous_validation(previous_validation, value, *prev_args) - Puppet.warn(error_msg) + error_msg = Puppet::Pops::Types::TypeMismatchDescriber.new.describe_mismatch("validate_legacy(#{function_name}) [#{caller_infos[0]}:#{caller_infos[1]}]", target_type, inferred_type) + if previous_validation(scope, function_name, value, *prev_args) + call_function('deprecation', 'validate_legacy', error_msg) else call_function('fail', error_msg) end end end - def previous_validation(previous_validation, value, *prev_args) + def previous_validation(scope, function_name, value, *prev_args) # Call the previous validation function and catch any errors. Return true if no errors are thrown. begin - call_function(previous_validation, value, *prev_args) + scope.send("function_#{function_name}".to_s, [value, *prev_args]) true rescue Puppet::ParseError false diff --git a/lib/puppet/parser/functions/deprecation.rb b/lib/puppet/parser/functions/deprecation.rb index fc861a6..0cb247d 100644 --- a/lib/puppet/parser/functions/deprecation.rb +++ b/lib/puppet/parser/functions/deprecation.rb @@ -9,7 +9,11 @@ EOS key = arguments[0] message = arguments[1] - - warning("deprecation. #{key}. #{message}") + + if ENV['STDLIB_LOG_DEPRECATIONS'] == "true" + caller_infos = caller.first.split(":") + err_message = "#{message} : #{caller_infos[0]} : #{caller_infos[1]}" + warning("deprecation. #{key}. #{err_message}") + end end end diff --git a/spec/acceptance/deprecation_spec.rb b/spec/acceptance/deprecation_spec.rb index d0d7fed..ea13700 100644 --- a/spec/acceptance/deprecation_spec.rb +++ b/spec/acceptance/deprecation_spec.rb @@ -1,19 +1,29 @@ #! /usr/bin/env ruby -S rspec require 'spec_helper_acceptance' -require 'shellwords' describe 'deprecation function' do - before :each do - FileUtils.rm_rf '/tmp/deprecation' + + if fact('operatingsystem') == 'windows' + test_file = 'C:/deprecation' + else + test_file = "/tmp/deprecation" + end + + # It seems that Windows needs everything to be on one line when using puppet apply -e, otherwise the manifests would be in an easier format + add_file_manifest = "\"deprecation('key', 'message') file { '#{test_file}': ensure => present, content => 'test', }\"" + remove_file_manifest = "file { '#{test_file}': ensure => absent }" + + before :all do + apply_manifest(remove_file_manifest) end context 'with --strict=error', if: get_puppet_version =~ /^4/ do before :all do - pp = <<-EOS - deprecation('key', 'message') - file { '/tmp/deprecation': ensure => present } - EOS - @result = on(default, puppet('apply', '--strict=error', '-e', Shellwords.shellescape(pp)), acceptable_exit_codes: (0...256)) + @result = on(default, puppet('apply', '--strict=error', '-e', add_file_manifest), acceptable_exit_codes: (0...256)) + end + + after :all do + apply_manifest(remove_file_manifest) end it "should return an error" do @@ -24,18 +34,18 @@ describe 'deprecation function' do expect(@result.stderr).to match(/deprecation. key. message/) end - describe file('/tmp/deprecation') do - it { is_expected.not_to exist } + describe file("#{test_file}") do + it { is_expected.not_to be_file } end end context 'with --strict=warning', if: get_puppet_version =~ /^4/ do before :all do - pp = <<-EOS - deprecation('key', 'message') - file { '/tmp/deprecation': ensure => present } - EOS - @result = on(default, puppet('apply', '--strict=warning', '-e', Shellwords.shellescape(pp)), acceptable_exit_codes: (0...256)) + @result = on(default, puppet('apply', '--strict=warning', '-e', add_file_manifest), acceptable_exit_codes: (0...256)) + end + + after :all do + apply_manifest(remove_file_manifest) end it "should not return an error" do @@ -46,18 +56,18 @@ describe 'deprecation function' do expect(@result.stderr).to match(/Warning: message/) end - describe file('/tmp/deprecation') do - it { is_expected.to exist } + describe file("#{test_file}") do + it { is_expected.to be_file } end end context 'with --strict=off', if: get_puppet_version =~ /^4/ do before :all do - pp = <<-EOS - deprecation('key', 'message') - file { '/tmp/deprecation': ensure => present } - EOS - @result = on(default, puppet('apply', '--strict=off', '-e', Shellwords.shellescape(pp)), acceptable_exit_codes: (0...256)) + @result = on(default, puppet('apply', '--strict=off', '-e', add_file_manifest), acceptable_exit_codes: (0...256)) + end + + after :all do + apply_manifest(remove_file_manifest) end it "should not return an error" do @@ -68,8 +78,8 @@ describe 'deprecation function' do expect(@result.stderr).not_to match(/Warning: message/) end - describe file('/tmp/deprecation') do - it { is_expected.to exist } + describe file("#{test_file}") do + it { is_expected.to be_file } end end end diff --git a/spec/classes/validate_legacy_spec.rb b/spec/classes/validate_legacy_spec.rb deleted file mode 100644 index ded6890..0000000 --- a/spec/classes/validate_legacy_spec.rb +++ /dev/null @@ -1,39 +0,0 @@ -require 'spec_helper' - -if Puppet.version.to_f >= 4.0 - # validate_legacy requires a proper scope to run, so we have to trigger a true compilation here, - # instead of being able to leverage the function test group. - describe 'test::validate_legacy', type: :class do - - describe 'validate_legacy passes assertion of type but not previous validation' do - let(:params) {{ type: "Optional[Integer]", prev_validation: "validate_re", value: 5, previous_arg1: ["^\\d+$", ""] }} - it { - Puppet.expects(:warn).with(includes('Accepting previously invalid value for target_type')) - is_expected.to compile - } - end - - describe 'validate_legacy passes assertion of type and previous validation' do - let(:params) {{ type: "Optional[String]", prev_validation: "validate_re", value: "5", previous_arg1: ["."] }} - it { is_expected.to compile } - end - - describe 'validate_legacy fails assertion of type and passes previous validation' do - let(:params) {{ type: "Optional[Integer]", prev_validation: "validate_re", value: "5", previous_arg1: ["."] }} - it { - Puppet.expects(:warn).with(includes('expected')) - is_expected.to compile - } - end - - describe 'validate_legacy fails assertion and fails previous validation' do - let(:params) {{ type: "Optional[Integer]", prev_validation: "validate_re", value: "5", previous_arg1: ["thisisnotright"] }} - it { is_expected.to compile.and_raise_error(/Error while evaluating a Function Call, \w* expected an \w* value, got \w*/) } - end - - describe 'validate_legacy works with multi-argument validate_ functions' do - let(:params) {{ type: "Integer", prev_validation: "validate_integer", value: 10, previous_arg1: 100, previous_arg2: 0 }} - it { is_expected.to compile } - end - end -end diff --git a/spec/fixtures/test/manifests/validate_legacy.pp b/spec/fixtures/test/manifests/validate_legacy.pp deleted file mode 100644 index 706df88..0000000 --- a/spec/fixtures/test/manifests/validate_legacy.pp +++ /dev/null @@ -1,18 +0,0 @@ -# Class to test stdlib validate_legacy function - -class test::validate_legacy( - $type, - $prev_validation, - $value, - $previous_arg1, - $previous_arg2 = undef, - ) { - - if $previous_arg2 == undef { - validate_legacy( $type, $prev_validation, $value, $previous_arg1 ) - } else { - validate_legacy( $type, $prev_validation, $value, $previous_arg1, $previous_arg2 ) - } - notice("Success") - -} diff --git a/spec/functions/assert_private_spec.rb b/spec/functions/assert_private_spec.rb index 98f2598..355e0dd 100755 --- a/spec/functions/assert_private_spec.rb +++ b/spec/functions/assert_private_spec.rb @@ -5,31 +5,26 @@ describe 'assert_private' do it "should not fail" do scope.expects(:lookupvar).with('module_name').returns('foo') scope.expects(:lookupvar).with('caller_module_name').returns('foo') - expect { - subject.call [] - }.not_to raise_error + + is_expected.to run.with_params() end end - context "with an explicit failure message" do - it "prints the failure message on error" do + context "when called from private class" do + before :each do scope.expects(:lookupvar).with('module_name').returns('foo') scope.expects(:lookupvar).with('caller_module_name').returns('bar') - expect { - subject.call ['failure message!'] - }.to raise_error Puppet::ParseError, /failure message!/ end - end - context "when called from private class" do it "should fail with a class error message" do - scope.expects(:lookupvar).with('module_name').returns('foo') - scope.expects(:lookupvar).with('caller_module_name').returns('bar') scope.source.expects(:name).returns('foo::baz') scope.source.expects(:type).returns('hostclass') - expect { - subject.call [] - }.to raise_error Puppet::ParseError, /Class foo::baz is private/ + + is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /Class foo::baz is private/) + end + + context "with an explicit failure message" do + it { is_expected.to run.with_params('failure message!').and_raise_error(Puppet::ParseError, /failure message!/) } end end @@ -39,9 +34,8 @@ describe 'assert_private' do scope.expects(:lookupvar).with('caller_module_name').returns('bar') scope.source.expects(:name).returns('foo::baz') scope.source.expects(:type).returns('definition') - expect { - subject.call [] - }.to raise_error Puppet::ParseError, /Definition foo::baz is private/ + + is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /Definition foo::baz is private/) end end end diff --git a/spec/functions/deprecation_spec.rb b/spec/functions/deprecation_spec.rb index c213190..4321c3d 100644 --- a/spec/functions/deprecation_spec.rb +++ b/spec/functions/deprecation_spec.rb @@ -42,6 +42,10 @@ if Puppet.version.to_f >= 4.0 end else describe 'deprecation' do + after(:context) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + ENV['STDLIB_LOG_DEPRECATIONS'] = "true" it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } diff --git a/spec/functions/is_array_spec.rb b/spec/functions/is_array_spec.rb index 0a8070b..2350b7f 100755 --- a/spec/functions/is_array_spec.rb +++ b/spec/functions/is_array_spec.rb @@ -1,12 +1,8 @@ require 'spec_helper' describe 'is_array' do + it { is_expected.not_to eq(nil) } - # Checking for deprecation warning - it 'should display a single deprecation' do - scope.expects(:warning).with(includes('This method is deprecated')) - is_expected.to run.with_params([]) - end 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.") @@ -21,4 +17,20 @@ describe 'is_array' do it { is_expected.to run.with_params('one').and_return(false) } it { is_expected.to run.with_params(1).and_return(false) } it { is_expected.to run.with_params({}).and_return(false) } + context 'Checking for deprecation warning' do + after(:context) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + # Checking for deprecation warning, which should only be provoked when the env variable for it is set. + it 'should display a single deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "true" + scope.expects(:warning).with(includes('This method is deprecated')) + is_expected.to run.with_params(['1.2.3.4']).and_return(true) + end + it 'should display no warning for deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "false" + scope.expects(:warning).with(includes('This method is deprecated')).never + is_expected.to run.with_params(['1.2.3.4']).and_return(true) + end + end end diff --git a/spec/functions/is_bool_spec.rb b/spec/functions/is_bool_spec.rb index 4550e61..7b2173e 100755 --- a/spec/functions/is_bool_spec.rb +++ b/spec/functions/is_bool_spec.rb @@ -1,12 +1,8 @@ require 'spec_helper' describe 'is_bool' do + it { is_expected.not_to eq(nil) } - # Checking for deprecation warning - it 'should display a single deprecation' do - scope.expects(:warning).with(includes('This method is deprecated')) - is_expected.to run.with_params(true) - end it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } it { is_expected.to run.with_params(true, false).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } it { is_expected.to run.with_params(true).and_return(true) } @@ -17,4 +13,21 @@ describe 'is_bool' do it { is_expected.to run.with_params([true]).and_return(false) } it { is_expected.to run.with_params('true').and_return(false) } it { is_expected.to run.with_params('false').and_return(false) } + context 'Checking for deprecation warning' do + after(:context) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + # Checking for deprecation warning, which should only be provoked when the env variable for it is set. + it 'should display a single deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "true" + scope.expects(:warning).with(includes('This method is deprecated')) + is_expected.to run.with_params(true).and_return(true) + end + it 'should display no warning for deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "false" + scope.expects(:warning).with(includes('This method is deprecated')).never + is_expected.to run.with_params(false).and_return(true) + end + end end + diff --git a/spec/functions/is_float_spec.rb b/spec/functions/is_float_spec.rb index cb7e148..44bdc48 100755 --- a/spec/functions/is_float_spec.rb +++ b/spec/functions/is_float_spec.rb @@ -1,13 +1,8 @@ require 'spec_helper' describe 'is_float' do - it { is_expected.not_to eq(nil) } - # Checking for deprecation warning - it 'should display a single deprecation' do - scope.expects(:warning).with(includes('This method is deprecated')) - is_expected.to run.with_params(3) - end + 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(0.1, 0.2).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } @@ -26,4 +21,22 @@ describe 'is_float' do it { is_expected.to run.with_params(1.0).and_return(true) } it { is_expected.to run.with_params(1).and_return(false) } end + + context 'Checking for deprecation warning' do + after(:context) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + # Checking for deprecation warning, which should only be provoked when the env variable for it is set. + it 'should display a single deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "true" + scope.expects(:warning).with(includes('This method is deprecated')) + is_expected.to run.with_params(2.2).and_return(true) + end + it 'should display no warning for deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "false" + scope.expects(:warning).with(includes('This method is deprecated')).never + is_expected.to run.with_params(1.0).and_return(true) + end + end + end diff --git a/spec/functions/is_integer_spec.rb b/spec/functions/is_integer_spec.rb index 2d68731..4b5dd21 100755 --- a/spec/functions/is_integer_spec.rb +++ b/spec/functions/is_integer_spec.rb @@ -1,14 +1,9 @@ require 'spec_helper' describe 'is_integer' do + it { is_expected.not_to eq(nil) } - # Checking for deprecation warning - it 'should display a single deprecation' do - scope.expects(:warning).with(includes('This method is deprecated')) - is_expected.to run.with_params(3) - end - it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } it { is_expected.to run.with_params(1, 2).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } @@ -16,7 +11,7 @@ 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(3.7).and_return(false) } it { is_expected.to run.with_params('3.7').and_return(false) } it { is_expected.to run.with_params(-3.7).and_return(false) } @@ -29,4 +24,22 @@ 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) } + + context 'Checking for deprecation warning' do + after(:context) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + # Checking for deprecation warning, which should only be provoked when the env variable for it is set. + it 'should display a single deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "true" + scope.expects(:warning).with(includes('This method is deprecated')) + is_expected.to run.with_params(50).and_return(true) + end + it 'should display no warning for deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "false" + scope.expects(:warning).with(includes('This method is deprecated')).never + is_expected.to run.with_params(50).and_return(true) + end + end + end diff --git a/spec/functions/is_ip_address_spec.rb b/spec/functions/is_ip_address_spec.rb index 9386ca9..fc8af60 100755 --- a/spec/functions/is_ip_address_spec.rb +++ b/spec/functions/is_ip_address_spec.rb @@ -20,9 +20,21 @@ describe 'is_ip_address' do it { is_expected.to run.with_params(1).and_return(false) } it { is_expected.to run.with_params({}).and_return(false) } it { is_expected.to run.with_params([]).and_return(false) } - # Checking for deprecation warning - it 'should display a single deprecation' do - scope.expects(:warning).with(includes('This method is deprecated')) - is_expected.to run.with_params('1.1.1.1') + + context 'Checking for deprecation warning', if: Puppet.version.to_f < 4.0 do + # Checking for deprecation warning, which should only be provoked when the env variable for it is set. + it 'should display a single deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "true" + scope.expects(:warning).with(includes('This method is deprecated')) + is_expected.to run.with_params('1.2.3.4').and_return(true) + end + it 'should display no warning for deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "false" + scope.expects(:warning).with(includes('This method is deprecated')).never + is_expected.to run.with_params('1.2.3.4').and_return(true) + end + after(:context) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end end end diff --git a/spec/functions/is_ipv4_address_spec.rb b/spec/functions/is_ipv4_address_spec.rb index 2b9fc49..9d53a9d 100644 --- a/spec/functions/is_ipv4_address_spec.rb +++ b/spec/functions/is_ipv4_address_spec.rb @@ -1,6 +1,7 @@ require 'spec_helper' describe 'is_ipv4_address' 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('1.2.3.4').and_return(true) } @@ -9,9 +10,21 @@ describe 'is_ipv4_address' do it { is_expected.to run.with_params('1.2.3.4.5').and_return(false) } it { is_expected.to run.with_params('').and_return(false) } it { is_expected.to run.with_params('one').and_return(false) } - # Checking for deprecation warning - it 'should display a single deprecation' do - scope.expects(:warning).with(includes('This method is deprecated')) - is_expected.to run.with_params('1.1.1.1') + + context 'Checking for deprecation warning', if: Puppet.version.to_f < 4.0 do + after(:context) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + # Checking for deprecation warning, which should only be provoked when the env variable for it is set. + it 'should display a single deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "true" + scope.expects(:warning).with(includes('This method is deprecated')) + is_expected.to run.with_params('1.2.3.4').and_return(true) + end + it 'should display no warning for deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "false" + scope.expects(:warning).with(includes('This method is deprecated')).never + is_expected.to run.with_params('1.2.3.4').and_return(true) + end end end diff --git a/spec/functions/is_ipv6_address_spec.rb b/spec/functions/is_ipv6_address_spec.rb index e3e4734..4449fea 100644 --- a/spec/functions/is_ipv6_address_spec.rb +++ b/spec/functions/is_ipv6_address_spec.rb @@ -1,6 +1,7 @@ require 'spec_helper' describe 'is_ipv6_address' 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('2001:0db8:85a3:0000:0000:8a2e:0370:7334').and_return(true) } @@ -9,9 +10,21 @@ describe 'is_ipv6_address' do it { is_expected.to run.with_params('1.2.3.4.5').and_return(false) } it { is_expected.to run.with_params('').and_return(false) } it { is_expected.to run.with_params('one').and_return(false) } - # Checking for deprecation warning - it 'should display a single deprecation' do - scope.expects(:warning).with(includes('This method is deprecated')) - is_expected.to run.with_params('2001:0db8:85a3:0000:0000:8a2e:0370:7334') + + context 'Checking for deprecation warning', if: Puppet.version.to_f < 4.0 do + after(:context) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + # Checking for deprecation warning, which should only be provoked when the env variable for it is set. + it 'should display a single deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "true" + scope.expects(:warning).with(includes('This method is deprecated')) + is_expected.to run.with_params('2001:0db8:85a3:0000:0000:8a2e:0370:7334').and_return(true) + end + it 'should display no warning for deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "false" + scope.expects(:warning).with(includes('This method is deprecated')).never + is_expected.to run.with_params('2001:0db8:85a3:0000:0000:8a2e:0370:7334').and_return(true) + end end end diff --git a/spec/functions/is_numeric_spec.rb b/spec/functions/is_numeric_spec.rb index f4ead56..ccfb9d8 100755 --- a/spec/functions/is_numeric_spec.rb +++ b/spec/functions/is_numeric_spec.rb @@ -1,14 +1,9 @@ require 'spec_helper' describe 'is_numeric' do + it { is_expected.not_to eq(nil) } - # Checking for deprecation warning - it 'should display a single deprecation' do - scope.expects(:warning).with(includes('This method is deprecated')) - is_expected.to run.with_params(3) - end - it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } it { is_expected.to run.with_params(1, 2).and_raise_error(Puppet::ParseError, /wrong number of arguments/i) } @@ -32,4 +27,22 @@ describe 'is_numeric' do 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(' - 1234').and_return(false) } + + context 'Checking for deprecation warning' do + after(:context) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + # Checking for deprecation warning, which should only be provoked when the env variable for it is set. + it 'should display a single deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "true" + scope.expects(:warning).with(includes('This method is deprecated')) + is_expected.to run.with_params(7).and_return(true) + end + it 'should display no warning for deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "false" + scope.expects(:warning).with(includes('This method is deprecated')).never + is_expected.to run.with_params(7).and_return(true) + end + end + end diff --git a/spec/functions/is_string_spec.rb b/spec/functions/is_string_spec.rb index 4f59b63..460d6e0 100755 --- a/spec/functions/is_string_spec.rb +++ b/spec/functions/is_string_spec.rb @@ -1,12 +1,8 @@ require 'spec_helper' describe 'is_string' do + it { is_expected.not_to eq(nil) } - # Checking for deprecation warning - it 'should display a single deprecation' do - scope.expects(:warning).with(includes('This method is deprecated')) - is_expected.to run.with_params('ha') - end 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.") @@ -30,4 +26,22 @@ describe 'is_string' do it { is_expected.to run.with_params(false).and_return(false) } it { is_expected.to run.with_params('one').and_return(true) } it { is_expected.to run.with_params('0001234').and_return(true) } + + context 'Checking for deprecation warning' do + after(:context) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + # Checking for deprecation warning, which should only be provoked when the env variable for it is set. + it 'should display a single deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "true" + scope.expects(:warning).with(includes('This method is deprecated')) + is_expected.to run.with_params('sponge').and_return(true) + end + it 'should display no warning for deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "false" + scope.expects(:warning).with(includes('This method is deprecated')).never + is_expected.to run.with_params('bob').and_return(true) + end + end + end diff --git a/spec/functions/validate_absolute_path_spec.rb b/spec/functions/validate_absolute_path_spec.rb index dbac88f..75df6bc 100755 --- a/spec/functions/validate_absolute_path_spec.rb +++ b/spec/functions/validate_absolute_path_spec.rb @@ -1,9 +1,14 @@ require 'spec_helper' describe 'validate_absolute_path' do + after(:context) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + # Checking for deprecation warning it 'should display a single deprecation' do # called twice because validate_absolute_path calls is_absolute_path + ENV['STDLIB_LOG_DEPRECATIONS'] = "true" scope.expects(:warning).with(includes('This method is deprecated')).twice is_expected.to run.with_params('c:/') end diff --git a/spec/functions/validate_array_spec.rb b/spec/functions/validate_array_spec.rb index 9f2e210..f395d16 100755 --- a/spec/functions/validate_array_spec.rb +++ b/spec/functions/validate_array_spec.rb @@ -1,10 +1,15 @@ require 'spec_helper' describe 'validate_array' do + describe 'signature validation' do + after(:context) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end it { is_expected.not_to eq(nil) } # Checking for deprecation warning it 'should display a single deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "true" scope.expects(:warning).with(includes('This method is deprecated')) is_expected.to run.with_params([]) end diff --git a/spec/functions/validate_bool_spec.rb b/spec/functions/validate_bool_spec.rb index 724e31f..4bd1453 100755 --- a/spec/functions/validate_bool_spec.rb +++ b/spec/functions/validate_bool_spec.rb @@ -1,9 +1,14 @@ require 'spec_helper' describe 'validate_bool' do + after(:context) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + # Checking for deprecation warning it 'should display a single deprecation' do #called twice, because validate_bool calls is_bool + ENV['STDLIB_LOG_DEPRECATIONS'] = "true" scope.expects(:warning).with(includes('This method is deprecated')).twice is_expected.to run.with_params(true) end diff --git a/spec/functions/validate_integer_spec.rb b/spec/functions/validate_integer_spec.rb index 3ca50b2..ffc59f8 100755 --- a/spec/functions/validate_integer_spec.rb +++ b/spec/functions/validate_integer_spec.rb @@ -1,8 +1,13 @@ require 'spec_helper' describe 'validate_integer' do + after(:context) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + # Checking for deprecation warning it 'should display a single deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "true" scope.expects(:warning).with(includes('This method is deprecated')) is_expected.to run.with_params(3) end diff --git a/spec/functions/validate_ip_address_spec.rb b/spec/functions/validate_ip_address_spec.rb index 10f6c37..7040a8d 100644 --- a/spec/functions/validate_ip_address_spec.rb +++ b/spec/functions/validate_ip_address_spec.rb @@ -1,6 +1,7 @@ require 'spec_helper' describe 'validate_ip_address' 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) } @@ -19,10 +20,24 @@ describe 'validate_ip_address' do it { is_expected.to run.with_params('3ffe:0505:0002::', '3ffe:0505:0002::2') } it { is_expected.to run.with_params('::1/64') } it { is_expected.to run.with_params('fe80::a00:27ff:fe94:44d6/64') } - it 'should display a single deprecation' do - scope.expects(:warning).with(includes('This method is deprecated')) - is_expected.to run.with_params('1.1.1.1') + + context 'Checking for deprecation warning', if: Puppet.version.to_f < 4.0 do + after(:context) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + # Checking for deprecation warning, which should only be provoked when the env variable for it is set. + it 'should display a single deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "true" + scope.expects(:warning).with(includes('This method is deprecated')) + is_expected.to run.with_params('1.2.3.4') + end + it 'should display no warning for deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "false" + scope.expects(:warning).with(includes('This method is deprecated')).never + is_expected.to run.with_params('1.2.3.4') + end end + context 'with netmasks' do it { is_expected.to run.with_params('8.8.8.8/0') } it { is_expected.to run.with_params('8.8.8.8/16') } diff --git a/spec/functions/validate_ipv4_address_spec.rb b/spec/functions/validate_ipv4_address_spec.rb index b67bf6f..b60dc8e 100755 --- a/spec/functions/validate_ipv4_address_spec.rb +++ b/spec/functions/validate_ipv4_address_spec.rb @@ -1,45 +1,58 @@ require 'spec_helper' describe 'validate_ipv4_address' 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 + context 'Checking for deprecation warning', if: Puppet.version.to_f < 4.0 do + after(:context) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + # Checking for deprecation warning, which should only be provoked when the env variable for it is set. it 'should display a single deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "true" scope.expects(:warning).with(includes('This method is deprecated')) - is_expected.to run.with_params('1.1.1.1') + is_expected.to run.with_params('1.2.3.4') end - - describe 'valid inputs' do - it { is_expected.to run.with_params('0.0.0.0') } - it { is_expected.to run.with_params('8.8.8.8') } - it { is_expected.to run.with_params('127.0.0.1') } - it { is_expected.to run.with_params('10.10.10.10') } - it { is_expected.to run.with_params('194.232.104.150') } - it { is_expected.to run.with_params('244.24.24.24') } - it { is_expected.to run.with_params('255.255.255.255') } - it { is_expected.to run.with_params('1.2.3.4', '5.6.7.8') } - context 'with netmasks' do - it { is_expected.to run.with_params('8.8.8.8/0') } - it { is_expected.to run.with_params('8.8.8.8/16') } - it { is_expected.to run.with_params('8.8.8.8/32') } - it { is_expected.to run.with_params('8.8.8.8/255.255.0.0') } - end + it 'should display no warning for deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "false" + scope.expects(:warning).with(includes('This method is deprecated')).never + is_expected.to run.with_params('1.2.3.4') end + 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(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('one').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) } - it { is_expected.to run.with_params('0.0.0').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) } - it { is_expected.to run.with_params('0.0.0.256').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) } - it { is_expected.to run.with_params('0.0.0.0.0').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) } - it { is_expected.to run.with_params('affe::beef').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) } - it { is_expected.to run.with_params('1.2.3.4', {}).and_raise_error(Puppet::ParseError, /is not a string/) } - it { is_expected.to run.with_params('1.2.3.4', 1).and_raise_error(Puppet::ParseError, /is not a string/) } - it { is_expected.to run.with_params('1.2.3.4', true).and_raise_error(Puppet::ParseError, /is not a string/) } - it { is_expected.to run.with_params('1.2.3.4', 'one').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) } + describe 'valid inputs' do + it { is_expected.to run.with_params('0.0.0.0') } + it { is_expected.to run.with_params('8.8.8.8') } + it { is_expected.to run.with_params('127.0.0.1') } + it { is_expected.to run.with_params('10.10.10.10') } + it { is_expected.to run.with_params('194.232.104.150') } + it { is_expected.to run.with_params('244.24.24.24') } + it { is_expected.to run.with_params('255.255.255.255') } + it { is_expected.to run.with_params('1.2.3.4', '5.6.7.8') } + context 'with netmasks' do + it { is_expected.to run.with_params('8.8.8.8/0') } + it { is_expected.to run.with_params('8.8.8.8/16') } + it { is_expected.to run.with_params('8.8.8.8/32') } + it { is_expected.to run.with_params('8.8.8.8/255.255.0.0') } end 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(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('one').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) } + it { is_expected.to run.with_params('0.0.0').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) } + it { is_expected.to run.with_params('0.0.0.256').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) } + it { is_expected.to run.with_params('0.0.0.0.0').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) } + it { is_expected.to run.with_params('affe::beef').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) } + it { is_expected.to run.with_params('1.2.3.4', {}).and_raise_error(Puppet::ParseError, /is not a string/) } + it { is_expected.to run.with_params('1.2.3.4', 1).and_raise_error(Puppet::ParseError, /is not a string/) } + it { is_expected.to run.with_params('1.2.3.4', true).and_raise_error(Puppet::ParseError, /is not a string/) } + it { is_expected.to run.with_params('1.2.3.4', 'one').and_raise_error(Puppet::ParseError, /is not a valid IPv4/) } + end end diff --git a/spec/functions/validate_ipv6_address_spec.rb b/spec/functions/validate_ipv6_address_spec.rb index 3afab56..87b535e 100755 --- a/spec/functions/validate_ipv6_address_spec.rb +++ b/spec/functions/validate_ipv6_address_spec.rb @@ -1,36 +1,50 @@ require 'spec_helper' describe 'validate_ipv6_address' 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('3ffe:0505:0002::') } - it { is_expected.to run.with_params('3ffe:0505:0002::', '3ffe:0505:0002::2') } - it { is_expected.to run.with_params('::1/64') } - it { is_expected.to run.with_params('fe80::a00:27ff:fe94:44d6/64') } - it 'should display a single deprecation' do - scope.expects(:warning).with(includes('This method is deprecated')) - is_expected.to run.with_params('3ffe:0505:0002::') - end + context 'Checking for deprecation warning', if: Puppet.version.to_f < 4.0 do + after(:context) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + # Checking for deprecation warning, which should only be provoked when the env variable for it is set. + it 'should display a single deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "true" + scope.expects(:warning).with(includes('This method is deprecated')) + is_expected.to run.with_params('3ffe:0505:0002::') + end + it 'should display no warning for deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "false" + scope.expects(:warning).with(includes('This method is deprecated')).never + is_expected.to run.with_params('3ffe:0505:0002::') end + end + + describe 'valid inputs' do + it { is_expected.to run.with_params('3ffe:0505:0002::') } + it { is_expected.to run.with_params('3ffe:0505:0002::', '3ffe:0505:0002::2') } + it { is_expected.to run.with_params('::1/64') } + it { is_expected.to run.with_params('fe80::a00:27ff:fe94:44d6/64') } + 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(true).and_raise_error(Puppet::ParseError, /is not a string/) } - it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) } - it { is_expected.to run.with_params('0.0.0').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) } - it { is_expected.to run.with_params('0.0.0.256').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) } - it { is_expected.to run.with_params('0.0.0.0.0').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) } - it { is_expected.to run.with_params('affe:beef').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) } - it { is_expected.to run.with_params('::1', {}).and_raise_error(Puppet::ParseError, /is not a string/) } - it { is_expected.to run.with_params('::1', true).and_raise_error(Puppet::ParseError, /is not a string/) } - it { is_expected.to run.with_params('::1', 'one').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) } - context 'unless running on ruby 1.8.7', :if => RUBY_VERSION != '1.8.7' do - it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /is not a string/) } - it { is_expected.to run.with_params('::1', 1).and_raise_error(Puppet::ParseError, /is not a string/) } - 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(true).and_raise_error(Puppet::ParseError, /is not a string/) } + it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) } + it { is_expected.to run.with_params('0.0.0').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) } + it { is_expected.to run.with_params('0.0.0.256').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) } + it { is_expected.to run.with_params('0.0.0.0.0').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) } + it { is_expected.to run.with_params('affe:beef').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) } + it { is_expected.to run.with_params('::1', {}).and_raise_error(Puppet::ParseError, /is not a string/) } + it { is_expected.to run.with_params('::1', true).and_raise_error(Puppet::ParseError, /is not a string/) } + it { is_expected.to run.with_params('::1', 'one').and_raise_error(Puppet::ParseError, /is not a valid IPv6/) } + context 'unless running on ruby 1.8.7', :if => RUBY_VERSION != '1.8.7' do + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /is not a string/) } + it { is_expected.to run.with_params('::1', 1).and_raise_error(Puppet::ParseError, /is not a string/) } end end end diff --git a/spec/functions/validate_legacy_spec.rb b/spec/functions/validate_legacy_spec.rb new file mode 100644 index 0000000..45089ef --- /dev/null +++ b/spec/functions/validate_legacy_spec.rb @@ -0,0 +1,68 @@ +require 'spec_helper' + +if Puppet.version.to_f >= 4.0 + describe 'validate_legacy' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(ArgumentError) } + + describe 'when passing the type assertion and passing the previous validation' do + before do + scope.expects(:function_validate_foo).with([5]).once + Puppet.expects(:notice).never + end + it 'passes without notice' do + is_expected.to run.with_params('Integer', 'validate_foo', 5) + end + end + + describe 'when passing the type assertion and failing the previous validation' do + before do + scope.expects(:function_validate_foo).with([5]).raises(Puppet::ParseError, 'foo').once + Puppet.expects(:notice).with(includes('Accepting previously invalid value for target type')) + end + it 'passes with a notice about newly accepted value' do + is_expected.to run.with_params('Integer', 'validate_foo', 5) + end + end + + describe 'when failing the type assertion and passing the previous validation' do + before do + scope.expects(:function_validate_foo).with(['5']).once + subject.func.expects(:call_function).with('deprecation', 'validate_legacy', includes('expected an Integer value')).once + end + it 'passes with a deprecation message' do + is_expected.to run.with_params('Integer', 'validate_foo', '5') + end + end + + describe 'when failing the type assertion and failing the previous validation' do + before do + scope.expects(:function_validate_foo).with(['5']).raises(Puppet::ParseError, 'foo').once + subject.func.expects(:call_function).with('fail', includes('expected an Integer value')).once + end + it 'fails with a helpful message' do + is_expected.to run.with_params('Integer', 'validate_foo', '5') + end + end + + describe 'when passing in undef' do + before do + scope.expects(:function_validate_foo).with([:undef]).once + Puppet.expects(:notice).never + end + it 'works' do + is_expected.to run.with_params('Optional[Integer]', 'validate_foo', :undef) + end + end + + describe 'when passing in multiple arguments' do + before do + scope.expects(:function_validate_foo).with([:undef, 1, 'foo']).once + Puppet.expects(:notice).never + end + it 'passes with a deprecation message' do + is_expected.to run.with_params('Optional[Integer]', 'validate_foo', :undef, 1, 'foo') + end + end + end +end diff --git a/spec/functions/validate_numeric_spec.rb b/spec/functions/validate_numeric_spec.rb index 2869e5f..4a65649 100755 --- a/spec/functions/validate_numeric_spec.rb +++ b/spec/functions/validate_numeric_spec.rb @@ -1,8 +1,13 @@ require 'spec_helper' describe 'validate_numeric' do + after(:context) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + # Checking for deprecation warning it 'should display a single deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "true" scope.expects(:warning).with(includes('This method is deprecated')) is_expected.to run.with_params(3) end diff --git a/spec/functions/validate_re_spec.rb b/spec/functions/validate_re_spec.rb index 4b78a2e..2fa9ddd 100755 --- a/spec/functions/validate_re_spec.rb +++ b/spec/functions/validate_re_spec.rb @@ -1,8 +1,13 @@ require 'spec_helper' describe 'validate_re' do + after(:context) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + # Checking for deprecation warning it 'should display a single deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "true" scope.expects(:warning).with(includes('This method is deprecated')) is_expected.to run.with_params('', '') end @@ -23,22 +28,6 @@ describe 'validate_re' do end describe 'invalid inputs' do - it { - pending('should implement stricter type checking') - is_expected.to run.with_params([], '').and_raise_error(Puppet::ParseError, /is not a String/) - } - it { - pending('should implement stricter type checking') - is_expected.to run.with_params('', {}).and_raise_error(Puppet::ParseError, /is not an Array/) - } - it { - pending('should implement stricter type checking') - is_expected.to run.with_params('', '', []).and_raise_error(Puppet::ParseError, /is not a String/) - } - it { - pending('should implement stricter type checking') - is_expected.to run.with_params(nil, nil).and_raise_error(Puppet::ParseError, /is not a String/) - } it { is_expected.to run.with_params('', []).and_raise_error(Puppet::ParseError, /does not match/) } it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, /does not match/) } it { is_expected.to run.with_params('', 'two').and_raise_error(Puppet::ParseError, /does not match/) } diff --git a/spec/functions/validate_string_spec.rb b/spec/functions/validate_string_spec.rb index ef7a1b4..45d6ffc 100755 --- a/spec/functions/validate_string_spec.rb +++ b/spec/functions/validate_string_spec.rb @@ -1,8 +1,13 @@ require 'spec_helper' describe 'validate_string' do + after(:context) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + # Checking for deprecation warning it 'should display a single deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = "true" scope.expects(:warning).with(includes('This method is deprecated')) is_expected.to run.with_params('', '') end |