-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathpuppet_agent_spec.rb
394 lines (334 loc) · 13.6 KB
/
puppet_agent_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# frozen_string_literal: true
require 'spec_helper'
# maps AIX release major fact value to the known AIX version
AIX_VERSION = {
'6100': '6.1',
'7100': '7.2',
'7200': '7.2',
}.freeze
def redhat_familly_supported_os
on_supported_os(
supported_os: [
{
'operatingsystem' => 'RedHat',
"operatingsystemrelease": ['5', '6', '7', '8'],
},
],
)
end
describe 'puppet_agent' do
package_version = '6.5.4'
global_params = { package_version: package_version }
def global_facts(facts, os)
facts.merge(
if os.include?('sles')
{
is_pe: true,
env_temp_variable: '/tmp',
operatingsystemmajrelease: facts[:operatingsystemrelease].split('.')[0],
}
elsif %r{redhat|centos|fedora|scientific|oracle}.match?(os)
{
env_temp_variable: '/tmp',
}
elsif os.include?('solaris')
{
is_pe: true,
puppet_agent_pid: 298,
}
elsif os.include?('aix')
{
is_pe: true,
platform_tag: "aix-#{AIX_VERSION[facts.dig(:os, 'release', 'major')]}-power",
}
elsif os.include?('windows')
{
puppet_agent_appdata: 'C:\\ProgramData',
puppet_confdir: 'C:\\ProgramData\\Puppetlabs\\puppet\\etc',
env_temp_variable: 'C:/tmp',
puppet_agent_pid: 42,
puppet_config: 'C:\\puppet.conf',
}
else
{}
end,
).merge(servername: 'master.example.vm')
end
context 'package version' do
context 'valid' do
['5.5.15-1.el7', '5.5.15.el7', '6.0.9.3.g886c5ab', 'present', 'latest'].each do |version|
redhat_familly_supported_os.each do |os, facts|
let(:facts) { global_facts(facts, os) }
context "on #{os}" do
let(:params) { { package_version: version } }
it { is_expected.to compile.with_all_deps }
it { expect { catalogue }.not_to raise_error }
it { is_expected.to contain_class('puppet_agent::prepare').with_package_version(version) }
it { is_expected.to contain_class('puppet_agent::install').with_package_version(version) }
it { is_expected.to contain_class('puppet_agent::configure').that_requires('Class[puppet_agent::install]') }
end
end
end
end
context 'invalid' do
['5.5.15x-1.el7', '5.5.15a+a.el7', '6.x0.9.3.g886c5abx'].each do |version|
redhat_familly_supported_os.each do |os, facts|
let(:facts) { global_facts(facts, os) }
let(:params) { { package_version: version } }
context "on #{os}" do
it { expect { catalogue }.to raise_error(%r{invalid version}) }
end
end
end
end
context 'latest' do
context 'on unsupported platform' do
on_supported_os.select { |platform, _| platform =~ %r{solaris|aix|windows|osx} }.each do |os, facts|
let(:facts) { global_facts(facts, os) }
let(:params) { { package_version: 'latest' } }
context os do
it { is_expected.not_to compile }
it { expect { catalogue }.to raise_error(Puppet::Error, %r{Setting package_version to 'latest' is not supported}) }
end
end
end
context 'on supported platform' do
on_supported_os.reject { |platform, _| platform =~ %r{solaris|aix|windows|osx} }.each do |os, facts|
let(:facts) { global_facts(facts, os) }
let(:params) { { package_version: 'latest' } }
context os do
it { is_expected.to compile.with_all_deps }
it { expect { catalogue }.not_to raise_error }
end
end
end
end
end
context 'supported_operating systems' do
on_supported_os.each do |os, facts|
context "on #{os}" do
let(:facts) do
global_facts(facts, os)
end
context 'when remote filebuckets are enabled' do
let(:pre_condition) { 'filebucket { "main": path => false }' }
before(:each) { Puppet.settings[:digest_algorithm] = 'sha256' }
context 'when an upgrade is required' do
let(:params) { { package_version: '6.18.0' } }
context 'with mismatching digest algorithms' do
let(:facts) do
global_facts(facts, os).merge(puppet_digest_algorithm: 'md5', aio_agent_version: '6.17.0')
end
it { is_expected.not_to compile }
it { expect { catalogue }.to raise_error(%r{Server: sha256, agent: md5}) }
end
context 'with matching digest algorithms' do
let(:facts) do
global_facts(facts, os).merge(puppet_digest_algorithm: 'sha256')
end
it { is_expected.to compile.with_all_deps }
it { expect { catalogue }.not_to raise_error }
end
end
context 'when no upgrade is required' do
let(:params) { { package_version: '6.17.0' } }
context 'with mismatching digest algorithms' do
let(:facts) do
global_facts(facts, os).merge(puppet_digest_algorithm: 'md5', aio_agent_version: '6.17.0')
end
it { is_expected.to compile }
it { expect { catalogue }.not_to raise_error }
end
end
end
# Windows, Solaris 10 and OS X use scripts for upgrading agents
# We test Solaris 11 in its own class
unless %r{windows|solaris|darwin}.match?(os)
context 'when using a dev build' do
let(:params) { { package_version: '5.2.0.100.g23e53f2' } }
it { is_expected.to contain_puppet_agent_end_run('5.2.0.100') }
end
context 'when using a release build' do
let(:params) { { package_version: '5.2.0' } }
it { is_expected.to contain_puppet_agent_end_run('5.2.0') }
end
end
context 'when the aio_agent_version fact is undefined' do
let(:facts) do
global_facts(facts, os).merge(aio_agent_version: nil)
end
it { is_expected.not_to compile }
end
unless %r{sles|solaris|aix}.match?(os)
context 'package_version is undef by default' do
let(:facts) do
global_facts(facts, os).merge(is_pe: false)
end
it { is_expected.to contain_class('puppet_agent').with_package_version(nil) }
end
end
context 'package_version is same as master when set to auto' do
let(:params) { { package_version: 'auto' } }
let(:facts) do
global_facts(facts, os).merge(serverversion: '7.6.5')
end
before :each do
allow(Puppet::FileSystem).to receive(:exist?).and_call_original
allow(Puppet::FileSystem).to receive(:read_preserve_line_endings).and_call_original
allow(Puppet::FileSystem).to receive(:exist?).with('/opt/puppetlabs/puppet/VERSION').and_return true
allow(Puppet::FileSystem).to receive(:read_preserve_line_endings).with('/opt/puppetlabs/puppet/VERSION').and_return "7.6.5\n"
end
it { is_expected.to contain_class('puppet_agent::prepare').with_package_version('7.6.5') }
it { is_expected.to contain_class('puppet_agent::install').with_package_version('7.6.5') }
end
end
end
end
context 'supported_operating systems' do
on_supported_os.each do |os, facts|
context "on #{os}" do
let(:facts) do
global_facts(facts, os).merge(is_pe: true)
end
before(:each) do
Puppet::Parser::Functions.newfunction(:pe_build_version, type: :rvalue) { |_args| '2000.0.0' }
Puppet::Parser::Functions.newfunction(:pe_compiling_server_aio_build, type: :rvalue) { |_args| '1.10.100' }
Puppet::Parser::Functions.newfunction(:defined, type: :rvalue) { |_args| true }
end
context 'package_version is initialized automatically' do
it { is_expected.to contain_class('puppet_agent').with_package_version(nil) }
end
context 'On a PE infrastructure node puppet_agent does nothing' do
before(:each) do
facts['pe_server_version'] = '2000.0.0'
end
it { is_expected.not_to contain_class('puppet_agent::prepare') }
it { is_expected.not_to contain_class('puppet_agent::install') }
end
end
end
end
context 'supported operating systems' do
on_supported_os.each do |os, facts|
context "on #{os}" do
let(:facts) do
global_facts(facts, os)
end
before(:each) do
if %r{sles|solaris|aix}.match?(os)
# Need to mock the PE functions
Puppet::Parser::Functions.newfunction(:pe_build_version, type: :rvalue) do |_args|
'2000.0.0'
end
end
end
context 'invalid package_versions' do
['1.3.5banana', '1.2', '10-q-5'].each do |version|
let(:params) { { package_version: version } }
it { expect { catalogue }.to raise_error(%r{invalid version}) }
end
end
context 'valid package_versions' do
['1.4.0.30.g886c5ab', '1.4.0', '1.4.0-10', '1.4.0.10'].each do |version|
let(:params) { { package_version: version } }
it { is_expected.to compile.with_all_deps }
it { expect { catalogue }.not_to raise_error }
end
end
[{}, { service_names: [] }].each do |params|
context "puppet_agent class with install_options with params: #{params}" do
let(:params) do
global_params.merge(
install_options: ['OPTION1=value1', 'OPTION2=value2'],
)
end
let(:expected_package_install_options) do
if os.include?('aix')
['--ignoreos', 'OPTION1=value1', 'OPTION2=value2']
else
['OPTION1=value1', 'OPTION2=value2']
end
end
let(:expected_class_install_options) { ['OPTION1=value1', 'OPTION2=value2'] }
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_class('puppet_agent::install').with_install_options(expected_class_install_options) }
unless %r{windows|solaris-10}.match?(os)
it { is_expected.to contain_package('puppet-agent') .with_install_options(expected_package_install_options) }
end
if os.include?('solaris-10')
it do
is_expected.to contain_exec('solaris_install script')
.with_command(
'/usr/bin/ctrun -l none /tmp/solaris_install.sh 298 2>&1 > /tmp/solaris_install.log &',
)
end
end
end
end
[{}, { service_names: [] }].each do |params|
context "puppet_agent class without any parameters(params: #{params})" do
let(:params) { params.merge(global_params) }
it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_class('puppet_agent') }
it { is_expected.to contain_class('puppet_agent::params') }
it { is_expected.to contain_class('puppet_agent::prepare') }
it { is_expected.to contain_class('puppet_agent::install').that_requires('Class[puppet_agent::prepare]') }
if facts[:osfamily] == 'Debian'
deb_package_version = package_version + '-1' + facts.dig(:os, 'distro', 'codename')
it { is_expected.to contain_package('puppet-agent').with_ensure(deb_package_version) }
elsif facts[:osfamily] == 'Solaris'
if facts[:operatingsystemmajrelease] == '11'
it { is_expected.to contain_package('puppet-agent').with_ensure('6.5.4') }
else
it do
is_expected.to contain_exec('solaris_install script')
.with_command(
'/usr/bin/ctrun -l none /tmp/solaris_install.sh 298 2>&1 > /tmp/solaris_install.log &',
)
end
end
elsif facts[:osfamily] == 'windows'
# Windows does not contain any Package resources
else
it { is_expected.to contain_package('puppet-agent').with_ensure(package_version) }
end
unless os.include?('windows')
unless %r{sles|solaris|aix}.match?(os)
it { is_expected.to contain_class('puppet_agent::service').that_requires('Class[puppet_agent::configure]') }
end
end
# Windows platform does not use Service resources; their services
# are managed by the MSI installer.
unless facts[:osfamily] == 'windows'
if params[:service_names].nil? && os !~ %r{sles|solaris|aix}
it { is_expected.to contain_service('puppet') }
else
it { is_expected.not_to contain_service('puppet') }
end
end
end
end
end
end
end
context 'unsupported operating system' do
describe 'puppet_agent class without any parameters on Solaris/Nexenta' do
let(:facts) do
{
os: {
architecture: 'i386',
family: 'Solaris',
name: 'Nexenta',
release: {
major: '3',
},
},
puppet_ssldir: '/dev/null/ssl',
puppet_config: '/dev/null/puppet.conf',
}
end
let(:params) { global_params }
it { expect { catalogue }.to raise_error(Puppet::Error, %r{Nexenta not supported}) }
end
end
end