(CONT-581) Adding deferred function support for password field
[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   let(:id) { '6F6B15509CF8E59E6E469F327F438280EF8D349F' }
7   let(:title) { 'my_source' }
8   let(:pre_condition) { 'class { "apt": }' }
9   let :facts do
10     {
11       os: {
12         family: 'Debian',
13         name: 'Debian',
14         release: {
15           major: '9',
16           full: '9.0'
17         },
18         distro: {
19           codename: 'stretch',
20           id: 'Debian'
21         }
22       }
23     }
24   end
25
26   context 'with defaults' do
27     context 'without location' do
28       it do
29         expect(subject).to raise_error(Puppet::Error, %r{source entry without specifying a location})
30       end
31     end
32
33     context 'with location' do
34       let(:params) { { location: 'hello.there' } }
35
36       it {
37         expect(subject).to contain_apt__setting('list-my_source').with(ensure: 'present').without_content(%r{# my_source\ndeb-src hello.there wheezy main\n})
38         expect(subject).not_to contain_package('apt-transport-https')
39       }
40     end
41   end
42
43   describe 'no defaults' do
44     context 'with complex pin' do
45       let :params do
46         {
47           location: 'hello.there',
48           pin: { 'release' => 'wishwash',
49                  'explanation' => 'wishwash',
50                  'priority' => 1001 }
51         }
52       end
53
54       it {
55         expect(subject).to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{hello.there stretch main\n})
56       }
57
58       it { is_expected.to contain_file('/etc/apt/sources.list.d/my_source.list').that_notifies('Class[Apt::Update]') }
59
60       it {
61         expect(subject).to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with(ensure: 'present',
62                                                                                                                 priority: 1001,
63                                                                                                                 explanation: 'wishwash',
64                                                                                                                 release: 'wishwash')
65       }
66     end
67
68     context 'with simple key' do
69       let :params do
70         {
71           comment: 'foo',
72           location: 'http://debian.mirror.iweb.ca/debian/',
73           release: 'sid',
74           repos: 'testing',
75           key: id,
76           pin: '10',
77           architecture: 'x86_64',
78           allow_unsigned: true
79         }
80       end
81
82       it {
83         expect(subject).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})
84                                                                  .without_content(%r{deb-src})
85       }
86
87       it {
88         expect(subject).to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with(ensure: 'present',
89                                                                                                                 priority: '10',
90                                                                                                                 origin: 'debian.mirror.iweb.ca')
91       }
92
93       it {
94         expect(subject).to contain_apt__key("Add key: #{id} from Apt::Source my_source").that_comes_before('Apt::Setting[list-my_source]').with(ensure: 'present',
95                                                                                                                                                 id: id)
96       }
97     end
98
99     context 'with complex key' do
100       let :params do
101         {
102           comment: 'foo',
103           location: 'http://debian.mirror.iweb.ca/debian/',
104           release: 'sid',
105           repos: 'testing',
106           key: {
107             'ensure' => 'refreshed',
108             'id' => id,
109             'server' => 'pgp.mit.edu',
110             'content' => 'GPG key content',
111             'source' => 'http://apt.puppetlabs.com/pubkey.gpg',
112             'weak_ssl' => true
113           },
114           pin: '10',
115           architecture: 'x86_64',
116           allow_unsigned: true
117         }
118       end
119
120       it {
121         expect(subject).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})
122                                                                  .without_content(%r{deb-src})
123       }
124
125       it {
126         expect(subject).to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with(ensure: 'present',
127                                                                                                                 priority: '10',
128                                                                                                                 origin: 'debian.mirror.iweb.ca')
129       }
130
131       it {
132         expect(subject).to contain_apt__key("Add key: #{id} from Apt::Source my_source").that_comes_before('Apt::Setting[list-my_source]').with(ensure: 'refreshed',
133                                                                                                                                                 id: id,
134                                                                                                                                                 server: 'pgp.mit.edu',
135                                                                                                                                                 content: 'GPG key content',
136                                                                                                                                                 source: 'http://apt.puppetlabs.com/pubkey.gpg',
137                                                                                                                                                 weak_ssl: true)
138       }
139     end
140   end
141
142   context 'with allow_insecure true' do
143     let :params do
144       {
145         location: 'hello.there',
146         allow_insecure: true
147       }
148     end
149
150     it {
151       expect(subject).to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# my_source\ndeb \[allow-insecure=yes\] hello.there stretch main\n})
152     }
153   end
154
155   context 'with allow_unsigned true' do
156     let :params do
157       {
158         location: 'hello.there',
159         allow_unsigned: true
160       }
161     end
162
163     it {
164       expect(subject).to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# my_source\ndeb \[trusted=yes\] hello.there stretch main\n})
165     }
166   end
167
168   context 'with check_valid_until false' do
169     let :params do
170       {
171         location: 'hello.there',
172         check_valid_until: false
173       }
174     end
175
176     it {
177       expect(subject).to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# my_source\ndeb \[check-valid-until=false\] hello.there stretch main\n})
178     }
179   end
180
181   context 'with check_valid_until true' do
182     let :params do
183       {
184         location: 'hello.there',
185         check_valid_until: true
186       }
187     end
188
189     it {
190       expect(subject).to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# my_source\ndeb hello.there stretch main\n})
191     }
192   end
193
194   context 'with keyring set' do
195     let :params do
196       {
197         location: 'hello.there',
198         keyring: '/usr/share/keyrings/foo-archive-keyring.gpg'
199       }
200     end
201
202     it {
203       expect(subject).to contain_apt__setting('list-my_source')
204         .with(ensure: 'present')
205         .with_content(%r{# my_source\ndeb \[signed-by=/usr/share/keyrings/foo-archive-keyring.gpg\] hello.there stretch main\n})
206     }
207   end
208
209   context 'with keyring, architecture and allow_unsigned set' do
210     let :params do
211       {
212         location: 'hello.there',
213         architecture: 'amd64',
214         allow_unsigned: true,
215         keyring: '/usr/share/keyrings/foo-archive-keyring.gpg'
216       }
217     end
218
219     it {
220       expect(subject).to contain_apt__setting('list-my_source')
221         .with(ensure: 'present')
222         .with_content(%r{# my_source\ndeb \[arch=amd64 trusted=yes signed-by=/usr/share/keyrings/foo-archive-keyring.gpg\] hello.there stretch main\n})
223     }
224   end
225
226   context 'with a https location, install apt-transport-https' do
227     let :params do
228       {
229         location: 'HTTPS://foo.bar',
230         allow_unsigned: false
231       }
232     end
233
234     it {
235       expect(subject).to contain_package('apt-transport-https')
236     }
237   end
238
239   context 'with a https location and custom release, install apt-transport-https' do
240     let :facts do
241       {
242         os: {
243           family: 'Debian',
244           name: 'Debian',
245           release: {
246             major: '9',
247             full: '9.0'
248           },
249           distro: {
250             codename: 'stretch',
251             id: 'Debian'
252           }
253         },
254         puppetversion: Puppet.version
255       }
256     end
257     let :params do
258       {
259         location: 'HTTPS://foo.bar',
260         allow_unsigned: false,
261         release: 'customrelease'
262       }
263     end
264
265     it {
266       expect(subject).to contain_package('apt-transport-https')
267     }
268   end
269
270   context 'with a https location, do not install apt-transport-https on oses not in list eg buster' do
271     let :facts do
272       {
273         os: {
274           family: 'Debian',
275           name: 'Debian',
276           release: {
277             major: '10',
278             full: '10.0'
279           },
280           distro: {
281             codename: 'buster',
282             id: 'Debian'
283           }
284         }
285       }
286     end
287     let :params do
288       {
289         location: 'https://foo.bar',
290         allow_unsigned: false
291       }
292     end
293
294     it {
295       expect(subject).not_to contain_package('apt-transport-https')
296     }
297   end
298
299   context 'with architecture equals x86_64' do
300     let :facts do
301       {
302         os: {
303           family: 'Debian',
304           name: 'Debian',
305           release: {
306             major: '7',
307             full: '7.0'
308           },
309           distro: {
310             codename: 'wheezy',
311             id: 'Debian'
312           }
313         }
314       }
315     end
316     let :params do
317       {
318         location: 'hello.there',
319         include: { 'deb' => false, 'src' => true },
320         architecture: 'x86_64'
321       }
322     end
323
324     it {
325       expect(subject).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})
326     }
327   end
328
329   context 'with architecture fact and unset architecture parameter' do
330     let :facts do
331       super().merge(architecture: 'amd64')
332     end
333     let :params do
334       {
335         location: 'hello.there',
336         include: { 'deb' => false, 'src' => true }
337       }
338     end
339
340     it {
341       expect(subject).to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# my_source\ndeb-src hello.there stretch main\n})
342     }
343   end
344
345   context 'with include_src => true' do
346     let :params do
347       {
348         location: 'hello.there',
349         include: { 'src' => true }
350       }
351     end
352
353     it {
354       expect(subject).to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# my_source\ndeb hello.there stretch main\ndeb-src hello.there stretch main\n})
355     }
356   end
357
358   context 'with include deb => false' do
359     let :params do
360       {
361         include: { 'deb' => false },
362         location: 'hello.there'
363       }
364     end
365
366     it {
367       expect(subject).to contain_apt__setting('list-my_source').with(ensure: 'present').without_content(%r{deb-src hello.there wheezy main\n})
368     }
369
370     it { is_expected.to contain_apt__setting('list-my_source').without_content(%r{deb hello.there wheezy main\n}) }
371   end
372
373   context 'with include src => true and include deb => false' do
374     let :params do
375       {
376         include: { 'deb' => false, 'src' => true },
377         location: 'hello.there'
378       }
379     end
380
381     it {
382       expect(subject).to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{deb-src hello.there stretch main\n})
383     }
384
385     it { is_expected.to contain_apt__setting('list-my_source').without_content(%r{deb hello.there stretch main\n}) }
386   end
387
388   context 'with ensure => absent' do
389     let :params do
390       {
391         ensure: 'absent'
392       }
393     end
394
395     it {
396       expect(subject).to contain_apt__setting('list-my_source').with(ensure: 'absent')
397     }
398   end
399
400   describe 'validation' do
401     context 'with no release' do
402       let :facts do
403         {
404           os: {
405             family: 'Debian',
406             name: 'Debian',
407             release: {
408               major: '8',
409               full: '8.0'
410             },
411             distro: {
412               id: 'Debian'
413             }
414           }
415         }
416       end
417       let(:params) { { location: 'hello.there' } }
418
419       it do
420         expect(subject).to raise_error(Puppet::Error, %r{os.distro.codename fact not available: release parameter required})
421       end
422     end
423
424     context 'with release is empty string' do
425       let(:params) { { location: 'hello.there', release: '' } }
426
427       it { is_expected.to contain_apt__setting('list-my_source').with_content(%r{hello\.there  main}) }
428     end
429
430     context 'with invalid pin' do
431       let :params do
432         {
433           location: 'hello.there',
434           pin: true
435         }
436       end
437
438       it do
439         expect(subject).to raise_error(Puppet::Error, %r{expects a value})
440       end
441     end
442
443     context 'with notify_update = undef (default)' do
444       let :params do
445         {
446           location: 'hello.there'
447         }
448       end
449
450       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(true) }
451     end
452
453     context 'with notify_update = true' do
454       let :params do
455         {
456           location: 'hello.there',
457           notify_update: true
458         }
459       end
460
461       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(true) }
462     end
463
464     context 'with notify_update = false' do
465       let :params do
466         {
467           location: 'hello.there',
468           notify_update: false
469         }
470       end
471
472       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(false) }
473     end
474   end
475 end