diff options
author | Mike Gabriel <mike.gabriel@das-netzwerkteam.de> | 2021-12-20 12:39:15 +0100 |
---|---|---|
committer | Mike Gabriel <mike.gabriel@das-netzwerkteam.de> | 2021-12-20 12:39:15 +0100 |
commit | f56d2d7888be1a27d7ef05d850afdc4e9dbb464a (patch) | |
tree | e193c75cbdb40c5290906d97b3ac8966a1099a47 /code/environments/production/modules/stdlib/spec | |
download | puppet.SGM-f56d2d7888be1a27d7ef05d850afdc4e9dbb464a.tar.gz puppet.SGM-f56d2d7888be1a27d7ef05d850afdc4e9dbb464a.tar.bz2 puppet.SGM-f56d2d7888be1a27d7ef05d850afdc4e9dbb464a.zip |
initial draft of a minimal puppet config template
Diffstat (limited to 'code/environments/production/modules/stdlib/spec')
318 files changed, 13058 insertions, 0 deletions
diff --git a/code/environments/production/modules/stdlib/spec/acceptance/abs_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/abs_spec.rb new file mode 100644 index 0000000..78d790f --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/abs_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper_acceptance' + +describe 'abs function' do + describe 'success' do + pp1 = <<-DOC + $input = '-34.56' + $output = abs($input) + notify { "$output": } + DOC + it 'accepts a string' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: 34.56}) + end + end + + pp2 = <<-DOC + $input = -35.46 + $output = abs($input) + notify { "$output": } + DOC + it 'accepts a float' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: 35.46}) + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/anchor_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/anchor_spec.rb new file mode 100644 index 0000000..3cf0c92 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/anchor_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper_acceptance' + +describe 'anchor type' do + describe 'success' do + pp = <<-DOC + class anchored { + anchor { 'anchored::begin': } + ~> anchor { 'anchored::end': } + } + + class anchorrefresh { + notify { 'first': } + ~> class { 'anchored': } + ~> anchor { 'final': } + } + + include anchorrefresh + DOC + it 'effects proper chaining of resources' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Anchor\[final\]: Triggered 'refresh'}) + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/any2array_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/any2array_spec.rb new file mode 100644 index 0000000..2f1ae4e --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/any2array_spec.rb @@ -0,0 +1,45 @@ +require 'spec_helper_acceptance' + +describe 'any2array function' do + describe 'success' do + pp1 = <<-DOC + $input = '' + $output = any2array($input) + validate_array($output) + notify { "Output: ${output}": } + DOC + it 'creates an empty array' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: Output: }) + end + end + + pp2 = <<-DOC + $input = ['array', 'test'] + $output = any2array($input) + validate_array($output) + notify { "Output: ${output}": } + DOC + it 'leaves arrays modified' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: Output: (\[|)array(,\s|)test(\]|)}) + end + end + + pp3 = <<-DOC + $input = {'test' => 'array'} + $output = any2array($input) + + validate_array($output) + # Check each element of the array is a plain string. + validate_string($output[0]) + validate_string($output[1]) + notify { "Output: ${output}": } + DOC + it 'turns a hash into an array' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: Output: (\[|)test(,\s|)array(\]|)}) + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/base64_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/base64_spec.rb new file mode 100644 index 0000000..5cc4d62 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/base64_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper_acceptance' + +describe 'base64 function' do + describe 'success' do + pp = <<-DOC + $encodestring = base64('encode', 'thestring') + $decodestring = base64('decode', $encodestring) + notify { $decodestring: } + DOC + it 'encodes then decode a string' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{thestring}) + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/bool2num_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/bool2num_spec.rb new file mode 100644 index 0000000..2a62435 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/bool2num_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper_acceptance' + +describe 'bool2num function' do + describe 'success' do + %w[false f 0 n no].each do |bool| + pp1 = <<-DOC + $input = "#{bool}" + $output = bool2num($input) + notify { "$output": } + DOC + it "should convert a given boolean, #{bool}, to 0" do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: 0}) + end + end + end + + %w[true t 1 y yes].each do |bool| + pp2 = <<-DOC + $input = "#{bool}" + $output = bool2num($input) + notify { "$output": } + DOC + it "should convert a given boolean, #{bool}, to 1" do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: 1}) + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/build_csv.rb b/code/environments/production/modules/stdlib/spec/acceptance/build_csv.rb new file mode 100644 index 0000000..9059858 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/build_csv.rb @@ -0,0 +1,89 @@ +# vim: set sw=2 sts=2 et tw=80 : +require 'rspec' + +# XXX Super ugly hack to keep from starting beaker nodes +module Kernel + # make an alias of the original require + alias original_require require + # rewrite require + def require(name) + original_require name if name != 'spec_helper_acceptance' + end +end +UNSUPPORTED_PLATFORMS = [].freeze +def fact(*_args) + [] +end +# XXX End hax + +# Get a list of functions for test coverage +function_list = Dir[File.join(File.dirname(__FILE__), '..', '..', 'lib', 'puppet', 'parser', 'functions', '*.rb')].map do |function_rb| + File.basename(function_rb, '.rb') +end + +## Configure rspec to parse tests +options = RSpec::Core::ConfigurationOptions.new(['spec/acceptance']) +configuration = RSpec.configuration +world = RSpec.world +options.parse_options +options.configure(configuration) +configuration.load_spec_files + +## Collect up tests and example groups into a hash +def get_tests(children) + children.each_with_object({}) do |c, memo| + memo[c.description] = {} + memo[c.description]['groups'] = get_tests(c.children) unless c.children.empty? + unless c.examples.empty? + memo[c.description]['tests'] = c.examples.map { |e| + e.description unless e.pending? + }.compact + end + next if c.examples.empty? + memo[c.description]['pending_tests'] = c.examples.map { |e| + e.description if e.pending? + }.compact + end +end + +def count_test_types_in(type, group) + return 0 if group.nil? + group.reduce(0) do |m, (k, v)| + m += v.length if k == type + m += count_tests_in(v) if v.is_a?(Hash) + m + end +end + +def count_tests_in(group) + count_test_types_in('tests', group) +end + +def count_pending_tests_in(group) + count_test_types_in('pending_tests', group) +end + +# Convert tests hash to csv format +def to_csv(function_list, tests) + function_list.map { |function_name| + v = tests["#{function_name} function"] + if v + positive_tests = count_tests_in(v['groups']['success']) + negative_tests = count_tests_in(v['groups']['failure']) + pending_tests = + count_pending_tests_in(v['groups']['failure']) + + count_pending_tests_in(v['groups']['failure']) + else + positive_tests = 0 + negative_tests = 0 + pending_tests = 0 + end + '%-25s, %-9d, %-9d, %-9d' % [function_name, positive_tests, negative_tests, pending_tests] + }.compact +end + +tests = get_tests(world.example_groups) +csv = to_csv(function_list, tests) +percentage_tested = "#{tests.count * 100 / function_list.count}%" +printf("%-25s, %-9s, %-9s, %-9s\n", "#{percentage_tested} have tests.", 'Positive', 'Negative', 'Pending') +puts csv diff --git a/code/environments/production/modules/stdlib/spec/acceptance/capitalize_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/capitalize_spec.rb new file mode 100644 index 0000000..ce75645 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/capitalize_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper_acceptance' + +describe 'capitalize function' do + describe 'success' do + pp1 = <<-DOC + $input = 'this is a string' + $output = capitalize($input) + notify { $output: } + DOC + it 'capitalizes the first letter of a string' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: This is a string}) + end + end + + pp2 = <<-DOC + $input = ['this', 'is', 'a', 'string'] + $output = capitalize($input) + notify { $output: } + DOC + regex_array = [%r{Notice: This}, %r{Notice: Is}, %r{Notice: A}, %r{Notice: String}] + it 'capitalizes the first letter of an array of strings' do + apply_manifest(pp2, :catch_failures => true) do |r| + regex_array.each do |i| + expect(r.stdout).to match(i) + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/ceiling_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/ceiling_spec.rb new file mode 100644 index 0000000..3f686d2 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/ceiling_spec.rb @@ -0,0 +1,37 @@ +require 'spec_helper_acceptance' + +describe 'ceiling function' do + describe 'success' do + pp1 = <<-DOC + $a = 12.8 + $b = 13 + $o = ceiling($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'ceilings floats' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp2 = <<-DOC + $a = 7 + $b = 7 + $o = ceiling($a) + if $o == $b { + notify { 'output is correct': } + } + DOC + it 'ceilings integers' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output is correct}) + end + end + end + describe 'failure' do + it 'handles improper argument counts' + it 'handles non-numbers' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/chomp_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/chomp_spec.rb new file mode 100644 index 0000000..ba5e7cb --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/chomp_spec.rb @@ -0,0 +1,19 @@ +require 'spec_helper_acceptance' + +describe 'chomp function' do + describe 'success' do + pp = <<-DOC + $input = "test\n" + if size($input) != 5 { + fail("Size of ${input} is not 5.") + } + $output = chomp($input) + if size($output) != 4 { + fail("Size of ${input} is not 4.") + } + DOC + it 'eats the newline' do + apply_manifest(pp, :catch_failures => true) + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/chop_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/chop_spec.rb new file mode 100644 index 0000000..6489c28 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/chop_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper_acceptance' + +describe 'chop function' do + describe 'success' do + pp1 = <<-DOC + $input = "test" + if size($input) != 4 { + fail("Size of ${input} is not 4.") + } + $output = chop($input) + if size($output) != 3 { + fail("Size of ${input} is not 3.") + } + DOC + it 'eats the last character' do + apply_manifest(pp1, :catch_failures => true) + end + + pp2 = <<-'DOC' + $input = "test\r\n" + if size($input) != 6 { + fail("Size of ${input} is not 6.") + } + $output = chop($input) + if size($output) != 4 { + fail("Size of ${input} is not 4.") + } + DOC + it 'eats the last two characters of \r\n' do + apply_manifest(pp2, :catch_failures => true) + end + + pp3 = <<-DOC + $input = "" + $output = chop($input) + DOC + it 'does not fail on empty strings' do + apply_manifest(pp3, :catch_failures => true) + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/clamp_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/clamp_spec.rb new file mode 100644 index 0000000..9885f56 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/clamp_spec.rb @@ -0,0 +1,38 @@ +require 'spec_helper_acceptance' + +describe 'clamp function' do + describe 'success' do + pp1 = <<-DOC + $x = 17 + $y = 225 + $z = 155 + $o = clamp($x, $y, $z) + if $o == $z { + notify { 'output correct': } + } + DOC + it 'clamps list of values' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp2 = <<-DOC + $a = [7, 19, 66] + $b = 19 + $o = clamp($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'clamps array of values' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end + describe 'failure' do + it 'handles improper argument counts' + it 'handles no arguments' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/concat_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/concat_spec.rb new file mode 100644 index 0000000..391b848 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/concat_spec.rb @@ -0,0 +1,52 @@ +require 'spec_helper_acceptance' + +describe 'concat function' do + describe 'success' do + pp1 = <<-DOC + $output = concat(['1','2','3'],['4','5','6']) + validate_array($output) + if size($output) != 6 { + fail("${output} should have 6 elements.") + } + DOC + it 'concats one array to another' do + apply_manifest(pp1, :catch_failures => true) + end + + pp2 = <<-DOC + $output = concat(['1','2','3'],'4','5','6',['7','8','9']) + validate_array($output) + if size($output) != 9 { + fail("${output} should have 9 elements.") + } + DOC + it 'concats arrays and primitives to array' do + apply_manifest(pp2, :catch_failures => true) + end + + pp3 = <<-DOC + $output = concat(['1','2','3'],['4','5','6'],['7','8','9']) + validate_array($output) + if size($output) != 9 { + fail("${output} should have 9 elements.") + } + DOC + it 'concats multiple arrays to one' do + apply_manifest(pp3, :catch_failures => true) + end + + pp4 = <<-DOC + $output = concat([{"a" => "b"}], {"c" => "d", "e" => "f"}) + validate_array($output) + if size($output) != 2 { + fail("${output} should have 2 elements.") + } + if $output[1] != {"c" => "d", "e" => "f"} { + fail("${output} does not have the expected hash for the second element.") + } + DOC + it 'concats hash arguments' do + apply_manifest(pp4, :catch_failures => true) + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/count_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/count_spec.rb new file mode 100644 index 0000000..c134836 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/count_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper_acceptance' + +describe 'count function' do + describe 'success' do + pp1 = <<-DOC + $input = [1,2,3,4] + $output = count($input) + notify { "$output": } + DOC + it 'counts elements in an array' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: 4}) + end + end + + pp2 = <<-DOC + $input = [1,1,1,2] + $output = count($input, 1) + notify { "$output": } + DOC + it 'counts elements in an array that match a second argument' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: 3}) + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/deep_merge_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/deep_merge_spec.rb new file mode 100644 index 0000000..29a2c79 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/deep_merge_spec.rb @@ -0,0 +1,18 @@ +require 'spec_helper_acceptance' + +describe 'deep_merge function' do + describe 'success' do + pp = <<-DOC + $hash1 = {'one' => 1, 'two' => 2, 'three' => { 'four' => 4 } } + $hash2 = {'two' => 'dos', 'three' => { 'five' => 5 } } + $merged_hash = deep_merge($hash1, $hash2) + + if $merged_hash != { 'one' => 1, 'two' => 'dos', 'three' => { 'four' => 4, 'five' => 5 } } { + fail("Hash was incorrectly merged.") + } + DOC + it 'deeps merge two hashes' do + apply_manifest(pp, :catch_failures => true) + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/defined_with_params_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/defined_with_params_spec.rb new file mode 100644 index 0000000..b12927a --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/defined_with_params_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper_acceptance' + +describe 'defined_with_params function' do + describe 'success' do + pp = <<-DOC + user { 'dan': + ensure => present, + } + + if defined_with_params(User[dan], {'ensure' => 'present' }) { + notify { 'User defined with ensure=>present': } + } + DOC + it 'successfullies notify' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: User defined with ensure=>present}) + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/delete_at_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/delete_at_spec.rb new file mode 100644 index 0000000..6a34e21 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/delete_at_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper_acceptance' + +describe 'delete_at function' do + describe 'success' do + pp = <<-DOC + $output = delete_at(['a','b','c','b'], 1) + if $output == ['a','c','b'] { + notify { 'output correct': } + } + DOC + it 'deletes elements of the array' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/delete_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/delete_spec.rb new file mode 100644 index 0000000..70877cb --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/delete_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper_acceptance' + +describe 'delete function' do + pp = <<-DOC + $output = delete(['a','b','c','b'], 'b') + if $output == ['a','c'] { + notify { 'output correct': } + } + DOC + describe 'success' do + it 'deletes elements of the array' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/delete_undef_values_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/delete_undef_values_spec.rb new file mode 100644 index 0000000..418c959 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/delete_undef_values_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper_acceptance' + +describe 'delete_undef_values function' do + describe 'success' do + pp = <<-DOC + $output = delete_undef_values({a=>'A', b=>'', c=>undef, d => false}) + if $output == { a => 'A', b => '', d => false } { + notify { 'output correct': } + } + DOC + it 'deletes elements of the array' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/delete_values_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/delete_values_spec.rb new file mode 100644 index 0000000..634d319 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/delete_values_spec.rb @@ -0,0 +1,23 @@ +require 'spec_helper_acceptance' + +describe 'delete_values function' do + describe 'success' do + pp = <<-DOC + $a = { 'a' => 'A', 'b' => 'B', 'B' => 'C', 'd' => 'B' } + $b = { 'a' => 'A', 'B' => 'C' } + $o = delete_values($a, 'B') + if $o == $b { + notify { 'output correct': } + } + DOC + it 'deletes elements of the hash' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end + describe 'failure' do + it 'handles non-hash arguments' + it 'handles improper argument counts' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/deprecation_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/deprecation_spec.rb new file mode 100644 index 0000000..9f25449 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/deprecation_spec.rb @@ -0,0 +1,92 @@ +require 'spec_helper_acceptance' + +describe 'deprecation function' do + test_file = if fact('operatingsystem') == 'windows' + 'C:/deprecation' + else + '/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 => return_puppet_version =~ %r{^4} do + let(:result) { on(default, puppet('apply', '--strict=error', '-e', add_file_manifest), :acceptable_exit_codes => (0...256)) } + + after :all do + apply_manifest(remove_file_manifest) + end + + it 'returns an error' do + expect(result.exit_code).to eq(1) + end + + it 'shows the error message' do + expect(result.stderr).to match(%r{deprecation. key. message}) + end + + describe file(test_file.to_s) do + it { is_expected.not_to be_file } + end + end + + context 'with --strict=warning', :if => return_puppet_version =~ %r{^4} do + let(:result) { on(default, puppet('apply', '--strict=warning', '-e', add_file_manifest), :acceptable_exit_codes => (0...256)) } + + after :all do + apply_manifest(remove_file_manifest) + end + + it 'does not return an error' do + expect(result.exit_code).to eq(0) + end + + it 'shows the error message' do + expect(result.stderr).to match(%r{Warning: message}) + end + + describe file(test_file.to_s) do + it { is_expected.to be_file } + end + end + + context 'with --strict=off', :if => return_puppet_version =~ %r{^4} do + let(:result) { on(default, puppet('apply', '--strict=off', '-e', add_file_manifest), :acceptable_exit_codes => (0...256)) } + + after :all do + apply_manifest(remove_file_manifest) + end + + it 'does not return an error' do + expect(result.exit_code).to eq(0) + end + + it 'does not show the error message' do + expect(result.stderr).not_to match(%r{Warning: message}) + end + + describe file(test_file.to_s) do + it { is_expected.to be_file } + end + end + + context 'puppet 3 test', :if => return_puppet_version =~ %r{^3} do + let(:result) { on(default, puppet('apply', '--parser=future', '-e', add_file_manifest), :acceptable_exit_codes => (0...256)) } + + after :all do + apply_manifest(remove_file_manifest) + end + + it 'returns a deprecation error' do + expect(result.stderr).to match(%r{Warning: message}) + end + it 'passes without error' do + expect(result.exit_code).to eq(0) + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/difference_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/difference_spec.rb new file mode 100644 index 0000000..7988f69 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/difference_spec.rb @@ -0,0 +1,24 @@ +require 'spec_helper_acceptance' + +describe 'difference function' do + describe 'success' do + pp = <<-DOC + $a = ['a','b','c'] + $b = ['b','c','d'] + $c = ['a'] + $o = difference($a, $b) + if $o == $c { + notify { 'output correct': } + } + DOC + it 'returns non-duplicates in the first array' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end + describe 'failure' do + it 'handles non-array arguments' + it 'handles improper argument counts' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/dirname_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/dirname_spec.rb new file mode 100644 index 0000000..a532e11 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/dirname_spec.rb @@ -0,0 +1,39 @@ +require 'spec_helper_acceptance' + +describe 'dirname function' do + describe 'success' do + context 'with absolute path' do + pp1 = <<-DOC + $a = '/path/to/a/file.txt' + $b = '/path/to/a' + $o = dirname($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'returns the dirname' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end + context 'with relative path' do + pp2 = <<-DOC + $a = 'path/to/a/file.txt' + $b = 'path/to/a' + $o = dirname($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'returns the dirname' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end + end + describe 'failure' do + it 'handles improper argument counts' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/downcase_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/downcase_spec.rb new file mode 100644 index 0000000..2ff55f4 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/downcase_spec.rb @@ -0,0 +1,37 @@ +require 'spec_helper_acceptance' + +describe 'downcase function' do + describe 'success' do + pp1 = <<-DOC + $a = 'AOEU' + $b = 'aoeu' + $o = downcase($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'returns the downcase' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp2 = <<-DOC + $a = 'aoeu aoeu' + $b = 'aoeu aoeu' + $o = downcase($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'doesn\'t affect lowercase words' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end + describe 'failure' do + it 'handles improper argument counts' + it 'handles non-strings' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/empty_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/empty_spec.rb new file mode 100644 index 0000000..c5c63c0 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/empty_spec.rb @@ -0,0 +1,51 @@ +require 'spec_helper_acceptance' + +describe 'empty function', :if => Puppet::Util::Package.versioncmp(Puppet.version, '5.5.0') < 0 do + describe 'success' do + pp1 = <<-DOC + $a = '' + $b = true + $o = empty($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'recognizes empty strings' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp2 = <<-DOC + $a = 'aoeu' + $b = false + $o = empty($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'recognizes non-empty strings' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp3 = <<-DOC + $a = 7 + $b = false + $o = empty($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'handles numerical values' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end + describe 'failure' do + it 'handles improper argument counts' + it 'handles non-strings' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/ensure_resource_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/ensure_resource_spec.rb new file mode 100644 index 0000000..21e73d3 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/ensure_resource_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper_acceptance' + +describe 'ensure_resource function' do + describe 'success' do + pp1 = <<-DOC + notify { "test": loglevel => 'err' } + ensure_resource('notify', 'test', { 'loglevel' => 'err' }) + DOC + it 'ensures a resource already declared' do + apply_manifest('') + + apply_manifest(pp1, :expect_changes => true) + end + + pp2 = <<-DOC + ensure_resource('notify', 'test', { 'loglevel' => 'err' }) + DOC + it 'ensures a undeclared resource' do + apply_manifest('') + + apply_manifest(pp2, :expect_changes => true) + end + it 'takes defaults arguments' + end + describe 'failure' do + it 'handles no arguments' + it 'handles non strings' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/flatten_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/flatten_spec.rb new file mode 100644 index 0000000..79d4854 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/flatten_spec.rb @@ -0,0 +1,37 @@ +require 'spec_helper_acceptance' + +describe 'flatten function', :if => Puppet::Util::Package.versioncmp(Puppet.version, '5.5.0') < 0 do + describe 'success' do + pp1 = <<-DOC + $a = ["a","b",["c",["d","e"],"f","g"]] + $b = ["a","b","c","d","e","f","g"] + $o = flatten($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'flattens arrays' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp2 = <<-DOC + $a = ["a","b","c","d","e","f","g"] + $b = ["a","b","c","d","e","f","g"] + $o = flatten($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'does not affect flat arrays' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end + describe 'failure' do + it 'handles improper argument counts' + it 'handles non-strings' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/floor_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/floor_spec.rb new file mode 100644 index 0000000..312eec3 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/floor_spec.rb @@ -0,0 +1,37 @@ +require 'spec_helper_acceptance' + +describe 'floor function' do + describe 'success' do + pp1 = <<-DOC + $a = 12.8 + $b = 12 + $o = floor($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'floors floats' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp2 = <<-DOC + $a = 7 + $b = 7 + $o = floor($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'floors integers' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end + describe 'failure' do + it 'handles improper argument counts' + it 'handles non-numbers' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/fqdn_rand_string_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/fqdn_rand_string_spec.rb new file mode 100644 index 0000000..5916553 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/fqdn_rand_string_spec.rb @@ -0,0 +1,64 @@ +require 'spec_helper_acceptance' + +describe 'fqdn_rand_string function' do + describe 'success' do + include_context 'with faked facts' + context "when the FQDN is 'fakehost.localdomain'" do + before :each do + fake_fact('fqdn', 'fakehost.localdomain') + end + + pp1 = <<-PUPPETCODE + $l = 10 + $o = fqdn_rand_string($l) + notice(inline_template('fqdn_rand_string is <%= @o.inspect %>')) + PUPPETCODE + it 'generates random alphanumeric strings' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{fqdn_rand_string is "(7oDp0KOr1b|9Acvnhkt4J)"}) + end + end + + pp2 = <<-PUPPETCODE + $l = 10 + $c = '0123456789' + $o = fqdn_rand_string($l, $c) + notice(inline_template('fqdn_rand_string is <%= @o.inspect %>')) + PUPPETCODE + it 'generates random alphanumeric strings with custom charsets' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{fqdn_rand_string is "(7203048515|2383756694)"}) + end + end + + pp3 = <<-PUPPETCODE + $l = 10 + $s = 'seed' + $o = fqdn_rand_string($l, undef, $s) + notice(inline_template('fqdn_rand_string is <%= @o.inspect %>')) + PUPPETCODE + it 'generates random alphanumeric strings with custom seeds' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{fqdn_rand_string is "(3HS4mbuI3E|1jJtAMs94d)"}) + end + end + + pp4 = <<-PUPPETCODE + $l = 10 + $c = '0123456789' + $s = 'seed' + $o = fqdn_rand_string($l, $c, $s) + notice(inline_template('fqdn_rand_string is <%= @o.inspect %>')) + PUPPETCODE + it 'generates random alphanumeric strings with custom charsets and seeds' do + apply_manifest(pp4, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{fqdn_rand_string is "(3104058232|7100592312)"}) + end + end + end + end + describe 'failure' do + it 'handles improper argument counts' + it 'handles non-numbers for length argument' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/fqdn_rotate_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/fqdn_rotate_spec.rb new file mode 100644 index 0000000..99f315e --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/fqdn_rotate_spec.rb @@ -0,0 +1,62 @@ +require 'spec_helper_acceptance' + +describe 'fqdn_rotate function' do + describe 'success' do + include_context 'with faked facts' + context "when the FQDN is 'fakehost.localdomain'" do + before :each do + fake_fact('fqdn', 'fakehost.localdomain') + end + + pp1 = <<-DOC + $a = ['a','b','c','d'] + $o = fqdn_rotate($a) + notice(inline_template('fqdn_rotate is <%= @o.inspect %>')) + DOC + it 'rotates arrays' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{fqdn_rotate is \["d", "a", "b", "c"\]}) + end + end + + pp2 = <<-DOC + $a = ['a','b','c','d'] + $s = 'seed' + $o = fqdn_rotate($a, $s) + notice(inline_template('fqdn_rotate is <%= @o.inspect %>')) + DOC + it 'rotates arrays with custom seeds' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{fqdn_rotate is \["c", "d", "a", "b"\]}) + end + end + + pp3 = <<-DOC + $a = 'abcd' + $o = fqdn_rotate($a) + notice(inline_template('fqdn_rotate is <%= @o.inspect %>')) + DOC + it 'rotates strings' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{fqdn_rotate is "dabc"}) + end + end + + pp4 = <<-DOC + $a = 'abcd' + $s = 'seed' + $o = fqdn_rotate($a, $s) + notice(inline_template('fqdn_rotate is <%= @o.inspect %>')) + DOC + it 'rotates strings with custom seeds' do + apply_manifest(pp4, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{fqdn_rotate is "cdab"}) + end + end + end + end + describe 'failure' do + it 'handles improper argument counts' + it 'handles invalid arguments' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/get_module_path_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/get_module_path_spec.rb new file mode 100644 index 0000000..f9b169c --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/get_module_path_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper_acceptance' + +describe 'get_module_path function' do + describe 'success' do + pp = <<-DOC + $a = $::is_pe ? { + 'true' => '/etc/puppetlabs/puppet/modules/dne', + 'false' => '/etc/puppet/modules/dne', + } + $o = get_module_path('dne') + if $o == $a { + notify { 'output correct': } + } else { + notify { "failed; module path is '$o'": } + } + DOC + it 'get_module_paths dne' do + apply_manifest(pp, :expect_failures => true) + end + end + describe 'failure' do + it 'handles improper argument counts' + it 'handles non-numbers' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/getparam_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/getparam_spec.rb new file mode 100644 index 0000000..6ed2257 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/getparam_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper_acceptance' + +describe 'getparam function' do + describe 'success' do + pp = <<-DOC + notify { 'rspec': + message => 'custom rspec message', + } + $o = getparam(Notify['rspec'], 'message') + notice(inline_template('getparam is <%= @o.inspect %>')) + DOC + it 'getparam a notify' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{getparam is "custom rspec message"}) + end + end + end + describe 'failure' do + it 'handles no arguments' + it 'handles non strings' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/getvar_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/getvar_spec.rb new file mode 100644 index 0000000..0b14afb --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/getvar_spec.rb @@ -0,0 +1,24 @@ +require 'spec_helper_acceptance' + +describe 'getvar function' do + describe 'success' do + pp = <<-DOC + class a::data { $foo = 'aoeu' } + include a::data + $b = 'aoeu' + $o = getvar("a::data::foo") + if $o == $b { + notify { 'output correct': } + } + DOC + it 'getvars from classes' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end + describe 'failure' do + it 'handles improper argument counts' + it 'handles non-numbers' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/grep_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/grep_spec.rb new file mode 100644 index 0000000..1fe2027 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/grep_spec.rb @@ -0,0 +1,24 @@ +require 'spec_helper_acceptance' + +describe 'grep function' do + describe 'success' do + pp = <<-DOC + $a = ['aaabbb','bbbccc','dddeee'] + $b = 'bbb' + $c = ['aaabbb','bbbccc'] + $o = grep($a,$b) + if $o == $c { + notify { 'output correct': } + } + DOC + it 'greps arrays' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end + describe 'failure' do + it 'handles improper argument counts' + it 'handles non-arrays' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/has_interface_with_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/has_interface_with_spec.rb new file mode 100644 index 0000000..d16dc1d --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/has_interface_with_spec.rb @@ -0,0 +1,52 @@ +require 'spec_helper_acceptance' + +describe 'has_interface_with function', :unless => ((fact('osfamily') == 'windows') || (fact('osfamily') == 'AIX')) do + describe 'success' do + pp1 = <<-DOC + $a = $::ipaddress + $o = has_interface_with('ipaddress', $a) + notice(inline_template('has_interface_with is <%= @o.inspect %>')) + DOC + it 'has_interface_with existing ipaddress' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{has_interface_with is true}) + end + end + + pp2 = <<-DOC + $a = '128.0.0.1' + $o = has_interface_with('ipaddress', $a) + notice(inline_template('has_interface_with is <%= @o.inspect %>')) + DOC + it 'has_interface_with absent ipaddress' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{has_interface_with is false}) + end + end + + pp3 = <<-DOC + if $osfamily == 'Solaris' or $osfamily == 'Darwin' { + $a = 'lo0' + }elsif $osfamily == 'windows' { + $a = $::kernelmajversion ? { + /6\.(2|3|4)/ => 'Ethernet0', + /6\.(0|1)/ => 'Local_Area_Connection', + /5\.(1|2)/ => undef, #Broken current in facter + } + }else { + $a = 'lo' + } + $o = has_interface_with($a) + notice(inline_template('has_interface_with is <%= @o.inspect %>')) + DOC + it 'has_interface_with existing interface' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{has_interface_with is true}) + end + end + end + describe 'failure' do + it 'handles no arguments' + it 'handles non strings' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/has_ip_address_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/has_ip_address_spec.rb new file mode 100644 index 0000000..13cdd77 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/has_ip_address_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper_acceptance' + +describe 'has_ip_address function', :unless => ((fact('osfamily') == 'windows') || (fact('osfamily') == 'AIX')) do + describe 'success' do + pp1 = <<-DOC + $a = '127.0.0.1' + $o = has_ip_address($a) + notice(inline_template('has_ip_address is <%= @o.inspect %>')) + DOC + it 'has_ip_address existing ipaddress' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{has_ip_address is true}) + end + end + + pp2 = <<-DOC + $a = '128.0.0.1' + $o = has_ip_address($a) + notice(inline_template('has_ip_address is <%= @o.inspect %>')) + DOC + it 'has_ip_address absent ipaddress' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{has_ip_address is false}) + end + end + end + describe 'failure' do + it 'handles no arguments' + it 'handles non strings' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/has_ip_network_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/has_ip_network_spec.rb new file mode 100644 index 0000000..e18f050 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/has_ip_network_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper_acceptance' + +describe 'has_ip_network function', :unless => ((fact('osfamily') == 'windows') || (fact('osfamily') == 'AIX')) do + describe 'success' do + pp1 = <<-DOC + $a = '127.0.0.0' + $o = has_ip_network($a) + notice(inline_template('has_ip_network is <%= @o.inspect %>')) + DOC + it 'has_ip_network existing ipaddress' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{has_ip_network is true}) + end + end + + pp2 = <<-DOC + $a = '128.0.0.0' + $o = has_ip_network($a) + notice(inline_template('has_ip_network is <%= @o.inspect %>')) + DOC + it 'has_ip_network absent ipaddress' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{has_ip_network is false}) + end + end + end + describe 'failure' do + it 'handles no arguments' + it 'handles non strings' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/has_key_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/has_key_spec.rb new file mode 100644 index 0000000..9da69c2 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/has_key_spec.rb @@ -0,0 +1,39 @@ +require 'spec_helper_acceptance' + +describe 'has_key function' do + describe 'success' do + pp1 = <<-DOC + $a = { 'aaa' => 'bbb','bbb' => 'ccc','ddd' => 'eee' } + $b = 'bbb' + $c = true + $o = has_key($a,$b) + if $o == $c { + notify { 'output correct': } + } + DOC + it 'has_keys in hashes' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp2 = <<-DOC + $a = { 'aaa' => 'bbb','bbb' => 'ccc','ddd' => 'eee' } + $b = 'ccc' + $c = false + $o = has_key($a,$b) + if $o == $c { + notify { 'output correct': } + } + DOC + it 'has_keys not in hashes' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end + describe 'failure' do + it 'handles improper argument counts' + it 'handles non-hashes' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/hash_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/hash_spec.rb new file mode 100644 index 0000000..82e9245 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/hash_spec.rb @@ -0,0 +1,24 @@ +require 'spec_helper_acceptance' + +describe 'hash function' do + describe 'success' do + pp = <<-DOC + $a = ['aaa','bbb','bbb','ccc','ddd','eee'] + $b = { 'aaa' => 'bbb', 'bbb' => 'ccc', 'ddd' => 'eee' } + $o = hash($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'hashs arrays' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + it 'handles odd-length arrays' + end + describe 'failure' do + it 'handles improper argument counts' + it 'handles non-arrays' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/intersection_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/intersection_spec.rb new file mode 100644 index 0000000..75dfe87 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/intersection_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper_acceptance' + +describe 'intersection function' do + describe 'success' do + pp = <<-DOC + $a = ['aaa','bbb','ccc'] + $b = ['bbb','ccc','ddd','eee'] + $c = ['bbb','ccc'] + $o = intersection($a,$b) + if $o == $c { + notify { 'output correct': } + } + DOC + it 'intersections arrays' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + it 'intersections empty arrays' + end + describe 'failure' do + it 'handles improper argument counts' + it 'handles non-arrays' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/is_a_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/is_a_spec.rb new file mode 100644 index 0000000..449e3e7 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/is_a_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper_acceptance' + +if return_puppet_version =~ %r{^4} + describe 'is_a function' do + pp1 = <<-DOC + if 'hello world'.is_a(String) { + notify { 'output correct': } + } + DOC + it 'matches a string' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp2 = <<-DOC + if 5.is_a(String) { + notify { 'output wrong': } + } + DOC + it 'does not match a integer as string' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).not_to match(%r{Notice: output wrong}) + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/is_array_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/is_array_spec.rb new file mode 100644 index 0000000..408ff1e --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/is_array_spec.rb @@ -0,0 +1,65 @@ +require 'spec_helper_acceptance' + +describe 'is_array function' do + describe 'success' do + pp1 = <<-DOC + $a = ['aaa','bbb','ccc'] + $b = true + $o = is_array($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_arrays arrays' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp2 = <<-DOC + $a = [] + $b = true + $o = is_array($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_arrays empty arrays' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp3 = <<-DOC + $a = "aoeu" + $b = false + $o = is_array($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_arrays strings' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp4 = <<-DOC + $a = {'aaa'=>'bbb'} + $b = false + $o = is_array($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_arrays hashes' do + apply_manifest(pp4, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end + describe 'failure' do + it 'handles improper argument counts' + it 'handles non-arrays' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/is_bool_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/is_bool_spec.rb new file mode 100644 index 0000000..e9dab73 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/is_bool_spec.rb @@ -0,0 +1,79 @@ +require 'spec_helper_acceptance' + +describe 'is_bool function' do + describe 'success' do + pp1 = <<-DOC + $a = ['aaa','bbb','ccc'] + $b = false + $o = is_bool($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_bools arrays' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp2 = <<-DOC + $a = true + $b = true + $o = is_bool($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_bools true' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp3 = <<-DOC + $a = false + $b = true + $o = is_bool($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_bools false' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp4 = <<-DOC + $a = "true" + $b = false + $o = is_bool($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_bools strings' do + apply_manifest(pp4, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp5 = <<-DOC + $a = {'aaa'=>'bbb'} + $b = false + $o = is_bool($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_bools hashes' do + apply_manifest(pp5, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end + describe 'failure' do + it 'handles improper argument counts' + it 'handles non-arrays' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/is_domain_name_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/is_domain_name_spec.rb new file mode 100644 index 0000000..b33eb7a --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/is_domain_name_spec.rb @@ -0,0 +1,81 @@ +require 'spec_helper_acceptance' + +describe 'is_domain_name function' do + describe 'success' do + pp1 = <<-DOC + $a = ['aaa.com','bbb','ccc'] + $o = is_domain_name($a) + notice(inline_template('is_domain_name is <%= @o.inspect %>')) + DOC + it 'is_domain_names arrays' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{is_domain_name is false}) + end + end + + pp2 = <<-DOC + $a = true + $o = is_domain_name($a) + notice(inline_template('is_domain_name is <%= @o.inspect %>')) + DOC + it 'is_domain_names true' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{is_domain_name is false}) + end + end + + pp3 = <<-DOC + $a = false + $o = is_domain_name($a) + notice(inline_template('is_domain_name is <%= @o.inspect %>')) + DOC + it 'is_domain_names false' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{is_domain_name is false}) + end + end + + pp4 = <<-DOC + $a = "3foo-bar.2bar-fuzz.com" + $b = true + $o = is_domain_name($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_domain_names strings with hyphens' do + apply_manifest(pp4, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp5 = <<-DOC + $a = "-bar.2bar-fuzz.com" + $b = false + $o = is_domain_name($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_domain_names strings beginning with hyphens' do + apply_manifest(pp5, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp6 = <<-DOC + $a = {'aaa'=>'www.com'} + $o = is_domain_name($a) + notice(inline_template('is_domain_name is <%= @o.inspect %>')) + DOC + it 'is_domain_names hashes' do + apply_manifest(pp6, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{is_domain_name is false}) + end + end + end + describe 'failure' do + it 'handles improper argument counts' + it 'handles non-arrays' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/is_float_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/is_float_spec.rb new file mode 100644 index 0000000..524f338 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/is_float_spec.rb @@ -0,0 +1,84 @@ +require 'spec_helper_acceptance' + +describe 'is_float function' do + describe 'success' do + pp1 = <<-DOC + $a = ['aaa.com','bbb','ccc'] + $o = is_float($a) + notice(inline_template('is_float is <%= @o.inspect %>')) + DOC + it 'is_floats arrays' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{is_float is false}) + end + end + + pp2 = <<-DOC + $a = true + $o = is_float($a) + notice(inline_template('is_float is <%= @o.inspect %>')) + DOC + it 'is_floats true' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{is_float is false}) + end + end + + pp3 = <<-DOC + $a = "3.5" + $b = true + $o = is_float($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_floats strings' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp4 = <<-DOC + $a = 3.5 + $b = true + $o = is_float($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_floats floats' do + apply_manifest(pp4, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp5 = <<-DOC + $a = 3 + $b = false + $o = is_float($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_floats integers' do + apply_manifest(pp5, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp6 = <<-DOC + $a = {'aaa'=>'www.com'} + $o = is_float($a) + notice(inline_template('is_float is <%= @o.inspect %>')) + DOC + it 'is_floats hashes' do + apply_manifest(pp6, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{is_float is false}) + end + end + end + describe 'failure' do + it 'handles improper argument counts' + it 'handles non-arrays' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/is_function_available_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/is_function_available_spec.rb new file mode 100644 index 0000000..8bb63f2 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/is_function_available_spec.rb @@ -0,0 +1,56 @@ +require 'spec_helper_acceptance' + +describe 'is_function_available function' do + describe 'success' do + pp1 = <<-DOC + $a = ['fail','include','require'] + $o = is_function_available($a) + notice(inline_template('is_function_available is <%= @o.inspect %>')) + DOC + it 'is_function_availables arrays' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{is_function_available is false}) + end + end + + pp2 = <<-DOC + $a = true + $o = is_function_available($a) + notice(inline_template('is_function_available is <%= @o.inspect %>')) + DOC + it 'is_function_availables true' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{is_function_available is false}) + end + end + + pp3 = <<-DOC + $a = "fail" + $b = true + $o = is_function_available($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_function_availables strings' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp4 = <<-DOC + $a = "is_function_available" + $o = is_function_available($a) + notice(inline_template('is_function_available is <%= @o.inspect %>')) + DOC + it 'is_function_availables function_availables' do + apply_manifest(pp4, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{is_function_available is true}) + end + end + end + describe 'failure' do + it 'handles improper argument counts' + it 'handles non-arrays' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/is_hash_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/is_hash_spec.rb new file mode 100644 index 0000000..c5f6b2e --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/is_hash_spec.rb @@ -0,0 +1,61 @@ +require 'spec_helper_acceptance' + +describe 'is_hash function' do + describe 'success' do + pp1 = <<-DOC + $a = ['aaa','bbb','ccc'] + $o = is_hash($a) + notice(inline_template('is_hash is <%= @o.inspect %>')) + DOC + it 'is_hashs arrays' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{is_hash is false}) + end + end + + pp2 = <<-DOC + $a = {} + $b = true + $o = is_hash($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_hashs empty hashs' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp3 = <<-DOC + $a = "aoeu" + $b = false + $o = is_hash($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_hashs strings' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp4 = <<-DOC + $a = {'aaa'=>'bbb'} + $b = true + $o = is_hash($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_hashs hashes' do + apply_manifest(pp4, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end + describe 'failure' do + it 'handles improper argument counts' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/is_integer_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/is_integer_spec.rb new file mode 100644 index 0000000..45a8c2b --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/is_integer_spec.rb @@ -0,0 +1,93 @@ +require 'spec_helper_acceptance' + +describe 'is_integer function' do + describe 'success' do + pp1 = <<-DOC + $a = ['aaa.com','bbb','ccc'] + $b = false + $o = is_integer($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_integers arrays' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp2 = <<-DOC + $a = true + $b = false + $o = is_integer($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_integers true' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp3 = <<-DOC + $a = "3" + $b = true + $o = is_integer($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_integers strings' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp4 = <<-DOC + $a = 3.5 + $b = false + $o = is_integer($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_integers floats' do + apply_manifest(pp4, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp5 = <<-DOC + $a = 3 + $b = true + $o = is_integer($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_integers integers' do + apply_manifest(pp5, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp6 = <<-DOC + $a = {'aaa'=>'www.com'} + $b = false + $o = is_integer($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_integers hashes' do + apply_manifest(pp6, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end + describe 'failure' do + it 'handles improper argument counts' + it 'handles non-arrays' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/is_ip_address_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/is_ip_address_spec.rb new file mode 100644 index 0000000..e528fe5 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/is_ip_address_spec.rb @@ -0,0 +1,78 @@ +require 'spec_helper_acceptance' + +describe 'is_ip_address function' do + describe 'success' do + pp1 = <<-DOC + $a = '1.2.3.4' + $b = true + $o = is_ip_address($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_ip_addresss ipv4' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp2 = <<-DOC + $a = "fe80:0000:cd12:d123:e2f8:47ff:fe09:dd74" + $b = true + $o = is_ip_address($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_ip_addresss ipv6' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp3 = <<-DOC + $a = "fe00::1" + $b = true + $o = is_ip_address($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_ip_addresss ipv6 compressed' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp4 = <<-DOC + $a = "aoeu" + $b = false + $o = is_ip_address($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_ip_addresss strings' do + apply_manifest(pp4, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp5 = <<-DOC + $a = '1.2.3.400' + $b = false + $o = is_ip_address($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_ip_addresss ipv4 out of range' do + apply_manifest(pp5, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end + describe 'failure' do + it 'handles improper argument counts' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/is_ipv4_address_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/is_ipv4_address_spec.rb new file mode 100644 index 0000000..04cb45f --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/is_ipv4_address_spec.rb @@ -0,0 +1,50 @@ +require 'spec_helper_acceptance' + +describe 'is_ipv4_address function' do + describe 'success' do + pp1 = <<-DOC + $a = '1.2.3.4' + $b = true + $o = is_ipv4_address($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_ipv4_addresss' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp2 = <<-DOC + $a = "aoeu" + $b = false + $o = is_ipv4_address($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_ipv4_addresss strings' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp3 = <<-DOC + $a = '1.2.3.400' + $b = false + $o = is_ipv4_address($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_ipv4_addresss ipv4 out of range' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end + describe 'failure' do + it 'handles improper argument counts' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/is_ipv6_address_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/is_ipv6_address_spec.rb new file mode 100644 index 0000000..03e5dd1 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/is_ipv6_address_spec.rb @@ -0,0 +1,64 @@ +require 'spec_helper_acceptance' + +describe 'is_ipv6_address function' do + describe 'success' do + pp1 = <<-DOC + $a = "fe80:0000:cd12:d123:e2f8:47ff:fe09:dd74" + $b = true + $o = is_ipv6_address($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_ipv6_addresss' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp2 = <<-DOC + $a = "fe00::1" + $b = true + $o = is_ipv6_address($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_ipv6_addresss ipv6 compressed' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp3 = <<-DOC + $a = "aoeu" + $b = false + $o = is_ipv6_address($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_ipv6_addresss strings' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp4 = <<-DOC + $a = 'fe80:0000:cd12:d123:e2f8:47ff:fe09:gggg' + $b = false + $o = is_ipv6_address($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_ipv6_addresss ip out of range' do + apply_manifest(pp4, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end + describe 'failure' do + it 'handles improper argument counts' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/is_mac_address_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/is_mac_address_spec.rb new file mode 100644 index 0000000..8e9d64d --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/is_mac_address_spec.rb @@ -0,0 +1,36 @@ +require 'spec_helper_acceptance' + +describe 'is_mac_address function' do + describe 'success' do + pp1 = <<-DOC + $a = '00:a0:1f:12:7f:a0' + $b = true + $o = is_mac_address($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_mac_addresss a mac' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp2 = <<-DOC + $a = '00:a0:1f:12:7f:g0' + $b = false + $o = is_mac_address($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_mac_addresss a mac out of range' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end + describe 'failure' do + it 'handles improper argument counts' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/is_numeric_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/is_numeric_spec.rb new file mode 100644 index 0000000..4ec7f0c --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/is_numeric_spec.rb @@ -0,0 +1,93 @@ +require 'spec_helper_acceptance' + +describe 'is_numeric function' do + describe 'success' do + pp1 = <<-DOC + $a = ['aaa.com','bbb','ccc'] + $b = false + $o = is_numeric($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_numerics arrays' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp2 = <<-DOC + $a = true + $b = false + $o = is_numeric($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_numerics true' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp3 = <<-DOC + $a = "3" + $b = true + $o = is_numeric($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_numerics strings' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp4 = <<-DOC + $a = 3.5 + $b = true + $o = is_numeric($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_numerics floats' do + apply_manifest(pp4, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp5 = <<-DOC + $a = 3 + $b = true + $o = is_numeric($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_numerics integers' do + apply_manifest(pp5, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp6 = <<-DOC + $a = {'aaa'=>'www.com'} + $b = false + $o = is_numeric($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_numerics hashes' do + apply_manifest(pp6, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end + describe 'failure' do + it 'handles improper argument counts' + it 'handles non-arrays' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/is_string_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/is_string_spec.rb new file mode 100644 index 0000000..e3ab31a --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/is_string_spec.rb @@ -0,0 +1,111 @@ +require 'spec_helper_acceptance' + +describe 'is_string function' do + describe 'success' do + pp1 = <<-DOC + $a = ['aaa.com','bbb','ccc'] + $b = false + $o = is_string($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_strings arrays' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp2 = <<-DOC + $a = true + $b = false + $o = is_string($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_strings true' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp3 = <<-DOC + $a = "aoeu" + $o = is_string($a) + notice(inline_template('is_string is <%= @o.inspect %>')) + DOC + it 'is_strings strings' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{is_string is true}) + end + end + + pp4 = <<-DOC + $a = "3" + $o = is_string($a) + notice(inline_template('is_string is <%= @o.inspect %>')) + DOC + it 'is_strings number strings' do + apply_manifest(pp4, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{is_string is false}) + end + end + + pp5 = <<-DOC + $a = 3.5 + $b = false + $o = is_string($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_strings floats' do + apply_manifest(pp5, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp6 = <<-DOC + $a = 3 + $b = false + $o = is_string($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_strings integers' do + apply_manifest(pp6, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp7 = <<-DOC + $a = {'aaa'=>'www.com'} + $b = false + $o = is_string($a) + if $o == $b { + notify { 'output correct': } + } + DOC + it 'is_strings hashes' do + apply_manifest(pp7, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + pp8 = <<-DOC + $a = undef + $o = is_string($a) + notice(inline_template('is_string is <%= @o.inspect %>')) + DOC + it 'is_strings undef' do + apply_manifest(pp8, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{is_string is true}) + end + end + end + describe 'failure' do + it 'handles improper argument counts' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/join_keys_to_values_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/join_keys_to_values_spec.rb new file mode 100644 index 0000000..a9f30e3 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/join_keys_to_values_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper_acceptance' + +describe 'join_keys_to_values function' do + describe 'success' do + pp = <<-DOC + $a = {'aaa'=>'bbb','ccc'=>'ddd'} + $b = ':' + $o = join_keys_to_values($a,$b) + notice(inline_template('join_keys_to_values is <%= @o.sort.inspect %>')) + DOC + it 'join_keys_to_valuess hashes' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{join_keys_to_values is \["aaa:bbb", "ccc:ddd"\]}) + end + end + it 'handles non hashes' + it 'handles empty hashes' + end + describe 'failure' do + it 'handles improper argument counts' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/join_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/join_spec.rb new file mode 100644 index 0000000..233b953 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/join_spec.rb @@ -0,0 +1,24 @@ +require 'spec_helper_acceptance' + +describe 'join function', :if => Puppet::Util::Package.versioncmp(Puppet.version, '5.5.0') < 0 do + describe 'success' do + pp = <<-DOC + $a = ['aaa','bbb','ccc'] + $b = ':' + $c = 'aaa:bbb:ccc' + $o = join($a,$b) + if $o == $c { + notify { 'output correct': } + } + DOC + it 'joins arrays' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + it 'handles non arrays' + end + describe 'failure' do + it 'handles improper argument counts' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/keys_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/keys_spec.rb new file mode 100644 index 0000000..9c4122c --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/keys_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper_acceptance' + +describe 'keys function', :if => Puppet::Util::Package.versioncmp(Puppet.version, '5.5.0') < 0 do + describe 'success' do + pp = <<-DOC + $a = {'aaa'=>'bbb','ccc'=>'ddd'} + $o = keys($a) + notice(inline_template('keys is <%= @o.sort.inspect %>')) + DOC + it 'keyss hashes' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{keys is \["aaa", "ccc"\]}) + end + end + it 'handles non hashes' + it 'handles empty hashes' + end + describe 'failure' do + it 'handles improper argument counts' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/loadjson_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/loadjson_spec.rb new file mode 100644 index 0000000..31d015a --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/loadjson_spec.rb @@ -0,0 +1,48 @@ +require 'spec_helper_acceptance' + +tmpdir = default.tmpdir('stdlib') + +describe 'loadjson function' do + describe 'success' do + shell("echo '{\"aaa\":1,\"bbb\":2,\"ccc\":3,\"ddd\":4}' > #{tmpdir}/test1json.json") + pp1 = <<-DOC + $o = loadjson('#{tmpdir}/test1json.json') + notice(inline_template('loadjson[aaa] is <%= @o["aaa"].inspect %>')) + notice(inline_template('loadjson[bbb] is <%= @o["bbb"].inspect %>')) + notice(inline_template('loadjson[ccc] is <%= @o["ccc"].inspect %>')) + notice(inline_template('loadjson[ddd] is <%= @o["ddd"].inspect %>')) + DOC + regex_array = [%r{loadjson\[aaa\] is 1}, %r{loadjson\[bbb\] is 2}, %r{loadjson\[ccc\] is 3}, %r{loadjson\[ddd\] is 4}] + it 'loadjsons array of values' do + apply_manifest(pp1, :catch_failures => true) do |r| + regex_array.each do |i| + expect(r.stdout).to match(i) + end + end + end + + pp2 = <<-DOC + $o = loadjson('#{tmpdir}/no-file.json', {'default' => 'value'}) + notice(inline_template('loadjson[default] is <%= @o["default"].inspect %>')) + DOC + it 'returns the default value if there is no file to load' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{loadjson\[default\] is "value"}) + end + end + + shell("echo '!' > #{tmpdir}/test2json.json") + pp3 = <<-DOC + $o = loadjson('#{tmpdir}/test2json.json', {'default' => 'value'}) + notice(inline_template('loadjson[default] is <%= @o["default"].inspect %>')) + DOC + it 'returns the default value if the file was parsed with an error' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{loadjson\[default\] is "value"}) + end + end + end + describe 'failure' do + it 'fails with no arguments' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/loadyaml_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/loadyaml_spec.rb new file mode 100644 index 0000000..f55274c --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/loadyaml_spec.rb @@ -0,0 +1,52 @@ +require 'spec_helper_acceptance' + +tmpdir = default.tmpdir('stdlib') + +describe 'loadyaml function' do + describe 'success' do + shell("echo '--- + aaa: 1 + bbb: 2 + ccc: 3 + ddd: 4' > #{tmpdir}/test1yaml.yaml") + pp1 = <<-DOC + $o = loadyaml('#{tmpdir}/test1yaml.yaml') + notice(inline_template('loadyaml[aaa] is <%= @o["aaa"].inspect %>')) + notice(inline_template('loadyaml[bbb] is <%= @o["bbb"].inspect %>')) + notice(inline_template('loadyaml[ccc] is <%= @o["ccc"].inspect %>')) + notice(inline_template('loadyaml[ddd] is <%= @o["ddd"].inspect %>')) + DOC + regex_array = [%r{loadyaml\[aaa\] is 1}, %r{loadyaml\[bbb\] is 2}, %r{loadyaml\[ccc\] is 3}, %r{loadyaml\[ddd\] is 4}] + it 'loadyamls array of values' do + apply_manifest(pp1, :catch_failures => true) do |r| + regex_array.each do |i| + expect(r.stdout).to match(i) + end + end + end + + pp2 = <<-DOC + $o = loadyaml('#{tmpdir}/no-file.yaml', {'default' => 'value'}) + notice(inline_template('loadyaml[default] is <%= @o["default"].inspect %>')) + DOC + it 'returns the default value if there is no file to load' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{loadyaml\[default\] is "value"}) + end + end + + shell("echo '!' > #{tmpdir}/test2yaml.yaml") + pp3 = <<-DOC + $o = loadyaml('#{tmpdir}/test2yaml.yaml', {'default' => 'value'}) + notice(inline_template('loadyaml[default] is <%= @o["default"].inspect %>')) + DOC + it 'returns the default value if the file was parsed with an error' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{loadyaml\[default\] is "value"}) + end + end + end + describe 'failure' do + it 'fails with no arguments' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/lstrip_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/lstrip_spec.rb new file mode 100644 index 0000000..fe9c013 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/lstrip_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper_acceptance' + +describe 'lstrip function' do + describe 'success' do + pp1 = <<-DOC + $a = [" the "," public "," art","galleries "] + # Anagram: Large picture halls, I bet + $o = lstrip($a) + notice(inline_template('lstrip is <%= @o.inspect %>')) + DOC + it 'lstrips arrays' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{lstrip is \["the ", "public ", "art", "galleries "\]}) + end + end + + pp2 = <<-DOC + $a = " blowzy night-frumps vex'd jack q " + $o = lstrip($a) + notice(inline_template('lstrip is <%= @o.inspect %>')) + DOC + it 'lstrips strings' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{lstrip is "blowzy night-frumps vex'd jack q "}) + end + end + end + describe 'failure' do + it 'handles no arguments' + it 'handles non strings or arrays' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/max_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/max_spec.rb new file mode 100644 index 0000000..9482871 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/max_spec.rb @@ -0,0 +1,18 @@ +require 'spec_helper_acceptance' + +describe 'max function' do + describe 'success' do + pp = <<-DOC + $o = max("the","public","art","galleries") + notice(inline_template('max is <%= @o.inspect %>')) + DOC + it 'maxs arrays' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{max is "the"}) + end + end + end + describe 'failure' do + it 'handles no arguments' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/member_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/member_spec.rb new file mode 100644 index 0000000..ebadeb4 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/member_spec.rb @@ -0,0 +1,57 @@ +require 'spec_helper_acceptance' + +describe 'member function' do + shared_examples 'item found' do + it 'outputs correctly' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + end + describe 'success' do + pp1 = <<-DOC + $a = ['aaa','bbb','ccc'] + $b = 'ccc' + $c = true + $o = member($a,$b) + if $o == $c { + notify { 'output correct': } + } + DOC + it 'members arrays' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: output correct}) + end + end + + describe 'members array of integers' do + let(:pp) do + <<-DOC + if member( [1,2,3,4], 4 ){ + notify { 'output correct': } + } + DOC + end + + it_behaves_like 'item found' do + end + end + describe 'members of mixed array' do + let(:pp) do + <<-DOC + if member( ['a','4',3], 'a' ){ + notify { 'output correct': } + } + DOC + end + + it_behaves_like 'item found' do + end + end + it 'members arrays without members' + end + + describe 'failure' do + it 'handles improper argument counts' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/merge_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/merge_spec.rb new file mode 100644 index 0000000..17e2b9e --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/merge_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper_acceptance' + +describe 'merge function' do + describe 'success' do + pp = <<-DOC + $a = {'one' => 1, 'two' => 2, 'three' => { 'four' => 4 } } + $b = {'two' => 'dos', 'three' => { 'five' => 5 } } + $o = merge($a, $b) + notice(inline_template('merge[one] is <%= @o["one"].inspect %>')) + notice(inline_template('merge[two] is <%= @o["two"].inspect %>')) + notice(inline_template('merge[three] is <%= @o["three"].inspect %>')) + DOC + regex_array = [%r{merge\[one\] is ("1"|1)}, %r{merge\[two\] is "dos"}, %r{merge\[three\] is {"five"=>("5"|5)}}] + it 'merges two hashes' do + apply_manifest(pp, :catch_failures => true) do |r| + regex_array.each do |i| + expect(r.stdout).to match(i) + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/min_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/min_spec.rb new file mode 100644 index 0000000..deb6605 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/min_spec.rb @@ -0,0 +1,18 @@ +require 'spec_helper_acceptance' + +describe 'min function' do + describe 'success' do + pp = <<-DOC + $o = min("the","public","art","galleries") + notice(inline_template('min is <%= @o.inspect %>')) + DOC + it 'mins arrays' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{min is "art"}) + end + end + end + describe 'failure' do + it 'handles no arguments' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/nodesets/centos-7-x64.yml b/code/environments/production/modules/stdlib/spec/acceptance/nodesets/centos-7-x64.yml new file mode 100644 index 0000000..5eebdef --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/nodesets/centos-7-x64.yml @@ -0,0 +1,10 @@ +HOSTS: + centos-7-x64: + roles: + - agent + - default + platform: el-7-x86_64 + hypervisor: vagrant + box: puppetlabs/centos-7.2-64-nocm +CONFIG: + type: foss diff --git a/code/environments/production/modules/stdlib/spec/acceptance/nodesets/debian-8-x64.yml b/code/environments/production/modules/stdlib/spec/acceptance/nodesets/debian-8-x64.yml new file mode 100644 index 0000000..fef6e63 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/nodesets/debian-8-x64.yml @@ -0,0 +1,10 @@ +HOSTS: + debian-8-x64: + roles: + - agent + - default + platform: debian-8-amd64 + hypervisor: vagrant + box: puppetlabs/debian-8.2-64-nocm +CONFIG: + type: foss diff --git a/code/environments/production/modules/stdlib/spec/acceptance/nodesets/default.yml b/code/environments/production/modules/stdlib/spec/acceptance/nodesets/default.yml new file mode 100644 index 0000000..dba339c --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/nodesets/default.yml @@ -0,0 +1,10 @@ +HOSTS: + ubuntu-1404-x64: + roles: + - agent + - default + platform: ubuntu-14.04-amd64 + hypervisor: vagrant + box: puppetlabs/ubuntu-14.04-64-nocm +CONFIG: + type: foss diff --git a/code/environments/production/modules/stdlib/spec/acceptance/nodesets/docker/centos-7.yml b/code/environments/production/modules/stdlib/spec/acceptance/nodesets/docker/centos-7.yml new file mode 100644 index 0000000..a3333aa --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/nodesets/docker/centos-7.yml @@ -0,0 +1,12 @@ +HOSTS: + centos-7-x64: + platform: el-7-x86_64 + hypervisor: docker + image: centos:7 + docker_preserve_image: true + docker_cmd: '["/usr/sbin/init"]' + # install various tools required to get the image up to usable levels + docker_image_commands: + - 'yum install -y crontabs tar wget openssl sysvinit-tools iproute which initscripts' +CONFIG: + trace_limit: 200 diff --git a/code/environments/production/modules/stdlib/spec/acceptance/nodesets/docker/debian-8.yml b/code/environments/production/modules/stdlib/spec/acceptance/nodesets/docker/debian-8.yml new file mode 100644 index 0000000..df5c319 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/nodesets/docker/debian-8.yml @@ -0,0 +1,11 @@ +HOSTS: + debian-8-x64: + platform: debian-8-amd64 + hypervisor: docker + image: debian:8 + docker_preserve_image: true + docker_cmd: '["/sbin/init"]' + docker_image_commands: + - 'apt-get update && apt-get install -y net-tools wget locales strace lsof && echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && locale-gen' +CONFIG: + trace_limit: 200 diff --git a/code/environments/production/modules/stdlib/spec/acceptance/nodesets/docker/ubuntu-14.04.yml b/code/environments/production/modules/stdlib/spec/acceptance/nodesets/docker/ubuntu-14.04.yml new file mode 100644 index 0000000..b1efa58 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/nodesets/docker/ubuntu-14.04.yml @@ -0,0 +1,12 @@ +HOSTS: + ubuntu-1404-x64: + platform: ubuntu-14.04-amd64 + hypervisor: docker + image: ubuntu:14.04 + docker_preserve_image: true + docker_cmd: '["/sbin/init"]' + docker_image_commands: + # ensure that upstart is booting correctly in the container + - 'rm /usr/sbin/policy-rc.d && rm /sbin/initctl && dpkg-divert --rename --remove /sbin/initctl && apt-get update && apt-get install -y net-tools wget && locale-gen en_US.UTF-8' +CONFIG: + trace_limit: 200 diff --git a/code/environments/production/modules/stdlib/spec/acceptance/num2bool_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/num2bool_spec.rb new file mode 100644 index 0000000..d95cb93 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/num2bool_spec.rb @@ -0,0 +1,76 @@ +require 'spec_helper_acceptance' + +describe 'num2bool function' do + describe 'success' do + pp1 = <<-DOC + $a = 1 + $b = "1" + $c = "50" + $ao = num2bool($a) + $bo = num2bool($b) + $co = num2bool($c) + notice(inline_template('a is <%= @ao.inspect %>')) + notice(inline_template('b is <%= @bo.inspect %>')) + notice(inline_template('c is <%= @co.inspect %>')) + DOC + regex_array_true = [%r{a is true}, %r{b is true}, %r{c is true}] + it 'bools positive numbers and numeric strings as true' do + apply_manifest(pp1, :catch_failures => true) do |r| + regex_array_true.each do |i| + expect(r.stdout).to match(i) + end + end + end + + pp2 = <<-DOC + $a = 0 + $b = -0.1 + $c = ["-50","1"] + $ao = num2bool($a) + $bo = num2bool($b) + $co = num2bool($c) + notice(inline_template('a is <%= @ao.inspect %>')) + notice(inline_template('b is <%= @bo.inspect %>')) + notice(inline_template('c is <%= @co.inspect %>')) + DOC + regex_array_false = [%r{a is false}, %r{b is false}, %r{c is false}] + it 'bools negative numbers as false' do + apply_manifest(pp2, :catch_failures => true) do |r| + regex_array_false.each do |i| + expect(r.stdout).to match(i) + end + end + end + end + + describe 'failure' do + pp3 = <<-DOC + $a = "a" + $ao = num2bool($a) + notice(inline_template('a is <%= @ao.inspect %>')) + DOC + it 'fails on words' do + expect(apply_manifest(pp3, :expect_failures => true).stderr).to match(%r{not look like a number}) + end + + pp4 = <<-DOC + $b = "1b" + $bo = num2bool($b) + notice(inline_template('b is <%= @bo.inspect %>')) + DOC + it 'fails on numberwords' do + expect(apply_manifest(pp4, :expect_failures => true).stderr).to match(%r{not look like a number}) + end + + pp5 = <<-DOC # rubocop:disable Lint/UselessAssignment + $c = {"c" => "-50"} + $co = num2bool($c) + notice(inline_template('c is <%= @co.inspect %>')) + DOC + it 'fails on non-numeric/strings' do + pending "The function will call .to_s.to_i on anything not a Numeric or + String, and results in 0. Is this intended?" + expect(apply_manifest(pp5(:expect_failures => true)).stderr).to match(%r{Unable to parse}) + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/parsejson_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/parsejson_spec.rb new file mode 100644 index 0000000..8a19907 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/parsejson_spec.rb @@ -0,0 +1,50 @@ +require 'spec_helper_acceptance' + +describe 'parsejson function' do + describe 'success' do + pp1 = <<-DOC + $a = '{"hunter": "washere", "tests": "passing"}' + $ao = parsejson($a) + $tests = $ao['tests'] + notice(inline_template('tests are <%= @tests.inspect %>')) + DOC + it 'parses valid json' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{tests are "passing"}) + end + end + end + + describe 'failure' do + pp2 = <<-DOC + $a = '{"hunter": "washere", "tests": "passing",}' + $ao = parsejson($a, 'tests are using the default value') + notice(inline_template('a is <%= @ao.inspect %>')) + DOC + it 'raises error on incorrect json - default value is used' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{tests are using the default value}) + end + end + + pp3 = <<-DOC + $a = '{"hunter": "washere", "tests": "passing",}' + $ao = parsejson($a) + notice(inline_template('a is <%= @ao.inspect %>')) + DOC + it 'raises error on incorrect json' do + apply_manifest(pp3, :expect_failures => true) do |r| + expect(r.stderr).to match(%r{expected next name}) + end + end + + pp4 = <<-DOC + $o = parsejson() + DOC + it 'raises error on incorrect number of arguments' do + apply_manifest(pp4, :expect_failures => true) do |r| + expect(r.stderr).to match(%r{wrong number of arguments}i) + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/parseyaml_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/parseyaml_spec.rb new file mode 100644 index 0000000..4cdf36d --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/parseyaml_spec.rb @@ -0,0 +1,52 @@ +require 'spec_helper_acceptance' + +describe 'parseyaml function' do + describe 'success' do + pp1 = <<-DOC + $a = "---\nhunter: washere\ntests: passing\n" + $o = parseyaml($a) + $tests = $o['tests'] + notice(inline_template('tests are <%= @tests.inspect %>')) + DOC + it 'parses valid yaml' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{tests are "passing"}) + end + end + end + + describe 'failure' do + pp2 = <<-DOC + $a = "---\nhunter: washere\ntests: passing\n:" + $o = parseyaml($a, {'tests' => 'using the default value'}) + $tests = $o['tests'] + notice(inline_template('tests are <%= @tests.inspect %>')) + DOC + it 'returns the default value on incorrect yaml' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{tests are "using the default value"}) + end + end + + pp3 = <<-DOC + $a = "---\nhunter: washere\ntests: passing\n:" + $o = parseyaml($a) + $tests = $o['tests'] + notice(inline_template('tests are <%= @tests.inspect %>')) + DOC + it 'raises error on incorrect yaml' do + apply_manifest(pp3, :expect_failures => true) do |r| + expect(r.stderr).to match(%r{(syntax error|did not find expected key)}) + end + end + + pp4 = <<-DOC + $o = parseyaml() + DOC + it 'raises error on incorrect number of arguments' do + apply_manifest(pp4, :expect_failures => true) do |r| + expect(r.stderr).to match(%r{wrong number of arguments}i) + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/pick_default_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/pick_default_spec.rb new file mode 100644 index 0000000..82b7ea5 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/pick_default_spec.rb @@ -0,0 +1,51 @@ +require 'spec_helper_acceptance' + +describe 'pick_default function' do + describe 'success' do + pp1 = <<-DOC + $a = undef + $o = pick_default($a, 'default') + notice(inline_template('picked is <%= @o.inspect %>')) + DOC + it 'pick_defaults a default value' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{picked is "default"}) + end + end + + pp2 = <<-DOC + $a = undef + $b = undef + $o = pick_default($a,$b) + notice(inline_template('picked is <%= @o.inspect %>')) + DOC + it 'pick_defaults with no value' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{picked is ""}) + end + end + + pp3 = <<-DOC + $a = "something" + $b = "long" + $o = pick_default($a, $b, 'default') + notice(inline_template('picked is <%= @o.inspect %>')) + DOC + it 'pick_defaults the first set value' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{picked is "something"}) + end + end + end + describe 'failure' do + pp4 = <<-DOC + $o = pick_default() + notice(inline_template('picked is <%= @o.inspect %>')) + DOC + it 'raises error with no values' do + apply_manifest(pp4, :expect_failures => true) do |r| + expect(r.stderr).to match(%r{Must receive at least one argument}) + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/pick_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/pick_spec.rb new file mode 100644 index 0000000..14834b4 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/pick_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper_acceptance' + +describe 'pick function' do + describe 'success' do + pp1 = <<-DOC + $a = undef + $o = pick($a, 'default') + notice(inline_template('picked is <%= @o.inspect %>')) + DOC + it 'picks a default value' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{picked is "default"}) + end + end + + pp2 = <<-DOC + $a = "something" + $b = "long" + $o = pick($a, $b, 'default') + notice(inline_template('picked is <%= @o.inspect %>')) + DOC + it 'picks the first set value' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{picked is "something"}) + end + end + end + + describe 'failure' do + pp3 = <<-DOC + $a = undef + $b = undef + $o = pick($a, $b) + notice(inline_template('picked is <%= @o.inspect %>')) + DOC + it 'raises error with all undef values' do + apply_manifest(pp3, :expect_failures => true) do |r| + expect(r.stderr).to match(%r{must receive at least one non empty value}) + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/prefix_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/prefix_spec.rb new file mode 100644 index 0000000..9a37fb3 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/prefix_spec.rb @@ -0,0 +1,40 @@ +require 'spec_helper_acceptance' + +describe 'prefix function' do + describe 'success' do + pp1 = <<-DOC + $o = prefix(['a','b','c'],'p') + notice(inline_template('prefix is <%= @o.inspect %>')) + DOC + it 'prefixes array of values' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{prefix is \["pa", "pb", "pc"\]}) + end + end + + pp2 = <<-DOC + $o = prefix([],'p') + notice(inline_template('prefix is <%= @o.inspect %>')) + DOC + it 'prefixs with empty array' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{prefix is \[\]}) + end + end + + pp3 = <<-DOC + $o = prefix(['a','b','c'], undef) + notice(inline_template('prefix is <%= @o.inspect %>')) + DOC + it 'prefixs array of values with undef' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{prefix is \["a", "b", "c"\]}) + end + end + end + describe 'failure' do + it 'fails with no arguments' + it 'fails when first argument is not array' + it 'fails when second argument is not string' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/pw_hash_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/pw_hash_spec.rb new file mode 100644 index 0000000..f6e2928 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/pw_hash_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper_acceptance' + +# Windows and OS X do not have useful implementations of crypt(3) +describe 'pw_hash function', :unless => %w[windows Darwin SLES].include?(fact('operatingsystem')) do + describe 'success' do + pp1 = <<-DOC + $o = pw_hash('password', 'sha-512', 'salt') + notice(inline_template('pw_hash is <%= @o.inspect %>')) + DOC + it 'hashes passwords' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{pw_hash is "\$6\$salt\$IxDD3jeSOb5eB1CX5LBsqZFVkJdido3OUILO5Ifz5iwMuTS4XMS130MTSuDDl3aCI6WouIL9AjRbLCelDCy\.g\."}) + end + end + + pp2 = <<-DOC + $o = pw_hash('', 'sha-512', 'salt') + notice(inline_template('pw_hash is <%= @o.inspect %>')) + DOC + it 'returns nil if no password is provided' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{pw_hash is nil}) + end + end + end + describe 'failure' do + it 'handles less than three arguments' + it 'handles more than three arguments' + it 'handles non strings' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/range_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/range_spec.rb new file mode 100644 index 0000000..a5a7d22 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/range_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper_acceptance' + +describe 'range function' do + describe 'success' do + pp1 = <<-DOC + $o = range('a','d') + notice(inline_template('range is <%= @o.inspect %>')) + DOC + it 'ranges letters' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{range is \["a", "b", "c", "d"\]}) + end + end + + pp2 = <<-DOC + $o = range('a','d', '2') + notice(inline_template('range is <%= @o.inspect %>')) + DOC + it 'ranges letters with a step' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{range is \["a", "c"\]}) + end + end + it 'ranges letters with a negative step' + it 'ranges numbers' + it 'ranges numbers with a step' + it 'ranges numbers with a negative step' + it 'ranges numeric strings' + it 'ranges zero padded numbers' + end + describe 'failure' do + it 'fails with no arguments' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/reject_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/reject_spec.rb new file mode 100644 index 0000000..753dd78 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/reject_spec.rb @@ -0,0 +1,40 @@ +require 'spec_helper_acceptance' + +describe 'reject function' do + describe 'success' do + pp1 = <<-DOC + $o = reject(['aaa','bbb','ccc','aaaddd'], 'aaa') + notice(inline_template('reject is <%= @o.inspect %>')) + DOC + it 'rejects array of values' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{reject is \["bbb", "ccc"\]}) + end + end + + pp2 = <<-DOC + $o = reject([],'aaa') + notice(inline_template('reject is <%= @o.inspect %>')) + DOC + it 'rejects with empty array' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{reject is \[\]}) + end + end + + pp3 = <<-DOC + $o = reject(['aaa','bbb','ccc','aaaddd'], undef) + notice(inline_template('reject is <%= @o.inspect %>')) + DOC + it 'rejects array of values with undef' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{reject is \[\]}) + end + end + end + describe 'failure' do + it 'fails with no arguments' + it 'fails when first argument is not array' + it 'fails when second argument is not string' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/reverse_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/reverse_spec.rb new file mode 100644 index 0000000..393fc30 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/reverse_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper_acceptance' + +describe 'reverse function' do + describe 'success' do + pp1 = <<-DOC + $a = "the public art galleries" + # Anagram: Large picture halls, I bet + $o = reverse($a) + notice(inline_template('reverse is <%= @o.inspect %>')) + DOC + it 'reverses strings' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{reverse is "seirellag tra cilbup eht"}) + end + end + end + describe 'failure' do + it 'handles no arguments' + it 'handles non strings or arrays' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/rstrip_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/rstrip_spec.rb new file mode 100644 index 0000000..60c5fff --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/rstrip_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper_acceptance' + +describe 'rstrip function' do + describe 'success' do + pp1 = <<-DOC + $a = [" the "," public "," art","galleries "] + # Anagram: Large picture halls, I bet + $o = rstrip($a) + notice(inline_template('rstrip is <%= @o.inspect %>')) + DOC + it 'rstrips arrays' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{rstrip is \[" the", " public", " art", "galleries"\]}) + end + end + + pp2 = <<-DOC + $a = " blowzy night-frumps vex'd jack q " + $o = rstrip($a) + notice(inline_template('rstrip is <%= @o.inspect %>')) + DOC + it 'rstrips strings' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{rstrip is " blowzy night-frumps vex'd jack q"}) + end + end + end + describe 'failure' do + it 'handles no arguments' + it 'handles non strings or arrays' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/shuffle_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/shuffle_spec.rb new file mode 100644 index 0000000..d8c03a2 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/shuffle_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper_acceptance' + +describe 'shuffle function' do + describe 'success' do + pp1 = <<-DOC + $a = ["1", "2", "3", "4", "5", "6", "7", "8", "the","public","art","galleries"] + # Anagram: Large picture halls, I bet + $o = shuffle($a) + notice(inline_template('shuffle is <%= @o.inspect %>')) + DOC + it 'shuffles arrays' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).not_to match(%r{shuffle is \["1", "2", "3", "4", "5", "6", "7", "8", "the", "public", "art", "galleries"\]}) + end + end + + pp2 = <<-DOC + $a = "blowzy night-frumps vex'd jack q" + $o = shuffle($a) + notice(inline_template('shuffle is <%= @o.inspect %>')) + DOC + it 'shuffles strings' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).not_to match(%r{shuffle is "blowzy night-frumps vex'd jack q"}) + end + end + end + describe 'failure' do + it 'handles no arguments' + it 'handles non strings or arrays' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/size_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/size_spec.rb new file mode 100644 index 0000000..015488d --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/size_spec.rb @@ -0,0 +1,53 @@ +require 'spec_helper_acceptance' + +describe 'size function' do + describe 'success' do + pp1 = <<-DOC + $a = 'discombobulate' + $o = size($a) + notice(inline_template('size is <%= @o.inspect %>')) + DOC + it 'single string size' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{size is 14}) + end + end + + pp2 = <<-DOC + $a = '' + $o = size($a) + notice(inline_template('size is <%= @o.inspect %>')) + DOC + it 'with empty string' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{size is 0}) + end + end + + pp3 = <<-DOC + $a = undef + $o = size($a) + notice(inline_template('size is <%= @o.inspect %>')) + DOC + it 'with undef' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{size is 0}) + end + end + + pp4 = <<-DOC + $a = ['discombobulate', 'moo'] + $o = size($a) + notice(inline_template('size is <%= @o.inspect %>')) + DOC + it 'strings in array' do + apply_manifest(pp4, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{size is 2}) + end + end + end + describe 'failure' do + it 'handles no arguments' + it 'handles non strings or arrays' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/sort_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/sort_spec.rb new file mode 100644 index 0000000..561fc68 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/sort_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper_acceptance' + +describe 'sort function' do + describe 'success' do + pp1 = <<-DOC + $a = ["the","public","art","galleries"] + # Anagram: Large picture halls, I bet + $o = sort($a) + notice(inline_template('sort is <%= @o.inspect %>')) + DOC + it 'sorts arrays' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{sort is \["art", "galleries", "public", "the"\]}) + end + end + + pp2 = <<-DOC + $a = "blowzy night-frumps vex'd jack q" + $o = sort($a) + notice(inline_template('sort is <%= @o.inspect %>')) + DOC + it 'sorts strings' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{sort is " '-abcdefghijklmnopqrstuvwxyz"}) + end + end + end + describe 'failure' do + it 'handles no arguments' + it 'handles non strings or arrays' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/squeeze_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/squeeze_spec.rb new file mode 100644 index 0000000..c3be9db --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/squeeze_spec.rb @@ -0,0 +1,44 @@ +require 'spec_helper_acceptance' + +describe 'squeeze function' do + describe 'success' do + pp1 = <<-DOC + # Real words! + $a = ["wallless", "laparohysterosalpingooophorectomy", "brrr", "goddessship"] + $o = squeeze($a) + notice(inline_template('squeeze is <%= @o.inspect %>')) + DOC + it 'squeezes arrays' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{squeeze is \["wales", "laparohysterosalpingophorectomy", "br", "godeship"\]}) + end + end + + it 'squeezez arrays with an argument' + pp2 = <<-DOC + $a = "wallless laparohysterosalpingooophorectomy brrr goddessship" + $o = squeeze($a) + notice(inline_template('squeeze is <%= @o.inspect %>')) + DOC + it 'squeezes strings' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{squeeze is "wales laparohysterosalpingophorectomy br godeship"}) + end + end + + pp3 = <<-DOC + $a = "countessship duchessship governessship hostessship" + $o = squeeze($a, 's') + notice(inline_template('squeeze is <%= @o.inspect %>')) + DOC + it 'squeezes strings with an argument' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{squeeze is "counteship ducheship governeship hosteship"}) + end + end + end + describe 'failure' do + it 'handles no arguments' + it 'handles non strings or arrays' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/str2bool_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/str2bool_spec.rb new file mode 100644 index 0000000..809456a --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/str2bool_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper_acceptance' + +describe 'str2bool function' do + describe 'success' do + pp = <<-DOC + $o = str2bool('y') + notice(inline_template('str2bool is <%= @o.inspect %>')) + DOC + it 'works with "y"' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{str2bool is true}) + end + end + it 'works with "Y"' + it 'works with "yes"' + it 'works with "1"' + it 'works with "true"' + it 'works with "n"' + it 'works with "N"' + it 'works with "no"' + it 'works with "0"' + it 'works with "false"' + it 'works with undef' + end + describe 'failure' do + it 'handles no arguments' + it 'handles non arrays or strings' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/str2saltedsha512_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/str2saltedsha512_spec.rb new file mode 100644 index 0000000..4e38e07 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/str2saltedsha512_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper_acceptance' + +describe 'str2saltedsha512 function' do + describe 'success' do + pp = <<-DOC + $o = str2saltedsha512('password') + notice(inline_template('str2saltedsha512 is <%= @o.inspect %>')) + DOC + it 'works with "y"' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{str2saltedsha512 is "[a-f0-9]{136}"}) + end + end + end + describe 'failure' do + it 'handles no arguments' + it 'handles more than one argument' + it 'handles non strings' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/strftime_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/strftime_spec.rb new file mode 100644 index 0000000..eec9a60 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/strftime_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper_acceptance' + +describe 'strftime function' do + describe 'success' do + pp = <<-DOC + $o = strftime('%C') + notice(inline_template('strftime is <%= @o.inspect %>')) + DOC + it 'gives the Century' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{strftime is "20"}) + end + end + it 'takes a timezone argument' + end + describe 'failure' do + it 'handles no arguments' + it 'handles invalid format strings' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/strip_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/strip_spec.rb new file mode 100644 index 0000000..5c4dfbd --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/strip_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper_acceptance' + +describe 'strip function' do + describe 'success' do + pp1 = <<-DOC + $a = [" the "," public "," art","galleries "] + # Anagram: Large picture halls, I bet + $o = strip($a) + notice(inline_template('strip is <%= @o.inspect %>')) + DOC + it 'strips arrays' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{strip is \["the", "public", "art", "galleries"\]}) + end + end + + pp2 = <<-DOC + $a = " blowzy night-frumps vex'd jack q " + $o = strip($a) + notice(inline_template('strip is <%= @o.inspect %>')) + DOC + it 'strips strings' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{strip is "blowzy night-frumps vex'd jack q"}) + end + end + end + describe 'failure' do + it 'handles no arguments' + it 'handles non strings or arrays' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/suffix_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/suffix_spec.rb new file mode 100644 index 0000000..6b04095 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/suffix_spec.rb @@ -0,0 +1,40 @@ +require 'spec_helper_acceptance' + +describe 'suffix function' do + describe 'success' do + pp1 = <<-DOC + $o = suffix(['a','b','c'],'p') + notice(inline_template('suffix is <%= @o.inspect %>')) + DOC + it 'suffixes array of values' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{suffix is \["ap", "bp", "cp"\]}) + end + end + + pp2 = <<-DOC + $o = suffix([],'p') + notice(inline_template('suffix is <%= @o.inspect %>')) + DOC + it 'suffixs with empty array' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{suffix is \[\]}) + end + end + + pp3 = <<-DOC + $o = suffix(['a','b','c'], undef) + notice(inline_template('suffix is <%= @o.inspect %>')) + DOC + it 'suffixs array of values with undef' do + apply_manifest(pp3, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{suffix is \["a", "b", "c"\]}) + end + end + end + describe 'failure' do + it 'fails with no arguments' + it 'fails when first argument is not array' + it 'fails when second argument is not string' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/swapcase_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/swapcase_spec.rb new file mode 100644 index 0000000..1d606f0 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/swapcase_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper_acceptance' + +describe 'swapcase function' do + describe 'success' do + pp = <<-DOC + $o = swapcase('aBcD') + notice(inline_template('swapcase is <%= @o.inspect %>')) + DOC + it 'works with strings' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{swapcase is "AbCd"}) + end + end + it 'works with arrays' + end + describe 'failure' do + it 'handles no arguments' + it 'handles non arrays or strings' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/time_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/time_spec.rb new file mode 100644 index 0000000..b0a4564 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/time_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper_acceptance' + +describe 'time function' do + describe 'success' do + pp1 = <<-DOC + $o = time() + notice(inline_template('time is <%= @o.inspect %>')) + DOC + it 'gives the time' do + apply_manifest(pp1, :catch_failures => true) do |r| + m = r.stdout.match(%r{time is (\d+)\D}) + # When I wrote this test + expect(Integer(m[1])).to be > 1_398_894_170 + end + end + + pp2 = <<-DOC + $o = time('UTC') + notice(inline_template('time is <%= @o.inspect %>')) + DOC + it 'takes a timezone argument' do + apply_manifest(pp2, :catch_failures => true) do |r| + m = r.stdout.match(%r{time is (\d+)\D}) + expect(Integer(m[1])).to be > 1_398_894_170 + end + end + end + describe 'failure' do + it 'handles more arguments' + it 'handles invalid timezones' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/to_bytes_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/to_bytes_spec.rb new file mode 100644 index 0000000..f042fe0 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/to_bytes_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper_acceptance' + +describe 'to_bytes function' do + describe 'success' do + pp = <<-DOC + $o = to_bytes('4 kB') + notice(inline_template('to_bytes is <%= @o.inspect %>')) + DOC + it 'converts kB to B' do + apply_manifest(pp, :catch_failures => true) do |r| + m = r.stdout.match(%r{to_bytes is (\d+)\D}) + expect(m[1]).to eq('4096') + end + end + it 'works without the B in unit' + it 'works without a space before unit' + it 'works without a unit' + it 'converts fractions' + end + describe 'failure' do + it 'handles no arguments' + it 'handles non integer arguments' + it 'handles unknown units like uB' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/try_get_value_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/try_get_value_spec.rb new file mode 100644 index 0000000..1112813 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/try_get_value_spec.rb @@ -0,0 +1,44 @@ +require 'spec_helper_acceptance' + +describe 'try_get_value function' do + describe 'success' do + pp1 = <<-DOC + $data = { + 'a' => { 'b' => 'passing'} + } + + $tests = try_get_value($data, 'a/b') + notice(inline_template('tests are <%= @tests.inspect %>')) + DOC + it 'gets a value' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{tests are "passing"}) + end + end + end + + describe 'failure' do + pp2 = <<-DOC + $data = { + 'a' => { 'b' => 'passing'} + } + + $tests = try_get_value($data, 'c/d', 'using the default value') + notice(inline_template('tests are <%= @tests.inspect %>')) + DOC + it 'uses a default value' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{using the default value}) + end + end + + pp = <<-DOC + $o = try_get_value() + DOC + it 'raises error on incorrect number of arguments' do + apply_manifest(pp, :expect_failures => true) do |r| + expect(r.stderr).to match(%r{wrong number of arguments}i) + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/type3x_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/type3x_spec.rb new file mode 100644 index 0000000..4b75548 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/type3x_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper_acceptance' + +describe 'type3x function' do + describe 'success' do + { + %{type3x({ 'a' => 'hash' })} => 'Hash', + %{type3x(['array'])} => 'Array', + %{type3x(false)} => 'Boolean', + %{type3x('asdf')} => 'String', + %{type3x(242)} => 'Integer', + %{type3x(3.14)} => 'Float', + }.each do |pp, type| + it "with type #{type}" do + apply_manifest(pp, :catch_failures => true) + end + end + end + + describe 'failure' do + pp_fail = <<-MANIFEST + type3x('one','two') + MANIFEST + it 'handles improper number of arguments' do + expect(apply_manifest(pp_fail, :expect_failures => true).stderr).to match(%r{Wrong number of arguments}) + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/type_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/type_spec.rb new file mode 100644 index 0000000..7b6e1fb --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/type_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper_acceptance' + +describe 'type function' do + describe 'success' do + pp1 = <<-DOC + $a = ["the","public","art","galleries"] + # Anagram: Large picture halls, I bet + $o = type($a) + notice(inline_template('type is <%= @o.to_s %>')) + DOC + it 'types arrays' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{type is Tuple\[String.*, String.*, String.*, String.*\]}) + end + end + + pp2 = <<-DOC + $a = "blowzy night-frumps vex'd jack q" + $o = type($a) + notice(inline_template('type is <%= @o.to_s %>')) + DOC + it 'types strings' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{type is String}) + end + end + it 'types hashes' + it 'types integers' + it 'types floats' + it 'types booleans' + end + describe 'failure' do + it 'handles no arguments' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/union_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/union_spec.rb new file mode 100644 index 0000000..e1b3d9a --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/union_spec.rb @@ -0,0 +1,23 @@ +require 'spec_helper_acceptance' + +describe 'union function' do + describe 'success' do + pp = <<-DOC + $a = ["the","public"] + $b = ["art"] + $c = ["galleries"] + # Anagram: Large picture halls, I bet + $o = union($a,$b,$c) + notice(inline_template('union is <%= @o.inspect %>')) + DOC + it 'unions arrays' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{union is \["the", "public", "art", "galleries"\]}) + end + end + end + describe 'failure' do + it 'handles no arguments' + it 'handles non arrays' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/unique_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/unique_spec.rb new file mode 100644 index 0000000..614eae5 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/unique_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper_acceptance' + +describe 'unique function' do + describe 'success' do + pp1 = <<-DOC + $a = ["wallless", "wallless", "brrr", "goddessship"] + $o = unique($a) + notice(inline_template('unique is <%= @o.inspect %>')) + DOC + it 'uniques arrays' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{unique is \["wallless", "brrr", "goddessship"\]}) + end + end + + pp2 = <<-DOC + $a = "wallless laparohysterosalpingooophorectomy brrr goddessship" + $o = unique($a) + notice(inline_template('unique is <%= @o.inspect %>')) + DOC + it 'uniques strings' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{unique is "wales prohytingcmbd"}) + end + end + end + describe 'failure' do + it 'handles no arguments' + it 'handles non strings or arrays' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/upcase_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/upcase_spec.rb new file mode 100644 index 0000000..a93c192 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/upcase_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper_acceptance' + +describe 'upcase function' do + describe 'success' do + pp1 = <<-DOC + $a = ["wallless", "laparohysterosalpingooophorectomy", "brrr", "goddessship"] + $o = upcase($a) + notice(inline_template('upcase is <%= @o.inspect %>')) + DOC + it 'upcases arrays' do + apply_manifest(pp1, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{upcase is \["WALLLESS", "LAPAROHYSTEROSALPINGOOOPHORECTOMY", "BRRR", "GODDESSSHIP"\]}) + end + end + + pp2 = <<-DOC + $a = "wallless laparohysterosalpingooophorectomy brrr goddessship" + $o = upcase($a) + notice(inline_template('upcase is <%= @o.inspect %>')) + DOC + it 'upcases strings' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{upcase is "WALLLESS LAPAROHYSTEROSALPINGOOOPHORECTOMY BRRR GODDESSSHIP"}) + end + end + end + describe 'failure' do + it 'handles no arguments' + it 'handles non strings or arrays' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/uriescape_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/uriescape_spec.rb new file mode 100644 index 0000000..9553625 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/uriescape_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper_acceptance' + +describe 'uriescape function' do + describe 'success' do + pp = <<-DOC + $a = ":/?#[]@!$&'()*+,;= \\\"{}" + $o = uriescape($a) + notice(inline_template('uriescape is <%= @o.inspect %>')) + DOC + it 'uriescape strings' do + apply_manifest(pp, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{uriescape is ":\/\?%23\[\]@!\$&'\(\)\*\+,;=%20%22%7B%7D"}) + end + end + it 'does nothing if a string is already safe' + end + describe 'failure' do + it 'handles no arguments' + it 'handles non strings or arrays' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/validate_absolute_path_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/validate_absolute_path_spec.rb new file mode 100644 index 0000000..3046459 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/validate_absolute_path_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper_acceptance' + +describe 'validate_absolute_path function' do + describe 'success' do + %w[ + C:/ + C:\\\\ + C:\\\\WINDOWS\\\\System32 + C:/windows/system32 + X:/foo/bar + X:\\\\foo\\\\bar + /var/tmp + /var/lib/puppet + /var/opt/../lib/puppet + ].each do |path| + pp = <<-DOC + $one = '#{path}' + validate_absolute_path($one) + DOC + it "validates a single argument #{path}" do + apply_manifest(pp, :catch_failures => true) + end + end + end + describe 'failure' do + it 'handles improper number of arguments' + it 'handles relative paths' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/validate_array_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/validate_array_spec.rb new file mode 100644 index 0000000..87016c2 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/validate_array_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper_acceptance' + +describe 'validate_array function' do + describe 'success' do + pp1 = <<-DOC + $one = ['a', 'b'] + validate_array($one) + DOC + it 'validates a single argument' do + apply_manifest(pp1, :catch_failures => true) + end + + pp2 = <<-DOC + $one = ['a', 'b'] + $two = [['c'], 'd'] + validate_array($one,$two) + DOC + it 'validates an multiple arguments' do + apply_manifest(pp2, :catch_failures => true) + end + [ + %{validate_array({'a' => 'hash' })}, + %{validate_array('string')}, + %{validate_array(false)}, + %{validate_array(undef)}, + ].each do |pp| + it "rejects #{pp.inspect}" do + expect(apply_manifest(pp, :expect_failures => true).stderr).to match(%r{is not an Array\. It looks to be a}) + end + end + end + describe 'failure' do + it 'handles improper number of arguments' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/validate_augeas_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/validate_augeas_spec.rb new file mode 100644 index 0000000..9a59f38 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/validate_augeas_spec.rb @@ -0,0 +1,61 @@ +require 'spec_helper_acceptance' + +describe 'validate_augeas function', :unless => (fact('osfamily') == 'windows') do + describe 'prep' do + it 'installs augeas for tests' + end + describe 'success' do + context 'with valid inputs with no 3rd argument' do + { + 'root:x:0:0:root:/root:/bin/bash\n' => 'Passwd.lns', + 'proc /proc proc nodev,noexec,nosuid 0 0\n' => 'Fstab.lns', + }.each do |line, lens| + pp1 = <<-DOC + $line = "#{line}" + $lens = "#{lens}" + validate_augeas($line, $lens) + DOC + it "validates a single argument for #{lens}" do + apply_manifest(pp1, :catch_failures => true) + end + end + end + + context 'with valid inputs with 3rd and 4th arguments' do + line = 'root:x:0:0:root:/root:/bin/barsh\n' + lens = 'Passwd.lns' + restriction = '$file/*[shell="/bin/barsh"]' + pp2 = <<-DOC + $line = "#{line}" + $lens = "#{lens}" + $restriction = ['#{restriction}'] + validate_augeas($line, $lens, $restriction, "my custom failure message") + DOC + it 'validates a restricted value' do + expect(apply_manifest(pp2, :expect_failures => true).stderr).to match(%r{my custom failure message}) + end + end + + context 'with invalid inputs' do + { + 'root:x:0:0:root' => 'Passwd.lns', + '127.0.1.1' => 'Hosts.lns', + }.each do |line, lens| + pp3 = <<-DOC + $line = "#{line}" + $lens = "#{lens}" + validate_augeas($line, $lens) + DOC + it "validates a single argument for #{lens}" do + apply_manifest(pp3, :expect_failures => true) + end + end + end + context 'with garbage inputs' do + it 'raises an error on invalid inputs' + end + end + describe 'failure' do + it 'handles improper number of arguments' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/validate_bool_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/validate_bool_spec.rb new file mode 100644 index 0000000..fb3ec14 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/validate_bool_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper_acceptance' + +describe 'validate_bool function' do + describe 'success' do + pp1 = <<-DOC + $one = true + validate_bool($one) + DOC + it 'validates a single argument' do + apply_manifest(pp1, :catch_failures => true) + end + + pp2 = <<-DOC + $one = true + $two = false + validate_bool($one,$two) + DOC + it 'validates an multiple arguments' do + apply_manifest(pp2, :catch_failures => true) + end + [ + %{validate_bool('true')}, + %{validate_bool('false')}, + %{validate_bool([true])}, + %{validate_bool(undef)}, + ].each do |pp3| + it "rejects #{pp3.inspect}" do + expect(apply_manifest(pp3, :expect_failures => true).stderr).to match(%r{is not a boolean\. It looks to be a}) + end + end + end + describe 'failure' do + it 'handles improper number of arguments' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/validate_cmd_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/validate_cmd_spec.rb new file mode 100644 index 0000000..a846b53 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/validate_cmd_spec.rb @@ -0,0 +1,50 @@ +require 'spec_helper_acceptance' + +describe 'validate_cmd function' do + describe 'success' do + pp1 = <<-DOC + $one = 'foo' + if $::osfamily == 'windows' { + $two = 'echo' #shell built-in + } else { + $two = '/bin/echo' + } + validate_cmd($one,$two) + DOC + it 'validates a true command' do + apply_manifest(pp1, :catch_failures => true) + end + + pp2 = <<-DOC + $one = 'foo' + if $::osfamily == 'windows' { + $two = 'C:/aoeu' + } else { + $two = '/bin/aoeu' + } + validate_cmd($one,$two) + DOC + it 'validates a fail command' do + apply_manifest(pp2, :expect_failures => true) + end + + pp3 = <<-DOC + $one = 'foo' + if $::osfamily == 'windows' { + $two = 'C:/aoeu' + } else { + $two = '/bin/aoeu' + } + validate_cmd($one,$two,"aoeu is dvorak") + DOC + it 'validates a fail command with a custom error message' do + apply_manifest(pp3, :expect_failures => true) do |output| + expect(output.stderr).to match(%r{aoeu is dvorak}) + end + end + end + describe 'failure' do + it 'handles improper number of arguments' + it 'handles improper argument types' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/validate_hash_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/validate_hash_spec.rb new file mode 100644 index 0000000..c349020 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/validate_hash_spec.rb @@ -0,0 +1,36 @@ +require 'spec_helper_acceptance' + +describe 'validate_hash function' do + describe 'success' do + pp1 = <<-DOC + $one = { 'a' => 1 } + validate_hash($one) + DOC + it 'validates a single argument' do + apply_manifest(pp1, :catch_failures => true) + end + + pp2 = <<-DOC + $one = { 'a' => 1 } + $two = { 'b' => 2 } + validate_hash($one,$two) + DOC + it 'validates an multiple arguments' do + apply_manifest(pp2, :catch_failures => true) + end + + [ + %{validate_hash('{ "not" => "hash" }')}, + %{validate_hash('string')}, + %{validate_hash(["array"])}, + %{validate_hash(undef)}, + ].each do |pp3| + it "rejects #{pp3.inspect}" do + expect(apply_manifest(pp3, :expect_failures => true).stderr).to match(%r{}) + end + end + end + describe 'failure' do + it 'handles improper number of arguments' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/validate_ipv4_address_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/validate_ipv4_address_spec.rb new file mode 100644 index 0000000..3ea165a --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/validate_ipv4_address_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper_acceptance' + +describe 'validate_ipv4_address function' do + describe 'success' do + pp1 = <<-DOC + $one = '1.2.3.4' + validate_ipv4_address($one) + DOC + it 'validates a single argument' do + apply_manifest(pp1, :catch_failures => true) + end + + pp2 = <<-DOC + $one = '1.2.3.4' + $two = '5.6.7.8' + validate_ipv4_address($one,$two) + DOC + it 'validates an multiple arguments' do + apply_manifest(pp2, :catch_failures => true) + end + end + describe 'failure' do + it 'handles improper number of arguments' + it 'handles ipv6 addresses' + it 'handles non-ipv4 strings' + it 'handles numbers' + it 'handles no arguments' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/validate_ipv6_address_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/validate_ipv6_address_spec.rb new file mode 100644 index 0000000..c329331 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/validate_ipv6_address_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper_acceptance' + +describe 'validate_ipv6_address function' do + describe 'success' do + pp1 = <<-DOC + $one = '3ffe:0505:0002::' + validate_ipv6_address($one) + DOC + it 'validates a single argument' do + apply_manifest(pp1, :catch_failures => true) + end + + pp2 = <<-DOC + $one = '3ffe:0505:0002::' + $two = '3ffe:0505:0001::' + validate_ipv6_address($one,$two) + DOC + it 'validates an multiple arguments' do + apply_manifest(pp2, :catch_failures => true) + end + end + describe 'failure' do + it 'handles improper number of arguments' + it 'handles ipv6 addresses' + it 'handles non-ipv6 strings' + it 'handles numbers' + it 'handles no arguments' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/validate_re_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/validate_re_spec.rb new file mode 100644 index 0000000..2d2291c --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/validate_re_spec.rb @@ -0,0 +1,46 @@ +require 'spec_helper_acceptance' + +describe 'validate_re function' do + describe 'success' do + pp1 = <<-DOC + $one = 'one' + $two = '^one$' + validate_re($one,$two) + DOC + it 'validates a string' do + apply_manifest(pp1, :catch_failures => true) + end + + pp2 = <<-DOC + $one = 'one' + $two = ['^one$', '^two'] + validate_re($one,$two) + DOC + it 'validates an array' do + apply_manifest(pp2, :catch_failures => true) + end + + pp3 = <<-DOC + $one = 'one' + $two = ['^two$', '^three'] + validate_re($one,$two) + DOC + it 'validates a failed array' do + apply_manifest(pp3, :expect_failures => true) + end + + pp4 = <<-DOC + $one = '3.4.3' + $two = '^2.7' + validate_re($one,$two,"The $puppetversion fact does not match 2.7") + DOC + it 'validates a failed array with a custom error message' do + expect(apply_manifest(pp4, :expect_failures => true).stderr).to match(%r{does not match}) + end + end + + describe 'failure' do + it 'handles improper number of arguments' + it 'handles improper argument types' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/validate_slength_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/validate_slength_spec.rb new file mode 100644 index 0000000..afbb97b --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/validate_slength_spec.rb @@ -0,0 +1,70 @@ +require 'spec_helper_acceptance' + +describe 'validate_slength function' do + describe 'success' do + pp1 = <<-DOC + $one = 'discombobulate' + $two = 17 + validate_slength($one,$two) + DOC + it 'validates a single string max' do + apply_manifest(pp1, :catch_failures => true) + end + + pp2 = <<-DOC + $one = ['discombobulate', 'moo'] + $two = 17 + validate_slength($one,$two) + DOC + it 'validates multiple string maxes' do + apply_manifest(pp2, :catch_failures => true) + end + + pp3 = <<-DOC + $one = ['discombobulate', 'moo'] + $two = 17 + $three = 3 + validate_slength($one,$two,$three) + DOC + it 'validates min/max of strings in array' do + apply_manifest(pp3, :catch_failures => true) + end + + pp4 = <<-DOC + $one = 'discombobulate' + $two = 1 + validate_slength($one,$two) + DOC + it 'validates a single string max of incorrect length' do + apply_manifest(pp4, :expect_failures => true) + end + + pp5 = <<-DOC + $one = ['discombobulate', 'moo'] + $two = 3 + validate_slength($one,$two) + DOC + it 'validates multiple string maxes of incorrect length' do + apply_manifest(pp5, :expect_failures => true) + end + + pp6 = <<-DOC + $one = ['discombobulate', 'moo'] + $two = 17 + $three = 10 + validate_slength($one,$two,$three) + DOC + it 'validates multiple strings min/maxes of incorrect length' do + apply_manifest(pp6, :expect_failures => true) + end + end + describe 'failure' do + it 'handles improper number of arguments' + it 'handles improper first argument type' + it 'handles non-strings in array of first argument' + it 'handles improper second argument type' + it 'handles improper third argument type' + it 'handles negative ranges' + it 'handles improper ranges' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/validate_string_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/validate_string_spec.rb new file mode 100644 index 0000000..d141f59 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/validate_string_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper_acceptance' + +describe 'validate_string function' do + describe 'success' do + pp1 = <<-DOC + $one = 'string' + validate_string($one) + DOC + it 'validates a single argument' do + apply_manifest(pp1, :catch_failures => true) + end + + pp2 = <<-DOC + $one = 'string' + $two = 'also string' + validate_string($one,$two) + DOC + it 'validates an multiple arguments' do + apply_manifest(pp2, :catch_failures => true) + end + + pp3 = <<-DOC + validate_string(undef) + DOC + it 'validates undef' do + apply_manifest(pp3, :catch_failures => true) + end + + { + %{validate_string({ 'a' => 'hash' })} => 'Hash', + %{validate_string(['array'])} => 'Array', + %{validate_string(false)} => 'FalseClass', + }.each do |pp4, type| + it "validates a non-string: #{pp4.inspect}" do + expect(apply_manifest(pp4, :expect_failures => true).stderr).to match(%r{a #{type}}) + end + end + end + describe 'failure' do + it 'handles improper number of arguments' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/values_at_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/values_at_spec.rb new file mode 100644 index 0000000..ffd6f4c --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/values_at_spec.rb @@ -0,0 +1,71 @@ +require 'spec_helper_acceptance' + +describe 'values_at function' do + describe 'success' do + pp1 = <<-DOC + $one = ['a','b','c','d','e'] + $two = 1 + $output = values_at($one,$two) + notice(inline_template('<%= @output.inspect %>')) + DOC + it 'returns a specific value' do + expect(apply_manifest(pp1, :catch_failures => true).stdout).to match(%r{\["b"\]}) + end + + pp2 = <<-DOC + $one = ['a','b','c','d','e'] + $two = -1 + $output = values_at($one,$two) + notice(inline_template('<%= @output.inspect %>')) + DOC + it 'returns a specific negative index value' do + pending("negative numbers don't work") + expect(apply_manifest(pp2, :catch_failures => true).stdout).to match(%r{\["e"\]}) + end + + pp3 = <<-DOC + $one = ['a','b','c','d','e'] + $two = "1-3" + $output = values_at($one,$two) + notice(inline_template('<%= @output.inspect %>')) + DOC + it 'returns a range of values' do + expect(apply_manifest(pp3, :catch_failures => true).stdout).to match(%r{\["b", "c", "d"\]}) + end + + pp4 = <<-DOC + $one = ['a','b','c','d','e'] + $two = ["1-3",0] + $output = values_at($one,$two) + notice(inline_template('<%= @output.inspect %>')) + DOC + it 'returns a negative specific value and range of values' do + expect(apply_manifest(pp4, :catch_failures => true).stdout).to match(%r{\["b", "c", "d", "a"\]}) + end + end + + describe 'failure' do + pp5 = <<-DOC + $one = ['a','b','c','d','e'] + $output = values_at($one) + notice(inline_template('<%= @output.inspect %>')) + DOC + it 'handles improper number of arguments' do + expect(apply_manifest(pp5, :expect_failures => true).stderr).to match(%r{Wrong number of arguments}) + end + + pp6 = <<-DOC + $one = ['a','b','c','d','e'] + $two = [] + $output = values_at($one,$two) + notice(inline_template('<%= @output.inspect %>')) + DOC + it 'handles non-indicies arguments' do + expect(apply_manifest(pp6, :expect_failures => true).stderr).to match(%r{at least one positive index}) + end + + it 'detects index ranges smaller than the start range' + it 'handles index ranges larger than array' + it 'handles non-integer indicies' + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/values_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/values_spec.rb new file mode 100644 index 0000000..b450dc7 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/values_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper_acceptance' + +describe 'values function', :if => Puppet::Util::Package.versioncmp(Puppet.version, '5.5.0') < 0 do + describe 'success' do + pp1 = <<-DOC + $arg = { + 'a' => 1, + 'b' => 2, + 'c' => 3, + } + $output = values($arg) + notice(inline_template('<%= @output.sort.inspect %>')) + DOC + it 'returns an array of values' do + expect(apply_manifest(pp1, :catch_failures => true).stdout).to match(%r{\[1, 2, 3\]}) + end + end + + describe 'failure' do + pp2 = <<-DOC + $arg = "foo" + $output = values($arg) + notice(inline_template('<%= @output.inspect %>')) + DOC + it 'handles non-hash arguments' do + expect(apply_manifest(pp2, :expect_failures => true).stderr).to match(%r{Requires hash}) + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/acceptance/zip_spec.rb b/code/environments/production/modules/stdlib/spec/acceptance/zip_spec.rb new file mode 100644 index 0000000..57adfa7 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/acceptance/zip_spec.rb @@ -0,0 +1,71 @@ +require 'spec_helper_acceptance' + +describe 'zip function' do + describe 'success' do + pp1 = <<-DOC + $one = [1,2,3,4] + $two = [5,6,7,8] + $output = zip($one,$two) + notice(inline_template('<%= @output.inspect %>')) + DOC + it 'zips two arrays of numbers together' do + expect(apply_manifest(pp1, :catch_failures => true).stdout).to match(%r{\[\[1, 5\], \[2, 6\], \[3, 7\], \[4, 8\]\]}) + end + + pp2 = <<-DOC + $one = [1,2,"three",4] + $two = [true,true,false,false] + $output = zip($one,$two) + notice(inline_template('<%= @output.inspect %>')) + DOC + it 'zips two arrays of numbers & bools together' do + expect(apply_manifest(pp2, :catch_failures => true).stdout).to match(%r{\[\[1, true\], \[2, true\], \["three", false\], \[4, false\]\]}) + end + + # XXX This only tests the argument `true`, even though the following are valid: + # 1 t y true yes + # 0 f n false no + # undef undefined + pp3 = <<-DOC + $one = [1,2,3,4] + $two = [5,6,7,8] + $output = zip($one,$two,true) + notice(inline_template('<%= @output.inspect %>')) + DOC + it 'zips two arrays of numbers together and flattens them' do + expect(apply_manifest(pp3, :catch_failures => true).stdout).to match(%r{\[1, 5, 2, 6, 3, 7, 4, 8\]}) + end + + # XXX Is this expected behavior? + pp4 = <<-DOC + $one = [1,2] + $two = [5,6,7,8] + $output = zip($one,$two) + notice(inline_template('<%= @output.inspect %>')) + DOC + it 'handles unmatched length' do + expect(apply_manifest(pp4, :catch_failures => true).stdout).to match(%r{\[\[1, 5\], \[2, 6\]\]}) + end + end + + describe 'failure' do + pp5 = <<-DOC + $one = [1,2] + $output = zip($one) + notice(inline_template('<%= @output.inspect %>')) + DOC + it 'handles improper number of arguments' do + expect(apply_manifest(pp5, :expect_failures => true).stderr).to match(%r{Wrong number of arguments}) + end + + pp6 = <<-DOC + $one = "a string" + $two = [5,6,7,8] + $output = zip($one,$two) + notice(inline_template('<%= @output.inspect %>')) + DOC + it 'handles improper argument types' do + expect(apply_manifest(pp6, :expect_failures => true).stderr).to match(%r{Requires array}) + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/fixtures/dscacheutil/root b/code/environments/production/modules/stdlib/spec/fixtures/dscacheutil/root new file mode 100644 index 0000000..1e34519 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/fixtures/dscacheutil/root @@ -0,0 +1,8 @@ +name: root +password: * +uid: 0 +gid: 0 +dir: /var/root +shell: /bin/bash +gecos: rawr Root + diff --git a/code/environments/production/modules/stdlib/spec/fixtures/lsuser/root b/code/environments/production/modules/stdlib/spec/fixtures/lsuser/root new file mode 100644 index 0000000..afd59ca --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/fixtures/lsuser/root @@ -0,0 +1,2 @@ +#name:home +root:/root diff --git a/code/environments/production/modules/stdlib/spec/fixtures/test/manifests/base32.pp b/code/environments/production/modules/stdlib/spec/fixtures/test/manifests/base32.pp new file mode 100644 index 0000000..5918633 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/fixtures/test/manifests/base32.pp @@ -0,0 +1,6 @@ +# Class to test the Stdlib::Base32 type alias +class test::base32 ( + Stdlib::Base32 $value, + ) { + notice('Success') +} diff --git a/code/environments/production/modules/stdlib/spec/fixtures/test/manifests/base64.pp b/code/environments/production/modules/stdlib/spec/fixtures/test/manifests/base64.pp new file mode 100644 index 0000000..d9e98d9 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/fixtures/test/manifests/base64.pp @@ -0,0 +1,6 @@ +# Class to test the Stdlib::Base64 type alias +class test::base64 ( + Stdlib::Base64 $value, + ) { + notice('Success') +} diff --git a/code/environments/production/modules/stdlib/spec/fixtures/test/manifests/deftype.pp b/code/environments/production/modules/stdlib/spec/fixtures/test/manifests/deftype.pp new file mode 100644 index 0000000..362d155 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/fixtures/test/manifests/deftype.pp @@ -0,0 +1,4 @@ +# Class to test deftype +define test::deftype( $param = 'foo' ) { + notify { "deftype: ${title}": } +} diff --git a/code/environments/production/modules/stdlib/spec/fixtures/test/manifests/ensure_resources.pp b/code/environments/production/modules/stdlib/spec/fixtures/test/manifests/ensure_resources.pp new file mode 100644 index 0000000..5f444c0 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/fixtures/test/manifests/ensure_resources.pp @@ -0,0 +1,4 @@ +# A helper class to test the ensure_resources function +class test::ensure_resources( $resource_type, $title_hash, $attributes_hash ) { + ensure_resources($resource_type, $title_hash, $attributes_hash) +} diff --git a/code/environments/production/modules/stdlib/spec/functions/abs_spec.rb b/code/environments/production/modules/stdlib/spec/functions/abs_spec.rb new file mode 100644 index 0000000..a151a7e --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/abs_spec.rb @@ -0,0 +1,45 @@ +require 'spec_helper' + +describe 'abs' do + it { is_expected.not_to eq(nil) } + + describe 'signature validation in puppet3', :unless => RSpec.configuration.puppet_future do + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('Current implementation ignores parameters after the first.') + is_expected.to run.with_params(1, 2).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + } + end + + describe 'signature validation in puppet4', :if => RSpec.configuration.puppet_future do + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params.and_raise_error(ArgumentError) + } + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params(1, 2).and_raise_error(ArgumentError) + } + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params([]).and_raise_error(ArgumentError) + } + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params({}).and_raise_error(ArgumentError) + } + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params(true).and_raise_error(ArgumentError) + } + end + + it { is_expected.to run.with_params(-34).and_return(34) } + it { is_expected.to run.with_params('-34').and_return(34) } + it { is_expected.to run.with_params(34).and_return(34) } + it { is_expected.to run.with_params('34').and_return(34) } + it { is_expected.to run.with_params(-34.5).and_return(34.5) } + it { is_expected.to run.with_params('-34.5').and_return(34.5) } + it { is_expected.to run.with_params(34.5).and_return(34.5) } + it { is_expected.to run.with_params('34.5').and_return(34.5) } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/any2array_spec.rb b/code/environments/production/modules/stdlib/spec/functions/any2array_spec.rb new file mode 100644 index 0000000..345adc2 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/any2array_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe 'any2array' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_return([]) } + it { is_expected.to run.with_params(true).and_return([true]) } + it { is_expected.to run.with_params('one').and_return(['one']) } + it { is_expected.to run.with_params('one', 'two').and_return(%w[one two]) } + it { is_expected.to run.with_params([]).and_return([]) } + it { is_expected.to run.with_params(['one']).and_return(['one']) } + it { is_expected.to run.with_params(%w[one two]).and_return(%w[one two]) } + it { is_expected.to run.with_params({}).and_return([]) } + it { is_expected.to run.with_params('key' => 'value').and_return(%w[key value]) } + + 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(['Ü']) } + it { is_expected.to run.with_params('∇').and_return(['∇']) } + it { is_expected.to run.with_params('€', '万', 'Ö', '♥', '割').and_return(['€', '万', 'Ö', '♥', '割']) } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/any2bool_spec.rb b/code/environments/production/modules/stdlib/spec/functions/any2bool_spec.rb new file mode 100644 index 0000000..eaead8f --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/any2bool_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +describe 'any2bool' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + + it { is_expected.to run.with_params(true).and_return(true) } + it { is_expected.to run.with_params(false).and_return(false) } + + it { is_expected.to run.with_params('1.5').and_return(true) } + + describe 'when testing stringy values that mean "true"' do + %w[TRUE 1 t y true yes].each do |value| + it { is_expected.to run.with_params(value).and_return(true) } + end + end + + describe 'when testing stringy values that mean "false"' do + ['FALSE', '', '0', 'f', 'n', 'false', 'no', 'undef', 'undefined', nil, :undef].each do |value| + it { is_expected.to run.with_params(value).and_return(false) } + end + end + + describe 'when testing numeric values that mean "true"' do + [1, '1', 1.5, '1.5'].each do |value| + it { is_expected.to run.with_params(value).and_return(true) } + end + end + + describe 'when testing numeric that mean "false"' do + [-1, '-1', -1.5, '-1.5', '0', 0].each do |value| + it { is_expected.to run.with_params(value).and_return(false) } + end + end + + describe 'everything else returns true' do + [[], {}, ['1'], [1], { :one => 1 }].each do |value| + it { is_expected.to run.with_params(value).and_return(true) } + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/assert_private_spec.rb b/code/environments/production/modules/stdlib/spec/functions/assert_private_spec.rb new file mode 100644 index 0000000..09abc77 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/assert_private_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +describe 'assert_private' do + context 'when called from inside module' do + it 'does not fail' do + scope.expects(:lookupvar).with('module_name').returns('foo') + scope.expects(:lookupvar).with('caller_module_name').returns('foo') + + is_expected.to run.with_params + end + end + + 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') + end + + it 'fails with a class error message' do + scope.source.expects(:name).returns('foo::baz') + scope.source.expects(:type).returns('hostclass') + + is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{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, %r{failure message!}) } + end + end + + context 'when called from private definition' do + it 'fails 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('definition') + + is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{Definition foo::baz is private}) + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/base64_spec.rb b/code/environments/production/modules/stdlib/spec/functions/base64_spec.rb new file mode 100644 index 0000000..dafab36 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/base64_spec.rb @@ -0,0 +1,60 @@ +require 'spec_helper' + +describe 'base64' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{first argument must be one of}) } + it { is_expected.to run.with_params('encode', ['two']).and_raise_error(Puppet::ParseError, %r{second argument must be a string}) } + it { is_expected.to run.with_params('encode', 2).and_raise_error(Puppet::ParseError, %r{second argument must be a string}) } + it { is_expected.to run.with_params('encode', 'thestring', 'three').and_raise_error(Puppet::ParseError, %r{third argument must be one of}) } + it { is_expected.to run.with_params('decode', "dGhlc3RyaW5n\n", 'strict').and_raise_error(ArgumentError) } + + it { is_expected.to run.with_params('encode', 'thestring').and_return("dGhlc3RyaW5n\n") } + it { is_expected.to run.with_params('decode', 'dGhlc3RyaW5n').and_return('thestring') } + it { is_expected.to run.with_params('decode', "dGhlc3RyaW5n\n").and_return('thestring') } + + it { is_expected.to run.with_params('encode', 'thestring', 'default').and_return("dGhlc3RyaW5n\n") } + it { is_expected.to run.with_params('decode', 'dGhlc3RyaW5n', 'default').and_return('thestring') } + it { is_expected.to run.with_params('decode', "dGhlc3RyaW5n\n", 'default').and_return('thestring') } + + it { is_expected.to run.with_params('encode', 'thestring', 'strict').and_return('dGhlc3RyaW5n') } + it { is_expected.to run.with_params('decode', 'dGhlc3RyaW5n', 'strict').and_return('thestring') } + + it { + is_expected.to run.with_params('encode', 'a very long string that will cause the base64 encoder to produce output with multiple lines') + .and_return("YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0\nIGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5l\ncw==\n") + } + it { + is_expected.to run.with_params('decode', "YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0\nIGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5l\ncw==\n") + .and_return('a very long string that will cause the base64 encoder to produce output with multiple lines') + } + it { + is_expected.to run.with_params('decode', 'YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0IGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5lcw==') + .and_return('a very long string that will cause the base64 encoder to produce output with multiple lines') + } + it { + is_expected.to run.with_params('encode', 'a very long string that will cause the base64 encoder to produce output with multiple lines', 'strict') + .and_return('YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0IGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5lcw==') + } + it { + is_expected.to run.with_params('decode', 'YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0IGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5lcw==', 'strict') + .and_return('a very long string that will cause the base64 encoder to produce output with multiple lines') + } + it { + is_expected.to run.with_params('encode', 'https://www.google.com.tw/?gws_rd=ssl#q=hello+world', 'urlsafe') + .and_return('aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS50dy8_Z3dzX3JkPXNzbCNxPWhlbGxvK3dvcmxk') + } + it { + is_expected.to run.with_params('decode', 'aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS50dy8_Z3dzX3JkPXNzbCNxPWhlbGxvK3dvcmxk', 'urlsafe') + .and_return('https://www.google.com.tw/?gws_rd=ssl#q=hello+world') + } + it { + is_expected.to run.with_params('encode', 'https://github.com/puppetlabs/puppetlabs-stdlib/pulls?utf8=%E2%9C%93&q=is%3Apr+is%3Aopen+Add', 'urlsafe') + .and_return('aHR0cHM6Ly9naXRodWIuY29tL3B1cHBldGxhYnMvcHVwcGV0bGFicy1zdGRsaWIvcHVsbHM_dXRmOD0lRTIlOUMlOTMmcT1pcyUzQXByK2lzJTNBb3BlbitBZGQ=') + } + it { + is_expected.to run.with_params('decode', 'aHR0cHM6Ly9naXRodWIuY29tL3B1cHBldGxhYnMvcHVwcGV0bGFicy1zdGRsaWIvcHVsbHM_dXRmOD0lRTIlOUMlOTMmcT1pcyUzQXByK2lzJTNBb3BlbitBZGQ=', 'urlsafe') + .and_return('https://github.com/puppetlabs/puppetlabs-stdlib/pulls?utf8=%E2%9C%93&q=is%3Apr+is%3Aopen+Add') + } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/basename_spec.rb b/code/environments/production/modules/stdlib/spec/functions/basename_spec.rb new file mode 100644 index 0000000..6957133 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/basename_spec.rb @@ -0,0 +1,19 @@ +require 'spec_helper' + +describe 'basename' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('/path/to/a/file.ext', []).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('/path/to/a/file.ext').and_return('file.ext') } + it { is_expected.to run.with_params('relative_path/to/a/file.ext').and_return('file.ext') } + it { is_expected.to run.with_params('/path/to/a/file.ext', '.ext').and_return('file') } + it { is_expected.to run.with_params('relative_path/to/a/file.ext', '.ext').and_return('file') } + it { is_expected.to run.with_params('scheme:///path/to/a/file.ext').and_return('file.ext') } + + context 'with UTF8 and double byte characters' do + it { is_expected.to run.with_params('scheme:///√ạĺűē/竹.ext').and_return('竹.ext') } + it { is_expected.to run.with_params('ҝẽγ:/√ạĺűē/竹.ㄘ', '.ㄘ').and_return('竹') } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/bool2num_spec.rb b/code/environments/production/modules/stdlib/spec/functions/bool2num_spec.rb new file mode 100644 index 0000000..3ba5e2c --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/bool2num_spec.rb @@ -0,0 +1,14 @@ +require 'spec_helper' + +describe 'bool2num' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + + [true, 'true', AlsoString.new('true')].each do |truthy| + it { is_expected.to run.with_params(truthy).and_return(1) } + end + + [false, 'false', AlsoString.new('false')].each do |falsey| + it { is_expected.to run.with_params(falsey).and_return(0) } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/bool2str_spec.rb b/code/environments/production/modules/stdlib/spec/functions/bool2str_spec.rb new file mode 100644 index 0000000..8d8f9b5 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/bool2str_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe 'bool2str' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + ['true', 'false', nil, :undef, ''].each do |invalid| + it { is_expected.to run.with_params(invalid).and_raise_error(Puppet::ParseError) } + end + it { is_expected.to run.with_params(true, 'yes', 'no', 'maybe').and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params(true, 'maybe').and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params(true, 0, 1).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params(true).and_return('true') } + it { is_expected.to run.with_params(false).and_return('false') } + it { is_expected.to run.with_params(true, 'yes', 'no').and_return('yes') } + it { is_expected.to run.with_params(false, 'yes', 'no').and_return('no') } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/camelcase_spec.rb b/code/environments/production/modules/stdlib/spec/functions/camelcase_spec.rb new file mode 100644 index 0000000..892e1da --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/camelcase_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' + +describe 'camelcase' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params(100).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('abc').and_return('Abc') } + it { is_expected.to run.with_params('aa_bb_cc').and_return('AaBbCc') } + it { is_expected.to run.with_params('_aa__bb__cc_').and_return('AaBbCc') } + it { is_expected.to run.with_params('100').and_return('100') } + it { is_expected.to run.with_params('1_00').and_return('100') } + 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([]) } + it { is_expected.to run.with_params(%w[abc aa_bb_cc]).and_return(%w[Abc AaBbCc]) } + it { is_expected.to run.with_params(['abc', 1, 'aa_bb_cc']).and_return(['Abc', 1, 'AaBbCc']) } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/capitalize_spec.rb b/code/environments/production/modules/stdlib/spec/functions/capitalize_spec.rb new file mode 100644 index 0000000..15c2acf --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/capitalize_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +describe 'capitalize' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params(100).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('one').and_return('One') } + it { is_expected.to run.with_params('one two').and_return('One two') } + it { is_expected.to run.with_params('ONE TWO').and_return('One two') } + + it { is_expected.to run.with_params(AlsoString.new('one')).and_return('One') } + it { is_expected.to run.with_params([]).and_return([]) } + it { is_expected.to run.with_params(%w[one two]).and_return(%w[One Two]) } + it { is_expected.to run.with_params(['one', 1, 'two']).and_return(['One', 1, 'Two']) } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/ceiling_spec.rb b/code/environments/production/modules/stdlib/spec/functions/ceiling_spec.rb new file mode 100644 index 0000000..0682720 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/ceiling_spec.rb @@ -0,0 +1,12 @@ +require 'spec_helper' + +describe 'ceiling' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('foo').and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params(34).and_return(34) } + it { is_expected.to run.with_params(-34).and_return(-34) } + it { is_expected.to run.with_params(33.1).and_return(34) } + it { is_expected.to run.with_params(-33.1).and_return(-33) } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/chomp_spec.rb b/code/environments/production/modules/stdlib/spec/functions/chomp_spec.rb new file mode 100644 index 0000000..aabf596 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/chomp_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper' + +describe 'chomp' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) } + it { + pending('Current implementation ignores parameters after the first.') + is_expected.to run.with_params('a', 'b').and_raise_error(Puppet::ParseError) + } + it { is_expected.to run.with_params('one').and_return('one') } + it { is_expected.to run.with_params("one\n").and_return('one') } + it { is_expected.to run.with_params("one\n\n").and_return("one\n") } + it { is_expected.to run.with_params(%W[one\n two three\n]).and_return(%w[one two three]) } + + it { is_expected.to run.with_params(AlsoString.new('one')).and_return('one') } + it { is_expected.to run.with_params(AlsoString.new("one\n")).and_return('one') } + it { is_expected.to run.with_params(AlsoString.new("one\n\n")).and_return("one\n") } + it { is_expected.to run.with_params([AlsoString.new("one\n"), AlsoString.new('two'), "three\n"]).and_return(%w[one two three]) } + + context 'with UTF8 and double byte characters' do + it { is_expected.to run.with_params("ůťƒ8\n\n").and_return("ůťƒ8\n") } + it { is_expected.to run.with_params("ネット\n\n").and_return("ネット\n") } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/chop_spec.rb b/code/environments/production/modules/stdlib/spec/functions/chop_spec.rb new file mode 100644 index 0000000..7b7c597 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/chop_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper' + +describe 'chop' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) } + it { + pending('Current implementation ignores parameters after the first.') + is_expected.to run.with_params('a', 'b').and_raise_error(Puppet::ParseError) + } + it { is_expected.to run.with_params('one').and_return('on') } + it { is_expected.to run.with_params("one\n").and_return('one') } + it { is_expected.to run.with_params("one\n\n").and_return("one\n") } + it { is_expected.to run.with_params(%W[one\n two three\n]).and_return(%w[one tw three]) } + + it { is_expected.to run.with_params(AlsoString.new('one')).and_return('on') } + it { is_expected.to run.with_params(AlsoString.new("one\n")).and_return('one') } + it { is_expected.to run.with_params(AlsoString.new("one\n\n")).and_return("one\n") } + it { is_expected.to run.with_params([AlsoString.new("one\n"), AlsoString.new('two'), "three\n"]).and_return(%w[one tw three]) } + + context 'with UTF8 and double byte characters' do + it { is_expected.to run.with_params("ůťƒ8\n\n").and_return("ůťƒ8\n") } + it { is_expected.to run.with_params("ネット\n\n").and_return("ネット\n") } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/clamp_spec.rb b/code/environments/production/modules/stdlib/spec/functions/clamp_spec.rb new file mode 100644 index 0000000..7f0de34 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/clamp_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe 'clamp' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(ArgumentError) } + it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params(12, 88, 71, 190).and_raise_error(Puppet::ParseError, %r{Wrong number of arguments, need three to clamp}) } + it { is_expected.to run.with_params('12string', 88, 15).and_raise_error(Puppet::ParseError, %r{Required explicit numeric}) } + it { is_expected.to run.with_params(1, 2, 'a' => 55).and_raise_error(Puppet::ParseError, %r{The Hash type is not allowed}) } + it { is_expected.to run.with_params('24', [575, 187]).and_return(187) } + it { is_expected.to run.with_params([4, 3, '99']).and_return(4) } + it { is_expected.to run.with_params(16, 750, 88).and_return(88) } + it { is_expected.to run.with_params([3, 873], 73).and_return(73) } + it { is_expected.to run.with_params([4], 8, 75).and_return(8) } + it { is_expected.to run.with_params([6], [31], 9911).and_return(31) } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/concat_spec.rb b/code/environments/production/modules/stdlib/spec/functions/concat_spec.rb new file mode 100644 index 0000000..c9f2ae5 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/concat_spec.rb @@ -0,0 +1,28 @@ +require 'spec_helper' + +describe 'concat' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params([1]).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params(1, [2]).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params([1], [2], [3]).and_return([1, 2, 3]) } + it { is_expected.to run.with_params(%w[1 2 3], %w[4 5 6]).and_return(%w[1 2 3 4 5 6]) } + it { is_expected.to run.with_params(%w[1 2 3], '4').and_return(%w[1 2 3 4]) } + it { is_expected.to run.with_params(%w[1 2 3], [%w[4 5], '6']).and_return(['1', '2', '3', %w[4 5], '6']) } + it { is_expected.to run.with_params(%w[1 2], %w[3 4], %w[5 6]).and_return(%w[1 2 3 4 5 6]) } + it { is_expected.to run.with_params(%w[1 2], '3', '4', %w[5 6]).and_return(%w[1 2 3 4 5 6]) } + + context 'with UTF8 and double byte characters' do + it { is_expected.to run.with_params([{ 'a' => 'b' }], 'c' => 'd', 'e' => 'f').and_return([{ 'a' => 'b' }, { 'c' => 'd', 'e' => 'f' }]) } + it { is_expected.to run.with_params(['ấ', 'β', '©'], %w[đ ể 文字列]).and_return(['ấ', 'β', '©', 'đ', 'ể', '文字列']) } + end + + arguments = [%w[1 2 3], %w[4 5 6]] + originals = [arguments[0].dup, arguments[1].dup] + it 'leaves the original array intact' do + _result = subject.call([arguments[0], arguments[1]]) + arguments.each_with_index do |argument, index| + expect(argument).to eq(originals[index]) + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/convert_base_spec.rb b/code/environments/production/modules/stdlib/spec/functions/convert_base_spec.rb new file mode 100644 index 0000000..d1a6cef --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/convert_base_spec.rb @@ -0,0 +1,24 @@ +require 'spec_helper' + +describe 'convert_base' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('asdf').and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('asdf', 'moo', 'cow').and_raise_error(ArgumentError) } + it { is_expected.to run.with_params(['1'], '2').and_raise_error(Puppet::ParseError, %r{argument must be either a string or an integer}) } + it { is_expected.to run.with_params('1', ['2']).and_raise_error(Puppet::ParseError, %r{argument must be either a string or an integer}) } + it { is_expected.to run.with_params('1', 1).and_raise_error(Puppet::ParseError, %r{base must be at least 2 and must not be greater than 36}) } + it { is_expected.to run.with_params('1', 37).and_raise_error(Puppet::ParseError, %r{base must be at least 2 and must not be greater than 36}) } + + it 'raises a ParseError if argument 1 is a string that does not correspond to an integer in base 10' do + is_expected.to run.with_params('ten', 6).and_raise_error(Puppet::ParseError, %r{argument must be an integer or a string corresponding to an integer in base 10}) + end + + it 'raises a ParseError if argument 2 is a string and does not correspond to an integer in base 10' do + is_expected.to run.with_params(100, 'hex').and_raise_error(Puppet::ParseError, %r{argument must be an integer or a string corresponding to an integer in base 10}) + end + + it { is_expected.to run.with_params('11', '16').and_return('b') } + it { is_expected.to run.with_params('35', '36').and_return('z') } + it { is_expected.to run.with_params(5, 2).and_return('101') } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/count_spec.rb b/code/environments/production/modules/stdlib/spec/functions/count_spec.rb new file mode 100644 index 0000000..9f5d544 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/count_spec.rb @@ -0,0 +1,23 @@ +require 'spec_helper' + +describe 'count' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('one').and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('one', 'two').and_return(1) } + it { + pending('should actually be like this, and not like above') + is_expected.to run.with_params('one', 'two').and_raise_error(ArgumentError) + } + it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(ArgumentError) } + it { is_expected.to run.with_params(%w[one two three]).and_return(3) } + it { is_expected.to run.with_params(%w[one two two], 'two').and_return(2) } + it { is_expected.to run.with_params(['one', nil, 'two']).and_return(2) } + it { is_expected.to run.with_params(['one', '', 'two']).and_return(2) } + it { is_expected.to run.with_params(['one', :undef, 'two']).and_return(2) } + + it { is_expected.to run.with_params(['ổņ℮', 'ŧщộ', 'three']).and_return(3) } + it { is_expected.to run.with_params(['ổņ℮', 'ŧщộ', 'ŧщộ'], 'ŧщộ').and_return(2) } + it { is_expected.to run.with_params(['ổņ℮', nil, 'ŧщộ']).and_return(2) } + it { is_expected.to run.with_params(['ổņ℮', :undef, 'ŧщộ']).and_return(2) } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/deep_merge_spec.rb b/code/environments/production/modules/stdlib/spec/functions/deep_merge_spec.rb new file mode 100644 index 0000000..d09bde0 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/deep_merge_spec.rb @@ -0,0 +1,58 @@ +require 'spec_helper' + +describe 'deep_merge' do + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('key' => 'value').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params({}, '2').and_raise_error(Puppet::ParseError, %r{unexpected argument type String}) } + it { is_expected.to run.with_params({}, 2).and_raise_error(Puppet::ParseError, %r{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({}) } + it { is_expected.to run.with_params({}, {}, {}, {}).and_return({}) } + it { is_expected.to run.with_params({ 'key' => 'value' }, '').and_return('key' => 'value') } + it { is_expected.to run.with_params({ 'key1' => 'value1' }, 'key2' => 'value2').and_return('key1' => 'value1', 'key2' => 'value2') } + + describe 'when arguments have key collisions' do + it 'prefers values from the last hash' do + is_expected.to run \ + .with_params({ 'key1' => 'value1', 'key2' => 'value2' }, 'key2' => 'replacement_value', 'key3' => 'value3') \ + .and_return('key1' => 'value1', 'key2' => 'replacement_value', 'key3' => 'value3') + end + it { + is_expected.to run \ + .with_params({ 'key1' => 'value1' }, { 'key1' => 'value2' }, 'key1' => 'value3') \ + .and_return('key1' => 'value3') + } + end + + describe 'when arguments have subhashes' do + it { + is_expected.to run \ + .with_params({ 'key1' => 'value1' }, 'key2' => 'value2', 'key3' => { 'subkey1' => 'value4' }) \ + .and_return('key1' => 'value1', 'key2' => 'value2', 'key3' => { 'subkey1' => 'value4' }) + } + it { + is_expected.to run \ + .with_params({ 'key1' => { 'subkey1' => 'value1' } }, 'key1' => { 'subkey2' => 'value2' }) \ + .and_return('key1' => { 'subkey1' => 'value1', 'subkey2' => 'value2' }) + } + it { + is_expected.to run \ + .with_params({ 'key1' => { 'subkey1' => { 'subsubkey1' => 'value1' } } }, 'key1' => { 'subkey1' => { 'subsubkey1' => 'value2' } }) \ + .and_return('key1' => { 'subkey1' => { 'subsubkey1' => 'value2' } }) + } + end + + arguments = { 'key1' => 'value1' }, { 'key2' => 'value2' } + originals = [arguments[0].dup, arguments[1].dup] + it 'does not change the original hashes' do + subject.call([arguments[0], arguments[1]]) + arguments.each_with_index do |argument, index| + expect(argument).to eq(originals[index]) + end + end + + context 'with UTF8 and double byte characters' do + it { is_expected.to run.with_params({ 'ĸέỹ1' => 'ϋǻļủë1' }, 'この文字列' => '万').and_return('ĸέỹ1' => 'ϋǻļủë1', 'この文字列' => '万') } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/defined_with_params_spec.rb b/code/environments/production/modules/stdlib/spec/functions/defined_with_params_spec.rb new file mode 100644 index 0000000..d404ba9 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/defined_with_params_spec.rb @@ -0,0 +1,72 @@ +require 'spec_helper' + +describe 'defined_with_params' do + describe 'when no resource is specified' do + it { is_expected.to run.with_params.and_raise_error(ArgumentError) } + end + describe 'when compared against a resource with no attributes' do + let :pre_condition do + 'user { "dan": }' + end + + it { is_expected.to run.with_params('User[dan]', {}).and_return(true) } + it { is_expected.to run.with_params('User[bob]', {}).and_return(false) } + it { is_expected.to run.with_params('User[dan]', 'foo' => 'bar').and_return(false) } + + context 'with UTF8 and double byte characters' do + it { is_expected.to run.with_params('User[ĵĭмოү]', {}).and_return(false) } + it { is_expected.to run.with_params('User[ポーラ]', {}).and_return(false) } + end + end + + describe 'when compared against a resource with attributes' do + let :pre_condition do + 'user { "dan": ensure => present, shell => "/bin/csh", managehome => false}' + end + + it { is_expected.to run.with_params('User[dan]', {}).and_return(true) } + it { is_expected.to run.with_params('User[dan]', '').and_return(true) } + it { is_expected.to run.with_params('User[dan]', 'ensure' => 'present').and_return(true) } + it { is_expected.to run.with_params('User[dan]', 'ensure' => 'present', 'managehome' => false).and_return(true) } + it { is_expected.to run.with_params('User[dan]', 'ensure' => 'absent', 'managehome' => false).and_return(false) } + end + + describe 'when passing undef values' do + let :pre_condition do + 'file { "/tmp/a": ensure => present }' + end + + it { is_expected.to run.with_params('File[/tmp/a]', {}).and_return(true) } + it { is_expected.to run.with_params('File[/tmp/a]', 'ensure' => 'present', 'owner' => :undef).and_return(true) } + end + + describe 'when the reference is a' do + let :pre_condition do + 'user { "dan": }' + end + + context 'with reference' do + it { is_expected.to run.with_params(Puppet::Resource.new('User[dan]'), {}).and_return(true) } + end + if Puppet::Util::Package.versioncmp(Puppet.version, '4.6.0') >= 0 + context 'with array' do + it 'fails' do + expect { + subject.call([['User[dan]'], {}]) + }.to raise_error ArgumentError, %r{not understood: 'Array'} + end + end + end + end + + describe 'when passed a defined type' do + let :pre_condition do + 'test::deftype { "foo": }' + end + + it { is_expected.to run.with_params('Test::Deftype[foo]', {}).and_return(true) } + it { is_expected.to run.with_params('Test::Deftype[bar]', {}).and_return(false) } + it { is_expected.to run.with_params(Puppet::Resource.new('Test::Deftype[foo]'), {}).and_return(true) } + it { is_expected.to run.with_params(Puppet::Resource.new('Test::Deftype[bar]'), {}).and_return(false) } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/delete_at_spec.rb b/code/environments/production/modules/stdlib/spec/functions/delete_at_spec.rb new file mode 100644 index 0000000..8889fbc --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/delete_at_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper' + +describe 'delete_at' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('one', 1).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params(1, 1).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params(['one'], 'two').and_raise_error(Puppet::ParseError) } + it { + pending('Current implementation ignores parameters after the first two.') + is_expected.to run.with_params(['one'], 0, 1).and_raise_error(Puppet::ParseError) + } + + describe 'argument validation' do + it { is_expected.to run.with_params([0, 1, 2], 3).and_raise_error(Puppet::ParseError) } + end + + it { is_expected.to run.with_params([0, 1, 2], 1).and_return([0, 2]) } + it { is_expected.to run.with_params([0, 1, 2], -1).and_return([0, 1]) } + it { is_expected.to run.with_params([0, 1, 2], -4).and_return([0, 1, 2]) } + it { is_expected.to run.with_params(%w[ƒờở βāř ьầż], 1).and_return(%w[ƒờở ьầż]) } + + it 'leaves the original array intact' do + argument = [1, 2, 3] + original = argument.dup + _result = subject.call([argument, 2]) + expect(argument).to eq(original) + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/delete_regex_spec.rb b/code/environments/production/modules/stdlib/spec/functions/delete_regex_spec.rb new file mode 100644 index 0000000..1e1cc2c --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/delete_regex_spec.rb @@ -0,0 +1,56 @@ +require 'spec_helper' + +describe 'delete_regex' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params([], 'two') } + it { is_expected.to run.with_params({}, 'two') } + it { is_expected.to run.with_params([], 'two', 'three').and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params([], 'two', 'three', 'four').and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params(1, 'two').and_raise_error(TypeError) } + + describe 'deleting from an array' do + it { is_expected.to run.with_params([], '').and_return([]) } + it { is_expected.to run.with_params([], 'two').and_return([]) } + it { is_expected.to run.with_params(['two'], 'two').and_return([]) } + it { is_expected.to run.with_params(%w[two two], 'two').and_return([]) } + it { is_expected.to run.with_params(%w[one two three], '^t.*').and_return(['one']) } + it { is_expected.to run.with_params(%w[ab b c b], 'b').and_return(%w[ab c]) } + it { is_expected.to run.with_params(%w[one two three], 'four').and_return(%w[one two three]) } + it { is_expected.to run.with_params(%w[one two three], 'e').and_return(%w[one two three]) } + it { is_expected.to run.with_params(%w[one two three], 'two').and_return(%w[one three]) } + it { is_expected.to run.with_params(%w[two one two three two], 'two').and_return(%w[one three]) } + it { is_expected.to run.with_params(['abracadabra'], 'abr').and_return(['abracadabra']) } + it { is_expected.to run.with_params(['abracadabra'], '^.*jimbob.*$').and_return(['abracadabra']) } + end + + describe 'deleting from an array' do + it { is_expected.to run.with_params({}, '').and_return({}) } + it { is_expected.to run.with_params({}, 'key').and_return({}) } + it { is_expected.to run.with_params({ 'key' => 'value' }, 'key').and_return({}) } + it { + is_expected.to run \ + .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, 'key2') \ + .and_return('key1' => 'value1', 'key3' => 'value3') + } + it { + is_expected.to run \ + .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, %w[key1 key2]) \ + .and_return('key3' => 'value3') + } + end + + it 'leaves the original array intact' do + argument1 = %w[one two three] + original1 = argument1.dup + subject.call([argument1, 'two']) + expect(argument1).to eq(original1) + end + it 'leaves the original hash intact' do + argument1 = { 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' } + original1 = argument1.dup + subject.call([argument1, 'key2']) + expect(argument1).to eq(original1) + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/delete_spec.rb b/code/environments/production/modules/stdlib/spec/functions/delete_spec.rb new file mode 100644 index 0000000..fa2f1b8 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/delete_spec.rb @@ -0,0 +1,79 @@ +require 'spec_helper' + +describe 'delete' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params([], 'two') } + it { is_expected.to run.with_params([], 'two', 'three').and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params(1, 'two').and_raise_error(TypeError) } + + describe 'deleting from an array' do + it { is_expected.to run.with_params([], '').and_return([]) } + it { is_expected.to run.with_params([], 'two').and_return([]) } + it { is_expected.to run.with_params(['two'], 'two').and_return([]) } + it { is_expected.to run.with_params(%w[two two], 'two').and_return([]) } + it { is_expected.to run.with_params(%w[ab b c b], 'b').and_return(%w[ab c]) } + it { is_expected.to run.with_params(%w[one two three], 'four').and_return(%w[one two three]) } + it { is_expected.to run.with_params(%w[one two three], 'e').and_return(%w[one two three]) } + it { is_expected.to run.with_params(%w[one two three], 'two').and_return(%w[one three]) } + it { is_expected.to run.with_params(%w[two one two three two], 'two').and_return(%w[one three]) } + it { is_expected.to run.with_params(%w[one two three two], %w[one two]).and_return(['three']) } + it { is_expected.to run.with_params(['ồאּẻ', 'ŧẅơ', 'ŧңŗё℮', 'ŧẅơ'], %w[ồאּẻ ŧẅơ]).and_return(['ŧңŗё℮']) } + end + + describe 'deleting from a string' do + it { is_expected.to run.with_params('', '').and_return('') } + it { is_expected.to run.with_params('bar', '').and_return('bar') } + it { is_expected.to run.with_params('', 'bar').and_return('') } + it { is_expected.to run.with_params('bar', 'bar').and_return('') } + it { is_expected.to run.with_params('barbar', 'bar').and_return('') } + it { is_expected.to run.with_params('barfoobar', 'bar').and_return('foo') } + it { is_expected.to run.with_params('foobarbabarz', 'bar').and_return('foobaz') } + it { is_expected.to run.with_params('foobarbabarz', %w[foo bar]).and_return('baz') } + it { is_expected.to run.with_params('ƒōōβậяβậβậяź', %w[ƒōō βậя]).and_return('βậź') } + + it { is_expected.to run.with_params('barfoobar', %w[barbar foo]).and_return('barbar') } + it { is_expected.to run.with_params('barfoobar', %w[foo barbar]).and_return('') } + end + + describe 'deleting from an array' do + it { is_expected.to run.with_params({}, '').and_return({}) } + it { is_expected.to run.with_params({}, 'key').and_return({}) } + it { is_expected.to run.with_params({ 'key' => 'value' }, 'key').and_return({}) } + it { + is_expected.to run \ + .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, 'key2') \ + .and_return('key1' => 'value1', 'key3' => 'value3') + } + it { + is_expected.to run \ + .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, %w[key1 key2]) \ + .and_return('key3' => 'value3') + } + it { + is_expected.to run \ + .with_params({ 'ĸəұ1' => 'νãŀủĕ1', 'ĸəұ2' => 'νãŀủĕ2', 'ĸəұ3' => 'νãŀủĕ3' }, %w[ĸəұ1 ĸəұ2]) \ + .and_return('ĸəұ3' => 'νãŀủĕ3') + } + end + + it 'leaves the original array intact' do + argument1 = %w[one two three] + original1 = argument1.dup + _result = subject.call([argument1, 'two']) + expect(argument1).to eq(original1) + end + it 'leaves the original string intact' do + argument1 = 'onetwothree' + original1 = argument1.dup + _result = subject.call([argument1, 'two']) + expect(argument1).to eq(original1) + end + it 'leaves the original hash intact' do + argument1 = { 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' } + original1 = argument1.dup + _result = subject.call([argument1, 'key2']) + expect(argument1).to eq(original1) + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/delete_undef_values_spec.rb b/code/environments/production/modules/stdlib/spec/functions/delete_undef_values_spec.rb new file mode 100644 index 0000000..2a282f5 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/delete_undef_values_spec.rb @@ -0,0 +1,58 @@ +require 'spec_helper' + +describe 'delete_undef_values' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError) } + + describe 'when deleting from an array' do + [:undef, '', nil].each do |undef_value| + describe "when undef is represented by #{undef_value.inspect}" do + before(:each) do + pending("review behaviour when being passed undef as #{undef_value.inspect}") if undef_value == '' + pending("review behaviour when being passed undef as #{undef_value.inspect}") if undef_value.nil? + end + it { is_expected.to run.with_params([undef_value]).and_return([]) } + it { is_expected.to run.with_params(['one', undef_value, 'two', 'three']).and_return(%w[one two three]) } + it { is_expected.to run.with_params(['ớņέ', undef_value, 'ŧשּׁō', 'ŧħґëə']).and_return(%w[ớņέ ŧשּׁō ŧħґëə]) } + end + + it 'leaves the original argument intact' do + argument = ['one', undef_value, 'two'] + original = argument.dup + _result = subject.call([argument, 2]) + expect(argument).to eq(original) + end + end + + it { is_expected.to run.with_params(['undef']).and_return(['undef']) } + end + + describe 'when deleting from a hash' do + [:undef, '', nil].each do |undef_value| + describe "when undef is represented by #{undef_value.inspect}" do + before(:each) do + pending("review behaviour when being passed undef as #{undef_value.inspect}") if undef_value == '' + pending("review behaviour when being passed undef as #{undef_value.inspect}") if undef_value.nil? + end + it { is_expected.to run.with_params('key' => undef_value).and_return({}) } + it { + is_expected.to run \ + .with_params('key1' => 'value1', 'undef_key' => undef_value, 'key2' => 'value2') \ + .and_return('key1' => 'value1', 'key2' => 'value2') + } + end + + it 'leaves the original argument intact' do + argument = { 'key1' => 'value1', 'key2' => undef_value } + original = argument.dup + _result = subject.call([argument, 2]) + expect(argument).to eq(original) + end + end + + it { is_expected.to run.with_params('key' => 'undef').and_return('key' => 'undef') } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/delete_values_spec.rb b/code/environments/production/modules/stdlib/spec/functions/delete_values_spec.rb new file mode 100644 index 0000000..20f2648 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/delete_values_spec.rb @@ -0,0 +1,45 @@ +require 'spec_helper' + +describe 'delete_values' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError) } + describe 'when the first argument is not a hash' do + it { is_expected.to run.with_params(1, 'two').and_raise_error(TypeError) } + it { is_expected.to run.with_params('one', 'two').and_raise_error(TypeError) } + it { is_expected.to run.with_params([], 'two').and_raise_error(TypeError) } + end + + describe 'when deleting from a hash' do + it { is_expected.to run.with_params({}, 'value').and_return({}) } + it { + is_expected.to run \ + .with_params({ 'key1' => 'value1' }, 'non-existing value') \ + .and_return('key1' => 'value1') + } + it { + is_expected.to run \ + .with_params({ 'ҝếỵ1 ' => 'νâĺūẹ1', 'ҝếỵ2' => 'value to delete' }, 'value to delete') \ + .and_return('ҝếỵ1 ' => 'νâĺūẹ1') + } + it { + is_expected.to run \ + .with_params({ 'key1' => 'value1', 'key2' => 'νǎŀữ℮ ťớ đêłểťė' }, 'νǎŀữ℮ ťớ đêłểťė') \ + .and_return('key1' => 'value1') + } + it { + is_expected.to run \ + .with_params({ 'key1' => 'value1', 'key2' => 'value to delete', 'key3' => 'value to delete' }, 'value to delete') \ + .and_return('key1' => 'value1') + } + end + + it 'leaves the original argument intact' do + argument = { 'key1' => 'value1', 'key2' => 'value2' } + original = argument.dup + _result = subject.call([argument, 'value2']) + expect(argument).to eq(original) + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/deprecation_spec.rb b/code/environments/production/modules/stdlib/spec/functions/deprecation_spec.rb new file mode 100644 index 0000000..8410867 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/deprecation_spec.rb @@ -0,0 +1,63 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'deprecation' do + before(:each) do + # this is to reset the strict variable to default + Puppet.settings[:strict] = :warning + end + + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(ArgumentError) } + + it 'displays a single warning' do + Puppet.expects(:warning).with(includes('heelo')) + is_expected.to run.with_params('key', 'heelo') + end + + it 'displays a single warning, despite multiple calls' do + Puppet.expects(:warning).with(includes('heelo')).once + (0..1).each do |_i| + is_expected.to run.with_params('key', 'heelo') + end + end + + it 'fails twice with message, with multiple calls. when strict= :error' do + Puppet.settings[:strict] = :error + Puppet.expects(:warning).with(includes('heelo')).never + (0..1).each do |_i| + is_expected.to run.with_params('key', 'heelo').and_raise_error(RuntimeError, %r{deprecation. key. heelo}) + end + end + + it 'displays nothing, despite multiple calls. strict= :off' do + Puppet.settings[:strict] = :off + Puppet.expects(:warning).with(includes('heelo')).never + (0..1).each do |_i| + is_expected.to run.with_params('key', 'heelo') + end + end + + after(:each) do + # this is to reset the strict variable to default + Puppet.settings[:strict] = :warning + end + end +elsif Puppet.version.to_f < 4.0 + # Puppet version < 4 will use these tests. + describe 'deprecation' do + after(:each) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + before(:each) do + ENV['STDLIB_LOG_DEPRECATIONS'] = 'true' + end + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + + it 'displays a single warning' do + scope.expects(:warning).with(includes('heelo')) + is_expected.to run.with_params('key', 'heelo') + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/difference_spec.rb b/code/environments/production/modules/stdlib/spec/functions/difference_spec.rb new file mode 100644 index 0000000..d8c6765 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/difference_spec.rb @@ -0,0 +1,23 @@ +require 'spec_helper' + +describe 'difference' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('one', []).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params([], 'two').and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params([], []).and_return([]) } + it { is_expected.to run.with_params([], ['one']).and_return([]) } + it { is_expected.to run.with_params(['one'], ['one']).and_return([]) } + it { is_expected.to run.with_params(['ớņέ'], ['']).and_return(['ớņέ']) } + it { is_expected.to run.with_params(['one'], []).and_return(['one']) } + it { is_expected.to run.with_params(%w[one two three], %w[two three]).and_return(['one']) } + it { is_expected.to run.with_params(['ớņέ', 'ŧשּׁō', 'ŧħґëə', 2], %w[ŧשּׁō ŧħґëə]).and_return(['ớņέ', 2]) } + it { is_expected.to run.with_params(%w[one two two three], %w[two three]).and_return(['one']) } + it { is_expected.to run.with_params(%w[one two three], %w[two two three]).and_return(['one']) } + it { is_expected.to run.with_params(%w[one two three], %w[two three four]).and_return(['one']) } + it 'does not confuse types' do is_expected.to run.with_params(%w[1 2 3], [1, 2]).and_return(%w[1 2 3]) end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/dig44_spec.rb b/code/environments/production/modules/stdlib/spec/functions/dig44_spec.rb new file mode 100644 index 0000000..c721b0c --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/dig44_spec.rb @@ -0,0 +1,128 @@ +require 'spec_helper' + +describe 'dig44' do + let(:data) do + { + 'a' => { + 'g' => '2', + 'e' => [ + 'f0', + 'f1', + { + 'x' => { + 'y' => 'z', + }, + }, + 'f3', + ], + }, + 'b' => true, + 'c' => false, + 'd' => '1', + 'e' => :undef, + 'f' => nil, + } + end + + let(:utf8_data) do + { + 'ẵ' => { + 'в' => [ + '©', + 'ĝ', + 'に', + ], + }, + } + end + + context 'with single values' do + it 'exists' do + is_expected.not_to be_nil + end + + it 'requires two arguments' do + is_expected.to run.with_params.and_raise_error(ArgumentError) + end + + it 'fails if the data is not a structure' do + is_expected.to run.with_params('test', []).and_raise_error(Puppet::Error) + end + + it 'fails if the path is not an array' do + is_expected.to run.with_params({}, '').and_raise_error(Puppet::Error) + end + + it 'returns the value if the value is string' do + is_expected.to run.with_params(data, ['d'], 'default').and_return('1') + end + + it 'returns true if the value is true' do + is_expected.to run.with_params(data, ['b'], 'default').and_return(true) + end + + it 'returns false if the value is false' do + is_expected.to run.with_params(data, ['c'], 'default').and_return(false) + end + + it 'returns the default if the value is nil' do + is_expected.to run.with_params(data, ['f'], 'default').and_return('default') + end + + it 'returns the default if the value is :undef (same as nil)' do + is_expected.to run.with_params(data, ['e'], 'default').and_return('default') + end + + it 'returns the default if the path is not found' do + is_expected.to run.with_params(data, ['missing'], 'default').and_return('default') + end + end + + context 'with structured values' do + it 'is able to extract a deeply nested hash value' do + is_expected.to run.with_params(data, %w[a g], 'default').and_return('2') + end + + it 'returns the default value if the path is too long' do + is_expected.to run.with_params(data, %w[a g c d], 'default').and_return('default') + end + + it 'supports an array index (number) in the path' do + is_expected.to run.with_params(data, ['a', 'e', 1], 'default').and_return('f1') + end + + it 'supports an array index (string) in the path' do + is_expected.to run.with_params(data, %w[a e 1], 'default').and_return('f1') + end + + it 'returns the default value if an array index is not a number' do + is_expected.to run.with_params(data, %w[a b c], 'default').and_return('default') + end + + it 'returns the default value if and index is out of array length' do + is_expected.to run.with_params(data, %w[a e 5], 'default').and_return('default') + end + + it 'is able to path though both arrays and hashes' do + is_expected.to run.with_params(data, %w[a e 2 x y], 'default').and_return('z') + end + + it 'returns "nil" if value is not found and no default value is provided' do + is_expected.to run.with_params(data, %w[a 1]).and_return(nil) + end + end + + context 'with internationalization (i18N) values' do + it 'is able to return a unicode character' do + is_expected.to run.with_params(utf8_data, ['ẵ', 'в', 0]).and_return('©') + end + + it 'is able to return a utf8 character' do + is_expected.to run.with_params(utf8_data, ['ẵ', 'в', 1]).and_return('ĝ') + end + + it 'is able to return a double byte character' do + is_expected.to run.with_params(utf8_data, ['ẵ', 'в', 2]).and_return('に') + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/dig_spec.rb b/code/environments/production/modules/stdlib/spec/functions/dig_spec.rb new file mode 100644 index 0000000..af5ec51 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/dig_spec.rb @@ -0,0 +1,12 @@ +require 'spec_helper' + +describe 'dig' do + it 'exists' do + expect(Puppet::Parser::Functions.function('dig')).to eq('function_dig') + end + + it 'gives a deprecation warning when called' do + scope.expects(:warning).with('dig() DEPRECATED: This function has been replaced in Puppet 4.5.0, please use dig44() for backwards compatibility or use the new version.') + scope.function_dig([{}, []]) + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/dirname_spec.rb b/code/environments/production/modules/stdlib/spec/functions/dirname_spec.rb new file mode 100644 index 0000000..da62eea --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/dirname_spec.rb @@ -0,0 +1,18 @@ +require 'spec_helper' + +describe 'dirname' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('/path/to/a/file.ext', []).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('/path/to/a/file.ext').and_return('/path/to/a') } + it { is_expected.to run.with_params('relative_path/to/a/file.ext').and_return('relative_path/to/a') } + + context 'with UTF8 and double byte characters' do + it { is_expected.to run.with_params('scheme:///√ạĺűē/竹.ext').and_return('scheme:///√ạĺűē') } + it { is_expected.to run.with_params('ҝẽγ:/√ạĺűē/竹.ㄘ').and_return('ҝẽγ:/√ạĺűē') } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/dos2unix_spec.rb b/code/environments/production/modules/stdlib/spec/functions/dos2unix_spec.rb new file mode 100644 index 0000000..8677d8c --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/dos2unix_spec.rb @@ -0,0 +1,47 @@ +require 'spec_helper' + +describe 'dos2unix' do + context 'when checking parameter validity' do + it { is_expected.not_to eq(nil) } + it do + is_expected.to run.with_params.and_raise_error(ArgumentError, %r{Wrong number of arguments}) + end + it do + is_expected.to run.with_params('one', 'two').and_raise_error(ArgumentError, %r{Wrong number of arguments}) + end + it do + is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) + end + it do + is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError) + end + it do + is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) + end + end + + context 'when converting from dos to unix format' do + sample_text = "Hello\r\nWorld\r\n" + desired_output = "Hello\nWorld\n" + + it 'outputs unix format' do + is_expected.to run.with_params(sample_text).and_return(desired_output) + end + end + + context 'with internationalization (i18N) values' do + sample_text_utf8 = "Ħ℮ļłǿ\r\nשׁөŕłđ\r\n" + desired_output_utf8 = "Ħ℮ļłǿ\nשׁөŕłđ\n" + + sample_text_doublebyte = "こんにちは\r\n世界\r\n" + desired_output_doublebyte = "こんにちは\n世界\n" + + it 'outputs uft8 string' do + is_expected.to run.with_params(sample_text_utf8).and_return(desired_output_utf8) + end + + it 'outputs double byte string' do + is_expected.to run.with_params(sample_text_doublebyte).and_return(desired_output_doublebyte) + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/downcase_spec.rb b/code/environments/production/modules/stdlib/spec/functions/downcase_spec.rb new file mode 100644 index 0000000..d49e169 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/downcase_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +describe 'downcase' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params(100).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('abc').and_return('abc') } + it { is_expected.to run.with_params('Abc').and_return('abc') } + it { is_expected.to run.with_params('ABC').and_return('abc') } + + it { is_expected.to run.with_params(AlsoString.new('ABC')).and_return('abc') } + it { is_expected.to run.with_params([]).and_return([]) } + it { is_expected.to run.with_params(%w[ONE TWO]).and_return(%w[one two]) } + it { is_expected.to run.with_params(['One', 1, 'Two']).and_return(['one', 1, 'two']) } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/empty_spec.rb b/code/environments/production/modules/stdlib/spec/functions/empty_spec.rb new file mode 100644 index 0000000..c6bf1e4 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/empty_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe 'empty', :if => Puppet::Util::Package.versioncmp(Puppet.version, '5.5.0') < 0 do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + it { + pending('Current implementation ignores parameters after the first.') + is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError) + } + it { is_expected.to run.with_params(0).and_return(false) } + it { is_expected.to run.with_params('').and_return(true) } + it { is_expected.to run.with_params('one').and_return(false) } + + it { is_expected.to run.with_params(AlsoString.new('')).and_return(true) } + it { is_expected.to run.with_params(AlsoString.new('one')).and_return(false) } + + it { is_expected.to run.with_params([]).and_return(true) } + it { is_expected.to run.with_params(['one']).and_return(false) } + + it { is_expected.to run.with_params({}).and_return(true) } + it { is_expected.to run.with_params('key' => 'value').and_return(false) } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/ensure_packages_spec.rb b/code/environments/production/modules/stdlib/spec/functions/ensure_packages_spec.rb new file mode 100644 index 0000000..6bdb015 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/ensure_packages_spec.rb @@ -0,0 +1,72 @@ +require 'spec_helper' + +describe 'ensure_packages' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + it { + pending('should not accept numbers as arguments') + is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) + } + it { + pending('should not accept numbers as arguments') + is_expected.to run.with_params(['packagename', 1]).and_raise_error(Puppet::ParseError) + } + it { is_expected.to run.with_params('packagename') } + it { is_expected.to run.with_params(%w[packagename1 packagename2]) } + + context 'when given a catalog with "package { puppet: ensure => absent }"' do + let(:pre_condition) { 'package { puppet: ensure => absent }' } + + describe 'after running ensure_package("facter")' do + before(:each) { subject.call(['facter']) } + + # this lambda is required due to strangeness within rspec-puppet's expectation handling + it { expect(-> { catalogue }).to contain_package('puppet').with_ensure('absent') } + it { expect(-> { catalogue }).to contain_package('facter').with_ensure('present') } + end + + describe 'after running ensure_package("facter", { "provider" => "gem" })' do + before(:each) { subject.call(['facter', { 'provider' => 'gem' }]) } + + # this lambda is required due to strangeness within rspec-puppet's expectation handling + it { expect(-> { catalogue }).to contain_package('puppet').with_ensure('absent').without_provider } + it { expect(-> { catalogue }).to contain_package('facter').with_ensure('present').with_provider('gem') } + end + end + + context 'when given an empty packages array' do + let(:pre_condition) { 'notify { "hi": } -> Package <| |>; $somearray = ["vim",""]; ensure_packages($somearray)' } + + describe 'after running ensure_package(["vim", ""])' do + it { expect { catalogue }.to raise_error(Puppet::ParseError, %r{Empty String provided}) } + end + end + + context 'when given hash of packages' do + before(:each) do + subject.call([{ 'foo' => { 'provider' => 'rpm' }, 'bar' => { 'provider' => 'gem' } }, { 'ensure' => 'present' }]) + subject.call([{ 'パッケージ' => { 'ensure' => 'absent' } }]) + subject.call([{ 'ρǻ¢κầģẻ' => { 'ensure' => 'absent' } }]) + end + + # this lambda is required due to strangeness within rspec-puppet's expectation handling + it { expect(-> { catalogue }).to contain_package('foo').with('provider' => 'rpm', 'ensure' => 'present') } + it { expect(-> { catalogue }).to contain_package('bar').with('provider' => 'gem', 'ensure' => 'present') } + + context 'with UTF8 and double byte characters' do + it { expect(-> { catalogue }).to contain_package('パッケージ').with('ensure' => 'absent') } + it { expect(-> { catalogue }).to contain_package('ρǻ¢κầģẻ').with('ensure' => 'absent') } + end + end + + context 'when 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(:each) { subject.call(['puppet', { 'ensure' => 'installed' }]) } + + # this lambda is required due to strangeness within rspec-puppet's expectation handling + it { expect(-> { catalogue }).to contain_package('puppet').with_ensure('present') } + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/ensure_resource_spec.rb b/code/environments/production/modules/stdlib/spec/functions/ensure_resource_spec.rb new file mode 100644 index 0000000..3ef3b70 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/ensure_resource_spec.rb @@ -0,0 +1,137 @@ +require 'spec_helper' + +describe 'ensure_resource' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{Must specify a type}) } + it { is_expected.to run.with_params('type').and_raise_error(ArgumentError, %r{Must specify a title}) } + if Puppet::Util::Package.versioncmp(Puppet.version, '4.6.0') >= 0 + it { is_expected.to run.with_params('type', 'title', {}, 'extras').and_raise_error(ArgumentError) } + else + it { is_expected.to run.with_params('type', 'title', {}, 'extras').and_raise_error(Puppet::ParseError) } + end + + it { + pending('should not accept numbers as arguments') + is_expected.to run.with_params(1, 2, 3).and_raise_error(Puppet::ParseError) + } + + context 'when given an empty catalog' do + describe 'after running ensure_resource("user", "username1", {})' do + before(:each) { subject.call(['User', 'username1', {}]) } + + # this lambda is required due to strangeness within rspec-puppet's expectation handling + it { expect(-> { catalogue }).to contain_user('username1').without_ensure } + end + + describe 'after running ensure_resource("user", "username1", { gid => undef })' do + before(:each) { subject.call(['User', 'username1', { 'gid' => :undef }]) } + + # this lambda is required due to strangeness within rspec-puppet's expectation handling + it { expect(-> { catalogue }).to contain_user('username1').without_ensure } + it { expect(-> { catalogue }).to contain_user('username1').without_gid } + end + + describe 'after running ensure_resource("user", "username1", { ensure => present, gid => undef })' do + before(:each) { subject.call(['User', 'username1', { 'ensure' => 'present', 'gid' => :undef }]) } + + # this lambda is required due to strangeness within rspec-puppet's expectation handling + it { expect(-> { catalogue }).to contain_user('username1').with_ensure('present') } + it { expect(-> { catalogue }).to contain_user('username1').without_gid } + end + + describe 'after running ensure_resource("test::deftype", "foo", {})' do + before(:each) { subject.call(['test::deftype', 'foo', {}]) } + + # this lambda is required due to strangeness within rspec-puppet's expectation handling + it { expect(-> { catalogue }).to contain_test__deftype('foo').without_ensure } + end + end + + context 'when given a catalog with UTF8 chars' do + describe 'after running ensure_resource("user", "Şắოрŀễ Ţëם", {})' do + before(:each) { subject.call(['User', 'Şắოрŀễ Ţëם', {}]) } + + # this lambda is required due to strangeness within rspec-puppet's expectation handling + it { expect(-> { catalogue }).to contain_user('Şắოрŀễ Ţëם').without_ensure } + end + + describe 'after running ensure_resource("user", "Şắოрŀễ Ţëם", { gid => undef })' do + before(:each) { subject.call(['User', 'Şắოрŀễ Ţëם', { 'gid' => :undef }]) } + + # this lambda is required due to strangeness within rspec-puppet's expectation handling + it { expect(-> { catalogue }).to contain_user('Şắოрŀễ Ţëם').without_ensure } + it { expect(-> { catalogue }).to contain_user('Şắოрŀễ Ţëם').without_gid } + end + + describe 'after running ensure_resource("user", "Şắოрŀễ Ţëם", { ensure => present, gid => undef })' do + before(:each) { subject.call(['User', 'Şắოрŀễ Ţëם', { 'ensure' => 'present', 'gid' => :undef }]) } + + # this lambda is required due to strangeness within rspec-puppet's expectation handling + it { expect(-> { catalogue }).to contain_user('Şắოрŀễ Ţëם').with_ensure('present') } + it { expect(-> { catalogue }).to contain_user('Şắოрŀễ Ţëם').without_gid } + end + end + + context 'when given a catalog with "user { username1: ensure => present }"' do + let(:pre_condition) { 'user { username1: ensure => present }' } + + describe 'after running ensure_resource("user", "username1", {})' do + before(:each) { subject.call(['User', 'username1', {}]) } + + # this lambda is required due to strangeness within rspec-puppet's expectation handling + it { expect(-> { catalogue }).to contain_user('username1').with_ensure('present') } + end + + describe 'after running ensure_resource("user", "username2", {})' do + before(:each) { subject.call(['User', 'username2', {}]) } + + # this lambda is required due to strangeness within rspec-puppet's expectation handling + it { expect(-> { catalogue }).to contain_user('username1').with_ensure('present') } + it { expect(-> { catalogue }).to contain_user('username2').without_ensure } + end + + describe 'after running ensure_resource("user", "username1", { gid => undef })' do + before(:each) { subject.call(['User', 'username1', { 'gid' => :undef }]) } + + # this lambda is required due to strangeness within rspec-puppet's expectation handling + it { expect(-> { catalogue }).to contain_user('username1').with_ensure('present') } + end + + describe 'after running ensure_resource("user", ["username1", "username2"], {})' do + before(:each) { subject.call(['User', %w[username1 username2], {}]) } + + # this lambda is required due to strangeness within rspec-puppet's expectation handling + it { expect(-> { catalogue }).to contain_user('username1').with_ensure('present') } + it { expect(-> { catalogue }).to contain_user('username2').without_ensure } + end + + describe 'when providing already set params' do + let(:params) { { 'ensure' => 'present' } } + + before(:each) { subject.call(['User', %w[username2 username3], params]) } + + # this lambda is required due to strangeness within rspec-puppet's expectation handling + it { expect(-> { catalogue }).to contain_user('username1').with(params) } + it { expect(-> { catalogue }).to contain_user('username2').with(params) } + end + + context 'when trying to add params' do + it { + is_expected.to run \ + .with_params('User', 'username1', 'ensure' => 'present', 'shell' => true) \ + .and_raise_error(Puppet::Resource::Catalog::DuplicateResourceError, %r{User\[username1\] is already declared}) + } + end + end + + context 'when given a catalog with "test::deftype { foo: }"' do + let(:pre_condition) { 'test::deftype { "foo": }' } + + describe 'after running ensure_resource("test::deftype", "foo", {})' do + before(:each) { subject.call(['test::deftype', 'foo', {}]) } + + # this lambda is required due to strangeness within rspec-puppet's expectation handling + it { expect(-> { catalogue }).to contain_test__deftype('foo').without_ensure } + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/ensure_resources_spec.rb b/code/environments/production/modules/stdlib/spec/functions/ensure_resources_spec.rb new file mode 100644 index 0000000..5ec0b20 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/ensure_resources_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper' + +describe 'ensure_resources' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{Must specify a type}) } + it { is_expected.to run.with_params('type').and_raise_error(ArgumentError, %r{Must specify a title}) } + + describe 'given a title hash of multiple resources' do + before(:each) { subject.call(['user', { 'dan' => { 'gid' => 'mygroup', 'uid' => '600' }, 'alex' => { 'gid' => 'mygroup', 'uid' => '700' } }, { 'ensure' => 'present' }]) } + + # this lambda is required due to strangeness within rspec-puppet's expectation handling + it { expect(-> { catalogue }).to contain_user('dan').with_ensure('present') } + it { expect(-> { catalogue }).to contain_user('alex').with_ensure('present') } + it { expect(-> { catalogue }).to contain_user('dan').with('gid' => 'mygroup', 'uid' => '600') } + it { expect(-> { catalogue }).to contain_user('alex').with('gid' => 'mygroup', 'uid' => '700') } + end + + describe 'given a title hash of a single resource' do + before(:each) { subject.call(['user', { 'dan' => { 'gid' => 'mygroup', 'uid' => '600' } }, { 'ensure' => 'present' }]) } + + # this lambda is required due to strangeness within rspec-puppet's expectation handling + it { expect(-> { catalogue }).to contain_user('dan').with_ensure('present') } + it { expect(-> { catalogue }).to contain_user('dan').with('gid' => 'mygroup', 'uid' => '600') } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/flatten_spec.rb b/code/environments/production/modules/stdlib/spec/functions/flatten_spec.rb new file mode 100644 index 0000000..df9ce34 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/flatten_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +describe 'flatten', :if => Puppet::Util::Package.versioncmp(Puppet.version, '5.5.0') < 0 do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params([], []).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params([]).and_return([]) } + it { is_expected.to run.with_params(['one']).and_return(['one']) } + it { is_expected.to run.with_params([['one']]).and_return(['one']) } + it { is_expected.to run.with_params(%w[a b c d e f g]).and_return(%w[a b c d e f g]) } + it { is_expected.to run.with_params([['a', 'b', ['c', %w[d e], 'f', 'g']]]).and_return(%w[a b c d e f g]) } + it { is_expected.to run.with_params(['ã', 'β', ['ĉ', %w[đ ẽ ƒ ġ]]]).and_return(%w[ã β ĉ đ ẽ ƒ ġ]) } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/floor_spec.rb b/code/environments/production/modules/stdlib/spec/functions/floor_spec.rb new file mode 100644 index 0000000..c669966 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/floor_spec.rb @@ -0,0 +1,12 @@ +require 'spec_helper' + +describe 'floor' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('foo').and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params(34).and_return(34) } + it { is_expected.to run.with_params(-34).and_return(-34) } + it { is_expected.to run.with_params(33.1).and_return(33) } + it { is_expected.to run.with_params(-33.1).and_return(-34) } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/fqdn_rand_string_spec.rb b/code/environments/production/modules/stdlib/spec/functions/fqdn_rand_string_spec.rb new file mode 100644 index 0000000..ed6e376 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/fqdn_rand_string_spec.rb @@ -0,0 +1,67 @@ +require 'spec_helper' + +describe 'fqdn_rand_string' do + let(:default_charset) { %r{\A[a-zA-Z0-9]{100}\z} } + + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(0).and_raise_error(ArgumentError, %r{first argument must be a positive integer}) } + it { is_expected.to run.with_params(1.5).and_raise_error(ArgumentError, %r{first argument must be a positive integer}) } + it { is_expected.to run.with_params(-10).and_raise_error(ArgumentError, %r{first argument must be a positive integer}) } + it { is_expected.to run.with_params('-10').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) } + it { is_expected.to run.with_params('string').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) } + it { is_expected.to run.with_params([]).and_raise_error(ArgumentError, %r{first argument must be a positive integer}) } + it { is_expected.to run.with_params({}).and_raise_error(ArgumentError, %r{first argument must be a positive integer}) } + it { is_expected.to run.with_params(1, 1).and_raise_error(ArgumentError, %r{second argument must be undef or a string}) } + it { is_expected.to run.with_params(1, []).and_raise_error(ArgumentError, %r{second argument must be undef or a string}) } + it { is_expected.to run.with_params(1, {}).and_raise_error(ArgumentError, %r{second argument must be undef or a string}) } + it { is_expected.to run.with_params(100).and_return(default_charset) } + it { is_expected.to run.with_params('100').and_return(default_charset) } + it { is_expected.to run.with_params(100, nil).and_return(default_charset) } + it { is_expected.to run.with_params(100, '').and_return(default_charset) } + it { is_expected.to run.with_params(100, 'a').and_return(%r{\Aa{100}\z}) } + it { is_expected.to run.with_params(100, 'ab').and_return(%r{\A[ab]{100}\z}) } + it { is_expected.to run.with_params(100, 'ãβ').and_return(%r{\A[ãβ]{100}\z}) } + + it "provides the same 'random' value on subsequent calls for the same host" do + expect(fqdn_rand_string(10)).to eql(fqdn_rand_string(10)) + end + + it 'considers the same host and same extra arguments to have the same random sequence' do + first_random = fqdn_rand_string(10, :extra_identifier => [1, 'same', 'host']) + second_random = fqdn_rand_string(10, :extra_identifier => [1, 'same', 'host']) + + expect(first_random).to eql(second_random) + end + + it 'allows extra arguments to control the random value on a single host' do + first_random = fqdn_rand_string(10, :extra_identifier => [1, 'different', 'host']) + second_different_random = fqdn_rand_string(10, :extra_identifier => [2, 'different', 'host']) + + expect(first_random).not_to eql(second_different_random) + end + + it 'returns different strings for different hosts' do + val1 = fqdn_rand_string(10, :host => 'first.host.com') + val2 = fqdn_rand_string(10, :host => 'second.host.com') + + expect(val1).not_to eql(val2) + end + + def fqdn_rand_string(max, args = {}) + host = args[:host] || '127.0.0.1' + charset = args[:charset] + extra = args[:extra_identifier] || [] + + # workaround not being able to use let(:facts) because some tests need + # multiple different hostnames in one context + scope.stubs(:lookupvar).with('::fqdn', {}).returns(host) + + function_args = [max] + if args.key?(:charset) || !extra.empty? + function_args << charset + end + function_args += extra + scope.function_fqdn_rand_string(function_args) + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/fqdn_rotate_spec.rb b/code/environments/production/modules/stdlib/spec/functions/fqdn_rotate_spec.rb new file mode 100644 index 0000000..93e0bba --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/fqdn_rotate_spec.rb @@ -0,0 +1,74 @@ +require 'spec_helper' + +describe 'fqdn_rotate' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(0).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work with}) } + it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work with}) } + 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('ã').and_return('ã') } + + it { is_expected.to run.with_params([]).and_return([]) } + it { is_expected.to run.with_params(['a']).and_return(['a']) } + + it 'rotates a string and the result should be the same size' do + expect(fqdn_rotate('asdf').size).to eq(4) + end + + it 'rotates a string to give the same results for one host' do + val1 = fqdn_rotate('abcdefg', :host => 'one') + val2 = fqdn_rotate('abcdefg', :host => 'one') + expect(val1).to eq(val2) + end + + it 'allows extra arguments to control the random rotation on a single host' do + val1 = fqdn_rotate('abcdefg', :extra_identifier => [1, 'different', 'host']) + val2 = fqdn_rotate('abcdefg', :extra_identifier => [2, 'different', 'host']) + expect(val1).not_to eq(val2) + end + + it 'considers the same host and same extra arguments to have the same random rotation' do + val1 = fqdn_rotate('abcdefg', :extra_identifier => [1, 'same', 'host']) + val2 = fqdn_rotate('abcdefg', :extra_identifier => [1, 'same', 'host']) + expect(val1).to eq(val2) + end + + it 'rotates a string to give different values on different hosts' do + val1 = fqdn_rotate('abcdefg', :host => 'one') + val2 = fqdn_rotate('abcdefg', :host => 'two') + expect(val1).not_to eq(val2) + end + + it 'accepts objects which extend String' do + result = fqdn_rotate(AlsoString.new('asdf')) + expect(result).to eq('dfas') + end + + it 'uses the Puppet::Util.deterministic_rand function' do + skip 'Puppet::Util#deterministic_rand not available' unless Puppet::Util.respond_to?(:deterministic_rand) + + Puppet::Util.expects(:deterministic_rand).with(44_489_829_212_339_698_569_024_999_901_561_968_770, 4) + fqdn_rotate('asdf') + end + + it 'does not leave the global seed in a deterministic state' do + fqdn_rotate('asdf') + rand1 = rand + fqdn_rotate('asdf') + rand2 = rand + expect(rand1).not_to eql(rand2) + end + + def fqdn_rotate(value, args = {}) + host = args[:host] || '127.0.0.1' + extra = args[:extra_identifier] || [] + + # workaround not being able to use let(:facts) because some tests need + # multiple different hostnames in one context + scope.stubs(:lookupvar).with('::fqdn').returns(host) + + function_args = [value] + extra + scope.function_fqdn_rotate(function_args) + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/fqdn_uuid_spec.rb b/code/environments/production/modules/stdlib/spec/functions/fqdn_uuid_spec.rb new file mode 100644 index 0000000..a6da6f2 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/fqdn_uuid_spec.rb @@ -0,0 +1,12 @@ +require 'spec_helper' + +describe 'fqdn_uuid' do + context 'with invalid parameters' do + it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{No arguments given$}) } + end + + context 'with given string' do + it { is_expected.to run.with_params('puppetlabs.com').and_return('9c70320f-6815-5fc5-ab0f-debe68bf764c') } + it { is_expected.to run.with_params('google.com').and_return('64ee70a4-8cc1-5d25-abf2-dea6c79a09c8') } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/get_module_path_spec.rb b/code/environments/production/modules/stdlib/spec/functions/get_module_path_spec.rb new file mode 100644 index 0000000..81290cb --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/get_module_path_spec.rb @@ -0,0 +1,49 @@ +require 'spec_helper' + +describe 'get_module_path' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{Wrong number of arguments, expects one}) } + it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{Wrong number of arguments, expects one}) } + it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError, %r{Wrong number of arguments, expects one}) } + it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{Could not find module}) } + + class StubModule + attr_reader :path + def initialize(path) + @path = path + end + end + + describe 'when locating a module' do + let(:modulepath) { '/tmp/does_not_exist' } + let(:path_of_module_foo) { StubModule.new('/tmp/does_not_exist/foo') } + + before(:each) { Puppet[:modulepath] = modulepath } + + context 'when in the default environment' do + before(:each) { Puppet::Module.expects(:find).with('foo', 'rp_env').returns(path_of_module_foo) } + + it { is_expected.to run.with_params('foo').and_return(path_of_module_foo.path) } + + context 'when the modulepath is a list' do + before(:each) { Puppet[:modulepath] = modulepath + 'tmp/something_else' } + + it { is_expected.to run.with_params('foo').and_return(path_of_module_foo.path) } + end + end + + context 'when in a non-default default environment' do + let(:environment) { 'test' } + + before(:each) { Puppet::Module.expects(:find).with('foo', 'test').returns(path_of_module_foo) } + + it { is_expected.to run.with_params('foo').and_return(path_of_module_foo.path) } + + context 'when the modulepath is a list' do + before(:each) { Puppet[:modulepath] = modulepath + 'tmp/something_else' } + + it { is_expected.to run.with_params('foo').and_return(path_of_module_foo.path) } + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/getparam_spec.rb b/code/environments/production/modules/stdlib/spec/functions/getparam_spec.rb new file mode 100644 index 0000000..3a3cc27 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/getparam_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' + +describe 'getparam' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{Must specify a reference}) } + it { is_expected.to run.with_params('User[one]').and_raise_error(ArgumentError, %r{Must specify name of a parameter}) } + it { is_expected.to run.with_params('User[one]', 2).and_raise_error(ArgumentError, %r{Must specify name of a parameter}) } + it { is_expected.to run.with_params('User[one]', []).and_raise_error(ArgumentError, %r{Must specify name of a parameter}) } + it { is_expected.to run.with_params('User[one]', {}).and_raise_error(ArgumentError, %r{Must specify name of a parameter}) } + + describe 'when compared against a user resource with no params' do + let(:pre_condition) { 'user { "one": }' } + + it { is_expected.to run.with_params('User[one]', 'ensure').and_return('') } + it { is_expected.to run.with_params('User[two]', 'ensure').and_return('') } + it { is_expected.to run.with_params('User[one]', 'shell').and_return('') } + end + + describe 'when compared against a user resource with params' do + let(:pre_condition) { 'user { "one": ensure => present, shell => "/bin/sh", managehome => false, }' } + + it { is_expected.to run.with_params('User[one]', 'ensure').and_return('present') } + it { is_expected.to run.with_params('User[two]', 'ensure').and_return('') } + it { is_expected.to run.with_params('User[one]', 'shell').and_return('/bin/sh') } + it { is_expected.to run.with_params('User[one]', 'managehome').and_return(false) } + end + + describe 'when compared against a user resource with UTF8 and double byte params' do + let(:pre_condition) { 'user { ["三", "ƒốưř"]: ensure => present }' } + + it { is_expected.to run.with_params('User[三]', 'ensure').and_return('present') } + it { is_expected.to run.with_params('User[ƒốưř]', 'ensure').and_return('present') } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/getvar_spec.rb b/code/environments/production/modules/stdlib/spec/functions/getvar_spec.rb new file mode 100644 index 0000000..cb865c7 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/getvar_spec.rb @@ -0,0 +1,36 @@ +require 'spec_helper' + +describe 'getvar' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + + it { is_expected.to run.with_params('$::foo').and_return(nil) } + + context 'with given variables in namespaces' do + let(:pre_condition) do + <<-PUPPETCODE + class site::data { $foo = 'baz' } + include site::data + PUPPETCODE + end + + it { is_expected.to run.with_params('site::data::foo').and_return('baz') } + it { is_expected.to run.with_params('::site::data::foo').and_return('baz') } + it { is_expected.to run.with_params('::site::data::bar').and_return(nil) } + end + + context 'with given variables in namespaces' do + let(:pre_condition) do + <<-PUPPETCODE + class site::info { $lock = 'ŧҺîš íš ắ śţřĭŋĝ' } + class site::new { $item = '万Ü€‰' } + include site::info + include site::new + PUPPETCODE + end + + it { is_expected.to run.with_params('site::info::lock').and_return('ŧҺîš íš ắ śţřĭŋĝ') } + it { is_expected.to run.with_params('::site::new::item').and_return('万Ü€‰') } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/glob_spec.rb b/code/environments/production/modules/stdlib/spec/functions/glob_spec.rb new file mode 100644 index 0000000..209aca6 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/glob_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe 'glob' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) } + 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([]) } + it { is_expected.to run.with_params(['/etc/xyzxyzxyz', '/etcxyzxyzxyz']).and_return([]) } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/grep_spec.rb b/code/environments/production/modules/stdlib/spec/functions/grep_spec.rb new file mode 100644 index 0000000..1122ffb --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/grep_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe 'grep' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('grep does not actually check this, and raises NoMethodError instead') + is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{first argument not an array}) + } + it { + pending('grep does not actually check this, and raises NoMethodError instead') + is_expected.to run.with_params(1, 'two').and_raise_error(Puppet::ParseError, %r{first argument not an array}) + } + it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params([], 'two').and_return([]) } + it { is_expected.to run.with_params(%w[one two three], 'two').and_return(['two']) } + it { is_expected.to run.with_params(%w[one two three], 't(wo|hree)').and_return(%w[two three]) } + it { is_expected.to run.with_params(%w[ờאּê ţשּׂỡ ţһŗəè], 'ţ(שּׂỡ|һŗəè)').and_return(%w[ţשּׂỡ ţһŗəè]) } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/has_interface_with_spec.rb b/code/environments/production/modules/stdlib/spec/functions/has_interface_with_spec.rb new file mode 100644 index 0000000..273ecb2 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/has_interface_with_spec.rb @@ -0,0 +1,39 @@ +require 'spec_helper' + +describe 'has_interface_with' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + + # We need to mock out the Facts so we can specify how we expect this function + # to behave on different platforms. + context 'when on Mac OS X Systems' do + let(:facts) { { :interfaces => 'lo0,gif0,stf0,en1,p2p0,fw0,en0,vmnet1,vmnet8,utun0' } } + + it { is_expected.to run.with_params('lo0').and_return(true) } + it { is_expected.to run.with_params('lo').and_return(false) } + end + + context 'when on Linux Systems' do + let(:facts) do + { + :interfaces => 'eth0,lo', + :ipaddress => '10.0.0.1', + :ipaddress_lo => '127.0.0.1', + :ipaddress_eth0 => '10.0.0.1', + :muppet => 'kermit', + :muppet_lo => 'mspiggy', + :muppet_eth0 => 'kermit', + } + end + + it { is_expected.to run.with_params('lo').and_return(true) } + it { is_expected.to run.with_params('lo0').and_return(false) } + it { is_expected.to run.with_params('ipaddress', '127.0.0.1').and_return(true) } + it { is_expected.to run.with_params('ipaddress', '10.0.0.1').and_return(true) } + it { is_expected.to run.with_params('ipaddress', '8.8.8.8').and_return(false) } + it { is_expected.to run.with_params('muppet', 'kermit').and_return(true) } + it { is_expected.to run.with_params('muppet', 'mspiggy').and_return(true) } + it { is_expected.to run.with_params('muppet', 'bigbird').and_return(false) } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/has_ip_address_spec.rb b/code/environments/production/modules/stdlib/spec/functions/has_ip_address_spec.rb new file mode 100644 index 0000000..33934f3 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/has_ip_address_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe 'has_ip_address' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + + context 'when on Linux Systems' do + let(:facts) do + { + :interfaces => 'eth0,lo', + :ipaddress => '10.0.0.1', + :ipaddress_lo => '127.0.0.1', + :ipaddress_eth0 => '10.0.0.1', + } + end + + it { is_expected.to run.with_params('127.0.0.1').and_return(true) } + it { is_expected.to run.with_params('10.0.0.1').and_return(true) } + it { is_expected.to run.with_params('8.8.8.8').and_return(false) } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/has_ip_network_spec.rb b/code/environments/production/modules/stdlib/spec/functions/has_ip_network_spec.rb new file mode 100644 index 0000000..cc6ff0b --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/has_ip_network_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +describe 'has_ip_network' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + + context 'when on Linux Systems' do + let(:facts) do + { + :interfaces => 'eth0,lo', + :network_lo => '127.0.0.0', + :network_eth0 => '10.0.0.0', + } + end + + it { is_expected.to run.with_params('127.0.0.0').and_return(true) } + it { is_expected.to run.with_params('10.0.0.0').and_return(true) } + it { is_expected.to run.with_params('8.8.8.0').and_return(false) } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/has_key_spec.rb b/code/environments/production/modules/stdlib/spec/functions/has_key_spec.rb new file mode 100644 index 0000000..6abebba --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/has_key_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe 'has_key' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{expects the first argument to be a hash}) } + it { is_expected.to run.with_params(1, 'two').and_raise_error(Puppet::ParseError, %r{expects the first argument to be a hash}) } + it { is_expected.to run.with_params([], 'two').and_raise_error(Puppet::ParseError, %r{expects the first argument to be a hash}) } + + it { is_expected.to run.with_params({ 'key' => 'value' }, 'key').and_return(true) } + it { is_expected.to run.with_params({}, 'key').and_return(false) } + it { is_expected.to run.with_params({ 'key' => 'value' }, 'not a key').and_return(false) } + + context 'with UTF8 and double byte characters' do + it { is_expected.to run.with_params({ 'κéỳ ' => '٧ậļųể' }, 'κéỳ ').and_return(true) } + it { is_expected.to run.with_params({ 'キー' => '٧ậļųể' }, 'キー').and_return(true) } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/hash_spec.rb b/code/environments/production/modules/stdlib/spec/functions/hash_spec.rb new file mode 100644 index 0000000..f32d54f --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/hash_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +describe 'hash' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('Current implementation ignores parameters after the first.') + is_expected.to run.with_params([], 'two').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params(['one']).and_raise_error(Puppet::ParseError, %r{Unable to compute}) } + it { is_expected.to run.with_params([]).and_return({}) } + it { is_expected.to run.with_params(%w[key1 value1]).and_return('key1' => 'value1') } + it { is_expected.to run.with_params(['κ℮ұ1', '√āĺűẻ1']).and_return('κ℮ұ1' => '√āĺűẻ1') } + it { is_expected.to run.with_params(%w[key1 value1 key2 value2]).and_return('key1' => 'value1', 'key2' => 'value2') } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/intersection_spec.rb b/code/environments/production/modules/stdlib/spec/functions/intersection_spec.rb new file mode 100644 index 0000000..4589928 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/intersection_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe 'intersection' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params('one', []).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params([], 'two').and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params([], []).and_return([]) } + it { is_expected.to run.with_params([], ['one']).and_return([]) } + it { is_expected.to run.with_params(['one'], []).and_return([]) } + it { is_expected.to run.with_params(['one'], ['one']).and_return(['one']) } + it { is_expected.to run.with_params(%w[one two three], %w[two three]).and_return(%w[two three]) } + it { is_expected.to run.with_params(%w[ōŋể ŧשợ ţђŕẽё], %w[ŧשợ ţђŕẽё]).and_return(%w[ŧשợ ţђŕẽё]) } + it { is_expected.to run.with_params(%w[one two two three], %w[two three]).and_return(%w[two three]) } + it { is_expected.to run.with_params(%w[one two three], %w[two two three]).and_return(%w[two three]) } + it { is_expected.to run.with_params(%w[one two three], %w[two three four]).and_return(%w[two three]) } + it 'does not confuse types' do is_expected.to run.with_params(%w[1 2 3], [1, 2]).and_return([]) end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/is_a_spec.rb b/code/environments/production/modules/stdlib/spec/functions/is_a_spec.rb new file mode 100644 index 0000000..a5fe590 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/is_a_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper' + +if ENV['FUTURE_PARSER'] == 'yes' + describe 'type_of' do + pending 'teach rspec-puppet to load future-only functions under 3.7.5' do + it { is_expected.not_to eq(nil) } + end + end +end + +if Puppet.version.to_f >= 4.0 + describe 'is_a' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('', '').and_raise_error(ArgumentError) } + + it 'succeeds when comparing a string and a string' do + is_expected.to run.with_params('hello world', String).and_return(true) + end + + it 'fails when comparing an integer and a string' do + is_expected.to run.with_params(5, String).and_return(false) + end + + it 'suceeds when comparing an UTF8 and double byte characters' do + comparison_array = ['このテキスト', 'ŧћịś ŧêχŧ'] + comparison_array.each do |comparison_value| + is_expected.to run.with_params(comparison_value, String).and_return(true) + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/is_array_spec.rb b/code/environments/production/modules/stdlib/spec/functions/is_array_spec.rb new file mode 100644 index 0000000..c6c016a --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/is_array_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' + +describe 'is_array' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('Current implementation ignores parameters after the first.') + is_expected.to run.with_params([], []).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params([]).and_return(true) } + it { is_expected.to run.with_params(['one']).and_return(true) } + it { is_expected.to run.with_params([1]).and_return(true) } + it { is_expected.to run.with_params([{}]).and_return(true) } + it { is_expected.to run.with_params([[]]).and_return(true) } + it { is_expected.to run.with_params('').and_return(false) } + 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 'with deprecation warning' do + after(:each) 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 'displays 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 'displays 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/code/environments/production/modules/stdlib/spec/functions/is_bool_spec.rb b/code/environments/production/modules/stdlib/spec/functions/is_bool_spec.rb new file mode 100644 index 0000000..182ef70 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/is_bool_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe 'is_bool' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(true, false).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(true).and_return(true) } + it { is_expected.to run.with_params(false).and_return(true) } + 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) } + 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 'with deprecation warning' do + after(:each) 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 'displays 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 'displays 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/code/environments/production/modules/stdlib/spec/functions/is_domain_name_spec.rb b/code/environments/production/modules/stdlib/spec/functions/is_domain_name_spec.rb new file mode 100644 index 0000000..c2d5988 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/is_domain_name_spec.rb @@ -0,0 +1,43 @@ +require 'spec_helper' + +describe 'is_domain_name' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + 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) } + it { is_expected.to run.with_params('').and_return(false) } + it { is_expected.to run.with_params('.').and_return(true) } + it { is_expected.to run.with_params('com').and_return(true) } + it { is_expected.to run.with_params('com.').and_return(true) } + it { is_expected.to run.with_params('x.com').and_return(true) } + it { is_expected.to run.with_params('x.com.').and_return(true) } + it { is_expected.to run.with_params('foo.example.com').and_return(true) } + it { is_expected.to run.with_params('foo.example.com.').and_return(true) } + it { is_expected.to run.with_params('2foo.example.com').and_return(true) } + it { is_expected.to run.with_params('2foo.example.com.').and_return(true) } + it { is_expected.to run.with_params('www.2foo.example.com').and_return(true) } + it { is_expected.to run.with_params('www.2foo.example.com.').and_return(true) } + describe 'inputs with spaces' do + it { is_expected.to run.with_params('invalid domain').and_return(false) } + end + describe 'inputs with hyphens' do + it { is_expected.to run.with_params('foo-bar.example.com').and_return(true) } + it { is_expected.to run.with_params('foo-bar.example.com.').and_return(true) } + it { is_expected.to run.with_params('www.foo-bar.example.com').and_return(true) } + it { is_expected.to run.with_params('www.foo-bar.example.com.').and_return(true) } + it { is_expected.to run.with_params('-foo.example.com').and_return(false) } + it { is_expected.to run.with_params('-foo.example.com.').and_return(false) } + end + # Values obtained from Facter values will be frozen strings + # in newer versions of Facter: + it { is_expected.to run.with_params('www.example.com'.freeze).and_return(true) } + describe 'top level domain must be alphabetic if there are multiple labels' do + it { is_expected.to run.with_params('2com').and_return(true) } + it { is_expected.to run.with_params('www.example.2com').and_return(false) } + end + describe 'IP addresses are not domain names' do + it { is_expected.to run.with_params('192.168.1.1').and_return(false) } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/is_email_address_spec.rb b/code/environments/production/modules/stdlib/spec/functions/is_email_address_spec.rb new file mode 100644 index 0000000..6c291e6 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/is_email_address_spec.rb @@ -0,0 +1,14 @@ +require 'spec_helper' + +describe 'is_email_address' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params([], []).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('bob@gmail.com').and_return(true) } + it { is_expected.to run.with_params('alice+puppetlabs.com@gmail.com').and_return(true) } + it { is_expected.to run.with_params('peter.parker@gmail.com').and_return(true) } + it { is_expected.to run.with_params('1.2.3@domain').and_return(false) } + 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([]).and_return(false) } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/is_float_spec.rb b/code/environments/production/modules/stdlib/spec/functions/is_float_spec.rb new file mode 100644 index 0000000..6f59c3e --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/is_float_spec.rb @@ -0,0 +1,40 @@ +require 'spec_helper' + +describe 'is_float' do + it { is_expected.not_to eq(nil) } + + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(0.1, 0.2).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + + describe 'passing a string' do + it { is_expected.to run.with_params('0.1').and_return(true) } + it { is_expected.to run.with_params('1.0').and_return(true) } + it { is_expected.to run.with_params('1').and_return(false) } + it { is_expected.to run.with_params('one').and_return(false) } + it { is_expected.to run.with_params('one 1.0').and_return(false) } + it { is_expected.to run.with_params('1.0 one').and_return(false) } + end + + describe 'passing numbers' do + it { is_expected.to run.with_params(0.1).and_return(true) } + 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 'with deprecation warning' do + after(:each) 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 'displays 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 'displays 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/code/environments/production/modules/stdlib/spec/functions/is_function_available_spec.rb b/code/environments/production/modules/stdlib/spec/functions/is_function_available_spec.rb new file mode 100644 index 0000000..887f069 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/is_function_available_spec.rb @@ -0,0 +1,9 @@ +require 'spec_helper' + +describe 'is_function_available' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('include').and_return(true) } + it { is_expected.to run.with_params('no_such_function').and_return(false) } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/is_hash_spec.rb b/code/environments/production/modules/stdlib/spec/functions/is_hash_spec.rb new file mode 100644 index 0000000..3ef061d --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/is_hash_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe 'is_hash' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('').and_return(false) } + it { is_expected.to run.with_params({}).and_return(true) } + it { is_expected.to run.with_params([]).and_return(false) } + it { is_expected.to run.with_params(1).and_return(false) } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/is_integer_spec.rb b/code/environments/production/modules/stdlib/spec/functions/is_integer_spec.rb new file mode 100644 index 0000000..00262ce --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/is_integer_spec.rb @@ -0,0 +1,46 @@ +require 'spec_helper' + +describe 'is_integer' do + it { is_expected.not_to eq(nil) } + + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(1, 2).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + + 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').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) } + 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('one').and_return(false) } + it { is_expected.to run.with_params([]).and_return(false) } + 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(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 'with deprecation warning' do + after(:each) 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 'displays 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 'displays 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/code/environments/production/modules/stdlib/spec/functions/is_ip_address_spec.rb b/code/environments/production/modules/stdlib/spec/functions/is_ip_address_spec.rb new file mode 100644 index 0000000..1c05726 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/is_ip_address_spec.rb @@ -0,0 +1,40 @@ +require 'spec_helper' + +describe 'is_ip_address' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params([], []).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('1.2.3.4').and_return(true) } + it { is_expected.to run.with_params('1.2.3.255').and_return(true) } + it { is_expected.to run.with_params('1.2.3.256').and_return(false) } + it { is_expected.to run.with_params('1.2.3').and_return(false) } + it { is_expected.to run.with_params('1.2.3.4.5').and_return(false) } + it { is_expected.to run.with_params('fe00::1').and_return(true) } + it { is_expected.to run.with_params('fe80:0000:cd12:d123:e2f8:47ff:fe09:dd74').and_return(true) } + it { is_expected.to run.with_params('FE80:0000:CD12:D123:E2F8:47FF:FE09:DD74').and_return(true) } + it { is_expected.to run.with_params('fe80:0000:cd12:d123:e2f8:47ff:fe09:zzzz').and_return(false) } + it { is_expected.to run.with_params('fe80:0000:cd12:d123:e2f8:47ff:fe09').and_return(false) } + it { is_expected.to run.with_params('fe80:0000:cd12:d123:e2f8:47ff:fe09:dd74:dd74').and_return(false) } + it { is_expected.to run.with_params('').and_return(false) } + 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) } + it { is_expected.to run.with_params([]).and_return(false) } + + 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 'displays 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 'displays 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(:each) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/is_ipv4_address_spec.rb b/code/environments/production/modules/stdlib/spec/functions/is_ipv4_address_spec.rb new file mode 100644 index 0000000..fdc2206 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/is_ipv4_address_spec.rb @@ -0,0 +1,31 @@ +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, %r{wrong number of arguments}i) } + + SharedData::IPV4_PATTERNS.each do |value| + it { is_expected.to run.with_params(value).and_return(true) } + end + + SharedData::IPV4_NEGATIVE_PATTERNS.each do |value| + it { is_expected.to run.with_params(value).and_return(false) } + end + + context 'Checking for deprecation warning', :if => Puppet.version.to_f < 4.0 do + after(:each) 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 'displays a single deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = 'true' + scope.expects(:warning).with(includes('This method is deprecated')) + is_expected.to run.with_params(SharedData::IPV4_PATTERNS.first).and_return(true) + end + it 'displays 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(SharedData::IPV4_PATTERNS.first).and_return(true) + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/is_ipv6_address_spec.rb b/code/environments/production/modules/stdlib/spec/functions/is_ipv6_address_spec.rb new file mode 100644 index 0000000..d85bef3 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/is_ipv6_address_spec.rb @@ -0,0 +1,29 @@ +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, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('2001:0db8:85a3:0000:0000:8a2e:0370:7334').and_return(true) } + it { is_expected.to run.with_params('85a3:0000:0000:8a2e:0370:7334:100.100.100.100').and_return(true) } + it { is_expected.to run.with_params('1.2.3').and_return(false) } + 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) } + + context 'Checking for deprecation warning', :if => Puppet.version.to_f < 4.0 do + after(:each) 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 'displays 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 'displays 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/code/environments/production/modules/stdlib/spec/functions/is_mac_address_spec.rb b/code/environments/production/modules/stdlib/spec/functions/is_mac_address_spec.rb new file mode 100644 index 0000000..1f15702 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/is_mac_address_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +describe 'is_mac_address' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params([], []).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('00:a0:1f:12:7f:a0').and_return(true) } + it { is_expected.to run.with_params('00:A0:1F:12:7F:A0').and_return(true) } + it { is_expected.to run.with_params('00:00:00:00:00:0g').and_return(false) } + it { is_expected.to run.with_params('').and_return(false) } + it { is_expected.to run.with_params('one').and_return(false) } + + context 'with UTF8 and double byte characters' do + it { is_expected.to run.with_params('ƒốưř').and_return(false) } + it { is_expected.to run.with_params('三+').and_return(false) } + end + + it { + pending 'should properly typecheck its arguments' + is_expected.to run.with_params(1).and_return(false) + } + it { + pending 'should properly typecheck its arguments' + is_expected.to run.with_params({}).and_return(false) + } + it { + pending 'should properly typecheck its arguments' + is_expected.to run.with_params([]).and_return(false) + } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/is_numeric_spec.rb b/code/environments/production/modules/stdlib/spec/functions/is_numeric_spec.rb new file mode 100644 index 0000000..c936d73 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/is_numeric_spec.rb @@ -0,0 +1,46 @@ +require 'spec_helper' + +describe 'is_numeric' do + it { is_expected.not_to eq(nil) } + + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(1, 2).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + + 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').and_return(true) } + + it { is_expected.to run.with_params(3.7).and_return(true) } + it { is_expected.to run.with_params('3.7').and_return(true) } + it { is_expected.to run.with_params(-3.7).and_return(true) } + it { is_expected.to run.with_params('-3.7').and_return(true) } + + it { is_expected.to run.with_params('-342.2315e-12').and_return(true) } + + it { is_expected.to run.with_params('one').and_return(false) } + it { is_expected.to run.with_params([]).and_return(false) } + 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(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(' - 1234').and_return(false) } + + context 'with deprecation warning' do + after(:each) 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 'displays 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 'displays 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/code/environments/production/modules/stdlib/spec/functions/is_string_spec.rb b/code/environments/production/modules/stdlib/spec/functions/is_string_spec.rb new file mode 100644 index 0000000..98b5578 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/is_string_spec.rb @@ -0,0 +1,45 @@ +require 'spec_helper' + +describe 'is_string' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('Current implementation ignores parameters after the first.') + is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + } + + it { is_expected.to run.with_params(3).and_return(false) } + it { is_expected.to run.with_params('3').and_return(false) } + it { is_expected.to run.with_params(-3).and_return(false) } + it { is_expected.to run.with_params('-3').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) } + 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([]).and_return(false) } + 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(true).and_return(false) } + 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 'with deprecation warning' do + after(:each) 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 'displays 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 'displays 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/code/environments/production/modules/stdlib/spec/functions/join_keys_to_values_spec.rb b/code/environments/production/modules/stdlib/spec/functions/join_keys_to_values_spec.rb new file mode 100644 index 0000000..8800499 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/join_keys_to_values_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper' + +describe 'join_keys_to_values' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{Takes exactly two arguments}) } + it { is_expected.to run.with_params({}, '', '').and_raise_error(Puppet::ParseError, %r{Takes exactly two arguments}) } + it { is_expected.to run.with_params('one', '').and_raise_error(TypeError, %r{The first argument must be a hash}) } + it { is_expected.to run.with_params({}, 2).and_raise_error(TypeError, %r{The second argument must be a string}) } + + 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({ 'key' => 'value' }, '').and_return(['keyvalue']) } + it { is_expected.to run.with_params({ 'key' => 'value' }, ':').and_return(['key:value']) } + + context 'with UTF8 and double byte characters' do + it { is_expected.to run.with_params({ 'ҝẽγ' => '√ạĺűē' }, ':').and_return(['ҝẽγ:√ạĺűē']) } + it { is_expected.to run.with_params({ 'ҝẽγ' => '√ạĺűē' }, '万').and_return(['ҝẽγ万√ạĺűē']) } + end + + it { is_expected.to run.with_params({ 'key' => nil }, ':').and_return(['key:']) } + it 'runs join_keys_to_values(<hash with multiple keys>, ":") and return the proper array' do + result = subject.call([{ 'key1' => 'value1', 'key2' => 'value2' }, ':']) + expect(result.sort).to eq(['key1:value1', 'key2:value2'].sort) + end + it 'runs join_keys_to_values(<hash with array value>, " ") and return the proper array' do + result = subject.call([{ 'key1' => 'value1', 'key2' => %w[value2 value3] }, ' ']) + expect(result.sort).to eq(['key1 value1', 'key2 value2', 'key2 value3'].sort) + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/join_spec.rb b/code/environments/production/modules/stdlib/spec/functions/join_spec.rb new file mode 100644 index 0000000..c24ee76 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/join_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe 'join', :if => Puppet::Util::Package.versioncmp(Puppet.version, '5.5.0') < 0 do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('Current implementation ignores parameters after the second.') + is_expected.to run.with_params([], '', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{Requires array to work with}) } + it { is_expected.to run.with_params([], 2).and_raise_error(Puppet::ParseError, %r{Requires string to work with}) } + + 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(['one']).and_return('one') } + it { is_expected.to run.with_params(['one'], ':').and_return('one') } + it { is_expected.to run.with_params(%w[one two three]).and_return('onetwothree') } + it { is_expected.to run.with_params(%w[one two three], ':').and_return('one:two:three') } + it { is_expected.to run.with_params(%w[ōŋể ŧשợ ţђŕẽё], ':').and_return('ōŋể:ŧשợ:ţђŕẽё') } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/keys_spec.rb b/code/environments/production/modules/stdlib/spec/functions/keys_spec.rb new file mode 100644 index 0000000..23a870e --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/keys_spec.rb @@ -0,0 +1,24 @@ +require 'spec_helper' + +describe 'keys', :if => Puppet::Util::Package.versioncmp(Puppet.version, '5.5.0') < 0 do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('Current implementation ignores parameters after the first.') + is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, %r{Requires hash to work with}) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires hash to work with}) } + it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{Requires hash to work with}) } + it { is_expected.to run.with_params({}).and_return([]) } + it { is_expected.to run.with_params('key' => 'value').and_return(['key']) } + it 'returns the array of keys' do + result = subject.call([{ 'key1' => 'value1', 'key2' => 'value2' }]) + expect(result).to match_array(%w[key1 key2]) + end + + it 'runs with UTF8 and double byte characters' do + result = subject.call([{ 'ҝểү' => '√ẳŀμệ', 'キー' => '値' }]) + expect(result).to match_array(%w[ҝểү キー]) + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/length_spec.rb b/code/environments/production/modules/stdlib/spec/functions/length_spec.rb new file mode 100644 index 0000000..550c557 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/length_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe 'length', :if => Puppet::Util::Package.versioncmp(Puppet.version, '5.5.0') < 0 do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{'length' expects 1 argument, got none}) } + it { is_expected.to run.with_params([], 'extra').and_raise_error(ArgumentError, %r{'length' expects 1 argument, got 2}) } + it { is_expected.to run.with_params(1).and_raise_error(ArgumentError, %r{expects a value of type String, Array, or Hash, got Integer}) } + it { is_expected.to run.with_params(true).and_raise_error(ArgumentError, %r{expects a value of type String, Array, or Hash, got Boolean}) } + it { is_expected.to run.with_params('1').and_return(1) } + it { is_expected.to run.with_params('1.0').and_return(3) } + it { is_expected.to run.with_params([]).and_return(0) } + it { is_expected.to run.with_params(['a']).and_return(1) } + it { is_expected.to run.with_params(%w[one two three]).and_return(3) } + it { is_expected.to run.with_params(%w[one two three four]).and_return(4) } + + it { is_expected.to run.with_params({}).and_return(0) } + it { is_expected.to run.with_params('1' => '2').and_return(1) } + it { is_expected.to run.with_params('1' => '2', '4' => '4').and_return(2) } + it { is_expected.to run.with_params('€' => '@', '竹' => 'ǿňè').and_return(2) } + + it { is_expected.to run.with_params('').and_return(0) } + it { is_expected.to run.with_params('a').and_return(1) } + it { is_expected.to run.with_params('abc').and_return(3) } + it { is_expected.to run.with_params('abcd').and_return(4) } + it { is_expected.to run.with_params('万').and_return(1) } + it { is_expected.to run.with_params('āβćđ').and_return(4) } + + context 'when using a class extending String' do + it { is_expected.to run.with_params(AlsoString.new('asdfghjkl')).and_return(9) } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/load_module_metadata_spec.rb b/code/environments/production/modules/stdlib/spec/functions/load_module_metadata_spec.rb new file mode 100644 index 0000000..eb9a94b --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/load_module_metadata_spec.rb @@ -0,0 +1,47 @@ +require 'spec_helper' + +describe 'load_module_metadata' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + + describe 'when calling with valid arguments' do + before :each do + allow(File).to receive(:read).with(%r{\/(stdlib|test)\/metadata.json}, :encoding => 'utf-8').and_return('{"name": "puppetlabs-stdlib"}') + allow(File).to receive(:read).with(%r{\/(stdlib|test)\/metadata.json}).and_return('{"name": "puppetlabs-stdlib"}') + end + + context 'when calling with valid utf8 and double byte character arguments' do + before :each do + allow(File).to receive(:read).with(%r{\/(stdlib|test)\/metadata.json}, :encoding => 'utf-8').and_return('{"ĭďèʼnţĩƒіểя": "ċơņťęאּť ỡƒ ţħíš - +この文字"}') + allow(File).to receive(:read).with(%r{\/(stdlib|test)\/metadata.json}).and_return('{"ĭďèʼnţĩƒіểя": "ċơņťęאּť ỡƒ ţħíš - +この文字"}') + end + + let(:prefix) { 'C:' if Puppet::Util::Platform.windows? } + + it 'jsons parse the file' do + allow(scope).to receive(:function_get_module_path).with(['science']).and_return("#{prefix}/path/to/module/") + allow(File).to receive(:exists?).with("#{prefix}/path/to/module/metadata.json").and_return(true) + allow(File).to receive(:read).with("#{prefix}/path/to/module/metadata.json").and_return('{"name": "spencer-science"}') + + result = subject.call(['science']) + expect(result['name']).to eq('spencer-science') + end + + it 'fails by default if there is no metadata.json' do + allow(scope).to receive(:function_get_module_path).with(['science']).and_return("#{prefix}/path/to/module/") + allow(File).to receive(:exists?).with("#{prefix}/path/to/module/metadata.json").and_return(false) + expect { subject.call(['science']) }.to raise_error(Puppet::ParseError) + end + + it 'returns nil if user allows empty metadata.json' do + allow(scope).to receive(:function_get_module_path).with(['science']).and_return("#{prefix}/path/to/module/") + allow(File).to receive(:exists?).with("#{prefix}/path/to/module/metadata.json").and_return(false) + result = subject.call(['science', true]) + expect(result).to eq({}) + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/loadjson_spec.rb b/code/environments/production/modules/stdlib/spec/functions/loadjson_spec.rb new file mode 100644 index 0000000..fc4d97d --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/loadjson_spec.rb @@ -0,0 +1,69 @@ +require 'spec_helper' + +describe 'loadjson' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) } + + describe 'when calling with valid arguments' do + before :each do + allow(File).to receive(:read).with(%r{\/(stdlib|test)\/metadata.json}, :encoding => 'utf-8').and_return('{"name": "puppetlabs-stdlib"}') + allow(File).to receive(:read).with(%r{\/(stdlib|test)\/metadata.json}).and_return('{"name": "puppetlabs-stdlib"}') + end + + context 'when a non-existing file is specified' do + let(:filename) do + if Puppet::Util::Platform.windows? + 'C:/tmp/doesnotexist' + else + '/tmp/doesnotexist' + end + end + + before(:each) do + allow(File).to receive(:exists?).with(filename).and_return(false).once + allow(PSON).to receive(:load).never + end + it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') } + it { is_expected.to run.with_params(filename, 'đẽƒằưļŧ' => '٧ẵłựέ').and_return('đẽƒằưļŧ' => '٧ẵłựέ') } + it { is_expected.to run.with_params(filename, 'デフォルト' => '値').and_return('デフォルト' => '値') } + end + + context 'when an existing file is specified' do + let(:filename) do + if Puppet::Util::Platform.windows? + 'C:/tmp/doesexist' + else + '/tmp/doesexist' + end + end + let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } } + let(:json) { '{"key":"value", {"ķęŷ":"νậŀųề" }, {"キー":"値" }' } + + before(:each) do + allow(File).to receive(:exists?).with(filename).and_return(true).once + allow(File).to receive(:read).with(filename).and_return(json).once + allow(File).to receive(:read).with(filename).and_return(json).once + allow(PSON).to receive(:load).with(json).and_return(data).once + end + it { is_expected.to run.with_params(filename).and_return(data) } + end + + context 'when the file could not be parsed' do + let(:filename) do + if Puppet::Util::Platform.windows? + 'C:/tmp/doesexist' + else + '/tmp/doesexist' + end + end + let(:json) { '{"key":"value"}' } + + before(:each) do + allow(File).to receive(:exists?).with(filename).and_return(true).once + allow(File).to receive(:read).with(filename).and_return(json).once + allow(PSON).to receive(:load).with(json).once.and_raise StandardError, 'Something terrible have happened!' + end + it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') } + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/loadyaml_spec.rb b/code/environments/production/modules/stdlib/spec/functions/loadyaml_spec.rb new file mode 100644 index 0000000..f104ca3 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/loadyaml_spec.rb @@ -0,0 +1,39 @@ +require 'spec_helper' + +describe 'loadyaml' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) } + + context 'when a non-existing file is specified' do + let(:filename) { '/tmp/doesnotexist' } + + before(:each) do + File.expects(:exists?).with(filename).returns(false).once + YAML.expects(:load_file).never + end + it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') } + it { is_expected.to run.with_params(filename, 'đẽƒằưļŧ' => '٧ẵłựέ').and_return('đẽƒằưļŧ' => '٧ẵłựέ') } + it { is_expected.to run.with_params(filename, 'デフォルト' => '値').and_return('デフォルト' => '値') } + end + + context 'when an existing file is specified' do + let(:filename) { '/tmp/doesexist' } + let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } } + + before(:each) do + File.expects(:exists?).with(filename).returns(true).once + YAML.expects(:load_file).with(filename).returns(data).once + end + it { is_expected.to run.with_params(filename).and_return(data) } + end + + context 'when the file could not be parsed' do + let(:filename) { '/tmp/doesexist' } + + before(:each) do + File.expects(:exists?).with(filename).returns(true).once + YAML.stubs(:load_file).with(filename).once.raises StandardError, 'Something terrible have happened!' + end + it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/lstrip_spec.rb b/code/environments/production/modules/stdlib/spec/functions/lstrip_spec.rb new file mode 100644 index 0000000..7c215d9 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/lstrip_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' + +describe 'lstrip' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('Current implementation ignores parameters after the first.') + is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work with}) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work with}) } + 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('') } + it { is_expected.to run.with_params("\t").and_return('') } + it { is_expected.to run.with_params("\t ").and_return('') } + it { is_expected.to run.with_params('one').and_return('one') } + it { is_expected.to run.with_params(' one').and_return('one') } + it { is_expected.to run.with_params(' one').and_return('one') } + it { is_expected.to run.with_params("\tone").and_return('one') } + it { is_expected.to run.with_params("\t one").and_return('one') } + it { is_expected.to run.with_params('one ').and_return('one ') } + it { is_expected.to run.with_params(' one ').and_return('one ') } + it { is_expected.to run.with_params(' one ').and_return('one ') } + it { is_expected.to run.with_params(' ǿňè ').and_return('ǿňè ') } + it { is_expected.to run.with_params("\tone ").and_return('one ') } + it { is_expected.to run.with_params("\t one ").and_return('one ') } + it { is_expected.to run.with_params("one \t").and_return("one \t") } + it { is_expected.to run.with_params(" one \t").and_return("one \t") } + it { is_expected.to run.with_params(" one \t").and_return("one \t") } + it { is_expected.to run.with_params("\tone \t").and_return("one \t") } + it { is_expected.to run.with_params("\t one \t").and_return("one \t") } + it { is_expected.to run.with_params(' o n e ').and_return('o n e ') } + it { is_expected.to run.with_params(AlsoString.new(' one ')).and_return('one ') } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/max_spec.rb b/code/environments/production/modules/stdlib/spec/functions/max_spec.rb new file mode 100644 index 0000000..bacd2fc --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/max_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +describe 'max' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(1).and_return(1) } + it { is_expected.to run.with_params(1, 2).and_return(2) } + it { is_expected.to run.with_params(1, 2, 3).and_return(3) } + it { is_expected.to run.with_params(3, 2, 1).and_return(3) } + it { is_expected.to run.with_params('one').and_return('one') } + it { is_expected.to run.with_params('one', 'two').and_return('two') } + it { is_expected.to run.with_params('one', 'two', 'three').and_return('two') } + it { is_expected.to run.with_params('three', 'two', 'one').and_return('two') } + + describe 'implementation artifacts' do + it { is_expected.to run.with_params(1, 'one').and_return('one') } + it { is_expected.to run.with_params('1', 'one').and_return('one') } + it { is_expected.to run.with_params('1.3e1', '1.4e0').and_return('1.4e0') } + it { is_expected.to run.with_params(1.3e1, 1.4e0).and_return(1.3e1) } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/member_spec.rb b/code/environments/production/modules/stdlib/spec/functions/member_spec.rb new file mode 100644 index 0000000..410c465 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/member_spec.rb @@ -0,0 +1,23 @@ +require 'spec_helper' + +describe 'member' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('Current implementation ignores parameters after the first.') + is_expected.to run.with_params([], [], []).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params([], '').and_return(false) } + it { is_expected.to run.with_params([], ['']).and_return(false) } + it { is_expected.to run.with_params([''], '').and_return(true) } + it { is_expected.to run.with_params([''], ['']).and_return(true) } + it { is_expected.to run.with_params([], 'one').and_return(false) } + it { is_expected.to run.with_params([], ['one']).and_return(false) } + it { is_expected.to run.with_params(['one'], 'one').and_return(true) } + it { is_expected.to run.with_params(['one'], ['one']).and_return(true) } + it { is_expected.to run.with_params(%w[one two three four], %w[four two]).and_return(true) } + it { is_expected.to run.with_params(%w[ọאּẹ ŧẅồ ţҺŗęē ƒơџŕ], %w[ƒơџŕ ŧẅồ]).and_return(true) } + it { is_expected.to run.with_params(%w[one two three four], %w[four five]).and_return(false) } + it { is_expected.to run.with_params(%w[ọאּẹ ŧẅồ ţҺŗęē ƒơџŕ], ['ƒơџŕ', 'ƒί√ə']).and_return(false) } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/merge_spec.rb b/code/environments/production/modules/stdlib/spec/functions/merge_spec.rb new file mode 100644 index 0000000..25e2658 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/merge_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper' + +describe 'merge' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params({}, 'two').and_raise_error(Puppet::ParseError, %r{unexpected argument type String}) } + it { is_expected.to run.with_params({}, 1).and_raise_error(Puppet::ParseError, %r{unexpected argument type (Fixnum|Integer)}) } + it { + pending 'should not special case this' + 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({}) } + describe 'should accept empty strings as puppet undef' do + it { is_expected.to run.with_params({}, '').and_return({}) } + end + it { is_expected.to run.with_params({ 'key' => 'value' }, {}).and_return('key' => 'value') } + it { is_expected.to run.with_params({}, 'key' => 'value').and_return('key' => 'value') } + it { is_expected.to run.with_params({ 'key' => 'value1' }, 'key' => 'value2').and_return('key' => 'value2') } + it { + is_expected.to run \ + .with_params({ 'key1' => 'value1' }, { 'key2' => 'value2' }, 'key3' => 'value3') \ + .and_return('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3') + } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/min_spec.rb b/code/environments/production/modules/stdlib/spec/functions/min_spec.rb new file mode 100644 index 0000000..2c325a4 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/min_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe 'min' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(1).and_return(1) } + it { is_expected.to run.with_params(1, 2).and_return(1) } + it { is_expected.to run.with_params(1, 2, 3).and_return(1) } + it { is_expected.to run.with_params(3, 2, 1).and_return(1) } + it { is_expected.to run.with_params(12, 8).and_return(8) } + it { is_expected.to run.with_params('one').and_return('one') } + it { is_expected.to run.with_params('one', 'two').and_return('one') } + it { is_expected.to run.with_params('one', 'two', 'three').and_return('one') } + it { is_expected.to run.with_params('three', 'two', 'one').and_return('one') } + + describe 'implementation artifacts' do + it { is_expected.to run.with_params(1, 'one').and_return(1) } + it { is_expected.to run.with_params('1', 'one').and_return('1') } + it { is_expected.to run.with_params('1.3e1', '1.4e0').and_return('1.3e1') } + it { is_expected.to run.with_params(1.3e1, 1.4e0).and_return(1.4e0) } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/num2bool_spec.rb b/code/environments/production/modules/stdlib/spec/functions/num2bool_spec.rb new file mode 100644 index 0000000..60533e3 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/num2bool_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe 'num2bool' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(1, 2).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('abc').and_raise_error(Puppet::ParseError, %r{does not look like a number}) } + it { is_expected.to run.with_params(1).and_return(true) } + it { is_expected.to run.with_params('1').and_return(true) } + it { is_expected.to run.with_params(1.5).and_return(true) } + it { is_expected.to run.with_params('1.5').and_return(true) } + it { is_expected.to run.with_params(-1).and_return(false) } + it { is_expected.to run.with_params('-1').and_return(false) } + it { is_expected.to run.with_params(-1.5).and_return(false) } + it { is_expected.to run.with_params('-1.5').and_return(false) } + it { is_expected.to run.with_params(0).and_return(false) } + it { is_expected.to run.with_params('0').and_return(false) } + it { is_expected.to run.with_params([]).and_return(false) } + it { is_expected.to run.with_params('[]').and_raise_error(Puppet::ParseError, %r{does not look like a number}) } + it { is_expected.to run.with_params({}).and_return(false) } + it { is_expected.to run.with_params('{}').and_raise_error(Puppet::ParseError, %r{does not look like a number}) } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/parsejson_spec.rb b/code/environments/production/modules/stdlib/spec/functions/parsejson_spec.rb new file mode 100644 index 0000000..45abe3b --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/parsejson_spec.rb @@ -0,0 +1,66 @@ +require 'spec_helper' + +describe 'parsejson' do + it 'exists' do + is_expected.not_to eq(nil) + end + + it 'raises an error if called without any arguments' do + is_expected.to run.with_params + .and_raise_error(%r{wrong number of arguments}i) + end + + context 'with correct JSON data' do + it 'is able to parse JSON data with a Hash' do + is_expected.to run.with_params('{"a":"1","b":"2"}') + .and_return('a' => '1', 'b' => '2') + end + + it 'is able to parse JSON data with an Array' do + is_expected.to run.with_params('["a","b","c"]') + .and_return(%w[a b c]) + end + + it 'is able to parse empty JSON values' do + actual_array = %w[[] {}] + expected = [[], {}] + actual_array.each_with_index do |actual, index| + is_expected.to run.with_params(actual).and_return(expected[index]) + end + end + + it 'is able to parse JSON data with a mixed structure' do + is_expected.to run.with_params('{"a":"1","b":2,"c":{"d":[true,false]}}') + .and_return('a' => '1', 'b' => 2, 'c' => { 'd' => [true, false] }) + end + + it 'is able to parse JSON data with a UTF8 and double byte characters' do + is_expected.to run.with_params('{"×":"これ","ý":"記号","です":{"©":["Á","ß"]}}') + .and_return('×' => 'これ', 'ý' => '記号', 'です' => { '©' => %w[Á ß] }) + end + + it 'does not return the default value if the data was parsed correctly' do + is_expected.to run.with_params('{"a":"1"}', 'default_value') + .and_return('a' => '1') + end + end + + context 'with incorrect JSON data' do + it 'raises an error with invalid JSON and no default' do + is_expected.to run.with_params('') + .and_raise_error(PSON::ParserError) + end + + it 'supports a structure for a default value' do + is_expected.to run.with_params('', 'a' => '1') + .and_return('a' => '1') + end + + ['', 1, 1.2, nil, true, false, [], {}, :yaml].each do |value| + it "should return the default value for an incorrect #{value.inspect} (#{value.class}) parameter" do + is_expected.to run.with_params(value, 'default_value') + .and_return('default_value') + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/parseyaml_spec.rb b/code/environments/production/modules/stdlib/spec/functions/parseyaml_spec.rb new file mode 100644 index 0000000..bcf0afd --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/parseyaml_spec.rb @@ -0,0 +1,83 @@ +require 'spec_helper' + +describe 'parseyaml' do + it 'exists' do + is_expected.not_to eq(nil) + end + + it 'raises an error if called without any arguments' do + is_expected.to run.with_params + .and_raise_error(%r{wrong number of arguments}i) + end + + context 'with correct YAML data' do + it 'is able to parse a YAML data with a String' do + actual_array = ['--- just a string', 'just a string'] + actual_array.each do |actual| + is_expected.to run.with_params(actual).and_return('just a string') + end + end + + it 'is able to parse YAML data with a Hash' do + is_expected.to run.with_params("---\na: '1'\nb: '2'\n") + .and_return('a' => '1', 'b' => '2') + end + + it 'is able to parse YAML data with an Array' do + is_expected.to run.with_params("---\n- a\n- b\n- c\n") + .and_return(%w[a b c]) + end + + it 'is able to parse YAML data with a mixed structure' do + is_expected.to run.with_params("---\na: '1'\nb: 2\nc:\n d:\n - :a\n - true\n - false\n") + .and_return('a' => '1', 'b' => 2, 'c' => { 'd' => [:a, true, false] }) + end + + it 'is able to parse YAML data with a UTF8 and double byte characters' do + is_expected.to run.with_params("---\na: ×\nこれ: 記号\nです:\n ©:\n - Á\n - ß\n") + .and_return('a' => '×', 'これ' => '記号', 'です' => { '©' => %w[Á ß] }) + end + + it 'does not return the default value if the data was parsed correctly' do + is_expected.to run.with_params("---\na: '1'\n", 'default_value') + .and_return('a' => '1') + end + end + + context 'on a modern ruby', :unless => RUBY_VERSION == '1.8.7' do + it 'raises an error with invalid YAML and no default' do + is_expected.to run.with_params('["one"') + .and_raise_error(Psych::SyntaxError) + end + end + + context 'when running on ruby 1.8.7, which does not have Psych', :if => RUBY_VERSION == '1.8.7' do + it 'raises an error with invalid YAML and no default' do + is_expected.to run.with_params('["one"') + .and_raise_error(ArgumentError) + end + end + + context 'with incorrect YAML data' do + it 'supports a structure for a default value' do + is_expected.to run.with_params('', 'a' => '1') + .and_return('a' => '1') + end + + [1, 1.2, nil, true, false, [], {}, :yaml].each do |value| + it "should return the default value for an incorrect #{value.inspect} (#{value.class}) parameter" do + is_expected.to run.with_params(value, 'default_value') + .and_return('default_value') + end + end + + context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do + ['---', '...', '*8', ''].each do |value| + it "should return the default value for an incorrect #{value.inspect} string parameter" do + is_expected.to run.with_params(value, 'default_value') + .and_return('default_value') + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/pick_default_spec.rb b/code/environments/production/modules/stdlib/spec/functions/pick_default_spec.rb new file mode 100644 index 0000000..2ddaa4b --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/pick_default_spec.rb @@ -0,0 +1,26 @@ +require 'spec_helper' + +describe 'pick_default' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(RuntimeError, %r{Must receive at least one argument}) } + + it { is_expected.to run.with_params('one', 'two').and_return('one') } + it { is_expected.to run.with_params('ớņệ', 'ťωơ').and_return('ớņệ') } + it { is_expected.to run.with_params('', 'two').and_return('two') } + it { is_expected.to run.with_params(:undef, 'two').and_return('two') } + it { is_expected.to run.with_params(:undefined, 'two').and_return('two') } + it { is_expected.to run.with_params(nil, 'two').and_return('two') } + + ['', :undef, :undefined, nil, {}, [], 1, 'default'].each do |value| + describe "when providing #{value.inspect} as default" do + it { is_expected.to run.with_params('one', value).and_return('one') } + it { is_expected.to run.with_params('ớņệ', value).and_return('ớņệ') } + it { is_expected.to run.with_params([], value).and_return([]) } + it { is_expected.to run.with_params({}, value).and_return({}) } + it { is_expected.to run.with_params(value, value).and_return(value) } + it { is_expected.to run.with_params(:undef, value).and_return(value) } + it { is_expected.to run.with_params(:undefined, value).and_return(value) } + it { is_expected.to run.with_params(nil, value).and_return(value) } + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/pick_spec.rb b/code/environments/production/modules/stdlib/spec/functions/pick_spec.rb new file mode 100644 index 0000000..d8c6fbf --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/pick_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' + +describe 'pick' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{must receive at least one non empty value}) } + it { is_expected.to run.with_params('', nil, :undef, :undefined).and_raise_error(Puppet::ParseError, %r{must receive at least one non empty value}) } + it { is_expected.to run.with_params('one', 'two').and_return('one') } + it { is_expected.to run.with_params('', 'two').and_return('two') } + it { is_expected.to run.with_params(:undef, 'two').and_return('two') } + it { is_expected.to run.with_params(:undefined, 'two').and_return('two') } + it { is_expected.to run.with_params(nil, 'two').and_return('two') } + + context 'with UTF8 and double byte characters' do + it { is_expected.to run.with_params(nil, 'このテキスト').and_return('このテキスト') } + it { is_expected.to run.with_params('', 'ŝẳмрłề џţƒ8 ţẽם', 'このテキスト').and_return('ŝẳмрłề џţƒ8 ţẽם') } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/prefix_spec.rb b/code/environments/production/modules/stdlib/spec/functions/prefix_spec.rb new file mode 100644 index 0000000..b4c61ba --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/prefix_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper' + +describe 'prefix' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('Current implementation ignores parameters after the second.') + is_expected.to run.with_params([], 'a', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{expected first argument to be an Array or a Hash}) } + it { is_expected.to run.with_params([], 2).and_raise_error(Puppet::ParseError, %r{expected second argument to be a String}) } + it { is_expected.to run.with_params([]).and_return([]) } + it { is_expected.to run.with_params(['one', 2]).and_return(%w[one 2]) } + it { is_expected.to run.with_params(['ớņệ', 2]).and_return(%w[ớņệ 2]) } + 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(['one'], 'pre').and_return(['preone']) } + it { is_expected.to run.with_params(%w[one two three], 'pre').and_return(%w[preone pretwo prethree]) } + it { is_expected.to run.with_params({}).and_return({}) } + it { is_expected.to run.with_params('key1' => 'value1', 2 => 3).and_return('key1' => 'value1', '2' => 3) } + it { is_expected.to run.with_params({}, '').and_return({}) } + it { is_expected.to run.with_params({ 'key' => 'value' }, '').and_return('key' => 'value') } + it { is_expected.to run.with_params({ 'key' => 'value' }, 'pre').and_return('prekey' => 'value') } + it { + is_expected.to run \ + .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, 'pre') \ + .and_return('prekey1' => 'value1', 'prekey2' => 'value2', 'prekey3' => 'value3') + } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/private_spec.rb b/code/environments/production/modules/stdlib/spec/functions/private_spec.rb new file mode 100644 index 0000000..1efc045 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/private_spec.rb @@ -0,0 +1,52 @@ +require 'spec_helper' + +describe 'private' do + it 'issues a warning' do + scope.expects(:warning).with("private() DEPRECATED: This function will cease to function on Puppet 4; please use assert_private() before upgrading to puppet 4 for backwards-compatibility, or migrate to the new parser's typing system.") # rubocop:disable Metrics/LineLength : unable to cut line to required length + begin + subject.call [] + rescue # rubocop:disable Lint/HandleExceptions + # ignore this + end + end + + context 'when called from inside module' do + it 'does 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 + end + end + + context 'with an explicit failure message' do + it 'prints the failure message on error' 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, %r{failure message!} + end + end + + context 'when called from private class' do + it 'fails 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, %r{Class foo::baz is private} + end + end + + context 'when called from private definition' do + it 'fails 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('definition') + expect { subject.call [] }.to raise_error Puppet::ParseError, %r{Definition foo::baz is private} + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/pw_hash_spec.rb b/code/environments/production/modules/stdlib/spec/functions/pw_hash_spec.rb new file mode 100644 index 0000000..31010d6 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/pw_hash_spec.rb @@ -0,0 +1,79 @@ +require 'spec_helper' + +describe 'pw_hash' do + it { is_expected.not_to eq(nil) } + + context 'when there are less than 3 arguments' do + it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('password').and_raise_error(ArgumentError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('password', 'sha-256').and_raise_error(ArgumentError, %r{wrong number of arguments}i) } + end + + context 'when there are more than 3 arguments' do + it { is_expected.to run.with_params('password', 'sha-256', 'salt', 'extra').and_raise_error(ArgumentError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('password', 'sha-256', 'salt', 'extra', 'extra').and_raise_error(ArgumentError, %r{wrong number of arguments}i) } + end + + context 'when the first argument is not a string' do + it { is_expected.to run.with_params([], 'sha-256', 'salt').and_raise_error(ArgumentError, %r{first argument must be a string}) } + it { is_expected.to run.with_params({}, 'sha-256', 'salt').and_raise_error(ArgumentError, %r{first argument must be a string}) } + it { is_expected.to run.with_params(1, 'sha-256', 'salt').and_raise_error(ArgumentError, %r{first argument must be a string}) } + it { is_expected.to run.with_params(true, 'sha-256', 'salt').and_raise_error(ArgumentError, %r{first argument must be a string}) } + end + + context 'when the first argument is undefined' do + it { is_expected.to run.with_params('', 'sha-256', 'salt').and_return(nil) } + it { is_expected.to run.with_params(nil, 'sha-256', 'salt').and_return(nil) } + end + + context 'when the second argument is not a string' do + it { is_expected.to run.with_params('password', [], 'salt').and_raise_error(ArgumentError, %r{second argument must be a string}) } + it { is_expected.to run.with_params('password', {}, 'salt').and_raise_error(ArgumentError, %r{second argument must be a string}) } + it { is_expected.to run.with_params('password', 1, 'salt').and_raise_error(ArgumentError, %r{second argument must be a string}) } + it { is_expected.to run.with_params('password', true, 'salt').and_raise_error(ArgumentError, %r{second argument must be a string}) } + end + + context 'when the second argument is not one of the supported hashing algorithms' do + it { is_expected.to run.with_params('password', 'no such algo', 'salt').and_raise_error(ArgumentError, %r{is not a valid hash type}) } + end + + context 'when the third argument is not a string' do + it { is_expected.to run.with_params('password', 'sha-256', []).and_raise_error(ArgumentError, %r{third argument must be a string}) } + it { is_expected.to run.with_params('password', 'sha-256', {}).and_raise_error(ArgumentError, %r{third argument must be a string}) } + it { is_expected.to run.with_params('password', 'sha-256', 1).and_raise_error(ArgumentError, %r{third argument must be a string}) } + it { is_expected.to run.with_params('password', 'sha-256', true).and_raise_error(ArgumentError, %r{third argument must be a string}) } + end + + context 'when the third argument is empty' do + it { is_expected.to run.with_params('password', 'sha-512', '').and_raise_error(ArgumentError, %r{third argument must not be empty}) } + end + + context 'when the third argument contains invalid characters' do + it { is_expected.to run.with_params('password', 'sha-512', 'one%').and_raise_error(ArgumentError, %r{characters in salt must be in the set}) } + end + + context 'when running on a platform with a weak String#crypt implementation' do + before(:each) { allow_any_instance_of(String).to receive(:crypt).with('$1$1').and_return('a bad hash') } # rubocop:disable RSpec/AnyInstance : Unable to find a viable replacement + + it { is_expected.to run.with_params('password', 'sha-512', 'salt').and_raise_error(Puppet::ParseError, %r{system does not support enhanced salts}) } + end + + if RUBY_PLATFORM == 'java' || 'test'.crypt('$1$1') == '$1$1$Bp8CU9Oujr9SSEw53WV6G.' + describe 'on systems with enhanced salts support' do + it { is_expected.to run.with_params('password', 'md5', 'salt').and_return('$1$salt$qJH7.N4xYta3aEG/dfqo/0') } + 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 diff --git a/code/environments/production/modules/stdlib/spec/functions/range_spec.rb b/code/environments/production/modules/stdlib/spec/functions/range_spec.rb new file mode 100644 index 0000000..1ae4adb --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/range_spec.rb @@ -0,0 +1,157 @@ +require 'spec_helper' + +describe 'range' do + it { is_expected.not_to eq(nil) } + + describe 'signature validation in puppet3', :unless => RSpec.configuration.puppet_future do + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('Current implementation ignores parameters after the third.') + is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params('1..2..3').and_raise_error(Puppet::ParseError, %r{Unable to compute range}i) } + it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, %r{Unknown range format}i) } + end + + describe 'signature validation in puppet4', :if => RSpec.configuration.puppet_future do + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params.and_raise_error(ArgumentError) + } + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params('').and_raise_error(ArgumentError) + } + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params({}).and_raise_error(ArgumentError) + } + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params([]).and_raise_error(ArgumentError) + } + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params(true).and_raise_error(ArgumentError) + } + it { + is_expected.to run.with_params(1, 2, 'foo').and_raise_error(ArgumentError) + } + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params(1, 2, []).and_raise_error(ArgumentError) + } + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params(1, 2, {}).and_raise_error(ArgumentError) + } + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params(1, 2, true).and_raise_error(ArgumentError) + } + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(ArgumentError) + } + it { + pending 'the puppet 4 implementation' + is_expected.to run.with_params('1..2..3').and_raise_error(ArgumentError) + } + end + + context 'with characters as bounds' do + it { is_expected.to run.with_params('d', 'a').and_return([]) } + it { is_expected.to run.with_params('a', 'a').and_return(['a']) } + it { is_expected.to run.with_params('a', 'b').and_return(%w[a b]) } + it { is_expected.to run.with_params('a', 'd').and_return(%w[a b c d]) } + it { is_expected.to run.with_params('a', 'd', 1).and_return(%w[a b c d]) } + it { is_expected.to run.with_params('a', 'd', '1').and_return(%w[a b c d]) } + it { is_expected.to run.with_params('a', 'd', 2).and_return(%w[a c]) } + it { is_expected.to run.with_params('a', 'd', -2).and_return(%w[a c]) } + it { is_expected.to run.with_params('a', 'd', 3).and_return(%w[a d]) } + it { is_expected.to run.with_params('a', 'd', 4).and_return(['a']) } + end + + context 'with strings as bounds' do + it { is_expected.to run.with_params('onea', 'oned').and_return(%w[onea oneb onec oned]) } + it { is_expected.to run.with_params('two', 'one').and_return([]) } + it { is_expected.to run.with_params('true', 'false').and_return([]) } + it { is_expected.to run.with_params('false', 'true').and_return(['false']) } + end + + context 'with integers as bounds' do + it { is_expected.to run.with_params(4, 1).and_return([]) } + it { is_expected.to run.with_params(1, 1).and_return([1]) } + it { is_expected.to run.with_params(1, 2).and_return([1, 2]) } + it { is_expected.to run.with_params(1, 4).and_return([1, 2, 3, 4]) } + it { is_expected.to run.with_params(1, 4, 1).and_return([1, 2, 3, 4]) } + it { is_expected.to run.with_params(1, 4, '1').and_return([1, 2, 3, 4]) } + it { is_expected.to run.with_params(1, 4, 2).and_return([1, 3]) } + it { is_expected.to run.with_params(1, 4, -2).and_return([1, 3]) } + it { is_expected.to run.with_params(1, 4, 3).and_return([1, 4]) } + it { is_expected.to run.with_params(1, 4, 4).and_return([1]) } + end + + context 'with integers as strings as bounds' do + it { is_expected.to run.with_params('4', '1').and_return([]) } + it { is_expected.to run.with_params('1', '1').and_return([1]) } + it { is_expected.to run.with_params('1', '2').and_return([1, 2]) } + it { is_expected.to run.with_params('1', '4').and_return([1, 2, 3, 4]) } + it { is_expected.to run.with_params('1', '4', 1).and_return([1, 2, 3, 4]) } + it { is_expected.to run.with_params('1', '4', '1').and_return([1, 2, 3, 4]) } + it { is_expected.to run.with_params('1', '4', 2).and_return([1, 3]) } + it { is_expected.to run.with_params('1', '4', -2).and_return([1, 3]) } + it { is_expected.to run.with_params('1', '4', 3).and_return([1, 4]) } + it { is_expected.to run.with_params('1', '4', 4).and_return([1]) } + end + + context 'with prefixed numbers as strings as bounds' do + it { is_expected.to run.with_params('host01', 'host04').and_return(%w[host01 host02 host03 host04]) } + it { is_expected.to run.with_params('01', '04').and_return([1, 2, 3, 4]) } + end + + context 'with prefixed numbers as utf8 strings as bounds' do + it { is_expected.to run.with_params('ħөŝŧ01', 'ħөŝŧ04').and_return(%w[ħөŝŧ01 ħөŝŧ02 ħөŝŧ03 ħөŝŧ04]) } + end + + context 'with prefixed numbers as double byte character strings as bounds' do + it { is_expected.to run.with_params('ホスト01', 'ホスト04').and_return(%w[ホスト01 ホスト02 ホスト03 ホスト04]) } + end + + context 'with dash-range syntax' do + it { is_expected.to run.with_params('4-1').and_return([]) } + it { is_expected.to run.with_params('1-1').and_return([1]) } + it { is_expected.to run.with_params('1-2').and_return([1, 2]) } + it { is_expected.to run.with_params('1-4').and_return([1, 2, 3, 4]) } + end + + context 'with two-dot-range syntax' do + it { is_expected.to run.with_params('4..1').and_return([]) } + it { is_expected.to run.with_params('1..1').and_return([1]) } + it { is_expected.to run.with_params('1..2').and_return([1, 2]) } + it { is_expected.to run.with_params('1..4').and_return([1, 2, 3, 4]) } + end + + context 'with three-dot-range syntax' do + it { is_expected.to run.with_params('4...1').and_return([]) } + it { is_expected.to run.with_params('1...1').and_return([]) } + it { is_expected.to run.with_params('1...2').and_return([1]) } + it { is_expected.to run.with_params('1...3').and_return([1, 2]) } + it { is_expected.to run.with_params('1...5').and_return([1, 2, 3, 4]) } + end + + describe 'when passing mixed arguments as bounds' do + it { + pending('these bounds should not be allowed as ruby will OOM hard. e.g. `(\'host0\'..\'hosta\').to_a` has 3239930 elements on ruby 1.9, adding more \'0\'s and \'a\'s increases that exponentially') # rubocop:disable Metrics/LineLength : unable to cut line to required length + is_expected.to run.with_params('0', 'a').and_raise_error(Puppet::ParseError, %r{cannot interpolate between numeric and non-numeric bounds}) + } + it { + pending('these bounds should not be allowed as ruby will OOM hard. e.g. `(\'host0\'..\'hosta\').to_a` has 3239930 elements on ruby 1.9, adding more \'0\'s and \'a\'s increases that exponentially') # rubocop:disable Metrics/LineLength : unable to cut line to required length + is_expected.to run.with_params(0, 'a').and_raise_error(Puppet::ParseError, %r{cannot interpolate between numeric and non-numeric bounds}) + } + it { + pending('these bounds should not be allowed as ruby will OOM hard. e.g. `(\'host0\'..\'hosta\').to_a` has 3239930 elements on ruby 1.9, adding more \'0\'s and \'a\'s increases that exponentially') # rubocop:disable Metrics/LineLength : unable to cut line to required length + is_expected.to run.with_params('h0', 'ha').and_raise_error(Puppet::ParseError, %r{cannot interpolate between numeric and non-numeric bounds}) + } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/regexpescape_spec.rb b/code/environments/production/modules/stdlib/spec/functions/regexpescape_spec.rb new file mode 100644 index 0000000..d5b1f15 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/regexpescape_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +describe 'regexpescape' do + describe 'signature validation' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('Current implementation ignores parameters after the first.') + is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } + it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } + it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } + end + + describe 'handling normal strings' do + it 'calls ruby\'s Regexp.escape function' do + Regexp.expects(:escape).with('regexp_string').returns('escaped_regexp_string').once + is_expected.to run.with_params('regexp_string').and_return('escaped_regexp_string') + end + end + + describe 'handling classes derived from String' do + it 'calls ruby\'s Regexp.escape function' do + regexp_string = AlsoString.new('regexp_string') + Regexp.expects(:escape).with(regexp_string).returns('escaped_regexp_string').once + is_expected.to run.with_params(regexp_string).and_return('escaped_regexp_string') + end + end + + describe 'strings in arrays handling' do + it { is_expected.to run.with_params([]).and_return([]) } + it { is_expected.to run.with_params(['one*', 'two']).and_return(['one\*', 'two']) } + it { is_expected.to run.with_params(['one*', 1, true, {}, 'two']).and_return(['one\*', 1, true, {}, 'two']) } + + context 'with UTF8 and double byte characters' do + it { is_expected.to run.with_params(['ŏʼnε*']).and_return(['ŏʼnε\*']) } + it { is_expected.to run.with_params(['インターネット*']).and_return(['インターネット\*']) } + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/reject_spec.rb b/code/environments/production/modules/stdlib/spec/functions/reject_spec.rb new file mode 100644 index 0000000..f083e74 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/reject_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe 'reject' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params([], 'pattern', 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + + it { + pending('reject does not actually check this, and raises NoMethodError instead') + is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{first argument not an array}) + } + it { + pending('reject does not actually check this, and raises NoMethodError instead') + is_expected.to run.with_params(1, 'two').and_raise_error(Puppet::ParseError, %r{first argument not an array}) + } + it { is_expected.to run.with_params([], 'two').and_return([]) } + it { is_expected.to run.with_params(%w[one two three], 'two').and_return(%w[one three]) } + it { is_expected.to run.with_params(%w[one two three], 't(wo|hree)').and_return(['one']) } + it { is_expected.to run.with_params(%w[όʼnệ ţщồ ţңяέέ], 'ţ(щồ|ңяέέ)').and_return(['όʼnệ']) } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/reverse_spec.rb b/code/environments/production/modules/stdlib/spec/functions/reverse_spec.rb new file mode 100644 index 0000000..6573fa7 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/reverse_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +describe 'reverse' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{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, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } + it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } + it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } + 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(['one']).and_return(['one']) } + it { is_expected.to run.with_params(%w[one two three]).and_return(%w[three two one]) } + it { is_expected.to run.with_params(%w[one two three four]).and_return(%w[four three two one]) } + it { is_expected.to run.with_params(%w[ổňë ťŵọ ŧңяəė ƒŏůŗ]).and_return(%w[ƒŏůŗ ŧңяəė ťŵọ ổňë]) } + + 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('abc').and_return('cba') } + it { is_expected.to run.with_params('abcd').and_return('dcba') } + it { is_expected.to run.with_params('āβćđ').and_return('đćβā') } + + context 'when using a class extending String' do + it 'calls its reverse method' do + value = AlsoString.new('asdfghjkl') + value.expects(:reverse).returns('foo') + expect(subject).to run.with_params(value).and_return('foo') + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/round_spec.rb b/code/environments/production/modules/stdlib/spec/functions/round_spec.rb new file mode 100644 index 0000000..af99afd --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/round_spec.rb @@ -0,0 +1,14 @@ +require 'spec_helper' + +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) } + 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) } + 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 diff --git a/code/environments/production/modules/stdlib/spec/functions/rstrip_spec.rb b/code/environments/production/modules/stdlib/spec/functions/rstrip_spec.rb new file mode 100644 index 0000000..c8cbeb1 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/rstrip_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' + +describe 'rstrip' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('Current implementation ignores parameters after the first.') + is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work with}) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work with}) } + 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('') } + it { is_expected.to run.with_params("\t").and_return('') } + it { is_expected.to run.with_params("\t ").and_return('') } + it { is_expected.to run.with_params('one').and_return('one') } + it { is_expected.to run.with_params(' one').and_return(' one') } + it { is_expected.to run.with_params(' one').and_return(' one') } + it { is_expected.to run.with_params("\tone").and_return("\tone") } + it { is_expected.to run.with_params("\t one").and_return("\t one") } + it { is_expected.to run.with_params('one ').and_return('one') } + it { is_expected.to run.with_params(' one ').and_return(' one') } + it { is_expected.to run.with_params(' one ').and_return(' one') } + it { is_expected.to run.with_params(' ǿňè ').and_return(' ǿňè') } + it { is_expected.to run.with_params("\tone ").and_return("\tone") } + it { is_expected.to run.with_params("\t one ").and_return("\t one") } + it { is_expected.to run.with_params("one\t").and_return('one') } + it { is_expected.to run.with_params(" one\t").and_return(' one') } + it { is_expected.to run.with_params(" one\t").and_return(' one') } + it { is_expected.to run.with_params("\tone\t").and_return("\tone") } + it { is_expected.to run.with_params("\t one\t").and_return("\t one") } + it { is_expected.to run.with_params(' o n e ').and_return(' o n e') } + it { is_expected.to run.with_params(AlsoString.new(' one ')).and_return(' one') } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/seeded_rand_spec.rb b/code/environments/production/modules/stdlib/spec/functions/seeded_rand_spec.rb new file mode 100644 index 0000000..9b7078a --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/seeded_rand_spec.rb @@ -0,0 +1,58 @@ +require 'spec_helper' + +describe 'seeded_rand' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(1).and_raise_error(ArgumentError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(0, '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) } + it { is_expected.to run.with_params(1.5, '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) } + it { is_expected.to run.with_params(-10, '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) } + it { is_expected.to run.with_params('-10', '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) } + it { is_expected.to run.with_params('string', '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) } + it { is_expected.to run.with_params([], '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) } + it { is_expected.to run.with_params({}, '').and_raise_error(ArgumentError, %r{first argument must be a positive integer}) } + it { is_expected.to run.with_params(1, 1).and_raise_error(ArgumentError, %r{second argument must be a string}) } + it { is_expected.to run.with_params(1, []).and_raise_error(ArgumentError, %r{second argument must be a string}) } + it { is_expected.to run.with_params(1, {}).and_raise_error(ArgumentError, %r{second argument must be a string}) } + + it 'provides a random number strictly less than the given max' do + expect(seeded_rand(3, 'seed')).to satisfy { |n| n.to_i < 3 } # rubocop:disable Lint/AmbiguousBlockAssociation : Cannot parenthesize without break code or violating other Rubocop rules + end + + it 'provides a random number greater or equal to zero' do + expect(seeded_rand(3, 'seed')).to satisfy { |n| n.to_i >= 0 } # rubocop:disable Lint/AmbiguousBlockAssociation : Cannot parenthesize without break code or violating other Rubocop rules + end + + it "provides the same 'random' value on subsequent calls for the same host" do + expect(seeded_rand(10, 'seed')).to eql(seeded_rand(10, 'seed')) + end + + it 'allows seed to control the random value on a single host' do + first_random = seeded_rand(1000, 'seed1') + second_different_random = seeded_rand(1000, 'seed2') + + expect(first_random).not_to eql(second_different_random) + end + + it 'does not return different values for different hosts' do + val1 = seeded_rand(1000, 'foo', :host => 'first.host.com') + val2 = seeded_rand(1000, 'foo', :host => 'second.host.com') + + expect(val1).to eql(val2) + end + + def seeded_rand(max, seed, args = {}) + host = args[:host] || '127.0.0.1' + + # workaround not being able to use let(:facts) because some tests need + # multiple different hostnames in one context + scope.stubs(:lookupvar).with('::fqdn', {}).returns(host) + + scope.function_seeded_rand([max, seed]) + end + + context 'with UTF8 and double byte characters' do + it { is_expected.to run.with_params(1000, 'ǿňè') } + it { is_expected.to run.with_params(1000, '文字列') } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/shell_escape_spec.rb b/code/environments/production/modules/stdlib/spec/functions/shell_escape_spec.rb new file mode 100644 index 0000000..ed63bc3 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/shell_escape_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper' + +describe 'shell_escape' do + it { is_expected.not_to eq(nil) } + + describe 'signature validation' do + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('foo', 'bar').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + end + + describe 'stringification' do + it { is_expected.to run.with_params(10).and_return('10') } + it { is_expected.to run.with_params(false).and_return('false') } + end + + describe 'escaping' do + it { is_expected.to run.with_params('foo').and_return('foo') } + it { is_expected.to run.with_params('foo bar').and_return('foo\ bar') } + it { + is_expected.to run.with_params('~`!@#$%^&*()_+-=[]\{}|;\':",./<>?') + .and_return('\~\`\!@\#\$\%\^\&\*\(\)_\+-\=\[\]\\\\\{\}\|\;\\\':\",./\<\>\?') + } + end + + context 'with UTF8 and double byte characters' do + it { is_expected.to run.with_params('スペー スを含むテ キスト').and_return('\\ス\\ペ\\ー\\ \\ス\\を\\含\\む\\テ\\ \\ \\キ\\ス\\ト') } + it { is_expected.to run.with_params('μťƒ 8 ŧĕχť').and_return('\\μ\\ť\\ƒ\\ 8\\ \\ \\ŧ\\ĕ\\χ\\ť') } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/shell_join_spec.rb b/code/environments/production/modules/stdlib/spec/functions/shell_join_spec.rb new file mode 100644 index 0000000..b1f7ff2 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/shell_join_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +describe 'shell_join' do + it { is_expected.not_to eq(nil) } + + describe 'signature validation' do + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(['foo'], ['bar']).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('foo').and_raise_error(Puppet::ParseError, %r{is not an Array}i) } + end + + describe 'shell argument joining' do + it { is_expected.to run.with_params(['foo']).and_return('foo') } + it { is_expected.to run.with_params(%w[foo bar]).and_return('foo bar') } + it { is_expected.to run.with_params(['foo', 'bar baz']).and_return('foo bar\ baz') } + it { + is_expected.to run.with_params(['~`!@#$', '%^&*()_+-=', '[]\{}|;\':"', ',./<>?']) + .and_return('\~\`\!@\#\$ \%\^\&\*\(\)_\+-\= \[\]\\\\\{\}\|\;\\\':\" ,./\<\>\?') + } + + context 'with UTF8 and double byte characters' do + it { is_expected.to run.with_params(%w[μťƒ 8 ŧĕχť]).and_return('\\μ\\ť\\ƒ 8 \\ŧ\\ĕ\\χ\\ť') } + it { is_expected.to run.with_params(['スペー', 'スを含むテ', ' キスト']).and_return('\\ス\\ペ\\ー \\ス\\を\\含\\む\\テ \\ \\キ\\ス\\ト') } + end + end + + describe 'stringification' do + it { is_expected.to run.with_params([10, false, 'foo']).and_return('10 false foo') } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/shell_split_spec.rb b/code/environments/production/modules/stdlib/spec/functions/shell_split_spec.rb new file mode 100644 index 0000000..beda043 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/shell_split_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +describe 'shell_split' do + it { is_expected.not_to eq(nil) } + + describe 'signature validation' do + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('foo', 'bar').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + end + + describe 'stringification' do + it { is_expected.to run.with_params(10).and_return(['10']) } + it { is_expected.to run.with_params(false).and_return(['false']) } + end + + describe 'shell line spliting' do + it { is_expected.to run.with_params('foo').and_return(['foo']) } + it { is_expected.to run.with_params('foo bar').and_return(%w[foo bar]) } + it { + is_expected.to run.with_params('\~\`\!@\#\$\%\^\&\*\(\)_\+-\=\[\]\\\\\{\}\|\;\\\':\",./\<\>\?') + .and_return(['~`!@#$%^&*()_+-=[]\{}|;\':",./<>?']) + } + it { + is_expected.to run.with_params('\~\`\!@\#\$ \%\^\&\*\(\)_\+-\= \[\]\\\\\{\}\|\;\\\':\" ,./\<\>\?') + .and_return(['~`!@#$', '%^&*()_+-=', '[]\{}|;\':"', ',./<>?']) + } + + context 'with UTF8 and double byte characters' do + it { is_expected.to run.with_params('\\μ\\ť\\ƒ 8 \\ŧ\\ĕ\\χ\\ť').and_return(%w[μťƒ 8 ŧĕχť]) } + it { is_expected.to run.with_params('\\ス\\ペ\\ー \\ス\\を\\含\\む\\テ \\ \\キ\\ス\\ト').and_return(['スペー', 'スを含むテ', ' キスト']) } + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/shuffle_spec.rb b/code/environments/production/modules/stdlib/spec/functions/shuffle_spec.rb new file mode 100644 index 0000000..eba3afa --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/shuffle_spec.rb @@ -0,0 +1,38 @@ +require 'spec_helper' + +describe 'shuffle' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{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, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } + it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } + it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } + + context 'when running with a specific seed' do + # make tests deterministic + before(:each) { srand(2) } + + 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(['one']).and_return(['one']) } + it { is_expected.to run.with_params(%w[one two three]).and_return(%w[two one three]) } + it { is_expected.to run.with_params(%w[one two three four]).and_return(%w[four three two one]) } + + 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('abc').and_return('bac') } + it { is_expected.to run.with_params('abcd').and_return('dcba') } + + context 'with UTF8 and double byte characters' do + it { is_expected.to run.with_params('ůţƒ8 ŧέχŧ şŧґíńģ').and_return('ģńş ůχţέƒŧí8ґŧŧ ') } + it { is_expected.to run.with_params('日本語の文字列').and_return('字本日語文列の') } + end + + context 'when using a class extending String' do + it { is_expected.to run.with_params(AlsoString.new('asdfghjkl')).and_return('lkhdsfajg') } + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/size_spec.rb b/code/environments/production/modules/stdlib/spec/functions/size_spec.rb new file mode 100644 index 0000000..a4079ea --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/size_spec.rb @@ -0,0 +1,38 @@ +require 'spec_helper' + +describe 'size' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{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, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Unknown type given}) } + it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{Unknown type given}) } + it { is_expected.to run.with_params('1').and_raise_error(Puppet::ParseError, %r{Requires either string, array or hash to work}) } + it { is_expected.to run.with_params('1.0').and_raise_error(Puppet::ParseError, %r{Requires either string, array or hash to work}) } + it { is_expected.to run.with_params([]).and_return(0) } + it { is_expected.to run.with_params(['a']).and_return(1) } + it { is_expected.to run.with_params(%w[one two three]).and_return(3) } + it { is_expected.to run.with_params(%w[one two three four]).and_return(4) } + + it { is_expected.to run.with_params({}).and_return(0) } + it { is_expected.to run.with_params('1' => '2').and_return(1) } + it { is_expected.to run.with_params('1' => '2', '4' => '4').and_return(2) } + it { is_expected.to run.with_params('€' => '@', '竹' => 'ǿňè').and_return(2) } + + it { is_expected.to run.with_params('').and_return(0) } + it { is_expected.to run.with_params('a').and_return(1) } + it { is_expected.to run.with_params('abc').and_return(3) } + it { is_expected.to run.with_params('abcd').and_return(4) } + it { is_expected.to run.with_params('万').and_return(1) } + it { is_expected.to run.with_params('āβćđ').and_return(4) } + + context 'when using a class extending String' do + it 'calls its size method' do + value = AlsoString.new('asdfghjkl') + value.expects(:size).returns('foo') + expect(subject).to run.with_params(value).and_return('foo') + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/sort_spec.rb b/code/environments/production/modules/stdlib/spec/functions/sort_spec.rb new file mode 100644 index 0000000..f21d19f --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/sort_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +describe 'sort' do + describe 'signature validation' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params([], 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('stricter input checking') + is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{requires string or array}) + } + it { + pending('stricter input checking') + is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{requires string or array}) + } + it { + pending('stricter input checking') + is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{requires string or array}) + } + 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(%w[c b a]).and_return(%w[a b c]) } + 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('cbda').and_return('abcd') } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/sprintf_hash_spec.rb b/code/environments/production/modules/stdlib/spec/functions/sprintf_hash_spec.rb new file mode 100644 index 0000000..4bead46 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/sprintf_hash_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +describe 'sprintf_hash' do + it 'exists' do + is_expected.not_to eq(nil) + end + + context 'with param count' do + it 'fails with no arguments' do + is_expected.to run.with_params.and_raise_error(ArgumentError, %r{expects 2 arguments}i) + end + it 'fails with 1 argument' do + is_expected.to run.with_params('').and_raise_error(ArgumentError, %r{expects 2 arguments}i) + end + it 'fails with too many arguments' do + is_expected.to run.with_params('', '', '').and_raise_error(ArgumentError, %r{expects 2 arguments}i) + end + end + + context 'with param type' do + it 'fails with wrong format type' do + is_expected.to run.with_params(false, {}).and_raise_error(ArgumentError, %r{parameter 'format' expects a String value}i) + end + it 'fails with wrong arguments type' do + is_expected.to run.with_params('', false).and_raise_error(ArgumentError, %r{parameter 'arguments' expects a Hash value}i) + end + end + + it 'prints formats with name placeholders' do + is_expected.to run.with_params('string %<foo>s and integer %<bar>b', 'foo' => '_foo_', 'bar' => 5) # rubocop:disable Style/FormatStringToken : Template tokens needed for purposes of test + .and_return('string _foo_ and integer 101') + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/squeeze_spec.rb b/code/environments/production/modules/stdlib/spec/functions/squeeze_spec.rb new file mode 100644 index 0000000..05bd884 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/squeeze_spec.rb @@ -0,0 +1,50 @@ +require 'spec_helper' + +describe 'squeeze' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('', '', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(1).and_raise_error(NoMethodError) } + it { is_expected.to run.with_params({}).and_raise_error(NoMethodError) } + it { is_expected.to run.with_params(true).and_raise_error(NoMethodError) } + + context 'when squeezing a single 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('aaaaaaaaa').and_return('a') } + it { is_expected.to run.with_params('aaaaaaaaa', 'a').and_return('a') } + it { is_expected.to run.with_params('aaaaaaaaabbbbbbbbbbcccccccccc', 'b-c').and_return('aaaaaaaaabc') } + end + + context 'with UTF8 and double byte characters' do + 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('ậậậậậậậậậậậậậậậậậ語語語語©') } + end + + context 'when squeezing values in an array' do + it { + is_expected.to run \ + .with_params(['', 'a', 'aaaaaaaaa', 'aaaaaaaaabbbbbbbbbbcccccccccc']) \ + .and_return(['', 'a', 'a', 'abc']) + } + it { + is_expected.to run \ + .with_params(['', 'a', 'aaaaaaaaa', 'aaaaaaaaabbbbbbbbbbcccccccccc'], 'a') \ + .and_return(['', 'a', 'a', 'abbbbbbbbbbcccccccccc']) + } + it { + is_expected.to run \ + .with_params(['', 'a', 'aaaaaaaaa', 'aaaaaaaaabbbbbbbbbbcccccccccc'], 'b-c') \ + .and_return(['', 'a', 'aaaaaaaaa', 'aaaaaaaaabc']) + } + end + + context 'when using a class extending String' do + it 'calls its squeeze method' do + value = AlsoString.new('aaaaaaaaa') + value.expects(:squeeze).returns('foo') + expect(subject).to run.with_params(value).and_return('foo') + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/str2bool_spec.rb b/code/environments/production/modules/stdlib/spec/functions/str2bool_spec.rb new file mode 100644 index 0000000..fe43c4c --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/str2bool_spec.rb @@ -0,0 +1,23 @@ +require 'spec_helper' + +describe 'str2bool' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('Current implementation ignores parameters after the first.') + is_expected.to run.with_params('true', 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{Unknown type of boolean given}) } + + describe 'when testing values that mean "true"' do + ['TRUE', '1', 't', 'y', 'true', 'yes', true].each do |value| + it { is_expected.to run.with_params(value).and_return(true) } + end + end + + describe 'when testing values that mean "false"' do + ['FALSE', '', '0', 'f', 'n', 'false', 'no', false, 'undef', 'undefined'].each do |value| + it { is_expected.to run.with_params(value).and_return(false) } + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/str2saltedsha512_spec.rb b/code/environments/production/modules/stdlib/spec/functions/str2saltedsha512_spec.rb new file mode 100644 index 0000000..e7513c9 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/str2saltedsha512_spec.rb @@ -0,0 +1,26 @@ +require 'spec_helper' + +describe 'str2saltedsha512' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('password', 2).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires a String argument}) } + + context 'when running with a specific seed' do + # make tests deterministic + before(:each) { srand(2) } + + it { + is_expected.to run.with_params('') + .and_return('0f8a612f4eeed08e47b3875d00f33c5688f7926298f2d9b5fe19d1323f910bc78b6f7b5892596d2fabaa65e7a8d99b3768c102610cf0432c4827eee01f09451e3fae4f7a') + } + it { + is_expected.to run.with_params('password') + .and_return('0f8a612f43134376566c5707718d600effcfb17581fc9d3fa64d7f447dfda317c174ffdb498d2c5bd5c2075dab41c9d7ada5afbdc6b55354980eb5ba61802371e6b64956') + } + it { + is_expected.to run.with_params('verylongpassword') + .and_return('0f8a612f7a448537540e062daa8621f9bae326ca8ccb899e1bdb10e7c218cebfceae2530b856662565fdc4d81e986fc50cfbbc46d50436610ed9429ff5e43f2c45b5d039') + } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/strftime_spec.rb b/code/environments/production/modules/stdlib/spec/functions/strftime_spec.rb new file mode 100644 index 0000000..92a6893 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/strftime_spec.rb @@ -0,0 +1,26 @@ +require 'spec_helper' + +describe 'strftime' do + it 'exists' do + expect(Puppet::Parser::Functions.function('strftime')).to eq('function_strftime') + end + + it 'raises a ParseError if there is less than 1 arguments' do + expect { scope.function_strftime([]) }.to(raise_error(Puppet::ParseError)) + end + + it 'using %s should be higher then when I wrote this test' do + result = scope.function_strftime(['%s']) + expect(result.to_i).to(be > 1_311_953_157) + end + + it 'using %s should be greater than 1.5 trillion' do + result = scope.function_strftime(['%s']) + expect(result.to_i).to(be > 1_500_000_000) + end + + it 'returns a date when given %Y-%m-%d' do + result = scope.function_strftime(['%Y-%m-%d']) + expect(result).to match(%r{^\d{4}-\d{2}-\d{2}$}) + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/strip_spec.rb b/code/environments/production/modules/stdlib/spec/functions/strip_spec.rb new file mode 100644 index 0000000..e13f33a --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/strip_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' + +describe 'strip' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('Current implementation ignores parameters after the first.') + is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work with}) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work with}) } + 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('') } + it { is_expected.to run.with_params("\t").and_return('') } + it { is_expected.to run.with_params("\t ").and_return('') } + it { is_expected.to run.with_params('one').and_return('one') } + it { is_expected.to run.with_params(' one').and_return('one') } + it { is_expected.to run.with_params(' one').and_return('one') } + it { is_expected.to run.with_params("\tone").and_return('one') } + it { is_expected.to run.with_params("\t one").and_return('one') } + it { is_expected.to run.with_params('one ').and_return('one') } + it { is_expected.to run.with_params(' one ').and_return('one') } + it { is_expected.to run.with_params(' one ').and_return('one') } + it { is_expected.to run.with_params("\tone ").and_return('one') } + it { is_expected.to run.with_params("\t one ").and_return('one') } + it { is_expected.to run.with_params("one \t").and_return('one') } + it { is_expected.to run.with_params(" one \t").and_return('one') } + it { is_expected.to run.with_params(" one \t").and_return('one') } + it { is_expected.to run.with_params("\tone \t").and_return('one') } + it { is_expected.to run.with_params("\t one \t").and_return('one') } + it { is_expected.to run.with_params(' o n e ').and_return('o n e') } + it { is_expected.to run.with_params(' ỏŋέ ').and_return('ỏŋέ') } + it { is_expected.to run.with_params(AlsoString.new(' one ')).and_return('one') } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/suffix_spec.rb b/code/environments/production/modules/stdlib/spec/functions/suffix_spec.rb new file mode 100644 index 0000000..f4d5bc1 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/suffix_spec.rb @@ -0,0 +1,40 @@ +require 'spec_helper' + +describe 'suffix' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('Current implementation ignores parameters after the second.') + is_expected.to run.with_params([], 'a', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{expected first argument to be an Array}) } + it { is_expected.to run.with_params([], 2).and_raise_error(Puppet::ParseError, %r{expected second argument to be a String}) } + it { is_expected.to run.with_params([]).and_return([]) } + it { is_expected.to run.with_params(['one', 2]).and_return(%w[one 2]) } + 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(['one'], 'post').and_return(['onepost']) } + it { is_expected.to run.with_params(%w[one two three], 'post').and_return(%w[onepost twopost threepost]) } + 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('key1' => 'value1', 2 => 3).and_return('key1' => 'value1', '2' => 3) + } + it { + is_expected.to run.with_params({}, '').and_return({}) + } + it { + is_expected.to run.with_params({ 'key' => 'value' }, '').and_return('key' => 'value') + } + it { + is_expected.to run.with_params({ 'key' => 'value' }, 'post').and_return('keypost' => 'value') + } + it { + is_expected.to run \ + .with_params({ 'key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3' }, 'post') \ + .and_return('key1post' => 'value1', 'key2post' => 'value2', 'key3post' => 'value3') + } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/swapcase_spec.rb b/code/environments/production/modules/stdlib/spec/functions/swapcase_spec.rb new file mode 100644 index 0000000..a970d71 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/swapcase_spec.rb @@ -0,0 +1,40 @@ +require 'spec_helper' + +describe 'swapcase' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('Current implementation ignores parameters after the first.') + is_expected.to run.with_params('a', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } + it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } + it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } + describe 'with strings as inputs' do + it { is_expected.to run.with_params('').and_return('') } + it { is_expected.to run.with_params('one').and_return('ONE') } + it { is_expected.to run.with_params('ONE').and_return('one') } + it { is_expected.to run.with_params('oNe').and_return('OnE') } + end + describe 'with arrays as inputs' do + it { is_expected.to run.with_params([]).and_return([]) } + describe 'only containing strings' do + it { is_expected.to run.with_params(['']).and_return(['']) } + it { is_expected.to run.with_params(['one']).and_return(['ONE']) } + it { is_expected.to run.with_params(['ONE']).and_return(['one']) } + it { is_expected.to run.with_params(['oNe']).and_return(['OnE']) } + it { is_expected.to run.with_params(%w[one ONE]).and_return(%w[ONE one]) } + it { is_expected.to run.with_params(%w[ONE OnE]).and_return(%w[one oNe]) } + it { is_expected.to run.with_params(%w[oNe one]).and_return(%w[OnE ONE]) } + end + describe 'containing mixed types' do + it { is_expected.to run.with_params(['OnE', {}]).and_return(['oNe', {}]) } + it { is_expected.to run.with_params(['OnE', 1]).and_return(['oNe', 1]) } + it { is_expected.to run.with_params(['OnE', []]).and_return(['oNe', []]) } + it { is_expected.to run.with_params(['OnE', ['two']]).and_return(['oNe', ['two']]) } + end + end + it 'accepts objects which extend String' do + is_expected.to run.with_params(AlsoString.new('OnE')).and_return('oNe') + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/time_spec.rb b/code/environments/production/modules/stdlib/spec/functions/time_spec.rb new file mode 100644 index 0000000..2d9472b --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/time_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +describe 'time' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params('a', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + + context 'when running at a specific time' do + before(:each) do + # get a value before stubbing the function + test_time = Time.utc(2006, 10, 13, 8, 15, 11) + Time.expects(:new).with.returns(test_time).once + end + it { is_expected.to run.with_params.and_return(1_160_727_311) } + it { is_expected.to run.with_params('').and_return(1_160_727_311) } + it { is_expected.to run.with_params([]).and_return(1_160_727_311) } + it { is_expected.to run.with_params({}).and_return(1_160_727_311) } + it { is_expected.to run.with_params('foo').and_return(1_160_727_311) } + it { is_expected.to run.with_params('UTC').and_return(1_160_727_311) } + it { is_expected.to run.with_params('America/New_York').and_return(1_160_727_311) } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/to_bytes_spec.rb b/code/environments/production/modules/stdlib/spec/functions/to_bytes_spec.rb new file mode 100644 index 0000000..f3efd90 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/to_bytes_spec.rb @@ -0,0 +1,70 @@ +require 'spec_helper' + +describe 'to_bytes' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('1', 'extras').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params([]).and_raise_error(TypeError, %r{(can't convert|no implicit conversion of) Array (in)?to String}) } + it { is_expected.to run.with_params({}).and_raise_error(TypeError, %r{(can't convert|no implicit conversion of) Hash (in)?to String}) } + it { is_expected.to run.with_params(true).and_raise_error(TypeError, %r{(can't convert|no implicit conversion of) (TrueClass|true) (in)?to String}) } + + describe 'when passing numbers' do + it { is_expected.to run.with_params(0).and_return(0) } + it { is_expected.to run.with_params(1).and_return(1) } + it { is_expected.to run.with_params(-1).and_return(-1) } + it { is_expected.to run.with_params(1.1).and_return(1.1) } + it { is_expected.to run.with_params(-1.1).and_return(-1.1) } + end + + describe 'when passing numbers as strings' do + describe 'without a unit' do + it { is_expected.to run.with_params('1').and_return(1) } + it { is_expected.to run.with_params('-1').and_return(-1) } + # these are so wrong + it { is_expected.to run.with_params('1.1').and_return(1) } + it { is_expected.to run.with_params('-1.1').and_return(-1) } + end + + describe 'with a unit' do + it { is_expected.to run.with_params('1k').and_return(1024) } + it { is_expected.to run.with_params('-1kB').and_return(-1024) } + it { is_expected.to run.with_params('1M').and_return(1024 * 1024) } + it { is_expected.to run.with_params('1G').and_return(1024 * 1024 * 1024) } + it { is_expected.to run.with_params('1T').and_return(1024 * 1024 * 1024 * 1024) } + it { is_expected.to run.with_params('1P').and_return(1024 * 1024 * 1024 * 1024 * 1024) } + it { is_expected.to run.with_params('1E').and_return(1024 * 1024 * 1024 * 1024 * 1024 * 1024) } + it { is_expected.to run.with_params('1.5e3M').and_return(1_572_864_000) } + + it { is_expected.to run.with_params('4k').and_return(4 * 1024) } + it { is_expected.to run.with_params('-4kB').and_return(4 * -1024) } + it { is_expected.to run.with_params('4M').and_return(4 * 1024 * 1024) } + it { is_expected.to run.with_params('4G').and_return(4 * 1024 * 1024 * 1024) } + it { is_expected.to run.with_params('4T').and_return(4 * 1024 * 1024 * 1024 * 1024) } + it { is_expected.to run.with_params('4P').and_return(4 * 1024 * 1024 * 1024 * 1024 * 1024) } + it { is_expected.to run.with_params('4E').and_return(4 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024) } + + # these are so wrong + it { is_expected.to run.with_params('1.0001 k').and_return(1024) } + it { is_expected.to run.with_params('-1.0001 kB').and_return(-1024) } + end + + describe 'with a unknown unit' do + it { is_expected.to run.with_params('1KB').and_raise_error(Puppet::ParseError, %r{Unknown prefix}) } + it { is_expected.to run.with_params('1K').and_raise_error(Puppet::ParseError, %r{Unknown prefix}) } + it { is_expected.to run.with_params('1mb').and_raise_error(Puppet::ParseError, %r{Unknown prefix}) } + it { is_expected.to run.with_params('1m').and_raise_error(Puppet::ParseError, %r{Unknown prefix}) } + it { is_expected.to run.with_params('1%').and_raise_error(Puppet::ParseError, %r{Unknown prefix}) } + it { is_expected.to run.with_params('1 p').and_raise_error(Puppet::ParseError, %r{Unknown prefix}) } + end + end + + # these are so wrong + describe 'when passing random stuff' do + it { is_expected.to run.with_params('-1....1').and_return(-1) } + it { is_expected.to run.with_params('-1.e.e.e.1').and_return(-1) } + it { is_expected.to run.with_params('-1+1').and_return(-1) } + it { is_expected.to run.with_params('1-1').and_return(1) } + it { is_expected.to run.with_params('1 kaboom').and_return(1024) } + it { is_expected.to run.with_params('kaboom').and_return(0) } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/to_json_pretty_spec.rb b/code/environments/production/modules/stdlib/spec/functions/to_json_pretty_spec.rb new file mode 100644 index 0000000..a8da623 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/to_json_pretty_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe 'to_json_pretty' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params([]).and_return("[\n\n]\n") } + it { is_expected.to run.with_params(['one']).and_return("[\n \"one\"\n]\n") } + it { is_expected.to run.with_params(%w[one two]).and_return("[\n \"one\",\n \"two\"\n]\n") } + it { is_expected.to run.with_params({}).and_return("{\n}\n") } + it { is_expected.to run.with_params('key' => 'value').and_return("{\n \"key\": \"value\"\n}\n") } + it { + is_expected.to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => %w[twoA twoB]) + .and_return("{\n \"one\": {\n \"oneA\": \"A\",\n \"oneB\": {\n \"oneB1\": \"1\",\n \"oneB2\": \"2\"\n }\n },\n \"two\": [\n \"twoA\",\n \"twoB\"\n ]\n}\n") # rubocop:disable Metrics/LineLength : Unable to reduce line to required length + } + it { is_expected.to run.with_params({ 'one' => '1', 'two' => nil }, true).and_return("{\n \"one\": \"1\"\n}\n") } + it { is_expected.to run.with_params(['one', 'two', nil, 'three'], true).and_return("[\n \"one\",\n \"two\",\n \"three\"\n]\n") } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/to_json_spec.rb b/code/environments/production/modules/stdlib/spec/functions/to_json_spec.rb new file mode 100644 index 0000000..187ca3c --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/to_json_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe 'to_json' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params('').and_return('""') } + it { is_expected.to run.with_params(true).and_return('true') } + it { is_expected.to run.with_params('one').and_return('"one"') } + it { is_expected.to run.with_params([]).and_return('[]') } + it { is_expected.to run.with_params(['one']).and_return('["one"]') } + it { is_expected.to run.with_params(%w[one two]).and_return('["one","two"]') } + it { is_expected.to run.with_params({}).and_return('{}') } + it { is_expected.to run.with_params('key' => 'value').and_return('{"key":"value"}') } + it { + is_expected.to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => %w[twoA twoB]) + .and_return('{"one":{"oneA":"A","oneB":{"oneB1":"1","oneB2":"2"}},"two":["twoA","twoB"]}') + } + + 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('"Ü"') } + it { is_expected.to run.with_params('∇').and_return('"∇"') } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/to_yaml_spec.rb b/code/environments/production/modules/stdlib/spec/functions/to_yaml_spec.rb new file mode 100644 index 0000000..16948f6 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/to_yaml_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe 'to_yaml' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params('').and_return("--- ''\n") } + it { is_expected.to run.with_params(true).and_return("--- true\n...\n") } + it { is_expected.to run.with_params('one').and_return("--- one\n...\n") } + it { is_expected.to run.with_params([]).and_return("--- []\n") } + it { is_expected.to run.with_params(['one']).and_return("---\n- one\n") } + it { is_expected.to run.with_params(%w[one two]).and_return("---\n- one\n- two\n") } + it { is_expected.to run.with_params({}).and_return("--- {}\n") } + it { is_expected.to run.with_params('key' => 'value').and_return("---\nkey: value\n") } + it { + is_expected.to run.with_params('one' => { 'oneA' => 'A', 'oneB' => { 'oneB1' => '1', 'oneB2' => '2' } }, 'two' => %w[twoA twoB]) + .and_return("---\none:\n oneA: A\n oneB:\n oneB1: '1'\n oneB2: '2'\ntwo:\n- twoA\n- twoB\n") + } + + it { is_expected.to run.with_params('‰').and_return("--- \"‰\"\n") } + it { is_expected.to run.with_params('∇').and_return("--- \"∇\"\n") } +end diff --git a/code/environments/production/modules/stdlib/spec/functions/try_get_value_spec.rb b/code/environments/production/modules/stdlib/spec/functions/try_get_value_spec.rb new file mode 100644 index 0000000..630296b --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/try_get_value_spec.rb @@ -0,0 +1,108 @@ +require 'spec_helper' + +describe 'try_get_value' do + let(:data) do + { + 'a' => { + 'g' => '2', + 'e' => [ + 'f0', + 'f1', + { + 'x' => { + 'y' => 'z', + }, + }, + 'f3', + ], + }, + 'b' => true, + 'c' => false, + 'd' => '1', + } + end + + context 'with single values' do + it 'exists' do + is_expected.not_to eq(nil) + end + + it 'is able to return a single value' do + is_expected.to run.with_params('test').and_return('test') + end + + it 'uses the default value if data is a single value and path is present' do + is_expected.to run.with_params('test', 'path', 'default').and_return('default') + end + + it 'returns default if there is no data' do + is_expected.to run.with_params(nil, nil, 'default').and_return('default') + end + + it 'is able to use data structures as default values' do + is_expected.to run.with_params('test', 'path', data).and_return(data) + end + end + + context 'with structure values' do + it 'is able to extracts a single hash value' do + is_expected.to run.with_params(data, 'd', 'default').and_return('1') + end + + it 'is able to extract a deeply nested hash value' do + is_expected.to run.with_params(data, 'a/g', 'default').and_return('2') + end + + it 'returns the default value if the path is not found' do + is_expected.to run.with_params(data, 'missing', 'default').and_return('default') + end + + it 'returns the default value if the path is too long' do + is_expected.to run.with_params(data, 'a/g/c/d', 'default').and_return('default') + end + + it 'supports an array index in the path' do + is_expected.to run.with_params(data, 'a/e/1', 'default').and_return('f1') + end + + it 'returns the default value if an array index is not a number' do + is_expected.to run.with_params(data, 'a/b/c', 'default').and_return('default') + end + + it 'returns the default value if and index is out of array length' do + is_expected.to run.with_params(data, 'a/e/5', 'default').and_return('default') + end + + it 'is able to path though both arrays and hashes' do + is_expected.to run.with_params(data, 'a/e/2/x/y', 'default').and_return('z') + end + + it 'is able to return "true" value: default' do + is_expected.to run.with_params(data, 'b', 'default').and_return(true) + end + + it 'is able to return "true" value' do + is_expected.to run.with_params(data, 'm', true).and_return(true) + end + + it 'is able to return "false" value: default' do + is_expected.to run.with_params(data, 'c', 'default').and_return(false) + end + + it 'is able to return "false" value' do + is_expected.to run.with_params(data, 'm', false).and_return(false) + end + + it 'returns "nil" if value is not found and no default value is provided' do + is_expected.to run.with_params(data, 'a/1').and_return(nil) + end + + it 'is able to use a custom path separator' do + is_expected.to run.with_params(data, 'a::g', 'default', '::').and_return('2') + end + + it 'is able to use a custom path separator: default' do + is_expected.to run.with_params(data, 'a::c', 'default', '::').and_return('default') + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/type3x_spec.rb b/code/environments/production/modules/stdlib/spec/functions/type3x_spec.rb new file mode 100644 index 0000000..3a71ed9 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/type3x_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +describe 'type3x' do + it 'exists' do + expect(Puppet::Parser::Functions.function('type3x')).to eq('function_type3x') + end + + it 'raises a ParseError if there is less than 1 arguments' do + expect { scope.function_type3x([]) }.to(raise_error(Puppet::ParseError)) + end + + it 'returns string when given a string' do + result = scope.function_type3x(['aaabbbbcccc']) + expect(result).to(eq('string')) + end + + it 'returns array when given an array' do + result = scope.function_type3x([%w[aaabbbbcccc asdf]]) + expect(result).to(eq('array')) + end + + it 'returns hash when given a hash' do + result = scope.function_type3x([{ 'a' => 1, 'b' => 2 }]) + expect(result).to(eq('hash')) + end + + it 'returns integer when given an integer' do + result = scope.function_type3x(['1']) + expect(result).to(eq('integer')) + end + + it 'returns float when given a float' do + result = scope.function_type3x(['1.34']) + expect(result).to(eq('float')) + end + + it 'returns boolean when given a boolean' do + result = scope.function_type3x([true]) + expect(result).to(eq('boolean')) + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/type_of_spec.rb b/code/environments/production/modules/stdlib/spec/functions/type_of_spec.rb new file mode 100644 index 0000000..4f55b2c --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/type_of_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper' + +if ENV['FUTURE_PARSER'] == 'yes' + describe 'type_of' do + pending 'teach rspec-puppet to load future-only functions under 3.7.5' do + it { is_expected.not_to eq(nil) } + end + end +end + +if Puppet.version.to_f >= 4.0 + describe 'type_of' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(ArgumentError) } + it { is_expected.to run.with_params('', '').and_raise_error(ArgumentError) } + + it 'gives the type of a string' do + expect(subject.call({}, 'hello world')).to be_kind_of(Puppet::Pops::Types::PStringType) + end + + it 'gives the type of an integer' do + expect(subject.call({}, 5)).to be_kind_of(Puppet::Pops::Types::PIntegerType) + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/type_spec.rb b/code/environments/production/modules/stdlib/spec/functions/type_spec.rb new file mode 100644 index 0000000..a28a913 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/type_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' + +describe 'type' do + it 'exists' do + expect(Puppet::Parser::Functions.function('type')).to eq('function_type') + end + + it 'gives a deprecation warning when called' do + scope.expects(:warning).with("type() DEPRECATED: This function will cease to function on Puppet 4; please use type3x() before upgrading to puppet 4 for backwards-compatibility, or migrate to the new parser's typing system.") # rubocop:disable Metrics/LineLength : Unable to reduce to required length + scope.function_type(['aoeu']) + end + + it 'returns string when given a string' do + result = scope.function_type(['aaabbbbcccc']) + expect(result).to(eq('string')) + end + + it 'returns array when given an array' do + result = scope.function_type([%w[aaabbbbcccc asdf]]) + expect(result).to(eq('array')) + end + + it 'returns hash when given a hash' do + result = scope.function_type([{ 'a' => 1, 'b' => 2 }]) + expect(result).to(eq('hash')) + end + + it 'returns integer when given an integer' do + result = scope.function_type(['1']) + expect(result).to(eq('integer')) + end + + it 'returns float when given a float' do + result = scope.function_type(['1.34']) + expect(result).to(eq('float')) + end + + it 'returns boolean when given a boolean' do + result = scope.function_type([true]) + expect(result).to(eq('boolean')) + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/union_spec.rb b/code/environments/production/modules/stdlib/spec/functions/union_spec.rb new file mode 100644 index 0000000..d8dfc4f --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/union_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper' + +describe 'union' do + describe 'argument checking' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('one', []).and_raise_error(Puppet::ParseError, %r{Every parameter must be an array}) } + it { is_expected.to run.with_params([], 'two').and_raise_error(Puppet::ParseError, %r{Every parameter must be an array}) } + it { is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError, %r{Every parameter must be an array}) } + end + + it { is_expected.to run.with_params([], []).and_return([]) } + it { is_expected.to run.with_params([], ['one']).and_return(['one']) } + it { is_expected.to run.with_params(['one'], []).and_return(['one']) } + it { is_expected.to run.with_params(['one'], ['one']).and_return(['one']) } + it { is_expected.to run.with_params(['one'], ['two']).and_return(%w[one two]) } + it { is_expected.to run.with_params(%w[one two three], %w[two three]).and_return(%w[one two three]) } + it { is_expected.to run.with_params(%w[one two two three], %w[two three]).and_return(%w[one two three]) } + it { is_expected.to run.with_params(%w[one two three], %w[two two three]).and_return(%w[one two three]) } + it { is_expected.to run.with_params(%w[one two], %w[two three], %w[one three]).and_return(%w[one two three]) } + it { is_expected.to run.with_params(%w[one two], %w[three four], %w[one two three], ['four']).and_return(%w[one two three four]) } + it { is_expected.to run.with_params(%w[ốńə ţשׂợ], %w[ŧĥяếệ ƒởųŗ], %w[ốńə ţשׂợ ŧĥяếệ], ['ƒởųŗ']).and_return(%w[ốńə ţשׂợ ŧĥяếệ ƒởųŗ]) } + it 'does not confuse types' do is_expected.to run.with_params(%w[1 2 3], [1, 2]).and_return(['1', '2', '3', 1, 2]) end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/unique_spec.rb b/code/environments/production/modules/stdlib/spec/functions/unique_spec.rb new file mode 100644 index 0000000..b8481e0 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/unique_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe 'unique' do + 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, %r{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, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } + it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{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(%w[a b a]).and_return(%w[a b]) } + it { is_expected.to run.with_params(%w[ã ъ ã]).and_return(%w[ã ъ]) } + 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('ãъ') } + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/unix2dos_spec.rb b/code/environments/production/modules/stdlib/spec/functions/unix2dos_spec.rb new file mode 100644 index 0000000..a6af64f --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/unix2dos_spec.rb @@ -0,0 +1,40 @@ +require 'spec_helper' + +describe 'unix2dos' do + context 'when checking parameter validity' do + it { is_expected.not_to eq(nil) } + it do + is_expected.to run.with_params.and_raise_error(ArgumentError, %r{Wrong number of arguments}) + end + it do + is_expected.to run.with_params('one', 'two').and_raise_error(ArgumentError, %r{Wrong number of arguments}) + end + it do + is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) + end + it do + is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError) + end + it do + is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError) + end + end + + context 'when converting from unix to dos format' do + sample_text = "Hello\nWorld\n" + desired_output = "Hello\r\nWorld\r\n" + + it 'outputs dos format' do + is_expected.to run.with_params(sample_text).and_return(desired_output) + end + end + + context 'when converting from dos to dos format' do + sample_text = "Hello\r\nWorld\r\n" + desired_output = "Hello\r\nWorld\r\n" + + it 'outputs dos format' do + is_expected.to run.with_params(sample_text).and_return(desired_output) + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/upcase_spec.rb b/code/environments/production/modules/stdlib/spec/functions/upcase_spec.rb new file mode 100644 index 0000000..2ef5282 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/upcase_spec.rb @@ -0,0 +1,26 @@ +require 'spec_helper' + +describe 'upcase' do + describe 'signature validation' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires an array, hash or object that responds to upcase}) } + it { is_expected.to run.with_params([1]).and_raise_error(Puppet::ParseError, %r{Requires an array, hash or object that responds to upcase}) } + end + + describe 'normal string handling' do + it { is_expected.to run.with_params('abc').and_return('ABC') } + it { is_expected.to run.with_params('Abc').and_return('ABC') } + it { is_expected.to run.with_params('ABC').and_return('ABC') } + end + + describe 'handling classes derived from String' do + it { is_expected.to run.with_params(AlsoString.new('ABC')).and_return('ABC') } + end + + describe 'strings in arrays handling' do + it { is_expected.to run.with_params([]).and_return([]) } + it { is_expected.to run.with_params(%w[One twO]).and_return(%w[ONE TWO]) } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/uriescape_spec.rb b/code/environments/production/modules/stdlib/spec/functions/uriescape_spec.rb new file mode 100644 index 0000000..3f1fd9d --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/uriescape_spec.rb @@ -0,0 +1,36 @@ +require 'spec_helper' + +describe 'uriescape' do + describe 'signature validation' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('Current implementation ignores parameters after the first.') + is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } + it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } + it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{Requires either array or string to work}) } + end + + describe 'handling normal strings' do + it 'calls ruby\'s URI.escape function' do + URI.expects(:escape).with('uri_string').returns('escaped_uri_string').once + is_expected.to run.with_params('uri_string').and_return('escaped_uri_string') + end + end + + describe 'handling classes derived from String' do + it 'calls ruby\'s URI.escape function' do + uri_string = AlsoString.new('uri_string') + URI.expects(:escape).with(uri_string).returns('escaped_uri_string').once + is_expected.to run.with_params(uri_string).and_return('escaped_uri_string') + end + end + + describe 'strings in arrays handling' do + it { is_expected.to run.with_params([]).and_return([]) } + it { is_expected.to run.with_params(['one}', 'two']).and_return(['one%7D', 'two']) } + it { is_expected.to run.with_params(['one}', 1, true, {}, 'two']).and_return(['one%7D', 1, true, {}, 'two']) } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/validate_absolute_path_spec.rb b/code/environments/production/modules/stdlib/spec/functions/validate_absolute_path_spec.rb new file mode 100644 index 0000000..5e93bb3 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/validate_absolute_path_spec.rb @@ -0,0 +1,72 @@ +require 'spec_helper' + +describe 'validate_absolute_path' do + after(:each) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + + # Checking for deprecation warning + it 'displays a single deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = 'true' + scope.expects(:warning).with(includes('This method is deprecated')) + is_expected.to run.with_params('c:/') + end + + describe 'signature validation' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + end + + describe 'valid paths handling' do + %w[ + C:/ + C:\\ + C:\\WINDOWS\\System32 + C:/windows/system32 + X:/foo/bar + X:\\foo\\bar + \\\\host\\windows + //host/windows + / + /var/tmp + /var/opt/../lib/puppet + ].each do |path| + it { is_expected.to run.with_params(path) } + it { is_expected.to run.with_params(['/tmp', path]) } + end + end + + describe 'invalid path handling' do + context 'with garbage inputs' do + [ + nil, + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + ].each do |path| + it { is_expected.to run.with_params(path).and_raise_error(Puppet::ParseError, %r{is not an absolute path}) } + it { is_expected.to run.with_params([path]).and_raise_error(Puppet::ParseError, %r{is not an absolute path}) } + it { is_expected.to run.with_params(['/tmp', path]).and_raise_error(Puppet::ParseError, %r{is not an absolute path}) } + end + end + + context 'with relative paths' do + %w[ + relative1 + . + .. + ./foo + ../foo + etc/puppetlabs/puppet + opt/puppet/bin + relative\\windows + ].each do |path| + it { is_expected.to run.with_params(path).and_raise_error(Puppet::ParseError, %r{is not an absolute path}) } + it { is_expected.to run.with_params([path]).and_raise_error(Puppet::ParseError, %r{is not an absolute path}) } + it { is_expected.to run.with_params(['/tmp', path]).and_raise_error(Puppet::ParseError, %r{is not an absolute path}) } + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/validate_array_spec.rb b/code/environments/production/modules/stdlib/spec/functions/validate_array_spec.rb new file mode 100644 index 0000000..660974a --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/validate_array_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' + +describe 'validate_array' do + describe 'signature validation' do + after(:each) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + it { is_expected.not_to eq(nil) } + # Checking for deprecation warning + it 'displays 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 + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + + describe 'valid inputs' do + it { is_expected.to run.with_params([]) } + it { is_expected.to run.with_params(['one']) } + it { is_expected.to run.with_params([], ['two']) } + it { is_expected.to run.with_params(['one'], ['two']) } + end + + describe 'invalid inputs' do + it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{is not an Array}) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{is not an Array}) } + it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{is not an Array}) } + it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{is not an Array}) } + it { is_expected.to run.with_params([], {}).and_raise_error(Puppet::ParseError, %r{is not an Array}) } + it { is_expected.to run.with_params([], 1).and_raise_error(Puppet::ParseError, %r{is not an Array}) } + it { is_expected.to run.with_params([], true).and_raise_error(Puppet::ParseError, %r{is not an Array}) } + it { is_expected.to run.with_params([], 'one').and_raise_error(Puppet::ParseError, %r{is not an Array}) } + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/validate_augeas_spec.rb b/code/environments/production/modules/stdlib/spec/functions/validate_augeas_spec.rb new file mode 100644 index 0000000..2dd0df8 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/validate_augeas_spec.rb @@ -0,0 +1,75 @@ +require 'spec_helper' + +describe 'validate_augeas' do + if Puppet.features.augeas? + describe 'signature validation' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('', '', [], '', 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('one', 'one', 'MSG to User', '4th arg').and_raise_error(NoMethodError) } + end + + describe 'valid inputs' do + inputs = [ + ["root:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns'], + ["proc /proc proc nodev,noexec,nosuid 0 0\n", 'Fstab.lns'], + ] + + inputs.each do |input| + it { is_expected.to run.with_params(*input) } + end + end + + describe 'valid inputs which fail augeas validation' do + # The intent here is to make sure valid inputs raise exceptions when they + # don't specify an error message to display. This is the behvior in + # 2.2.x and prior. + inputs = [ + ["root:x:0:0:root\n", 'Passwd.lns'], + ["127.0.1.1\n", 'Hosts.lns'], + ] + + inputs.each do |input| + it { is_expected.to run.with_params(*input).and_raise_error(Puppet::ParseError, %r{validate_augeas.*?matched less than it should}) } + end + end + + describe 'when specifying nice error messages' do + # The intent here is to make sure the function returns the 4th argument + # in the exception thrown + inputs = [ + ["root:x:0:0:root\n", 'Passwd.lns', [], 'Failed to validate passwd content'], + ["127.0.1.1\n", 'Hosts.lns', [], 'Wrong hosts content'], + ] + + inputs.each do |input| + it { is_expected.to run.with_params(*input).and_raise_error(Puppet::ParseError, %r{#{input[3]}}) } + end + end + + describe 'matching additional tests' do + inputs = [ + ["root:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns', ['$file/foobar']], + ["root:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns', ['$file/root/shell[.="/bin/sh"]', 'foobar']], + ] + + inputs.each do |input| + it { is_expected.to run.with_params(*input) } + end + end + + describe 'failing additional tests' do + inputs = [ + ["foobar:x:0:0:root:/root:/bin/bash\n", 'Passwd.lns', ['$file/foobar']], + ["root:x:0:0:root:/root:/bin/sh\n", 'Passwd.lns', ['$file/root/shell[.="/bin/sh"]', 'foobar']], + ] + + inputs.each do |input| + it { is_expected.to run.with_params(*input).and_raise_error(Puppet::ParseError, %r{testing path}) } + end + end + else + skip 'ruby-augeas not installed' + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/validate_bool_spec.rb b/code/environments/production/modules/stdlib/spec/functions/validate_bool_spec.rb new file mode 100644 index 0000000..565d52e --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/validate_bool_spec.rb @@ -0,0 +1,36 @@ +require 'spec_helper' + +describe 'validate_bool' do + after(:each) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + + # Checking for deprecation warning + it 'displays 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) + end + + describe 'signature validation' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + end + + describe 'acceptable values' do + it { is_expected.to run.with_params(true) } + it { is_expected.to run.with_params(false) } + it { is_expected.to run.with_params(true, false, false, true) } + end + + describe 'validation failures' do + it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{is not a boolean}) } + it { is_expected.to run.with_params(true, 'one').and_raise_error(Puppet::ParseError, %r{is not a boolean}) } + it { is_expected.to run.with_params('one', false).and_raise_error(Puppet::ParseError, %r{is not a boolean}) } + it { is_expected.to run.with_params('true').and_raise_error(Puppet::ParseError, %r{is not a boolean}) } + it { is_expected.to run.with_params('false').and_raise_error(Puppet::ParseError, %r{is not a boolean}) } + it { is_expected.to run.with_params(true, 'false').and_raise_error(Puppet::ParseError, %r{is not a boolean}) } + it { is_expected.to run.with_params('true', false).and_raise_error(Puppet::ParseError, %r{is not a boolean}) } + it { is_expected.to run.with_params('true', false, false, false, false, false).and_raise_error(Puppet::ParseError, %r{is not a boolean}) } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/validate_cmd_spec.rb b/code/environments/production/modules/stdlib/spec/functions/validate_cmd_spec.rb new file mode 100644 index 0000000..91fd0d1 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/validate_cmd_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +describe 'validate_cmd', :unless => Puppet::Util::Platform.windows? do + let(:touch) { File.exist?('/usr/bin/touch') ? '/usr/bin/touch' : '/bin/touch' } + + describe 'signature validation' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('', '', '', 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('should implement stricter type checking') + is_expected.to run.with_params([], '', '').and_raise_error(Puppet::ParseError, %r{content must be a string}) + } + it { + pending('should implement stricter type checking') + is_expected.to run.with_params('', [], '').and_raise_error(Puppet::ParseError, %r{checkscript must be a string}) + } + it { + pending('should implement stricter type checking') + is_expected.to run.with_params('', '', []).and_raise_error(Puppet::ParseError, %r{custom error message must be a string}) + } + end + + context 'when validation fails' do + context 'with % placeholder' do + it { + is_expected.to run + .with_params('', "#{touch} % /no/such/file").and_raise_error(Puppet::ParseError, %r{Execution of '#{touch} \S+ \/no\/such\/file' returned 1:.*(cannot touch|o such file or)}) + } + it { is_expected.to run.with_params('', "#{touch} % /no/such/file", 'custom error').and_raise_error(Puppet::ParseError, %r{custom error}) } + end + context 'without % placeholder' do + it { + is_expected.to run + .with_params('', "#{touch} /no/such/file").and_raise_error(Puppet::ParseError, %r{Execution of '#{touch} \/no\/such\/file \S+' returned 1:.*(cannot touch|o such file or)}) + } + it { is_expected.to run.with_params('', "#{touch} /no/such/file", 'custom error').and_raise_error(Puppet::ParseError, %r{custom error}) } + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/validate_domain_name_spec.rb b/code/environments/production/modules/stdlib/spec/functions/validate_domain_name_spec.rb new file mode 100644 index 0000000..0ac1a3d --- /dev/null +++ b/code/environments/production/modules/stdlib/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, %r{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, %r{is not a string}) } + it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{is not a string}) } + + it { is_expected.to run.with_params('foo.example.com', []).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params('foo.example.com', {}).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params('foo.example.com', 1).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params('foo.example.com', true).and_raise_error(Puppet::ParseError, %r{is not a string}) } + + it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, %r{is not a syntactically correct domain name}) } + it { is_expected.to run.with_params('invalid domain').and_raise_error(Puppet::ParseError, %r{is not a syntactically correct domain name}) } + it { is_expected.to run.with_params('-foo.example.com').and_raise_error(Puppet::ParseError, %r{is not a syntactically correct domain name}) } + it { is_expected.to run.with_params('www.example.2com').and_raise_error(Puppet::ParseError, %r{is not a syntactically correct domain name}) } + it { is_expected.to run.with_params('192.168.1.1').and_raise_error(Puppet::ParseError, %r{is not a syntactically correct domain name}) } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/validate_email_address_spec.rb b/code/environments/production/modules/stdlib/spec/functions/validate_email_address_spec.rb new file mode 100644 index 0000000..520c7a4 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/validate_email_address_spec.rb @@ -0,0 +1,23 @@ +require 'spec_helper' + +describe 'validate_email_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, %r{wrong number of arguments}i) } + end + + describe 'valid inputs' do + it { is_expected.to run.with_params('bob@gmail.com') } + it { is_expected.to run.with_params('alice+puppetlabs.com@gmail.com') } + end + + describe 'invalid inputs' do + it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{is not a valid email}) } + it { is_expected.to run.with_params('bob@gmail.com', {}).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params('bob@gmail.com', true).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params('bob@gmail.com', 'one').and_raise_error(Puppet::ParseError, %r{is not a valid email}) } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/validate_hash_spec.rb b/code/environments/production/modules/stdlib/spec/functions/validate_hash_spec.rb new file mode 100644 index 0000000..be212de --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/validate_hash_spec.rb @@ -0,0 +1,38 @@ +require 'spec_helper' + +describe 'validate_hash' do + describe 'signature validation' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + + describe 'check for deprecation warning' do + after(:each) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + # Checking for deprecation warning + it 'displays a single deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = 'true' + scope.expects(:warning).with(includes('This method is deprecated')) + is_expected.to run.with_params('key' => 'value') + end + end + + describe 'valid inputs' do + it { is_expected.to run.with_params({}) } + it { is_expected.to run.with_params('key' => 'value') } + it { is_expected.to run.with_params({}, 'key' => 'value') } + it { is_expected.to run.with_params({ 'key1' => 'value1' }, 'key2' => 'value2') } + end + + describe 'invalid inputs' do + it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{is not a Hash}) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{is not a Hash}) } + it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{is not a Hash}) } + it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{is not a Hash}) } + it { is_expected.to run.with_params({}, []).and_raise_error(Puppet::ParseError, %r{is not a Hash}) } + it { is_expected.to run.with_params({}, 1).and_raise_error(Puppet::ParseError, %r{is not a Hash}) } + it { is_expected.to run.with_params({}, true).and_raise_error(Puppet::ParseError, %r{is not a Hash}) } + it { is_expected.to run.with_params({}, 'one').and_raise_error(Puppet::ParseError, %r{is not a Hash}) } + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/validate_integer_spec.rb b/code/environments/production/modules/stdlib/spec/functions/validate_integer_spec.rb new file mode 100644 index 0000000..4792d95 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/validate_integer_spec.rb @@ -0,0 +1,101 @@ +require 'spec_helper' + +describe 'validate_integer' do + after(:each) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + + # Checking for deprecation warning + it 'displays 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 + + describe 'signature validation' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + + [true, 'true', false, 'false', 'iAmAString', '1test', '1 test', 'test 1', 'test 1 test', 7.0, -7.0, {}, { 'key' => 'value' }, { 1 => 2 }, '', :undef, 'x'].each do |invalid| + it { is_expected.to run.with_params(invalid).and_raise_error(Puppet::ParseError, %r{to be an Integer}) } + it { is_expected.to run.with_params(invalid, 10).and_raise_error(Puppet::ParseError, %r{to be an Integer}) } + it { is_expected.to run.with_params(invalid, 10, -10).and_raise_error(Puppet::ParseError, %r{to be an Integer}) } + it { is_expected.to run.with_params([0, 1, 2, invalid, 3, 4], 10, -10).and_raise_error(Puppet::ParseError, %r{to be an Integer}) } + end + + context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do + it { is_expected.to run.with_params([0, 1, 2, { 1 => 2 }, 3, 4], 10, -10).and_raise_error(Puppet::ParseError, %r{to be an Integer}) } + end + + context 'when running on ruby, which munges hashes weirdly', :if => RUBY_VERSION == '1.8.7' do + it { is_expected.to run.with_params([0, 1, 2, { 1 => 2 }, 3, 4], 10, -10).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params([0, 1, 2, { 0 => 2 }, 3, 4], 10, -10).and_raise_error(Puppet::ParseError) } + end + + it { is_expected.to run.with_params(1, '').and_raise_error(Puppet::ParseError, %r{to be unset or an Integer}) } + it { is_expected.to run.with_params(1, 2, '').and_raise_error(Puppet::ParseError, %r{to be unset or an Integer}) } + it { is_expected.to run.with_params(1, 2, 3).and_raise_error(Puppet::ParseError, %r{second argument to be larger than third argument}) } + end + + context 'with no range constraints' do + it { is_expected.to run.with_params(1) } + it { is_expected.to run.with_params(-1) } + it { is_expected.to run.with_params('1') } + it { is_expected.to run.with_params('-1') } + it { is_expected.to run.with_params([1, 2, 3, 4]) } + it { is_expected.to run.with_params([1, '2', '3', 4]) } + end + + context 'with a maximum limit of 10' do + describe 'rejects numbers greater than the limit' do + it { is_expected.to run.with_params(11, 10).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) } + it { is_expected.to run.with_params(100, 10).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) } + it { is_expected.to run.with_params(2**65, 10).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) } + it { is_expected.to run.with_params([1, 2, 10, 100], 10).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) } + end + + describe 'accepts numbers less or equal to the limit' do + it { is_expected.to run.with_params(10, 10) } + it { is_expected.to run.with_params(1, 10) } + it { is_expected.to run.with_params(-1, 10) } + it { is_expected.to run.with_params('1', 10) } + it { is_expected.to run.with_params('-1', 10) } + it { is_expected.to run.with_params([1, 2, 3, 4], 10) } + it { is_expected.to run.with_params([1, '2', '3', 4], 10) } + end + end + + context 'with a minimum limit of -10' do + describe 'rejects numbers greater than the upper limit' do + it { is_expected.to run.with_params(11, 10, -10).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) } + it { is_expected.to run.with_params(100, 10, -10).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) } + it { is_expected.to run.with_params(2**65, 10, -10).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) } + it { is_expected.to run.with_params([1, 2, 10, 100], 10, -10).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) } + end + + describe 'rejects numbers smaller than the lower limit' do + it { is_expected.to run.with_params(-11, 10, -10).and_raise_error(Puppet::ParseError, %r{to be greater or equal}) } + it { is_expected.to run.with_params(-100, 10, -10).and_raise_error(Puppet::ParseError, %r{to be greater or equal}) } + it { is_expected.to run.with_params(-2**65, 10, -10).and_raise_error(Puppet::ParseError, %r{to be greater or equal}) } + it { is_expected.to run.with_params([-10, 1, 2, 10, -100], 10, -10).and_raise_error(Puppet::ParseError, %r{to be greater or equal}) } + end + + describe 'accepts numbers between and including the limits' do + it { is_expected.to run.with_params(10, 10, -10) } + it { is_expected.to run.with_params(-10, 10, -10) } + it { is_expected.to run.with_params(1, 10, -10) } + it { is_expected.to run.with_params(-1, 10, -10) } + it { is_expected.to run.with_params('1', 10, -10) } + it { is_expected.to run.with_params('-1', 10, -10) } + it { is_expected.to run.with_params([1, 2, 3, 4], 10, -10) } + it { is_expected.to run.with_params([1, '2', '3', 4], 10, -10) } + end + end + + it { is_expected.to run.with_params(10, 10, 10) } + + describe 'empty upper limit is interpreted as infinity' do + it { is_expected.to run.with_params(11, '', 10) } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/validate_ip_address_spec.rb b/code/environments/production/modules/stdlib/spec/functions/validate_ip_address_spec.rb new file mode 100644 index 0000000..714f7b2 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/validate_ip_address_spec.rb @@ -0,0 +1,64 @@ +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, %r{wrong number of arguments}i) } + 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') } + 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') } + + context 'Checking for deprecation warning', :if => Puppet.version.to_f < 4.0 do + after(:each) 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 'displays 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 'displays 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') } + 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, %r{is not a string}) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{is not a valid IP}) } + it { is_expected.to run.with_params('0.0.0').and_raise_error(Puppet::ParseError, %r{is not a valid IP}) } + it { is_expected.to run.with_params('0.0.0.256').and_raise_error(Puppet::ParseError, %r{is not a valid IP}) } + it { is_expected.to run.with_params('0.0.0.0.0').and_raise_error(Puppet::ParseError, %r{is not a valid IP}) } + it { is_expected.to run.with_params('1.2.3.4', {}).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params('1.2.3.4', 1).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params('1.2.3.4', true).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params('1.2.3.4', 'one').and_raise_error(Puppet::ParseError, %r{is not a valid IP}) } + it { is_expected.to run.with_params('::1', {}).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params('::1', true).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params('::1', 'one').and_raise_error(Puppet::ParseError, %r{is not a valid IP}) } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/validate_ipv4_address_spec.rb b/code/environments/production/modules/stdlib/spec/functions/validate_ipv4_address_spec.rb new file mode 100644 index 0000000..72a8018 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/validate_ipv4_address_spec.rb @@ -0,0 +1,40 @@ +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, %r{wrong number of arguments}i) } + end + + context 'Checking for deprecation warning', :if => Puppet.version.to_f < 4.0 do + after(:each) 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 'displays a single deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = 'true' + scope.expects(:warning).with(includes('This method is deprecated')) + is_expected.to run.with_params(SharedData::IPV4_PATTERNS.first) + end + it 'displays 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(SharedData::IPV4_PATTERNS.first) + end + end + + SharedData::IPV4_PATTERNS.each do |value| + it { is_expected.to run.with_params(value) } + end + + SharedData::IPV4_NEGATIVE_PATTERNS.each do |value| + it { is_expected.to run.with_params(value).and_raise_error(Puppet::ParseError, %r{is not a valid IPv4}) } + end + + describe 'invalid inputs' do + [{}, [], 1, true].each do |invalid| + it { is_expected.to run.with_params(invalid).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params(SharedData::IPV4_PATTERNS.first, invalid).and_raise_error(Puppet::ParseError, %r{is not a string}) } + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/validate_ipv6_address_spec.rb b/code/environments/production/modules/stdlib/spec/functions/validate_ipv6_address_spec.rb new file mode 100644 index 0000000..5f4e312 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/validate_ipv6_address_spec.rb @@ -0,0 +1,59 @@ +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, %r{wrong number of arguments}i) } + end + + context 'Checking for deprecation warning', :if => Puppet.version.to_f < 4.0 do + after(:each) 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 'displays 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 'displays 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') } + it { is_expected.to run.with_params('fe80:0000:0000:0000:0204:61ff:fe9d:f156') } + it { is_expected.to run.with_params('fe80:0:0:0:204:61ff:fe9d:f156') } + it { is_expected.to run.with_params('fe80::204:61ff:fe9d:f156') } + it { is_expected.to run.with_params('fe80:0:0:0:0204:61ff:254.157.241.86') } + it { is_expected.to run.with_params('::1') } + it { is_expected.to run.with_params('fe80::') } + it { is_expected.to run.with_params('2001::') } + end + + describe 'invalid inputs' do + it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, %r{is not a valid IPv6}) } + it { is_expected.to run.with_params('0.0.0').and_raise_error(Puppet::ParseError, %r{is not a valid IPv6}) } + it { is_expected.to run.with_params('0.0.0.256').and_raise_error(Puppet::ParseError, %r{is not a valid IPv6}) } + it { is_expected.to run.with_params('0.0.0.0.0').and_raise_error(Puppet::ParseError, %r{is not a valid IPv6}) } + it { is_expected.to run.with_params('::ffff:2.3.4').and_raise_error(Puppet::ParseError, %r{is not a valid IPv6}) } + it { is_expected.to run.with_params('::ffff:257.1.2.3').and_raise_error(Puppet::ParseError, %r{is not a valid IPv6}) } + it { is_expected.to run.with_params('::ffff:12345678901234567890.1.26').and_raise_error(Puppet::ParseError, %r{is not a valid IPv6}) } + it { is_expected.to run.with_params('affe:beef').and_raise_error(Puppet::ParseError, %r{is not a valid IPv6}) } + it { is_expected.to run.with_params('::1', {}).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params('::1', true).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params('::1', 'one').and_raise_error(Puppet::ParseError, %r{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, %r{is not a string}) } + it { is_expected.to run.with_params('::1', 1).and_raise_error(Puppet::ParseError, %r{is not a string}) } + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/validate_legacy_spec.rb b/code/environments/production/modules/stdlib/spec/functions/validate_legacy_spec.rb new file mode 100644 index 0000000..06c3b51 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/validate_legacy_spec.rb @@ -0,0 +1,68 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.4.0') >= 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(:each) 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(:each) 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(:each) do + scope.expects(:function_validate_foo).with(['5']).once + subject.func.expects(:call_function).with('deprecation', 'validate_legacy', includes('Integer')).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(:each) do + scope.expects(:function_validate_foo).with(['5']).raises(Puppet::ParseError, 'foo').once + subject.func.expects(:call_function).with('fail', includes('Integer')).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(:each) 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(:each) 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/code/environments/production/modules/stdlib/spec/functions/validate_numeric_spec.rb b/code/environments/production/modules/stdlib/spec/functions/validate_numeric_spec.rb new file mode 100644 index 0000000..75217a2 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/validate_numeric_spec.rb @@ -0,0 +1,100 @@ +require 'spec_helper' + +describe 'validate_numeric' do + after(:each) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + + # Checking for deprecation warning + it 'displays 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 + + describe 'signature validation' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(1, 2, 3, 4).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + + [true, 'true', false, 'false', 'iAmAString', '1test', '1 test', 'test 1', 'test 1 test', {}, { 'key' => 'value' }, { 1 => 2 }, '', :undef, 'x'].each do |invalid| + it { is_expected.to run.with_params(invalid).and_raise_error(Puppet::ParseError, %r{to be a Numeric}) } + it { is_expected.to run.with_params(invalid, 10.0).and_raise_error(Puppet::ParseError, %r{to be a Numeric}) } + it { is_expected.to run.with_params(invalid, 10.0, -10.0).and_raise_error(Puppet::ParseError, %r{to be a Numeric}) } + end + + context 'when running on modern rubies', :unless => RUBY_VERSION == '1.8.7' do + it { is_expected.to run.with_params([0, 1, 2, { 1 => 2 }, 3, 4], 10, -10).and_raise_error(Puppet::ParseError, %r{to be a Numeric}) } + end + + context 'when running on ruby, which munges hashes weirdly', :if => RUBY_VERSION == '1.8.7' do + it { is_expected.to run.with_params([0, 1, 2, { 1 => 2 }, 3, 4], 10, -10).and_raise_error(Puppet::ParseError) } + it { is_expected.to run.with_params([0, 1, 2, { 0 => 2 }, 3, 4], 10, -10).and_raise_error(Puppet::ParseError) } + end + + it { is_expected.to run.with_params(1, '').and_raise_error(Puppet::ParseError, %r{to be unset or a Numeric}) } + it { is_expected.to run.with_params(1, 2, '').and_raise_error(Puppet::ParseError, %r{to be unset or a Numeric}) } + it { is_expected.to run.with_params(1, 2, 3).and_raise_error(Puppet::ParseError, %r{second argument to be larger than third argument}) } + end + + context 'with no range constraints' do + it { is_expected.to run.with_params(1) } + it { is_expected.to run.with_params(-1) } + it { is_expected.to run.with_params('1') } + it { is_expected.to run.with_params('-1') } + it { is_expected.to run.with_params([1, 2, 3, 4]) } + it { is_expected.to run.with_params([1, '2', '3', 4]) } + end + + context 'with a maximum limit of 10.0' do + describe 'rejects numbers greater than the limit' do + it { is_expected.to run.with_params(11, 10.0).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) } + it { is_expected.to run.with_params(100, 10.0).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) } + it { is_expected.to run.with_params(2**65, 10.0).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) } + it { is_expected.to run.with_params([1, 2, 10.0, 100], 10.0).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) } + end + + describe 'accepts numbers less or equal to the limit' do + it { is_expected.to run.with_params(10.0, 10.0) } + it { is_expected.to run.with_params(1, 10.0) } + it { is_expected.to run.with_params(-1, 10.0) } + it { is_expected.to run.with_params('1', 10.0) } + it { is_expected.to run.with_params('-1', 10.0) } + it { is_expected.to run.with_params([1, 2, 3, 4], 10.0) } + it { is_expected.to run.with_params([1, '2', '3', 4], 10.0) } + end + end + + context 'with a minimum limit of -10.0' do + describe 'rejects numbers greater than the upper limit' do + it { is_expected.to run.with_params(11, 10.0, -10.0).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) } + it { is_expected.to run.with_params(100, 10.0, -10.0).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) } + it { is_expected.to run.with_params(2**65, 10.0, -10.0).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) } + it { is_expected.to run.with_params([1, 2, 10.0, 100], 10.0, -10.0).and_raise_error(Puppet::ParseError, %r{to be smaller or equal}) } + end + + describe 'rejects numbers smaller than the lower limit' do + it { is_expected.to run.with_params(-11, 10.0, -10.0).and_raise_error(Puppet::ParseError, %r{to be greater or equal}) } + it { is_expected.to run.with_params(-100, 10.0, -10.0).and_raise_error(Puppet::ParseError, %r{to be greater or equal}) } + it { is_expected.to run.with_params(-2**65, 10.0, -10.0).and_raise_error(Puppet::ParseError, %r{to be greater or equal}) } + it { is_expected.to run.with_params([-10.0, 1, 2, 10.0, -100], 10.0, -10.0).and_raise_error(Puppet::ParseError, %r{to be greater or equal}) } + end + + describe 'accepts numbers between and including the limits' do + it { is_expected.to run.with_params(10.0, 10.0, -10.0) } + it { is_expected.to run.with_params(-10.0, 10.0, -10.0) } + it { is_expected.to run.with_params(1, 10.0, -10.0) } + it { is_expected.to run.with_params(-1, 10.0, -10.0) } + it { is_expected.to run.with_params('1', 10.0, -10.0) } + it { is_expected.to run.with_params('-1', 10.0, -10.0) } + it { is_expected.to run.with_params([1, 2, 3, 4], 10.0, -10.0) } + it { is_expected.to run.with_params([1, '2', '3', 4], 10.0, -10.0) } + end + end + + it { is_expected.to run.with_params(10.0, 10.0, 10.0) } + + describe 'empty upper limit is interpreted as infinity' do + it { is_expected.to run.with_params(11, '', 10.0) } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/validate_re_spec.rb b/code/environments/production/modules/stdlib/spec/functions/validate_re_spec.rb new file mode 100644 index 0000000..84391b3 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/validate_re_spec.rb @@ -0,0 +1,56 @@ +require 'spec_helper' + +describe 'validate_re' do + after(:each) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + + # Checking for deprecation warning + it 'displays 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 + + describe 'signature validation' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('', '', '', 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + + describe 'valid inputs' do + it { is_expected.to run.with_params('', '') } + it { is_expected.to run.with_params('', ['']) } + it { is_expected.to run.with_params('', [''], 'custom error') } + it { is_expected.to run.with_params('one', '^one') } + it { is_expected.to run.with_params('one', ['^one', '^two']) } + it { is_expected.to run.with_params('one', ['^one', '^two'], 'custom error') } + end + + describe 'invalid inputs' do + it { is_expected.to run.with_params('', []).and_raise_error(Puppet::ParseError, %r{does not match}) } + it { is_expected.to run.with_params('one', 'two').and_raise_error(Puppet::ParseError, %r{does not match}) } + it { is_expected.to run.with_params('', 'two').and_raise_error(Puppet::ParseError, %r{does not match}) } + it { is_expected.to run.with_params('', ['two']).and_raise_error(Puppet::ParseError, %r{does not match}) } + it { is_expected.to run.with_params('', ['two'], 'custom error').and_raise_error(Puppet::ParseError, %r{custom error}) } + it { is_expected.to run.with_params('notone', '^one').and_raise_error(Puppet::ParseError, %r{does not match}) } + it { is_expected.to run.with_params('notone', ['^one', '^two']).and_raise_error(Puppet::ParseError, %r{does not match}) } + it { is_expected.to run.with_params('notone', ['^one', '^two'], 'custom error').and_raise_error(Puppet::ParseError, %r{custom error}) } + end + + describe 'non-string inputs' do + [ + 1, # Fixnum + 3.14, # Float + nil, # NilClass + true, # TrueClass + false, # FalseClass + ['10'], # Array + :key, # Symbol + { :key => 'val' }, # Hash + ].each do |input| + it { is_expected.to run.with_params(input, '.*').and_raise_error(Puppet::ParseError, %r{needs to be a String}) } + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/validate_slength_spec.rb b/code/environments/production/modules/stdlib/spec/functions/validate_slength_spec.rb new file mode 100644 index 0000000..bde989b --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/validate_slength_spec.rb @@ -0,0 +1,81 @@ +require 'spec_helper' + +describe 'validate_slength' do + after(:each) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + + # Checking for deprecation warning + it 'displays a single deprecation' do + ENV['STDLIB_LOG_DEPRECATIONS'] = 'true' + scope.expects(:warning).with(includes('This method is deprecated')) + is_expected.to run.with_params('1234567890', 10) + end + + describe 'signature validation' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('', 2, 3, 'extra').and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params('', '').and_raise_error(Puppet::ParseError, %r{second argument to be a positive Numeric}) } + it { is_expected.to run.with_params('', -1).and_raise_error(Puppet::ParseError, %r{second argument to be a positive Numeric}) } + it { is_expected.to run.with_params('', 1, '').and_raise_error(Puppet::ParseError, %r{third argument to be unset or a positive Numeric}) } + it { is_expected.to run.with_params('', 1, -1).and_raise_error(Puppet::ParseError, %r{third argument to be unset or a positive Numeric}) } + it { is_expected.to run.with_params('', 1, 2).and_raise_error(Puppet::ParseError, %r{argument to be equal to or larger than third argument}) } + end + + context 'with a maximum length of 10' do + describe 'rejects strings longer than the limit' do + it { is_expected.to run.with_params('1234567890a', 10).and_raise_error(Puppet::ParseError, %r{Expected length}) } + it { is_expected.to run.with_params('1234567890abcdef', 10).and_raise_error(Puppet::ParseError, %r{Expected length}) } + it { is_expected.to run.with_params(%w[one 1234567890abcdef], 10).and_raise_error(Puppet::ParseError, %r{Expected length}) } + end + + describe 'accepts strings shorter or equal to the limit' do + it { is_expected.to run.with_params('1234567890', 10) } + it { is_expected.to run.with_params('12345', 10) } + it { is_expected.to run.with_params(%w[one two], 10) } + end + end + + context 'with a minimum length of 5' do + describe 'rejects strings longer than the upper limit' do + it { is_expected.to run.with_params('1234567890a', 10, 5).and_raise_error(Puppet::ParseError, %r{Expected length}) } + it { is_expected.to run.with_params('1234567890abcdef', 10, 5).and_raise_error(Puppet::ParseError, %r{Expected length}) } + end + + describe 'rejects numbers shorter than the lower limit' do + it { is_expected.to run.with_params('one', 10, 5).and_raise_error(Puppet::ParseError, %r{Expected length}) } + it { is_expected.to run.with_params(%w[12345678 two], 10, 5).and_raise_error(Puppet::ParseError, %r{Expected length}) } + end + + describe 'accepts strings of length between and including the limits' do + it { is_expected.to run.with_params('12345', 10, 5) } + it { is_expected.to run.with_params('123456', 10, 5) } + it { is_expected.to run.with_params('1234567', 10, 5) } + it { is_expected.to run.with_params('12345678', 10, 5) } + it { is_expected.to run.with_params('123456789', 10, 5) } + it { is_expected.to run.with_params('1234567890', 10, 5) } + it { is_expected.to run.with_params(%w[1233456 12345678], 10, 5) } + end + end + + describe 'corner cases' do + it { + pending('this should work') + is_expected.to run.with_params('', 0, 0) + } + it { is_expected.to run.with_params('1234567890', 10, 10) } + end + + describe 'empty upper limit is interpreted as infinity' do + it { + pending('not implemented') + is_expected.to run.with_params('1234567890ab', '', 10) + } + it { + pending('not implemented') + is_expected.to run.with_params('12345678', '', 10).and_raise_error(Puppet::ParseError, %r{Expected length}) + } + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/validate_string_spec.rb b/code/environments/production/modules/stdlib/spec/functions/validate_string_spec.rb new file mode 100644 index 0000000..4459f00 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/validate_string_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +describe 'validate_string' do + after(:each) do + ENV.delete('STDLIB_LOG_DEPRECATIONS') + end + + # Checking for deprecation warning + it 'displays 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 + + describe 'signature validation' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + + describe 'valid inputs' do + it { is_expected.to run.with_params('') } + it { is_expected.to run.with_params(nil) } + it { is_expected.to run.with_params('one') } + it { is_expected.to run.with_params('one', 'two') } + end + + describe 'invalid inputs' do + it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, %r{is not a string}) } + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/validate_x509_rsa_key_pair_spec.rb b/code/environments/production/modules/stdlib/spec/functions/validate_x509_rsa_key_pair_spec.rb new file mode 100644 index 0000000..891edd2 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/validate_x509_rsa_key_pair_spec.rb @@ -0,0 +1,181 @@ +require 'spec_helper' + +describe 'validate_x509_rsa_key_pair' do + # rubocop:disable Layout/IndentHeredoc : Heredoc's are meant to be indented in this way + let(:valid_cert) do + <<DOC +-----BEGIN CERTIFICATE----- +MIIC9jCCAeCgAwIBAgIRAK11n3X7aypJ7FPM8UFyAeowCwYJKoZIhvcNAQELMBIx +EDAOBgNVBAoTB0FjbWUgQ28wHhcNMTUxMTIzMjIzOTU4WhcNMTYxMTIyMjIzOTU4 +WjASMRAwDgYDVQQKEwdBY21lIENvMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB +CgKCAQEAz9bY/piKahD10AiJSfbI2A8NG5UwRz0r9T/WfvNVdhgrsGFgNQjvpUoZ +nNJpQIHBbgMOiXqfATFjJl5FjEkSf7GUHohlGVls9MX2JmVvknzsiitd75H/EJd+ +N+k915lix8Vqmj8d1CTlbF/8tEjzANI67Vqw5QTuqebO7rkIUvRg6yiRfSo75FK1 +RinCJyl++kmleBwQZBInQyg95GvJ5JTqMzBs67DeeyzskDhTeTePRYVF2NwL8QzY +htvLIBERTNsyU5i7nkxY5ptUwgFUwd93LH4Q19tPqL5C5RZqXxhE51thOOwafm+a +W/cRkqYqV+tv+j1jJ3WICyF1JNW0BQIDAQABo0swSTAOBgNVHQ8BAf8EBAMCAKAw +EwYDVR0lBAwwCgYIKwYBBQUHAwEwDAYDVR0TAQH/BAIwADAUBgNVHREEDTALggls +b2NhbGhvc3QwCwYJKoZIhvcNAQELA4IBAQAzRo0hpVTrFQZLIXpwvKwZVGvJdCkV +P95DTsSk/VTGV+/YtxrRqks++hJZnctm2PbnTsCAoIP3AMx+vicCKiKrxvpsLU8/ ++6cowUbcuGMdSQktwDqbAgEhQlLsETll06w1D/KC+ejOc4+LRn3GQcEyGDtMk/EX +IeAvBZHr4/kVXWnfo6kzCLcku1f8yE/yDEFClZe9XV1Lk/s+3YfXVtNnMJJ1giZI +QVOe6CkmuQq+4AtIeW8aLkvlfp632jag1F77a1y+L268koKkj0hBMrtcErVQaxmq +xym0+soR4Tk4pTIGckeFglrLxkP2JpM/yTwSEAVlmG9vgTliYKyR0uMl +-----END CERTIFICATE----- +DOC + end + + let(:valid_key) do + <<DOC +-----BEGIN RSA PRIVATE KEY----- +MIIEogIBAAKCAQEAz9bY/piKahD10AiJSfbI2A8NG5UwRz0r9T/WfvNVdhgrsGFg +NQjvpUoZnNJpQIHBbgMOiXqfATFjJl5FjEkSf7GUHohlGVls9MX2JmVvknzsiitd +75H/EJd+N+k915lix8Vqmj8d1CTlbF/8tEjzANI67Vqw5QTuqebO7rkIUvRg6yiR +fSo75FK1RinCJyl++kmleBwQZBInQyg95GvJ5JTqMzBs67DeeyzskDhTeTePRYVF +2NwL8QzYhtvLIBERTNsyU5i7nkxY5ptUwgFUwd93LH4Q19tPqL5C5RZqXxhE51th +OOwafm+aW/cRkqYqV+tv+j1jJ3WICyF1JNW0BQIDAQABAoIBADAiZ/r+xP+vkd5u +O61/lCBFzBlZQecdybJw6HJaVK6XBndA9hESUr4LHUdui6W+51ddKd65IV4bXAUk +zCKjQb+FFvLDT/bA+TTvLATUdTSN7hJJ3OWBAHuNOlQklof6JCB0Hi4+89+P8/pX +eKUgR/cmuTMDT/iaXdPHeqFbBQyA1ZpQFRjN5LyyJMS/9FkywuNc5wlpsArtc51T +gIKENUZCuPhosR+kMFc2iuTNvqZWPhvouSrmhi2O6nSqV+oy0+irlqSpCF2GsCI8 +72TtLpq94Grrq0BEH5avouV+Lp4k83vO65OKCQKUFQlxz3Xkxm2U3J7KzxqnRtM3 +/b+cJ/kCgYEA6/yOnaEYhH/7ijhZbPn8RujXZ5VGJXKJqIuaPiHMmHVS5p1j6Bah +2PcnqJA2IlLs3UloN+ziAxAIH6KCBiwlQ/uPBNMMaJsIjPNBEy8axjndKhKUpidg +R0OJ7RQqMShOJ8akrSfWdPtXC/GBuwCYE//t77GgZaIMO3FcT9EKA48CgYEA4Xcx +Fia0Jg9iyAhNmUOXI6hWcGENavMx01+x7XFhbnMjIKTZevFfTnTkrX6HyLXyGtMU +gHOn+k4PE/purI4ARrKO8m5wYEKqSIt4dBMTkIXXirfQjXgfjR8E4T/aPe5fOFZo +7OYuxLRtzmG1C2sW4txwKAKX1LaWcVx/RLSttSsCgYBbcj8Brk+F6OJcqYFdzXGJ +OOlf5mSMVlopyg83THmwCqbZXtw8L6kAHqZrl5airmfDSJLuOQlMDoZXW+3u3mSC +d5TwVahVUN57YDgzaumBLyMZDqIz0MZqVy23hTzkV64Rk9R0lR9xrYQJyMhw4sYL +2f0mCTsSpzz+O+t9so+i2QKBgEC38gMlwPhb2kMI/x1LZYr6uzUu5qcYf+jowy4h +KZKGwkKQj0zXFEB1FV8nvtpCP+irRmtIx6L13SYi8LnfWPzyLE4ynVdES5TfVAgd +obQOdzx+XwL8xDHCAaiWp5K3ZeXKB/xYZnxYPlzLdyh76Ond1OPnOqX4c16+6llS +c7pZAoGATd9NckT0XtXLEsF3IraDivq8dP6bccX2DNfS8UeEvRRrRwpFpSRrmuGb +jbG4yzoIX4RjQfj/z48hwhJB+cKiN9WwcPsFXtHe7v3F6BRwK0JUfrCiXad8/SGZ +KAf7Dfqi608zBdnPWHacre2Y35gPHB00nFQOLS6u46aBNSq07YA= +-----END RSA PRIVATE KEY----- +DOC + end + + let(:another_valid_key) do + <<DOC +-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEAoISxYJBTPAeAzFnm+lE/ljLlmGal2Xr3vwZKkvJiuKA/m4QJ +0ZNdtkBSDOVuG2dXVv6W4sChRtsCdvuVe7bjTYvlU8TWM3VEJDL9l9cRXScxxlKQ +Xwb35y1yV35NJfaK/jzm9KcErtQQs1RxvGlWRaohmLM8uQcuhjZfMsSlQoHQD5LX +sbPtk82RPyxYc1dj2vsaoi1VvuP2+jv4xLQOmNJY1bT5GTurqiltmxEtWhNNmGg0 +2wtK00ifqLVO5HNc3gXQCDM2M99Sbmn1YtbrgsU9xMYfcPmvQvb+YoKskyoqck+c +HR//hi7vslbxABrny15LBkEfRc4TickphSGYXwIDAQABAoIBAATEzGw8/WwMIQRx +K06GeWgh7PZBHm4+m/ud2TtSXiJ0CE+7dXs3cJJIiOd/LW08/bhE6gCkjmYHfaRB +Ryicv1X/cPmzIFX5BuQ4a5ZGOmrVDkKBE27vSxAgJoR46RvWnjx9XLMp/xaekDxz +psldK8X4DvV1ZbltgDFWji947hvyqUtHdKnkQnc5j7aCIFJf9GMfzaeeDPMaL8WF +mVL4iy9EAOjNOHBshZj/OHyU5FbJ8ROwZQlCOiLCdFegftSIXt8EYDnjB3BdsALH +N6hquqrD7xDKyRbTD0K7lqxUubuMwTQpi61jZD8TBTXEPyFVAnoMpXkc0Y+np40A +YiIsR+kCgYEAyrc4Bh6fb9gt49IXGXOSRZ5i5+TmJho4kzIONrJ7Ndclwx9wzHfh +eGBodWaw5CxxQGMf4vEiaZrpAiSFeDffBLR+Wa2TFE5aWkdYkR34maDjO00m4PE1 +S+YsZoGw7rGmmj+KS4qv2T26FEHtUI+F31RC1FPohLsQ22Jbn1ORipsCgYEAyrYB +J2Ncf2DlX1C0GfxyUHQOTNl0V5gpGvpbZ0WmWksumYz2kSGOAJkxuDKd9mKVlAcz +czmN+OOetuHTNqds2JJKKJy6hJbgCdd9aho3dId5Xs4oh4YwuFQiG8R/bJZfTlXo +99Qr02L7MmDWYLmrR3BA/93UPeorHPtjqSaYU40CgYEAtmGfWwokIglaSDVVqQVs +3YwBqmcrla5TpkMLvLRZ2/fktqfL4Xod9iKu+Klajv9ZKTfFkXWno2HHL7FSD/Yc +hWwqnV5oDIXuDnlQOse/SeERb+IbD5iUfePpoJQgbrCQlwiB0TNGwOojR2SFMczf +Ai4aLlQLx5dSND9K9Y7HS+8CgYEAixlHQ2r4LuQjoTs0ytwi6TgqE+vn3K+qDTwc +eoods7oBWRaUn1RCKAD3UClToZ1WfMRQNtIYrOAsqdveXpOWqioAP0wE5TTOuZIo +GiWxRgIsc7TNtOmNBv+chCdbNP0emxdyjJUIGb7DFnfCw47EjHnn8Guc13uXaATN +B2ZXgoUCgYAGa13P0ggUf5BMJpBd8S08jKRyvZb1CDXcUCuGtk2yEx45ern9U5WY +zJ13E5z9MKKO8nkGBqrRfjJa8Xhxk4HKNFuzHEet5lvNE7IKCF4YQRb0ZBhnb/78 ++4ZKjFki1RrWRNSw9TdvrK6qaDKgTtCTtfRVXAYQXUgq7lSFOTtL3A== +-----END RSA PRIVATE KEY----- +DOC + end + # rubocop:enable Layout/IndentHeredoc + + let(:valid_cert_but_indented) do + valid_cert.gsub(%r{^}, ' ') + end + + let(:valid_key_but_indented) do + valid_key.gsub(%r{^}, ' ') + end + + let(:malformed_cert) do + truncate_middle(valid_cert) + end + + let(:malformed_key) do + truncate_middle(valid_key) + end + + let(:bad_cert) do + 'foo' + end + + let(:bad_key) do + 'bar' + end + + context 'with function signature validation' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params(0, 1, 2, 3).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + end + + context 'with valid input' do + describe 'valid certificate and key' do + it { is_expected.to run.with_params(valid_cert, valid_key) } + end + end + + context 'with bad input' do + describe 'valid certificate, valid but indented key' do + it { is_expected.to run.with_params(valid_cert, valid_key_but_indented).and_raise_error(Puppet::ParseError, %r{Not a valid RSA key}) } + end + + describe 'valid certificate, malformed key' do + it { is_expected.to run.with_params(valid_cert, malformed_key).and_raise_error(Puppet::ParseError, %r{Not a valid RSA key}) } + end + + describe 'valid certificate, bad key' do + it { is_expected.to run.with_params(valid_cert, bad_key).and_raise_error(Puppet::ParseError, %r{Not a valid RSA key}) } + end + + describe 'valid but indented certificate, valid key' do + it { is_expected.to run.with_params(valid_cert_but_indented, valid_key).and_raise_error(Puppet::ParseError, %r{Not a valid x509 certificate}) } + end + + describe 'malformed certificate, valid key' do + it { is_expected.to run.with_params(malformed_cert, valid_key).and_raise_error(Puppet::ParseError, %r{Not a valid x509 certificate}) } + end + + describe 'bad certificate, valid key' do + it { is_expected.to run.with_params(bad_cert, valid_key).and_raise_error(Puppet::ParseError, %r{Not a valid x509 certificate}) } + end + + describe 'validate certificate and key; certficate not signed by key' do + it { is_expected.to run.with_params(valid_cert, another_valid_key).and_raise_error(Puppet::ParseError, %r{Certificate signature does not match supplied key}) } + end + + describe 'valid cert and key but arguments in wrong order' do + it { is_expected.to run.with_params(valid_key, valid_cert).and_raise_error(Puppet::ParseError, %r{Not a valid x509 certificate}) } + end + + describe 'non-string arguments' do + it { is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params(1, 1).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params(true, true).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params('foo', {}).and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params(1, 'bar').and_raise_error(Puppet::ParseError, %r{is not a string}) } + it { is_expected.to run.with_params('baz', true).and_raise_error(Puppet::ParseError, %r{is not a string}) } + end + end + + def truncate_middle(string) + chars_to_truncate = 48 + middle = (string.length / 2).floor + start_pos = middle - (chars_to_truncate / 2) + end_pos = middle + (chars_to_truncate / 2) + + string[start_pos...end_pos] = '' + string + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/values_at_spec.rb b/code/environments/production/modules/stdlib/spec/functions/values_at_spec.rb new file mode 100644 index 0000000..c405423 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/values_at_spec.rb @@ -0,0 +1,54 @@ +require 'spec_helper' + +describe 'values_at' do + describe 'signature validation' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('Current implementation ignores parameters after the first two.') + is_expected.to run.with_params([], 0, 1).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params('', 1).and_raise_error(Puppet::ParseError, %r{Requires array}i) } + it { is_expected.to run.with_params({}, 1).and_raise_error(Puppet::ParseError, %r{Requires array}i) } + it { is_expected.to run.with_params(true, 1).and_raise_error(Puppet::ParseError, %r{Requires array}i) } + it { is_expected.to run.with_params(1, 1).and_raise_error(Puppet::ParseError, %r{Requires array}i) } + it { is_expected.to run.with_params([0, 1, 2], 'two').and_raise_error(Puppet::ParseError, %r{Unknown format of given index}) } + it { is_expected.to run.with_params([0, 1, 2], []).and_raise_error(Puppet::ParseError, %r{provide at least one positive index}) } + it { is_expected.to run.with_params([0, 1, 2], '-1-1').and_raise_error(Puppet::ParseError, %r{Unknown format of given index}) } + it { is_expected.to run.with_params([0, 1, 2], '2-1').and_raise_error(Puppet::ParseError, %r{Stop index in given indices range is smaller than the start index}) } + end + + context 'when requesting a single item' do + it { is_expected.to run.with_params([0, 1, 2], -1).and_raise_error(Puppet::ParseError, %r{Unknown format of given index}) } + it { is_expected.to run.with_params([0, 1, 2], 0).and_return([0]) } + it { is_expected.to run.with_params([0, 1, 2], 1).and_return([1]) } + it { is_expected.to run.with_params([0, 1, 2], [1]).and_return([1]) } + it { is_expected.to run.with_params([0, 1, 2], '1').and_return([1]) } + it { is_expected.to run.with_params([0, 1, 2], '1-1').and_return([1]) } + it { is_expected.to run.with_params([0, 1, 2], 2).and_return([2]) } + it { is_expected.to run.with_params([0, 1, 2], 3).and_raise_error(Puppet::ParseError, %r{index exceeds array size}) } + end + + context 'when requesting a single item using UTF8 and double byte characters' do + it { is_expected.to run.with_params(%w[ẩ β с ď], 0).and_return(['ẩ']) } + it { is_expected.to run.with_params(%w[文 字 の 値], 2).and_return(['の']) } + end + + context 'when requesting multiple items' do + it { is_expected.to run.with_params([0, 1, 2], [1, -1]).and_raise_error(Puppet::ParseError, %r{Unknown format of given index}) } + it { is_expected.to run.with_params([0, 1, 2], [0, 2]).and_return([0, 2]) } + it { is_expected.to run.with_params([0, 1, 2], ['0-2', 1, 2]).and_return([0, 1, 2, 1, 2]) } + it { is_expected.to run.with_params([0, 1, 2], [3, 2]).and_raise_error(Puppet::ParseError, %r{index exceeds array size}) } + + describe 'different range syntaxes' do + it { is_expected.to run.with_params([0, 1, 2], '0-2').and_return([0, 1, 2]) } + it { is_expected.to run.with_params([0, 1, 2], '0..2').and_return([0, 1, 2]) } + it { is_expected.to run.with_params([0, 1, 2], '0...2').and_return([0, 1]) } + it { + pending('fix this bounds check') + is_expected.to run.with_params([0, 1, 2], '0...3').and_return([0, 1, 2]) + } + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/values_spec.rb b/code/environments/production/modules/stdlib/spec/functions/values_spec.rb new file mode 100644 index 0000000..85554c6 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/values_spec.rb @@ -0,0 +1,24 @@ +require 'spec_helper' + +describe 'values', :if => Puppet::Util::Package.versioncmp(Puppet.version, '5.5.0') < 0 do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{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, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params('').and_raise_error(Puppet::ParseError, %r{Requires hash to work with}) } + it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, %r{Requires hash to work with}) } + it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{Requires hash to work with}) } + it { is_expected.to run.with_params({}).and_return([]) } + it { is_expected.to run.with_params('key' => 'value').and_return(['value']) } + it 'returns the array of values' do + result = subject.call([{ 'key1' => 'value1', 'key2' => 'value2', 'duplicate_value_key' => 'value2' }]) + expect(result).to match_array(%w[value1 value2 value2]) + end + + it 'runs with UTF8 and double byte characters' do + result = subject.call([{ 'かぎ' => '使用', 'ҝĕұ' => '√ẩŀứệ', 'ҝĕұďŭрļǐçằťè' => '√ẩŀứệ' }]) + expect(result).to match_array(['使用', '√ẩŀứệ', '√ẩŀứệ']) + end +end diff --git a/code/environments/production/modules/stdlib/spec/functions/zip_spec.rb b/code/environments/production/modules/stdlib/spec/functions/zip_spec.rb new file mode 100644 index 0000000..2cc8c0c --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/functions/zip_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe 'zip' do + it { is_expected.not_to eq(nil) } + it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) } + it { + pending('Current implementation ignores parameters after the third.') + is_expected.to run.with_params([], [], true, []).and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) + } + it { is_expected.to run.with_params([], []).and_return([]) } + it { is_expected.to run.with_params([1, 2, 3], [4, 5, 6]).and_return([[1, 4], [2, 5], [3, 6]]) } + it { is_expected.to run.with_params([1, 2, 3], [4, 5, 6], false).and_return([[1, 4], [2, 5], [3, 6]]) } + it { is_expected.to run.with_params([1, 2, 3], [4, 5, 6], true).and_return([1, 4, 2, 5, 3, 6]) } + + context 'with UTF8 and double byte characters' do + it { is_expected.to run.with_params(%w[ầ ь ć], %w[đ ề ƒ]).and_return([%w[ầ đ], %w[ь ề], %w[ć ƒ]]) } + it { is_expected.to run.with_params(%w[ペ 含 値], %w[ッ 文 イ]).and_return([%w[ペ ッ], %w[含 文], %w[値 イ]]) } + end +end diff --git a/code/environments/production/modules/stdlib/spec/monkey_patches/alias_should_to_must.rb b/code/environments/production/modules/stdlib/spec/monkey_patches/alias_should_to_must.rb new file mode 100644 index 0000000..34dd076 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/monkey_patches/alias_should_to_must.rb @@ -0,0 +1,8 @@ +require 'rspec' + +class Object + # This is necessary because the RAL has a 'should' + # method. + alias must should + alias must_not should_not +end diff --git a/code/environments/production/modules/stdlib/spec/monkey_patches/publicize_methods.rb b/code/environments/production/modules/stdlib/spec/monkey_patches/publicize_methods.rb new file mode 100644 index 0000000..70cf4f0 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/monkey_patches/publicize_methods.rb @@ -0,0 +1,10 @@ +# Some monkey-patching to allow us to test private methods. +class Class + def publicize_methods(*methods) + saved_private_instance_methods = methods.empty? ? private_instance_methods : methods + + class_eval { public(*saved_private_instance_methods) } + yield + class_eval { private(*saved_private_instance_methods) } + end +end diff --git a/code/environments/production/modules/stdlib/spec/spec_helper.rb b/code/environments/production/modules/stdlib/spec/spec_helper.rb new file mode 100644 index 0000000..efd225b --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/spec_helper.rb @@ -0,0 +1,30 @@ +require 'puppetlabs_spec_helper/module_spec_helper' +require 'rspec-puppet-facts' + +begin + require 'spec_helper_local' if File.file?(File.join(File.dirname(__FILE__), 'spec_helper_local.rb')) +rescue LoadError => loaderror + warn "Could not require spec_helper_local: #{loaderror.message}" +end + +include RspecPuppetFacts + +default_facts = { + puppetversion: Puppet.version, + facterversion: Facter.version, +} + +default_facts_path = File.expand_path(File.join(File.dirname(__FILE__), 'default_facts.yml')) +default_module_facts_path = File.expand_path(File.join(File.dirname(__FILE__), 'default_module_facts.yml')) + +if File.exist?(default_facts_path) && File.readable?(default_facts_path) + default_facts.merge!(YAML.safe_load(File.read(default_facts_path))) +end + +if File.exist?(default_module_facts_path) && File.readable?(default_module_facts_path) + default_facts.merge!(YAML.safe_load(File.read(default_module_facts_path))) +end + +RSpec.configure do |c| + c.default_facts = default_facts +end diff --git a/code/environments/production/modules/stdlib/spec/spec_helper_acceptance.rb b/code/environments/production/modules/stdlib/spec/spec_helper_acceptance.rb new file mode 100644 index 0000000..68e8263 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/spec_helper_acceptance.rb @@ -0,0 +1,54 @@ +require 'puppet' +require 'beaker-rspec' +require 'beaker/puppet_install_helper' +require 'beaker/module_install_helper' + +run_puppet_install_helper +install_ca_certs unless ENV['PUPPET_INSTALL_TYPE'] =~ %r{pe}i +install_module_on(hosts) +install_module_dependencies_on(hosts) + +RSpec.configure do |c| + # Readable test descriptions + c.formatter = :documentation + + # Configure all nodes in nodeset + c.before :suite do + end +end + +def return_puppet_version + (on default, puppet('--version')).output.chomp +end + +RSpec.shared_context 'with faked facts' do + let(:facts_d) do + puppet_version = return_puppet_version + if fact('osfamily') =~ %r{windows}i + if fact('kernelmajversion').to_f < 6.0 + 'C:/Documents and Settings/All Users/Application Data/PuppetLabs/facter/facts.d' + else + 'C:/ProgramData/PuppetLabs/facter/facts.d' + end + elsif Puppet::Util::Package.versioncmp(puppet_version, '4.0.0') < 0 && fact('is_pe', '--puppet') == 'true' + '/etc/puppetlabs/facter/facts.d' + else + '/etc/facter/facts.d' + end + end + + before :each do + # No need to create on windows, PE creates by default + if fact('osfamily') !~ %r{windows}i + shell("mkdir -p '#{facts_d}'") + end + end + + after :each do + shell("rm -f '#{facts_d}/fqdn.txt'", :acceptable_exit_codes => [0, 1]) + end + + def fake_fact(name, value) + shell("echo #{name}=#{value} > '#{facts_d}/#{name}.txt'") + end +end diff --git a/code/environments/production/modules/stdlib/spec/spec_helper_local.rb b/code/environments/production/modules/stdlib/spec/spec_helper_local.rb new file mode 100644 index 0000000..5fea4da --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/spec_helper_local.rb @@ -0,0 +1,31 @@ +# automatically load any shared examples or contexts +Dir['./spec/support/**/*.rb'].sort.each { |f| require f } + +# HACK: to enable all the expect syntax (like allow_any_instance_of) in rspec-puppet examples +RSpec::Mocks::Syntax.enable_expect(RSpec::Puppet::ManifestMatchers) + +RSpec.configure do |config| + # supply tests with a possibility to test for the future parser + config.add_setting :puppet_future + config.puppet_future = Puppet.version.to_f >= 4.0 + + config.before :each do + # Ensure that we don't accidentally cache facts and environment between + # test cases. This requires each example group to explicitly load the + # facts being exercised with something like + # Facter.collection.loader.load(:ipaddress) + Facter.clear + Facter.clear_messages + + RSpec::Mocks.setup + end + + config.after :each do + RSpec::Mocks.verify + RSpec::Mocks.teardown + end +end + +# Helper class to test handling of arguments which are derived from string +class AlsoString < String +end diff --git a/code/environments/production/modules/stdlib/spec/support/shared_data.rb b/code/environments/production/modules/stdlib/spec/support/shared_data.rb new file mode 100644 index 0000000..013ec10 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/support/shared_data.rb @@ -0,0 +1,38 @@ +module SharedData + IPV4_PATTERNS = [ + '0.0.0.0', + '1.2.3.4', + '10.10.10.10', + '127.0.0.1', + '192.88.99.0', + '194.232.104.150', + '224.0.0.0', + '244.24.24.24', + '255.255.255.255', + '8.8.8.8', + '8.8.8.8/0', + '8.8.8.8/16', + '8.8.8.8/255.255.0.0', + '8.8.8.8/32', + ].freeze + IPV4_NEGATIVE_PATTERNS = [ + '', + '0000', + '0.0.0.0.', + '0.0.0.0./0.0.0.0.', + '0.0.0.0./1', + '0.0.0.0.0', + '0.0.0.0/0.0.0.0.', + '0.0.0.256', + '0.0.0', + '1.2.3.4.5', + '1.2.3', + '10.010.10.10', + '2001:0db8:85a3:0000:0000:8a2e:0370:73342001:0db8:85a3:0000:0000:8a2e:0370:7334', + '4.4.4', + '77', + '9999.9999.9999.9999', + 'affe::beef', + 'nope', + ].freeze +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/absolute_path_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/absolute_path_spec.rb new file mode 100644 index 0000000..55a6862 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/absolute_path_spec.rb @@ -0,0 +1,64 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Compat::Absolute_path' do + describe 'valid paths handling' do + %w[ + C:/ + C:\\ + C:\\WINDOWS\\System32 + C:/windows/system32 + X:/foo/bar + X:\\foo\\bar + \\\\host\\windows + //host/windows + / + /var/tmp + /var/opt/../lib/puppet + /var/opt//lib/puppet + /var/ůťƒ8 + /var/ネット + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'invalid path handling' do + context 'with garbage inputs' do + [ + nil, + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + + context 'with relative paths' do + %w[ + relative1 + . + .. + ./foo + ../foo + etc/puppetlabs/puppet + opt/puppet/bin + relative\\windows + \var\ůťƒ8 + \var\ネット + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/array_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/array_spec.rb new file mode 100644 index 0000000..0b0da3c --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/array_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Compat::Array' do + describe 'accepts arrays' do + [ + [], + ['one'], + [1], + [{}], + [[]], + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'rejects other values' do + [ + '', + 'one', + '1', + {}, + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/base32_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/base32_spec.rb new file mode 100644 index 0000000..7e0076f --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/base32_spec.rb @@ -0,0 +1,53 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Base32' do + describe 'valid handling' do + %w[ + ASDASDDASD3453453 + ASDASDDASD3453453= + ASDASDDASD3453453== + ASDASDDASD3453453=== + ASDASDDASD3453453==== + ASDASDDASD3453453===== + ASDASDDASD3453453====== + asdasddasd3453453 + asdasddasd3453453= + asdasddasd3453453== + asdasddasd3453453=== + asdasddasd3453453==== + asdasddasd3453453===== + asdasddasd3453453====== + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'invalid path handling' do + context 'garbage inputs' do + [ + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + 'asdasd!@#$', + '=asdasd9879876876+/', + 'asda=sd9879876876+/', + 'asdaxsd9879876876+/===', + 'asdads asdasd', + 'asdasddasd3453453=======', + 'asdaSddasd', + 'asdasddasd1', + 'asdasddasd9', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/base64_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/base64_spec.rb new file mode 100644 index 0000000..1c53916 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/base64_spec.rb @@ -0,0 +1,38 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Base64' do + describe 'valid handling' do + %w[ + asdasdASDSADA342386832/746+= + asdasdASDSADA34238683274/6+ + asdasdASDSADA3423868327/46+== + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'invalid path handling' do + context 'garbage inputs' do + [ + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + 'asdasd!@#$', + '=asdasd9879876876+/', + 'asda=sd9879876876+/', + 'asdaxsd9879876876+/===', + 'asdads asdasd', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/bool_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/bool_spec.rb new file mode 100644 index 0000000..bdc8f75 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/bool_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Compat::Bool' do + describe 'accepts booleans' do + [ + true, + false, + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'rejects other values' do + [ + [1], + [{}], + [true], + 'true', + 'false', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/compat__ip_address.rb b/code/environments/production/modules/stdlib/spec/type_aliases/compat__ip_address.rb new file mode 100644 index 0000000..671c64b --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/compat__ip_address.rb @@ -0,0 +1,38 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Compat::Ip_address' do + describe 'accepts ipv4 and ipv6 addresses' do + [ + '224.0.0.0', + '255.255.255.255', + '0.0.0.0', + '192.88.99.0', + '2001:0db8:85a3:0000:0000:8a2e:0370:7334', + 'fa76:8765:34ac:0823:ab76:eee9:0987:1111', + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + describe 'rejects other values' do + [ + nil, + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + 'nope', + '77', + '4.4.4', + '2001:0db8:85a3:000000:0000:8a2e:0370:7334', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/compat__ipv4_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/compat__ipv4_spec.rb new file mode 100644 index 0000000..dfd4be1 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/compat__ipv4_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Compat::Ipv4' do + describe 'accepts ipv4 addresses' do + SharedData::IPV4_PATTERNS.each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + describe 'rejects other values' do + SharedData::IPV4_NEGATIVE_PATTERNS.each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/compat__ipv6_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/compat__ipv6_spec.rb new file mode 100644 index 0000000..94211b6 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/compat__ipv6_spec.rb @@ -0,0 +1,44 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Compat::Ipv6' do + describe 'accepts ipv6 addresses' do + [ + '2001:0db8:85a3:0000:0000:8a2e:0370:7334', + 'fa76:8765:34ac:0823:ab76:eee9:0987:1111', + 'fe80:0000:0000:0000:0204:61ff:fe9d:f156', + 'fe80:0:0:0:204:61ff:fe9d:f156', + 'fe80::204:61ff:fe9d:f156', + 'fe80:0:0:0:0204:61ff:254.157.241.86', + '::1', + 'fe80::', + '2001::', + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + describe 'rejects other values' do + [ + nil, + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + 'nope', + '77', + '4.4.4', + '2000:7334', + '::ffff:2.3.4', + '::ffff:257.1.2.3', + '::ffff:12345678901234567890.1.26', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/filemode_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/filemode_spec.rb new file mode 100644 index 0000000..282f74a --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/filemode_spec.rb @@ -0,0 +1,46 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Filemode' do + describe 'valid modes' do + %w[ + 0644 + 1644 + 2644 + 4644 + 0123 + 0777 + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'invalid modes' do + context 'with garbage inputs' do + [ + nil, + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + 'ネット', + '644', + '7777', + '1', + '22', + '333', + '55555', + '0x123', + '0649', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/filesource_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/filesource_spec.rb new file mode 100644 index 0000000..da1bc3e --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/filesource_spec.rb @@ -0,0 +1,59 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Filesource' do + describe 'valid handling' do + %w[ + https://hello.com + https://notcreative.org + https://canstillaccepthttps.co.uk + http://anhttp.com + http://runningoutofideas.gov + file:///hello/bla + file:///foo/bar.log + puppet:///modules/foo/bar.log + puppet://pm.example.com/modules/foo/bar.log + puppet://192.0.2.1/modules/foo/bar.log + /usr2/username/bin:/usr/local/bin:/usr/bin:. + C:/ + C:\\ + C:\\WINDOWS\\System32 + C:/windows/system32 + X:/foo/bar + X:\\foo\\bar + \\\\host\\windows + //host/windows + /var/tmp + /var/opt/../lib/puppet + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'invalid path handling' do + context 'garbage inputs' do + [ + nil, + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + '*/Users//nope', + '\\Users/hc/wksp/stdlib', + 'C:noslashes', + '\\var\\tmp', + 'puppet:///foo/bar.log', + 'puppet:///pm.example.com/modules/foo/bar.log', + 'puppet://bob@pm.example.com/modules/foo/bar.log', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/float_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/float_spec.rb new file mode 100644 index 0000000..3d1d667 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/float_spec.rb @@ -0,0 +1,26 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Compat::Float' do + describe 'accepts floats' do + [ + 3.7, + '3.7', + -3.7, + '-342.2315e-12', + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + 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, '3', -3, '-3'].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/fqdn_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/fqdn_spec.rb new file mode 100644 index 0000000..e50aab1 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/fqdn_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Fqdn' do + describe 'valid handling' do + %w[ + example + example.com + www.example.com + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'invalid path handling' do + context 'garbage inputs' do + [ + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + '2001:DB8::1', + 'www www.example.com', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/hash_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/hash_spec.rb new file mode 100644 index 0000000..6e88a42 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/hash_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Compat::Hash' do + describe 'accepts hashes' do + [ + {}, + { 'one' => 'two' }, + { 'wan' => 3 }, + { '001' => 'helly' }, + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + describe 'rejects other values' do + [ + '', + 'one', + '1', + [], + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/host_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/host_spec.rb new file mode 100644 index 0000000..a85e0ee --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/host_spec.rb @@ -0,0 +1,44 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Host' do + describe 'valid handling' do + %w[ + example + example.com + www.example.com + 2001:0db8:85a3:0000:0000:8a2e:0370:7334 + fa76:8765:34ac:0823:ab76:eee9:0987:1111 + 2001:0db8::1 + 224.0.0.0 + 255.255.255.255 + 0.0.0.0 + 192.88.99.0 + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'invalid handling' do + context 'garbage inputs' do + [ + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + 'www www.example.com', + 'bob@example.com', + '%.example.com', + '2001:0d8', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/httpsurl_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/httpsurl_spec.rb new file mode 100644 index 0000000..dd95875 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/httpsurl_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::HTTPSUrl' do + describe 'valid handling' do + %w[ + https://hello.com + https://notcreative.org + https://notexciting.co.uk + https://graphemica.com/❤ + https://graphemica.com/緩 + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'invalid path handling' do + context 'with garbage inputs' do + [ + nil, + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + 'httds://notquiteright.org', + 'hptts:/nah', + 'https;//notrightbutclose.org', + 'http://graphemica.com/❤', + 'http://graphemica.com/緩', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/httpurl_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/httpurl_spec.rb new file mode 100644 index 0000000..6684e24 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/httpurl_spec.rb @@ -0,0 +1,44 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::HTTPUrl' do + describe 'valid handling' do + %w[ + https://hello.com + https://notcreative.org + https://canstillaccepthttps.co.uk + http://anhttp.com + http://runningoutofideas.gov + http:// + http://graphemica.com/❤ + http://graphemica.com/緩 + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'invalid path handling' do + context 'with garbage inputs' do + [ + nil, + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + 'httds://notquiteright.org', + 'hptts:/nah', + 'https;//notrightbutclose.org', + 'hts://graphemica.com/❤', + 'https:graphemica.com/緩', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/integer_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/integer_spec.rb new file mode 100644 index 0000000..29298fa --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/integer_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Compat::Integer' do + describe 'accepts integers' do + [ + 3, + '3', + -3, + '-3', + "123\nfoo", + "foo\n123", + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'rejects other values' do + ["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 + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/ip_address.rb b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address.rb new file mode 100644 index 0000000..e107f42 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address.rb @@ -0,0 +1,44 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Ip_address' do + describe 'accepts ipv4 and ipv6 addresses' do + [ + '224.0.0.0', + '255.255.255.255', + '0.0.0.0', + '192.88.99.0', + '2001:0db8:85a3:0000:0000:8a2e:0370:7334', + 'fa76:8765:34ac:0823:ab76:eee9:0987:1111', + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + describe 'rejects other values' do + [ + nil, + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + 'nope', + '77', + '4.4.4', + '2001:0db8:85a3:000000:0000:8a2e:0370:7334', + '2001::0db8::1', + ' 2001:0db8::1', + '2001:0db8::1 ', + ' 2001:0db8::1 ', + 'foobar2001:0db8::1', + '2001:0db8::1foobar', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_nosubnet_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_nosubnet_spec.rb new file mode 100644 index 0000000..921d957 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_nosubnet_spec.rb @@ -0,0 +1,44 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::IP::Address::Nosubnet' do + describe 'accepts ipv4 and ipv6 addresses without subnets' do + [ + '224.0.0.0', + '255.255.255.255', + '0.0.0.0', + '192.88.99.0', + '2001:0db8:85a3:0000:0000:8a2e:0370:7334', + 'fa76:8765:34ac:0823:ab76:eee9:0987:1111', + '127.0.0.1', + '8.8.4.4', + '52.10.10.141', + 'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210', + 'FF01:0:0:0:0:0:0:101', + 'FF01::101', + '::', + '12AB::CD30:192.168.0.1', + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'rejects other values' do + [ + '10.1.240.4/24', + 'FF01:0:0:0:0:0:0:101/32', + 'FF01::101/60', + 'nope', + '77', + '4.4.4', + '2001:0db8:85a3:000000:0000:8a2e:0370:7334', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_spec.rb new file mode 100644 index 0000000..e603350 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_spec.rb @@ -0,0 +1,44 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::IP::Address' do + describe 'accepts ipv4 and ipv6 addresses' do + [ + '224.0.0.0', + '255.255.255.255', + '0.0.0.0', + '192.88.99.0', + '2001:0db8:85a3:0000:0000:8a2e:0370:7334', + 'fa76:8765:34ac:0823:ab76:eee9:0987:1111', + '127.0.0.1', + '8.8.4.4', + '10.1.240.4/24', + '52.10.10.141', + 'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210', + 'FF01:0:0:0:0:0:0:101', + 'FF01::101', + 'FF01:0:0:0:0:0:0:101/32', + 'FF01::101/60', + '::', + '12AB::CD30:192.168.0.1', + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'rejects other values' do + [ + 'nope', + '77', + '4.4.4', + '2001:0db8:85a3:000000:0000:8a2e:0370:7334', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v4_nosubnet_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v4_nosubnet_spec.rb new file mode 100644 index 0000000..ab74f8c --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v4_nosubnet_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::IP::Address::V4::Nosubnet' do + describe 'accepts ipv4 addresses without subnets' do + [ + '127.0.0.1', + '8.8.4.4', + '52.10.10.141', + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'rejects other values' do + [ + '10.1.240.4/24', + '192.168.1', + 'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210', + '12AB::CD30:192.168.0.1', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v4_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v4_spec.rb new file mode 100644 index 0000000..10854c8 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v4_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::IP::Address::V4' do + describe 'accepts ipv4 addresses' do + [ + '127.0.0.1', + '8.8.4.4', + '10.1.240.4/24', + '52.10.10.141', + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'rejects other values' do + [ + '192.168.1', + 'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210', + '12AB::CD30:192.168.0.1', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v6_alternative_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v6_alternative_spec.rb new file mode 100644 index 0000000..9fbf7ca --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v6_alternative_spec.rb @@ -0,0 +1,28 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::IP::Address::V6::Alternative' do + describe 'accepts ipv6 addresses in alternative format' do + [ + '0:0:0:0:0:0:13.1.68.3', + '0:0:0:0:0:FFFF:129.144.52.38', + '0:0:0:0:0:FFFF:129.144.52.38/60', + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'rejects other values' do + [ + 'nope', + '127.0.0.1', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v6_compressed_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v6_compressed_spec.rb new file mode 100644 index 0000000..e2b7dd5 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v6_compressed_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::IP::Address::V6::Compressed' do + describe 'accepts ipv6 addresses in compressed format' do + [ + '1080::8:800:200C:417A', + '1080::8:800:200C:417A/60', + 'FF01::101', + '::1', + '::', + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'rejects other values' do + [ + 'nope', + '127.0.0.1', + 'FEDC::BA98:7654:3210::3210', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v6_full_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v6_full_spec.rb new file mode 100644 index 0000000..cc8013d --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v6_full_spec.rb @@ -0,0 +1,28 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::IP::Address::V6::Full' do + describe 'accepts ipv6 addresses in full format' do + [ + 'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210', + 'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210/60', + '1080:0:0:0:8:800:200C:417A', + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'rejects other values' do + [ + 'nope', + '127.0.0.1', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v6_nosubnet_alternative_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v6_nosubnet_alternative_spec.rb new file mode 100644 index 0000000..0b36cb9 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v6_nosubnet_alternative_spec.rb @@ -0,0 +1,28 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::IP::Address::V6::Nosubnet::Alternative' do + describe 'accepts ipv6 addresses in alternative format without subnets' do + [ + '0:0:0:0:0:0:13.1.68.3', + '0:0:0:0:0:FFFF:129.144.52.38', + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'rejects other values' do + [ + '0:0:0:0:0:FFFF:129.144.52.38/60', + 'nope', + '127.0.0.1', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v6_nosubnet_compressed_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v6_nosubnet_compressed_spec.rb new file mode 100644 index 0000000..96af035 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v6_nosubnet_compressed_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::IP::Address::V6::Nosubnet::Compressed' do + describe 'accepts ipv6 addresses in compressed format without subnets' do + [ + '1080::8:800:200C:417A', + 'FF01::101', + '::1', + '::', + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'rejects other values' do + [ + '1080::8:800:200C:417A/60', + 'nope', + '127.0.0.1', + 'FEDC::BA98:7654:3210::3210', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v6_nosubnet_full_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v6_nosubnet_full_spec.rb new file mode 100644 index 0000000..9135e00 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v6_nosubnet_full_spec.rb @@ -0,0 +1,28 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::IP::Address::V6::Nosubnet::Full' do + describe 'accepts ipv6 addresses in full format without subnets' do + [ + 'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210', + '1080:0:0:0:8:800:200C:417A', + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'rejects other values' do + [ + 'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210/60', + 'nope', + '127.0.0.1', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v6_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v6_spec.rb new file mode 100644 index 0000000..864c565 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/ip_address_v6_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::IP::Address::V6' do + describe 'accepts ipv6 addresses' do + [ + 'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210', + 'FF01:0:0:0:0:0:0:101', + 'FF01::101', + 'FF01:0:0:0:0:0:0:101/32', + 'FF01::101/60', + '::', + '12AB::CD30:192.168.0.1', + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'rejects other values' do + [ + '127.0.0.1', + '10.1.240.4/24', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/ipv4_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/ipv4_spec.rb new file mode 100644 index 0000000..c6c4b28 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/ipv4_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Ipv4' do + describe 'accepts ipv4 addresses' do + SharedData::IPV4_PATTERNS.each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + describe 'rejects other values' do + SharedData::IPV4_NEGATIVE_PATTERNS.each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/ipv6_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/ipv6_spec.rb new file mode 100644 index 0000000..820c469 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/ipv6_spec.rb @@ -0,0 +1,50 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Ipv6' do + describe 'accepts ipv6 addresses' do + [ + '2001:0db8:85a3:0000:0000:8a2e:0370:7334', + 'fa76:8765:34ac:0823:ab76:eee9:0987:1111', + 'fe80:0000:0000:0000:0204:61ff:fe9d:f156', + 'fe80:0:0:0:204:61ff:fe9d:f156', + 'fe80::204:61ff:fe9d:f156', + 'fe80:0:0:0:0204:61ff:254.157.241.86', + '::1', + 'fe80::', + '2001::', + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + describe 'rejects other values' do + [ + nil, + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + 'nope', + '77', + '4.4.4', + '2000:7334', + '::ffff:2.3.4', + '::ffff:257.1.2.3', + '::ffff:12345678901234567890.1.26', + '2001::0db8::1', + ' 2001:0db8::1', + '2001:0db8::1 ', + ' 2001:0db8::1 ', + 'foobar2001:0db8::1', + '2001:0db8::1foobar', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/numeric_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/numeric_spec.rb new file mode 100644 index 0000000..a59b4e2 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/numeric_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Compat::Numeric' do + describe 'accepts numerics' do + [ + 3, + '3', + -3, + '-3', + 3.7, + '3.7', + -3.7, + '-342.2315e-12', + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + 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'].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/port__privileged_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/port__privileged_spec.rb new file mode 100644 index 0000000..51ddd24 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/port__privileged_spec.rb @@ -0,0 +1,39 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Port::Privileged' do + describe 'valid ports' do + [ + 80, + 443, + 1023, + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'invalid path handling' do + context 'garbage inputs' do + [ + nil, + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + 'https', + '443', + -1, + 1337, + 1024, + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/port__unprivileged_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/port__unprivileged_spec.rb new file mode 100644 index 0000000..0009e1f --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/port__unprivileged_spec.rb @@ -0,0 +1,40 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Port::Unprivileged' do + describe 'valid unprivilegedport' do + [ + 1024, + 1337, + 65_000, + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'invalid path handling' do + context 'garbage inputs' do + [ + nil, + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + 'https', + '443', + -1, + 80, + 443, + 1023, + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/port_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/port_spec.rb new file mode 100644 index 0000000..3c9582c --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/port_spec.rb @@ -0,0 +1,39 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Port' do + describe 'valid ports' do + [ + 80, + 443, + 1337, + 65_000, + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'invalid path handling' do + context 'garbage inputs' do + [ + nil, + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + 'https', + '443', + -1, + 65_536, + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/string_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/string_spec.rb new file mode 100644 index 0000000..93a9d0f --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/string_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Compat::String' do + describe 'accepts strings' do + [ + '', + 'one', + nil, + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'rejects other values' do + [ + [], + {}, + 1, + true, + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/unixpath_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/unixpath_spec.rb new file mode 100644 index 0000000..3d9e95c --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/unixpath_spec.rb @@ -0,0 +1,44 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Unixpath' do + describe 'valid handling' do + %w[ + /usr2/username/bin:/usr/local/bin:/usr/bin:. + /var/tmp + /Users/helencampbell/workspace/puppetlabs-stdlib + /var/ůťƒ8 + /var/ネット + /var//tmp + /var/../tmp + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'invalid path handling' do + context 'with garbage inputs' do + [ + nil, + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + 'C:/whatever', + '\\var\\tmp', + '\\Users/hc/wksp/stdlib', + '*/Users//nope', + "var\ůťƒ8", + "var\ネット", + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/type_aliases/windowspath_spec.rb b/code/environments/production/modules/stdlib/spec/type_aliases/windowspath_spec.rb new file mode 100644 index 0000000..fdf2dc4 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/type_aliases/windowspath_spec.rb @@ -0,0 +1,45 @@ +require 'spec_helper' + +if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0 + describe 'Stdlib::Windowspath' do + describe 'valid handling' do + %w[ + C:\\ + C:\\WINDOWS\\System32 + C:/windows/system32 + X:/foo/bar + X:\\foo\\bar + \\\\host\\windows + X:/var/ůťƒ8 + X:/var/ネット + ].each do |value| + describe value.inspect do + it { is_expected.to allow_value(value) } + end + end + end + + describe 'invalid path handling' do + context 'with garbage inputs' do + [ + nil, + [nil], + [nil, nil], + { 'foo' => 'bar' }, + {}, + '', + 'httds://notquiteright.org', + '/usr2/username/bin:/usr/local/bin:/usr/bin:.', + 'C;//notright/here', + 'C:noslashes', + 'C:ネット', + 'C:ůťƒ8', + ].each do |value| + describe value.inspect do + it { is_expected.not_to allow_value(value) } + end + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/unit/facter/facter_dot_d_spec.rb b/code/environments/production/modules/stdlib/spec/unit/facter/facter_dot_d_spec.rb new file mode 100644 index 0000000..49707ab --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/unit/facter/facter_dot_d_spec.rb @@ -0,0 +1,30 @@ +require 'spec_helper' +require 'facter/facter_dot_d' + +describe Facter::Util::DotD do # rubocop:disable RSpec/FilePath : Spec path is as it should be + context 'with a simple fact' do + before :each do + Facter.stubs(:version).returns('1.6.1') + subject.stubs(:entries).returns(['/etc/facter/facts.d/fake_fact.txt']) + File.stubs(:readlines).with('/etc/facter/facts.d/fake_fact.txt').returns(['fake_fact=fake fact']) + subject.create + end + + it 'returns successfully' do + expect(Facter.fact(:fake_fact).value).to eq('fake fact') + end + end + + context 'with a fact with equals signs' do + before :each do + Facter.stubs(:version).returns('1.6.1') + subject.stubs(:entries).returns(['/etc/facter/facts.d/foo.txt']) + File.stubs(:readlines).with('/etc/facter/facts.d/foo.txt').returns(['foo=1+1=2']) + subject.create + end + + it 'returns successfully' do + expect(Facter.fact(:foo).value).to eq('1+1=2') + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/unit/facter/package_provider_spec.rb b/code/environments/production/modules/stdlib/spec/unit/facter/package_provider_spec.rb new file mode 100644 index 0000000..2ebfe2d --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/unit/facter/package_provider_spec.rb @@ -0,0 +1,43 @@ +require 'spec_helper' +require 'puppet/type' +require 'puppet/type/package' + +describe 'package_provider', :type => :fact do + before(:each) { Facter.clear } + after(:each) { Facter.clear } + + ['4.2.2', '3.7.1 (Puppet Enterprise 3.2.1)'].each do |puppetversion| + describe "on puppet ''#{puppetversion}''" do + before :each do + Facter.stubs(:value).returns puppetversion + end + + context 'when darwin' do + it 'returns pkgdmg' do + provider = Puppet::Type.type(:package).provider(:pkgdmg) + Puppet::Type.type(:package).stubs(:defaultprovider).returns provider + + expect(Facter.fact(:package_provider).value).to eq('pkgdmg') + end + end + + context 'when centos 7' do + it 'returns yum' do + provider = Puppet::Type.type(:package).provider(:yum) + Puppet::Type.type(:package).stubs(:defaultprovider).returns provider + + expect(Facter.fact(:package_provider).value).to eq('yum') + end + end + + context 'when ubuntu' do + it 'returns apt' do + provider = Puppet::Type.type(:package).provider(:apt) + Puppet::Type.type(:package).stubs(:defaultprovider).returns provider + + expect(Facter.fact(:package_provider).value).to eq('apt') + end + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/unit/facter/pe_version_spec.rb b/code/environments/production/modules/stdlib/spec/unit/facter/pe_version_spec.rb new file mode 100644 index 0000000..73c4bfd --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/unit/facter/pe_version_spec.rb @@ -0,0 +1,88 @@ +require 'spec_helper' + +describe 'PE Version specs' do + before :each do + # Explicitly load the pe_version.rb file which contains generated facts + # that cannot be automatically loaded. Puppet 2.x implements + # Facter.collection.load while Facter 1.x markes Facter.collection.load as + # a private method. + if Facter.collection.respond_to? :load + Facter.collection.load(:pe_version) + else + Facter.collection.loader.load(:pe_version) + end + end + + context 'when puppetversion is nil' do + before :each do + Facter.fact(:puppetversion).stubs(:value).returns(nil) + end + + it 'puppetversion is nil' do + expect(Facter.fact(:puppetversion).value).to be_nil + end + + it 'pe_version is nil' do + expect(Facter.fact(:pe_version).value).to be_nil + end + end + + context 'when PE is installed' do + %w[2.6.1 2.10.300].each do |version| + puppetversion = "2.7.19 (Puppet Enterprise #{version})" + context "puppetversion => #{puppetversion}" do + before :each do + Facter.fact(:puppetversion).stubs(:value).returns(puppetversion) + end + + (major, minor, patch) = version.split('.') + + it 'returns true' do + expect(Facter.fact(:is_pe).value).to eq(true) + end + + it "Should have a version of #{version}" do + expect(Facter.fact(:pe_version).value).to eq(version) + end + + it "Should have a major version of #{major}" do + expect(Facter.fact(:pe_major_version).value).to eq(major) + end + + it "Should have a minor version of #{minor}" do + expect(Facter.fact(:pe_minor_version).value).to eq(minor) + end + + it "Should have a patch version of #{patch}" do + expect(Facter.fact(:pe_patch_version).value).to eq(patch) + end + end + end + end + + context 'when PE is not installed' do + before :each do + Facter.fact(:puppetversion).stubs(:value).returns('2.7.19') + end + + it 'is_pe is false' do + expect(Facter.fact(:is_pe).value).to eq(false) + end + + it 'pe_version is nil' do + expect(Facter.fact(:pe_version).value).to be_nil + end + + it 'pe_major_version is nil' do + expect(Facter.fact(:pe_major_version).value).to be_nil + end + + it 'pe_minor_version is nil' do + expect(Facter.fact(:pe_minor_version).value).to be_nil + end + + it 'has a patch version' do + expect(Facter.fact(:pe_patch_version).value).to be_nil + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/unit/facter/root_home_spec.rb b/code/environments/production/modules/stdlib/spec/unit/facter/root_home_spec.rb new file mode 100644 index 0000000..656f65d --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/unit/facter/root_home_spec.rb @@ -0,0 +1,67 @@ +require 'spec_helper' +require 'facter/root_home' +describe 'Root Home Specs' do + describe Facter::Util::RootHome do + context 'when solaris' do + let(:root_ent) { 'root:x:0:0:Super-User:/:/sbin/sh' } + let(:expected_root_home) { '/' } + + it 'returns /' do + Facter::Util::Resolution.expects(:exec).with('getent passwd root').returns(root_ent) + expect(described_class.returnt_root_home).to eq(expected_root_home) + end + end + context 'when linux' do + let(:root_ent) { 'root:x:0:0:root:/root:/bin/bash' } + let(:expected_root_home) { '/root' } + + it 'returns /root' do + Facter::Util::Resolution.expects(:exec).with('getent passwd root').returns(root_ent) + expect(described_class.returnt_root_home).to eq(expected_root_home) + end + end + context 'when windows' do + before :each do + Facter::Util::Resolution.expects(:exec).with('getent passwd root').returns(nil) + end + it 'is nil on windows' do + expect(described_class.returnt_root_home).to be_nil + end + end + end + + describe 'root_home', :type => :fact do + before(:each) { Facter.clear } + after(:each) { Facter.clear } + + context 'when macosx' do + before(:each) do + Facter.fact(:kernel).stubs(:value).returns('Darwin') + Facter.fact(:osfamily).stubs(:value).returns('Darwin') + end + let(:expected_root_home) { '/var/root' } + + sample_dscacheutil = File.read(fixtures('dscacheutil', 'root')) + + it 'returns /var/root' do + Facter::Util::Resolution.stubs(:exec).with('dscacheutil -q user -a name root').returns(sample_dscacheutil) + expect(Facter.fact(:root_home).value).to eq(expected_root_home) + end + end + + context 'when aix' do + before(:each) do + Facter.fact(:kernel).stubs(:value).returns('AIX') + Facter.fact(:osfamily).stubs(:value).returns('AIX') + end + let(:expected_root_home) { '/root' } + + sample_lsuser = File.read(fixtures('lsuser', 'root')) + + it 'returns /root' do + Facter::Util::Resolution.stubs(:exec).with('lsuser -c -a home root').returns(sample_lsuser) + expect(Facter.fact(:root_home).value).to eq(expected_root_home) + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/unit/facter/service_provider_spec.rb b/code/environments/production/modules/stdlib/spec/unit/facter/service_provider_spec.rb new file mode 100644 index 0000000..202f7c0 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/unit/facter/service_provider_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' +require 'puppet/type' +require 'puppet/type/service' + +describe 'service_provider', :type => :fact do + before(:each) { Facter.clear } + after(:each) { Facter.clear } + + context 'when macosx' do + it 'returns launchd' do + provider = Puppet::Type.type(:service).provider(:launchd) + Puppet::Type.type(:service).stubs(:defaultprovider).returns provider + + expect(Facter.fact(:service_provider).value).to eq('launchd') + end + end + + context 'when systemd' do + it 'returns systemd' do + provider = Puppet::Type.type(:service).provider(:systemd) + Puppet::Type.type(:service).stubs(:defaultprovider).returns provider + + expect(Facter.fact(:service_provider).value).to eq('systemd') + end + end + + context 'when redhat' do + it 'returns redhat' do + provider = Puppet::Type.type(:service).provider(:redhat) + Puppet::Type.type(:service).stubs(:defaultprovider).returns provider + + expect(Facter.fact(:service_provider).value).to eq('redhat') + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/unit/facter/util/puppet_settings_spec.rb b/code/environments/production/modules/stdlib/spec/unit/facter/util/puppet_settings_spec.rb new file mode 100644 index 0000000..dd870bb --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/unit/facter/util/puppet_settings_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' +require 'facter/util/puppet_settings' + +describe Facter::Util::PuppetSettings do + describe '#with_puppet' do + context 'without Puppet loaded' do + before(:each) do + Module.expects(:const_get).with('Puppet').raises(NameError) + end + + it 'is nil' do + expect(subject.with_puppet { Puppet[:vardir] }).to be_nil + end + it 'does not yield to the block' do + Puppet.expects(:[]).never + expect(subject.with_puppet { Puppet[:vardir] }).to be_nil + end + end + context 'with Puppet loaded' do + module Puppet; end + let(:vardir) { '/var/lib/puppet' } + + before :each do + Puppet.expects(:[]).with(:vardir).returns vardir + end + + it 'yields to the block' do + subject.with_puppet { Puppet[:vardir] } + end + it 'returns the nodes vardir' do + expect(subject.with_puppet { Puppet[:vardir] }).to eq vardir + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/unit/puppet/parser/functions/enclose_ipv6_spec.rb b/code/environments/production/modules/stdlib/spec/unit/puppet/parser/functions/enclose_ipv6_spec.rb new file mode 100644 index 0000000..c2a237e --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/unit/puppet/parser/functions/enclose_ipv6_spec.rb @@ -0,0 +1,68 @@ +require 'spec_helper' + +describe 'the enclose_ipv6 function' do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it 'exists' do + expect(Puppet::Parser::Functions.function('enclose_ipv6')).to eq('function_enclose_ipv6') + end + + it 'raises a ParseError if there is less than 1 arguments' do + expect { scope.function_enclose_ipv6([]) }.to(raise_error(Puppet::ParseError)) + end + + it 'raises a ParseError if there is more than 1 arguments' do + expect { scope.function_enclose_ipv6(%w[argument1 argument2]) }.to(raise_error(Puppet::ParseError)) + end + + it 'raises a ParseError when given garbage' do + expect { scope.function_enclose_ipv6(['garbage']) }.to(raise_error(Puppet::ParseError)) + end + + it 'raises a ParseError when given something else than a string or an array' do + expect { scope.function_enclose_ipv6([['1' => '127.0.0.1']]) }.to(raise_error(Puppet::ParseError)) + end + + it 'does not raise a ParseError when given a single ip string' do + expect { scope.function_enclose_ipv6(['127.0.0.1']) }.not_to raise_error + end + + it 'does not raise a ParseError when given * as ip string' do + expect { scope.function_enclose_ipv6(['*']) }.not_to raise_error + end + + it 'does not raise a ParseError when given an array of ip strings' do + expect { scope.function_enclose_ipv6([['127.0.0.1', 'fe80::1']]) }.not_to raise_error + end + + it 'does not raise a ParseError when given differently notations of ip addresses' do + expect { scope.function_enclose_ipv6([['127.0.0.1', 'fe80::1', '[fe80::1]']]) }.not_to raise_error + end + + it 'raises a ParseError when given a wrong ipv4 address' do + expect { scope.function_enclose_ipv6(['127..0.0.1']) }.to(raise_error(Puppet::ParseError)) + end + + it 'raises a ParseError when given a ipv4 address with square brackets' do + expect { scope.function_enclose_ipv6(['[127.0.0.1]']) }.to(raise_error(Puppet::ParseError)) + end + + it 'raises a ParseError when given a wrong ipv6 address' do + expect { scope.function_enclose_ipv6(['fe80:::1']) }.to(raise_error(Puppet::ParseError)) + end + + it 'embraces ipv6 adresses within an array of ip addresses' do + result = scope.function_enclose_ipv6([['127.0.0.1', 'fe80::1', '[fe80::2]']]) + expect(result).to(eq(['127.0.0.1', '[fe80::1]', '[fe80::2]'])) + end + + it 'embraces a single ipv6 adresse' do + result = scope.function_enclose_ipv6(['fe80::1']) + expect(result).to(eq(['[fe80::1]'])) + end + + it 'does not embrace a single ipv4 adresse' do + result = scope.function_enclose_ipv6(['127.0.0.1']) + expect(result).to(eq(['127.0.0.1'])) + end +end diff --git a/code/environments/production/modules/stdlib/spec/unit/puppet/parser/functions/is_absolute_path_spec.rb b/code/environments/production/modules/stdlib/spec/unit/puppet/parser/functions/is_absolute_path_spec.rb new file mode 100644 index 0000000..589ad82 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/unit/puppet/parser/functions/is_absolute_path_spec.rb @@ -0,0 +1,87 @@ +require 'spec_helper' + +describe 'is_absolute_path' do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + let(:function_args) do + [] + end + + let(:function) do + scope.function_is_absolute_path(function_args) + end + + describe 'validate arity' do + let(:function_args) do + [1, 2] + end + + it 'raises a ParseError if there is more than 1 arguments' do + -> { function }.should(raise_error(ArgumentError)) + end + end + + it 'exists' do + Puppet::Parser::Functions.function(subject).should == "function_#{subject}" + end + + # help enforce good function defination + it 'contains arity' do + end + + it 'raises a ParseError if there is less than 1 arguments' do + -> { function }.should(raise_error(ArgumentError)) + end + + describe 'should retrun true' do + let(:return_value) do + true + end + + describe 'windows' do + let(:function_args) do + ['c:\temp\test.txt'] + end + + it 'returns data' do + function.should eq(return_value) + end + end + + describe 'non-windows' do + let(:function_args) do + ['/temp/test.txt'] + end + + it 'returns data' do + function.should eq(return_value) + end + end + end + + describe 'should return false' do + let(:return_value) do + false + end + + describe 'windows' do + let(:function_args) do + ['..\temp\test.txt'] + end + + it 'returns data' do + function.should eq(return_value) + end + end + + describe 'non-windows' do + let(:function_args) do + ['../var/lib/puppet'] + end + + it 'returns data' do + function.should eq(return_value) + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/unit/puppet/provider/file_line/ruby_spec.rb b/code/environments/production/modules/stdlib/spec/unit/puppet/provider/file_line/ruby_spec.rb new file mode 100644 index 0000000..d0f653a --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/unit/puppet/provider/file_line/ruby_spec.rb @@ -0,0 +1,258 @@ +require 'spec_helper' + +provider_class = Puppet::Type.type(:file_line).provider(:ruby) +# 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 + include PuppetlabsSpec::Files + + let :tmpfile do + tmpfilename('file_line_test') + end + let :content do + '' + end + let :params do + {} + end + let :resource do + Puppet::Type::File_line.new({ + :name => 'foo', + :path => tmpfile, + :line => 'foo', + }.merge(params)) + end + let :provider do + provider_class.new(resource) + end + + before :each do + File.open(tmpfile, 'w') do |fh| + fh.write(content) + end + end + + describe 'line parameter' do + context 'when line exists' do + let(:content) { 'foo' } + + it 'detects the line' do + expect(provider).to be_exists + end + end + context 'when line does not exist' do + let(:content) { 'foo bar' } + + it 'requests changes' do + expect(provider).not_to be_exists + end + it 'appends the line' do + provider.create + expect(File.read(tmpfile).chomp).to eq("foo bar\nfoo") + end + end + end + + describe 'match parameter' do + let(:params) { { :match => '^bar' } } + + describe 'does not match line - line does not exist - replacing' do + let(:content) { "foo bar\nbar" } + + it 'requests changes' do + expect(provider).not_to be_exists + end + it 'replaces the match' do + provider.create + expect(File.read(tmpfile).chomp).to eq("foo bar\nfoo") + end + end + + describe 'does not match line - line does not exist - appending' do + let(:params) { super().merge(:replace => false) } + let(:content) { "foo bar\nbar" } + + it 'does not request changes' do + expect(provider).to be_exists + end + end + + context 'when does not match line - line exists' do + let(:content) { "foo\nbar" } + + it 'detects the line' do + expect(provider).to be_exists + end + end + + context 'when matches line - line exists' do + let(:params) { { :match => '^foo' } } + let(:content) { "foo\nbar" } + + it 'detects the line' do + expect(provider).to be_exists + end + end + + context 'when matches line - line does not exist' do + let(:params) { { :match => '^foo' } } + let(:content) { "foo bar\nbar" } + + it 'requests changes' do + expect(provider).not_to be_exists + end + it 'replaces the match' do + provider.create + expect(File.read(tmpfile).chomp).to eq("foo\nbar") + end + end + end + + describe 'append_on_no_match' do + let(:params) do + { + :append_on_no_match => false, + :match => '^foo1$', + } + end + + context 'when matching' do + let(:content) { "foo1\nbar" } + + it 'requests changes' do + expect(provider).not_to be_exists + end + it 'replaces the match' do + provider.create + expect(File.read(tmpfile).chomp).to eql("foo\nbar") + end + end + context 'when not matching' do + let(:content) { "foo3\nbar" } + + it 'does not affect the file' do + expect(provider).to be_exists + end + end + end + + describe 'replace_all_matches_not_matching_line' do + context 'when replace is false' do + let(:params) do + { + :replace_all_matches_not_matching_line => true, + :replace => false, + } + end + + it 'raises an error' do + expect { provider.exists? }.to raise_error(Puppet::Error, %r{replace must be true}) + end + end + + context 'when match matches line - when there are more matches than lines' do + let(:params) do + { + :replace_all_matches_not_matching_line => true, + :match => '^foo', + :multiple => true, + } + end + let(:content) { "foo\nfoo bar\nbar\nfoo baz" } + + it 'requests changes' do + expect(provider).not_to be_exists + end + it 'replaces the matches' do + provider.create + expect(File.read(tmpfile).chomp).to eql("foo\nfoo\nbar\nfoo") + end + end + + context 'when match matches line - when there are the same matches and lines' do + let(:params) do + { + :replace_all_matches_not_matching_line => true, + :match => '^foo', + :multiple => true, + } + end + let(:content) { "foo\nfoo\nbar" } + + it 'does not request changes' do + expect(provider).to be_exists + end + end + + context 'when match does not match line - when there are more matches than lines' do + let(:params) do + { + :replace_all_matches_not_matching_line => true, + :match => '^bar', + :multiple => true, + } + end + let(:content) { "foo\nfoo bar\nbar\nbar baz" } + + it 'requests changes' do + expect(provider).not_to be_exists + end + it 'replaces the matches' do + provider.create + expect(File.read(tmpfile).chomp).to eql("foo\nfoo bar\nfoo\nfoo") + end + end + + context 'when match does not match line - when there are the same matches and lines' do + let(:params) do + { + :replace_all_matches_not_matching_line => true, + :match => '^bar', + :multiple => true, + } + end + let(:content) { "foo\nfoo\nbar\nbar baz" } + + it 'requests changes' do + expect(provider).not_to be_exists + end + it 'replaces the matches' do + provider.create + expect(File.read(tmpfile).chomp).to eql("foo\nfoo\nfoo\nfoo") + end + end + end + + context 'when match does not match line - when there are no matches' do + let(:params) do + { + :replace_all_matches_not_matching_line => true, + :match => '^bar', + :multiple => true, + } + end + let(:content) { "foo\nfoo bar" } + + it 'does not request changes' do + expect(provider).to be_exists + end + end + + context 'when match does not match line - when there are no matches or lines' do + let(:params) do + { + :replace_all_matches_not_matching_line => true, + :match => '^bar', + :multiple => true, + } + end + let(:content) { 'foo bar' } + + it 'requests changes' do + expect(provider).not_to be_exists + end + it 'appends the line' do + provider.create + expect(File.read(tmpfile).chomp).to eql("foo bar\nfoo") + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/unit/puppet/provider/file_line/ruby_spec_alter.rb b/code/environments/production/modules/stdlib/spec/unit/puppet/provider/file_line/ruby_spec_alter.rb new file mode 100644 index 0000000..c2b30ff --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/unit/puppet/provider/file_line/ruby_spec_alter.rb @@ -0,0 +1,374 @@ +require 'spec_helper' + +provider_class = Puppet::Type.type(:file_line).provider(:ruby) +# 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 + include PuppetlabsSpec::Files + + let :tmpfile do + tmpfilename('file_line_test') + end + let :content do + '' + end + let :params do + {} + end + let :resource do + Puppet::Type::File_line.new({ + :name => 'foo', + :path => tmpfile, + :line => 'foo', + }.merge(params)) + end + let :provider do + provider_class.new(resource) + end + + before :each do + File.open(tmpfile, 'w') do |fh| + fh.write(content) + end + end + + describe '#create' do + context 'when adding' do + pending('To be added.') + end + context 'when replacing' do + let :params do + { + :line => 'foo = bar', + :match => '^foo\s*=.*$', + :replace => false, + } + end + let(:content) { "foo1\nfoo=blah\nfoo2\nfoo3" } + + it "providor 'be_exists'" do + expect(provider).to be_exists + end + it 'does not replace the matching line' do + provider.create + expect(File.read(tmpfile).chomp).to eql("foo1\nfoo=blah\nfoo2\nfoo3") + end + it 'appends the line if no matches are found' do + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2") } + expect(provider.exists?).to be false + provider.create + expect(File.read(tmpfile).chomp).to eql("foo1\nfoo2\nfoo = bar") + end + it 'raises an error with invalid values' do + expect { + @resource = Puppet::Type::File_line.new( + :name => 'foo', :path => tmpfile, :line => 'foo = bar', :match => '^foo\s*=.*$', :replace => 'asgadga', + ) + }.to raise_error(Puppet::Error, %r{Invalid value "asgadga"\. Valid values are true, false\.}) + end + end + end + describe '#destroy' do + pending('To be added?') + end + context 'when matching' do + # rubocop:disable RSpec/InstanceVariable : replacing before with let breaks the tests, variables need to be altered within it block : multi + before :each do + @resource = Puppet::Type::File_line.new( + :name => 'foo', + :path => tmpfile, + :line => 'foo = bar', + :match => '^foo\s*=.*$', + ) + @provider = provider_class.new(@resource) + end + describe 'using match' do + it 'raises an error if more than one line matches, and should not have modified the file' do + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz") } + expect { @provider.create }.to raise_error(Puppet::Error, %r{More than one line.*matches}) + expect(File.read(tmpfile)).to eql("foo1\nfoo=blah\nfoo2\nfoo=baz") + end + + it 'replaces all lines that matches' do + @resource = Puppet::Type::File_line.new(:name => 'foo', :path => tmpfile, :line => 'foo = bar', :match => '^foo\s*=.*$', :multiple => true) + @provider = provider_class.new(@resource) + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo=blah\nfoo2\nfoo=baz") } + @provider.create + expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2\nfoo = bar") + end + + it 'replaces 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') { |fh| fh.write("\thard\tcore\t90\n\thard\tcore\t0\n") } + @provider.create + expect(File.read(tmpfile).chomp).to eql("\thard\tcore\t0\n\thard\tcore\t0") + end + + it 'raises an error with invalid values' do + expect { + @resource = Puppet::Type::File_line.new( + :name => 'foo', :path => tmpfile, :line => 'foo = bar', :match => '^foo\s*=.*$', :multiple => 'asgadga', + ) + }.to raise_error(Puppet::Error, %r{Invalid value "asgadga"\. Valid values are true, false\.}) + end + + it 'replaces a line that matches' do + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo=blah\nfoo2") } + @provider.create + expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2") + end + it 'adds a new line if no lines match' do + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2") } + @provider.create + expect(File.read(tmpfile)).to eql("foo1\nfoo2\nfoo = bar\n") + end + it 'does nothing if the exact line already exists' do + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = bar\nfoo2") } + @provider.create + expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2") + end + end + describe 'using match+append_on_no_match - when there is a match' do + it 'replaces line' 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') { |fh| fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz") } + expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = blah\nfoo2\nfoo = baz") + end + end + describe 'using match+append_on_no_match - when there is no match' do + it 'does 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') { |fh| fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz") } + expect(File.read(tmpfile).chomp).to eql("foo1\nfoo = blah\nfoo2\nfoo = baz") + end + end + end + context 'when match+replace+append_on_no_match' do + pending('to do') + end + context 'when after' do + let :resource do + Puppet::Type::File_line.new( + :name => 'foo', + :path => tmpfile, + :line => 'inserted = line', + :after => '^foo1', + ) + end + + let :provider do + provider_class.new(resource) + end + + context 'when match and after set' do + shared_context 'when resource_create' do + let(:match) { '^foo2$' } + let(:after) { '^foo1$' } + let(:resource) do + Puppet::Type::File_line.new( + :name => 'foo', + :path => tmpfile, + :line => 'inserted = line', + :after => after, + :match => match, + ) + end + end + before :each do + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2\nfoo = baz") } + end + describe 'inserts at match' do + include_context 'resource_create' + it { + provider.create + expect(File.read(tmpfile).chomp).to eq("foo1\ninserted = line\nfoo = baz") + } + end + describe 'inserts a new line after when no match' do + include_context 'resource_create' do + let(:match) { '^nevergoingtomatch$' } + end + it { + provider.create + expect(File.read(tmpfile).chomp).to eq("foo1\ninserted = line\nfoo2\nfoo = baz") + } + end + describe 'append to end of file if no match for both after and match' do + include_context 'resource_create' do + let(:match) { '^nevergoingtomatch$' } + let(:after) { '^stillneverafter' } + end + it { + provider.create + expect(File.read(tmpfile).chomp).to eq("foo1\nfoo2\nfoo = baz\ninserted = line") + } + end + end + context 'with one line matching the after expression' do + before :each do + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz") } + end + + it 'inserts the specified line after the line matching the "after" expression' do + provider.create + expect(File.read(tmpfile).chomp).to eql("foo1\ninserted = line\nfoo = blah\nfoo2\nfoo = baz") + end + end + context 'with multiple lines matching the after expression' do + before :each do + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo = blah\nfoo2\nfoo1\nfoo = baz") } + end + + it 'errors out stating "One or no line must match the pattern"' do + expect { provider.create }.to raise_error(Puppet::Error, %r{One or no line must match the pattern}) + end + + it 'adds the line after all lines matching the after expression' do + @resource = Puppet::Type::File_line.new(:name => 'foo', :path => tmpfile, :line => 'inserted = line', :after => '^foo1$', :multiple => true) + @provider = provider_class.new(@resource) + @provider.create + expect(File.read(tmpfile).chomp).to eql("foo1\ninserted = line\nfoo = blah\nfoo2\nfoo1\ninserted = line\nfoo = baz") + end + end + context 'with no lines matching the after expression' do + let :content do + "foo3\nfoo = blah\nfoo2\nfoo = baz\n" + end + + before :each do + File.open(tmpfile, 'w') { |fh| fh.write(content) } + end + + it 'appends the specified line to the file' do + provider.create + expect(File.read(tmpfile)).to eq(content << resource[:line] << "\n") + end + end + end + context 'when removing with a line' do + before :each do + # TODO: these should be ported over to use the PuppetLabs spec_helper + # file fixtures once the following pull request has been merged: + # https://github.com/puppetlabs/puppetlabs-stdlib/pull/73/files + @resource = Puppet::Type::File_line.new( + :name => 'foo', + :path => tmpfile, + :line => 'foo', + :ensure => 'absent', + ) + @provider = provider_class.new(@resource) + end + it 'removes the line if it exists' do + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") } + @provider.destroy + expect(File.read(tmpfile)).to eql("foo1\nfoo2") + end + it 'removes the line without touching the last new line' do + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2\n") } + @provider.destroy + expect(File.read(tmpfile)).to eql("foo1\nfoo2\n") + end + it 'removes any occurence of the line' do + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2\nfoo\nfoo") } + @provider.destroy + expect(File.read(tmpfile)).to eql("foo1\nfoo2\n") + end + it 'example in the docs' do + @resource = Puppet::Type::File_line.new(:name => 'bashrc_proxy', :ensure => 'absent', :path => tmpfile, :line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128') + @provider = provider_class.new(@resource) + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2\nexport HTTP_PROXY=http://squid.puppetlabs.vm:3128\nfoo4\n") } + @provider.destroy + expect(File.read(tmpfile)).to eql("foo1\nfoo2\nfoo4\n") + end + end + context 'when removing with a match' do + before :each do + @resource = Puppet::Type::File_line.new( + :name => 'foo', + :path => tmpfile, + :line => 'foo2', + :ensure => 'absent', + :match => 'o$', + :match_for_absence => true, + ) + @provider = provider_class.new(@resource) + end + + it 'finds a line to match' do + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") } + expect(@provider.exists?).to be true + end + + it 'removes one line if it matches' do + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") } + @provider.destroy + expect(File.read(tmpfile)).to eql("foo1\nfoo2") + end + + it 'the line parameter is actually not used at all but is silently ignored if here' do + @resource = Puppet::Type::File_line.new(:name => 'foo', :path => tmpfile, :line => 'supercalifragilisticexpialidocious', :ensure => 'absent', :match => 'o$', :match_for_absence => true) + @provider = provider_class.new(@resource) + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") } + @provider.destroy + expect(File.read(tmpfile)).to eql("foo1\nfoo2") + end + + it 'and may not be here and does not need to be here' do + @resource = Puppet::Type::File_line.new(:name => 'foo', :path => tmpfile, :ensure => 'absent', :match => 'o$', :match_for_absence => true) + @provider = provider_class.new(@resource) + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") } + @provider.destroy + expect(File.read(tmpfile)).to eql("foo1\nfoo2") + end + + it 'raises an error if more than one line matches' do + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2\nfoo\nfoo") } + expect { @provider.destroy }.to raise_error(Puppet::Error, %r{More than one line}) + end + + it 'removes multiple lines if :multiple is true' do + @resource = Puppet::Type::File_line.new(:name => 'foo', :path => tmpfile, :line => 'foo2', :ensure => 'absent', :match => 'o$', :multiple => true, :match_for_absence => true) + @provider = provider_class.new(@resource) + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2\nfoo\nfoo") } + @provider.destroy + expect(File.read(tmpfile)).to eql("foo1\nfoo2\n") + end + + it 'ignores the match if match_for_absence is not specified' do + @resource = Puppet::Type::File_line.new(:name => 'foo', :path => tmpfile, :line => 'foo2', :ensure => 'absent', :match => 'o$') + @provider = provider_class.new(@resource) + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") } + @provider.destroy + expect(File.read(tmpfile)).to eql("foo1\nfoo\n") + end + + it 'ignores the match if match_for_absence is false' do + @resource = Puppet::Type::File_line.new(:name => 'foo', :path => tmpfile, :line => 'foo2', :ensure => 'absent', :match => 'o$', :match_for_absence => false) + @provider = provider_class.new(@resource) + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo\nfoo2") } + @provider.destroy + expect(File.read(tmpfile)).to eql("foo1\nfoo\n") + end + + it 'example in the docs' do + @resource = Puppet::Type::File_line.new( + :name => 'bashrc_proxy', :ensure => 'absent', :path => tmpfile, :line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128', + :match => '^export\ HTTP_PROXY\=', :match_for_absence => true + ) + @provider = provider_class.new(@resource) + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2\nexport HTTP_PROXY=foo\nfoo4\n") } + @provider.destroy + expect(File.read(tmpfile)).to eql("foo1\nfoo2\nfoo4\n") + end + + it 'example in the docs showing line is redundant' do + @resource = Puppet::Type::File_line.new(:name => 'bashrc_proxy', :ensure => 'absent', :path => tmpfile, :match => '^export\ HTTP_PROXY\=', :match_for_absence => true) + @provider = provider_class.new(@resource) + File.open(tmpfile, 'w') { |fh| fh.write("foo1\nfoo2\nexport HTTP_PROXY=foo\nfoo4\n") } + @provider.destroy + expect(File.read(tmpfile)).to eql("foo1\nfoo2\nfoo4\n") + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb b/code/environments/production/modules/stdlib/spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb new file mode 100644 index 0000000..5114dec --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/unit/puppet/provider/file_line/ruby_spec_use_cases.rb @@ -0,0 +1,135 @@ +require 'spec_helper' + +provider_class = Puppet::Type.type(:file_line).provider(:ruby) +# 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 + include PuppetlabsSpec::Files + + let :tmpfile do + tmpfilename('file_line_test') + end + let :content do + '' + end + let :params do + {} + end + let :resource do + Puppet::Type::File_line.new({ + :name => 'foo', + :path => tmpfile, + :line => 'foo', + }.merge(params)) + end + let :provider do + provider_class.new(resource) + end + + before :each do + File.open(tmpfile, 'w') do |fh| + fh.write(content) + end + end + + describe 'customer use cases - no lines' do + describe 'MODULES-5003' do + let(:params) do + { + :line => "*\thard\tcore\t0", + :match => "^[ \t]*\\*[ \t]+hard[ \t]+core[ \t]+.*", + :multiple => true, + } + end + let(:content) { "* hard core 90\n* hard core 10\n" } + + it 'requests changes' do + expect(provider).not_to be_exists + end + it 'replaces the matches' do + provider.create + expect(File.read(tmpfile).chomp).to eq("* hard core 0\n* hard core 0") + end + end + + describe 'MODULES-5003 - one match, one line - just ensure the line exists' do + let(:params) do + { + :line => "*\thard\tcore\t0", + :match => "^[ \t]*\\*[ \t]+hard[ \t]+core[ \t]+.*", + :multiple => true, + } + end + let(:content) { "* hard core 90\n* hard core 0\n" } + + it 'does not request changes' do + expect(provider).to be_exists + end + end + + describe 'MODULES-5003 - one match, one line - replace all matches, even when line exists' do + let(:params) do + { + :line => "*\thard\tcore\t0", + :match => "^[ \t]*\\*[ \t]+hard[ \t]+core[ \t]+.*", + :multiple => true, + + }.merge(:replace_all_matches_not_matching_line => true) + end + let(:content) { "* hard core 90\n* hard core 0\n" } + + it 'requests changes' do + expect(provider).not_to be_exists + end + it 'replaces the matches' do + provider.create + expect(File.read(tmpfile).chomp).to eq("* hard core 0\n* hard core 0") + end + end + + describe 'MODULES-5651 - match, no line' do + let(:params) do + { + :line => 'LogLevel=notice', + :match => '^#LogLevel$', + } + end + let(:content) { "#LogLevel\nstuff" } + + it 'requests changes' do + expect(provider).not_to be_exists + end + it 'replaces the match' do + provider.create + expect(File.read(tmpfile).chomp).to eq("LogLevel=notice\nstuff") + end + end + + describe 'MODULES-5651 - match, line' do + let(:params) do + { + :line => 'LogLevel=notice', + :match => '^#LogLevel$', + } + end + let(:content) { "#Loglevel\nLogLevel=notice\nstuff" } + + it 'does not request changes' do + expect(provider).to be_exists + end + end + + describe 'MODULES-5651 - no match, line' do + let(:params) do + { + :line => 'LogLevel=notice', + :match => '^#LogLevel$', + } + end + let(:content) { "LogLevel=notice\nstuff" } + + it 'does not request changes' do + expect(provider).to be_exists + end + end + end +end diff --git a/code/environments/production/modules/stdlib/spec/unit/puppet/type/anchor_spec.rb b/code/environments/production/modules/stdlib/spec/unit/puppet/type/anchor_spec.rb new file mode 100644 index 0000000..c2d9779 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/unit/puppet/type/anchor_spec.rb @@ -0,0 +1,9 @@ +require 'spec_helper' + +anchor = Puppet::Type.type(:anchor).new(:name => 'ntp::begin') + +describe anchor do + it 'stringifies normally' do + expect(anchor.to_s).to eq('Anchor[ntp::begin]') + end +end diff --git a/code/environments/production/modules/stdlib/spec/unit/puppet/type/file_line_spec.rb b/code/environments/production/modules/stdlib/spec/unit/puppet/type/file_line_spec.rb new file mode 100644 index 0000000..627bdf0 --- /dev/null +++ b/code/environments/production/modules/stdlib/spec/unit/puppet/type/file_line_spec.rb @@ -0,0 +1,111 @@ +require 'spec_helper' +require 'tempfile' +describe Puppet::Type.type(:file_line) do + let :tmp_path do + if Puppet::Util::Platform.windows? + 'C:\tmp\path' + else + '/tmp/path' + end + end + let :my_path do + if Puppet::Util::Platform.windows? + 'C:\my\path' + else + '/my/path' + end + end + let :file_line do + Puppet::Type.type(:file_line).new(:name => 'foo', :line => 'line', :path => tmp_path) + end + + it 'accepts a line' do + file_line[:line] = 'my_line' + expect(file_line[:line]).to eq('my_line') + end + it 'accepts a path' do + file_line[:path] = my_path + expect(file_line[:path]).to eq(my_path) + end + it 'accepts a match regex' do + file_line[:match] = '^foo.*$' + expect(file_line[:match]).to eq('^foo.*$') + end + + it 'accepts a match regex that does not match the specified line' do + expect { + Puppet::Type.type(:file_line).new( + :name => 'foo', :path => my_path, :line => 'foo=bar', :match => '^bar=blah$', + ) + }.not_to raise_error + end + it 'accepts a match regex that does match the specified line' do + expect { + Puppet::Type.type(:file_line).new( + :name => 'foo', :path => my_path, :line => 'foo=bar', :match => '^\s*foo=.*$', + ) + }.not_to raise_error + end + it 'accepts utf8 characters' do + expect { + Puppet::Type.type(:file_line).new( + :name => 'ƒồỗ', :path => my_path, :line => 'ƒồỗ=ьåя', :match => '^ьåя=βļάħ$', + ) + }.not_to raise_error + end + it 'accepts double byte characters' do + expect { + Puppet::Type.type(:file_line).new( + :name => 'フーバー', :path => my_path, :line => 'この=それ', :match => '^この=ああ$', + ) + }.not_to raise_error + end + it 'accepts posix filenames' do + file_line[:path] = tmp_path + expect(file_line[:path]).to eq(tmp_path) + end + it 'does not accept unqualified path' do + expect { file_line[:path] = 'file' }.to raise_error(Puppet::Error, %r{File paths must be fully qualified}) + end + it 'requires that a line is specified' do + expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => tmp_path) }.to raise_error(Puppet::Error, %r{line is a required attribute}) + end + it 'does not require that a line is specified when matching for absence' do + expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => tmp_path, :ensure => :absent, :match_for_absence => :true, :match => 'match') }.not_to raise_error # rubocop:disable Metrics/LineLength + end + it 'although if a line is specified anyway when matching for absence it still works and the line is silently ignored' do + expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => tmp_path, :line => 'i_am_irrelevant', :ensure => :absent, :match_for_absence => :true, :match => 'match') }.not_to raise_error # rubocop:disable Metrics/LineLength + end + it 'requires that a file is specified' do + expect { Puppet::Type.type(:file_line).new(:name => 'foo', :line => 'path') }.to raise_error(Puppet::Error, %r{path is a required attribute}) + end + it 'defaults to ensure => present' do + expect(file_line[:ensure]).to eq :present + end + it 'defaults to replace => true' do + expect(file_line[:replace]).to eq :true + end + it 'defaults to encoding => UTF-8' do + expect(file_line[:encoding]).to eq 'UTF-8' + end + it 'accepts encoding => iso-8859-1' do + expect { Puppet::Type.type(:file_line).new(:name => 'foo', :path => tmp_path, :ensure => :present, :encoding => 'iso-8859-1', :line => 'bar') }.not_to raise_error + end + + it 'autorequires the file it manages' do + catalog = Puppet::Resource::Catalog.new + file = Puppet::Type.type(:file).new(:name => tmp_path) + catalog.add_resource file + catalog.add_resource file_line + relationship = file_line.autorequire.find do |rel| + (rel.source.to_s == "File[#{tmp_path}]") && (rel.target.to_s == file_line.to_s) + end + expect(relationship).to be_a Puppet::Relationship + end + + it 'does not autorequire the file it manages if it is not managed' do + catalog = Puppet::Resource::Catalog.new + catalog.add_resource file_line + expect(file_line.autorequire).to be_empty + end +end |