(MODULES-10763) Remove frequency collector
[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: '8',
49           full: '8.0',
50         },
51         distro: {
52           codename: 'jessie',
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 14.04' => {
346         os: {
347           family: 'Debian',
348           name: 'Ubuntu',
349           release: {
350             major: '14',
351             full: '14.04',
352           },
353           distro: {
354             codename: 'trusty',
355             id: 'Ubuntu',
356           },
357         },
358       },
359       'Ubuntu 16.04' => {
360         os: {
361           family: 'Debian',
362           name: 'Ubuntu',
363           release: {
364             major: '16',
365             full: '16.04',
366           },
367           distro: {
368             codename: 'xenial',
369             id: 'Ubuntu',
370           },
371         },
372       },
373       'Ubuntu 18.04' => {
374         os: {
375           family: 'Debian',
376           name: 'Ubuntu',
377           release: {
378             major: '18',
379             full: '18.04',
380           },
381           distro: {
382             codename: 'bionic',
383             id: 'Ubuntu',
384           },
385         },
386       },
387       'Debian 7.0' => {
388         os: {
389           family: 'Debian',
390           name: 'Debian',
391           release: {
392             major: '7',
393             full: '7.0',
394           },
395           distro: {
396             codename: 'wheezy',
397             id: 'Debian',
398           },
399         },
400       },
401       'Debian 8.0' => {
402         os: {
403           family: 'Debian',
404           name: 'Debian',
405           release: {
406             major: '8',
407             full: '8.0',
408           },
409           distro: {
410             codename: 'jessie',
411             id: 'Debian',
412           },
413         },
414       },
415       'Debian 9.0' => {
416         os: {
417           family: 'Debian',
418           name: 'Debian',
419           release: {
420             major: '9',
421             full: '9.0',
422           },
423           distro: {
424             codename: 'stretch',
425             id: 'Debian',
426           },
427         },
428       },
429       'Debian 10.0' => {
430         os: {
431           family: 'Debian',
432           name: 'Debian',
433           release: {
434             major: '10',
435             full: '10.0',
436           },
437           distro: {
438             codename: 'buster',
439             id: 'Debian',
440           },
441         },
442       },
443     }
444
445     facts_hash.each do |os, facts|
446       context "on #{os}" do
447         let(:facts) do
448           facts
449         end
450         let(:params) do
451           {
452             auth_conf_entries: [
453               {
454                 machine: 'deb.example.net',
455                 login: 'foologin',
456                 password: 'secret',
457               },
458               {
459                 machine: 'apt.example.com',
460                 login: 'aptlogin',
461                 password: 'supersecret',
462               },
463             ],
464           }
465         end
466
467         context 'with manage_auth_conf => true' do
468           let(:params) do
469             super().merge(manage_auth_conf: true)
470           end
471
472           # Going forward starting with Ubuntu 16.04 and Debian 9.0
473           # /etc/apt/auth.conf is owned by _apt. In previous versions it is
474           # root.
475           auth_conf_owner = case os
476                             when 'Ubuntu 14.04', 'Debian 7.0', 'Debian 8.0'
477                               'root'
478                             else
479                               '_apt'
480                             end
481
482           auth_conf_content = "// This file is managed by Puppet. DO NOT EDIT.
483 machine deb.example.net login foologin password secret
484 machine apt.example.com login aptlogin password supersecret
485 "
486
487           it {
488             is_expected.to contain_file('/etc/apt/auth.conf').with(ensure: 'present',
489                                                                    owner: auth_conf_owner,
490                                                                    group: 'root',
491                                                                    mode: '0600',
492                                                                    notify: 'Class[Apt::Update]',
493                                                                    content: sensitive(auth_conf_content))
494           }
495         end
496
497         context 'with manage_auth_conf => false' do
498           let(:params) do
499             super().merge(manage_auth_conf: false)
500           end
501
502           it {
503             is_expected.not_to contain_file('/etc/apt/auth.conf')
504           }
505         end
506       end
507
508       context 'with improperly specified entries for /etc/apt/auth.conf' do
509         let(:params) do
510           {
511             auth_conf_entries: [
512               {
513                 machinn: 'deb.example.net',
514                 username: 'foologin',
515                 password: 'secret',
516               },
517               {
518                 machine: 'apt.example.com',
519                 login: 'aptlogin',
520                 password: 'supersecret',
521               },
522             ],
523           }
524         end
525
526         it { is_expected.to raise_error(Puppet::Error) }
527       end
528     end
529   end
530
531   context 'with sources defined on valid os.family' do
532     let :facts do
533       {
534         os: {
535           family: 'Debian',
536           name: 'Ubuntu',
537           release: {
538             major: '16',
539             full: '16.04',
540           },
541           distro: {
542             codename: 'xenial',
543             id: 'Ubuntu',
544           },
545         },
546       }
547     end
548     let(:params) do
549       { sources: {
550         'debian_unstable' => {
551           'location'          => 'http://debian.mirror.iweb.ca/debian/',
552           'release'           => 'unstable',
553           'repos'             => 'main contrib non-free',
554           'key'               => { 'id' => '150C8614919D8446E01E83AF9AA38DCD55BE302B', 'server' => 'subkeys.pgp.net' },
555           'pin'               => '-10',
556           'include'           => { 'src' => true },
557         },
558         'puppetlabs' => {
559           'location' => 'http://apt.puppetlabs.com',
560           'repos'      => 'main',
561           'key'        => { 'id' => '6F6B15509CF8E59E6E469F327F438280EF8D349F', 'server' => 'pgp.mit.edu' },
562         },
563       } }
564     end
565
566     it {
567       is_expected.to contain_apt__setting('list-debian_unstable').with(ensure: 'present')
568     }
569
570     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$}) }
571     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$}) }
572
573     it {
574       is_expected.to contain_apt__setting('list-puppetlabs').with(ensure: 'present')
575     }
576
577     it { is_expected.to contain_file('/etc/apt/sources.list.d/puppetlabs.list').with_content(%r{^deb http://apt.puppetlabs.com xenial main$}) }
578   end
579
580   context 'with confs defined on valid os.family' do
581     let :facts do
582       {
583         os: {
584           family: 'Debian',
585           name: 'Ubuntu',
586           release: {
587             major: '16',
588             full: '16.04',
589           },
590           distro: {
591             codename: 'xenial',
592             id: 'Ubuntu',
593           },
594         },
595       }
596     end
597     let(:params) do
598       { confs: {
599         'foo' => {
600           'content' => 'foo',
601         },
602         'bar' => {
603           'content' => 'bar',
604         },
605       } }
606     end
607
608     it {
609       is_expected.to contain_apt__conf('foo').with(content: 'foo')
610     }
611
612     it {
613       is_expected.to contain_apt__conf('bar').with(content: 'bar')
614     }
615   end
616
617   context 'with keys defined on valid os.family' do
618     let :facts do
619       {
620         os: {
621           family: 'Debian',
622           name: 'Ubuntu',
623           release: {
624             major: '16',
625             full: '16.04',
626           },
627           distro: {
628             codename: 'xenial',
629             id: 'Ubuntu',
630           },
631         },
632       }
633     end
634     let(:params) do
635       { keys: {
636         '55BE302B' => {
637           'server' => 'subkeys.pgp.net',
638         },
639         'EF8D349F' => {
640           'server' => 'pgp.mit.edu',
641         },
642       } }
643     end
644
645     it {
646       is_expected.to contain_apt__key('55BE302B').with(server: 'subkeys.pgp.net')
647     }
648
649     it {
650       is_expected.to contain_apt__key('EF8D349F').with(server: 'pgp.mit.edu')
651     }
652   end
653
654   context 'with ppas defined on valid os.family' do
655     let :facts do
656       {
657         os: {
658           family: 'Debian',
659           name: 'Ubuntu',
660           release: {
661             major: '16',
662             full: '16.04',
663           },
664           distro: {
665             codename: 'xenial',
666             id: 'Ubuntu',
667           },
668         },
669       }
670     end
671     let(:params) do
672       { ppas: {
673         'ppa:drizzle-developers/ppa' => {},
674         'ppa:nginx/stable' => {},
675       } }
676     end
677
678     it { is_expected.to contain_apt__ppa('ppa:drizzle-developers/ppa') }
679     it { is_expected.to contain_apt__ppa('ppa:nginx/stable') }
680   end
681
682   context 'with settings defined on valid os.family' do
683     let :facts do
684       {
685         os: {
686           family: 'Debian',
687           name: 'Ubuntu',
688           release: {
689             major: '16',
690             full: '16.04',
691           },
692           distro: {
693             codename: 'xenial',
694             id: 'Ubuntu',
695           },
696         },
697       }
698     end
699     let(:params) do
700       { settings: {
701         'conf-banana' => { 'content' => 'banana' },
702         'pref-banana' => { 'content' => 'banana' },
703       } }
704     end
705
706     it { is_expected.to contain_apt__setting('conf-banana') }
707     it { is_expected.to contain_apt__setting('pref-banana') }
708   end
709
710   context 'with pins defined on valid os.family' do
711     let :facts do
712       {
713         os: {
714           family: 'Debian',
715           name: 'Ubuntu',
716           release: {
717             major: '16',
718             full: '16.04',
719           },
720           distro: {
721             codename: 'xenial',
722             id: 'Ubuntu',
723           },
724         },
725       }
726     end
727     let(:params) do
728       { pins: {
729         'stable' => { 'priority' => 600, 'order' => 50 },
730         'testing' =>  { 'priority' => 700, 'order' => 100 },
731       } }
732     end
733
734     it { is_expected.to contain_apt__pin('stable') }
735     it { is_expected.to contain_apt__pin('testing') }
736   end
737
738   describe 'failing tests' do
739     context "with purge['sources.list']=>'banana'" do
740       let(:params) { { purge: { 'sources.list' => 'banana' } } }
741
742       it do
743         is_expected.to raise_error(Puppet::Error)
744       end
745     end
746
747     context "with purge['sources.list.d']=>'banana'" do
748       let(:params) { { purge: { 'sources.list.d' => 'banana' } } }
749
750       it do
751         is_expected.to raise_error(Puppet::Error)
752       end
753     end
754
755     context "with purge['preferences']=>'banana'" do
756       let(:params) { { purge: { 'preferences' => 'banana' } } }
757
758       it do
759         is_expected.to raise_error(Puppet::Error)
760       end
761     end
762
763     context "with purge['preferences.d']=>'banana'" do
764       let(:params) { { purge: { 'preferences.d' => 'banana' } } }
765
766       it do
767         is_expected.to raise_error(Puppet::Error)
768       end
769     end
770
771     context "with purge['apt.conf.d']=>'banana'" do
772       let(:params) { { purge: { 'apt.conf.d' => 'banana' } } }
773
774       it do
775         is_expected.to raise_error(Puppet::Error)
776       end
777     end
778   end
779 end