Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / ensure_resource_spec.rb
1 require 'spec_helper'
2
3 describe 'ensure_resource' do
4   it { is_expected.not_to eq(nil) }
5   it { is_expected.to run.with_params().and_raise_error(ArgumentError, /Must specify a type/) }
6   it { is_expected.to run.with_params('type').and_raise_error(ArgumentError, /Must specify a title/) }
7   if Puppet::Util::Package.versioncmp(Puppet.version, '4.6.0') >= 0
8     it { is_expected.to run.with_params('type', 'title', {}, 'extras').and_raise_error(ArgumentError) }
9   else
10     it { is_expected.to run.with_params('type', 'title', {}, 'extras').and_raise_error(Puppet::ParseError) }
11   end
12
13   it {
14     pending("should not accept numbers as arguments")
15     is_expected.to run.with_params(1,2,3).and_raise_error(Puppet::ParseError)
16   }
17
18   context 'given an empty catalog' do
19     describe 'after running ensure_resource("user", "username1", {})' do
20       before { subject.call(['User', 'username1', {}]) }
21
22       # this lambda is required due to strangeness within rspec-puppet's expectation handling
23       it { expect(lambda { catalogue }).to contain_user('username1').without_ensure }
24     end
25
26     describe 'after running ensure_resource("user", "username1", { gid => undef })' do
27       before { subject.call(['User', 'username1', { 'gid' => :undef }]) }
28
29       # this lambda is required due to strangeness within rspec-puppet's expectation handling
30       it { expect(lambda { catalogue }).to contain_user('username1').without_ensure }
31       it { expect(lambda { catalogue }).to contain_user('username1').without_gid }
32     end
33
34     describe 'after running ensure_resource("user", "username1", { ensure => present, gid => undef })' do
35       before { subject.call(['User', 'username1', { 'ensure' => 'present', 'gid' => :undef }]) }
36
37       # this lambda is required due to strangeness within rspec-puppet's expectation handling
38       it { expect(lambda { catalogue }).to contain_user('username1').with_ensure('present') }
39       it { expect(lambda { catalogue }).to contain_user('username1').without_gid }
40     end
41
42     describe 'after running ensure_resource("test::deftype", "foo", {})' do
43       before { subject.call(['test::deftype', 'foo', {}]) }
44
45       # this lambda is required due to strangeness within rspec-puppet's expectation handling
46       it { expect(lambda { catalogue }).to contain_test__deftype('foo').without_ensure }
47     end
48   end
49
50   context 'given a catalog with UTF8 chars' do
51     describe 'after running ensure_resource("user", "Şắოрŀễ Ţëם", {})' do
52       before { subject.call(['User', 'Şắოрŀễ Ţëם', {}]) }
53
54       # this lambda is required due to strangeness within rspec-puppet's expectation handling
55       it { expect(lambda { catalogue }).to contain_user('Şắოрŀễ Ţëם').without_ensure }
56     end
57
58     describe 'after running ensure_resource("user", "Şắოрŀễ Ţëם", { gid => undef })' do
59       before { subject.call(['User', 'Şắოрŀễ Ţëם', { 'gid' => :undef }]) }
60
61       # this lambda is required due to strangeness within rspec-puppet's expectation handling
62       it { expect(lambda { catalogue }).to contain_user('Şắოрŀễ Ţëם').without_ensure }
63       it { expect(lambda { catalogue }).to contain_user('Şắოрŀễ Ţëם').without_gid }
64     end
65
66     describe 'after running ensure_resource("user", "Şắოрŀễ Ţëם", { ensure => present, gid => undef })' do
67       before { subject.call(['User', 'Şắოрŀễ Ţëם', { 'ensure' => 'present', 'gid' => :undef }]) }
68
69       # this lambda is required due to strangeness within rspec-puppet's expectation handling
70       it { expect(lambda { catalogue }).to contain_user('Şắოрŀễ Ţëם').with_ensure('present') }
71       it { expect(lambda { catalogue }).to contain_user('Şắოрŀễ Ţëם').without_gid }
72     end
73   end
74
75   context 'given a catalog with "user { username1: ensure => present }"' do
76     let(:pre_condition) { 'user { username1: ensure => present }' }
77
78     describe 'after running ensure_resource("user", "username1", {})' do
79       before { subject.call(['User', 'username1', {}]) }
80
81       # this lambda is required due to strangeness within rspec-puppet's expectation handling
82       it { expect(lambda { catalogue }).to contain_user('username1').with_ensure('present') }
83     end
84
85     describe 'after running ensure_resource("user", "username2", {})' do
86       before { subject.call(['User', 'username2', {}]) }
87
88       # this lambda is required due to strangeness within rspec-puppet's expectation handling
89       it { expect(lambda { catalogue }).to contain_user('username1').with_ensure('present') }
90       it { expect(lambda { catalogue }).to contain_user('username2').without_ensure }
91     end
92
93     describe 'after running ensure_resource("user", "username1", { gid => undef })' do
94       before { subject.call(['User', 'username1', { 'gid' => :undef }]) }
95
96       # this lambda is required due to strangeness within rspec-puppet's expectation handling
97       it { expect(lambda { catalogue }).to contain_user('username1').with_ensure('present') }
98     end
99
100     describe 'after running ensure_resource("user", ["username1", "username2"], {})' do
101       before { subject.call(['User', ['username1', 'username2'], {}]) }
102
103       # this lambda is required due to strangeness within rspec-puppet's expectation handling
104       it { expect(lambda { catalogue }).to contain_user('username1').with_ensure('present') }
105       it { expect(lambda { catalogue }).to contain_user('username2').without_ensure }
106     end
107
108     describe 'when providing already set params' do
109       let(:params) { { 'ensure' => 'present' } }
110       before { subject.call(['User', ['username2', 'username3'], params]) }
111
112       # this lambda is required due to strangeness within rspec-puppet's expectation handling
113       it { expect(lambda { catalogue }).to contain_user('username1').with(params) }
114       it { expect(lambda { catalogue }).to contain_user('username2').with(params) }
115     end
116
117     context 'when trying to add params' do
118       it { is_expected.to run \
119         .with_params('User', 'username1', { 'ensure' => 'present', 'shell' => true }) \
120         .and_raise_error(Puppet::Resource::Catalog::DuplicateResourceError, /User\[username1\] is already declared/)
121       }
122     end
123   end
124
125   context 'given a catalog with "test::deftype { foo: }"' do
126     let(:pre_condition) { 'test::deftype { "foo": }' }
127
128     describe 'after running ensure_resource("test::deftype", "foo", {})' do
129       before { subject.call(['test::deftype', 'foo', {}]) }
130
131       # this lambda is required due to strangeness within rspec-puppet's expectation handling
132       it { expect(lambda { catalogue }).to contain_test__deftype('foo').without_ensure }
133     end
134   end
135 end