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