(CONT-773) Re-pin puppet_agent
[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       is_expected.to contain_file('sources.list').that_notifies('Class[Apt::Update]').only_with(sources_list)
62     }
63
64     it {
65       is_expected.to contain_file('sources.list.d').that_notifies('Class[Apt::Update]').only_with(sources_list_d)
66     }
67
68     it {
69       is_expected.to contain_file('preferences').that_notifies('Class[Apt::Update]').only_with(preferences)
70     }
71
72     it {
73       is_expected.to contain_file('preferences.d').that_notifies('Class[Apt::Update]').only_with(preferences_d)
74     }
75
76     it {
77       is_expected.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       is_expected.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       is_expected.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         is_expected.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         is_expected.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         is_expected.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         is_expected.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         is_expected.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         is_expected.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         is_expected.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         is_expected.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         is_expected.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         is_expected.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         is_expected.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       it {
218         is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
219           %r{Acquire::http::proxy "http://localhost:8080/";},
220         ).without_content(
221           %r{Acquire::https::proxy "DIRECT";},
222         )
223       }
224     end
225
226     context 'when ensure=absent' do
227       let(:params) { { proxy: { 'ensure' => 'absent' } } }
228
229       it {
230         is_expected.to contain_apt__setting('conf-proxy').with(ensure: 'absent',
231                                                                priority: '01')
232       }
233     end
234   end
235   context 'with lots of non-defaults' do
236     let :params do
237       {
238         update: { 'frequency' => 'always', 'timeout' => 1, 'tries' => 3 },
239         purge: { 'sources.list' => false, 'sources.list.d' => false,
240                  'preferences' => false, 'preferences.d' => false,
241                  'apt.conf.d' => false },
242       }
243     end
244
245     it {
246       is_expected.to contain_file('sources.list').with(content: nil)
247     }
248
249     it {
250       is_expected.to contain_file('sources.list.d').with(purge: false,
251                                                          recurse: false)
252     }
253
254     it {
255       is_expected.to contain_file('preferences').with(ensure: 'file')
256     }
257
258     it {
259       is_expected.to contain_file('preferences.d').with(purge: false,
260                                                         recurse: false)
261     }
262
263     it {
264       is_expected.to contain_file('apt.conf.d').with(purge: false,
265                                                      recurse: false)
266     }
267
268     it {
269       is_expected.to contain_exec('apt_update').with(refreshonly: false,
270                                                      timeout: 1,
271                                                      tries: 3)
272     }
273   end
274
275   context 'with lots of non-defaults' do
276     let :params do
277       {
278         update: { 'frequency' => 'always', 'timeout' => 1, 'tries' => 3 },
279         purge: { 'sources.list' => true, 'sources.list.d' => true,
280                  'preferences' => true, 'preferences.d' => true,
281                  'apt.conf.d' => true },
282       }
283     end
284
285     it {
286       is_expected.to contain_file('sources.list').with(content: "# Repos managed by puppet.\n")
287     }
288
289     it {
290       is_expected.to contain_file('sources.list.d').with(purge: true,
291                                                          recurse: true)
292     }
293
294     it {
295       is_expected.to contain_file('preferences').with(ensure: 'absent')
296     }
297
298     it {
299       is_expected.to contain_file('preferences.d').with(purge: true,
300                                                         recurse: true)
301     }
302
303     it {
304       is_expected.to contain_file('apt.conf.d').with(purge: true,
305                                                      recurse: true)
306     }
307
308     it {
309       is_expected.to contain_exec('apt_update').with(refreshonly: false,
310                                                      timeout: 1,
311                                                      tries: 3)
312     }
313   end
314
315   context 'with defaults for sources_list_force' do
316     let :params do
317       {
318         update: { 'frequency' => 'always', 'timeout' => 1, 'tries' => 3 },
319         purge: { 'sources.list' => true },
320         sources_list_force: false,
321       }
322     end
323
324     it {
325       is_expected.to contain_file('sources.list').with(content: "# Repos managed by puppet.\n")
326     }
327   end
328
329   context 'with non defaults for sources_list_force' do
330     let :params do
331       {
332         update: { 'frequency' => 'always', 'timeout' => 1, 'tries' => 3 },
333         purge: { 'sources.list' => true },
334         sources_list_force: true,
335       }
336     end
337
338     it {
339       is_expected.to contain_file('sources.list').with(ensure: 'absent')
340     }
341   end
342
343   context 'with entries for /etc/apt/auth.conf' do
344     facts_hash = {
345       'Ubuntu 18.04' => {
346         os: {
347           family: 'Debian',
348           name: 'Ubuntu',
349           release: {
350             major: '18',
351             full: '18.04',
352           },
353           distro: {
354             codename: 'bionic',
355             id: 'Ubuntu',
356           },
357         },
358       },
359       'Debian 9.0' => {
360         os: {
361           family: 'Debian',
362           name: 'Debian',
363           release: {
364             major: '9',
365             full: '9.0',
366           },
367           distro: {
368             codename: 'stretch',
369             id: 'Debian',
370           },
371         },
372       },
373       'Debian 10.0' => {
374         os: {
375           family: 'Debian',
376           name: 'Debian',
377           release: {
378             major: '10',
379             full: '10.0',
380           },
381           distro: {
382             codename: 'buster',
383             id: 'Debian',
384           },
385         },
386       },
387     }
388
389     facts_hash.each do |os, facts|
390       context "on #{os}" do
391         let(:facts) do
392           facts
393         end
394         let(:params) do
395           {
396             auth_conf_entries: [
397               {
398                 machine: 'deb.example.net',
399                 login: 'foologin',
400                 password: 'secret',
401               },
402               {
403                 machine: 'apt.example.com',
404                 login: 'aptlogin',
405                 password: 'supersecret',
406               },
407             ],
408           }
409         end
410
411         context 'with manage_auth_conf => true' do
412           let(:params) do
413             super().merge(manage_auth_conf: true)
414           end
415
416           auth_conf_content = "// This file is managed by Puppet. DO NOT EDIT.
417 machine deb.example.net login foologin password secret
418 machine apt.example.com login aptlogin password supersecret
419 "
420
421           it {
422             is_expected.to contain_file('/etc/apt/auth.conf').with(ensure: 'present',
423                                                                    owner: '_apt',
424                                                                    group: 'root',
425                                                                    mode: '0600',
426                                                                    notify: 'Class[Apt::Update]',
427                                                                    content: sensitive(auth_conf_content))
428           }
429         end
430
431         context 'with manage_auth_conf => false' do
432           let(:params) do
433             super().merge(manage_auth_conf: false)
434           end
435
436           it {
437             is_expected.not_to contain_file('/etc/apt/auth.conf')
438           }
439         end
440       end
441
442       context 'with improperly specified entries for /etc/apt/auth.conf' do
443         let(:params) do
444           {
445             auth_conf_entries: [
446               {
447                 machinn: 'deb.example.net',
448                 username: 'foologin',
449                 password: 'secret',
450               },
451               {
452                 machine: 'apt.example.com',
453                 login: 'aptlogin',
454                 password: 'supersecret',
455               },
456             ],
457           }
458         end
459
460         it { is_expected.to raise_error(Puppet::Error) }
461       end
462     end
463   end
464
465   context 'with sources defined on valid os.family' do
466     let :facts do
467       {
468         os: {
469           family: 'Debian',
470           name: 'Ubuntu',
471           release: {
472             major: '18',
473             full: '18.04',
474           },
475           distro: {
476             codename: 'bionic',
477             id: 'Ubuntu',
478           },
479         },
480       }
481     end
482     let(:params) do
483       { sources: {
484         'debian_unstable' => {
485           'location'          => 'http://debian.mirror.iweb.ca/debian/',
486           'release'           => 'unstable',
487           'repos'             => 'main contrib non-free',
488           'key'               => { 'id' => '150C8614919D8446E01E83AF9AA38DCD55BE302B', 'server' => 'subkeys.pgp.net' },
489           'pin'               => '-10',
490           'include'           => { 'src' => true },
491         },
492         'puppetlabs' => {
493           'location' => 'http://apt.puppetlabs.com',
494           'repos'      => 'main',
495           'key'        => { 'id' => '6F6B15509CF8E59E6E469F327F438280EF8D349F', 'server' => 'pgp.mit.edu' },
496         },
497       } }
498     end
499
500     it {
501       is_expected.to contain_apt__setting('list-debian_unstable').with(ensure: 'present')
502     }
503
504     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$}) }
505     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$}) }
506
507     it {
508       is_expected.to contain_apt__setting('list-puppetlabs').with(ensure: 'present')
509     }
510
511     it { is_expected.to contain_file('/etc/apt/sources.list.d/puppetlabs.list').with_content(%r{^deb http://apt.puppetlabs.com bionic main$}) }
512   end
513
514   context 'with confs defined on valid os.family' do
515     let :facts do
516       {
517         os: {
518           family: 'Debian',
519           name: 'Ubuntu',
520           release: {
521             major: '18',
522             full: '18.04',
523           },
524           distro: {
525             codename: 'bionic',
526             id: 'Ubuntu',
527           },
528         },
529       }
530     end
531     let(:params) do
532       { confs: {
533         'foo' => {
534           'content' => 'foo',
535         },
536         'bar' => {
537           'content' => 'bar',
538         },
539       } }
540     end
541
542     it {
543       is_expected.to contain_apt__conf('foo').with(content: 'foo')
544     }
545
546     it {
547       is_expected.to contain_apt__conf('bar').with(content: 'bar')
548     }
549   end
550
551   context 'with keys defined on valid os.family' do
552     let :facts do
553       {
554         os: {
555           family: 'Debian',
556           name: 'Ubuntu',
557           release: {
558             major: '18',
559             full: '18.04',
560           },
561           distro: {
562             codename: 'bionic',
563             id: 'Ubuntu',
564           },
565         },
566       }
567     end
568     let(:params) do
569       { keys: {
570         '55BE302B' => {
571           'server' => 'subkeys.pgp.net',
572         },
573         'EF8D349F' => {
574           'server' => 'pgp.mit.edu',
575         },
576       } }
577     end
578
579     it {
580       is_expected.to contain_apt__key('55BE302B').with(server: 'subkeys.pgp.net')
581     }
582
583     it {
584       is_expected.to contain_apt__key('EF8D349F').with(server: 'pgp.mit.edu')
585     }
586   end
587
588   context 'with ppas defined on valid os.family' do
589     let :facts do
590       {
591         os: {
592           family: 'Debian',
593           name: 'Ubuntu',
594           release: {
595             major: '18',
596             full: '18.04',
597           },
598           distro: {
599             codename: 'bionic',
600             id: 'Ubuntu',
601           },
602         },
603       }
604     end
605     let(:params) do
606       { ppas: {
607         'ppa:drizzle-developers/ppa' => {},
608         'ppa:nginx/stable' => {},
609       } }
610     end
611
612     it { is_expected.to contain_apt__ppa('ppa:drizzle-developers/ppa') }
613     it { is_expected.to contain_apt__ppa('ppa:nginx/stable') }
614   end
615
616   context 'with settings defined on valid os.family' do
617     let :facts do
618       {
619         os: {
620           family: 'Debian',
621           name: 'Ubuntu',
622           release: {
623             major: '18',
624             full: '18.04',
625           },
626           distro: {
627             codename: 'bionic',
628             id: 'Ubuntu',
629           },
630         },
631       }
632     end
633     let(:params) do
634       { settings: {
635         'conf-banana' => { 'content' => 'banana' },
636         'pref-banana' => { 'content' => 'banana' },
637       } }
638     end
639
640     it { is_expected.to contain_apt__setting('conf-banana') }
641     it { is_expected.to contain_apt__setting('pref-banana') }
642   end
643
644   context 'with pins defined on valid os.family' do
645     let :facts do
646       {
647         os: {
648           family: 'Debian',
649           name: 'Ubuntu',
650           release: {
651             major: '18',
652             full: '18.04',
653           },
654           distro: {
655             codename: 'bionic',
656             id: 'Ubuntu',
657           },
658         },
659       }
660     end
661     let(:params) do
662       { pins: {
663         'stable' => { 'priority' => 600, 'order' => 50 },
664         'testing' =>  { 'priority' => 700, 'order' => 100 },
665       } }
666     end
667
668     it { is_expected.to contain_apt__pin('stable') }
669     it { is_expected.to contain_apt__pin('testing') }
670   end
671
672   describe 'failing tests' do
673     context "with purge['sources.list']=>'banana'" do
674       let(:params) { { purge: { 'sources.list' => 'banana' } } }
675
676       it do
677         is_expected.to raise_error(Puppet::Error)
678       end
679     end
680
681     context "with purge['sources.list.d']=>'banana'" do
682       let(:params) { { purge: { 'sources.list.d' => 'banana' } } }
683
684       it do
685         is_expected.to raise_error(Puppet::Error)
686       end
687     end
688
689     context "with purge['preferences']=>'banana'" do
690       let(:params) { { purge: { 'preferences' => 'banana' } } }
691
692       it do
693         is_expected.to raise_error(Puppet::Error)
694       end
695     end
696
697     context "with purge['preferences.d']=>'banana'" do
698       let(:params) { { purge: { 'preferences.d' => 'banana' } } }
699
700       it do
701         is_expected.to raise_error(Puppet::Error)
702       end
703     end
704
705     context "with purge['apt.conf.d']=>'banana'" do
706       let(:params) { { purge: { 'apt.conf.d' => 'banana' } } }
707
708       it do
709         is_expected.to raise_error(Puppet::Error)
710       end
711     end
712   end
713 end