Merge pull request #1040 from david22swan/release_prep
[puppet-modules/puppetlabs-apt.git] / spec / defines / source_spec.rb
1 # frozen_string_literal: true
2
3 require 'spec_helper'
4
5 describe 'apt::source' do
6   GPG_KEY_ID = '6F6B15509CF8E59E6E469F327F438280EF8D349F'
7
8   let :pre_condition do
9     'class { "apt": }'
10   end
11
12   let :title do
13     'my_source'
14   end
15
16   let :facts do
17     {
18       os: {
19         family: 'Debian',
20         name: 'Debian',
21         release: {
22           major: '9',
23           full: '9.0',
24         },
25         distro: {
26           codename: 'stretch',
27           id: 'Debian',
28         },
29       },
30     }
31   end
32
33   context 'with defaults' do
34     context 'without location' do
35       it do
36         is_expected.to raise_error(Puppet::Error, %r{source entry without specifying a location})
37       end
38     end
39     context 'with location' do
40       let(:params) { { location: 'hello.there' } }
41
42       it {
43         is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').without_content(%r{# my_source\ndeb-src hello.there wheezy main\n})
44         is_expected.not_to contain_package('apt-transport-https')
45       }
46     end
47   end
48
49   describe 'no defaults' do
50     context 'with complex pin' do
51       let :params do
52         {
53           location: 'hello.there',
54           pin: { 'release' => 'wishwash',
55                  'explanation' => 'wishwash',
56                  'priority'    => 1001 },
57         }
58       end
59
60       it {
61         is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{hello.there stretch main\n})
62       }
63
64       it { is_expected.to contain_file('/etc/apt/sources.list.d/my_source.list').that_notifies('Class[Apt::Update]') }
65
66       it {
67         is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with(ensure: 'present',
68                                                                                                             priority: 1001,
69                                                                                                             explanation: 'wishwash',
70                                                                                                             release: 'wishwash')
71       }
72     end
73
74     context 'with simple key' do
75       let :params do
76         {
77           comment: 'foo',
78           location: 'http://debian.mirror.iweb.ca/debian/',
79           release: 'sid',
80           repos: 'testing',
81           key: GPG_KEY_ID,
82           pin: '10',
83           architecture: 'x86_64',
84           allow_unsigned: true,
85         }
86       end
87
88       it {
89         is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# foo\ndeb \[arch=x86_64 trusted=yes\] http://debian.mirror.iweb.ca/debian/ sid testing\n})
90                                                              .without_content(%r{deb-src})
91       }
92
93       it {
94         is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with(ensure: 'present',
95                                                                                                             priority: '10',
96                                                                                                             origin: 'debian.mirror.iweb.ca')
97       }
98
99       it {
100         is_expected.to contain_apt__key("Add key: #{GPG_KEY_ID} from Apt::Source my_source").that_comes_before('Apt::Setting[list-my_source]').with(ensure: 'present',
101                                                                                                                                                     id: GPG_KEY_ID)
102       }
103     end
104
105     context 'with complex key' do
106       let :params do
107         {
108           comment: 'foo',
109           location: 'http://debian.mirror.iweb.ca/debian/',
110           release: 'sid',
111           repos: 'testing',
112           key: {
113             'ensure' => 'refreshed',
114             'id' => GPG_KEY_ID,
115             'server' => 'pgp.mit.edu',
116             'content' => 'GPG key content',
117             'source'  => 'http://apt.puppetlabs.com/pubkey.gpg',
118             'weak_ssl' => true,
119           },
120           pin: '10',
121           architecture: 'x86_64',
122           allow_unsigned: true,
123         }
124       end
125
126       it {
127         is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# foo\ndeb \[arch=x86_64 trusted=yes\] http://debian.mirror.iweb.ca/debian/ sid testing\n})
128                                                              .without_content(%r{deb-src})
129       }
130
131       it {
132         is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with(ensure: 'present',
133                                                                                                             priority: '10',
134                                                                                                             origin: 'debian.mirror.iweb.ca')
135       }
136
137       it {
138         is_expected.to contain_apt__key("Add key: #{GPG_KEY_ID} from Apt::Source my_source").that_comes_before('Apt::Setting[list-my_source]').with(ensure: 'refreshed',
139                                                                                                                                                     id: GPG_KEY_ID,
140                                                                                                                                                     server: 'pgp.mit.edu',
141                                                                                                                                                     content: 'GPG key content',
142                                                                                                                                                     source: 'http://apt.puppetlabs.com/pubkey.gpg',
143                                                                                                                                                     weak_ssl: true)
144       }
145     end
146   end
147
148   context 'with allow_insecure true' do
149     let :params do
150       {
151         location: 'hello.there',
152         allow_insecure: true,
153       }
154     end
155
156     it {
157       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# my_source\ndeb \[allow-insecure=yes\] hello.there stretch main\n})
158     }
159   end
160
161   context 'with allow_unsigned true' do
162     let :params do
163       {
164         location: 'hello.there',
165         allow_unsigned: true,
166       }
167     end
168
169     it {
170       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# my_source\ndeb \[trusted=yes\] hello.there stretch main\n})
171     }
172   end
173
174   context 'with keyring set' do
175     let :params do
176       {
177         location: 'hello.there',
178         keyring: '/usr/share/keyrings/foo-archive-keyring.gpg',
179       }
180     end
181
182     it {
183       is_expected.to contain_apt__setting('list-my_source')
184         .with(ensure: 'present')
185         .with_content(%r{# my_source\ndeb \[signed-by=/usr/share/keyrings/foo-archive-keyring.gpg\] hello.there stretch main\n})
186     }
187   end
188
189   context 'with keyring, architecture and allow_unsigned set' do
190     let :params do
191       {
192         location: 'hello.there',
193         architecture: 'amd64',
194         allow_unsigned: true,
195         keyring: '/usr/share/keyrings/foo-archive-keyring.gpg',
196       }
197     end
198
199     it {
200       is_expected.to contain_apt__setting('list-my_source')
201         .with(ensure: 'present')
202         .with_content(%r{# my_source\ndeb \[arch=amd64 trusted=yes signed-by=/usr/share/keyrings/foo-archive-keyring.gpg\] hello.there stretch main\n})
203     }
204   end
205
206   context 'with a https location, install apt-transport-https' do
207     let :params do
208       {
209         location: 'HTTPS://foo.bar',
210         allow_unsigned: false,
211       }
212     end
213
214     it {
215       is_expected.to contain_package('apt-transport-https')
216     }
217   end
218
219   context 'with a https location and custom release, install apt-transport-https' do
220     let :facts do
221       {
222         os: {
223           family: 'Debian',
224           name: 'Debian',
225           release: {
226             major: '9',
227             full: '9.0',
228           },
229           distro: {
230             codename: 'stretch',
231             id: 'Debian',
232           },
233         },
234         puppetversion: Puppet.version,
235       }
236     end
237     let :params do
238       {
239         location: 'HTTPS://foo.bar',
240         allow_unsigned: false,
241         release: 'customrelease',
242       }
243     end
244
245     it {
246       is_expected.to contain_package('apt-transport-https')
247     }
248   end
249
250   context 'with a https location, do not install apt-transport-https on oses not in list eg buster' do
251     let :facts do
252       {
253         os: {
254           family: 'Debian',
255           name: 'Debian',
256           release: {
257             major: '10',
258             full: '10.0',
259           },
260           distro: {
261             codename: 'buster',
262             id: 'Debian',
263           },
264         },
265       }
266     end
267     let :params do
268       {
269         location: 'https://foo.bar',
270         allow_unsigned: false,
271       }
272     end
273
274     it {
275       is_expected.not_to contain_package('apt-transport-https')
276     }
277   end
278
279   context 'with architecture equals x86_64' do
280     let :facts do
281       {
282         os: {
283           family: 'Debian',
284           name: 'Debian',
285           release: {
286             major: '7',
287             full: '7.0',
288           },
289           distro: {
290             codename: 'wheezy',
291             id: 'Debian',
292           },
293         },
294       }
295     end
296     let :params do
297       {
298         location: 'hello.there',
299         include: { 'deb' => false, 'src' => true },
300         architecture: 'x86_64',
301       }
302     end
303
304     it {
305       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# my_source\ndeb-src \[arch=x86_64\] hello.there wheezy main\n})
306     }
307   end
308
309   context 'with architecture fact and unset architecture parameter' do
310     let :facts do
311       super().merge(architecture: 'amd64')
312     end
313     let :params do
314       {
315         location: 'hello.there',
316         include: { 'deb' => false, 'src' => true },
317       }
318     end
319
320     it {
321       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# my_source\ndeb-src hello.there stretch main\n})
322     }
323   end
324
325   context 'with include_src => true' do
326     let :params do
327       {
328         location: 'hello.there',
329         include: { 'src' => true },
330       }
331     end
332
333     it {
334       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# my_source\ndeb hello.there stretch main\ndeb-src hello.there stretch main\n})
335     }
336   end
337
338   context 'with include deb => false' do
339     let :params do
340       {
341         include: { 'deb' => false },
342         location: 'hello.there',
343       }
344     end
345
346     it {
347       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').without_content(%r{deb-src hello.there wheezy main\n})
348     }
349     it { is_expected.to contain_apt__setting('list-my_source').without_content(%r{deb hello.there wheezy main\n}) }
350   end
351
352   context 'with include src => true and include deb => false' do
353     let :params do
354       {
355         include: { 'deb' => false, 'src' => true },
356         location: 'hello.there',
357       }
358     end
359
360     it {
361       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{deb-src hello.there stretch main\n})
362     }
363     it { is_expected.to contain_apt__setting('list-my_source').without_content(%r{deb hello.there stretch main\n}) }
364   end
365
366   context 'with ensure => absent' do
367     let :params do
368       {
369         ensure: 'absent',
370       }
371     end
372
373     it {
374       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'absent')
375     }
376   end
377
378   describe 'validation' do
379     context 'with no release' do
380       let :facts do
381         {
382           os: {
383             family: 'Debian',
384             name: 'Debian',
385             release: {
386               major: '8',
387               full: '8.0',
388             },
389             distro: {
390               id: 'Debian',
391             },
392           },
393         }
394       end
395       let(:params) { { location: 'hello.there' } }
396
397       it do
398         is_expected.to raise_error(Puppet::Error, %r{os.distro.codename fact not available: release parameter required})
399       end
400     end
401
402     context 'with release is empty string' do
403       let(:params) { { location: 'hello.there', release: '' } }
404
405       it { is_expected.to contain_apt__setting('list-my_source').with_content(%r{hello\.there  main}) }
406     end
407
408     context 'with invalid pin' do
409       let :params do
410         {
411           location: 'hello.there',
412           pin: true,
413         }
414       end
415
416       it do
417         is_expected.to raise_error(Puppet::Error, %r{expects a value})
418       end
419     end
420
421     context 'with notify_update = undef (default)' do
422       let :params do
423         {
424           location: 'hello.there',
425         }
426       end
427
428       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(true) }
429     end
430
431     context 'with notify_update = true' do
432       let :params do
433         {
434           location: 'hello.there',
435           notify_update: true,
436         }
437       end
438
439       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(true) }
440     end
441
442     context 'with notify_update = false' do
443       let :params do
444         {
445           location: 'hello.there',
446           notify_update: false,
447         }
448       end
449
450       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(false) }
451     end
452   end
453 end