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