summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rwxr-xr-xspec/acceptance/clamp_spec.rb40
-rwxr-xr-xspec/acceptance/ensure_resource_spec.rb20
-rw-r--r--spec/functions/clamp_spec.rb16
-rw-r--r--spec/functions/seeded_rand_spec.rb53
-rw-r--r--spec/functions/validate_ip_address_spec.rb46
-rw-r--r--spec/unit/facter/package_provider_spec.rb57
-rw-r--r--spec/unit/puppet/parser/functions/is_absolute_path_spec.rb86
7 files changed, 287 insertions, 31 deletions
diff --git a/spec/acceptance/clamp_spec.rb b/spec/acceptance/clamp_spec.rb
new file mode 100755
index 0000000..0189258
--- /dev/null
+++ b/spec/acceptance/clamp_spec.rb
@@ -0,0 +1,40 @@
+#! /usr/bin/env ruby -S rspec
+require 'spec_helper_acceptance'
+
+describe 'clamp function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
+ describe 'success' do
+ it 'clamps list of values' do
+ pp = <<-EOS
+ $x = 17
+ $y = 225
+ $z = 155
+ $o = clamp($x, $y, $z)
+ if $o == $z {
+ notify { 'output correct': }
+ }
+ EOS
+
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).to match(/Notice: output correct/)
+ end
+ end
+ it 'clamps array of values' do
+ pp = <<-EOS
+ $a = [7, 19, 66]
+ $b = 19
+ $o = clamp($a)
+ if $o == $b {
+ notify { 'output correct': }
+ }
+ EOS
+
+ apply_manifest(pp, :catch_failures => true) do |r|
+ expect(r.stdout).to match(/Notice: output correct/)
+ end
+ end
+ end
+ describe 'failure' do
+ it 'handles improper argument counts'
+ it 'handles no arguments'
+ end
+end
diff --git a/spec/acceptance/ensure_resource_spec.rb b/spec/acceptance/ensure_resource_spec.rb
index fe619a9..93f25dd 100755
--- a/spec/acceptance/ensure_resource_spec.rb
+++ b/spec/acceptance/ensure_resource_spec.rb
@@ -1,18 +1,26 @@
#! /usr/bin/env ruby -S rspec
require 'spec_helper_acceptance'
-describe 'ensure_resource function', :unless => fact('osfamily') =~ /(windows|Suse)/i do
+describe 'ensure_resource function' do
describe 'success' do
- it 'ensure_resource a package' do
- apply_manifest('package { "rake": ensure => absent, provider => "gem", }')
+ it 'ensures a resource already declared' do
+ apply_manifest('')
pp = <<-EOS
- $a = "rake"
- ensure_resource('package', $a, {'provider' => 'gem'})
+ notify { "test": loglevel => 'err' }
+ ensure_resource('notify', 'test', { 'loglevel' => 'err' })
+ EOS
+
+ apply_manifest(pp, :expect_changes => true)
+ end
+
+ it 'ensures a undeclared resource' do
+ apply_manifest('')
+ pp = <<-EOS
+ ensure_resource('notify', 'test', { 'loglevel' => 'err' })
EOS
apply_manifest(pp, :expect_changes => true)
end
- it 'ensures a resource already declared'
it 'takes defaults arguments'
end
describe 'failure' do
diff --git a/spec/functions/clamp_spec.rb b/spec/functions/clamp_spec.rb
new file mode 100644
index 0000000..3e2fe7a
--- /dev/null
+++ b/spec/functions/clamp_spec.rb
@@ -0,0 +1,16 @@
+require 'spec_helper'
+
+describe 'clamp' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(ArgumentError) }
+ it { is_expected.to run.with_params([]).and_raise_error(Puppet::ParseError) }
+ it { is_expected.to run.with_params(12, 88, 71, 190).and_raise_error(Puppet::ParseError, /Wrong number of arguments, need three to clamp/) }
+ it { is_expected.to run.with_params('12string', 88, 15).and_raise_error(Puppet::ParseError, /Required explicit numeric/) }
+ it { is_expected.to run.with_params(1, 2, {'a' => 55}).and_raise_error(Puppet::ParseError, /The Hash type is not allowed/) }
+ it { is_expected.to run.with_params('24', [575, 187]).and_return(187) }
+ it { is_expected.to run.with_params([4, 3, '99']).and_return(4) }
+ it { is_expected.to run.with_params(16, 750, 88).and_return(88) }
+ it { is_expected.to run.with_params([3, 873], 73).and_return(73) }
+ it { is_expected.to run.with_params([4], 8, 75).and_return(8) }
+ it { is_expected.to run.with_params([6], [31], 9911).and_return(31) }
+end
diff --git a/spec/functions/seeded_rand_spec.rb b/spec/functions/seeded_rand_spec.rb
new file mode 100644
index 0000000..38e134e
--- /dev/null
+++ b/spec/functions/seeded_rand_spec.rb
@@ -0,0 +1,53 @@
+require 'spec_helper'
+
+describe 'seeded_rand' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(ArgumentError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(1).and_raise_error(ArgumentError, /wrong number of arguments/i) }
+ it { is_expected.to run.with_params(0, '').and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+ it { is_expected.to run.with_params(1.5, '').and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+ it { is_expected.to run.with_params(-10, '').and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+ it { is_expected.to run.with_params("-10", '').and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+ it { is_expected.to run.with_params("string", '').and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+ it { is_expected.to run.with_params([], '').and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+ it { is_expected.to run.with_params({}, '').and_raise_error(ArgumentError, /first argument must be a positive integer/) }
+ it { is_expected.to run.with_params(1, 1).and_raise_error(ArgumentError, /second argument must be a string/) }
+ it { is_expected.to run.with_params(1, []).and_raise_error(ArgumentError, /second argument must be a string/) }
+ it { is_expected.to run.with_params(1, {}).and_raise_error(ArgumentError, /second argument must be a string/) }
+
+ it "provides a random number strictly less than the given max" do
+ expect(seeded_rand(3, 'seed')).to satisfy {|n| n.to_i < 3 }
+ end
+
+ it "provides a random number greater or equal to zero" do
+ expect(seeded_rand(3, 'seed')).to satisfy {|n| n.to_i >= 0 }
+ end
+
+ it "provides the same 'random' value on subsequent calls for the same host" do
+ expect(seeded_rand(10, 'seed')).to eql(seeded_rand(10, 'seed'))
+ end
+
+ it "allows seed to control the random value on a single host" do
+ first_random = seeded_rand(1000, 'seed1')
+ second_different_random = seeded_rand(1000, 'seed2')
+
+ expect(first_random).not_to eql(second_different_random)
+ end
+
+ it "should not return different values for different hosts" do
+ val1 = seeded_rand(1000, 'foo', :host => "first.host.com")
+ val2 = seeded_rand(1000, 'foo', :host => "second.host.com")
+
+ expect(val1).to eql(val2)
+ end
+
+ def seeded_rand(max, seed, args = {})
+ host = args[:host] || '127.0.0.1'
+
+ # workaround not being able to use let(:facts) because some tests need
+ # multiple different hostnames in one context
+ scope.stubs(:lookupvar).with("::fqdn", {}).returns(host)
+
+ scope.function_seeded_rand([max, seed])
+ end
+end
diff --git a/spec/functions/validate_ip_address_spec.rb b/spec/functions/validate_ip_address_spec.rb
new file mode 100644
index 0000000..b56ce51
--- /dev/null
+++ b/spec/functions/validate_ip_address_spec.rb
@@ -0,0 +1,46 @@
+require 'spec_helper'
+
+describe 'validate_ip_address' do
+ describe 'signature validation' do
+ it { is_expected.not_to eq(nil) }
+ it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError, /wrong number of arguments/i) }
+ end
+
+ describe 'valid inputs' do
+ it { is_expected.to run.with_params('0.0.0.0') }
+ it { is_expected.to run.with_params('8.8.8.8') }
+ it { is_expected.to run.with_params('127.0.0.1') }
+ it { is_expected.to run.with_params('10.10.10.10') }
+ it { is_expected.to run.with_params('194.232.104.150') }
+ it { is_expected.to run.with_params('244.24.24.24') }
+ it { is_expected.to run.with_params('255.255.255.255') }
+ it { is_expected.to run.with_params('1.2.3.4', '5.6.7.8') }
+ it { is_expected.to run.with_params('3ffe:0505:0002::') }
+ it { is_expected.to run.with_params('3ffe:0505:0002::', '3ffe:0505:0002::2') }
+ it { is_expected.to run.with_params('::1/64') }
+ it { is_expected.to run.with_params('fe80::a00:27ff:fe94:44d6/64') }
+ context 'with netmasks' do
+ it { is_expected.to run.with_params('8.8.8.8/0') }
+ it { is_expected.to run.with_params('8.8.8.8/16') }
+ it { is_expected.to run.with_params('8.8.8.8/32') }
+ it { is_expected.to run.with_params('8.8.8.8/255.255.0.0') }
+ end
+ end
+
+ describe 'invalid inputs' do
+ it { is_expected.to run.with_params({}).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params(1).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params(true).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params('one').and_raise_error(Puppet::ParseError, /is not a valid IP/) }
+ it { is_expected.to run.with_params('0.0.0').and_raise_error(Puppet::ParseError, /is not a valid IP/) }
+ it { is_expected.to run.with_params('0.0.0.256').and_raise_error(Puppet::ParseError, /is not a valid IP/) }
+ it { is_expected.to run.with_params('0.0.0.0.0').and_raise_error(Puppet::ParseError, /is not a valid IP/) }
+ it { is_expected.to run.with_params('1.2.3.4', {}).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params('1.2.3.4', 1).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params('1.2.3.4', true).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params('1.2.3.4', 'one').and_raise_error(Puppet::ParseError, /is not a valid IP/) }
+ it { is_expected.to run.with_params('::1', {}).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params('::1', true).and_raise_error(Puppet::ParseError, /is not a string/) }
+ it { is_expected.to run.with_params('::1', 'one').and_raise_error(Puppet::ParseError, /is not a valid IP/) }
+ end
+end
diff --git a/spec/unit/facter/package_provider_spec.rb b/spec/unit/facter/package_provider_spec.rb
index d1aee94..3954faf 100644
--- a/spec/unit/facter/package_provider_spec.rb
+++ b/spec/unit/facter/package_provider_spec.rb
@@ -7,31 +7,38 @@ describe 'package_provider', :type => :fact do
before { Facter.clear }
after { Facter.clear }
- context "darwin" do
- it "should return pkgdmg" do
- provider = Puppet::Type.type(:package).provider(:pkgdmg)
- Puppet::Type.type(:package).stubs(:defaultprovider).returns provider
-
- expect(Facter.fact(:package_provider).value).to eq('pkgdmg')
- end
- end
-
- context "centos 7" do
- it "should return yum" do
- provider = Puppet::Type.type(:package).provider(:yum)
- Puppet::Type.type(:package).stubs(:defaultprovider).returns provider
-
- expect(Facter.fact(:package_provider).value).to eq('yum')
+ ['4.2.2', '3.7.1 (Puppet Enterprise 3.2.1)'].each do |puppetversion|
+ describe "on puppet ''#{puppetversion}''" do
+ before :each do
+ Facter.stubs(:value).returns puppetversion
+ end
+
+ context "darwin" do
+ it "should return pkgdmg" do
+ provider = Puppet::Type.type(:package).provider(:pkgdmg)
+ Puppet::Type.type(:package).stubs(:defaultprovider).returns provider
+
+ expect(Facter.fact(:package_provider).value).to eq('pkgdmg')
+ end
+ end
+
+ context "centos 7" do
+ it "should return yum" do
+ provider = Puppet::Type.type(:package).provider(:yum)
+ Puppet::Type.type(:package).stubs(:defaultprovider).returns provider
+
+ expect(Facter.fact(:package_provider).value).to eq('yum')
+ end
+ end
+
+ context "ubuntu" do
+ it "should return apt" do
+ provider = Puppet::Type.type(:package).provider(:apt)
+ Puppet::Type.type(:package).stubs(:defaultprovider).returns provider
+
+ expect(Facter.fact(:package_provider).value).to eq('apt')
+ end
+ end
end
end
-
- context "ubuntu" do
- it "should return apt" do
- provider = Puppet::Type.type(:package).provider(:apt)
- Puppet::Type.type(:package).stubs(:defaultprovider).returns provider
-
- expect(Facter.fact(:package_provider).value).to eq('apt')
- end
- end
-
end
diff --git a/spec/unit/puppet/parser/functions/is_absolute_path_spec.rb b/spec/unit/puppet/parser/functions/is_absolute_path_spec.rb
new file mode 100644
index 0000000..8931208
--- /dev/null
+++ b/spec/unit/puppet/parser/functions/is_absolute_path_spec.rb
@@ -0,0 +1,86 @@
+require 'spec_helper'
+
+describe :is_absolute_path do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+
+ let(:function_args) do
+ []
+ end
+
+ let(:function) do
+ scope.function_is_absolute_path(function_args)
+ end
+
+
+ describe 'validate arity' do
+ let(:function_args) do
+ [1,2]
+ end
+ it "should raise a ParseError if there is more than 1 arguments" do
+ lambda { function }.should( raise_error(ArgumentError))
+ end
+
+ end
+
+ it "should exist" do
+ Puppet::Parser::Functions.function(subject).should == "function_#{subject}"
+ end
+
+ # help enforce good function defination
+ it 'should contain arity' do
+
+ end
+
+ it "should raise a ParseError if there is less than 1 arguments" do
+ lambda { function }.should( raise_error(ArgumentError))
+ end
+
+
+ describe 'should retrun true' do
+ let(:return_value) do
+ true
+ end
+
+ describe 'windows' do
+ let(:function_args) do
+ ['c:\temp\test.txt']
+ end
+ it 'should return data' do
+ function.should eq(return_value)
+ end
+ end
+
+ describe 'non-windows' do
+ let(:function_args) do
+ ['/temp/test.txt']
+ end
+
+ it 'should return data' do
+ function.should eq(return_value)
+ end
+ end
+ end
+
+ describe 'should return false' do
+ let(:return_value) do
+ false
+ end
+ describe 'windows' do
+ let(:function_args) do
+ ['..\temp\test.txt']
+ end
+ it 'should return data' do
+ function.should eq(return_value)
+ end
+ end
+
+ describe 'non-windows' do
+ let(:function_args) do
+ ['../var/lib/puppet']
+ end
+ it 'should return data' do
+ function.should eq(return_value)
+ end
+ end
+ end
+end \ No newline at end of file