(CONT-581) Adding deferred function support for password field
[puppet-modules/puppetlabs-apt.git] / spec / classes / apt_spec.rb
1 # frozen_string_literal: true
2
3 require 'spec_helper'
4
5 sources_list = {  ensure: 'file',
6                   path: '/etc/apt/sources.list',
7                   owner: 'root',
8                   group: 'root',
9                   notify: 'Class[Apt::Update]' }
10
11 sources_list_d = { ensure: 'directory',
12                    path: '/etc/apt/sources.list.d',
13                    owner: 'root',
14                    group: 'root',
15                    purge: false,
16                    recurse: false,
17                    notify: 'Class[Apt::Update]' }
18
19 preferences = { ensure: 'file',
20                 path: '/etc/apt/preferences',
21                 owner: 'root',
22                 group: 'root',
23                 notify: 'Class[Apt::Update]' }
24
25 preferences_d = { ensure: 'directory',
26                   path: '/etc/apt/preferences.d',
27                   owner: 'root',
28                   group: 'root',
29                   purge: false,
30                   recurse: false,
31                   notify: 'Class[Apt::Update]' }
32
33 apt_conf_d = {    ensure: 'directory',
34                   path: '/etc/apt/apt.conf.d',
35                   owner: 'root',
36                   group: 'root',
37                   purge: false,
38                   recurse: false,
39                   notify: 'Class[Apt::Update]' }
40
41 describe 'apt' do
42   let(:facts) do
43     {
44       os: {
45         family: 'Debian',
46         name: 'Debian',
47         release: {
48           major: '9',
49           full: '9.0'
50         },
51         distro: {
52           codename: 'stretch',
53           id: 'Debian'
54         }
55       }
56     }
57   end
58
59   context 'with defaults' do
60     it {
61       expect(subject).to contain_file('sources.list').that_notifies('Class[Apt::Update]').only_with(sources_list)
62     }
63
64     it {
65       expect(subject).to contain_file('sources.list.d').that_notifies('Class[Apt::Update]').only_with(sources_list_d)
66     }
67
68     it {
69       expect(subject).to contain_file('preferences').that_notifies('Class[Apt::Update]').only_with(preferences)
70     }
71
72     it {
73       expect(subject).to contain_file('preferences.d').that_notifies('Class[Apt::Update]').only_with(preferences_d)
74     }
75
76     it {
77       expect(subject).to contain_file('apt.conf.d').that_notifies('Class[Apt::Update]').only_with(apt_conf_d)
78     }
79
80     it { is_expected.to contain_file('/etc/apt/auth.conf').with_ensure('absent') }
81
82     it 'lays down /etc/apt/apt.conf.d/15update-stamp' do
83       expect(subject).to contain_file('/etc/apt/apt.conf.d/15update-stamp').with(group: 'root',
84                                                                                  owner: 'root').with_content(
85                                                                                    %r{APT::Update::Post-Invoke-Success {"touch /var/lib/apt/periodic/update-success-stamp 2>/dev/null || true";};},
86                                                                                  )
87     end
88
89     it {
90       expect(subject).to contain_exec('apt_update').with(refreshonly: 'true')
91     }
92
93     it { is_expected.not_to contain_apt__setting('conf-proxy') }
94   end
95
96   describe 'proxy=' do
97     context 'when host=localhost' do
98       let(:params) { { proxy: { 'host' => 'localhost' } } }
99
100       it {
101         expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
102           %r{Acquire::http::proxy "http://localhost:8080/";},
103         ).without_content(
104           %r{Acquire::https::proxy },
105         )
106       }
107     end
108
109     context 'when host=localhost and per-host[proxyscope]=proxyhost' do
110       let(:params) { { proxy: { 'host' => 'localhost', 'perhost' => [{ 'scope' => 'proxyscope', 'host' => 'proxyhost' }] } } }
111
112       it {
113         expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
114           %r{Acquire::http::proxy::proxyscope "http://proxyhost:8080/";},
115         )
116       }
117     end
118
119     context 'when host=localhost and per-host[proxyscope]=proxyhost:8081' do
120       let(:params) { { proxy: { 'host' => 'localhost', 'perhost' => [{ 'scope' => 'proxyscope', 'host' => 'proxyhost', 'port' => 8081 }] } } }
121
122       it {
123         expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
124           %r{Acquire::http::proxy::proxyscope "http://proxyhost:8081/";},
125         )
126       }
127     end
128
129     context 'when host=localhost and per-host[proxyscope]=[https]proxyhost' do
130       let(:params) { { proxy: { 'host' => 'localhost', 'perhost' => [{ 'scope' => 'proxyscope', 'host' => 'proxyhost', 'https' => true }] } } }
131
132       it {
133         expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
134           %r{Acquire::https::proxy::proxyscope "https://proxyhost:8080/";},
135         )
136       }
137     end
138
139     context 'when host=localhost and per-host[proxyscope]=[direct]' do
140       let(:params) { { proxy: { 'host' => 'localhost', 'perhost' => [{ 'scope' => 'proxyscope', 'direct' => true }] } } }
141
142       it {
143         expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
144           %r{Acquire::http::proxy::proxyscope "DIRECT";},
145         )
146       }
147     end
148
149     context 'when host=localhost and per-host[proxyscope]=[https][direct]' do
150       let(:params) { { proxy: { 'host' => 'localhost', 'perhost' => [{ 'scope' => 'proxyscope', 'https' => true, 'direct' => true }] } } }
151
152       it {
153         expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
154           %r{Acquire::https::proxy::proxyscope "DIRECT";},
155         )
156       }
157     end
158
159     context 'when host=localhost and per-host[proxyscope]=proxyhost and per-host[proxyscope2]=proxyhost2' do
160       let(:params) { { proxy: { 'host' => 'localhost', 'perhost' => [{ 'scope' => 'proxyscope', 'host' => 'proxyhost' }, { 'scope' => 'proxyscope2', 'host' => 'proxyhost2' }] } } }
161
162       it {
163         expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
164           %r{Acquire::http::proxy::proxyscope "http://proxyhost:8080/";},
165         ).with_content(
166           %r{Acquire::http::proxy::proxyscope2 "http://proxyhost2:8080/";},
167         )
168       }
169     end
170
171     context 'when host=localhost and port=8180' do
172       let(:params) { { proxy: { 'host' => 'localhost', 'port' => 8180 } } }
173
174       it {
175         expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
176           %r{Acquire::http::proxy "http://localhost:8180/";},
177         ).without_content(
178           %r{Acquire::https::proxy },
179         )
180       }
181     end
182
183     context 'when host=localhost and https=true' do
184       let(:params) { { proxy: { 'host' => 'localhost', 'https' => true } } }
185
186       it {
187         expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
188           %r{Acquire::http::proxy "http://localhost:8080/";},
189         ).with_content(
190           %r{Acquire::https::proxy "https://localhost:8080/";},
191         )
192       }
193     end
194
195     context 'when host=localhost and direct=true' do
196       let(:params) { { proxy: { 'host' => 'localhost', 'direct' => true } } }
197
198       it {
199         expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
200           %r{Acquire::http::proxy "http://localhost:8080/";},
201         ).with_content(
202           %r{Acquire::https::proxy "DIRECT";},
203         )
204       }
205     end
206
207     context 'when host=localhost and https=true and direct=true' do
208       let(:params) { { proxy: { 'host' => 'localhost', 'https' => true, 'direct' => true } } }
209
210       it {
211         expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
212           %r{Acquire::http::proxy "http://localhost:8080/";},
213         ).with_content(
214           %r{Acquire::https::proxy "https://localhost:8080/";},
215         )
216       }
217
218       it {
219         expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
220           %r{Acquire::http::proxy "http://localhost:8080/";},
221         ).without_content(
222           %r{Acquire::https::proxy "DIRECT";},
223         )
224       }
225     end
226
227     context 'when ensure=absent' do
228       let(:params) { { proxy: { 'ensure' => 'absent' } } }
229
230       it {
231         expect(subject).to contain_apt__setting('conf-proxy').with(ensure: 'absent',
232                                                                    priority: '01')
233       }
234     end
235   end
236
237   context 'with lots of non-defaults - one' do
238     let :params do
239       {
240         update: { 'frequency' => 'always', 'timeout' => 1, 'tries' => 3 },
241         purge: { 'sources.list' => false, 'sources.list.d' => false,
242                  'preferences' => false, 'preferences.d' => false,
243                  'apt.conf.d' => false }
244       }
245     end
246
247     it {
248       expect(subject).to contain_file('sources.list').with(content: nil)
249     }
250
251     it {
252       expect(subject).to contain_file('sources.list.d').with(purge: false,
253                                                              recurse: false)
254     }
255
256     it {
257       expect(subject).to contain_file('preferences').with(ensure: 'file')
258     }
259
260     it {
261       expect(subject).to contain_file('preferences.d').with(purge: false,
262                                                             recurse: false)
263     }
264
265     it {
266       expect(subject).to contain_file('apt.conf.d').with(purge: false,
267                                                          recurse: false)
268     }
269
270     it {
271       expect(subject).to contain_exec('apt_update').with(refreshonly: false,
272                                                          timeout: 1,
273                                                          tries: 3)
274     }
275   end
276
277   context 'with lots of non-defaults - two' do
278     let :params do
279       {
280         update: { 'frequency' => 'always', 'timeout' => 1, 'tries' => 3 },
281         purge: { 'sources.list' => true, 'sources.list.d' => true,
282                  'preferences' => true, 'preferences.d' => true,
283                  'apt.conf.d' => true }
284       }
285     end
286
287     it {
288       expect(subject).to contain_file('sources.list').with(content: "# Repos managed by puppet.\n")
289     }
290
291     it {
292       expect(subject).to contain_file('sources.list.d').with(purge: true,
293                                                              recurse: true)
294     }
295
296     it {
297       expect(subject).to contain_file('preferences').with(ensure: 'absent')
298     }
299
300     it {
301       expect(subject).to contain_file('preferences.d').with(purge: true,
302                                                             recurse: true)
303     }
304
305     it {
306       expect(subject).to contain_file('apt.conf.d').with(purge: true,
307                                                          recurse: true)
308     }
309
310     it {
311       expect(subject).to contain_exec('apt_update').with(refreshonly: false,
312                                                          timeout: 1,
313                                                          tries: 3)
314     }
315   end
316
317   context 'with defaults for sources_list_force' do
318     let :params do
319       {
320         update: { 'frequency' => 'always', 'timeout' => 1, 'tries' => 3 },
321         purge: { 'sources.list' => true },
322         sources_list_force: false
323       }
324     end
325
326     it {
327       expect(subject).to contain_file('sources.list').with(content: "# Repos managed by puppet.\n")
328     }
329   end
330
331   context 'with non defaults for sources_list_force' do
332     let :params do
333       {
334         update: { 'frequency' => 'always', 'timeout' => 1, 'tries' => 3 },
335         purge: { 'sources.list' => true },
336         sources_list_force: true
337       }
338     end
339
340     it {
341       expect(subject).to contain_file('sources.list').with(ensure: 'absent')
342     }
343   end
344
345   context 'with entries for /etc/apt/auth.conf' do
346     facts_hash = {
347       'Ubuntu 18.04' => {
348         os: {
349           family: 'Debian',
350           name: 'Ubuntu',
351           release: {
352             major: '18',
353             full: '18.04'
354           },
355           distro: {
356             codename: 'bionic',
357             id: 'Ubuntu'
358           }
359         }
360       },
361       'Debian 9.0' => {
362         os: {
363           family: 'Debian',
364           name: 'Debian',
365           release: {
366             major: '9',
367             full: '9.0'
368           },
369           distro: {
370             codename: 'stretch',
371             id: 'Debian'
372           }
373         }
374       },
375       'Debian 10.0' => {
376         os: {
377           family: 'Debian',
378           name: 'Debian',
379           release: {
380             major: '10',
381             full: '10.0'
382           },
383           distro: {
384             codename: 'buster',
385             id: 'Debian'
386           }
387         }
388       }
389     }
390
391     facts_hash.each do |os, facts|
392       context "when on #{os}" do
393         let(:facts) do
394           facts
395         end
396         let(:params) do
397           {
398             auth_conf_entries: [
399               {
400                 machine: 'deb.example.net',
401                 login: 'foologin',
402                 password: 'secret'
403               },
404               {
405                 machine: 'apt.example.com',
406                 login: 'aptlogin',
407                 password: 'supersecret'
408               },
409             ]
410           }
411         end
412
413         context 'with manage_auth_conf => true' do
414           let(:params) do
415             super().merge(manage_auth_conf: true)
416           end
417
418           auth_conf_content = <<~CONTENT
419             // This file is managed by Puppet. DO NOT EDIT.
420             machine deb.example.net login foologin password secret
421             machine apt.example.com login aptlogin password supersecret
422           CONTENT
423
424           it {
425             expect(subject).to contain_file('/etc/apt/auth.conf').with(ensure: 'present',
426                                                                        owner: '_apt',
427                                                                        group: 'root',
428                                                                        mode: '0600',
429                                                                        notify: 'Class[Apt::Update]',
430                                                                        content: sensitive(auth_conf_content))
431           }
432         end
433
434         context 'with manage_auth_conf => false' do
435           let(:params) do
436             super().merge(manage_auth_conf: false)
437           end
438
439           it {
440             expect(subject).not_to contain_file('/etc/apt/auth.conf')
441           }
442         end
443       end
444
445       context 'with improperly specified entries for /etc/apt/auth.conf' do
446         let(:params) do
447           {
448             auth_conf_entries: [
449               {
450                 machinn: 'deb.example.net',
451                 username: 'foologin',
452                 password: 'secret'
453               },
454               {
455                 machine: 'apt.example.com',
456                 login: 'aptlogin',
457                 password: 'supersecret'
458               },
459             ]
460           }
461         end
462
463         it { is_expected.to raise_error(Puppet::Error) }
464       end
465     end
466   end
467
468   context 'with sources defined on valid os.family' do
469     let :facts do
470       {
471         os: {
472           family: 'Debian',
473           name: 'Ubuntu',
474           release: {
475             major: '18',
476             full: '18.04'
477           },
478           distro: {
479             codename: 'bionic',
480             id: 'Ubuntu'
481           }
482         }
483       }
484     end
485     let(:params) do
486       { sources: {
487         'debian_unstable' => {
488           'location' => 'http://debian.mirror.iweb.ca/debian/',
489           'release' => 'unstable',
490           'repos' => 'main contrib non-free',
491           'key' => { 'id' => '150C8614919D8446E01E83AF9AA38DCD55BE302B', 'server' => 'subkeys.pgp.net' },
492           'pin' => '-10',
493           'include' => { 'src' => true }
494         },
495         'puppetlabs' => {
496           'location' => 'http://apt.puppetlabs.com',
497           'repos' => 'main',
498           'key' => { 'id' => '6F6B15509CF8E59E6E469F327F438280EF8D349F', 'server' => 'pgp.mit.edu' }
499         }
500       } }
501     end
502
503     it {
504       expect(subject).to contain_apt__setting('list-debian_unstable').with(ensure: 'present')
505     }
506
507     it { is_expected.to contain_file('/etc/apt/sources.list.d/debian_unstable.list').with_content(%r{^deb http://debian.mirror.iweb.ca/debian/ unstable main contrib non-free$}) }
508     it { is_expected.to contain_file('/etc/apt/sources.list.d/debian_unstable.list').with_content(%r{^deb-src http://debian.mirror.iweb.ca/debian/ unstable main contrib non-free$}) }
509
510     it {
511       expect(subject).to contain_apt__setting('list-puppetlabs').with(ensure: 'present')
512     }
513
514     it { is_expected.to contain_file('/etc/apt/sources.list.d/puppetlabs.list').with_content(%r{^deb http://apt.puppetlabs.com bionic main$}) }
515   end
516
517   context 'with confs defined on valid os.family' do
518     let :facts do
519       {
520         os: {
521           family: 'Debian',
522           name: 'Ubuntu',
523           release: {
524             major: '18',
525             full: '18.04'
526           },
527           distro: {
528             codename: 'bionic',
529             id: 'Ubuntu'
530           }
531         }
532       }
533     end
534     let(:params) do
535       { confs: {
536         'foo' => {
537           'content' => 'foo'
538         },
539         'bar' => {
540           'content' => 'bar'
541         }
542       } }
543     end
544
545     it {
546       expect(subject).to contain_apt__conf('foo').with(content: 'foo')
547     }
548
549     it {
550       expect(subject).to contain_apt__conf('bar').with(content: 'bar')
551     }
552   end
553
554   context 'with keys defined on valid os.family' do
555     let :facts do
556       {
557         os: {
558           family: 'Debian',
559           name: 'Ubuntu',
560           release: {
561             major: '18',
562             full: '18.04'
563           },
564           distro: {
565             codename: 'bionic',
566             id: 'Ubuntu'
567           }
568         }
569       }
570     end
571     let(:params) do
572       { keys: {
573         '55BE302B' => {
574           'server' => 'subkeys.pgp.net'
575         },
576         'EF8D349F' => {
577           'server' => 'pgp.mit.edu'
578         }
579       } }
580     end
581
582     it {
583       expect(subject).to contain_apt__key('55BE302B').with(server: 'subkeys.pgp.net')
584     }
585
586     it {
587       expect(subject).to contain_apt__key('EF8D349F').with(server: 'pgp.mit.edu')
588     }
589   end
590
591   context 'with ppas defined on valid os.family' do
592     let :facts do
593       {
594         os: {
595           family: 'Debian',
596           name: 'Ubuntu',
597           release: {
598             major: '18',
599             full: '18.04'
600           },
601           distro: {
602             codename: 'bionic',
603             id: 'Ubuntu'
604           }
605         }
606       }
607     end
608     let(:params) do
609       { ppas: {
610         'ppa:drizzle-developers/ppa' => {},
611         'ppa:nginx/stable' => {}
612       } }
613     end
614
615     it { is_expected.to contain_apt__ppa('ppa:drizzle-developers/ppa') }
616     it { is_expected.to contain_apt__ppa('ppa:nginx/stable') }
617   end
618
619   context 'with settings defined on valid os.family' do
620     let :facts do
621       {
622         os: {
623           family: 'Debian',
624           name: 'Ubuntu',
625           release: {
626             major: '18',
627             full: '18.04'
628           },
629           distro: {
630             codename: 'bionic',
631             id: 'Ubuntu'
632           }
633         }
634       }
635     end
636     let(:params) do
637       { settings: {
638         'conf-banana' => { 'content' => 'banana' },
639         'pref-banana' => { 'content' => 'banana' }
640       } }
641     end
642
643     it { is_expected.to contain_apt__setting('conf-banana') }
644     it { is_expected.to contain_apt__setting('pref-banana') }
645   end
646
647   context 'with pins defined on valid os.family' do
648     let :facts do
649       {
650         os: {
651           family: 'Debian',
652           name: 'Ubuntu',
653           release: {
654             major: '18',
655             full: '18.04'
656           },
657           distro: {
658             codename: 'bionic',
659             id: 'Ubuntu'
660           }
661         }
662       }
663     end
664     let(:params) do
665       { pins: {
666         'stable' => { 'priority' => 600, 'order' => 50 },
667         'testing' => { 'priority' => 700, 'order' => 100 }
668       } }
669     end
670
671     it { is_expected.to contain_apt__pin('stable') }
672     it { is_expected.to contain_apt__pin('testing') }
673   end
674
675   describe 'failing tests' do
676     context "with purge['sources.list']=>'banana'" do
677       let(:params) { { purge: { 'sources.list' => 'banana' } } }
678
679       it do
680         expect(subject).to raise_error(Puppet::Error)
681       end
682     end
683
684     context "with purge['sources.list.d']=>'banana'" do
685       let(:params) { { purge: { 'sources.list.d' => 'banana' } } }
686
687       it do
688         expect(subject).to raise_error(Puppet::Error)
689       end
690     end
691
692     context "with purge['preferences']=>'banana'" do
693       let(:params) { { purge: { 'preferences' => 'banana' } } }
694
695       it do
696         expect(subject).to raise_error(Puppet::Error)
697       end
698     end
699
700     context "with purge['preferences.d']=>'banana'" do
701       let(:params) { { purge: { 'preferences.d' => 'banana' } } }
702
703       it do
704         expect(subject).to raise_error(Puppet::Error)
705       end
706     end
707
708     context "with purge['apt.conf.d']=>'banana'" do
709       let(:params) { { purge: { 'apt.conf.d' => 'banana' } } }
710
711       it do
712         expect(subject).to raise_error(Puppet::Error)
713       end
714     end
715   end
716 end