69338ff548e868ba66d72140595bd304620fa5a6
[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: { 'ensure' => 'refreshed',
124                  'id' => GPG_KEY_ID,
125                  'server' => 'pgp.mit.edu',
126                  'content' => 'GPG key content',
127                  'source'  => 'http://apt.puppetlabs.com/pubkey.gpg' },
128           pin: '10',
129           architecture: 'x86_64',
130           allow_unsigned: true,
131         }
132       end
133
134       it {
135         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})
136                                                              .without_content(%r{deb-src})
137       }
138
139       it {
140         is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with(ensure: 'present',
141                                                                                                             priority: '10',
142                                                                                                             origin: 'debian.mirror.iweb.ca')
143       }
144
145       it {
146         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',
147                                                                                                                                                     id: GPG_KEY_ID,
148                                                                                                                                                     server: 'pgp.mit.edu',
149                                                                                                                                                     content: 'GPG key content',
150                                                                                                                                                     source: 'http://apt.puppetlabs.com/pubkey.gpg')
151       }
152     end
153   end
154
155   context 'with allow_unsigned true' do
156     let :facts do
157       {
158         os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
159         lsbdistid: 'Debian',
160         lsbdistcodename: 'jessie',
161         osfamily: 'Debian',
162         puppetversion: Puppet.version,
163       }
164     end
165     let :params do
166       {
167         location: 'hello.there',
168         allow_unsigned: true,
169       }
170     end
171
172     it {
173       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})
174     }
175   end
176
177   context 'with a https location, install apt-transport-https' do
178     let :facts do
179       {
180         os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
181         lsbdistid: 'Debian',
182         lsbdistcodename: 'jessie',
183         osfamily: 'Debian',
184         puppetversion: Puppet.version,
185       }
186     end
187     let :params do
188       {
189         location: 'HTTPS://foo.bar',
190         allow_unsigned: false,
191       }
192     end
193
194     it {
195       is_expected.to contain_package('apt-transport-https')
196     }
197   end
198
199   context 'with a https location and custom release, install apt-transport-https' do
200     let :facts do
201       {
202         os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
203         lsbdistid: 'Debian',
204         lsbdistcodename: 'jessie',
205         osfamily: 'Debian',
206         puppetversion: Puppet.version,
207       }
208     end
209     let :params do
210       {
211         location: 'HTTPS://foo.bar',
212         allow_unsigned: false,
213         release: 'customrelease',
214       }
215     end
216
217     it {
218       is_expected.to contain_package('apt-transport-https')
219     }
220   end
221
222   context 'with a https location, do not install apt-transport-https on oses not in list eg buster' do
223     let :facts do
224       {
225         os: { family: 'Debian', name: 'Debian', release: { major: '10', full: '10.0' } },
226         lsbdistid: 'Debian',
227         lsbdistcodename: 'buster',
228         osfamily: 'Debian',
229         puppetversion: Puppet.version,
230       }
231     end
232     let :params do
233       {
234         location: 'https://foo.bar',
235         allow_unsigned: false,
236       }
237     end
238
239     it {
240       is_expected.not_to contain_package('apt-transport-https')
241     }
242   end
243
244   context 'with architecture equals x86_64' do
245     let :facts do
246       {
247         os: { family: 'Debian', name: 'Debian', release: { major: '7', full: '7.0' } },
248         lsbdistid: 'Debian',
249         lsbdistcodename: 'wheezy',
250         osfamily: 'Debian',
251         puppetversion: Puppet.version,
252       }
253     end
254     let :params do
255       {
256         location: 'hello.there',
257         include: { 'deb' => false, 'src' => true },
258         architecture: 'x86_64',
259       }
260     end
261
262     it {
263       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})
264     }
265   end
266
267   context 'with architecture fact and unset architecture parameter' do
268     let :facts do
269       {
270         architecture: 'amd64',
271         os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
272         lsbdistid: 'Debian',
273         lsbdistcodename: 'jessie',
274         osfamily: 'Debian',
275         puppetversion: Puppet.version,
276       }
277     end
278     let :params do
279       {
280         location: 'hello.there',
281         include: { 'deb' => false, 'src' => true },
282       }
283     end
284
285     it {
286       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# my_source\ndeb-src hello.there jessie main\n})
287     }
288   end
289
290   context 'with include_src => true' do
291     let :facts do
292       {
293         os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
294         lsbdistid: 'Debian',
295         lsbdistcodename: 'jessie',
296         osfamily: 'Debian',
297         puppetversion: Puppet.version,
298       }
299     end
300     let :params do
301       {
302         location: 'hello.there',
303         include: { 'src' => true },
304       }
305     end
306
307     it {
308       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})
309     }
310   end
311
312   context 'with include deb => false' do
313     let :facts do
314       {
315         os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
316         lsbdistid: 'debian',
317         lsbdistcodename: 'jessie',
318         osfamily: 'debian',
319         puppetversion: Puppet.version,
320       }
321     end
322     let :params do
323       {
324         include: { 'deb' => false },
325         location: 'hello.there',
326       }
327     end
328
329     it {
330       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').without_content(%r{deb-src hello.there wheezy main\n})
331     }
332     it { is_expected.to contain_apt__setting('list-my_source').without_content(%r{deb hello.there wheezy main\n}) }
333   end
334
335   context 'with include src => true and include deb => false' do
336     let :facts do
337       {
338         os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
339         lsbdistid: 'debian',
340         lsbdistcodename: 'jessie',
341         osfamily: 'debian',
342         puppetversion: Puppet.version,
343       }
344     end
345     let :params do
346       {
347         include: { 'deb' => false, 'src' => true },
348         location: 'hello.there',
349       }
350     end
351
352     it {
353       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{deb-src hello.there jessie main\n})
354     }
355     it { is_expected.to contain_apt__setting('list-my_source').without_content(%r{deb hello.there jessie main\n}) }
356   end
357
358   context 'with ensure => absent' do
359     let :facts do
360       {
361         os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
362         lsbdistid: 'Debian',
363         lsbdistcodename: 'jessie',
364         osfamily: 'Debian',
365         puppetversion: Puppet.version,
366       }
367     end
368     let :params do
369       {
370         ensure: 'absent',
371       }
372     end
373
374     it {
375       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'absent')
376     }
377   end
378
379   describe 'validation' do
380     context 'with no release' do
381       let :facts do
382         {
383           os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
384           lsbdistid: 'Debian',
385           osfamily: 'Debian',
386           puppetversion: Puppet.version,
387         }
388       end
389       let(:params) { { location: 'hello.there' } }
390
391       it do
392         is_expected.to raise_error(Puppet::Error, %r{lsbdistcodename fact not available: release parameter required})
393       end
394     end
395
396     context 'with release is empty string' do
397       let :facts do
398         {
399           os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
400           lsbdistid: 'Debian',
401           osfamily: 'Debian',
402           puppetversion: Puppet.version,
403         }
404       end
405       let(:params) { { location: 'hello.there', release: '' } }
406
407       it { is_expected.to contain_apt__setting('list-my_source').with_content(%r{hello\.there  main}) }
408     end
409
410     context 'with invalid pin' do
411       let :facts do
412         {
413           os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
414           lsbdistid: 'Debian',
415           lsbdistcodename: 'jessie',
416           osfamily: 'Debian',
417           puppetversion: Puppet.version,
418         }
419       end
420       let :params do
421         {
422           location: 'hello.there',
423           pin: true,
424         }
425       end
426
427       it do
428         is_expected.to raise_error(Puppet::Error, %r{expects a value})
429       end
430     end
431
432     context 'with notify_update = undef (default)' do
433       let :facts do
434         {
435           os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
436           lsbdistid: 'Debian',
437           lsbdistcodename: 'jessie',
438           osfamily: 'Debian',
439           puppetversion: Puppet.version,
440         }
441       end
442       let :params do
443         {
444           location: 'hello.there',
445         }
446       end
447
448       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(true) }
449     end
450
451     context 'with notify_update = true' do
452       let :facts do
453         {
454           os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
455           lsbdistid: 'Debian',
456           lsbdistcodename: 'jessie',
457           osfamily: 'Debian',
458           puppetversion: Puppet.version,
459         }
460       end
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 :facts do
473         {
474           os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
475           lsbdistid: 'Debian',
476           lsbdistcodename: 'jessie',
477           osfamily: 'Debian',
478           puppetversion: Puppet.version,
479         }
480       end
481       let :params do
482         {
483           location: 'hello.there',
484           notify_update: false,
485         }
486       end
487
488       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(false) }
489     end
490   end
491 end