cb65c1e0012a12193a03ac9e6d854f3838ad38d6
[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 = "// This file is managed by Puppet. DO NOT EDIT.
419 machine deb.example.net login foologin password secret
420 machine apt.example.com login aptlogin password supersecret
421 "
422
423           it {
424             expect(subject).to contain_file('/etc/apt/auth.conf').with(ensure: 'present',
425                                                                        owner: '_apt',
426                                                                        group: 'root',
427                                                                        mode: '0600',
428                                                                        notify: 'Class[Apt::Update]',
429                                                                        content: sensitive(auth_conf_content))
430           }
431         end
432
433         context 'with manage_auth_conf => false' do
434           let(:params) do
435             super().merge(manage_auth_conf: false)
436           end
437
438           it {
439             expect(subject).not_to contain_file('/etc/apt/auth.conf')
440           }
441         end
442       end
443
444       context 'with improperly specified entries for /etc/apt/auth.conf' do
445         let(:params) do
446           {
447             auth_conf_entries: [
448               {
449                 machinn: 'deb.example.net',
450                 username: 'foologin',
451                 password: 'secret'
452               },
453               {
454                 machine: 'apt.example.com',
455                 login: 'aptlogin',
456                 password: 'supersecret'
457               },
458             ]
459           }
460         end
461
462         it { is_expected.to raise_error(Puppet::Error) }
463       end
464     end
465   end
466
467   context 'with sources defined on valid os.family' do
468     let :facts do
469       {
470         os: {
471           family: 'Debian',
472           name: 'Ubuntu',
473           release: {
474             major: '18',
475             full: '18.04'
476           },
477           distro: {
478             codename: 'bionic',
479             id: 'Ubuntu'
480           }
481         }
482       }
483     end
484     let(:params) do
485       { sources: {
486         'debian_unstable' => {
487           'location' => 'http://debian.mirror.iweb.ca/debian/',
488           'release' => 'unstable',
489           'repos' => 'main contrib non-free',
490           'key' => { 'id' => '150C8614919D8446E01E83AF9AA38DCD55BE302B', 'server' => 'subkeys.pgp.net' },
491           'pin' => '-10',
492           'include' => { 'src' => true }
493         },
494         'puppetlabs' => {
495           'location' => 'http://apt.puppetlabs.com',
496           'repos' => 'main',
497           'key' => { 'id' => '6F6B15509CF8E59E6E469F327F438280EF8D349F', 'server' => 'pgp.mit.edu' }
498         }
499       } }
500     end
501
502     it {
503       expect(subject).to contain_apt__setting('list-debian_unstable').with(ensure: 'present')
504     }
505
506     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$}) }
507     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$}) }
508
509     it {
510       expect(subject).to contain_apt__setting('list-puppetlabs').with(ensure: 'present')
511     }
512
513     it { is_expected.to contain_file('/etc/apt/sources.list.d/puppetlabs.list').with_content(%r{^deb http://apt.puppetlabs.com bionic main$}) }
514   end
515
516   context 'with confs defined on valid os.family' do
517     let :facts do
518       {
519         os: {
520           family: 'Debian',
521           name: 'Ubuntu',
522           release: {
523             major: '18',
524             full: '18.04'
525           },
526           distro: {
527             codename: 'bionic',
528             id: 'Ubuntu'
529           }
530         }
531       }
532     end
533     let(:params) do
534       { confs: {
535         'foo' => {
536           'content' => 'foo'
537         },
538         'bar' => {
539           'content' => 'bar'
540         }
541       } }
542     end
543
544     it {
545       expect(subject).to contain_apt__conf('foo').with(content: 'foo')
546     }
547
548     it {
549       expect(subject).to contain_apt__conf('bar').with(content: 'bar')
550     }
551   end
552
553   context 'with keys defined on valid os.family' do
554     let :facts do
555       {
556         os: {
557           family: 'Debian',
558           name: 'Ubuntu',
559           release: {
560             major: '18',
561             full: '18.04'
562           },
563           distro: {
564             codename: 'bionic',
565             id: 'Ubuntu'
566           }
567         }
568       }
569     end
570     let(:params) do
571       { keys: {
572         '55BE302B' => {
573           'server' => 'subkeys.pgp.net'
574         },
575         'EF8D349F' => {
576           'server' => 'pgp.mit.edu'
577         }
578       } }
579     end
580
581     it {
582       expect(subject).to contain_apt__key('55BE302B').with(server: 'subkeys.pgp.net')
583     }
584
585     it {
586       expect(subject).to contain_apt__key('EF8D349F').with(server: 'pgp.mit.edu')
587     }
588   end
589
590   context 'with ppas defined on valid os.family' do
591     let :facts do
592       {
593         os: {
594           family: 'Debian',
595           name: 'Ubuntu',
596           release: {
597             major: '18',
598             full: '18.04'
599           },
600           distro: {
601             codename: 'bionic',
602             id: 'Ubuntu'
603           }
604         }
605       }
606     end
607     let(:params) do
608       { ppas: {
609         'ppa:drizzle-developers/ppa' => {},
610         'ppa:nginx/stable' => {}
611       } }
612     end
613
614     it { is_expected.to contain_apt__ppa('ppa:drizzle-developers/ppa') }
615     it { is_expected.to contain_apt__ppa('ppa:nginx/stable') }
616   end
617
618   context 'with settings defined on valid os.family' do
619     let :facts do
620       {
621         os: {
622           family: 'Debian',
623           name: 'Ubuntu',
624           release: {
625             major: '18',
626             full: '18.04'
627           },
628           distro: {
629             codename: 'bionic',
630             id: 'Ubuntu'
631           }
632         }
633       }
634     end
635     let(:params) do
636       { settings: {
637         'conf-banana' => { 'content' => 'banana' },
638         'pref-banana' => { 'content' => 'banana' }
639       } }
640     end
641
642     it { is_expected.to contain_apt__setting('conf-banana') }
643     it { is_expected.to contain_apt__setting('pref-banana') }
644   end
645
646   context 'with pins defined on valid os.family' do
647     let :facts do
648       {
649         os: {
650           family: 'Debian',
651           name: 'Ubuntu',
652           release: {
653             major: '18',
654             full: '18.04'
655           },
656           distro: {
657             codename: 'bionic',
658             id: 'Ubuntu'
659           }
660         }
661       }
662     end
663     let(:params) do
664       { pins: {
665         'stable' => { 'priority' => 600, 'order' => 50 },
666         'testing' => { 'priority' => 700, 'order' => 100 }
667       } }
668     end
669
670     it { is_expected.to contain_apt__pin('stable') }
671     it { is_expected.to contain_apt__pin('testing') }
672   end
673
674   describe 'failing tests' do
675     context "with purge['sources.list']=>'banana'" do
676       let(:params) { { purge: { 'sources.list' => 'banana' } } }
677
678       it do
679         expect(subject).to raise_error(Puppet::Error)
680       end
681     end
682
683     context "with purge['sources.list.d']=>'banana'" do
684       let(:params) { { purge: { 'sources.list.d' => 'banana' } } }
685
686       it do
687         expect(subject).to raise_error(Puppet::Error)
688       end
689     end
690
691     context "with purge['preferences']=>'banana'" do
692       let(:params) { { purge: { 'preferences' => 'banana' } } }
693
694       it do
695         expect(subject).to raise_error(Puppet::Error)
696       end
697     end
698
699     context "with purge['preferences.d']=>'banana'" do
700       let(:params) { { purge: { 'preferences.d' => 'banana' } } }
701
702       it do
703         expect(subject).to raise_error(Puppet::Error)
704       end
705     end
706
707     context "with purge['apt.conf.d']=>'banana'" do
708       let(:params) { { purge: { 'apt.conf.d' => 'banana' } } }
709
710       it do
711         expect(subject).to raise_error(Puppet::Error)
712       end
713     end
714   end
715 end