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