Update stdlib and concat to 6.1.0 both
[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, %r{Must specify a type}) }
6   it { is_expected.to run.with_params('type').and_raise_error(ArgumentError, %r{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 'when given an empty catalog' do
19     describe 'after running ensure_resource("user", "username1", {})' do
20       before(:each) { subject.execute('User', 'username1', {}) }
21
22       # this lambda is required due to strangeness within rspec-puppet's expectation handling
23       it { expect(-> { catalogue }).to contain_user('username1').without_ensure }
24     end
25
26     describe 'after running ensure_resource("user", "username1", { gid => undef })' do
27       before(:each) { subject.execute('User', 'username1', 'gid' => undef_value) }
28
29       # this lambda is required due to strangeness within rspec-puppet's expectation handling
30       it { expect(-> { catalogue }).to contain_user('username1').without_ensure }
31       it { expect(-> { 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(:each) { subject.execute('User', 'username1', 'ensure' => 'present', 'gid' => undef_value) }
36
37       # this lambda is required due to strangeness within rspec-puppet's expectation handling
38       it { expect(-> { catalogue }).to contain_user('username1').with_ensure('present') }
39       it { expect(-> { catalogue }).to contain_user('username1').without_gid }
40     end
41
42     describe 'after running ensure_resource("test::deftype", "foo", {})' do
43       let(:pre_condition) { 'define test::deftype { }' }
44
45       before(:each) { subject.execute('test::deftype', 'foo', {}) }
46
47       # this lambda is required due to strangeness within rspec-puppet's expectation handling
48       it { expect(-> { catalogue }).to contain_test__deftype('foo').without_ensure }
49     end
50   end
51
52   context 'when given a catalog with UTF8 chars' do
53     describe 'after running ensure_resource("user", "Şắოрŀễ Ţëם", {})' do
54       before(:each) { subject.execute('User', 'Şắოрŀễ Ţëם', {}) }
55
56       # this lambda is required due to strangeness within rspec-puppet's expectation handling
57       it { expect(-> { catalogue }).to contain_user('Şắოрŀễ Ţëם').without_ensure }
58     end
59
60     describe 'after running ensure_resource("user", "Şắოрŀễ Ţëם", { gid => undef })' do
61       before(:each) { subject.execute('User', 'Şắოрŀễ Ţëם', 'gid' => undef_value) }
62
63       # this lambda is required due to strangeness within rspec-puppet's expectation handling
64       it { expect(-> { catalogue }).to contain_user('Şắოрŀễ Ţëם').without_ensure }
65       it { expect(-> { catalogue }).to contain_user('Şắოрŀễ Ţëם').without_gid }
66     end
67
68     describe 'after running ensure_resource("user", "Şắოрŀễ Ţëם", { ensure => present, gid => undef })' do
69       before(:each) { subject.execute('User', 'Şắოрŀễ Ţëם', 'ensure' => 'present', 'gid' => undef_value) }
70
71       # this lambda is required due to strangeness within rspec-puppet's expectation handling
72       it { expect(-> { catalogue }).to contain_user('Şắოрŀễ Ţëם').with_ensure('present') }
73       it { expect(-> { catalogue }).to contain_user('Şắოрŀễ Ţëם').without_gid }
74     end
75   end
76
77   context 'when given a catalog with "user { username1: ensure => present }"' do
78     let(:pre_condition) { 'user { username1: ensure => present }' }
79
80     describe 'after running ensure_resource("user", "username1", {})' do
81       before(:each) { subject.execute('User', 'username1', {}) }
82
83       # this lambda is required due to strangeness within rspec-puppet's expectation handling
84       it { expect(-> { catalogue }).to contain_user('username1').with_ensure('present') }
85     end
86
87     describe 'after running ensure_resource("user", "username2", {})' do
88       before(:each) { subject.execute('User', 'username2', {}) }
89
90       # this lambda is required due to strangeness within rspec-puppet's expectation handling
91       it { expect(-> { catalogue }).to contain_user('username1').with_ensure('present') }
92       it { expect(-> { catalogue }).to contain_user('username2').without_ensure }
93     end
94
95     describe 'after running ensure_resource("user", "username1", { gid => undef })' do
96       before(:each) { subject.execute('User', 'username1', 'gid' => undef_value) }
97
98       # this lambda is required due to strangeness within rspec-puppet's expectation handling
99       it { expect(-> { catalogue }).to contain_user('username1').with_ensure('present') }
100     end
101
102     describe 'after running ensure_resource("user", ["username1", "username2"], {})' do
103       before(:each) { subject.execute('User', ['username1', 'username2'], {}) }
104
105       # this lambda is required due to strangeness within rspec-puppet's expectation handling
106       it { expect(-> { catalogue }).to contain_user('username1').with_ensure('present') }
107       it { expect(-> { catalogue }).to contain_user('username2').without_ensure }
108     end
109
110     describe 'when providing already set params' do
111       let(:params) { { 'ensure' => 'present' } }
112
113       before(:each) { subject.execute('User', ['username2', 'username3'], params) }
114
115       # this lambda is required due to strangeness within rspec-puppet's expectation handling
116       it { expect(-> { catalogue }).to contain_user('username1').with(params) }
117       it { expect(-> { catalogue }).to contain_user('username2').with(params) }
118     end
119
120     context 'when trying to add params' do
121       it {
122         is_expected.to run \
123           .with_params('User', 'username1', 'ensure' => 'present', 'shell' => true) \
124           .and_raise_error(Puppet::Resource::Catalog::DuplicateResourceError, %r{User\[username1\] is already declared})
125       }
126     end
127   end
128
129   context 'when given a catalog with "test::deftype { foo: }"' do
130     let(:pre_condition) { 'define test::deftype { } test::deftype { "foo": }' }
131
132     describe 'after running ensure_resource("test::deftype", "foo", {})' do
133       before(:each) { subject.execute('test::deftype', 'foo', {}) }
134
135       # this lambda is required due to strangeness within rspec-puppet's expectation handling
136       it { expect(-> { catalogue }).to contain_test__deftype('foo').without_ensure }
137     end
138   end
139
140   if Puppet::Util::Package.versioncmp(Puppet.version, '6.0.0') < 0
141     def undef_value
142       :undef
143     end
144   else
145     def undef_value
146       nil
147     end
148   end
149 end