|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +describe 'stdlib::defined_with_params' do |
| 6 | + describe 'when no resource is specified' do |
| 7 | + it { is_expected.to run.with_params.and_raise_error(ArgumentError) } |
| 8 | + end |
| 9 | + |
| 10 | + describe 'when compared against a resource with no attributes' do |
| 11 | + let :pre_condition do |
| 12 | + 'user { "dan": }' |
| 13 | + end |
| 14 | + |
| 15 | + it { is_expected.to run.with_params('User[dan]', {}).and_return(true) } |
| 16 | + it { is_expected.to run.with_params('User[bob]', {}).and_return(false) } |
| 17 | + it { is_expected.to run.with_params('User[dan]', 'foo' => 'bar').and_return(false) } |
| 18 | + |
| 19 | + context 'with UTF8 and double byte characters' do |
| 20 | + it { is_expected.to run.with_params('User[ĵĭмოү]', {}).and_return(false) } |
| 21 | + it { is_expected.to run.with_params('User[ポーラ]', {}).and_return(false) } |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + describe 'when compared against a resource with attributes' do |
| 26 | + let :pre_condition do |
| 27 | + 'user { "dan": ensure => present, shell => "/bin/csh", managehome => false}' |
| 28 | + end |
| 29 | + |
| 30 | + it { is_expected.to run.with_params('User[dan]', {}).and_return(true) } |
| 31 | + it { is_expected.to run.with_params('User[dan]', '').and_return(true) } |
| 32 | + it { is_expected.to run.with_params('User[dan]', 'ensure' => 'present').and_return(true) } |
| 33 | + it { is_expected.to run.with_params('User[dan]', 'ensure' => 'present', 'managehome' => false).and_return(true) } |
| 34 | + it { is_expected.to run.with_params('User[dan]', 'ensure' => 'absent', 'managehome' => false).and_return(false) } |
| 35 | + end |
| 36 | + |
| 37 | + describe 'when passing undef values' do |
| 38 | + let :pre_condition do |
| 39 | + 'file { "/tmp/a": ensure => present }' |
| 40 | + end |
| 41 | + let(:is_puppet_6_or_greater) { Puppet::Util::Package.versioncmp(Puppet.version, '6.0.0') >= 0 } |
| 42 | + let(:undef_value) { is_puppet_6_or_greater ? nil : :undef } # even if :undef would work on 6.0.1, :undef should not be used |
| 43 | + |
| 44 | + it { is_expected.to run.with_params('File[/tmp/a]', {}).and_return(true) } |
| 45 | + it { is_expected.to run.with_params('File[/tmp/a]', 'ensure' => 'present', 'owner' => undef_value).and_return(true) } |
| 46 | + end |
| 47 | + |
| 48 | + describe 'when the reference is a' do |
| 49 | + let :pre_condition do |
| 50 | + 'user { "dan": }' |
| 51 | + end |
| 52 | + |
| 53 | + context 'with reference' do |
| 54 | + it { is_expected.to run.with_params(Puppet::Resource.new('User[dan]'), {}).and_return(true) } |
| 55 | + end |
| 56 | + |
| 57 | + if Puppet::Util::Package.versioncmp(Puppet.version, '4.6.0') >= 0 |
| 58 | + context 'with array' do |
| 59 | + it 'fails' do |
| 60 | + expect { |
| 61 | + subject.execute(['User[dan]'], {}) |
| 62 | + }.to raise_error(ArgumentError, %r{expects a value of type String or Type\[Resource\], got Tuple}) |
| 63 | + end |
| 64 | + end |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + describe 'when passed a defined type' do |
| 69 | + let :pre_condition do |
| 70 | + <<-PRECOND |
| 71 | + define test::deftype( |
| 72 | + Optional $port = undef |
| 73 | + ) { } |
| 74 | +
|
| 75 | + test::deftype { "foo": } |
| 76 | + test::deftype { "baz": port => 100 } |
| 77 | + test::deftype { "adv": port => 200 } |
| 78 | + test::deftype { "adv2": port => 200 } |
| 79 | +
|
| 80 | + # Unsure how to stub this out below properly |
| 81 | + if stdlib::defined_with_params(Test::Deftype, { 'port' => 200 }) { |
| 82 | + notify { 'Duplicate found somewhere': } |
| 83 | + } |
| 84 | + if stdlib::defined_with_params(Test::Deftype, { 'port' => 'nope' }) { |
| 85 | + notify { 'Should not find me': } |
| 86 | + } |
| 87 | + PRECOND |
| 88 | + end |
| 89 | + |
| 90 | + it { is_expected.to run.with_params('Test::Deftype[foo]', {}).and_return(true) } |
| 91 | + it { is_expected.to run.with_params('Test::Deftype[bar]', {}).and_return(false) } |
| 92 | + it { is_expected.to run.with_params(Puppet::Resource.new('Test::Deftype[foo]'), {}).and_return(true) } |
| 93 | + |
| 94 | + it { |
| 95 | + expect(subject).to run.with_params(Puppet::Resource.new('Test::Deftype[bar]'), {}).and_return(false) |
| 96 | + |
| 97 | + expect(catalogue.resource('Notify[Duplicate found somewhere]')).not_to be_nil |
| 98 | + expect(catalogue.resource('Notify[Should not find me]')).to be_nil |
| 99 | + } |
| 100 | + end |
| 101 | + |
| 102 | + describe 'when called from within a defined type looking for a defined type of the same type' do |
| 103 | + let :pre_condition do |
| 104 | + <<-PRECOND |
| 105 | + define test::deftype( |
| 106 | + Optional $port = undef |
| 107 | + ) { |
| 108 | + if stdlib::defined_with_params(Test::Deftype, { 'port' => $port }) { |
| 109 | + fail('Ruh Roh Shaggy') |
| 110 | + } |
| 111 | + } |
| 112 | +
|
| 113 | + test::deftype { 'foo': } |
| 114 | + test::deftype { 'bar': port => 200 } |
| 115 | + PRECOND |
| 116 | + end |
| 117 | + |
| 118 | + # Testing to make sure that the internal logic handles this case via the pre_condition |
| 119 | + it { is_expected.to run.with_params('NoOp[noop]', {}).and_return(false) } |
| 120 | + end |
| 121 | + |
| 122 | + describe 'when passed a class' do |
| 123 | + let :pre_condition do |
| 124 | + 'class test () { } class { "test": }' |
| 125 | + end |
| 126 | + |
| 127 | + it { is_expected.to run.with_params('Class[test]', {}).and_return(true) } |
| 128 | + it { is_expected.to run.with_params('Class["bar"]', {}).and_return(false) } |
| 129 | + it { is_expected.to run.with_params('Class[bar]', {}).and_return(false) } |
| 130 | + it { is_expected.to run.with_params(Puppet::Resource.new('class', 'test'), {}).and_return(true) } |
| 131 | + it { is_expected.to run.with_params(Puppet::Resource.new('Class["bar"]'), {}).and_return(false) } |
| 132 | + it { is_expected.to run.with_params(Puppet::Resource.new('Class[bar]'), {}).and_return(false) } |
| 133 | + end |
| 134 | +end |
0 commit comments