Test ensure value of key is passed in for apt::source
[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, do not install apt-transport-https on oses not in list eg buster' do
200     let :facts do
201       {
202         os: { family: 'Debian', name: 'Debian', release: { major: '10', full: '10.0' } },
203         lsbdistid: 'Debian',
204         lsbdistcodename: 'buster',
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       }
214     end
215
216     it {
217       is_expected.not_to contain_package('apt-transport-https')
218     }
219   end
220
221   context 'with architecture equals x86_64' do
222     let :facts do
223       {
224         os: { family: 'Debian', name: 'Debian', release: { major: '7', full: '7.0' } },
225         lsbdistid: 'Debian',
226         lsbdistcodename: 'wheezy',
227         osfamily: 'Debian',
228         puppetversion: Puppet.version,
229       }
230     end
231     let :params do
232       {
233         location: 'hello.there',
234         include: { 'deb' => false, 'src' => true },
235         architecture: 'x86_64',
236       }
237     end
238
239     it {
240       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})
241     }
242   end
243
244   context 'with architecture fact and unset architecture parameter' do
245     let :facts do
246       {
247         architecture: 'amd64',
248         os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
249         lsbdistid: 'Debian',
250         lsbdistcodename: 'jessie',
251         osfamily: 'Debian',
252         puppetversion: Puppet.version,
253       }
254     end
255     let :params do
256       {
257         location: 'hello.there',
258         include: { 'deb' => false, 'src' => true },
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 hello.there jessie main\n})
264     }
265   end
266
267   context 'with include_src => true' do
268     let :facts do
269       {
270         os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
271         lsbdistid: 'Debian',
272         lsbdistcodename: 'jessie',
273         osfamily: 'Debian',
274         puppetversion: Puppet.version,
275       }
276     end
277     let :params do
278       {
279         location: 'hello.there',
280         include: { 'src' => true },
281       }
282     end
283
284     it {
285       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})
286     }
287   end
288
289   context 'with include deb => false' do
290     let :facts do
291       {
292         os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
293         lsbdistid: 'debian',
294         lsbdistcodename: 'jessie',
295         osfamily: 'debian',
296         puppetversion: Puppet.version,
297       }
298     end
299     let :params do
300       {
301         include: { 'deb' => false },
302         location: 'hello.there',
303       }
304     end
305
306     it {
307       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').without_content(%r{deb-src hello.there wheezy main\n})
308     }
309     it { is_expected.to contain_apt__setting('list-my_source').without_content(%r{deb hello.there wheezy main\n}) }
310   end
311
312   context 'with include src => true and 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, 'src' => true },
325         location: 'hello.there',
326       }
327     end
328
329     it {
330       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{deb-src hello.there jessie main\n})
331     }
332     it { is_expected.to contain_apt__setting('list-my_source').without_content(%r{deb hello.there jessie main\n}) }
333   end
334
335   context 'with ensure => absent' 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         ensure: 'absent',
348       }
349     end
350
351     it {
352       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'absent')
353     }
354   end
355
356   describe 'validation' do
357     context 'with no release' do
358       let :facts do
359         {
360           os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
361           lsbdistid: 'Debian',
362           osfamily: 'Debian',
363           puppetversion: Puppet.version,
364         }
365       end
366       let(:params) { { location: 'hello.there' } }
367
368       it do
369         is_expected.to raise_error(Puppet::Error, %r{lsbdistcodename fact not available: release parameter required})
370       end
371     end
372
373     context 'with release is empty string' do
374       let :facts do
375         {
376           os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
377           lsbdistid: 'Debian',
378           osfamily: 'Debian',
379           puppetversion: Puppet.version,
380         }
381       end
382       let(:params) { { location: 'hello.there', release: '' } }
383
384       it { is_expected.to contain_apt__setting('list-my_source').with_content(%r{hello\.there  main}) }
385     end
386
387     context 'with invalid pin' do
388       let :facts do
389         {
390           os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
391           lsbdistid: 'Debian',
392           lsbdistcodename: 'jessie',
393           osfamily: 'Debian',
394           puppetversion: Puppet.version,
395         }
396       end
397       let :params do
398         {
399           location: 'hello.there',
400           pin: true,
401         }
402       end
403
404       it do
405         is_expected.to raise_error(Puppet::Error, %r{expects a value})
406       end
407     end
408
409     context 'with notify_update = undef (default)' do
410       let :facts do
411         {
412           os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
413           lsbdistid: 'Debian',
414           lsbdistcodename: 'jessie',
415           osfamily: 'Debian',
416           puppetversion: Puppet.version,
417         }
418       end
419       let :params do
420         {
421           location: 'hello.there',
422         }
423       end
424
425       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(true) }
426     end
427
428     context 'with notify_update = true' do
429       let :facts do
430         {
431           os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
432           lsbdistid: 'Debian',
433           lsbdistcodename: 'jessie',
434           osfamily: 'Debian',
435           puppetversion: Puppet.version,
436         }
437       end
438       let :params do
439         {
440           location: 'hello.there',
441           notify_update: true,
442         }
443       end
444
445       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(true) }
446     end
447
448     context 'with notify_update = false' do
449       let :facts do
450         {
451           os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
452           lsbdistid: 'Debian',
453           lsbdistcodename: 'jessie',
454           osfamily: 'Debian',
455           puppetversion: Puppet.version,
456         }
457       end
458       let :params do
459         {
460           location: 'hello.there',
461           notify_update: false,
462         }
463       end
464
465       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(false) }
466     end
467   end
468 end