ee881324436b4bbef6f2ddf81115e9809f81717c
[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       }
44     end
45   end
46
47   describe 'no defaults' do
48     let :facts do
49       {
50         os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
51         lsbdistid: 'Debian',
52         lsbdistcodename: 'jessie',
53         osfamily: 'Debian',
54         operatingsystem: 'Debian',
55         lsbdistrelease: '8.0',
56         puppetversion: Puppet.version,
57       }
58     end
59
60     context 'with complex pin' do
61       let :params do
62         {
63           location: 'hello.there',
64           pin: { 'release' => 'wishwash',
65                  'explanation' => 'wishwash',
66                  'priority'    => 1001 },
67         }
68       end
69
70       it {
71         is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{hello.there jessie main\n})
72       }
73
74       it { is_expected.to contain_file('/etc/apt/sources.list.d/my_source.list').that_notifies('Class[Apt::Update]') }
75
76       it {
77         is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with(ensure: 'present',
78                                                                                                             priority: 1001,
79                                                                                                             explanation: 'wishwash',
80                                                                                                             release: 'wishwash')
81       }
82     end
83
84     context 'with simple key' do
85       let :params do
86         {
87           comment: 'foo',
88           location: 'http://debian.mirror.iweb.ca/debian/',
89           release: 'sid',
90           repos: 'testing',
91           key: GPG_KEY_ID,
92           pin: '10',
93           architecture: 'x86_64',
94           allow_unsigned: true,
95         }
96       end
97
98       it {
99         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})
100                                                              .without_content(%r{deb-src})
101       }
102
103       it {
104         is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with(ensure: 'present',
105                                                                                                             priority: '10',
106                                                                                                             origin: 'debian.mirror.iweb.ca')
107       }
108
109       it {
110         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',
111                                                                                                                                                     id: GPG_KEY_ID)
112       }
113     end
114
115     context 'with complex key' do
116       let :params do
117         {
118           comment: 'foo',
119           location: 'http://debian.mirror.iweb.ca/debian/',
120           release: 'sid',
121           repos: 'testing',
122           key: { 'id' => GPG_KEY_ID, 'server' => 'pgp.mit.edu',
123                  'content' => 'GPG key content',
124                  'source'  => 'http://apt.puppetlabs.com/pubkey.gpg' },
125           pin: '10',
126           architecture: 'x86_64',
127           allow_unsigned: true,
128         }
129       end
130
131       it {
132         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})
133                                                              .without_content(%r{deb-src})
134       }
135
136       it {
137         is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with(ensure: 'present',
138                                                                                                             priority: '10',
139                                                                                                             origin: 'debian.mirror.iweb.ca')
140       }
141
142       it {
143         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',
144                                                                                                                                                     id: GPG_KEY_ID,
145                                                                                                                                                     server: 'pgp.mit.edu',
146                                                                                                                                                     content: 'GPG key content',
147                                                                                                                                                     source: 'http://apt.puppetlabs.com/pubkey.gpg')
148       }
149     end
150
151     context 'with simple key' do
152       let :params do
153         {
154           comment: 'foo',
155           location: 'http://debian.mirror.iweb.ca/debian/',
156           release: 'sid',
157           repos: 'testing',
158           key: GPG_KEY_ID,
159           pin: '10',
160           architecture: 'x86_64',
161           allow_unsigned: true,
162         }
163       end
164
165       it {
166         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})
167                                                              .without_content(%r{deb-src})
168       }
169
170       it {
171         is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with(ensure: 'present',
172                                                                                                             priority: '10',
173                                                                                                             origin: 'debian.mirror.iweb.ca')
174       }
175
176       it {
177         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',
178                                                                                                                                                     id: GPG_KEY_ID)
179       }
180     end
181   end
182
183   context 'with allow_unsigned true' do
184     let :facts do
185       {
186         os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
187         lsbdistid: 'Debian',
188         lsbdistcodename: 'jessie',
189         osfamily: 'Debian',
190         puppetversion: Puppet.version,
191       }
192     end
193     let :params do
194       {
195         location: 'hello.there',
196         allow_unsigned: true,
197       }
198     end
199
200     it {
201       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})
202     }
203   end
204
205   context 'with architecture equals x86_64' do
206     let :facts do
207       {
208         os: { family: 'Debian', name: 'Debian', release: { major: '7', full: '7.0' } },
209         lsbdistid: 'Debian',
210         lsbdistcodename: 'wheezy',
211         osfamily: 'Debian',
212         puppetversion: Puppet.version,
213       }
214     end
215     let :params do
216       {
217         location: 'hello.there',
218         include: { 'deb' => false, 'src' => true },
219         architecture: 'x86_64',
220       }
221     end
222
223     it {
224       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})
225     }
226   end
227
228   context 'with architecture fact and unset architecture parameter' do
229     let :facts do
230       {
231         architecture: 'amd64',
232         os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
233         lsbdistid: 'Debian',
234         lsbdistcodename: 'jessie',
235         osfamily: 'Debian',
236         puppetversion: Puppet.version,
237       }
238     end
239     let :params do
240       {
241         location: 'hello.there',
242         include: { 'deb' => false, 'src' => true },
243       }
244     end
245
246     it {
247       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# my_source\ndeb-src hello.there jessie main\n})
248     }
249   end
250
251   context 'with include_src => true' do
252     let :facts do
253       {
254         os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
255         lsbdistid: 'Debian',
256         lsbdistcodename: 'jessie',
257         osfamily: 'Debian',
258         puppetversion: Puppet.version,
259       }
260     end
261     let :params do
262       {
263         location: 'hello.there',
264         include: { 'src' => true },
265       }
266     end
267
268     it {
269       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})
270     }
271   end
272
273   context 'with include deb => false' do
274     let :facts do
275       {
276         os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
277         lsbdistid: 'debian',
278         lsbdistcodename: 'jessie',
279         osfamily: 'debian',
280         puppetversion: Puppet.version,
281       }
282     end
283     let :params do
284       {
285         include: { 'deb' => false },
286         location: 'hello.there',
287       }
288     end
289
290     it {
291       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').without_content(%r{deb-src hello.there wheezy main\n})
292     }
293     it { is_expected.to contain_apt__setting('list-my_source').without_content(%r{deb hello.there wheezy main\n}) }
294   end
295
296   context 'with include src => true and include deb => false' do
297     let :facts do
298       {
299         os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
300         lsbdistid: 'debian',
301         lsbdistcodename: 'jessie',
302         osfamily: 'debian',
303         puppetversion: Puppet.version,
304       }
305     end
306     let :params do
307       {
308         include: { 'deb' => false, 'src' => true },
309         location: 'hello.there',
310       }
311     end
312
313     it {
314       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{deb-src hello.there jessie main\n})
315     }
316     it { is_expected.to contain_apt__setting('list-my_source').without_content(%r{deb hello.there jessie main\n}) }
317   end
318
319   context 'with ensure => absent' do
320     let :facts do
321       {
322         os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
323         lsbdistid: 'Debian',
324         lsbdistcodename: 'jessie',
325         osfamily: 'Debian',
326         puppetversion: Puppet.version,
327       }
328     end
329     let :params do
330       {
331         ensure: 'absent',
332       }
333     end
334
335     it {
336       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'absent')
337     }
338   end
339
340   describe 'validation' do
341     context 'with no release' do
342       let :facts do
343         {
344           os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
345           lsbdistid: 'Debian',
346           osfamily: 'Debian',
347           puppetversion: Puppet.version,
348         }
349       end
350       let(:params) { { location: 'hello.there' } }
351
352       it do
353         is_expected.to raise_error(Puppet::Error, %r{lsbdistcodename fact not available: release parameter required})
354       end
355     end
356
357     context 'with release is empty string' 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', release: '' } }
367
368       it { is_expected.to contain_apt__setting('list-my_source').with_content(%r{hello\.there  main}) }
369     end
370
371     context 'with invalid pin' do
372       let :facts do
373         {
374           os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
375           lsbdistid: 'Debian',
376           lsbdistcodename: 'jessie',
377           osfamily: 'Debian',
378           puppetversion: Puppet.version,
379         }
380       end
381       let :params do
382         {
383           location: 'hello.there',
384           pin: true,
385         }
386       end
387
388       it do
389         is_expected.to raise_error(Puppet::Error, %r{expects a value})
390       end
391     end
392
393     context 'with notify_update = undef (default)' do
394       let :facts do
395         {
396           os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
397           lsbdistid: 'Debian',
398           lsbdistcodename: 'jessie',
399           osfamily: 'Debian',
400           puppetversion: Puppet.version,
401         }
402       end
403       let :params do
404         {
405           location: 'hello.there',
406         }
407       end
408
409       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(true) }
410     end
411
412     context 'with notify_update = true' do
413       let :facts do
414         {
415           os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
416           lsbdistid: 'Debian',
417           lsbdistcodename: 'jessie',
418           osfamily: 'Debian',
419           puppetversion: Puppet.version,
420         }
421       end
422       let :params do
423         {
424           location: 'hello.there',
425           notify_update: true,
426         }
427       end
428
429       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(true) }
430     end
431
432     context 'with notify_update = false' 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           notify_update: false,
446         }
447       end
448
449       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(false) }
450     end
451   end
452 end