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