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