summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rwxr-xr-xspec/acceptance/ensure_packages_spec.rb22
-rwxr-xr-xspec/acceptance/ensure_resource_spec.rb2
-rw-r--r--spec/acceptance/fqdn_rand_string_spec.rb111
-rwxr-xr-xspec/acceptance/fqdn_rotate_spec.rb107
-rw-r--r--spec/acceptance/pw_hash_spec.rb2
-rwxr-xr-xspec/acceptance/try_get_value_spec.rb47
-rwxr-xr-xspec/acceptance/union_spec.rb5
-rw-r--r--spec/fixtures/lsuser/root2
-rwxr-xr-xspec/functions/base64_spec.rb1
-rw-r--r--spec/functions/convert_base_spec.rb24
-rw-r--r--spec/functions/dos2unix_spec.rb40
-rwxr-xr-xspec/functions/load_module_metadata.rb16
-rwxr-xr-xspec/functions/size_spec.rb9
-rwxr-xr-xspec/functions/str2bool_spec.rb4
-rw-r--r--spec/functions/try_get_value_spec.rb100
-rwxr-xr-xspec/functions/union_spec.rb9
-rw-r--r--spec/functions/unix2dos_spec.rb40
-rwxr-xr-xspec/functions/validate_slength_spec.rb2
-rwxr-xr-xspec/spec_helper_acceptance.rb54
-rwxr-xr-xspec/unit/facter/root_home_spec.rb13
-rwxr-xr-xspec/unit/puppet/provider/file_line/ruby_spec.rb147
-rwxr-xr-xspec/unit/puppet/type/file_line_spec.rb3
22 files changed, 574 insertions, 186 deletions
diff --git a/spec/acceptance/ensure_packages_spec.rb b/spec/acceptance/ensure_packages_spec.rb
deleted file mode 100755
index aedcfb5..0000000
--- a/spec/acceptance/ensure_packages_spec.rb
+++ /dev/null
@@ -1,22 +0,0 @@
-#! /usr/bin/env ruby -S rspec
-require 'spec_helper_acceptance'
-
-describe 'ensure_packages function', :unless => fact('osfamily') =~ /windows/i do
- describe 'success' do
- it 'ensure_packages a package' do
- apply_manifest('package { "rake": ensure => absent, provider => "gem", }')
- pp = <<-EOS
- $a = "rake"
- ensure_packages($a,{'provider' => 'gem'})
- EOS
-
- apply_manifest(pp, :expect_changes => true)
- end
- it 'ensures a package already declared'
- it 'takes defaults arguments'
- end
- describe 'failure' do
- it 'handles no arguments'
- it 'handles non strings'
- end
-end
diff --git a/spec/acceptance/ensure_resource_spec.rb b/spec/acceptance/ensure_resource_spec.rb
index 1cee53d..fe619a9 100755
--- a/spec/acceptance/ensure_resource_spec.rb
+++ b/spec/acceptance/ensure_resource_spec.rb
@@ -1,7 +1,7 @@
#! /usr/bin/env ruby -S rspec
require 'spec_helper_acceptance'
-describe 'ensure_resource function', :unless => fact('osfamily') =~ /windows/i do
+describe 'ensure_resource function', :unless => fact('osfamily') =~ /(windows|Suse)/i do
describe 'success' do
it 'ensure_resource a package' do
apply_manifest('package { "rake": ensure => absent, provider => "gem", }')
diff --git a/spec/acceptance/fqdn_rand_string_spec.rb b/spec/acceptance/fqdn_rand_string_spec.rb
index 881cff3..9c6d701 100644
--- a/spec/acceptance/fqdn_rand_string_spec.rb
+++ b/spec/acceptance/fqdn_rand_string_spec.rb
@@ -3,80 +3,59 @@ require 'spec_helper_acceptance'
describe 'fqdn_rand_string function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
describe 'success' do
- let(:facts_d) do
- if fact('is_pe', '--puppet') == "true"
- if fact('osfamily') =~ /windows/i
- if fact('kernelmajversion').to_f < 6.0
- 'c:/documents and settings/all users/application data/puppetlabs/facter/facts.d'
- else
- 'c:/programdata/puppetlabs/facter/facts.d'
- end
- else
- '/etc/puppetlabs/facter/facts.d'
- end
- else
- '/etc/facter/facts.d'
- end
- end
- after :each do
- shell("if [ -f '#{facts_d}/fqdn.txt' ] ; then rm '#{facts_d}/fqdn.txt' ; fi")
- end
- before :each do
- #no need to create on windows, pe creates by default
- if fact('osfamily') !~ /windows/i
- shell("mkdir -p '#{facts_d}'")
+ include_context "with faked facts"
+ context "when the FQDN is 'fakehost.localdomain'" do
+ before :each do
+ fake_fact("fqdn", "fakehost.localdomain")
end
- end
- it 'generates random alphanumeric strings' do
- shell("echo fqdn=fakehost.localdomain > '#{facts_d}/fqdn.txt'")
- pp = <<-eos
- $l = 10
- $o = fqdn_rand_string($l)
- notice(inline_template('fqdn_rand_string is <%= @o.inspect %>'))
- eos
- apply_manifest(pp, :catch_failures => true) do |r|
- expect(r.stdout).to match(/fqdn_rand_string is "7oDp0KOr1b"/)
+ it 'generates random alphanumeric strings' do
+ pp = <<-eos
+ $l = 10
+ $o = fqdn_rand_string($l)
+ notice(inline_template('fqdn_rand_string is <%= @o.inspect %>'))
+ eos
+
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).to match(/fqdn_rand_string is "7oDp0KOr1b"/)
+ end
end
- end
- it 'generates random alphanumeric strings with custom charsets' do
- shell("echo fqdn=fakehost.localdomain > '#{facts_d}/fqdn.txt'")
- pp = <<-eos
- $l = 10
- $c = '0123456789'
- $o = fqdn_rand_string($l, $c)
- notice(inline_template('fqdn_rand_string is <%= @o.inspect %>'))
- eos
+ it 'generates random alphanumeric strings with custom charsets' do
+ pp = <<-eos
+ $l = 10
+ $c = '0123456789'
+ $o = fqdn_rand_string($l, $c)
+ notice(inline_template('fqdn_rand_string is <%= @o.inspect %>'))
+ eos
- apply_manifest(pp, :catch_failures => true) do |r|
- expect(r.stdout).to match(/fqdn_rand_string is "7203048515"/)
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).to match(/fqdn_rand_string is "7203048515"/)
+ end
end
- end
- it 'generates random alphanumeric strings with custom seeds' do
- shell("echo fqdn=fakehost.localdomain > '#{facts_d}/fqdn.txt'")
- pp = <<-eos
- $l = 10
- $s = 'seed'
- $o = fqdn_rand_string($l, undef, $s)
- notice(inline_template('fqdn_rand_string is <%= @o.inspect %>'))
- eos
+ it 'generates random alphanumeric strings with custom seeds' do
+ pp = <<-eos
+ $l = 10
+ $s = 'seed'
+ $o = fqdn_rand_string($l, undef, $s)
+ notice(inline_template('fqdn_rand_string is <%= @o.inspect %>'))
+ eos
- apply_manifest(pp, :catch_failures => true) do |r|
- expect(r.stdout).to match(/fqdn_rand_string is "3HS4mbuI3E"/)
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).to match(/fqdn_rand_string is "3HS4mbuI3E"/)
+ end
end
- end
- it 'generates random alphanumeric strings with custom charsets and seeds' do
- shell("echo fqdn=fakehost.localdomain > '#{facts_d}/fqdn.txt'")
- pp = <<-eos
- $l = 10
- $c = '0123456789'
- $s = 'seed'
- $o = fqdn_rand_string($l, $c, $s)
- notice(inline_template('fqdn_rand_string is <%= @o.inspect %>'))
- eos
+ it 'generates random alphanumeric strings with custom charsets and seeds' do
+ pp = <<-eos
+ $l = 10
+ $c = '0123456789'
+ $s = 'seed'
+ $o = fqdn_rand_string($l, $c, $s)
+ notice(inline_template('fqdn_rand_string is <%= @o.inspect %>'))
+ eos
- apply_manifest(pp, :catch_failures => true) do |r|
- expect(r.stdout).to match(/fqdn_rand_string is "3104058232"/)
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).to match(/fqdn_rand_string is "3104058232"/)
+ end
end
end
end
diff --git a/spec/acceptance/fqdn_rotate_spec.rb b/spec/acceptance/fqdn_rotate_spec.rb
index f1a15d3..404351f 100755
--- a/spec/acceptance/fqdn_rotate_spec.rb
+++ b/spec/acceptance/fqdn_rotate_spec.rb
@@ -3,78 +3,57 @@ require 'spec_helper_acceptance'
describe 'fqdn_rotate function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
describe 'success' do
- let(:facts_d) do
- if fact('is_pe', '--puppet') == "true"
- if fact('osfamily') =~ /windows/i
- if fact('kernelmajversion').to_f < 6.0
- 'C:/Documents and Settings/All Users/Application Data/PuppetLabs/facter/facts.d'
- else
- 'C:/ProgramData/PuppetLabs/facter/facts.d'
- end
- else
- '/etc/puppetlabs/facter/facts.d'
- end
- else
- '/etc/facter/facts.d'
- end
- end
- after :each do
- shell("if [ -f '#{facts_d}/fqdn.txt' ] ; then rm '#{facts_d}/fqdn.txt' ; fi")
- end
- before :each do
- #No need to create on windows, PE creates by default
- if fact('osfamily') !~ /windows/i
- shell("mkdir -p '#{facts_d}'")
+ include_context "with faked facts"
+ context "when the FQDN is 'fakehost.localdomain'" do
+ before :each do
+ fake_fact("fqdn", "fakehost.localdomain")
end
- end
- it 'rotates arrays' do
- shell("echo fqdn=fakehost.localdomain > '#{facts_d}/fqdn.txt'")
- pp = <<-EOS
- $a = ['a','b','c','d']
- $o = fqdn_rotate($a)
- notice(inline_template('fqdn_rotate is <%= @o.inspect %>'))
- EOS
- apply_manifest(pp, :catch_failures => true) do |r|
- expect(r.stdout).to match(/fqdn_rotate is \["d", "a", "b", "c"\]/)
+ it 'rotates arrays' do
+ pp = <<-EOS
+ $a = ['a','b','c','d']
+ $o = fqdn_rotate($a)
+ notice(inline_template('fqdn_rotate is <%= @o.inspect %>'))
+ EOS
+
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).to match(/fqdn_rotate is \["d", "a", "b", "c"\]/)
+ end
end
- end
- it 'rotates arrays with custom seeds' do
- shell("echo fqdn=fakehost.localdomain > '#{facts_d}/fqdn.txt'")
- pp = <<-EOS
- $a = ['a','b','c','d']
- $s = 'seed'
- $o = fqdn_rotate($a, $s)
- notice(inline_template('fqdn_rotate is <%= @o.inspect %>'))
- EOS
+ it 'rotates arrays with custom seeds' do
+ pp = <<-EOS
+ $a = ['a','b','c','d']
+ $s = 'seed'
+ $o = fqdn_rotate($a, $s)
+ notice(inline_template('fqdn_rotate is <%= @o.inspect %>'))
+ EOS
- apply_manifest(pp, :catch_failures => true) do |r|
- expect(r.stdout).to match(/fqdn_rotate is \["c", "d", "a", "b"\]/)
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).to match(/fqdn_rotate is \["c", "d", "a", "b"\]/)
+ end
end
- end
- it 'rotates strings' do
- shell("echo fqdn=fakehost.localdomain > '#{facts_d}/fqdn.txt'")
- pp = <<-EOS
- $a = 'abcd'
- $o = fqdn_rotate($a)
- notice(inline_template('fqdn_rotate is <%= @o.inspect %>'))
- EOS
+ it 'rotates strings' do
+ pp = <<-EOS
+ $a = 'abcd'
+ $o = fqdn_rotate($a)
+ notice(inline_template('fqdn_rotate is <%= @o.inspect %>'))
+ EOS
- apply_manifest(pp, :catch_failures => true) do |r|
- expect(r.stdout).to match(/fqdn_rotate is "dabc"/)
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).to match(/fqdn_rotate is "dabc"/)
+ end
end
- end
- it 'rotates strings with custom seeds' do
- shell("echo fqdn=fakehost.localdomain > '#{facts_d}/fqdn.txt'")
- pp = <<-EOS
- $a = 'abcd'
- $s = 'seed'
- $o = fqdn_rotate($a, $s)
- notice(inline_template('fqdn_rotate is <%= @o.inspect %>'))
- EOS
+ it 'rotates strings with custom seeds' do
+ pp = <<-EOS
+ $a = 'abcd'
+ $s = 'seed'
+ $o = fqdn_rotate($a, $s)
+ notice(inline_template('fqdn_rotate is <%= @o.inspect %>'))
+ EOS
- apply_manifest(pp, :catch_failures => true) do |r|
- expect(r.stdout).to match(/fqdn_rotate is "cdab"/)
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).to match(/fqdn_rotate is "cdab"/)
+ end
end
end
end
diff --git a/spec/acceptance/pw_hash_spec.rb b/spec/acceptance/pw_hash_spec.rb
index eddb782..cd4cb87 100644
--- a/spec/acceptance/pw_hash_spec.rb
+++ b/spec/acceptance/pw_hash_spec.rb
@@ -2,7 +2,7 @@
require 'spec_helper_acceptance'
# Windows and OS X do not have useful implementations of crypt(3)
-describe 'pw_hash function', :unless => (UNSUPPORTED_PLATFORMS + ['windows', 'Darwin']).include?(fact('operatingsystem')) do
+describe 'pw_hash function', :unless => (UNSUPPORTED_PLATFORMS + ['windows', 'Darwin', 'SLES']).include?(fact('operatingsystem')) do
describe 'success' do
it 'hashes passwords' do
pp = <<-EOS
diff --git a/spec/acceptance/try_get_value_spec.rb b/spec/acceptance/try_get_value_spec.rb
new file mode 100755
index 0000000..c0bf38a
--- /dev/null
+++ b/spec/acceptance/try_get_value_spec.rb
@@ -0,0 +1,47 @@
+#! /usr/bin/env ruby -S rspec
+require 'spec_helper_acceptance'
+
+describe 'try_get_value function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
+ describe 'success' do
+ it 'gets a value' do
+ pp = <<-EOS
+ $data = {
+ 'a' => { 'b' => 'passing'}
+ }
+
+ $tests = try_get_value($data, 'a/b')
+ notice(inline_template('tests are <%= @tests.inspect %>'))
+ EOS
+
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).to match(/tests are "passing"/)
+ end
+ end
+ end
+ describe 'failure' do
+ it 'uses a default value' do
+ pp = <<-EOS
+ $data = {
+ 'a' => { 'b' => 'passing'}
+ }
+
+ $tests = try_get_value($data, 'c/d', 'using the default value')
+ notice(inline_template('tests are <%= @tests.inspect %>'))
+ EOS
+
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).to match(/using the default value/)
+ end
+ end
+
+ it 'raises error on incorrect number of arguments' do
+ pp = <<-EOS
+ $o = try_get_value()
+ EOS
+
+ apply_manifest(pp, :expect_failures => true) do |r|
+ expect(r.stderr).to match(/wrong number of arguments/i)
+ end
+ end
+ end
+end
diff --git a/spec/acceptance/union_spec.rb b/spec/acceptance/union_spec.rb
index 6db8d0c..160fd7b 100755
--- a/spec/acceptance/union_spec.rb
+++ b/spec/acceptance/union_spec.rb
@@ -6,9 +6,10 @@ describe 'union function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('opera
it 'unions arrays' do
pp = <<-EOS
$a = ["the","public"]
- $b = ["art","galleries"]
+ $b = ["art"]
+ $c = ["galleries"]
# Anagram: Large picture halls, I bet
- $o = union($a,$b)
+ $o = union($a,$b,$c)
notice(inline_template('union is <%= @o.inspect %>'))
EOS
diff --git a/spec/fixtures/lsuser/root b/spec/fixtures/lsuser/root
new file mode 100644
index 0000000..afd59ca
--- /dev/null
+++ b/spec/fixtures/lsuser/root
@@ -0,0 +1,2 @@
+#name:home
+root:/root
diff --git a/spec/functions/base64_spec.rb b/spec/functions/base64_spec.rb
index 42512b3..c529e9e 100755
--- a/spec/functions/base64_spec.rb
+++ b/spec/functions/base64_spec.rb
@@ -10,6 +10,7 @@ describe 'base64' do
it { is_expected.to run.with_params("encode", 2).and_raise_error(Puppet::ParseError, /second argument must be a string/) }
it { is_expected.to run.with_params("encode", "thestring").and_return("dGhlc3RyaW5n\n") }
+ it { is_expected.to run.with_params("encode", "a very long string that will cause the base64 encoder to produce output with multiple lines").and_return("YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0\nIGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5l\ncw==\n") }
it { is_expected.to run.with_params("decode", "dGhlc3RyaW5n").and_return("thestring") }
it { is_expected.to run.with_params("decode", "dGhlc3RyaW5n\n").and_return("thestring") }
end
diff --git a/spec/functions/convert_base_spec.rb b/spec/functions/convert_base_spec.rb
new file mode 100644
index 0000000..8ab2284
--- /dev/null
+++ b/spec/functions/convert_base_spec.rb
@@ -0,0 +1,24 @@
+require 'spec_helper'
+
+describe 'convert_base' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(ArgumentError) }
+ it { is_expected.to run.with_params("asdf").and_raise_error(ArgumentError) }
+ it { is_expected.to run.with_params("asdf","moo","cow").and_raise_error(ArgumentError) }
+ it { is_expected.to run.with_params(["1"],"2").and_raise_error(Puppet::ParseError, /argument must be either a string or an integer/) }
+ it { is_expected.to run.with_params("1",["2"]).and_raise_error(Puppet::ParseError, /argument must be either a string or an integer/) }
+ it { is_expected.to run.with_params("1",1).and_raise_error(Puppet::ParseError, /base must be at least 2 and must not be greater than 36/) }
+ it { is_expected.to run.with_params("1",37).and_raise_error(Puppet::ParseError, /base must be at least 2 and must not be greater than 36/) }
+
+ it "should raise a ParseError if argument 1 is a string that does not correspond to an integer in base 10" do
+ is_expected.to run.with_params("ten",6).and_raise_error(Puppet::ParseError, /argument must be an integer or a string corresponding to an integer in base 10/)
+ end
+
+ it "should raise a ParseError if argument 2 is a string and does not correspond to an integer in base 10" do
+ is_expected.to run.with_params(100,"hex").and_raise_error(Puppet::ParseError, /argument must be an integer or a string corresponding to an integer in base 10/)
+ end
+
+ it { is_expected.to run.with_params("11",'16').and_return('b') }
+ it { is_expected.to run.with_params("35",'36').and_return('z') }
+ it { is_expected.to run.with_params(5, 2).and_return('101') }
+end
diff --git a/spec/functions/dos2unix_spec.rb b/spec/functions/dos2unix_spec.rb
new file mode 100644
index 0000000..9c84703
--- /dev/null
+++ b/spec/functions/dos2unix_spec.rb
@@ -0,0 +1,40 @@
+require 'spec_helper'
+
+describe 'dos2unix' do
+ context 'Checking parameter validity' do
+ it { is_expected.not_to eq(nil) }
+ it do
+ is_expected.to run.with_params.and_raise_error(ArgumentError, /Wrong number of arguments/)
+ end
+ it do
+ is_expected.to run.with_params('one', 'two').and_raise_error(ArgumentError, /Wrong number of arguments/)
+ end
+ it do
+ is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError)
+ end
+ it do
+ is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError)
+ end
+ it do
+ is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError)
+ end
+ end
+
+ context 'Converting from dos to unix format' do
+ sample_text = "Hello\r\nWorld\r\n"
+ desired_output = "Hello\nWorld\n"
+
+ it 'should output unix format' do
+ should run.with_params(sample_text).and_return(desired_output)
+ end
+ end
+
+ context 'Converting from unix to unix format' do
+ sample_text = "Hello\nWorld\n"
+ desired_output = "Hello\nWorld\n"
+
+ it 'should output unix format' do
+ should run.with_params(sample_text).and_return(desired_output)
+ end
+ end
+end
diff --git a/spec/functions/load_module_metadata.rb b/spec/functions/load_module_metadata.rb
new file mode 100755
index 0000000..ba542eb
--- /dev/null
+++ b/spec/functions/load_module_metadata.rb
@@ -0,0 +1,16 @@
+require 'spec_helper'
+
+describe 'load_module_metadata' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params("one", "two").and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+
+ it "should json parse the file" do
+ allow(scope).to receive(:function_get_module_path).with(['science']).and_return('/path/to/module/')
+ allow(File).to receive(:exists?).with(/metadata.json/).and_return(true)
+ allow(File).to receive(:read).with(/metadata.json/).and_return('{"name": "spencer-science"}')
+
+ result = subject.call(['science'])
+ expect(result['name']).to eq('spencer-science')
+ end
+end
diff --git a/spec/functions/size_spec.rb b/spec/functions/size_spec.rb
index 6b64866..c0047ee 100755
--- a/spec/functions/size_spec.rb
+++ b/spec/functions/size_spec.rb
@@ -8,15 +8,18 @@ describe 'size' do
is_expected.to run.with_params([], 'extra').and_raise_error(Puppet::ParseError, /wrong number of arguments/i)
}
it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /Unknown type given/) }
- it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /Unknown type given/) }
it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /Unknown type given/) }
- it { is_expected.to run.with_params('1').and_raise_error(Puppet::ParseError, /Requires either string or array to work/) }
- it { is_expected.to run.with_params('1.0').and_raise_error(Puppet::ParseError, /Requires either string or array to work/) }
+ it { is_expected.to run.with_params('1').and_raise_error(Puppet::ParseError, /Requires either string, array or hash to work/) }
+ it { is_expected.to run.with_params('1.0').and_raise_error(Puppet::ParseError, /Requires either string, array or hash to work/) }
it { is_expected.to run.with_params([]).and_return(0) }
it { is_expected.to run.with_params(['a']).and_return(1) }
it { is_expected.to run.with_params(['one', 'two', 'three']).and_return(3) }
it { is_expected.to run.with_params(['one', 'two', 'three', 'four']).and_return(4) }
+ it { is_expected.to run.with_params({}).and_return(0) }
+ it { is_expected.to run.with_params({'1' => '2'}).and_return(1) }
+ it { is_expected.to run.with_params({'1' => '2', '4' => '4'}).and_return(2) }
+
it { is_expected.to run.with_params('').and_return(0) }
it { is_expected.to run.with_params('a').and_return(1) }
it { is_expected.to run.with_params('abc').and_return(3) }
diff --git a/spec/functions/str2bool_spec.rb b/spec/functions/str2bool_spec.rb
index 3b439b2..7d8c47c 100755
--- a/spec/functions/str2bool_spec.rb
+++ b/spec/functions/str2bool_spec.rb
@@ -10,13 +10,13 @@ describe 'str2bool' do
it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /Unknown type of boolean given/) }
describe 'when testing values that mean "true"' do
- [ '1', 't', 'y', 'true', 'yes', true ].each do |value|
+ [ 'TRUE','1', 't', 'y', 'true', 'yes', true ].each do |value|
it { is_expected.to run.with_params(value).and_return(true) }
end
end
describe 'when testing values that mean "false"' do
- [ '', '0', 'f', 'n', 'false', 'no', false, 'undef', 'undefined' ].each do |value|
+ [ 'FALSE','', '0', 'f', 'n', 'false', 'no', false, 'undef', 'undefined' ].each do |value|
it { is_expected.to run.with_params(value).and_return(false) }
end
end
diff --git a/spec/functions/try_get_value_spec.rb b/spec/functions/try_get_value_spec.rb
new file mode 100644
index 0000000..38c0efd
--- /dev/null
+++ b/spec/functions/try_get_value_spec.rb
@@ -0,0 +1,100 @@
+require 'spec_helper'
+
+describe 'try_get_value' do
+
+ let(:data) do
+ {
+ 'a' => {
+ 'g' => '2',
+ 'e' => [
+ 'f0',
+ 'f1',
+ {
+ 'x' => {
+ 'y' => 'z'
+ }
+ },
+ 'f3',
+ ]
+ },
+ 'b' => true,
+ 'c' => false,
+ 'd' => '1',
+ }
+ end
+
+ context 'single values' do
+ it 'should exist' do
+ is_expected.not_to eq(nil)
+ end
+
+ it 'should be able to return a single value' do
+ is_expected.to run.with_params('test').and_return('test')
+ end
+
+ it 'should use the default value if data is a single value and path is present' do
+ is_expected.to run.with_params('test', 'path', 'default').and_return('default')
+ end
+
+ it 'should return default if there is no data' do
+ is_expected.to run.with_params(nil, nil, 'default').and_return('default')
+ end
+
+ it 'should be able to use data structures as default values' do
+ is_expected.to run.with_params('test', 'path', data).and_return(data)
+ end
+ end
+
+ context 'structure values' do
+ it 'should be able to extracts a single hash value' do
+ is_expected.to run.with_params(data, 'd', 'default').and_return('1')
+ end
+
+ it 'should be able to extract a deeply nested hash value' do
+ is_expected.to run.with_params(data, 'a/g', 'default').and_return('2')
+ end
+
+ it 'should return the default value if the path is not found' do
+ is_expected.to run.with_params(data, 'missing', 'default').and_return('default')
+ end
+
+ it 'should return the default value if the path is too long' do
+ is_expected.to run.with_params(data, 'a/g/c/d', 'default').and_return('default')
+ end
+
+ it 'should support an array index in the path' do
+ is_expected.to run.with_params(data, 'a/e/1', 'default').and_return('f1')
+ end
+
+ it 'should return the default value if an array index is not a number' do
+ is_expected.to run.with_params(data, 'a/b/c', 'default').and_return('default')
+ end
+
+ it 'should return the default value if and index is out of array length' do
+ is_expected.to run.with_params(data, 'a/e/5', 'default').and_return('default')
+ end
+
+ it 'should be able to path though both arrays and hashes' do
+ is_expected.to run.with_params(data, 'a/e/2/x/y', 'default').and_return('z')
+ end
+
+ it 'should be able to return "true" value' do
+ is_expected.to run.with_params(data, 'b', 'default').and_return(true)
+ is_expected.to run.with_params(data, 'm', true).and_return(true)
+ end
+
+ it 'should be able to return "false" value' do
+ is_expected.to run.with_params(data, 'c', 'default').and_return(false)
+ is_expected.to run.with_params(data, 'm', false).and_return(false)
+ end
+
+ it 'should return "nil" if value is not found and no default value is provided' do
+ is_expected.to run.with_params(data, 'a/1').and_return(nil)
+ end
+
+ it 'should be able to use a custom path separator' do
+ is_expected.to run.with_params(data, 'a::g', 'default', '::').and_return('2')
+ is_expected.to run.with_params(data, 'a::c', 'default', '::').and_return('default')
+ end
+ end
+end
diff --git a/spec/functions/union_spec.rb b/spec/functions/union_spec.rb
index 970e1fe..cfd38b6 100755
--- a/spec/functions/union_spec.rb
+++ b/spec/functions/union_spec.rb
@@ -5,10 +5,9 @@ describe 'union' do
it { is_expected.not_to eq(nil) }
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
- it { is_expected.to run.with_params('one', 'two', 'three').and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
- it { is_expected.to run.with_params('one', []).and_raise_error(Puppet::ParseError, /Requires 2 arrays/) }
- it { is_expected.to run.with_params([], 'two').and_raise_error(Puppet::ParseError, /Requires 2 arrays/) }
- it { is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError, /Requires 2 arrays/) }
+ it { is_expected.to run.with_params('one', []).and_raise_error(Puppet::ParseError, /Every parameter must be an array/) }
+ it { is_expected.to run.with_params([], 'two').and_raise_error(Puppet::ParseError, /Every parameter must be an array/) }
+ it { is_expected.to run.with_params({}, {}).and_raise_error(Puppet::ParseError, /Every parameter must be an array/) }
end
it { is_expected.to run.with_params([], []).and_return([]) }
@@ -19,5 +18,7 @@ describe 'union' do
it { is_expected.to run.with_params(['one', 'two', 'three'], ['two', 'three']).and_return(['one', 'two', 'three']) }
it { is_expected.to run.with_params(['one', 'two', 'two', 'three'], ['two', 'three']).and_return(['one', 'two', 'three']) }
it { is_expected.to run.with_params(['one', 'two', 'three'], ['two', 'two', 'three']).and_return(['one', 'two', 'three']) }
+ it { is_expected.to run.with_params(['one', 'two'], ['two', 'three'], ['one', 'three']).and_return(['one', 'two', 'three']) }
+ it { is_expected.to run.with_params(['one', 'two'], ['three', 'four'], ['one', 'two', 'three'], ['four']).and_return(['one', 'two', 'three', 'four']) }
it 'should not confuse types' do is_expected.to run.with_params(['1', '2', '3'], [1, 2]).and_return(['1', '2', '3', 1, 2]) end
end
diff --git a/spec/functions/unix2dos_spec.rb b/spec/functions/unix2dos_spec.rb
new file mode 100644
index 0000000..8537a26
--- /dev/null
+++ b/spec/functions/unix2dos_spec.rb
@@ -0,0 +1,40 @@
+require 'spec_helper'
+
+describe 'unix2dos' do
+ context 'Checking parameter validity' do
+ it { is_expected.not_to eq(nil) }
+ it do
+ is_expected.to run.with_params.and_raise_error(ArgumentError, /Wrong number of arguments/)
+ end
+ it do
+ is_expected.to run.with_params('one', 'two').and_raise_error(ArgumentError, /Wrong number of arguments/)
+ end
+ it do
+ is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError)
+ end
+ it do
+ is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError)
+ end
+ it do
+ is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError)
+ end
+ end
+
+ context 'Converting from unix to dos format' do
+ sample_text = "Hello\nWorld\n"
+ desired_output = "Hello\r\nWorld\r\n"
+
+ it 'should output dos format' do
+ should run.with_params(sample_text).and_return(desired_output)
+ end
+ end
+
+ context 'Converting from dos to dos format' do
+ sample_text = "Hello\r\nWorld\r\n"
+ desired_output = "Hello\r\nWorld\r\n"
+
+ it 'should output dos format' do
+ should run.with_params(sample_text).and_return(desired_output)
+ end
+ end
+end
diff --git a/spec/functions/validate_slength_spec.rb b/spec/functions/validate_slength_spec.rb
index 391f83a..5a8fa6a 100755
--- a/spec/functions/validate_slength_spec.rb
+++ b/spec/functions/validate_slength_spec.rb
@@ -10,7 +10,7 @@ describe 'validate_slength' do
it { is_expected.to run.with_params('', -1).and_raise_error(Puppet::ParseError, /second argument to be a positive Numeric/) }
it { is_expected.to run.with_params('', 1, '').and_raise_error(Puppet::ParseError, /third argument to be unset or a positive Numeric/) }
it { is_expected.to run.with_params('', 1, -1).and_raise_error(Puppet::ParseError, /third argument to be unset or a positive Numeric/) }
- it { is_expected.to run.with_params('', 1, 2).and_raise_error(Puppet::ParseError, /argument to be larger than third argument/) }
+ it { is_expected.to run.with_params('', 1, 2).and_raise_error(Puppet::ParseError, /argument to be equal to or larger than third argument/) }
end
context "with a maximum length of 10" do
diff --git a/spec/spec_helper_acceptance.rb b/spec/spec_helper_acceptance.rb
index 39d2d52..f784112 100755
--- a/spec/spec_helper_acceptance.rb
+++ b/spec/spec_helper_acceptance.rb
@@ -1,28 +1,10 @@
#! /usr/bin/env ruby -S rspec
require 'beaker-rspec'
+require 'beaker/puppet_install_helper'
UNSUPPORTED_PLATFORMS = []
-unless ENV['RS_PROVISION'] == 'no' or ENV['BEAKER_provision'] == 'no'
- foss_opts = {
- :default_action => 'gem_install',
- :version => (ENV['PUPPET_VERSION'] || '3.8.1'),
- }
-
- if default.is_pe?; then install_pe; else install_puppet( foss_opts ); end
-
- hosts.each do |host|
- if host['platform'] !~ /windows/i
- if host.is_pe?
- on host, 'mkdir -p /etc/puppetlabs/facter/facts.d'
- else
- on host, "/bin/touch #{host['puppetpath']}/hiera.yaml"
- on host, "mkdir -p #{host['distmoduledir']}"
- on host, 'mkdir -p /etc/facter/facts.d'
- end
- end
- end
-end
+run_puppet_install_helper
RSpec.configure do |c|
# Project root
@@ -50,3 +32,35 @@ def is_future_parser_enabled?
end
return false
end
+
+RSpec.shared_context "with faked facts" do
+ let(:facts_d) do
+ puppet_version = (on default, puppet('--version')).output.chomp
+ if fact('osfamily') =~ /windows/i
+ if fact('kernelmajversion').to_f < 6.0
+ 'C:/Documents and Settings/All Users/Application Data/PuppetLabs/facter/facts.d'
+ else
+ 'C:/ProgramData/PuppetLabs/facter/facts.d'
+ end
+ elsif Puppet::Util::Package.versioncmp(puppet_version, '4.0.0') < 0 and fact('is_pe', '--puppet') == "true"
+ '/etc/puppetlabs/facter/facts.d'
+ else
+ '/etc/facter/facts.d'
+ end
+ end
+
+ before :each do
+ #No need to create on windows, PE creates by default
+ if fact('osfamily') !~ /windows/i
+ shell("mkdir -p '#{facts_d}'")
+ end
+ end
+
+ after :each do
+ shell("rm -f '#{facts_d}/fqdn.txt'", :acceptable_exit_codes => [0,1])
+ end
+
+ def fake_fact(name, value)
+ shell("echo #{name}=#{value} > '#{facts_d}/#{name}.txt'")
+ end
+end
diff --git a/spec/unit/facter/root_home_spec.rb b/spec/unit/facter/root_home_spec.rb
index 98fe141..ac5160a 100755
--- a/spec/unit/facter/root_home_spec.rb
+++ b/spec/unit/facter/root_home_spec.rb
@@ -49,4 +49,17 @@ describe 'root_home', :type => :fact do
end
end
+ context "aix" do
+ before do
+ Facter.fact(:kernel).stubs(:value).returns("AIX")
+ Facter.fact(:osfamily).stubs(:value).returns("AIX")
+ end
+ let(:expected_root_home) { "/root" }
+ sample_lsuser = File.read(fixtures('lsuser','root'))
+
+ it "should return /root" do
+ Facter::Util::Resolution.stubs(:exec).with("lsuser -C -a home root").returns(sample_lsuser)
+ expect(Facter.fact(:root_home).value).to eq(expected_root_home)
+ end
+ end
end
diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb
index 8fe3932..23e649c 100755
--- a/spec/unit/puppet/provider/file_line/ruby_spec.rb
+++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb
@@ -36,7 +36,58 @@ describe provider_class do
expect(File.read(tmpfile).chomp).to eq('foo')
end
end
+ context 'when using replace' do
+ before :each do
+ # TODO: these should be ported over to use the PuppetLabs spec_helper
+ # file fixtures once the following pull request has been merged:
+ # https://github.com/puppetlabs/puppetlabs-stdlib/pull/73/files
+ tmp = Tempfile.new('tmp')
+ @tmpfile = tmp.path
+ tmp.close!
+ @resource = Puppet::Type::File_line.new(
+ {
+ :name => 'foo',
+ :path => @tmpfile,
+ :line => 'foo = bar',
+ :match => '^foo\s*=.*$',
+ :replace => false,
+ }
+ )
+ @provider = provider_class.new(@resource)
+ end
+
+ it 'should not replace the matching line' do
+ File.open(@tmpfile, 'w') do |fh|
+ fh.write("foo1\nfoo=blah\nfoo2\nfoo3")
+ end
+ expect(@provider.exists?).to be_truthy
+ @provider.create
+ expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo=blah\nfoo2\nfoo3")
+ end
+
+ it 'should append the line if no matches are found' do
+ File.open(@tmpfile, 'w') do |fh|
+ fh.write("foo1\nfoo2")
+ end
+ expect(@provider.exists?).to be_nil
+ @provider.create
+ expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo2\nfoo = bar")
+ end
+ it 'should raise an error with invalid values' do
+ expect {
+ @resource = Puppet::Type::File_line.new(
+ {
+ :name => 'foo',
+ :path => @tmpfile,
+ :line => 'foo = bar',
+ :match => '^foo\s*=.*$',
+ :replace => 'asgadga',
+ }
+ )
+ }.to raise_error(Puppet::Error, /Invalid value "asgadga"\. Valid values are true, false\./)
+ end
+ end
context "when matching" do
before :each do
# TODO: these should be ported over to use the PuppetLabs spec_helper
@@ -290,4 +341,100 @@ describe provider_class do
expect(File.read(@tmpfile)).to eql("foo1\nfoo2\n")
end
end
+
+ context "when removing with a match" do
+ before :each do
+ # TODO: these should be ported over to use the PuppetLabs spec_helper
+ # file fixtures once the following pull request has been merged:
+ # https://github.com/puppetlabs/puppetlabs-stdlib/pull/73/files
+ tmp = Tempfile.new('tmp')
+ @tmpfile = tmp.path
+ tmp.close!
+ @resource = Puppet::Type::File_line.new(
+ {
+ :name => 'foo',
+ :path => @tmpfile,
+ :line => 'foo2',
+ :ensure => 'absent',
+ :match => 'o$',
+ :match_for_absence => true,
+ }
+ )
+ @provider = provider_class.new(@resource)
+ end
+
+ it 'should remove one line if it matches' do
+ File.open(@tmpfile, 'w') do |fh|
+ fh.write("foo1\nfoo\nfoo2")
+ end
+ @provider.destroy
+ expect(File.read(@tmpfile)).to eql("foo1\nfoo2")
+ end
+
+ it 'should raise an error if more than one line matches' do
+ File.open(@tmpfile, 'w') do |fh|
+ fh.write("foo1\nfoo\nfoo2\nfoo\nfoo")
+ end
+ expect { @provider.destroy }.to raise_error(Puppet::Error, /More than one line/)
+ end
+
+ it 'should remove multiple lines if :multiple is true' do
+ @resource = Puppet::Type::File_line.new(
+ {
+ :name => 'foo',
+ :path => @tmpfile,
+ :line => 'foo2',
+ :ensure => 'absent',
+ :match => 'o$',
+ :multiple => true,
+ :match_for_absence => true,
+ }
+ )
+ @provider = provider_class.new(@resource)
+ File.open(@tmpfile, 'w') do |fh|
+ fh.write("foo1\nfoo\nfoo2\nfoo\nfoo")
+ end
+ @provider.destroy
+ expect(File.read(@tmpfile)).to eql("foo1\nfoo2\n")
+ end
+
+ it 'should ignore the match if match_for_absense is not specified' do
+ @resource = Puppet::Type::File_line.new(
+ {
+ :name => 'foo',
+ :path => @tmpfile,
+ :line => 'foo2',
+ :ensure => 'absent',
+ :match => 'o$',
+ }
+ )
+ @provider = provider_class.new(@resource)
+ File.open(@tmpfile, 'w') do |fh|
+ fh.write("foo1\nfoo\nfoo2")
+ end
+ @provider.destroy
+ expect(File.read(@tmpfile)).to eql("foo1\nfoo\n")
+ end
+
+ it 'should ignore the match if match_for_absense is false' do
+ @resource = Puppet::Type::File_line.new(
+ {
+ :name => 'foo',
+ :path => @tmpfile,
+ :line => 'foo2',
+ :ensure => 'absent',
+ :match => 'o$',
+ :match_for_absence => false,
+ }
+ )
+ @provider = provider_class.new(@resource)
+ File.open(@tmpfile, 'w') do |fh|
+ fh.write("foo1\nfoo\nfoo2")
+ end
+ @provider.destroy
+ expect(File.read(@tmpfile)).to eql("foo1\nfoo\n")
+ end
+
+ end
+
end
diff --git a/spec/unit/puppet/type/file_line_spec.rb b/spec/unit/puppet/type/file_line_spec.rb
index 410d0bf..f1430f2 100755
--- a/spec/unit/puppet/type/file_line_spec.rb
+++ b/spec/unit/puppet/type/file_line_spec.rb
@@ -49,6 +49,9 @@ describe Puppet::Type.type(:file_line) do
it 'should default to ensure => present' do
expect(file_line[:ensure]).to eq :present
end
+ it 'should default to replace => true' do
+ expect(file_line[:replace]).to eq :true
+ end
it "should autorequire the file it manages" do
catalog = Puppet::Resource::Catalog.new