]> review.fuel-infra Code Review - puppet-modules/puppetlabs-apt.git/blob - spec/defines/source_spec.rb
Add support for signed-by in source entries
[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: { 'ensure' => 'refreshed',
113                  'id' => GPG_KEY_ID,
114                  'server' => 'pgp.mit.edu',
115                  'content' => 'GPG key content',
116                  'source'  => 'http://apt.puppetlabs.com/pubkey.gpg' },
117           pin: '10',
118           architecture: 'x86_64',
119           allow_unsigned: true,
120         }
121       end
122
123       it {
124         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})
125                                                              .without_content(%r{deb-src})
126       }
127
128       it {
129         is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with(ensure: 'present',
130                                                                                                             priority: '10',
131                                                                                                             origin: 'debian.mirror.iweb.ca')
132       }
133
134       it {
135         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',
136                                                                                                                                                     id: GPG_KEY_ID,
137                                                                                                                                                     server: 'pgp.mit.edu',
138                                                                                                                                                     content: 'GPG key content',
139                                                                                                                                                     source: 'http://apt.puppetlabs.com/pubkey.gpg')
140       }
141     end
142   end
143
144   context 'with allow_unsigned true' do
145     let :params do
146       {
147         location: 'hello.there',
148         allow_unsigned: true,
149       }
150     end
151
152     it {
153       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})
154     }
155   end
156
157   context 'with keyring set' do
158     let :params do
159       {
160         location: 'hello.there',
161         keyring: '/usr/share/keyrings/foo-archive-keyring.gpg',
162       }
163     end
164
165     it {
166       is_expected.to contain_apt__setting('list-my_source')
167         .with(ensure: 'present')
168         .with_content(%r{# my_source\ndeb \[signed-by=/usr/share/keyrings/foo-archive-keyring.gpg\] hello.there jessie main\n})
169     }
170   end
171
172   context 'with keyring, architecture and allow_unsigned set' do
173     let :params do
174       {
175         location: 'hello.there',
176         architecture: 'amd64',
177         allow_unsigned: true,
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 \[arch=amd64 trusted=yes signed-by=/usr/share/keyrings/foo-archive-keyring.gpg\] hello.there jessie main\n})
186     }
187   end
188
189   context 'with a https location, install apt-transport-https' do
190     let :params do
191       {
192         location: 'HTTPS://foo.bar',
193         allow_unsigned: false,
194       }
195     end
196
197     it {
198       is_expected.to contain_package('apt-transport-https')
199     }
200   end
201
202   context 'with a https location and custom release, install apt-transport-https' do
203     let :facts do
204       {
205         os: {
206           family: 'Debian',
207           name: 'Debian',
208           release: {
209             major: '8',
210             full: '8.0',
211           },
212           distro: {
213             codename: 'jessie',
214             id: 'Debian',
215           },
216         },
217         puppetversion: Puppet.version,
218       }
219     end
220     let :params do
221       {
222         location: 'HTTPS://foo.bar',
223         allow_unsigned: false,
224         release: 'customrelease',
225       }
226     end
227
228     it {
229       is_expected.to contain_package('apt-transport-https')
230     }
231   end
232
233   context 'with a https location, do not install apt-transport-https on oses not in list eg buster' do
234     let :facts do
235       {
236         os: {
237           family: 'Debian',
238           name: 'Debian',
239           release: {
240             major: '10',
241             full: '10.0',
242           },
243           distro: {
244             codename: 'buster',
245             id: 'Debian',
246           },
247         },
248       }
249     end
250     let :params do
251       {
252         location: 'https://foo.bar',
253         allow_unsigned: false,
254       }
255     end
256
257     it {
258       is_expected.not_to contain_package('apt-transport-https')
259     }
260   end
261
262   context 'with architecture equals x86_64' do
263     let :facts do
264       {
265         os: {
266           family: 'Debian',
267           name: 'Debian',
268           release: {
269             major: '7',
270             full: '7.0',
271           },
272           distro: {
273             codename: 'wheezy',
274             id: 'Debian',
275           },
276         },
277       }
278     end
279     let :params do
280       {
281         location: 'hello.there',
282         include: { 'deb' => false, 'src' => true },
283         architecture: 'x86_64',
284       }
285     end
286
287     it {
288       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})
289     }
290   end
291
292   context 'with architecture fact and unset architecture parameter' do
293     let :facts do
294       super().merge(architecture: 'amd64')
295     end
296     let :params do
297       {
298         location: 'hello.there',
299         include: { 'deb' => false, 'src' => true },
300       }
301     end
302
303     it {
304       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# my_source\ndeb-src hello.there jessie main\n})
305     }
306   end
307
308   context 'with include_src => true' do
309     let :params do
310       {
311         location: 'hello.there',
312         include: { 'src' => true },
313       }
314     end
315
316     it {
317       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})
318     }
319   end
320
321   context 'with include deb => false' do
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 :params do
337       {
338         include: { 'deb' => false, 'src' => true },
339         location: 'hello.there',
340       }
341     end
342
343     it {
344       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{deb-src hello.there jessie main\n})
345     }
346     it { is_expected.to contain_apt__setting('list-my_source').without_content(%r{deb hello.there jessie main\n}) }
347   end
348
349   context 'with ensure => absent' do
350     let :params do
351       {
352         ensure: 'absent',
353       }
354     end
355
356     it {
357       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'absent')
358     }
359   end
360
361   describe 'validation' do
362     context 'with no release' do
363       let :facts do
364         {
365           os: {
366             family: 'Debian',
367             name: 'Debian',
368             release: {
369               major: '8',
370               full: '8.0',
371             },
372             distro: {
373               id: 'Debian',
374             },
375           },
376         }
377       end
378       let(:params) { { location: 'hello.there' } }
379
380       it do
381         is_expected.to raise_error(Puppet::Error, %r{os.distro.codename fact not available: release parameter required})
382       end
383     end
384
385     context 'with release is empty string' do
386       let(:params) { { location: 'hello.there', release: '' } }
387
388       it { is_expected.to contain_apt__setting('list-my_source').with_content(%r{hello\.there  main}) }
389     end
390
391     context 'with invalid pin' do
392       let :params do
393         {
394           location: 'hello.there',
395           pin: true,
396         }
397       end
398
399       it do
400         is_expected.to raise_error(Puppet::Error, %r{expects a value})
401       end
402     end
403
404     context 'with notify_update = undef (default)' do
405       let :params do
406         {
407           location: 'hello.there',
408         }
409       end
410
411       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(true) }
412     end
413
414     context 'with notify_update = true' do
415       let :params do
416         {
417           location: 'hello.there',
418           notify_update: true,
419         }
420       end
421
422       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(true) }
423     end
424
425     context 'with notify_update = false' do
426       let :params do
427         {
428           location: 'hello.there',
429           notify_update: false,
430         }
431       end
432
433       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(false) }
434     end
435   end
436 end