Cleanup via rubocop of ruby code
[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 'defaults' do
15     context 'without location' do
16       let :facts do
17         {
18           os: { family: 'Debian', name: 'Debian', release: { major: '7', full: '7.0' } },
19           osfamily: 'Debian',
20           lsbdistcodename: 'wheezy',
21           puppetversion: Puppet.version,
22         }
23       end
24
25       it do
26         expect {
27           subject.call
28         }.to raise_error(Puppet::Error, %r{source entry without specifying a location})
29       end
30     end
31     context 'with location' do
32       let :facts do
33         {
34           os: { family: 'Debian', name: 'Debian', release: { major: '7', full: '7.0' } },
35           lsbdistid: 'Debian',
36           lsbdistcodename: 'wheezy',
37           osfamily: 'Debian',
38           puppetversion: Puppet.version,
39         }
40       end
41       let(:params) { { location: 'hello.there' } }
42
43       it {
44         is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').without_content(%r{# my_source\ndeb-src hello.there wheezy main\n})
45       }
46     end
47   end
48
49   describe 'no defaults' do
50     let :facts do
51       {
52         os: { family: 'Debian', name: 'Debian', release: { major: '7', full: '7.0' } },
53         lsbdistid: 'Debian',
54         lsbdistcodename: 'wheezy',
55         osfamily: 'Debian',
56         operatingsystem: 'Debian',
57         lsbdistrelease: '7.0',
58         puppetversion: Puppet.version,
59       }
60     end
61
62     context 'with complex pin' do
63       let :params do
64         {
65           location: 'hello.there',
66           pin: { 'release' => 'wishwash',
67                  'explanation' => 'wishwash',
68                  'priority'    => 1001 },
69         }
70       end
71
72       it {
73         is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{hello.there wheezy main\n})
74       }
75
76       it { is_expected.to contain_file('/etc/apt/sources.list.d/my_source.list').that_notifies('Class[Apt::Update]') }
77
78       it {
79         is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with(ensure: 'present',
80                                                                                                             priority: 1001,
81                                                                                                             explanation: 'wishwash',
82                                                                                                             release: 'wishwash')
83       }
84     end
85
86     context 'with simple key' do
87       let :params do
88         {
89           comment: 'foo',
90           location: 'http://debian.mirror.iweb.ca/debian/',
91           release: 'sid',
92           repos: 'testing',
93           key: GPG_KEY_ID,
94           pin: '10',
95           architecture: 'x86_64',
96           allow_unsigned: true,
97         }
98       end
99
100       it {
101         is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(/# foo\ndeb \[arch=x86_64 trusted=yes\] http:\/\/debian\.mirror\.iweb\.ca\/debian\/ sid testing\n/).without_content(%r{deb-src}) # rubocop:disable Metrics/LineLength
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(/# foo\ndeb \[arch=x86_64 trusted=yes\] http:\/\/debian\.mirror\.iweb\.ca\/debian\/ sid testing\n/).without_content(%r{deb-src}) # rubocop:disable Metrics/LineLength
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(/# foo\ndeb \[arch=x86_64 trusted=yes\] http:\/\/debian\.mirror\.iweb\.ca\/debian\/ sid testing\n/).without_content(%r{deb-src}) # rubocop:disable Metrics/LineLength
167       }
168
169       it {
170         is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with(ensure: 'present',
171                                                                                                             priority: '10',
172                                                                                                             origin: 'debian.mirror.iweb.ca')
173       }
174
175       it {
176         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',
177                                                                                                                                                     id: GPG_KEY_ID)
178       }
179     end
180   end
181
182   context 'allow_unsigned true' do
183     let :facts do
184       {
185         os: { family: 'Debian', name: 'Debian', release: { major: '7', full: '7.0' } },
186         lsbdistid: 'Debian',
187         lsbdistcodename: 'wheezy',
188         osfamily: 'Debian',
189         puppetversion: Puppet.version,
190       }
191     end
192     let :params do
193       {
194         location: 'hello.there',
195         allow_unsigned: true,
196       }
197     end
198
199     it {
200       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# my_source\ndeb \[trusted=yes\] hello.there wheezy main\n})
201     }
202   end
203
204   context 'architecture equals x86_64' do
205     let :facts do
206       {
207         os: { family: 'Debian', name: 'Debian', release: { major: '7', full: '7.0' } },
208         lsbdistid: 'Debian',
209         lsbdistcodename: 'wheezy',
210         osfamily: 'Debian',
211         puppetversion: Puppet.version,
212       }
213     end
214     let :params do
215       {
216         location: 'hello.there',
217         include: { 'deb' => false, 'src' => true },
218         architecture: 'x86_64',
219       }
220     end
221
222     it {
223       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})
224     }
225   end
226
227   context 'with architecture fact and unset architecture parameter' do
228     let :facts do
229       {
230         architecture: 'amd64',
231         os: { family: 'Debian', name: 'Debian', release: { major: '7', full: '7.0' } },
232         lsbdistid: 'Debian',
233         lsbdistcodename: 'wheezy',
234         osfamily: 'Debian',
235         puppetversion: Puppet.version,
236       }
237     end
238     let :params do
239       {
240         location: 'hello.there',
241         include: { 'deb' => false, 'src' => true },
242       }
243     end
244
245     it {
246       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# my_source\ndeb-src hello.there wheezy main\n})
247     }
248   end
249
250   context 'include_src => true' do
251     let :facts do
252       {
253         os: { family: 'Debian', name: 'Debian', release: { major: '7', full: '7.0' } },
254         lsbdistid: 'Debian',
255         lsbdistcodename: 'wheezy',
256         osfamily: 'Debian',
257         puppetversion: Puppet.version,
258       }
259     end
260     let :params do
261       {
262         location: 'hello.there',
263         include: { 'src' => true },
264       }
265     end
266
267     it {
268       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# my_source\ndeb hello.there wheezy main\ndeb-src hello.there wheezy main\n})
269     }
270   end
271
272   context 'include deb => false' do
273     let :facts do
274       {
275         os: { family: 'Debian', name: 'Debian', release: { major: '7', full: '7.0' } },
276         lsbdistid: 'debian',
277         lsbdistcodename: 'wheezy',
278         osfamily: 'debian',
279         puppetversion: Puppet.version,
280       }
281     end
282     let :params do
283       {
284         include: { 'deb' => false },
285         location: 'hello.there',
286       }
287     end
288
289     it {
290       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').without_content(%r{deb-src hello.there wheezy main\n})
291     }
292     it { is_expected.to contain_apt__setting('list-my_source').without_content(%r{deb hello.there wheezy main\n}) }
293   end
294
295   context 'include src => true and include deb => false' do
296     let :facts do
297       {
298         os: { family: 'Debian', name: 'Debian', release: { major: '7', full: '7.0' } },
299         lsbdistid: 'debian',
300         lsbdistcodename: 'wheezy',
301         osfamily: 'debian',
302         puppetversion: Puppet.version,
303       }
304     end
305     let :params do
306       {
307         include: { 'deb' => false, 'src' => true },
308         location: 'hello.there',
309       }
310     end
311
312     it {
313       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{deb-src hello.there wheezy main\n})
314     }
315     it { is_expected.to contain_apt__setting('list-my_source').without_content(%r{deb hello.there wheezy main\n}) }
316   end
317
318   context 'ensure => absent' do
319     let :facts do
320       {
321         os: { family: 'Debian', name: 'Debian', release: { major: '7', full: '7.0' } },
322         lsbdistid: 'Debian',
323         lsbdistcodename: 'wheezy',
324         osfamily: 'Debian',
325         puppetversion: Puppet.version,
326       }
327     end
328     let :params do
329       {
330         ensure: 'absent',
331       }
332     end
333
334     it {
335       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'absent')
336     }
337   end
338
339   describe 'validation' do
340     context 'no release' do
341       let :facts do
342         {
343           os: { family: 'Debian', name: 'Debian', release: { major: '7', full: '7.0' } },
344           lsbdistid: 'Debian',
345           osfamily: 'Debian',
346           puppetversion: Puppet.version,
347         }
348       end
349       let(:params) { { location: 'hello.there' } }
350
351       it do
352         expect {
353           subject.call
354         }.to raise_error(Puppet::Error, %r{lsbdistcodename fact not available: release parameter required})
355       end
356     end
357
358     context 'release is empty string' do
359       let :facts do
360         {
361           os: { family: 'Debian', name: 'Debian', release: { major: '7', full: '7.0' } },
362           lsbdistid: 'Debian',
363           osfamily: 'Debian',
364           puppetversion: Puppet.version,
365         }
366       end
367       let(:params) { { location: 'hello.there', release: '' } }
368
369       it { is_expected.to contain_apt__setting('list-my_source').with_content(%r{hello\.there  main}) }
370     end
371
372     context 'invalid pin' do
373       let :facts do
374         {
375           os: { family: 'Debian', name: 'Debian', release: { major: '7', full: '7.0' } },
376           lsbdistid: 'Debian',
377           lsbdistcodename: 'wheezy',
378           osfamily: 'Debian',
379           puppetversion: Puppet.version,
380         }
381       end
382       let :params do
383         {
384           location: 'hello.there',
385           pin: true,
386         }
387       end
388
389       it do
390         expect {
391           subject.call
392         }.to raise_error(Puppet::Error, %r{invalid value for pin})
393       end
394     end
395
396     context 'with notify_update = undef (default)' do
397       let :facts do
398         {
399           os: { family: 'Debian', name: 'Debian', release: { major: '7', full: '7.0' } },
400           lsbdistid: 'Debian',
401           lsbdistcodename: 'wheezy',
402           osfamily: 'Debian',
403           puppetversion: Puppet.version,
404         }
405       end
406       let :params do
407         {
408           location: 'hello.there',
409         }
410       end
411
412       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(true) }
413     end
414
415     context 'with notify_update = true' do
416       let :facts do
417         {
418           os: { family: 'Debian', name: 'Debian', release: { major: '7', full: '7.0' } },
419           lsbdistid: 'Debian',
420           lsbdistcodename: 'wheezy',
421           osfamily: 'Debian',
422           puppetversion: Puppet.version,
423         }
424       end
425       let :params do
426         {
427           location: 'hello.there',
428           notify_update: true,
429         }
430       end
431
432       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(true) }
433     end
434
435     context 'with notify_update = false' do
436       let :facts do
437         {
438           os: { family: 'Debian', name: 'Debian', release: { major: '7', full: '7.0' } },
439           lsbdistid: 'Debian',
440           lsbdistcodename: 'wheezy',
441           osfamily: 'Debian',
442           puppetversion: Puppet.version,
443         }
444       end
445       let :params do
446         {
447           location: 'hello.there',
448           notify_update: false,
449         }
450       end
451
452       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(false) }
453     end
454   end
455 end