Merge pull request #993 from kenyon/source_weak_ssl
[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: '8',
23           full: '8.0',
24         },
25         distro: {
26           codename: 'jessie',
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 jessie 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_unsigned true' do
149     let :params do
150       {
151         location: 'hello.there',
152         allow_unsigned: 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 \[trusted=yes\] hello.there jessie main\n})
158     }
159   end
160
161   context 'with keyring set' do
162     let :params do
163       {
164         location: 'hello.there',
165         keyring: '/usr/share/keyrings/foo-archive-keyring.gpg',
166       }
167     end
168
169     it {
170       is_expected.to contain_apt__setting('list-my_source')
171         .with(ensure: 'present')
172         .with_content(%r{# my_source\ndeb \[signed-by=/usr/share/keyrings/foo-archive-keyring.gpg\] hello.there jessie main\n})
173     }
174   end
175
176   context 'with keyring, architecture and allow_unsigned set' do
177     let :params do
178       {
179         location: 'hello.there',
180         architecture: 'amd64',
181         allow_unsigned: true,
182         keyring: '/usr/share/keyrings/foo-archive-keyring.gpg',
183       }
184     end
185
186     it {
187       is_expected.to contain_apt__setting('list-my_source')
188         .with(ensure: 'present')
189         .with_content(%r{# my_source\ndeb \[arch=amd64 trusted=yes signed-by=/usr/share/keyrings/foo-archive-keyring.gpg\] hello.there jessie main\n})
190     }
191   end
192
193   context 'with a https location, install apt-transport-https' do
194     let :params do
195       {
196         location: 'HTTPS://foo.bar',
197         allow_unsigned: false,
198       }
199     end
200
201     it {
202       is_expected.to contain_package('apt-transport-https')
203     }
204   end
205
206   context 'with a https location and custom release, install apt-transport-https' do
207     let :facts do
208       {
209         os: {
210           family: 'Debian',
211           name: 'Debian',
212           release: {
213             major: '8',
214             full: '8.0',
215           },
216           distro: {
217             codename: 'jessie',
218             id: 'Debian',
219           },
220         },
221         puppetversion: Puppet.version,
222       }
223     end
224     let :params do
225       {
226         location: 'HTTPS://foo.bar',
227         allow_unsigned: false,
228         release: 'customrelease',
229       }
230     end
231
232     it {
233       is_expected.to contain_package('apt-transport-https')
234     }
235   end
236
237   context 'with a https location, do not install apt-transport-https on oses not in list eg buster' do
238     let :facts do
239       {
240         os: {
241           family: 'Debian',
242           name: 'Debian',
243           release: {
244             major: '10',
245             full: '10.0',
246           },
247           distro: {
248             codename: 'buster',
249             id: 'Debian',
250           },
251         },
252       }
253     end
254     let :params do
255       {
256         location: 'https://foo.bar',
257         allow_unsigned: false,
258       }
259     end
260
261     it {
262       is_expected.not_to contain_package('apt-transport-https')
263     }
264   end
265
266   context 'with architecture equals x86_64' do
267     let :facts do
268       {
269         os: {
270           family: 'Debian',
271           name: 'Debian',
272           release: {
273             major: '7',
274             full: '7.0',
275           },
276           distro: {
277             codename: 'wheezy',
278             id: 'Debian',
279           },
280         },
281       }
282     end
283     let :params do
284       {
285         location: 'hello.there',
286         include: { 'deb' => false, 'src' => true },
287         architecture: 'x86_64',
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 \[arch=x86_64\] hello.there wheezy main\n})
293     }
294   end
295
296   context 'with architecture fact and unset architecture parameter' do
297     let :facts do
298       super().merge(architecture: 'amd64')
299     end
300     let :params do
301       {
302         location: 'hello.there',
303         include: { 'deb' => false, '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-src hello.there jessie main\n})
309     }
310   end
311
312   context 'with include_src => true' do
313     let :params do
314       {
315         location: 'hello.there',
316         include: { '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 hello.there jessie main\ndeb-src hello.there jessie main\n})
322     }
323   end
324
325   context 'with include deb => false' do
326     let :params do
327       {
328         include: { 'deb' => false },
329         location: 'hello.there',
330       }
331     end
332
333     it {
334       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').without_content(%r{deb-src hello.there wheezy main\n})
335     }
336     it { is_expected.to contain_apt__setting('list-my_source').without_content(%r{deb hello.there wheezy main\n}) }
337   end
338
339   context 'with include src => true and include deb => false' do
340     let :params do
341       {
342         include: { 'deb' => false, 'src' => true },
343         location: 'hello.there',
344       }
345     end
346
347     it {
348       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{deb-src hello.there jessie main\n})
349     }
350     it { is_expected.to contain_apt__setting('list-my_source').without_content(%r{deb hello.there jessie main\n}) }
351   end
352
353   context 'with ensure => absent' do
354     let :params do
355       {
356         ensure: 'absent',
357       }
358     end
359
360     it {
361       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'absent')
362     }
363   end
364
365   describe 'validation' do
366     context 'with no release' do
367       let :facts do
368         {
369           os: {
370             family: 'Debian',
371             name: 'Debian',
372             release: {
373               major: '8',
374               full: '8.0',
375             },
376             distro: {
377               id: 'Debian',
378             },
379           },
380         }
381       end
382       let(:params) { { location: 'hello.there' } }
383
384       it do
385         is_expected.to raise_error(Puppet::Error, %r{os.distro.codename fact not available: release parameter required})
386       end
387     end
388
389     context 'with release is empty string' do
390       let(:params) { { location: 'hello.there', release: '' } }
391
392       it { is_expected.to contain_apt__setting('list-my_source').with_content(%r{hello\.there  main}) }
393     end
394
395     context 'with invalid pin' do
396       let :params do
397         {
398           location: 'hello.there',
399           pin: true,
400         }
401       end
402
403       it do
404         is_expected.to raise_error(Puppet::Error, %r{expects a value})
405       end
406     end
407
408     context 'with notify_update = undef (default)' do
409       let :params do
410         {
411           location: 'hello.there',
412         }
413       end
414
415       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(true) }
416     end
417
418     context 'with notify_update = true' do
419       let :params do
420         {
421           location: 'hello.there',
422           notify_update: true,
423         }
424       end
425
426       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(true) }
427     end
428
429     context 'with notify_update = false' do
430       let :params do
431         {
432           location: 'hello.there',
433           notify_update: false,
434         }
435       end
436
437       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(false) }
438     end
439   end
440 end