diff options
author | Micah Anderson <micah@riseup.net> | 2016-11-04 10:54:28 -0400 |
---|---|---|
committer | Micah Anderson <micah@riseup.net> | 2016-11-04 10:54:28 -0400 |
commit | 34a381efa8f6295080c843f86bfa07d4e41056af (patch) | |
tree | 9282cf5d4c876688602705a7fa0002bc4a810bde /puppet/modules/stdlib/spec/functions | |
parent | 0a72bc6fd292bf9367b314fcb0347c4d35042f16 (diff) | |
parent | 5821964ff7e16ca7aa9141bd09a77d355db492a9 (diff) |
Merge branch 'develop'
Diffstat (limited to 'puppet/modules/stdlib/spec/functions')
98 files changed, 4146 insertions, 0 deletions
diff --git a/puppet/modules/stdlib b/puppet/modules/stdlib deleted file mode 160000 -Subproject 71123634744b9fe2ec7d6a3e38e9789fd84801e diff --git a/puppet/modules/stdlib/spec/functions/abs_spec.rb b/puppet/modules/stdlib/spec/functions/abs_spec.rb new file mode 100755 index 00000000..3c25ce28 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/abs_spec.rb @@ -0,0 +1,25 @@ +#! /usr/bin/env ruby -S rspec + +require 'spec_helper' + +describe "the abs function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("abs")).to eq("function_abs") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_abs([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should convert a negative number into a positive" do + result = scope.function_abs(["-34"]) + expect(result).to(eq(34)) + end + + it "should do nothing with a positive number" do + result = scope.function_abs(["5678"]) + expect(result).to(eq(5678)) + end +end diff --git a/puppet/modules/stdlib/spec/functions/any2array_spec.rb b/puppet/modules/stdlib/spec/functions/any2array_spec.rb new file mode 100755 index 00000000..87cd04b5 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/any2array_spec.rb @@ -0,0 +1,55 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the any2array function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("any2array")).to eq("function_any2array") + end + + it "should return an empty array if there is less than 1 argument" do + result = scope.function_any2array([]) + expect(result).to(eq([])) + end + + it "should convert boolean true to [ true ] " do + result = scope.function_any2array([true]) + expect(result).to(eq([true])) + end + + it "should convert one object to [object]" do + result = scope.function_any2array(['one']) + expect(result).to(eq(['one'])) + end + + it "should convert multiple objects to [objects]" do + result = scope.function_any2array(['one', 'two']) + expect(result).to(eq(['one', 'two'])) + end + + it "should return empty array it was called with" do + result = scope.function_any2array([[]]) + expect(result).to(eq([])) + end + + it "should return one-member array it was called with" do + result = scope.function_any2array([['string']]) + expect(result).to(eq(['string'])) + end + + it "should return multi-member array it was called with" do + result = scope.function_any2array([['one', 'two']]) + expect(result).to(eq(['one', 'two'])) + end + + it "should return members of a hash it was called with" do + result = scope.function_any2array([{ 'key' => 'value' }]) + expect(result).to(eq(['key', 'value'])) + end + + it "should return an empty array if it was called with an empty hash" do + result = scope.function_any2array([{ }]) + expect(result).to(eq([])) + end +end diff --git a/puppet/modules/stdlib/spec/functions/base64_spec.rb b/puppet/modules/stdlib/spec/functions/base64_spec.rb new file mode 100755 index 00000000..e93fafcd --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/base64_spec.rb @@ -0,0 +1,34 @@ +#! /usr/bin/env ruby -S rspec + +require 'spec_helper' + +describe "the base64 function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("base64")).to eq("function_base64") + end + + it "should raise a ParseError if there are other than 2 arguments" do + expect { scope.function_base64([]) }.to(raise_error(Puppet::ParseError)) + expect { scope.function_base64(["asdf"]) }.to(raise_error(Puppet::ParseError)) + expect { scope.function_base64(["asdf","moo","cow"]) }.to(raise_error(Puppet::ParseError)) + end + + it "should raise a ParseError if argument 1 isn't 'encode' or 'decode'" do + expect { scope.function_base64(["bees","astring"]) }.to(raise_error(Puppet::ParseError, /first argument must be one of/)) + end + + it "should raise a ParseError if argument 2 isn't a string" do + expect { scope.function_base64(["encode",["2"]]) }.to(raise_error(Puppet::ParseError, /second argument must be a string/)) + end + + it "should encode a encoded string" do + result = scope.function_base64(["encode",'thestring']) + expect(result).to match(/\AdGhlc3RyaW5n\n\Z/) + end + it "should decode a base64 encoded string" do + result = scope.function_base64(["decode",'dGhlc3RyaW5n']) + expect(result).to eq('thestring') + end +end diff --git a/puppet/modules/stdlib/spec/functions/bool2num_spec.rb b/puppet/modules/stdlib/spec/functions/bool2num_spec.rb new file mode 100755 index 00000000..3904d7e4 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/bool2num_spec.rb @@ -0,0 +1,38 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the bool2num function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("bool2num")).to eq("function_bool2num") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_bool2num([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should convert true to 1" do + result = scope.function_bool2num([true]) + expect(result).to(eq(1)) + end + + it "should convert 'true' to 1" do + result = scope.function_bool2num(['true']) + result.should(eq(1)) + end + + it "should convert 'false' to 0" do + result = scope.function_bool2num(['false']) + expect(result).to(eq(0)) + end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new('true') + result = scope.function_bool2num([value]) + result.should(eq(1)) + end +end diff --git a/puppet/modules/stdlib/spec/functions/capitalize_spec.rb b/puppet/modules/stdlib/spec/functions/capitalize_spec.rb new file mode 100755 index 00000000..fd0e92ba --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/capitalize_spec.rb @@ -0,0 +1,28 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the capitalize function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("capitalize")).to eq("function_capitalize") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_capitalize([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should capitalize the beginning of a string" do + result = scope.function_capitalize(["abc"]) + expect(result).to(eq("Abc")) + end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new('abc') + result = scope.function_capitalize([value]) + result.should(eq('Abc')) + end +end diff --git a/puppet/modules/stdlib/spec/functions/chomp_spec.rb b/puppet/modules/stdlib/spec/functions/chomp_spec.rb new file mode 100755 index 00000000..b1e1e60f --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/chomp_spec.rb @@ -0,0 +1,28 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the chomp function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("chomp")).to eq("function_chomp") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_chomp([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should chomp the end of a string" do + result = scope.function_chomp(["abc\n"]) + expect(result).to(eq("abc")) + end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new("abc\n") + result = scope.function_chomp([value]) + result.should(eq("abc")) + end +end diff --git a/puppet/modules/stdlib/spec/functions/chop_spec.rb b/puppet/modules/stdlib/spec/functions/chop_spec.rb new file mode 100755 index 00000000..c8a19519 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/chop_spec.rb @@ -0,0 +1,28 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the chop function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("chop")).to eq("function_chop") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_chop([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should chop the end of a string" do + result = scope.function_chop(["asdf\n"]) + expect(result).to(eq("asdf")) + end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new("abc\n") + result = scope.function_chop([value]) + result.should(eq('abc')) + end +end diff --git a/puppet/modules/stdlib/spec/functions/concat_spec.rb b/puppet/modules/stdlib/spec/functions/concat_spec.rb new file mode 100755 index 00000000..49fa6bb3 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/concat_spec.rb @@ -0,0 +1,50 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the concat function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should raise a ParseError if the client does not provide at least two arguments" do + expect { scope.function_concat([]) }.to(raise_error(Puppet::ParseError)) + expect { scope.function_concat([[1]]) }.to(raise_error(Puppet::ParseError)) + end + + it "should raise a ParseError if the first parameter is not an array" do + expect { scope.function_concat([1, []])}.to(raise_error(Puppet::ParseError)) + end + + it "should not raise a ParseError if the client provides more than two arguments" do + expect { scope.function_concat([[1],[2],[3]]) }.not_to raise_error + end + + it "should be able to concat an array" do + result = scope.function_concat([['1','2','3'],['4','5','6']]) + expect(result).to(eq(['1','2','3','4','5','6'])) + end + + it "should be able to concat a primitive to an array" do + result = scope.function_concat([['1','2','3'],'4']) + expect(result).to(eq(['1','2','3','4'])) + end + + it "should not accidentally flatten nested arrays" do + result = scope.function_concat([['1','2','3'],[['4','5'],'6']]) + expect(result).to(eq(['1','2','3',['4','5'],'6'])) + end + + it "should leave the original array intact" do + array_original = ['1','2','3'] + result = scope.function_concat([array_original,['4','5','6']]) + array_original.should(eq(['1','2','3'])) + end + + it "should be able to concat multiple arrays" do + result = scope.function_concat([['1','2','3'],['4','5','6'],['7','8','9']]) + expect(result).to(eq(['1','2','3','4','5','6','7','8','9'])) + end + + it "should be able to concat mix of primitives and arrays to a final array" do + result = scope.function_concat([['1','2','3'],'4',['5','6','7']]) + expect(result).to(eq(['1','2','3','4','5','6','7'])) + end +end diff --git a/puppet/modules/stdlib/spec/functions/count_spec.rb b/puppet/modules/stdlib/spec/functions/count_spec.rb new file mode 100755 index 00000000..f8f1d484 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/count_spec.rb @@ -0,0 +1,31 @@ +#! /usr/bin/env ruby -S rspec + +require 'spec_helper' + +describe "the count function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("count")).to eq("function_count") + end + + it "should raise a ArgumentError if there is more than 2 arguments" do + expect { scope.function_count(['foo', 'bar', 'baz']) }.to( raise_error(ArgumentError)) + end + + it "should be able to count arrays" do + expect(scope.function_count([["1","2","3"]])).to(eq(3)) + end + + it "should be able to count matching elements in arrays" do + expect(scope.function_count([["1", "2", "2"], "2"])).to(eq(2)) + end + + it "should not count nil or empty strings" do + expect(scope.function_count([["foo","bar",nil,""]])).to(eq(2)) + end + + it 'does not count an undefined hash key or an out of bound array index (which are both :undef)' do + expect(scope.function_count([["foo",:undef,:undef]])).to eq(1) + end +end diff --git a/puppet/modules/stdlib/spec/functions/deep_merge_spec.rb b/puppet/modules/stdlib/spec/functions/deep_merge_spec.rb new file mode 100755 index 00000000..7087904a --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/deep_merge_spec.rb @@ -0,0 +1,105 @@ +#! /usr/bin/env ruby -S rspec + +require 'spec_helper' + +describe Puppet::Parser::Functions.function(:deep_merge) do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + describe 'when calling deep_merge from puppet' do + it "should not compile when no arguments are passed" do + skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ + Puppet[:code] = '$x = deep_merge()' + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /wrong number of arguments/) + end + + it "should not compile when 1 argument is passed" do + skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ + Puppet[:code] = "$my_hash={'one' => 1}\n$x = deep_merge($my_hash)" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /wrong number of arguments/) + end + end + + describe 'when calling deep_merge on the scope instance' do + it 'should require all parameters are hashes' do + expect { new_hash = scope.function_deep_merge([{}, '2'])}.to raise_error(Puppet::ParseError, /unexpected argument type String/) + expect { new_hash = scope.function_deep_merge([{}, 2])}.to raise_error(Puppet::ParseError, /unexpected argument type Fixnum/) + end + + it 'should accept empty strings as puppet undef' do + expect { new_hash = scope.function_deep_merge([{}, ''])}.not_to raise_error + end + + it 'should be able to deep_merge two hashes' do + new_hash = scope.function_deep_merge([{'one' => '1', 'two' => '1'}, {'two' => '2', 'three' => '2'}]) + expect(new_hash['one']).to eq('1') + expect(new_hash['two']).to eq('2') + expect(new_hash['three']).to eq('2') + end + + it 'should deep_merge multiple hashes' do + hash = scope.function_deep_merge([{'one' => 1}, {'one' => '2'}, {'one' => '3'}]) + expect(hash['one']).to eq('3') + end + + it 'should accept empty hashes' do + expect(scope.function_deep_merge([{},{},{}])).to eq({}) + end + + it 'should deep_merge subhashes' do + hash = scope.function_deep_merge([{'one' => 1}, {'two' => 2, 'three' => { 'four' => 4 } }]) + expect(hash['one']).to eq(1) + expect(hash['two']).to eq(2) + expect(hash['three']).to eq({ 'four' => 4 }) + end + + it 'should append to subhashes' do + hash = scope.function_deep_merge([{'one' => { 'two' => 2 } }, { 'one' => { 'three' => 3 } }]) + expect(hash['one']).to eq({ 'two' => 2, 'three' => 3 }) + end + + it 'should append to subhashes 2' do + hash = scope.function_deep_merge([{'one' => 1, 'two' => 2, 'three' => { 'four' => 4 } }, {'two' => 'dos', 'three' => { 'five' => 5 } }]) + expect(hash['one']).to eq(1) + expect(hash['two']).to eq('dos') + expect(hash['three']).to eq({ 'four' => 4, 'five' => 5 }) + end + + it 'should append to subhashes 3' do + hash = scope.function_deep_merge([{ 'key1' => { 'a' => 1, 'b' => 2 }, 'key2' => { 'c' => 3 } }, { 'key1' => { 'b' => 99 } }]) + expect(hash['key1']).to eq({ 'a' => 1, 'b' => 99 }) + expect(hash['key2']).to eq({ 'c' => 3 }) + end + + it 'should not change the original hashes' do + hash1 = {'one' => { 'two' => 2 } } + hash2 = { 'one' => { 'three' => 3 } } + hash = scope.function_deep_merge([hash1, hash2]) + expect(hash1).to eq({'one' => { 'two' => 2 } }) + expect(hash2).to eq({ 'one' => { 'three' => 3 } }) + expect(hash['one']).to eq({ 'two' => 2, 'three' => 3 }) + end + + it 'should not change the original hashes 2' do + hash1 = {'one' => { 'two' => [1,2] } } + hash2 = { 'one' => { 'three' => 3 } } + hash = scope.function_deep_merge([hash1, hash2]) + expect(hash1).to eq({'one' => { 'two' => [1,2] } }) + expect(hash2).to eq({ 'one' => { 'three' => 3 } }) + expect(hash['one']).to eq({ 'two' => [1,2], 'three' => 3 }) + end + + it 'should not change the original hashes 3' do + hash1 = {'one' => { 'two' => [1,2, {'two' => 2} ] } } + hash2 = { 'one' => { 'three' => 3 } } + hash = scope.function_deep_merge([hash1, hash2]) + expect(hash1).to eq({'one' => { 'two' => [1,2, {'two' => 2}] } }) + expect(hash2).to eq({ 'one' => { 'three' => 3 } }) + expect(hash['one']).to eq({ 'two' => [1,2, {'two' => 2} ], 'three' => 3 }) + expect(hash['one']['two']).to eq([1,2, {'two' => 2}]) + end + end +end diff --git a/puppet/modules/stdlib/spec/functions/defined_with_params_spec.rb b/puppet/modules/stdlib/spec/functions/defined_with_params_spec.rb new file mode 100755 index 00000000..35903047 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/defined_with_params_spec.rb @@ -0,0 +1,37 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +require 'rspec-puppet' +describe 'defined_with_params' do + describe 'when a resource is not 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 do + is_expected.to run.with_params('User[dan]', {}).and_return(true) + is_expected.to run.with_params('User[bob]', {}).and_return(false) + is_expected.to run.with_params('User[dan]', {'foo' => 'bar'}).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 do + is_expected.to run.with_params('User[dan]', {}).and_return(true) + is_expected.to run.with_params('User[dan]', '').and_return(true) + is_expected.to run.with_params('User[dan]', {'ensure' => 'present'} + ).and_return(true) + is_expected.to run.with_params('User[dan]', + {'ensure' => 'present', 'managehome' => false} + ).and_return(true) + is_expected.to run.with_params('User[dan]', + {'ensure' => 'absent', 'managehome' => false} + ).and_return(false) + end + end +end diff --git a/puppet/modules/stdlib/spec/functions/delete_at_spec.rb b/puppet/modules/stdlib/spec/functions/delete_at_spec.rb new file mode 100755 index 00000000..7c20aec4 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/delete_at_spec.rb @@ -0,0 +1,25 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the delete_at function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("delete_at")).to eq("function_delete_at") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_delete_at([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should delete an item at specified location from an array" do + result = scope.function_delete_at([['a','b','c'],1]) + expect(result).to(eq(['a','c'])) + end + + it "should not change origin array passed as argument" do + origin_array = ['a','b','c','d'] + result = scope.function_delete_at([origin_array, 1]) + expect(origin_array).to(eq(['a','b','c','d'])) + end +end diff --git a/puppet/modules/stdlib/spec/functions/delete_spec.rb b/puppet/modules/stdlib/spec/functions/delete_spec.rb new file mode 100755 index 00000000..c8edd78e --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/delete_spec.rb @@ -0,0 +1,61 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the delete function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("delete")).to eq("function_delete") + end + + it "should raise a ParseError if there are fewer than 2 arguments" do + expect { scope.function_delete([]) }.to(raise_error(Puppet::ParseError)) + end + + it "should raise a ParseError if there are greater than 2 arguments" do + expect { scope.function_delete([[], 'foo', 'bar']) }.to(raise_error(Puppet::ParseError)) + end + + it "should raise a TypeError if a number is passed as the first argument" do + expect { scope.function_delete([1, 'bar']) }.to(raise_error(TypeError)) + end + + it "should delete all instances of an element from an array" do + result = scope.function_delete([['a', 'b', 'c', 'b'], 'b']) + expect(result).to(eq(['a', 'c'])) + end + + it "should delete all instances of a substring from a string" do + result = scope.function_delete(['foobarbabarz', 'bar']) + expect(result).to(eq('foobaz')) + end + + it "should delete a key from a hash" do + result = scope.function_delete([{'a' => 1, 'b' => 2, 'c' => 3}, 'b']) + expect(result).to(eq({'a' => 1, 'c' => 3})) + end + + it 'should accept an array of items to delete' do + result = scope.function_delete([{'a' => 1, 'b' => 2, 'c' => 3}, ['b', 'c']]) + expect(result).to(eq({'a' => 1})) + end + + it "should not change origin array passed as argument" do + origin_array = ['a', 'b', 'c', 'd'] + result = scope.function_delete([origin_array, 'b']) + expect(origin_array).to(eq(['a', 'b', 'c', 'd'])) + end + + it "should not change the origin string passed as argument" do + origin_string = 'foobarbabarz' + result = scope.function_delete([origin_string, 'bar']) + expect(origin_string).to(eq('foobarbabarz')) + end + + it "should not change origin hash passed as argument" do + origin_hash = {'a' => 1, 'b' => 2, 'c' => 3} + result = scope.function_delete([origin_hash, 'b']) + expect(origin_hash).to(eq({'a' => 1, 'b' => 2, 'c' => 3})) + end + +end diff --git a/puppet/modules/stdlib/spec/functions/delete_undef_values_spec.rb b/puppet/modules/stdlib/spec/functions/delete_undef_values_spec.rb new file mode 100755 index 00000000..dc679535 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/delete_undef_values_spec.rb @@ -0,0 +1,41 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the delete_undef_values function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("delete_undef_values")).to eq("function_delete_undef_values") + end + + it "should raise a ParseError if there is less than 1 argument" do + expect { scope.function_delete_undef_values([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should raise a ParseError if the argument is not Array nor Hash" do + expect { scope.function_delete_undef_values(['']) }.to( raise_error(Puppet::ParseError)) + expect { scope.function_delete_undef_values([nil]) }.to( raise_error(Puppet::ParseError)) + end + + it "should delete all undef items from Array and only these" do + result = scope.function_delete_undef_values([['a',:undef,'c','undef']]) + expect(result).to(eq(['a','c','undef'])) + end + + it "should delete all undef items from Hash and only these" do + result = scope.function_delete_undef_values([{'a'=>'A','b'=>:undef,'c'=>'C','d'=>'undef'}]) + expect(result).to(eq({'a'=>'A','c'=>'C','d'=>'undef'})) + end + + it "should not change origin array passed as argument" do + origin_array = ['a',:undef,'c','undef'] + result = scope.function_delete_undef_values([origin_array]) + expect(origin_array).to(eq(['a',:undef,'c','undef'])) + end + + it "should not change origin hash passed as argument" do + origin_hash = { 'a' => 1, 'b' => :undef, 'c' => 'undef' } + result = scope.function_delete_undef_values([origin_hash]) + expect(origin_hash).to(eq({ 'a' => 1, 'b' => :undef, 'c' => 'undef' })) + end +end diff --git a/puppet/modules/stdlib/spec/functions/delete_values_spec.rb b/puppet/modules/stdlib/spec/functions/delete_values_spec.rb new file mode 100755 index 00000000..4f4d411b --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/delete_values_spec.rb @@ -0,0 +1,36 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the delete_values function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("delete_values")).to eq("function_delete_values") + end + + it "should raise a ParseError if there are fewer than 2 arguments" do + expect { scope.function_delete_values([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should raise a ParseError if there are greater than 2 arguments" do + expect { scope.function_delete_values([[], 'foo', 'bar']) }.to( raise_error(Puppet::ParseError)) + end + + it "should raise a TypeError if the argument is not a hash" do + expect { scope.function_delete_values([1,'bar']) }.to( raise_error(TypeError)) + expect { scope.function_delete_values(['foo','bar']) }.to( raise_error(TypeError)) + expect { scope.function_delete_values([[],'bar']) }.to( raise_error(TypeError)) + end + + it "should delete all instances of a value from a hash" do + result = scope.function_delete_values([{ 'a'=>'A', 'b'=>'B', 'B'=>'C', 'd'=>'B' },'B']) + expect(result).to(eq({ 'a'=>'A', 'B'=>'C' })) + end + + it "should not change origin hash passed as argument" do + origin_hash = { 'a' => 1, 'b' => 2, 'c' => 3 } + result = scope.function_delete_values([origin_hash, 2]) + expect(origin_hash).to(eq({ 'a' => 1, 'b' => 2, 'c' => 3 })) + end + +end diff --git a/puppet/modules/stdlib/spec/functions/difference_spec.rb b/puppet/modules/stdlib/spec/functions/difference_spec.rb new file mode 100755 index 00000000..24b2b1bc --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/difference_spec.rb @@ -0,0 +1,19 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the difference function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("difference")).to eq("function_difference") + end + + it "should raise a ParseError if there are fewer than 2 arguments" do + expect { scope.function_difference([]) }.to( raise_error(Puppet::ParseError) ) + end + + it "should return the difference between two arrays" do + result = scope.function_difference([["a","b","c"],["b","c","d"]]) + expect(result).to(eq(["a"])) + end +end diff --git a/puppet/modules/stdlib/spec/functions/dirname_spec.rb b/puppet/modules/stdlib/spec/functions/dirname_spec.rb new file mode 100755 index 00000000..8a3bcabf --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/dirname_spec.rb @@ -0,0 +1,24 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the dirname function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("dirname")).to eq("function_dirname") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_dirname([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should return dirname for an absolute path" do + result = scope.function_dirname(['/path/to/a/file.ext']) + expect(result).to(eq('/path/to/a')) + end + + it "should return dirname for a relative path" do + result = scope.function_dirname(['path/to/a/file.ext']) + expect(result).to(eq('path/to/a')) + end +end diff --git a/puppet/modules/stdlib/spec/functions/downcase_spec.rb b/puppet/modules/stdlib/spec/functions/downcase_spec.rb new file mode 100755 index 00000000..edebc44f --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/downcase_spec.rb @@ -0,0 +1,33 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the downcase function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("downcase")).to eq("function_downcase") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_downcase([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should downcase a string" do + result = scope.function_downcase(["ASFD"]) + expect(result).to(eq("asfd")) + end + + it "should do nothing to a string that is already downcase" do + result = scope.function_downcase(["asdf asdf"]) + expect(result).to(eq("asdf asdf")) + end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new("ASFD") + result = scope.function_downcase([value]) + result.should(eq('asfd')) + end +end diff --git a/puppet/modules/stdlib/spec/functions/empty_spec.rb b/puppet/modules/stdlib/spec/functions/empty_spec.rb new file mode 100755 index 00000000..6a97c4cd --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/empty_spec.rb @@ -0,0 +1,32 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the empty function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + it "should exist" do + expect(Puppet::Parser::Functions.function("empty")).to eq("function_empty") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_empty([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should return a true for an empty string" do + result = scope.function_empty(['']) + expect(result).to(eq(true)) + end + + it "should return a false for a non-empty string" do + result = scope.function_empty(['asdf']) + expect(result).to(eq(false)) + end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new() + result = scope.function_empty([value]) + result.should(eq(true)) + end +end diff --git a/puppet/modules/stdlib/spec/functions/ensure_packages_spec.rb b/puppet/modules/stdlib/spec/functions/ensure_packages_spec.rb new file mode 100755 index 00000000..436be10b --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/ensure_packages_spec.rb @@ -0,0 +1,81 @@ +#! /usr/bin/env ruby + +require 'spec_helper' +require 'rspec-puppet' +require 'puppet_spec/compiler' + +describe 'ensure_packages' do + include PuppetSpec::Compiler + + before :each do + Puppet::Parser::Functions.autoloader.loadall + Puppet::Parser::Functions.function(:ensure_packages) + Puppet::Parser::Functions.function(:ensure_resource) + Puppet::Parser::Functions.function(:defined_with_params) + Puppet::Parser::Functions.function(:create_resources) + end + + let :node do Puppet::Node.new('localhost') end + let :compiler do Puppet::Parser::Compiler.new(node) end + let :scope do + if Puppet.version.to_f >= 3.0 + Puppet::Parser::Scope.new(compiler) + else + newscope = Puppet::Parser::Scope.new + newscope.compiler = compiler + newscope.source = Puppet::Resource::Type.new(:node, :localhost) + newscope + end + end + + describe 'argument handling' do + it 'fails with no arguments' do + expect { + scope.function_ensure_packages([]) + }.to raise_error(Puppet::ParseError, /0 for 1 or 2/) + end + + it 'accepts an array of values' do + scope.function_ensure_packages([['foo']]) + end + + it 'accepts a single package name as a string' do + scope.function_ensure_packages(['foo']) + end + end + + context 'given a catalog with puppet package => absent' do + let :catalog do + compile_to_catalog(<<-EOS + ensure_packages(['facter']) + package { puppet: ensure => absent } + EOS + ) + end + + it 'has no effect on Package[puppet]' do + expect(catalog.resource(:package, 'puppet')['ensure']).to eq('absent') + end + end + + context 'given a clean catalog' do + let :catalog do + compile_to_catalog('ensure_packages(["facter"])') + end + + it 'declares package resources with ensure => present' do + expect(catalog.resource(:package, 'facter')['ensure']).to eq('present') + end + end + + context 'given a clean catalog and specified defaults' do + let :catalog do + compile_to_catalog('ensure_packages(["facter"], {"provider" => "gem"})') + end + + it 'declares package resources with ensure => present' do + expect(catalog.resource(:package, 'facter')['ensure']).to eq('present') + expect(catalog.resource(:package, 'facter')['provider']).to eq('gem') + end + end +end diff --git a/puppet/modules/stdlib/spec/functions/ensure_resource_spec.rb b/puppet/modules/stdlib/spec/functions/ensure_resource_spec.rb new file mode 100755 index 00000000..33bcac0d --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/ensure_resource_spec.rb @@ -0,0 +1,113 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' +require 'rspec-puppet' +require 'puppet_spec/compiler' + +describe 'ensure_resource' do + include PuppetSpec::Compiler + + before :all do + Puppet::Parser::Functions.autoloader.loadall + Puppet::Parser::Functions.function(:ensure_packages) + end + + let :node do Puppet::Node.new('localhost') end + let :compiler do Puppet::Parser::Compiler.new(node) end + let :scope do Puppet::Parser::Scope.new(compiler) end + + describe 'when a type or title is not specified' do + it { expect { scope.function_ensure_resource([]) }.to raise_error } + it { expect { scope.function_ensure_resource(['type']) }.to raise_error } + end + + describe 'when compared against a resource with no attributes' do + let :catalog do + compile_to_catalog(<<-EOS + user { "dan": } + ensure_resource('user', 'dan', {}) + EOS + ) + end + + it 'should contain the the ensured resources' do + expect(catalog.resource(:user, 'dan').to_s).to eq('User[dan]') + end + end + + describe 'works when compared against a resource with non-conflicting attributes' do + [ + "ensure_resource('User', 'dan', {})", + "ensure_resource('User', 'dan', '')", + "ensure_resource('User', 'dan', {'ensure' => 'present'})", + "ensure_resource('User', 'dan', {'ensure' => 'present', 'managehome' => false})" + ].each do |ensure_resource| + pp = <<-EOS + user { "dan": ensure => present, shell => "/bin/csh", managehome => false} + #{ensure_resource} + EOS + + it { expect { compile_to_catalog(pp) }.to_not raise_error } + end + end + + describe 'fails when compared against a resource with conflicting attributes' do + pp = <<-EOS + user { "dan": ensure => present, shell => "/bin/csh", managehome => false} + ensure_resource('User', 'dan', {'ensure' => 'absent', 'managehome' => false}) + EOS + + it { expect { compile_to_catalog(pp) }.to raise_error } + end + + describe 'when an array of new resources are passed in' do + let :catalog do + compile_to_catalog("ensure_resource('User', ['dan', 'alex'], {})") + end + + it 'should contain the ensured resources' do + expect(catalog.resource('User[dan]').to_s).to eq('User[dan]') + expect(catalog.resource('User[alex]').to_s).to eq('User[alex]') + end + end + + describe 'when an array of existing resources is compared against existing resources' do + pp = <<-EOS + user { 'dan': ensure => present; 'alex': ensure => present } + ensure_resource('User', ['dan', 'alex'], {}) + EOS + + let :catalog do + compile_to_catalog(pp) + end + + it 'should return the existing resources' do + expect(catalog.resource('User[dan]').to_s).to eq('User[dan]') + expect(catalog.resource('User[alex]').to_s).to eq('User[alex]') + end + end + + describe 'works when compared against existing resources with attributes' do + [ + "ensure_resource('User', ['dan', 'alex'], {})", + "ensure_resource('User', ['dan', 'alex'], '')", + "ensure_resource('User', ['dan', 'alex'], {'ensure' => 'present'})", + ].each do |ensure_resource| + pp = <<-EOS + user { 'dan': ensure => present; 'alex': ensure => present } + #{ensure_resource} + EOS + + it { expect { compile_to_catalog(pp) }.to_not raise_error } + end + end + + describe 'fails when compared against existing resources with conflicting attributes' do + pp = <<-EOS + user { 'dan': ensure => present; 'alex': ensure => present } + ensure_resource('User', ['dan', 'alex'], {'ensure' => 'absent'}) + EOS + + it { expect { compile_to_catalog(pp) }.to raise_error } + end + +end diff --git a/puppet/modules/stdlib/spec/functions/flatten_spec.rb b/puppet/modules/stdlib/spec/functions/flatten_spec.rb new file mode 100755 index 00000000..de8c66d6 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/flatten_spec.rb @@ -0,0 +1,27 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the flatten function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + it "should exist" do + expect(Puppet::Parser::Functions.function("flatten")).to eq("function_flatten") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_flatten([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should raise a ParseError if there is more than 1 argument" do + expect { scope.function_flatten([[], []]) }.to( raise_error(Puppet::ParseError)) + end + + it "should flatten a complex data structure" do + result = scope.function_flatten([["a","b",["c",["d","e"],"f","g"]]]) + expect(result).to(eq(["a","b","c","d","e","f","g"])) + end + + it "should do nothing to a structure that is already flat" do + result = scope.function_flatten([["a","b","c","d"]]) + expect(result).to(eq(["a","b","c","d"])) + end +end diff --git a/puppet/modules/stdlib/spec/functions/floor_spec.rb b/puppet/modules/stdlib/spec/functions/floor_spec.rb new file mode 100755 index 00000000..12a69179 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/floor_spec.rb @@ -0,0 +1,39 @@ +#! /usr/bin/env ruby -S rspec + +require 'spec_helper' + +describe "the floor function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("floor")).to eq("function_floor") + end + + it "should raise a ParseError if there is less than 1 argument" do + expect { scope.function_floor([]) }.to( raise_error(Puppet::ParseError, /Wrong number of arguments/)) + end + + it "should should raise a ParseError if input isn't numeric (eg. String)" do + expect { scope.function_floor(["foo"]) }.to( raise_error(Puppet::ParseError, /Wrong argument type/)) + end + + it "should should raise a ParseError if input isn't numeric (eg. Boolean)" do + expect { scope.function_floor([true]) }.to( raise_error(Puppet::ParseError, /Wrong argument type/)) + end + + it "should return an integer when a numeric type is passed" do + result = scope.function_floor([12.4]) + expect(result.is_a?(Integer)).to(eq(true)) + end + + it "should return the input when an integer is passed" do + result = scope.function_floor([7]) + expect(result).to(eq(7)) + end + + it "should return the largest integer less than or equal to the input" do + result = scope.function_floor([3.8]) + expect(result).to(eq(3)) + end +end + diff --git a/puppet/modules/stdlib/spec/functions/fqdn_rotate_spec.rb b/puppet/modules/stdlib/spec/functions/fqdn_rotate_spec.rb new file mode 100755 index 00000000..40057d4f --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/fqdn_rotate_spec.rb @@ -0,0 +1,43 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the fqdn_rotate function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("fqdn_rotate")).to eq("function_fqdn_rotate") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_fqdn_rotate([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should rotate a string and the result should be the same size" do + scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1") + result = scope.function_fqdn_rotate(["asdf"]) + expect(result.size).to(eq(4)) + end + + it "should rotate a string to give the same results for one host" do + scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1").twice + expect(scope.function_fqdn_rotate(["abcdefg"])).to eql(scope.function_fqdn_rotate(["abcdefg"])) + end + + it "should rotate a string to give different values on different hosts" do + scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1") + val1 = scope.function_fqdn_rotate(["abcdefghijklmnopqrstuvwxyz01234567890987654321"]) + scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.2") + val2 = scope.function_fqdn_rotate(["abcdefghijklmnopqrstuvwxyz01234567890987654321"]) + expect(val1).not_to eql(val2) + end + + it "should accept objects which extend String" do + class AlsoString < String + end + + scope.expects(:lookupvar).with("::fqdn").returns("127.0.0.1") + value = AlsoString.new("asdf") + result = scope.function_fqdn_rotate([value]) + result.size.should(eq(4)) + end +end diff --git a/puppet/modules/stdlib/spec/functions/get_module_path_spec.rb b/puppet/modules/stdlib/spec/functions/get_module_path_spec.rb new file mode 100755 index 00000000..38ce6459 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/get_module_path_spec.rb @@ -0,0 +1,46 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe Puppet::Parser::Functions.function(:get_module_path) do + Internals = PuppetlabsSpec::PuppetInternals + class StubModule + attr_reader :path + def initialize(path) + @path = path + end + end + + def scope(environment = "production") + Internals.scope(:compiler => Internals.compiler(:node => Internals.node(:environment => environment))) + end + + it 'should only allow one argument' do + expect { scope.function_get_module_path([]) }.to raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/) + expect { scope.function_get_module_path(['1','2','3']) }.to raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/) + end + it 'should raise an exception when the module cannot be found' do + expect { scope.function_get_module_path(['foo']) }.to raise_error(Puppet::ParseError, /Could not find module/) + 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 } + + it 'should be able to find module paths from the modulepath setting' do + Puppet::Module.expects(:find).with('foo', 'production').returns(path_of_module_foo) + expect(scope.function_get_module_path(['foo'])).to eq(path_of_module_foo.path) + end + it 'should be able to find module paths when the modulepath is a list' do + Puppet[:modulepath] = modulepath + ":/tmp" + Puppet::Module.expects(:find).with('foo', 'production').returns(path_of_module_foo) + expect(scope.function_get_module_path(['foo'])).to eq(path_of_module_foo.path) + end + it 'should respect the environment' do + skip("Disabled on Puppet 2.6.x") if Puppet.version =~ /^2\.6\b/ + Puppet.settings[:environment] = 'danstestenv' + Puppet::Module.expects(:find).with('foo', 'danstestenv').returns(path_of_module_foo) + expect(scope('danstestenv').function_get_module_path(['foo'])).to eq(path_of_module_foo.path) + end + end +end diff --git a/puppet/modules/stdlib/spec/functions/getparam_spec.rb b/puppet/modules/stdlib/spec/functions/getparam_spec.rb new file mode 100755 index 00000000..833c4d4f --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/getparam_spec.rb @@ -0,0 +1,76 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' +require 'rspec-puppet' +require 'puppet_spec/compiler' + +describe 'getparam' do + include PuppetSpec::Compiler + + before :each do + Puppet::Parser::Functions.autoloader.loadall + Puppet::Parser::Functions.function(:getparam) + end + + let :node do Puppet::Node.new('localhost') end + let :compiler do Puppet::Parser::Compiler.new(node) end + if Puppet.version.to_f >= 3.0 + let :scope do Puppet::Parser::Scope.new(compiler) end + else + let :scope do + newscope = Puppet::Parser::Scope.new + newscope.compiler = compiler + newscope.source = Puppet::Resource::Type.new(:node, :localhost) + newscope + end + end + + it "should exist" do + expect(Puppet::Parser::Functions.function("getparam")).to eq("function_getparam") + end + + describe 'when a resource is not specified' do + it { expect { scope.function_getparam([]) }.to raise_error } + it { expect { scope.function_getparam(['User[dan]']) }.to raise_error } + it { expect { scope.function_getparam(['User[dan]']) }.to raise_error } + it { expect { scope.function_getparam(['User[dan]', {}]) }.to raise_error } + # This seems to be OK because we just check for a string. + it { expect { scope.function_getparam(['User[dan]', '']) }.to_not raise_error } + end + + describe 'when compared against a resource with no params' do + let :catalog do + compile_to_catalog(<<-EOS + user { "dan": } + EOS + ) + end + + it do + expect(scope.function_getparam(['User[dan]', 'shell'])).to eq('') + end + end + + describe 'when compared against a resource with params' do + let :catalog do + compile_to_catalog(<<-EOS + user { 'dan': ensure => present, shell => '/bin/sh', managehome => false} + $test = getparam(User[dan], 'shell') + EOS + ) + end + + it do + resource = Puppet::Parser::Resource.new(:user, 'dan', {:scope => scope}) + resource.set_parameter('ensure', 'present') + resource.set_parameter('shell', '/bin/sh') + resource.set_parameter('managehome', false) + compiler.add_resource(scope, resource) + + expect(scope.function_getparam(['User[dan]', 'shell'])).to eq('/bin/sh') + expect(scope.function_getparam(['User[dan]', ''])).to eq('') + expect(scope.function_getparam(['User[dan]', 'ensure'])).to eq('present') + # TODO: Expected this to be false, figure out why we're getting '' back. + expect(scope.function_getparam(['User[dan]', 'managehome'])).to eq('') + end + end +end diff --git a/puppet/modules/stdlib/spec/functions/getvar_spec.rb b/puppet/modules/stdlib/spec/functions/getvar_spec.rb new file mode 100755 index 00000000..87ab9b5a --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/getvar_spec.rb @@ -0,0 +1,37 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe Puppet::Parser::Functions.function(:getvar) do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + describe 'when calling getvar from puppet' do + + it "should not compile when no arguments are passed" do + skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ + Puppet[:code] = '$foo = getvar()' + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /wrong number of arguments/) + end + + it "should not compile when too many arguments are passed" do + skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ + Puppet[:code] = '$foo = getvar("foo::bar", "baz")' + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /wrong number of arguments/) + end + + it "should lookup variables in other namespaces" do + skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ + Puppet[:code] = <<-'ENDofPUPPETcode' + class site::data { $foo = 'baz' } + include site::data + $foo = getvar("site::data::foo") + if $foo != 'baz' { + fail('getvar did not return what we expect') + } + ENDofPUPPETcode + scope.compiler.compile + end + end +end diff --git a/puppet/modules/stdlib/spec/functions/grep_spec.rb b/puppet/modules/stdlib/spec/functions/grep_spec.rb new file mode 100755 index 00000000..9c671dd8 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/grep_spec.rb @@ -0,0 +1,19 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the grep function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("grep")).to eq("function_grep") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_grep([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should grep contents from an array" do + result = scope.function_grep([["aaabbb","bbbccc","dddeee"], "bbb"]) + expect(result).to(eq(["aaabbb","bbbccc"])) + end +end diff --git a/puppet/modules/stdlib/spec/functions/has_interface_with_spec.rb b/puppet/modules/stdlib/spec/functions/has_interface_with_spec.rb new file mode 100755 index 00000000..23e09a95 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/has_interface_with_spec.rb @@ -0,0 +1,64 @@ +#!/usr/bin/env ruby -S rspec +require 'spec_helper' + +describe Puppet::Parser::Functions.function(:has_interface_with) do + + let(:scope) do + PuppetlabsSpec::PuppetInternals.scope + end + + # The subject of these examples is the method itself. + subject do + function_name = Puppet::Parser::Functions.function(:has_interface_with) + scope.method(function_name) + end + + # We need to mock out the Facts so we can specify how we expect this function + # to behave on different platforms. + context "On Mac OS X Systems" do + before :each do + scope.stubs(:lookupvar).with("interfaces").returns('lo0,gif0,stf0,en1,p2p0,fw0,en0,vmnet1,vmnet8,utun0') + end + it 'should have loopback (lo0)' do + expect(subject.call(['lo0'])).to be_truthy + end + it 'should not have loopback (lo)' do + expect(subject.call(['lo'])).to be_falsey + end + end + context "On Linux Systems" do + before :each do + scope.stubs(:lookupvar).with("interfaces").returns('eth0,lo') + scope.stubs(:lookupvar).with("ipaddress").returns('10.0.0.1') + scope.stubs(:lookupvar).with("ipaddress_lo").returns('127.0.0.1') + scope.stubs(:lookupvar).with("ipaddress_eth0").returns('10.0.0.1') + scope.stubs(:lookupvar).with('muppet').returns('kermit') + scope.stubs(:lookupvar).with('muppet_lo').returns('mspiggy') + scope.stubs(:lookupvar).with('muppet_eth0').returns('kermit') + end + it 'should have loopback (lo)' do + expect(subject.call(['lo'])).to be_truthy + end + it 'should not have loopback (lo0)' do + expect(subject.call(['lo0'])).to be_falsey + end + it 'should have ipaddress with 127.0.0.1' do + expect(subject.call(['ipaddress', '127.0.0.1'])).to be_truthy + end + it 'should have ipaddress with 10.0.0.1' do + expect(subject.call(['ipaddress', '10.0.0.1'])).to be_truthy + end + it 'should not have ipaddress with 10.0.0.2' do + expect(subject.call(['ipaddress', '10.0.0.2'])).to be_falsey + end + it 'should have muppet named kermit' do + expect(subject.call(['muppet', 'kermit'])).to be_truthy + end + it 'should have muppet named mspiggy' do + expect(subject.call(['muppet', 'mspiggy'])).to be_truthy + end + it 'should not have muppet named bigbird' do + expect(subject.call(['muppet', 'bigbird'])).to be_falsey + end + end +end diff --git a/puppet/modules/stdlib/spec/functions/has_ip_address_spec.rb b/puppet/modules/stdlib/spec/functions/has_ip_address_spec.rb new file mode 100755 index 00000000..0df12a7b --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/has_ip_address_spec.rb @@ -0,0 +1,39 @@ +#!/usr/bin/env ruby -S rspec +require 'spec_helper' + +describe Puppet::Parser::Functions.function(:has_ip_address) do + + let(:scope) do + PuppetlabsSpec::PuppetInternals.scope + end + + subject do + function_name = Puppet::Parser::Functions.function(:has_ip_address) + scope.method(function_name) + end + + context "On Linux Systems" do + before :each do + scope.stubs(:lookupvar).with('interfaces').returns('eth0,lo') + scope.stubs(:lookupvar).with('ipaddress').returns('10.0.2.15') + scope.stubs(:lookupvar).with('ipaddress_eth0').returns('10.0.2.15') + scope.stubs(:lookupvar).with('ipaddress_lo').returns('127.0.0.1') + end + + it 'should have primary address (10.0.2.15)' do + expect(subject.call(['10.0.2.15'])).to be_truthy + end + + it 'should have lookupback address (127.0.0.1)' do + expect(subject.call(['127.0.0.1'])).to be_truthy + end + + it 'should not have other address' do + expect(subject.call(['192.1681.1.1'])).to be_falsey + end + + it 'should not have "mspiggy" on an interface' do + expect(subject.call(['mspiggy'])).to be_falsey + end + end +end diff --git a/puppet/modules/stdlib/spec/functions/has_ip_network_spec.rb b/puppet/modules/stdlib/spec/functions/has_ip_network_spec.rb new file mode 100755 index 00000000..2a2578e2 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/has_ip_network_spec.rb @@ -0,0 +1,36 @@ +#!/usr/bin/env ruby -S rspec +require 'spec_helper' + +describe Puppet::Parser::Functions.function(:has_ip_network) do + + let(:scope) do + PuppetlabsSpec::PuppetInternals.scope + end + + subject do + function_name = Puppet::Parser::Functions.function(:has_ip_network) + scope.method(function_name) + end + + context "On Linux Systems" do + before :each do + scope.stubs(:lookupvar).with('interfaces').returns('eth0,lo') + scope.stubs(:lookupvar).with('network').returns(:undefined) + scope.stubs(:lookupvar).with('network_eth0').returns('10.0.2.0') + scope.stubs(:lookupvar).with('network_lo').returns('127.0.0.1') + end + + it 'should have primary network (10.0.2.0)' do + expect(subject.call(['10.0.2.0'])).to be_truthy + end + + it 'should have loopback network (127.0.0.0)' do + expect(subject.call(['127.0.0.1'])).to be_truthy + end + + it 'should not have other network' do + expect(subject.call(['192.168.1.0'])).to be_falsey + end + end +end + diff --git a/puppet/modules/stdlib/spec/functions/has_key_spec.rb b/puppet/modules/stdlib/spec/functions/has_key_spec.rb new file mode 100755 index 00000000..6b718005 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/has_key_spec.rb @@ -0,0 +1,42 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe Puppet::Parser::Functions.function(:has_key) do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + describe 'when calling has_key from puppet' do + it "should not compile when no arguments are passed" do + skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ + Puppet[:code] = '$x = has_key()' + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /wrong number of arguments/) + end + + it "should not compile when 1 argument is passed" do + skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ + Puppet[:code] = "$x = has_key('foo')" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /wrong number of arguments/) + end + + it "should require the first value to be a Hash" do + skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ + Puppet[:code] = "$x = has_key('foo', 'bar')" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /expects the first argument to be a hash/) + end + end + + describe 'when calling the function has_key from a scope instance' do + it 'should detect existing keys' do + expect(scope.function_has_key([{'one' => 1}, 'one'])).to be_truthy + end + + it 'should detect existing keys' do + expect(scope.function_has_key([{'one' => 1}, 'two'])).to be_falsey + end + end +end diff --git a/puppet/modules/stdlib/spec/functions/hash_spec.rb b/puppet/modules/stdlib/spec/functions/hash_spec.rb new file mode 100755 index 00000000..ec2988b0 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/hash_spec.rb @@ -0,0 +1,19 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the hash function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("hash")).to eq("function_hash") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_hash([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should convert an array to a hash" do + result = scope.function_hash([['a',1,'b',2,'c',3]]) + expect(result).to(eq({'a'=>1,'b'=>2,'c'=>3})) + end +end diff --git a/puppet/modules/stdlib/spec/functions/intersection_spec.rb b/puppet/modules/stdlib/spec/functions/intersection_spec.rb new file mode 100755 index 00000000..6361304f --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/intersection_spec.rb @@ -0,0 +1,19 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the intersection function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("intersection")).to eq("function_intersection") + end + + it "should raise a ParseError if there are fewer than 2 arguments" do + expect { scope.function_intersection([]) }.to( raise_error(Puppet::ParseError) ) + end + + it "should return the intersection of two arrays" do + result = scope.function_intersection([["a","b","c"],["b","c","d"]]) + expect(result).to(eq(["b","c"])) + end +end diff --git a/puppet/modules/stdlib/spec/functions/is_array_spec.rb b/puppet/modules/stdlib/spec/functions/is_array_spec.rb new file mode 100755 index 00000000..94920a4c --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/is_array_spec.rb @@ -0,0 +1,29 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the is_array function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("is_array")).to eq("function_is_array") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_is_array([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should return true if passed an array" do + result = scope.function_is_array([[1,2,3]]) + expect(result).to(eq(true)) + end + + it "should return false if passed a hash" do + result = scope.function_is_array([{'a'=>1}]) + expect(result).to(eq(false)) + end + + it "should return false if passed a string" do + result = scope.function_is_array(["asdf"]) + expect(result).to(eq(false)) + end +end diff --git a/puppet/modules/stdlib/spec/functions/is_bool_spec.rb b/puppet/modules/stdlib/spec/functions/is_bool_spec.rb new file mode 100755 index 00000000..4a342ba4 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/is_bool_spec.rb @@ -0,0 +1,44 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the is_bool function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("is_bool")).to eq("function_is_bool") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_is_bool([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should return true if passed a TrueClass" do + result = scope.function_is_bool([true]) + expect(result).to(eq(true)) + end + + it "should return true if passed a FalseClass" do + result = scope.function_is_bool([false]) + expect(result).to(eq(true)) + end + + it "should return false if passed the string 'true'" do + result = scope.function_is_bool(['true']) + expect(result).to(eq(false)) + end + + it "should return false if passed the string 'false'" do + result = scope.function_is_bool(['false']) + expect(result).to(eq(false)) + end + + it "should return false if passed an array" do + result = scope.function_is_bool([["a","b"]]) + expect(result).to(eq(false)) + end + + it "should return false if passed a hash" do + result = scope.function_is_bool([{"a" => "b"}]) + expect(result).to(eq(false)) + end +end diff --git a/puppet/modules/stdlib/spec/functions/is_domain_name_spec.rb b/puppet/modules/stdlib/spec/functions/is_domain_name_spec.rb new file mode 100755 index 00000000..4d05f5cd --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/is_domain_name_spec.rb @@ -0,0 +1,64 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the is_domain_name function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("is_domain_name")).to eq("function_is_domain_name") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_is_domain_name([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should return true if a valid short domain name" do + result = scope.function_is_domain_name(["x.com"]) + expect(result).to(be_truthy) + end + + it "should return true if the domain is ." do + result = scope.function_is_domain_name(["."]) + expect(result).to(be_truthy) + end + + it "should return true if the domain is x.com." do + result = scope.function_is_domain_name(["x.com."]) + expect(result).to(be_truthy) + end + + it "should return true if a valid domain name" do + result = scope.function_is_domain_name(["foo.bar.com"]) + expect(result).to(be_truthy) + end + + it "should allow domain parts to start with numbers" do + result = scope.function_is_domain_name(["3foo.2bar.com"]) + expect(result).to(be_truthy) + end + + it "should allow domain to end with a dot" do + result = scope.function_is_domain_name(["3foo.2bar.com."]) + expect(result).to(be_truthy) + end + + it "should allow a single part domain" do + result = scope.function_is_domain_name(["orange"]) + expect(result).to(be_truthy) + end + + it "should return false if domain parts start with hyphens" do + result = scope.function_is_domain_name(["-3foo.2bar.com"]) + expect(result).to(be_falsey) + end + + it "should return true if domain contains hyphens" do + result = scope.function_is_domain_name(["3foo-bar.2bar-fuzz.com"]) + expect(result).to(be_truthy) + end + + it "should return false if domain name contains spaces" do + result = scope.function_is_domain_name(["not valid"]) + expect(result).to(be_falsey) + end +end diff --git a/puppet/modules/stdlib/spec/functions/is_float_spec.rb b/puppet/modules/stdlib/spec/functions/is_float_spec.rb new file mode 100755 index 00000000..d926634e --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/is_float_spec.rb @@ -0,0 +1,33 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the is_float function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("is_float")).to eq("function_is_float") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_is_float([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should return true if a float" do + result = scope.function_is_float(["0.12"]) + expect(result).to(eq(true)) + end + + it "should return false if a string" do + result = scope.function_is_float(["asdf"]) + expect(result).to(eq(false)) + end + + it "should return false if an integer" do + result = scope.function_is_float(["3"]) + expect(result).to(eq(false)) + end + it "should return true if a float is created from an arithmetical operation" do + result = scope.function_is_float([3.2*2]) + expect(result).to(eq(true)) + end +end diff --git a/puppet/modules/stdlib/spec/functions/is_function_available.rb b/puppet/modules/stdlib/spec/functions/is_function_available.rb new file mode 100755 index 00000000..3a9aa1b2 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/is_function_available.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the is_function_available function" do + before :all do + Puppet::Parser::Functions.autoloader.loadall + end + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + expect(Puppet::Parser::Functions.function("is_function_available")).to eq("function_is_function_available") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { @scope.function_is_function_available([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should return false if a nonexistent function is passed" do + result = @scope.function_is_function_available(['jeff_mccunes_left_sock']) + expect(result).to(eq(false)) + end + + it "should return true if an available function is passed" do + result = @scope.function_is_function_available(['require']) + expect(result).to(eq(true)) + end + +end diff --git a/puppet/modules/stdlib/spec/functions/is_hash_spec.rb b/puppet/modules/stdlib/spec/functions/is_hash_spec.rb new file mode 100755 index 00000000..a8494111 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/is_hash_spec.rb @@ -0,0 +1,29 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the is_hash function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("is_hash")).to eq("function_is_hash") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_is_hash([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should return true if passed a hash" do + result = scope.function_is_hash([{"a"=>1,"b"=>2}]) + expect(result).to(eq(true)) + end + + it "should return false if passed an array" do + result = scope.function_is_hash([["a","b"]]) + expect(result).to(eq(false)) + end + + it "should return false if passed a string" do + result = scope.function_is_hash(["asdf"]) + expect(result).to(eq(false)) + end +end diff --git a/puppet/modules/stdlib/spec/functions/is_integer_spec.rb b/puppet/modules/stdlib/spec/functions/is_integer_spec.rb new file mode 100755 index 00000000..f0cbca80 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/is_integer_spec.rb @@ -0,0 +1,69 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the is_integer function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("is_integer")).to eq("function_is_integer") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_is_integer([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should return true if an integer" do + result = scope.function_is_integer(["3"]) + expect(result).to(eq(true)) + end + + it "should return true if a negative integer" do + result = scope.function_is_integer(["-7"]) + expect(result).to(eq(true)) + end + + it "should return false if a float" do + result = scope.function_is_integer(["3.2"]) + expect(result).to(eq(false)) + end + + it "should return false if a string" do + result = scope.function_is_integer(["asdf"]) + expect(result).to(eq(false)) + end + + it "should return true if an integer is created from an arithmetical operation" do + result = scope.function_is_integer([3*2]) + expect(result).to(eq(true)) + end + + it "should return false if an array" do + result = scope.function_is_numeric([["asdf"]]) + expect(result).to(eq(false)) + end + + it "should return false if a hash" do + result = scope.function_is_numeric([{"asdf" => false}]) + expect(result).to(eq(false)) + end + + it "should return false if a boolean" do + result = scope.function_is_numeric([true]) + expect(result).to(eq(false)) + end + + it "should return false if a whitespace is in the string" do + result = scope.function_is_numeric([" -1324"]) + expect(result).to(eq(false)) + end + + it "should return false if it is zero prefixed" do + result = scope.function_is_numeric(["0001234"]) + expect(result).to(eq(false)) + end + + it "should return false if it is wrapped inside an array" do + result = scope.function_is_numeric([[1234]]) + expect(result).to(eq(false)) + end +end diff --git a/puppet/modules/stdlib/spec/functions/is_ip_address_spec.rb b/puppet/modules/stdlib/spec/functions/is_ip_address_spec.rb new file mode 100755 index 00000000..c16d12b1 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/is_ip_address_spec.rb @@ -0,0 +1,39 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the is_ip_address function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("is_ip_address")).to eq("function_is_ip_address") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_is_ip_address([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should return true if an IPv4 address" do + result = scope.function_is_ip_address(["1.2.3.4"]) + expect(result).to(eq(true)) + end + + it "should return true if a full IPv6 address" do + result = scope.function_is_ip_address(["fe80:0000:cd12:d123:e2f8:47ff:fe09:dd74"]) + expect(result).to(eq(true)) + end + + it "should return true if a compressed IPv6 address" do + result = scope.function_is_ip_address(["fe00::1"]) + expect(result).to(eq(true)) + end + + it "should return false if not valid" do + result = scope.function_is_ip_address(["asdf"]) + expect(result).to(eq(false)) + end + + it "should return false if IP octets out of range" do + result = scope.function_is_ip_address(["1.1.1.300"]) + expect(result).to(eq(false)) + end +end diff --git a/puppet/modules/stdlib/spec/functions/is_mac_address_spec.rb b/puppet/modules/stdlib/spec/functions/is_mac_address_spec.rb new file mode 100755 index 00000000..66edd197 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/is_mac_address_spec.rb @@ -0,0 +1,29 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the is_mac_address function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("is_mac_address")).to eq("function_is_mac_address") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_is_mac_address([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should return true if a valid mac address" do + result = scope.function_is_mac_address(["00:a0:1f:12:7f:a0"]) + expect(result).to(eq(true)) + end + + it "should return false if octets are out of range" do + result = scope.function_is_mac_address(["00:a0:1f:12:7f:g0"]) + expect(result).to(eq(false)) + end + + it "should return false if not valid" do + result = scope.function_is_mac_address(["not valid"]) + expect(result).to(eq(false)) + end +end diff --git a/puppet/modules/stdlib/spec/functions/is_numeric_spec.rb b/puppet/modules/stdlib/spec/functions/is_numeric_spec.rb new file mode 100755 index 00000000..4176961d --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/is_numeric_spec.rb @@ -0,0 +1,119 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the is_numeric function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("is_numeric")).to eq("function_is_numeric") + end + + it "should raise a ParseError if there is less than 1 argument" do + expect { scope.function_is_numeric([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should return true if an integer" do + result = scope.function_is_numeric(["3"]) + expect(result).to(eq(true)) + end + + it "should return true if a float" do + result = scope.function_is_numeric(["3.2"]) + expect(result).to(eq(true)) + end + + it "should return true if an integer is created from an arithmetical operation" do + result = scope.function_is_numeric([3*2]) + expect(result).to(eq(true)) + end + + it "should return true if a float is created from an arithmetical operation" do + result = scope.function_is_numeric([3.2*2]) + expect(result).to(eq(true)) + end + + it "should return false if a string" do + result = scope.function_is_numeric(["asdf"]) + expect(result).to(eq(false)) + end + + it "should return false if an array" do + result = scope.function_is_numeric([["asdf"]]) + expect(result).to(eq(false)) + end + + it "should return false if an array of integers" do + result = scope.function_is_numeric([[1,2,3,4]]) + expect(result).to(eq(false)) + end + + it "should return false if a hash" do + result = scope.function_is_numeric([{"asdf" => false}]) + expect(result).to(eq(false)) + end + + it "should return false if a hash with numbers in it" do + result = scope.function_is_numeric([{1 => 2}]) + expect(result).to(eq(false)) + end + + it "should return false if a boolean" do + result = scope.function_is_numeric([true]) + expect(result).to(eq(false)) + end + + it "should return true if a negative float with exponent" do + result = scope.function_is_numeric(["-342.2315e-12"]) + expect(result).to(eq(true)) + end + + it "should return false if a negative integer with whitespaces before/after the dash" do + result = scope.function_is_numeric([" - 751"]) + expect(result).to(eq(false)) + end + +# it "should return true if a hexadecimal" do +# result = scope.function_is_numeric(["0x52F8c"]) +# result.should(eq(true)) +# end +# +# it "should return true if a hexadecimal with uppercase 0X prefix" do +# result = scope.function_is_numeric(["0X52F8c"]) +# result.should(eq(true)) +# end +# +# it "should return false if a hexadecimal without a prefix" do +# result = scope.function_is_numeric(["52F8c"]) +# result.should(eq(false)) +# end +# +# it "should return true if a octal" do +# result = scope.function_is_numeric(["0751"]) +# result.should(eq(true)) +# end +# +# it "should return true if a negative hexadecimal" do +# result = scope.function_is_numeric(["-0x52F8c"]) +# result.should(eq(true)) +# end +# +# it "should return true if a negative octal" do +# result = scope.function_is_numeric(["-0751"]) +# result.should(eq(true)) +# end +# +# it "should return false if a negative octal with whitespaces before/after the dash" do +# result = scope.function_is_numeric([" - 0751"]) +# result.should(eq(false)) +# end +# +# it "should return false if a bad hexadecimal" do +# result = scope.function_is_numeric(["0x23d7g"]) +# result.should(eq(false)) +# end +# +# it "should return false if a bad octal" do +# result = scope.function_is_numeric(["0287"]) +# result.should(eq(false)) +# end +end diff --git a/puppet/modules/stdlib/spec/functions/is_string_spec.rb b/puppet/modules/stdlib/spec/functions/is_string_spec.rb new file mode 100755 index 00000000..6a0801ae --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/is_string_spec.rb @@ -0,0 +1,34 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the is_string function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("is_string")).to eq("function_is_string") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_is_string([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should return true if a string" do + result = scope.function_is_string(["asdf"]) + expect(result).to(eq(true)) + end + + it "should return false if an integer" do + result = scope.function_is_string(["3"]) + expect(result).to(eq(false)) + end + + it "should return false if a float" do + result = scope.function_is_string(["3.23"]) + expect(result).to(eq(false)) + end + + it "should return false if an array" do + result = scope.function_is_string([["a","b","c"]]) + expect(result).to(eq(false)) + end +end diff --git a/puppet/modules/stdlib/spec/functions/join_keys_to_values_spec.rb b/puppet/modules/stdlib/spec/functions/join_keys_to_values_spec.rb new file mode 100755 index 00000000..4a9ae87a --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/join_keys_to_values_spec.rb @@ -0,0 +1,40 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the join_keys_to_values function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("join_keys_to_values")).to eq("function_join_keys_to_values") + end + + it "should raise a ParseError if there are fewer than two arguments" do + expect { scope.function_join_keys_to_values([{}]) }.to raise_error Puppet::ParseError + end + + it "should raise a ParseError if there are greater than two arguments" do + expect { scope.function_join_keys_to_values([{}, 'foo', 'bar']) }.to raise_error Puppet::ParseError + end + + it "should raise a TypeError if the first argument is an array" do + expect { scope.function_join_keys_to_values([[1,2], ',']) }.to raise_error TypeError + end + + it "should raise a TypeError if the second argument is an array" do + expect { scope.function_join_keys_to_values([{}, [1,2]]) }.to raise_error TypeError + end + + it "should raise a TypeError if the second argument is a number" do + expect { scope.function_join_keys_to_values([{}, 1]) }.to raise_error TypeError + end + + it "should return an empty array given an empty hash" do + result = scope.function_join_keys_to_values([{}, ":"]) + expect(result).to eq([]) + end + + it "should join hash's keys to its values" do + result = scope.function_join_keys_to_values([{'a'=>1,2=>'foo',:b=>nil}, ":"]) + expect(result).to match_array(['a:1','2:foo','b:']) + end +end diff --git a/puppet/modules/stdlib/spec/functions/join_spec.rb b/puppet/modules/stdlib/spec/functions/join_spec.rb new file mode 100755 index 00000000..793c36fa --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/join_spec.rb @@ -0,0 +1,19 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the join function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("join")).to eq("function_join") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_join([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should join an array into a string" do + result = scope.function_join([["a","b","c"], ":"]) + expect(result).to(eq("a:b:c")) + end +end diff --git a/puppet/modules/stdlib/spec/functions/keys_spec.rb b/puppet/modules/stdlib/spec/functions/keys_spec.rb new file mode 100755 index 00000000..f2e7d428 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/keys_spec.rb @@ -0,0 +1,21 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the keys function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("keys")).to eq("function_keys") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_keys([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should return an array of keys when given a hash" do + result = scope.function_keys([{'a'=>1, 'b'=>2}]) + # =~ performs 'array with same elements' (set) matching + # For more info see RSpec::Matchers::MatchArray + expect(result).to match_array(['a','b']) + end +end diff --git a/puppet/modules/stdlib/spec/functions/loadyaml_spec.rb b/puppet/modules/stdlib/spec/functions/loadyaml_spec.rb new file mode 100755 index 00000000..cdc3d6f5 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/loadyaml_spec.rb @@ -0,0 +1,25 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the loadyaml function" do + include PuppetlabsSpec::Files + + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("loadyaml")).to eq("function_loadyaml") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_loadyaml([]) }.to raise_error(Puppet::ParseError) + end + + it "should convert YAML file to a data structure" do + yaml_file = tmpfilename ('yamlfile') + File.open(yaml_file, 'w') do |fh| + fh.write("---\n aaa: 1\n bbb: 2\n ccc: 3\n ddd: 4\n") + end + result = scope.function_loadyaml([yaml_file]) + expect(result).to eq({"aaa" => 1, "bbb" => 2, "ccc" => 3, "ddd" => 4 }) + end +end diff --git a/puppet/modules/stdlib/spec/functions/lstrip_spec.rb b/puppet/modules/stdlib/spec/functions/lstrip_spec.rb new file mode 100755 index 00000000..68cca1c5 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/lstrip_spec.rb @@ -0,0 +1,28 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the lstrip function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("lstrip")).to eq("function_lstrip") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_lstrip([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should lstrip a string" do + result = scope.function_lstrip([" asdf"]) + expect(result).to(eq('asdf')) + end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new(" asdf") + result = scope.function_lstrip([value]) + result.should(eq("asdf")) + end +end diff --git a/puppet/modules/stdlib/spec/functions/max_spec.rb b/puppet/modules/stdlib/spec/functions/max_spec.rb new file mode 100755 index 00000000..c3d8a132 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/max_spec.rb @@ -0,0 +1,27 @@ +#! /usr/bin/env ruby -S rspec + +require 'spec_helper' + +describe "the max function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("max")).to eq("function_max") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_max([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should be able to compare strings" do + expect(scope.function_max(["albatross","dog","horse"])).to(eq("horse")) + end + + it "should be able to compare numbers" do + expect(scope.function_max([6,8,4])).to(eq(8)) + end + + it "should be able to compare a number with a stringified number" do + expect(scope.function_max([1,"2"])).to(eq("2")) + end +end diff --git a/puppet/modules/stdlib/spec/functions/member_spec.rb b/puppet/modules/stdlib/spec/functions/member_spec.rb new file mode 100755 index 00000000..1a1d7c66 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/member_spec.rb @@ -0,0 +1,34 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the member function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("member")).to eq("function_member") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_member([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should return true if a member is in an array" do + result = scope.function_member([["a","b","c"], "a"]) + expect(result).to(eq(true)) + end + + it "should return false if a member is not in an array" do + result = scope.function_member([["a","b","c"], "d"]) + expect(result).to(eq(false)) + end + + it "should return true if a member array is in an array" do + result = scope.function_member([["a","b","c"], ["a", "b"]]) + expect(result).to(eq(true)) + end + + it "should return false if a member array is not in an array" do + result = scope.function_member([["a","b","c"], ["d", "e"]]) + expect(result).to(eq(false)) + end +end diff --git a/puppet/modules/stdlib/spec/functions/merge_spec.rb b/puppet/modules/stdlib/spec/functions/merge_spec.rb new file mode 100755 index 00000000..2abf9762 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/merge_spec.rb @@ -0,0 +1,52 @@ +#! /usr/bin/env ruby -S rspec + +require 'spec_helper' + +describe Puppet::Parser::Functions.function(:merge) do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + describe 'when calling merge from puppet' do + it "should not compile when no arguments are passed" do + skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ + Puppet[:code] = '$x = merge()' + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /wrong number of arguments/) + end + + it "should not compile when 1 argument is passed" do + skip("Fails on 2.6.x, see bug #15912") if Puppet.version =~ /^2\.6\./ + Puppet[:code] = "$my_hash={'one' => 1}\n$x = merge($my_hash)" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /wrong number of arguments/) + end + end + + describe 'when calling merge on the scope instance' do + it 'should require all parameters are hashes' do + expect { new_hash = scope.function_merge([{}, '2'])}.to raise_error(Puppet::ParseError, /unexpected argument type String/) + expect { new_hash = scope.function_merge([{}, 2])}.to raise_error(Puppet::ParseError, /unexpected argument type Fixnum/) + end + + it 'should accept empty strings as puppet undef' do + expect { new_hash = scope.function_merge([{}, ''])}.not_to raise_error + end + + it 'should be able to merge two hashes' do + new_hash = scope.function_merge([{'one' => '1', 'two' => '1'}, {'two' => '2', 'three' => '2'}]) + expect(new_hash['one']).to eq('1') + expect(new_hash['two']).to eq('2') + expect(new_hash['three']).to eq('2') + end + + it 'should merge multiple hashes' do + hash = scope.function_merge([{'one' => 1}, {'one' => '2'}, {'one' => '3'}]) + expect(hash['one']).to eq('3') + end + + it 'should accept empty hashes' do + expect(scope.function_merge([{},{},{}])).to eq({}) + end + end +end diff --git a/puppet/modules/stdlib/spec/functions/min_spec.rb b/puppet/modules/stdlib/spec/functions/min_spec.rb new file mode 100755 index 00000000..35a08900 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/min_spec.rb @@ -0,0 +1,27 @@ +#! /usr/bin/env ruby -S rspec + +require 'spec_helper' + +describe "the min function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("min")).to eq("function_min") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_min([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should be able to compare strings" do + expect(scope.function_min(["albatross","dog","horse"])).to(eq("albatross")) + end + + it "should be able to compare numbers" do + expect(scope.function_min([6,8,4])).to(eq(4)) + end + + it "should be able to compare a number with a stringified number" do + expect(scope.function_min([1,"2"])).to(eq(1)) + end +end diff --git a/puppet/modules/stdlib/spec/functions/num2bool_spec.rb b/puppet/modules/stdlib/spec/functions/num2bool_spec.rb new file mode 100755 index 00000000..d0ba9354 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/num2bool_spec.rb @@ -0,0 +1,67 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the num2bool function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("num2bool")).to eq("function_num2bool") + end + + it "should raise a ParseError if there are no arguments" do + expect { scope.function_num2bool([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should raise a ParseError if there are more than 1 arguments" do + expect { scope.function_num2bool(["foo","bar"]) }.to( raise_error(Puppet::ParseError)) + end + + it "should raise a ParseError if passed something non-numeric" do + expect { scope.function_num2bool(["xyzzy"]) }.to( raise_error(Puppet::ParseError)) + end + + it "should return true if passed string 1" do + result = scope.function_num2bool(["1"]) + expect(result).to(be_truthy) + end + + it "should return true if passed string 1.5" do + result = scope.function_num2bool(["1.5"]) + expect(result).to(be_truthy) + end + + it "should return true if passed number 1" do + result = scope.function_num2bool([1]) + expect(result).to(be_truthy) + end + + it "should return false if passed string 0" do + result = scope.function_num2bool(["0"]) + expect(result).to(be_falsey) + end + + it "should return false if passed number 0" do + result = scope.function_num2bool([0]) + expect(result).to(be_falsey) + end + + it "should return false if passed string -1" do + result = scope.function_num2bool(["-1"]) + expect(result).to(be_falsey) + end + + it "should return false if passed string -1.5" do + result = scope.function_num2bool(["-1.5"]) + expect(result).to(be_falsey) + end + + it "should return false if passed number -1" do + result = scope.function_num2bool([-1]) + expect(result).to(be_falsey) + end + + it "should return false if passed float -1.5" do + result = scope.function_num2bool([-1.5]) + expect(result).to(be_falsey) + end +end diff --git a/puppet/modules/stdlib/spec/functions/parsejson_spec.rb b/puppet/modules/stdlib/spec/functions/parsejson_spec.rb new file mode 100755 index 00000000..1dd41b96 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/parsejson_spec.rb @@ -0,0 +1,22 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the parsejson function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("parsejson")).to eq("function_parsejson") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_parsejson([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should convert JSON to a data structure" do + json = <<-EOS +["aaa","bbb","ccc"] +EOS + result = scope.function_parsejson([json]) + expect(result).to(eq(['aaa','bbb','ccc'])) + end +end diff --git a/puppet/modules/stdlib/spec/functions/parseyaml_spec.rb b/puppet/modules/stdlib/spec/functions/parseyaml_spec.rb new file mode 100755 index 00000000..e5f145ba --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/parseyaml_spec.rb @@ -0,0 +1,24 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the parseyaml function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("parseyaml")).to eq("function_parseyaml") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_parseyaml([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should convert YAML to a data structure" do + yaml = <<-EOS +- aaa +- bbb +- ccc +EOS + result = scope.function_parseyaml([yaml]) + expect(result).to(eq(['aaa','bbb','ccc'])) + end +end diff --git a/puppet/modules/stdlib/spec/functions/pick_default_spec.rb b/puppet/modules/stdlib/spec/functions/pick_default_spec.rb new file mode 100755 index 00000000..db10cc35 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/pick_default_spec.rb @@ -0,0 +1,58 @@ +#!/usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the pick_default function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("pick_default")).to eq("function_pick_default") + end + + it 'should return the correct value' do + expect(scope.function_pick_default(['first', 'second'])).to eq('first') + end + + it 'should return the correct value if the first value is empty' do + expect(scope.function_pick_default(['', 'second'])).to eq('second') + end + + it 'should skip empty string values' do + expect(scope.function_pick_default(['', 'first'])).to eq('first') + end + + it 'should skip :undef values' do + expect(scope.function_pick_default([:undef, 'first'])).to eq('first') + end + + it 'should skip :undefined values' do + expect(scope.function_pick_default([:undefined, 'first'])).to eq('first') + end + + it 'should return the empty string if it is the last possibility' do + expect(scope.function_pick_default([:undef, :undefined, ''])).to eq('') + end + + it 'should return :undef if it is the last possibility' do + expect(scope.function_pick_default(['', :undefined, :undef])).to eq(:undef) + end + + it 'should return :undefined if it is the last possibility' do + expect(scope.function_pick_default([:undef, '', :undefined])).to eq(:undefined) + end + + it 'should return the empty string if it is the only possibility' do + expect(scope.function_pick_default([''])).to eq('') + end + + it 'should return :undef if it is the only possibility' do + expect(scope.function_pick_default([:undef])).to eq(:undef) + end + + it 'should return :undefined if it is the only possibility' do + expect(scope.function_pick_default([:undefined])).to eq(:undefined) + end + + it 'should error if no values are passed' do + expect { scope.function_pick_default([]) }.to raise_error(Puppet::Error, /Must receive at least one argument./) + end +end diff --git a/puppet/modules/stdlib/spec/functions/pick_spec.rb b/puppet/modules/stdlib/spec/functions/pick_spec.rb new file mode 100755 index 00000000..8be8f587 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/pick_spec.rb @@ -0,0 +1,34 @@ +#!/usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the pick function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("pick")).to eq("function_pick") + end + + it 'should return the correct value' do + expect(scope.function_pick(['first', 'second'])).to eq('first') + end + + it 'should return the correct value if the first value is empty' do + expect(scope.function_pick(['', 'second'])).to eq('second') + end + + it 'should remove empty string values' do + expect(scope.function_pick(['', 'first'])).to eq('first') + end + + it 'should remove :undef values' do + expect(scope.function_pick([:undef, 'first'])).to eq('first') + end + + it 'should remove :undefined values' do + expect(scope.function_pick([:undefined, 'first'])).to eq('first') + end + + it 'should error if no values are passed' do + expect { scope.function_pick([]) }.to( raise_error(Puppet::ParseError, "pick(): must receive at least one non empty value")) + end +end diff --git a/puppet/modules/stdlib/spec/functions/prefix_spec.rb b/puppet/modules/stdlib/spec/functions/prefix_spec.rb new file mode 100755 index 00000000..34cac530 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/prefix_spec.rb @@ -0,0 +1,28 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the prefix function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "raises a ParseError if there is less than 1 arguments" do + expect { scope.function_prefix([]) }.to raise_error(Puppet::ParseError, /number of arguments/) + end + + it "raises an error if the first argument is not an array" do + expect { + scope.function_prefix([Object.new]) + }.to raise_error(Puppet::ParseError, /expected first argument to be an Array/) + end + + + it "raises an error if the second argument is not a string" do + expect { + scope.function_prefix([['first', 'second'], 42]) + }.to raise_error(Puppet::ParseError, /expected second argument to be a String/) + end + + it "returns a prefixed array" do + result = scope.function_prefix([['a','b','c'], 'p']) + expect(result).to(eq(['pa','pb','pc'])) + end +end diff --git a/puppet/modules/stdlib/spec/functions/private_spec.rb b/puppet/modules/stdlib/spec/functions/private_spec.rb new file mode 100755 index 00000000..c70759fa --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/private_spec.rb @@ -0,0 +1,55 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe Puppet::Parser::Functions.function(:private) do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + subject do + function_name = Puppet::Parser::Functions.function(:private) + scope.method(function_name) + end + + context "when called from inside module" do + it "should not fail" do + scope.expects(:lookupvar).with('module_name').returns('foo') + scope.expects(:lookupvar).with('caller_module_name').returns('foo') + expect { + subject.call [] + }.not_to raise_error + 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, /failure message!/ + end + end + + context "when called from private class" do + it "should fail with a class error message" do + scope.expects(:lookupvar).with('module_name').returns('foo') + scope.expects(:lookupvar).with('caller_module_name').returns('bar') + scope.source.expects(:name).returns('foo::baz') + scope.source.expects(:type).returns('hostclass') + expect { + subject.call [] + }.to raise_error Puppet::ParseError, /Class foo::baz is private/ + end + end + + context "when called from private definition" do + it "should fail with a class error message" do + scope.expects(:lookupvar).with('module_name').returns('foo') + scope.expects(:lookupvar).with('caller_module_name').returns('bar') + scope.source.expects(:name).returns('foo::baz') + scope.source.expects(:type).returns('definition') + expect { + subject.call [] + }.to raise_error Puppet::ParseError, /Definition foo::baz is private/ + end + end +end diff --git a/puppet/modules/stdlib/spec/functions/range_spec.rb b/puppet/modules/stdlib/spec/functions/range_spec.rb new file mode 100755 index 00000000..ef86f971 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/range_spec.rb @@ -0,0 +1,86 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the range function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "exists" do + expect(Puppet::Parser::Functions.function("range")).to eq("function_range") + end + + it "raises a ParseError if there is less than 1 arguments" do + expect { scope.function_range([]) }.to raise_error Puppet::ParseError, /Wrong number of arguments.*0 for 1/ + end + + describe 'with a letter range' do + it "returns a letter range" do + result = scope.function_range(["a","d"]) + expect(result).to eq ['a','b','c','d'] + end + + it "returns a letter range given a step of 1" do + result = scope.function_range(["a","d","1"]) + expect(result).to eq ['a','b','c','d'] + end + + it "returns a stepped letter range" do + result = scope.function_range(["a","d","2"]) + expect(result).to eq ['a','c'] + end + + it "returns a stepped letter range given a negative step" do + result = scope.function_range(["a","d","-2"]) + expect(result).to eq ['a','c'] + end + end + + describe 'with a number range' do + it "returns a number range" do + result = scope.function_range(["1","4"]) + expect(result).to eq [1,2,3,4] + end + + it "returns a number range given a step of 1" do + result = scope.function_range(["1","4","1"]) + expect(result).to eq [1,2,3,4] + end + + it "returns a stepped number range" do + result = scope.function_range(["1","4","2"]) + expect(result).to eq [1,3] + end + + it "returns a stepped number range given a negative step" do + result = scope.function_range(["1","4","-2"]) + expect(result).to eq [1,3] + end + end + + describe 'with a numeric-like string range' do + it "works with padded hostname like strings" do + expected = ("host01".."host10").to_a + expect(scope.function_range(["host01","host10"])).to eq expected + end + + it "coerces zero padded digits to integers" do + expected = (0..10).to_a + expect(scope.function_range(["00", "10"])).to eq expected + end + end + + describe 'with a numeric range' do + it "returns a range of numbers" do + expected = (1..10).to_a + expect(scope.function_range([1,10])).to eq expected + end + it "returns a range of numbers with step parameter" do + expected = (1..10).step(2).to_a + expect(scope.function_range([1,10,2])).to eq expected + end + it "works with mixed numeric like strings and numeric arguments" do + expected = (1..10).to_a + expect(scope.function_range(['1',10])).to eq expected + expect(scope.function_range([1,'10'])).to eq expected + end + end +end diff --git a/puppet/modules/stdlib/spec/functions/reject_spec.rb b/puppet/modules/stdlib/spec/functions/reject_spec.rb new file mode 100755 index 00000000..88a992ef --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/reject_spec.rb @@ -0,0 +1,20 @@ +#!/usr/bin/env ruby + +require 'spec_helper' + +describe "the reject function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("reject")).to eq("function_reject") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_reject([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should reject contents from an array" do + result = scope.function_reject([["1111", "aaabbb","bbbccc","dddeee"], "bbb"]) + expect(result).to(eq(["1111", "dddeee"])) + end +end diff --git a/puppet/modules/stdlib/spec/functions/reverse_spec.rb b/puppet/modules/stdlib/spec/functions/reverse_spec.rb new file mode 100755 index 00000000..1f047943 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/reverse_spec.rb @@ -0,0 +1,28 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the reverse function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("reverse")).to eq("function_reverse") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_reverse([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should reverse a string" do + result = scope.function_reverse(["asdfghijkl"]) + expect(result).to(eq('lkjihgfdsa')) + end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new('asdfghjkl') + result = scope.function_reverse([value]) + result.should(eq('lkjhgfdsa')) + end +end diff --git a/puppet/modules/stdlib/spec/functions/rstrip_spec.rb b/puppet/modules/stdlib/spec/functions/rstrip_spec.rb new file mode 100755 index 00000000..f6b48389 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/rstrip_spec.rb @@ -0,0 +1,33 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the rstrip function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("rstrip")).to eq("function_rstrip") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_rstrip([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should rstrip a string" do + result = scope.function_rstrip(["asdf "]) + expect(result).to(eq('asdf')) + end + + it "should rstrip each element in an array" do + result = scope.function_rstrip([["a ","b ", "c "]]) + expect(result).to(eq(['a','b','c'])) + end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new('asdf ') + result = scope.function_rstrip([value]) + result.should(eq('asdf')) + end +end diff --git a/puppet/modules/stdlib/spec/functions/shuffle_spec.rb b/puppet/modules/stdlib/spec/functions/shuffle_spec.rb new file mode 100755 index 00000000..a62c1fb5 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/shuffle_spec.rb @@ -0,0 +1,33 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the shuffle function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("shuffle")).to eq("function_shuffle") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_shuffle([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should shuffle a string and the result should be the same size" do + result = scope.function_shuffle(["asdf"]) + expect(result.size).to(eq(4)) + end + + it "should shuffle a string but the sorted contents should still be the same" do + result = scope.function_shuffle(["adfs"]) + expect(result.split("").sort.join("")).to(eq("adfs")) + end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new('asdf') + result = scope.function_shuffle([value]) + result.size.should(eq(4)) + end +end diff --git a/puppet/modules/stdlib/spec/functions/size_spec.rb b/puppet/modules/stdlib/spec/functions/size_spec.rb new file mode 100755 index 00000000..18eb48f9 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/size_spec.rb @@ -0,0 +1,24 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the size function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("size")).to eq("function_size") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_size([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should return the size of a string" do + result = scope.function_size(["asdf"]) + expect(result).to(eq(4)) + end + + it "should return the size of an array" do + result = scope.function_size([["a","b","c"]]) + expect(result).to(eq(3)) + end +end diff --git a/puppet/modules/stdlib/spec/functions/sort_spec.rb b/puppet/modules/stdlib/spec/functions/sort_spec.rb new file mode 100755 index 00000000..4c2a66cf --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/sort_spec.rb @@ -0,0 +1,24 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the sort function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("sort")).to eq("function_sort") + end + + it "should raise a ParseError if there is not 1 arguments" do + expect { scope.function_sort(['','']) }.to( raise_error(Puppet::ParseError)) + end + + it "should sort an array" do + result = scope.function_sort([["a","c","b"]]) + expect(result).to(eq(['a','b','c'])) + end + + it "should sort a string" do + result = scope.function_sort(["acb"]) + expect(result).to(eq('abc')) + end +end diff --git a/puppet/modules/stdlib/spec/functions/squeeze_spec.rb b/puppet/modules/stdlib/spec/functions/squeeze_spec.rb new file mode 100755 index 00000000..cd0eb37f --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/squeeze_spec.rb @@ -0,0 +1,24 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the squeeze function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("squeeze")).to eq("function_squeeze") + end + + it "should raise a ParseError if there is less than 2 arguments" do + expect { scope.function_squeeze([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should squeeze a string" do + result = scope.function_squeeze(["aaabbbbcccc"]) + expect(result).to(eq('abc')) + end + + it "should squeeze all elements in an array" do + result = scope.function_squeeze([["aaabbbbcccc","dddfff"]]) + expect(result).to(eq(['abc','df'])) + end +end diff --git a/puppet/modules/stdlib/spec/functions/str2bool_spec.rb b/puppet/modules/stdlib/spec/functions/str2bool_spec.rb new file mode 100755 index 00000000..1d205d75 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/str2bool_spec.rb @@ -0,0 +1,31 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the str2bool function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("str2bool")).to eq("function_str2bool") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_str2bool([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should convert string 'true' to true" do + result = scope.function_str2bool(["true"]) + expect(result).to(eq(true)) + end + + it "should convert string 'undef' to false" do + result = scope.function_str2bool(["undef"]) + expect(result).to(eq(false)) + end + + it "should return the boolean it was called with" do + result = scope.function_str2bool([true]) + expect(result).to(eq(true)) + result = scope.function_str2bool([false]) + expect(result).to(eq(false)) + end +end diff --git a/puppet/modules/stdlib/spec/functions/str2saltedsha512_spec.rb b/puppet/modules/stdlib/spec/functions/str2saltedsha512_spec.rb new file mode 100755 index 00000000..ab7f57f1 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/str2saltedsha512_spec.rb @@ -0,0 +1,45 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the str2saltedsha512 function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("str2saltedsha512")).to eq("function_str2saltedsha512") + end + + it "should raise a ParseError if there is less than 1 argument" do + expect { scope.function_str2saltedsha512([]) }.to( raise_error(Puppet::ParseError) ) + end + + it "should raise a ParseError if there is more than 1 argument" do + expect { scope.function_str2saltedsha512(['foo', 'bar', 'baz']) }.to( raise_error(Puppet::ParseError) ) + end + + it "should return a salted-sha512 password hash 136 characters in length" do + result = scope.function_str2saltedsha512(["password"]) + expect(result.length).to(eq(136)) + end + + it "should raise an error if you pass a non-string password" do + expect { scope.function_str2saltedsha512([1234]) }.to( raise_error(Puppet::ParseError) ) + end + + it "should generate a valid password" do + # Allow the function to generate a password based on the string 'password' + password_hash = scope.function_str2saltedsha512(["password"]) + + # Separate the Salt and Password from the Password Hash + salt = password_hash[0..7] + password = password_hash[8..-1] + + # Convert the Salt and Password from Hex to Binary Data + str_salt = Array(salt.lines).pack('H*') + str_password = Array(password.lines).pack('H*') + + # Combine the Binary Salt with 'password' and compare the end result + saltedpass = Digest::SHA512.digest(str_salt + 'password') + result = (str_salt + saltedpass).unpack('H*')[0] + expect(result).to eq(password_hash) + end +end diff --git a/puppet/modules/stdlib/spec/functions/strftime_spec.rb b/puppet/modules/stdlib/spec/functions/strftime_spec.rb new file mode 100755 index 00000000..ebec54b8 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/strftime_spec.rb @@ -0,0 +1,29 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the strftime function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("strftime")).to eq("function_strftime") + end + + it "should raise 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 > 1311953157) + end + + it "using %s should be lower then 1.5 trillion" do + result = scope.function_strftime(["%s"]) + expect(result.to_i).to(be < 1500000000) + end + + it "should return a date when given %Y-%m-%d" do + result = scope.function_strftime(["%Y-%m-%d"]) + expect(result).to match(/^\d{4}-\d{2}-\d{2}$/) + end +end diff --git a/puppet/modules/stdlib/spec/functions/strip_spec.rb b/puppet/modules/stdlib/spec/functions/strip_spec.rb new file mode 100755 index 00000000..4ac8daf8 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/strip_spec.rb @@ -0,0 +1,27 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the strip function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + it "should exist" do + expect(Puppet::Parser::Functions.function("strip")).to eq("function_strip") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_strip([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should strip a string" do + result = scope.function_strip([" ab cd "]) + expect(result).to(eq('ab cd')) + end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new(' as df ') + result = scope.function_strip([value]) + result.should(eq('as df')) + end +end diff --git a/puppet/modules/stdlib/spec/functions/suffix_spec.rb b/puppet/modules/stdlib/spec/functions/suffix_spec.rb new file mode 100755 index 00000000..c7783c64 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/suffix_spec.rb @@ -0,0 +1,27 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the suffix function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "raises a ParseError if there is less than 1 arguments" do + expect { scope.function_suffix([]) }.to raise_error(Puppet::ParseError, /number of arguments/) + end + + it "raises an error if the first argument is not an array" do + expect { + scope.function_suffix([Object.new]) + }.to raise_error(Puppet::ParseError, /expected first argument to be an Array/) + end + + it "raises an error if the second argument is not a string" do + expect { + scope.function_suffix([['first', 'second'], 42]) + }.to raise_error(Puppet::ParseError, /expected second argument to be a String/) + end + + it "returns a suffixed array" do + result = scope.function_suffix([['a','b','c'], 'p']) + expect(result).to(eq(['ap','bp','cp'])) + end +end diff --git a/puppet/modules/stdlib/spec/functions/swapcase_spec.rb b/puppet/modules/stdlib/spec/functions/swapcase_spec.rb new file mode 100755 index 00000000..791d1dfa --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/swapcase_spec.rb @@ -0,0 +1,28 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the swapcase function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("swapcase")).to eq("function_swapcase") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_swapcase([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should swapcase a string" do + result = scope.function_swapcase(["aaBBccDD"]) + expect(result).to(eq('AAbbCCdd')) + end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new("aaBBccDD") + result = scope.function_swapcase([value]) + result.should(eq("AAbbCCdd")) + end +end diff --git a/puppet/modules/stdlib/spec/functions/time_spec.rb b/puppet/modules/stdlib/spec/functions/time_spec.rb new file mode 100755 index 00000000..6e22515f --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/time_spec.rb @@ -0,0 +1,29 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the time function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("time")).to eq("function_time") + end + + it "should raise a ParseError if there is more than 2 arguments" do + expect { scope.function_time(['','']) }.to( raise_error(Puppet::ParseError)) + end + + it "should return a number" do + result = scope.function_time([]) + expect(result).to be_an(Integer) + end + + it "should be higher then when I wrote this test" do + result = scope.function_time([]) + expect(result).to(be > 1311953157) + end + + it "should be lower then 1.5 trillion" do + result = scope.function_time([]) + expect(result).to(be < 1500000000) + end +end diff --git a/puppet/modules/stdlib/spec/functions/to_bytes_spec.rb b/puppet/modules/stdlib/spec/functions/to_bytes_spec.rb new file mode 100755 index 00000000..0f6ade91 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/to_bytes_spec.rb @@ -0,0 +1,83 @@ +#! /usr/bin/env ruby -S rspec + +require 'spec_helper' + +describe "the to_bytes function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("to_bytes")).to eq("function_to_bytes") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_to_bytes([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should convert kB to B" do + result = scope.function_to_bytes(["4 kB"]) + expect(result).to(eq(4096)) + end + + it "should convert MB to B" do + result = scope.function_to_bytes(["4 MB"]) + expect(result).to(eq(4194304)) + end + + it "should convert GB to B" do + result = scope.function_to_bytes(["4 GB"]) + expect(result).to(eq(4294967296)) + end + + it "should convert TB to B" do + result = scope.function_to_bytes(["4 TB"]) + expect(result).to(eq(4398046511104)) + end + + it "should convert PB to B" do + result = scope.function_to_bytes(["4 PB"]) + expect(result).to(eq(4503599627370496)) + end + + it "should convert PB to B" do + result = scope.function_to_bytes(["4 EB"]) + expect(result).to(eq(4611686018427387904)) + end + + it "should work without B in unit" do + result = scope.function_to_bytes(["4 k"]) + expect(result).to(eq(4096)) + end + + it "should work without a space before unit" do + result = scope.function_to_bytes(["4k"]) + expect(result).to(eq(4096)) + end + + it "should work without a unit" do + result = scope.function_to_bytes(["5678"]) + expect(result).to(eq(5678)) + end + + it "should convert fractions" do + result = scope.function_to_bytes(["1.5 kB"]) + expect(result).to(eq(1536)) + end + + it "should convert scientific notation" do + result = scope.function_to_bytes(["1.5e2 B"]) + expect(result).to(eq(150)) + end + + it "should do nothing with a positive number" do + result = scope.function_to_bytes([5678]) + expect(result).to(eq(5678)) + end + + it "should should raise a ParseError if input isn't a number" do + expect { scope.function_to_bytes(["foo"]) }.to( raise_error(Puppet::ParseError)) + end + + it "should should raise a ParseError if prefix is unknown" do + expect { scope.function_to_bytes(["5 uB"]) }.to( raise_error(Puppet::ParseError)) + end +end diff --git a/puppet/modules/stdlib/spec/functions/type3x_spec.rb b/puppet/modules/stdlib/spec/functions/type3x_spec.rb new file mode 100644 index 00000000..d21236a6 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/type3x_spec.rb @@ -0,0 +1,43 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the type3x function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + it "should exist" do + expect(Puppet::Parser::Functions.function("type3x")).to eq("function_type3x") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_type3x([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should return string when given a string" do + result = scope.function_type3x(["aaabbbbcccc"]) + expect(result).to(eq('string')) + end + + it "should return array when given an array" do + result = scope.function_type3x([["aaabbbbcccc","asdf"]]) + expect(result).to(eq('array')) + end + + it "should return hash when given a hash" do + result = scope.function_type3x([{"a"=>1,"b"=>2}]) + expect(result).to(eq('hash')) + end + + it "should return integer when given an integer" do + result = scope.function_type3x(["1"]) + expect(result).to(eq('integer')) + end + + it "should return float when given a float" do + result = scope.function_type3x(["1.34"]) + expect(result).to(eq('float')) + end + + it "should return boolean when given a boolean" do + result = scope.function_type3x([true]) + expect(result).to(eq('boolean')) + end +end diff --git a/puppet/modules/stdlib/spec/functions/type_spec.rb b/puppet/modules/stdlib/spec/functions/type_spec.rb new file mode 100755 index 00000000..b683fcfa --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/type_spec.rb @@ -0,0 +1,44 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the type function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + it "should exist" do + expect(Puppet::Parser::Functions.function("type")).to eq("function_type") + end + + it "should give a deprecation warning when called" do + scope.expects(:warning).with("type() DEPRECATED: This function will cease to function on Puppet 4; please use type3x() before upgrading to puppet 4 for backwards-compatibility, or migrate to the new parser's typing system.") + scope.function_type(["aoeu"]) + end + + it "should return string when given a string" do + result = scope.function_type(["aaabbbbcccc"]) + expect(result).to(eq('string')) + end + + it "should return array when given an array" do + result = scope.function_type([["aaabbbbcccc","asdf"]]) + expect(result).to(eq('array')) + end + + it "should return hash when given a hash" do + result = scope.function_type([{"a"=>1,"b"=>2}]) + expect(result).to(eq('hash')) + end + + it "should return integer when given an integer" do + result = scope.function_type(["1"]) + expect(result).to(eq('integer')) + end + + it "should return float when given a float" do + result = scope.function_type(["1.34"]) + expect(result).to(eq('float')) + end + + it "should return boolean when given a boolean" do + result = scope.function_type([true]) + expect(result).to(eq('boolean')) + end +end diff --git a/puppet/modules/stdlib/spec/functions/union_spec.rb b/puppet/modules/stdlib/spec/functions/union_spec.rb new file mode 100755 index 00000000..706f4cbc --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/union_spec.rb @@ -0,0 +1,19 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the union function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("union")).to eq("function_union") + end + + it "should raise a ParseError if there are fewer than 2 arguments" do + expect { scope.function_union([]) }.to( raise_error(Puppet::ParseError) ) + end + + it "should join two arrays together" do + result = scope.function_union([["a","b","c"],["b","c","d"]]) + expect(result).to(eq(["a","b","c","d"])) + end +end diff --git a/puppet/modules/stdlib/spec/functions/unique_spec.rb b/puppet/modules/stdlib/spec/functions/unique_spec.rb new file mode 100755 index 00000000..7cd3a566 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/unique_spec.rb @@ -0,0 +1,33 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the unique function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("unique")).to eq("function_unique") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_unique([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should remove duplicate elements in a string" do + result = scope.function_unique(["aabbc"]) + expect(result).to(eq('abc')) + end + + it "should remove duplicate elements in an array" do + result = scope.function_unique([["a","a","b","b","c"]]) + expect(result).to(eq(['a','b','c'])) + end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new('aabbc') + result = scope.function_unique([value]) + result.should(eq('abc')) + end +end diff --git a/puppet/modules/stdlib/spec/functions/upcase_spec.rb b/puppet/modules/stdlib/spec/functions/upcase_spec.rb new file mode 100755 index 00000000..3cf8b055 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/upcase_spec.rb @@ -0,0 +1,33 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the upcase function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("upcase")).to eq("function_upcase") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_upcase([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should upcase a string" do + result = scope.function_upcase(["abc"]) + expect(result).to(eq('ABC')) + end + + it "should do nothing if a string is already upcase" do + result = scope.function_upcase(["ABC"]) + expect(result).to(eq('ABC')) + end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new('abc') + result = scope.function_upcase([value]) + result.should(eq('ABC')) + end +end diff --git a/puppet/modules/stdlib/spec/functions/uriescape_spec.rb b/puppet/modules/stdlib/spec/functions/uriescape_spec.rb new file mode 100755 index 00000000..2321e5ab --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/uriescape_spec.rb @@ -0,0 +1,33 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the uriescape function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("uriescape")).to eq("function_uriescape") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_uriescape([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should uriescape a string" do + result = scope.function_uriescape([":/?#[]@!$&'()*+,;= \"{}"]) + expect(result).to(eq(':/?%23[]@!$&\'()*+,;=%20%22%7B%7D')) + end + + it "should do nothing if a string is already safe" do + result = scope.function_uriescape(["ABCdef"]) + expect(result).to(eq('ABCdef')) + end + + it "should accept objects which extend String" do + class AlsoString < String + end + + value = AlsoString.new('abc') + result = scope.function_uriescape([value]) + result.should(eq('abc')) + end +end diff --git a/puppet/modules/stdlib/spec/functions/validate_absolute_path_spec.rb b/puppet/modules/stdlib/spec/functions/validate_absolute_path_spec.rb new file mode 100755 index 00000000..36c836bd --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/validate_absolute_path_spec.rb @@ -0,0 +1,104 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe Puppet::Parser::Functions.function(:validate_absolute_path) do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + # The subject of these examples is the method itself. + subject do + # This makes sure the function is loaded within each test + function_name = Puppet::Parser::Functions.function(:validate_absolute_path) + scope.method(function_name) + end + + describe "Valid Paths" do + def self.valid_paths + %w{ + C:/ + C:\\ + C:\\WINDOWS\\System32 + C:/windows/system32 + X:/foo/bar + X:\\foo\\bar + /var/tmp + /var/lib/puppet + /var/opt/../lib/puppet + } + end + + context "Without Puppet::Util.absolute_path? (e.g. Puppet <= 2.6)" do + before :each do + # The intent here is to mock Puppet to behave like Puppet 2.6 does. + # Puppet 2.6 does not have the absolute_path? method. This is only a + # convenience test, stdlib should be run with the Puppet 2.6.x in the + # $LOAD_PATH in addition to 2.7.x and master. + Puppet::Util.expects(:respond_to?).with(:absolute_path?).returns(false) + end + valid_paths.each do |path| + it "validate_absolute_path(#{path.inspect}) should not fail" do + expect { subject.call [path] }.not_to raise_error + end + end + valid_paths do + it "validate_absolute_path(#{valid_paths.inspect}) should not fail" do + expect { subject.call [valid_paths] }.not_to raise_error + end + end + end + + context "Puppet without mocking" do + valid_paths.each do |path| + it "validate_absolute_path(#{path.inspect}) should not fail" do + expect { subject.call [path] }.not_to raise_error + end + end + valid_paths do + it "validate_absolute_path(#{valid_paths.inspect}) should not fail" do + expect { subject.call [valid_paths] }.not_to raise_error + end + end + end + end + + describe 'Invalid paths' do + context 'Garbage inputs' do + [ + nil, + [ nil ], + [ nil, nil ], + { 'foo' => 'bar' }, + { }, + '', + ].each do |path| + it "validate_absolute_path(#{path.inspect}) should fail" do + expect { subject.call [path] }.to raise_error Puppet::ParseError + end + end + end + + context 'Relative paths' do + def self.rel_paths + %w{ + relative1 + . + .. + ./foo + ../foo + etc/puppetlabs/puppet + opt/puppet/bin + } + end + rel_paths.each do |path| + it "validate_absolute_path(#{path.inspect}) should fail" do + expect { subject.call [path] }.to raise_error Puppet::ParseError + end + end + rel_paths do + it "validate_absolute_path(#{rel_paths.inspect}) should fail" do + expect { subject.call [rel_paths] }.to raise_error Puppet::ParseError + end + end + end + end +end + diff --git a/puppet/modules/stdlib/spec/functions/validate_array_spec.rb b/puppet/modules/stdlib/spec/functions/validate_array_spec.rb new file mode 100755 index 00000000..4b31cfde --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/validate_array_spec.rb @@ -0,0 +1,38 @@ +#! /usr/bin/env ruby -S rspec + +require 'spec_helper' + +describe Puppet::Parser::Functions.function(:validate_array) do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + describe 'when calling validate_array from puppet' do + + %w{ true false }.each do |the_string| + it "should not compile when #{the_string} is a string" do + Puppet[:code] = "validate_array('#{the_string}')" + expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not an Array/) + end + + it "should not compile when #{the_string} is a bare word" do + Puppet[:code] = "validate_array(#{the_string})" + expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not an Array/) + end + end + + it "should compile when multiple array arguments are passed" do + Puppet[:code] = <<-'ENDofPUPPETcode' + $foo = [ ] + $bar = [ 'one', 'two' ] + validate_array($foo, $bar) + ENDofPUPPETcode + scope.compiler.compile + end + + it "should not compile when an undef variable is passed" do + Puppet[:code] = <<-'ENDofPUPPETcode' + $foo = undef + validate_array($foo) + ENDofPUPPETcode + expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not an Array/) + end + end +end diff --git a/puppet/modules/stdlib/spec/functions/validate_augeas_spec.rb b/puppet/modules/stdlib/spec/functions/validate_augeas_spec.rb new file mode 100755 index 00000000..c695ba2e --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/validate_augeas_spec.rb @@ -0,0 +1,103 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe Puppet::Parser::Functions.function(:validate_augeas), :if => Puppet.features.augeas? do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + # The subject of these examplres is the method itself. + subject do + # This makes sure the function is loaded within each test + function_name = Puppet::Parser::Functions.function(:validate_augeas) + scope.method(function_name) + end + + context 'Using Puppet::Parser::Scope.new' do + + describe 'Garbage inputs' do + inputs = [ + [ nil ], + [ [ nil ] ], + [ { 'foo' => 'bar' } ], + [ { } ], + [ '' ], + [ "one", "one", "MSG to User", "4th arg" ], + ] + + inputs.each do |input| + it "validate_augeas(#{input.inspect}) should fail" do + expect { subject.call [input] }.to raise_error Puppet::ParseError + end + end + 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 "validate_augeas(#{input.inspect}) should not fail" do + expect { subject.call input }.not_to raise_error + end + end + end + + describe "Valid inputs which should raise an exception without a message" 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 "validate_augeas(#{input.inspect}) should fail" do + expect { subject.call input }.to raise_error /validate_augeas.*?matched less than it should/ + end + end + end + + describe "Nicer Error Messages" do + # The intent here is to make sure the function returns the 3rd 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 "validate_augeas(#{input.inspect}) should fail" do + expect { subject.call input }.to raise_error /#{input[2]}/ + end + end + end + + describe "Passing simple unit 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 "validate_augeas(#{input.inspect}) should fail" do + expect { subject.call input }.not_to raise_error + end + end + end + + describe "Failing simple unit 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 "validate_augeas(#{input.inspect}) should fail" do + expect { subject.call input }.to raise_error /testing path/ + end + end + end + end +end diff --git a/puppet/modules/stdlib/spec/functions/validate_bool_spec.rb b/puppet/modules/stdlib/spec/functions/validate_bool_spec.rb new file mode 100755 index 00000000..a352d3b5 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/validate_bool_spec.rb @@ -0,0 +1,51 @@ +#! /usr/bin/env ruby -S rspec + +require 'spec_helper' + +describe Puppet::Parser::Functions.function(:validate_bool) do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + describe 'when calling validate_bool from puppet' do + + %w{ true false }.each do |the_string| + + it "should not compile when #{the_string} is a string" do + Puppet[:code] = "validate_bool('#{the_string}')" + expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a boolean/) + end + + it "should compile when #{the_string} is a bare word" do + Puppet[:code] = "validate_bool(#{the_string})" + scope.compiler.compile + end + + end + + it "should not compile when an arbitrary string is passed" do + Puppet[:code] = 'validate_bool("jeff and dan are awesome")' + expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a boolean/) + end + + it "should not compile when no arguments are passed" do + Puppet[:code] = 'validate_bool()' + expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /wrong number of arguments/) + end + + it "should compile when multiple boolean arguments are passed" do + Puppet[:code] = <<-'ENDofPUPPETcode' + $foo = true + $bar = false + validate_bool($foo, $bar, true, false) + ENDofPUPPETcode + scope.compiler.compile + end + + it "should compile when multiple boolean arguments are passed" do + Puppet[:code] = <<-'ENDofPUPPETcode' + $foo = true + $bar = false + validate_bool($foo, $bar, true, false, 'jeff') + ENDofPUPPETcode + expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a boolean/) + end + end +end diff --git a/puppet/modules/stdlib/spec/functions/validate_cmd_spec.rb b/puppet/modules/stdlib/spec/functions/validate_cmd_spec.rb new file mode 100755 index 00000000..7cb9782d --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/validate_cmd_spec.rb @@ -0,0 +1,85 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +TESTEXE = File.exists?('/usr/bin/test') ? '/usr/bin/test' : '/bin/test' +TOUCHEXE = File.exists?('/usr/bin/touch') ? '/usr/bin/touch' : '/bin/touch' + +describe Puppet::Parser::Functions.function(:validate_cmd) do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + subject do + function_name = Puppet::Parser::Functions.function(:validate_cmd) + scope.method(function_name) + end + + context 'with no % placeholder' do + describe "with an explicit failure message" do + it "prints the failure message on error" do + expect { + subject.call ['', '/bin/false', 'failure message!'] + }.to raise_error Puppet::ParseError, /failure message!/ + end + end + + describe "on validation failure" do + it "includes the command error output" do + expect { + subject.call ['', "#{TOUCHEXE} /cant/touch/this"] + }.to raise_error Puppet::ParseError, /(cannot touch|o such file or)/ + end + + it "includes the command return value" do + expect { + subject.call ['', '/cant/run/this'] + }.to raise_error Puppet::ParseError, /returned 1\b/ + end + end + + describe "when performing actual validation" do + it "can positively validate file content" do + expect { subject.call ["non-empty", "#{TESTEXE} -s"] }.to_not raise_error + end + + it "can negatively validate file content" do + expect { + subject.call ["", "#{TESTEXE} -s"] + }.to raise_error Puppet::ParseError, /failed to validate.*test -s/ + end + end + end + + context 'with % placeholder' do + describe "with an explicit failure message" do + it "prints the failure message on error" do + expect { + subject.call ['', '/bin/false % -f', 'failure message!'] + }.to raise_error Puppet::ParseError, /failure message!/ + end + end + describe "on validation failure" do + it "includes the command error output" do + expect { + subject.call ['', "#{TOUCHEXE} /cant/touch/this"] + }.to raise_error Puppet::ParseError, /(cannot touch|o such file or)/ + end + + it "includes the command return value" do + expect { + subject.call ['', '/cant/run/this % -z'] + }.to raise_error Puppet::ParseError, /Execution of '\/cant\/run\/this .+ -z' returned 1/ + end + end + + describe "when performing actual validation" do + it "can positively validate file content" do + expect { subject.call ["non-empty", "#{TESTEXE} -s %"] }.to_not raise_error + end + + it "can negatively validate file content" do + expect { + subject.call ["", "#{TESTEXE} -s %"] + }.to raise_error Puppet::ParseError, /failed to validate.*test -s/ + end + end + end +end diff --git a/puppet/modules/stdlib/spec/functions/validate_hash_spec.rb b/puppet/modules/stdlib/spec/functions/validate_hash_spec.rb new file mode 100755 index 00000000..a0c35c23 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/validate_hash_spec.rb @@ -0,0 +1,43 @@ +#! /usr/bin/env ruby -S rspec + +require 'spec_helper' + +describe Puppet::Parser::Functions.function(:validate_hash) do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + describe 'when calling validate_hash from puppet' do + + %w{ true false }.each do |the_string| + + it "should not compile when #{the_string} is a string" do + Puppet[:code] = "validate_hash('#{the_string}')" + expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a Hash/) + end + + it "should not compile when #{the_string} is a bare word" do + Puppet[:code] = "validate_hash(#{the_string})" + expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a Hash/) + end + + end + + it "should compile when multiple hash arguments are passed" do + Puppet[:code] = <<-'ENDofPUPPETcode' + $foo = {} + $bar = { 'one' => 'two' } + validate_hash($foo, $bar) + ENDofPUPPETcode + scope.compiler.compile + end + + it "should not compile when an undef variable is passed" do + Puppet[:code] = <<-'ENDofPUPPETcode' + $foo = undef + validate_hash($foo) + ENDofPUPPETcode + expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a Hash/) + end + + end + +end diff --git a/puppet/modules/stdlib/spec/functions/validate_ipv4_address_spec.rb b/puppet/modules/stdlib/spec/functions/validate_ipv4_address_spec.rb new file mode 100755 index 00000000..45401a42 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/validate_ipv4_address_spec.rb @@ -0,0 +1,64 @@ +#! /usr/bin/env ruby -S rspec + +require "spec_helper" + +describe Puppet::Parser::Functions.function(:validate_ipv4_address) do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + describe "when calling validate_ipv4_address from puppet" do + describe "when given IPv4 address strings" do + it "should compile with one argument" do + Puppet[:code] = "validate_ipv4_address('1.2.3.4')" + scope.compiler.compile + end + + it "should compile with multiple arguments" do + Puppet[:code] = "validate_ipv4_address('1.2.3.4', '5.6.7.8')" + scope.compiler.compile + end + end + + describe "when given an IPv6 address" do + it "should not compile" do + Puppet[:code] = "validate_ipv4_address('3ffe:505')" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /not a valid IPv4 address/) + end + end + + describe "when given other strings" do + it "should not compile" do + Puppet[:code] = "validate_ipv4_address('hello', 'world')" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /not a valid IPv4 address/) + end + end + + describe "when given numbers" do + it "should not compile" do + Puppet[:code] = "validate_ipv4_address(1, 2)" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /is not a valid IPv4 address/) + end + end + + describe "when given booleans" do + it "should not compile" do + Puppet[:code] = "validate_ipv4_address(true, false)" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /is not a string/) + end + end + + it "should not compile when no arguments are passed" do + Puppet[:code] = "validate_ipv4_address()" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /wrong number of arguments/) + end + end +end diff --git a/puppet/modules/stdlib/spec/functions/validate_ipv6_address_spec.rb b/puppet/modules/stdlib/spec/functions/validate_ipv6_address_spec.rb new file mode 100755 index 00000000..a839d902 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/validate_ipv6_address_spec.rb @@ -0,0 +1,67 @@ +#! /usr/bin/env ruby -S rspec + +require "spec_helper" + +describe Puppet::Parser::Functions.function(:validate_ipv6_address) do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + describe "when calling validate_ipv6_address from puppet" do + describe "when given IPv6 address strings" do + it "should compile with one argument" do + Puppet[:code] = "validate_ipv6_address('3ffe:0505:0002::')" + scope.compiler.compile + end + + it "should compile with multiple arguments" do + Puppet[:code] = "validate_ipv6_address('3ffe:0505:0002::', '3ffe:0505:0001::')" + scope.compiler.compile + end + end + + describe "when given an ipv4 address" do + it "should not compile" do + Puppet[:code] = "validate_ipv6_address('1.2.3.4')" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /not a valid IPv6 address/) + end + end + + describe "when given other strings" do + it "should not compile" do + Puppet[:code] = "validate_ipv6_address('hello', 'world')" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /not a valid IPv6 address/) + end + end + + # 1.8.7 is EOL'd and also absolutely insane about ipv6 + unless RUBY_VERSION == '1.8.7' + describe "when given numbers" do + it "should not compile" do + Puppet[:code] = "validate_ipv6_address(1, 2)" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /not a valid IPv6 address/) + end + end + end + + describe "when given booleans" do + it "should not compile" do + Puppet[:code] = "validate_ipv6_address(true, false)" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /is not a string/) + end + end + + it "should not compile when no arguments are passed" do + Puppet[:code] = "validate_ipv6_address()" + expect { + scope.compiler.compile + }.to raise_error(Puppet::ParseError, /wrong number of arguments/) + end + end +end diff --git a/puppet/modules/stdlib/spec/functions/validate_re_spec.rb b/puppet/modules/stdlib/spec/functions/validate_re_spec.rb new file mode 100755 index 00000000..d29988bf --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/validate_re_spec.rb @@ -0,0 +1,77 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe Puppet::Parser::Functions.function(:validate_re) do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + # The subject of these examplres is the method itself. + subject do + # This makes sure the function is loaded within each test + function_name = Puppet::Parser::Functions.function(:validate_re) + scope.method(function_name) + end + + context 'Using Puppet::Parser::Scope.new' do + + describe 'Garbage inputs' do + inputs = [ + [ nil ], + [ [ nil ] ], + [ { 'foo' => 'bar' } ], + [ { } ], + [ '' ], + [ "one", "one", "MSG to User", "4th arg" ], + ] + + inputs.each do |input| + it "validate_re(#{input.inspect}) should fail" do + expect { subject.call [input] }.to raise_error Puppet::ParseError + end + end + end + + describe 'Valid inputs' do + inputs = [ + [ '/full/path/to/something', '^/full' ], + [ '/full/path/to/something', 'full' ], + [ '/full/path/to/something', ['full', 'absent'] ], + [ '/full/path/to/something', ['full', 'absent'], 'Message to the user' ], + ] + + inputs.each do |input| + it "validate_re(#{input.inspect}) should not fail" do + expect { subject.call input }.not_to raise_error + end + end + end + describe "Valid inputs which should raise an exception without a message" 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 = [ + [ "hello", [ "bye", "later", "adios" ] ], + [ "greetings", "salutations" ], + ] + + inputs.each do |input| + it "validate_re(#{input.inspect}) should fail" do + expect { subject.call input }.to raise_error /validate_re.*?does not match/ + end + end + end + describe "Nicer Error Messages" do + # The intent here is to make sure the function returns the 3rd argument + # in the exception thrown + inputs = [ + [ "hello", [ "bye", "later", "adios" ], "MSG to User" ], + [ "greetings", "salutations", "Error, greetings does not match salutations" ], + ] + + inputs.each do |input| + it "validate_re(#{input.inspect}) should fail" do + expect { subject.call input }.to raise_error /#{input[2]}/ + end + end + end + end +end diff --git a/puppet/modules/stdlib/spec/functions/validate_slength_spec.rb b/puppet/modules/stdlib/spec/functions/validate_slength_spec.rb new file mode 100755 index 00000000..e23f61a2 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/validate_slength_spec.rb @@ -0,0 +1,67 @@ +#! /usr/bin/env ruby -S rspec + +require 'spec_helper' + +describe "the validate_slength function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("validate_slength")).to eq("function_validate_slength") + end + + describe "validating the input argument types" do + it "raises an error if there are less than two arguments" do + expect { scope.function_validate_slength([]) }.to raise_error Puppet::ParseError, /Wrong number of arguments/ + end + + it "raises an error if there are more than three arguments" do + expect { scope.function_validate_slength(['input', 1, 2, 3]) }.to raise_error Puppet::ParseError, /Wrong number of arguments/ + end + + it "raises an error if the first argument is not a string" do + expect { scope.function_validate_slength([Object.new, 2, 1]) }.to raise_error Puppet::ParseError, /Expected first argument.*got .*Object/ + end + + it "raises an error if the second argument cannot be cast to an Integer" do + expect { scope.function_validate_slength(['input', Object.new]) }.to raise_error Puppet::ParseError, /Expected second argument.*got .*Object/ + end + + it "raises an error if the third argument cannot be cast to an Integer" do + expect { scope.function_validate_slength(['input', 1, Object.new]) }.to raise_error Puppet::ParseError, /Expected third argument.*got .*Object/ + end + + it "raises an error if the second argument is smaller than the third argument" do + expect { scope.function_validate_slength(['input', 1, 2]) }.to raise_error Puppet::ParseError, /Expected second argument to be larger than third argument/ + end + end + + describe "validating the input string length" do + describe "when the input is a string" do + it "fails validation if the string is larger than the max length" do + expect { scope.function_validate_slength(['input', 1]) }.to raise_error Puppet::ParseError, /Expected length .* between 0 and 1, was 5/ + end + + it "fails validation if the string is less than the min length" do + expect { scope.function_validate_slength(['input', 10, 6]) }.to raise_error Puppet::ParseError, /Expected length .* between 6 and 10, was 5/ + end + + it "doesn't raise an error if the string is under the max length" do + scope.function_validate_slength(['input', 10]) + end + + it "doesn't raise an error if the string is equal to the max length" do + scope.function_validate_slength(['input', 5]) + end + + it "doesn't raise an error if the string is equal to the min length" do + scope.function_validate_slength(['input', 10, 5]) + end + end + + describe "when the input is an array" do + it "fails validation if one of the array elements is not a string" do + expect { scope.function_validate_slength([["a", "b", Object.new], 2]) }.to raise_error Puppet::ParseError, /Expected element at array position 2 .*String, got .*Object/ + end + end + end +end diff --git a/puppet/modules/stdlib/spec/functions/validate_string_spec.rb b/puppet/modules/stdlib/spec/functions/validate_string_spec.rb new file mode 100755 index 00000000..3b4fb3e1 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/validate_string_spec.rb @@ -0,0 +1,60 @@ +#! /usr/bin/env ruby -S rspec + +require 'spec_helper' + +describe Puppet::Parser::Functions.function(:validate_string) do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + describe 'when calling validate_string from puppet' do + + %w{ foo bar baz }.each do |the_string| + + it "should compile when #{the_string} is a string" do + Puppet[:code] = "validate_string('#{the_string}')" + scope.compiler.compile + end + + it "should compile when #{the_string} is a bare word" do + Puppet[:code] = "validate_string(#{the_string})" + scope.compiler.compile + end + + end + + %w{ true false }.each do |the_string| + it "should compile when #{the_string} is a string" do + Puppet[:code] = "validate_string('#{the_string}')" + scope.compiler.compile + end + + it "should not compile when #{the_string} is a bare word" do + Puppet[:code] = "validate_string(#{the_string})" + expect { scope.compiler.compile }.to raise_error(Puppet::ParseError, /is not a string/) + end + end + + it "should compile when multiple string arguments are passed" do + Puppet[:code] = <<-'ENDofPUPPETcode' + $foo = '' + $bar = 'two' + validate_string($foo, $bar) + ENDofPUPPETcode + scope.compiler.compile + end + + it "should compile when an explicitly undef variable is passed (NOTE THIS MAY NOT BE DESIRABLE)" do + Puppet[:code] = <<-'ENDofPUPPETcode' + $foo = undef + validate_string($foo) + ENDofPUPPETcode + scope.compiler.compile + end + + it "should compile when an undefined variable is passed (NOTE THIS MAY NOT BE DESIRABLE)" do + Puppet[:code] = <<-'ENDofPUPPETcode' + validate_string($foobarbazishouldnotexist) + ENDofPUPPETcode + scope.compiler.compile + end + end +end diff --git a/puppet/modules/stdlib/spec/functions/values_at_spec.rb b/puppet/modules/stdlib/spec/functions/values_at_spec.rb new file mode 100755 index 00000000..86e3c31c --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/values_at_spec.rb @@ -0,0 +1,38 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the values_at function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("values_at")).to eq("function_values_at") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_values_at([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should raise a ParseError if you try to use a range where stop is greater then start" do + expect { scope.function_values_at([['a','b'],["3-1"]]) }.to( raise_error(Puppet::ParseError)) + end + + it "should return a value at from an array" do + result = scope.function_values_at([['a','b','c'],"1"]) + expect(result).to(eq(['b'])) + end + + it "should return a value at from an array when passed a range" do + result = scope.function_values_at([['a','b','c'],"0-1"]) + expect(result).to(eq(['a','b'])) + end + + it "should return chosen values from an array when passed number of indexes" do + result = scope.function_values_at([['a','b','c'],["0","2"]]) + expect(result).to(eq(['a','c'])) + end + + it "should return chosen values from an array when passed ranges and multiple indexes" do + result = scope.function_values_at([['a','b','c','d','e','f','g'],["0","2","4-5"]]) + expect(result).to(eq(['a','c','e','f'])) + end +end diff --git a/puppet/modules/stdlib/spec/functions/values_spec.rb b/puppet/modules/stdlib/spec/functions/values_spec.rb new file mode 100755 index 00000000..08d21b03 --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/values_spec.rb @@ -0,0 +1,31 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the values function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + expect(Puppet::Parser::Functions.function("values")).to eq("function_values") + end + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_values([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should return values from a hash" do + result = scope.function_values([{'a'=>'1','b'=>'2','c'=>'3'}]) + # =~ is the RSpec::Matchers::MatchArray matcher. + # A.K.A. "array with same elements" (multiset) matching + expect(result).to match_array(%w{ 1 2 3 }) + end + + it "should return a multiset" do + result = scope.function_values([{'a'=>'1','b'=>'3','c'=>'3'}]) + expect(result).to match_array(%w{ 1 3 3 }) + expect(result).not_to match_array(%w{ 1 3 }) + end + + it "should raise a ParseError unless a Hash is provided" do + expect { scope.function_values([['a','b','c']]) }.to( raise_error(Puppet::ParseError)) + end +end diff --git a/puppet/modules/stdlib/spec/functions/zip_spec.rb b/puppet/modules/stdlib/spec/functions/zip_spec.rb new file mode 100755 index 00000000..f265fcee --- /dev/null +++ b/puppet/modules/stdlib/spec/functions/zip_spec.rb @@ -0,0 +1,31 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the zip function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should raise a ParseError if there is less than 1 arguments" do + expect { scope.function_zip([]) }.to( raise_error(Puppet::ParseError)) + end + + it "should be able to zip an array" do + result = scope.function_zip([['1','2','3'],['4','5','6']]) + expect(result).to(eq([["1", "4"], ["2", "5"], ["3", "6"]])) + result = scope.function_zip([['1','2','3'],['4','5','6'], false]) + result.should(eq([["1", "4"], ["2", "5"], ["3", "6"]])) + end + + it "should be able to zip an array and flatten" do + result = scope.function_zip([['1','2','3'],['4','5','6'], true]) + result.should(eq(["1", "4", "2", "5", "3", "6"])) + end + + it "should accept objects which extend String for the second argument" do + class AlsoString < String + end + + value = AlsoString.new('false') + result = scope.function_zip([['1','2','3'],['4','5','6'],value]) + result.should(eq([["1", "4"], ["2", "5"], ["3", "6"]])) + end +end |