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