1 # frozen_string_literal: true
5 sources_list = { ensure: 'file',
6 path: '/etc/apt/sources.list',
9 notify: 'Class[Apt::Update]' }
11 sources_list_d = { ensure: 'directory',
12 path: '/etc/apt/sources.list.d',
17 notify: 'Class[Apt::Update]' }
19 preferences = { ensure: 'file',
20 path: '/etc/apt/preferences',
23 notify: 'Class[Apt::Update]' }
25 preferences_d = { ensure: 'directory',
26 path: '/etc/apt/preferences.d',
31 notify: 'Class[Apt::Update]' }
33 apt_conf_d = { ensure: 'directory',
34 path: '/etc/apt/apt.conf.d',
39 notify: 'Class[Apt::Update]' }
44 os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
47 lsbdistcodename: 'jessie',
51 context 'with defaults' do
53 is_expected.to contain_file('sources.list').that_notifies('Class[Apt::Update]').only_with(sources_list)
57 is_expected.to contain_file('sources.list.d').that_notifies('Class[Apt::Update]').only_with(sources_list_d)
61 is_expected.to contain_file('preferences').that_notifies('Class[Apt::Update]').only_with(preferences)
65 is_expected.to contain_file('preferences.d').that_notifies('Class[Apt::Update]').only_with(preferences_d)
69 is_expected.to contain_file('apt.conf.d').that_notifies('Class[Apt::Update]').only_with(apt_conf_d)
72 it { is_expected.to contain_file('/etc/apt/auth.conf').with_ensure('absent') }
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";};},
82 is_expected.to contain_exec('apt_update').with(refreshonly: 'true')
85 it { is_expected.not_to contain_apt__setting('conf-proxy') }
89 context 'when host=localhost' do
90 let(:params) { { proxy: { 'host' => 'localhost' } } }
93 is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
94 %r{Acquire::http::proxy "http://localhost:8080/";},
96 %r{Acquire::https::proxy},
101 context 'when host=localhost and port=8180' do
102 let(:params) { { proxy: { 'host' => 'localhost', 'port' => 8180 } } }
105 is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
106 %r{Acquire::http::proxy "http://localhost:8180/";},
108 %r{Acquire::https::proxy},
113 context 'when host=localhost and https=true' do
114 let(:params) { { proxy: { 'host' => 'localhost', 'https' => true } } }
117 is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
118 %r{Acquire::http::proxy "http://localhost:8080/";},
120 %r{Acquire::https::proxy "https://localhost:8080/";},
125 context 'when host=localhost and direct=true' do
126 let(:params) { { proxy: { 'host' => 'localhost', 'direct' => true } } }
129 is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
130 %r{Acquire::http::proxy "http://localhost:8080/";},
132 %r{Acquire::https::proxy "DIRECT";},
137 context 'when host=localhost and https=true and direct=true' do
138 let(:params) { { proxy: { 'host' => 'localhost', 'https' => true, 'direct' => true } } }
141 is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
142 %r{Acquire::http::proxy "http://localhost:8080/";},
144 %r{Acquire::https::proxy "https://localhost:8080/";},
148 is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
149 %r{Acquire::http::proxy "http://localhost:8080/";},
151 %r{Acquire::https::proxy "DIRECT";},
156 context 'when ensure=absent' do
157 let(:params) { { proxy: { 'ensure' => 'absent' } } }
160 is_expected.to contain_apt__setting('conf-proxy').with(ensure: 'absent',
165 context 'with lots of non-defaults' do
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 },
176 is_expected.to contain_file('sources.list').with(content: nil)
180 is_expected.to contain_file('sources.list.d').with(purge: false,
185 is_expected.to contain_file('preferences').with(ensure: 'file')
189 is_expected.to contain_file('preferences.d').with(purge: false,
194 is_expected.to contain_file('apt.conf.d').with(purge: false,
199 is_expected.to contain_exec('apt_update').with(refreshonly: false,
205 context 'with lots of non-defaults' do
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 },
216 is_expected.to contain_file('sources.list').with(content: "# Repos managed by puppet.\n")
220 is_expected.to contain_file('sources.list.d').with(purge: true,
225 is_expected.to contain_file('preferences').with(ensure: 'absent')
229 is_expected.to contain_file('preferences.d').with(purge: true,
234 is_expected.to contain_file('apt.conf.d').with(purge: true,
239 is_expected.to contain_exec('apt_update').with(refreshonly: false,
245 context 'with defaults for sources_list_force' do
248 update: { 'frequency' => 'always', 'timeout' => 1, 'tries' => 3 },
249 purge: { 'sources.list' => true },
250 sources_list_force: false,
255 is_expected.to contain_file('sources.list').with(content: "# Repos managed by puppet.\n")
259 context 'with non defaults for sources_list_force' do
262 update: { 'frequency' => 'always', 'timeout' => 1, 'tries' => 3 },
263 purge: { 'sources.list' => true },
264 sources_list_force: true,
269 is_expected.to contain_file('sources.list').with(ensure: 'absent')
273 context 'with entries for /etc/apt/auth.conf' do
276 os: { family: 'Debian', name: 'Ubuntu', release: { major: '14', full: '14.04' } },
278 lsbdistcodename: 'trusty',
280 lsbdistrelease: '14.04',
283 os: { family: 'Debian', name: 'Ubuntu', release: { major: '16', full: '16.04' } },
285 lsbdistcodename: 'xenial',
287 lsbdistrelease: '16.04',
290 os: { family: 'Debian', name: 'Ubuntu', release: { major: '18', full: '18.04' } },
292 lsbdistcodename: 'bionic',
294 lsbdistrelease: '18.04',
297 os: { family: 'Debian', name: 'Debian', release: { major: '7', full: '7.0' } },
300 lsbdistcodename: 'wheezy',
303 os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
306 lsbdistcodename: 'jessie',
309 os: { family: 'Debian', name: 'Debian', release: { major: '9', full: '9.0' } },
312 lsbdistcodename: 'stretch',
315 os: { family: 'Debian', name: 'Debian', release: { major: '10', full: '10.0' } },
318 lsbdistcodename: 'buster',
322 facts_hash.each do |os, facts|
323 context "on #{os}" do
331 machine: 'deb.example.net',
336 machine: 'apt.example.com',
338 password: 'supersecret',
344 context 'with manage_auth_conf => true' do
346 super().merge(manage_auth_conf: true)
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
352 auth_conf_owner = case os
353 when 'Ubuntu 14.04', 'Debian 7.0', 'Debian 8.0'
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
365 is_expected.to contain_file('/etc/apt/auth.conf').with(ensure: 'present',
366 owner: auth_conf_owner,
369 notify: 'Class[Apt::Update]',
370 content: sensitive(auth_conf_content))
374 context 'with manage_auth_conf => false' do
376 super().merge(manage_auth_conf: false)
380 is_expected.not_to contain_file('/etc/apt/auth.conf')
385 context 'with improperly specified entries for /etc/apt/auth.conf' do
390 machinn: 'deb.example.net',
391 username: 'foologin',
395 machine: 'apt.example.com',
397 password: 'supersecret',
403 it { is_expected.to raise_error(Puppet::Error) }
408 context 'with sources defined on valid osfamily' do
411 os: { family: 'Debian', name: 'Ubuntu', release: { major: '16', full: '16.04' } },
413 lsbdistcodename: 'xenial',
415 lsbdistrelease: '16.04',
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' },
426 'include' => { 'src' => true },
429 'location' => 'http://apt.puppetlabs.com',
431 'key' => { 'id' => '6F6B15509CF8E59E6E469F327F438280EF8D349F', 'server' => 'pgp.mit.edu' },
437 is_expected.to contain_apt__setting('list-debian_unstable').with(ensure: 'present')
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$}) }
444 is_expected.to contain_apt__setting('list-puppetlabs').with(ensure: 'present')
447 it { is_expected.to contain_file('/etc/apt/sources.list.d/puppetlabs.list').with_content(%r{^deb http://apt.puppetlabs.com xenial main$}) }
450 context 'with confs defined on valid osfamily' do
453 os: { family: 'Debian', name: 'Ubuntu', release: { major: '16', full: '16.04' } },
455 lsbdistcodename: 'xenial',
471 is_expected.to contain_apt__conf('foo').with(content: 'foo')
475 is_expected.to contain_apt__conf('bar').with(content: 'bar')
479 context 'with keys defined on valid osfamily' do
482 os: { family: 'Debian', name: 'Ubuntu', release: { major: '16', full: '16.04' } },
484 lsbdistcodename: 'xenial',
491 'server' => 'subkeys.pgp.net',
494 'server' => 'pgp.mit.edu',
500 is_expected.to contain_apt__key('55BE302B').with(server: 'subkeys.pgp.net')
504 is_expected.to contain_apt__key('EF8D349F').with(server: 'pgp.mit.edu')
508 context 'with ppas defined on valid osfamily' do
511 os: { family: 'Debian', name: 'Ubuntu', release: { major: '16', full: '16.04' } },
513 lsbdistcodename: 'xenial',
515 lsbdistrelease: '16.04',
520 'ppa:drizzle-developers/ppa' => {},
521 'ppa:nginx/stable' => {},
525 it { is_expected.to contain_apt__ppa('ppa:drizzle-developers/ppa') }
526 it { is_expected.to contain_apt__ppa('ppa:nginx/stable') }
529 context 'with settings defined on valid osfamily' do
532 os: { family: 'Debian', name: 'Ubuntu', release: { major: '16', full: '16.04' } },
534 lsbdistcodename: 'xenial',
540 'conf-banana' => { 'content' => 'banana' },
541 'pref-banana' => { 'content' => 'banana' },
545 it { is_expected.to contain_apt__setting('conf-banana') }
546 it { is_expected.to contain_apt__setting('pref-banana') }
549 context 'with pins defined on valid osfamily' do
552 os: { family: 'Debian', name: 'Ubuntu', release: { major: '16', full: '16.04' } },
554 lsbdistcodename: 'xenial',
560 'stable' => { 'priority' => 600, 'order' => 50 },
561 'testing' => { 'priority' => 700, 'order' => 100 },
565 it { is_expected.to contain_apt__pin('stable') }
566 it { is_expected.to contain_apt__pin('testing') }
569 describe 'failing tests' do
570 context "with purge['sources.list']=>'banana'" do
571 let(:params) { { purge: { 'sources.list' => 'banana' } } }
574 is_expected.to raise_error(Puppet::Error)
578 context "with purge['sources.list.d']=>'banana'" do
579 let(:params) { { purge: { 'sources.list.d' => 'banana' } } }
582 is_expected.to raise_error(Puppet::Error)
586 context "with purge['preferences']=>'banana'" do
587 let(:params) { { purge: { 'preferences' => 'banana' } } }
590 is_expected.to raise_error(Puppet::Error)
594 context "with purge['preferences.d']=>'banana'" do
595 let(:params) { { purge: { 'preferences.d' => 'banana' } } }
598 is_expected.to raise_error(Puppet::Error)
602 context "with purge['apt.conf.d']=>'banana'" do
603 let(:params) { { purge: { 'apt.conf.d' => 'banana' } } }
606 is_expected.to raise_error(Puppet::Error)