(CONT-773) Rubocop Auto Fixes 11-15
[puppet-modules/puppetlabs-apt.git] / spec / defines / ppa_spec.rb
1 # frozen_string_literal: true
2
3 require 'spec_helper'
4
5 def ppa_exec_params(user, repo, distro = 'trusty', environment = [])
6   [
7     environment: environment,
8     command: "/opt/puppetlabs/puppet/cache/add-apt-repository-#{user}-ubuntu-#{repo}-#{distro}.sh",
9     logoutput: 'on_failure',
10   ]
11 end
12
13 describe 'apt::ppa' do
14   let :pre_condition do
15     'class { "apt": }'
16   end
17
18   describe 'defaults' do
19     let :facts do
20       {
21         os: {
22           family: 'Debian',
23           name: 'Ubuntu',
24           release: {
25             major: '18',
26             full: '18.04',
27           },
28           distro: {
29             codename: 'trusty',
30             id: 'Ubuntu',
31           },
32         },
33         puppet_vardir: '/opt/puppetlabs/puppet/cache'
34       }
35     end
36
37     let(:title) { 'ppa:needs/substitution' }
38
39     it { is_expected.not_to contain_package('python-software-properties') }
40
41     it {
42       expect(subject).to contain_exec('add-apt-repository-ppa:needs/substitution')
43         .that_notifies('Class[Apt::Update]')
44         .with(*ppa_exec_params('needs', 'substitution'))
45     }
46   end
47
48   [
49     'ppa:foo/bar',
50     'ppa:foo/bar1.0',
51     'ppa:foo10/bar10',
52     'ppa:foo-/bar_',
53   ].each do |value|
54     describe 'valid resource names' do
55       let :facts do
56         {
57           os: {
58             family: 'Debian',
59             name: 'Ubuntu',
60             release: {
61               major: '18',
62               full: '18.04',
63             },
64             distro: {
65               codename: 'trusty',
66               id: 'Ubuntu',
67             },
68           },
69         }
70       end
71
72       let(:title) { value }
73
74       it { is_expected.not_to raise_error }
75       it { is_expected.to contain_exec("add-apt-repository-#{value}") }
76     end
77   end
78
79   [
80     'ppa:foo!/bar',
81     'ppa:foo/bar!',
82     'ppa:foo1,0/bar',
83     'ppa:foo/bar/foobar',
84     '|| ls -la ||',
85     '|| touch /tmp/foo.txt ||',
86   ].each do |value|
87     describe 'invalid resource names' do
88       let :facts do
89         {
90           os: {
91             family: 'Debian',
92             name: 'Ubuntu',
93             release: {
94               major: '18',
95               full: '18.04',
96             },
97             distro: {
98               codename: 'trusty',
99               id: 'Ubuntu',
100             },
101           },
102         }
103       end
104
105       let(:title) { value }
106
107       it { is_expected.to raise_error(Puppet::PreformattedError, %r{Invalid PPA name: #{value}}) }
108     end
109   end
110
111   describe 'Ubuntu 15.10 sources.list filename' do
112     let :facts do
113       {
114         os: {
115           family: 'Debian',
116           name: 'Ubuntu',
117           release: {
118             major: '15',
119             full: '15.10',
120           },
121           distro: {
122             codename: 'wily',
123             id: 'Ubuntu',
124           },
125         },
126         puppet_vardir: '/opt/puppetlabs/puppet/cache',
127       }
128     end
129
130     let(:title) { 'ppa:user/foo' }
131
132     it {
133       expect(subject).to contain_exec('add-apt-repository-ppa:user/foo')
134         .that_notifies('Class[Apt::Update]')
135         .with(*ppa_exec_params('user', 'foo', 'wily'))
136     }
137   end
138
139   describe 'package_name => software-properties-common' do
140     let :pre_condition do
141       'class { "apt": }'
142     end
143
144     let :params do
145       {
146         package_name: 'software-properties-common',
147         package_manage: true,
148       }
149     end
150
151     let :facts do
152       {
153         os: {
154           family: 'Debian',
155           name: 'Ubuntu',
156           release: {
157             major: '18',
158             full: '18.04',
159           },
160           distro: {
161             codename: 'trusty',
162             id: 'Ubuntu',
163           },
164         },
165         puppet_vardir: '/opt/puppetlabs/puppet/cache',
166       }
167     end
168
169     let(:title) { 'ppa:needs/substitution' }
170
171     it { is_expected.to contain_package('software-properties-common') }
172
173     it {
174       expect(subject).to contain_exec('add-apt-repository-ppa:needs/substitution')
175         .that_notifies('Class[Apt::Update]')
176         .with(*ppa_exec_params('needs', 'substitution'))
177     }
178   end
179
180   describe 'package_manage => false' do
181     let :pre_condition do
182       'class { "apt": }'
183     end
184
185     let :facts do
186       {
187         os: {
188           family: 'Debian',
189           name: 'Ubuntu',
190           release: {
191             major: '18',
192             full: '18.04',
193           },
194           distro: {
195             codename: 'trusty',
196             id: 'Ubuntu',
197           },
198         },
199         puppet_vardir: '/opt/puppetlabs/puppet/cache',
200       }
201     end
202
203     let :params do
204       {
205         package_manage: false,
206       }
207     end
208
209     let(:title) { 'ppa:needs/substitution' }
210
211     it { is_expected.not_to contain_package('python-software-properties') }
212
213     it {
214       expect(subject).to contain_exec('add-apt-repository-ppa:needs/substitution')
215         .that_notifies('Class[Apt::Update]')
216         .with(*ppa_exec_params('needs', 'substitution'))
217     }
218   end
219
220   describe 'apt included, no proxy' do
221     let :pre_condition do
222       'class { "apt": }
223       apt::ppa { "ppa:user/foo2": }
224       '
225     end
226
227     let :facts do
228       {
229         os: {
230           family: 'Debian',
231           name: 'Ubuntu',
232           release: {
233             major: '18',
234             full: '18.04',
235           },
236           distro: {
237             codename: 'trusty',
238             id: 'Ubuntu',
239           },
240         },
241         puppet_vardir: '/opt/puppetlabs/puppet/cache',
242       }
243     end
244
245     let :params do
246       {
247         package_manage: true,
248         require: 'Apt::Ppa[ppa:user/foo2]',
249       }
250     end
251
252     let(:title) { 'ppa:user/foo' }
253
254     it { is_expected.to compile.with_all_deps }
255     it { is_expected.to contain_package('software-properties-common') }
256
257     it {
258       expect(subject).to contain_exec('add-apt-repository-ppa:user/foo')
259         .that_notifies('Class[Apt::Update]')
260         .with(*ppa_exec_params('user', 'foo'))
261     }
262   end
263
264   describe 'apt included, proxy host' do
265     let :pre_condition do
266       'class { "apt":
267         proxy => { "host" => "localhost" },
268       }'
269     end
270
271     let :facts do
272       {
273         os: {
274           family: 'Debian',
275           name: 'Ubuntu',
276           release: {
277             major: '18',
278             full: '18.04',
279           },
280           distro: {
281             codename: 'trusty',
282             id: 'Ubuntu',
283           },
284         },
285         puppet_vardir: '/opt/puppetlabs/puppet/cache',
286       }
287     end
288
289     let :params do
290       {
291         'package_manage' => true,
292       }
293     end
294
295     let(:title) { 'ppa:user/foo' }
296
297     it { is_expected.to contain_package('software-properties-common') }
298
299     it {
300       expect(subject).to contain_exec('add-apt-repository-ppa:user/foo')
301         .that_notifies('Class[Apt::Update]')
302         .with(*ppa_exec_params('user', 'foo', 'trusty', ['http_proxy=http://localhost:8080']))
303     }
304   end
305
306   describe 'apt included, proxy host and port' do
307     let :pre_condition do
308       'class { "apt":
309         proxy => { "host" => "localhost", "port" => 8180 },
310       }'
311     end
312
313     let :facts do
314       {
315         os: {
316           family: 'Debian',
317           name: 'Ubuntu',
318           release: {
319             major: '18',
320             full: '18.04',
321           },
322           distro: {
323             codename: 'trusty',
324             id: 'Ubuntu',
325           },
326         },
327         puppet_vardir: '/opt/puppetlabs/puppet/cache',
328       }
329     end
330
331     let :params do
332       {
333         package_manage: true,
334       }
335     end
336
337     let(:title) { 'ppa:user/foo' }
338
339     it { is_expected.to contain_package('software-properties-common') }
340
341     it {
342       expect(subject).to contain_exec('add-apt-repository-ppa:user/foo')
343         .that_notifies('Class[Apt::Update]')
344         .with(*ppa_exec_params('user', 'foo', 'trusty', ['http_proxy=http://localhost:8180']))
345     }
346   end
347
348   describe 'apt included, proxy host and port and https' do
349     let :pre_condition do
350       'class { "apt":
351         proxy => { "host" => "localhost", "port" => 8180, "https" => true },
352       }'
353     end
354
355     let :facts do
356       {
357         os: {
358           family: 'Debian',
359           name: 'Ubuntu',
360           release: {
361             major: '18',
362             full: '18.04',
363           },
364           distro: {
365             codename: 'trusty',
366             id: 'Ubuntu',
367           },
368         },
369         puppet_vardir: '/opt/puppetlabs/puppet/cache',
370       }
371     end
372
373     let :params do
374       {
375         package_manage: true,
376       }
377     end
378
379     let(:title) { 'ppa:user/foo' }
380
381     it { is_expected.to contain_package('software-properties-common') }
382
383     it {
384       expect(subject).to contain_exec('add-apt-repository-ppa:user/foo')
385         .that_notifies('Class[Apt::Update]')
386         .with(*ppa_exec_params('user', 'foo', 'trusty', ['http_proxy=http://localhost:8180', 'https_proxy=https://localhost:8180']))
387     }
388   end
389
390   describe 'ensure absent' do
391     let :pre_condition do
392       'class { "apt": }'
393     end
394
395     let :facts do
396       {
397         os: {
398           family: 'Debian',
399           name: 'Ubuntu',
400           release: {
401             major: '18',
402             full: '18.04',
403           },
404           distro: {
405             codename: 'trusty',
406             id: 'Ubuntu',
407           },
408         },
409         puppet_vardir: '/opt/puppetlabs/puppet/cache',
410       }
411     end
412
413     let(:title) { 'ppa:user/foo' }
414
415     let :params do
416       {
417         ensure: 'absent',
418       }
419     end
420
421     it {
422       expect(subject).to contain_tidy("remove-apt-repository-script-#{title}")
423         .with('path' => '/opt/puppetlabs/puppet/cache/add-apt-repository-user-ubuntu-foo-trusty.sh')
424
425       expect(subject).to contain_tidy("remove-apt-repository-#{title}")
426         .with('path' => '/etc/apt/sources.list.d/user-ubuntu-foo-trusty.list')
427         .that_notifies('Class[Apt::Update]')
428     }
429   end
430
431   context 'with validation' do
432     describe 'no release' do
433       let :facts do
434         {
435           os: {
436             family: 'Debian',
437             name: 'Ubuntu',
438             release: {
439               major: '18',
440               full: '18.04',
441             },
442             distro: {
443               codename: nil,
444               id: 'Ubuntu',
445             },
446           },
447         }
448       end
449
450       let(:title) { 'ppa:user/foo' }
451
452       it do
453         expect(subject).to raise_error(Puppet::Error, %r{os.distro.codename fact not available: release parameter required})
454       end
455     end
456
457     describe 'not ubuntu' do
458       let :facts do
459         {
460           os: {
461             family: 'Debian',
462             name: 'Debian',
463             release: {
464               major: '6',
465               full: '6.0.7',
466             },
467             distro: {
468               codename: 'wheezy',
469               id: 'Debian',
470             },
471           },
472         }
473       end
474
475       let(:title) { 'ppa:user/foo' }
476
477       it do
478         expect(subject).to raise_error(Puppet::Error, %r{not currently supported on Debian})
479       end
480     end
481   end
482 end