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]' }
59 context 'with defaults' do
61 expect(subject).to contain_file('sources.list').that_notifies('Class[Apt::Update]').only_with(sources_list)
65 expect(subject).to contain_file('sources.list.d').that_notifies('Class[Apt::Update]').only_with(sources_list_d)
69 expect(subject).to contain_file('preferences').that_notifies('Class[Apt::Update]').only_with(preferences)
73 expect(subject).to contain_file('preferences.d').that_notifies('Class[Apt::Update]').only_with(preferences_d)
77 expect(subject).to contain_file('apt.conf.d').that_notifies('Class[Apt::Update]').only_with(apt_conf_d)
80 it { is_expected.to contain_file('/etc/apt/auth.conf').with_ensure('absent') }
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";};},
90 expect(subject).to contain_exec('apt_update').with(refreshonly: 'true')
93 it { is_expected.not_to contain_apt__setting('conf-proxy') }
97 context 'when host=localhost' do
98 let(:params) { { proxy: { 'host' => 'localhost' } } }
101 expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
102 %r{Acquire::http::proxy "http://localhost:8080/";},
104 %r{Acquire::https::proxy },
109 context 'when host=localhost and per-host[proxyscope]=proxyhost' do
110 let(:params) { { proxy: { 'host' => 'localhost', 'perhost' => [{ 'scope' => 'proxyscope', 'host' => 'proxyhost' }] } } }
113 expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
114 %r{Acquire::http::proxy::proxyscope "http://proxyhost:8080/";},
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 }] } } }
123 expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
124 %r{Acquire::http::proxy::proxyscope "http://proxyhost:8081/";},
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 }] } } }
133 expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
134 %r{Acquire::https::proxy::proxyscope "https://proxyhost:8080/";},
139 context 'when host=localhost and per-host[proxyscope]=[direct]' do
140 let(:params) { { proxy: { 'host' => 'localhost', 'perhost' => [{ 'scope' => 'proxyscope', 'direct' => true }] } } }
143 expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
144 %r{Acquire::http::proxy::proxyscope "DIRECT";},
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 }] } } }
153 expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
154 %r{Acquire::https::proxy::proxyscope "DIRECT";},
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' }] } } }
163 expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
164 %r{Acquire::http::proxy::proxyscope "http://proxyhost:8080/";},
166 %r{Acquire::http::proxy::proxyscope2 "http://proxyhost2:8080/";},
171 context 'when host=localhost and port=8180' do
172 let(:params) { { proxy: { 'host' => 'localhost', 'port' => 8180 } } }
175 expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
176 %r{Acquire::http::proxy "http://localhost:8180/";},
178 %r{Acquire::https::proxy },
183 context 'when host=localhost and https=true' do
184 let(:params) { { proxy: { 'host' => 'localhost', 'https' => true } } }
187 expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
188 %r{Acquire::http::proxy "http://localhost:8080/";},
190 %r{Acquire::https::proxy "https://localhost:8080/";},
195 context 'when host=localhost and direct=true' do
196 let(:params) { { proxy: { 'host' => 'localhost', 'direct' => true } } }
199 expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
200 %r{Acquire::http::proxy "http://localhost:8080/";},
202 %r{Acquire::https::proxy "DIRECT";},
207 context 'when host=localhost and https=true and direct=true' do
208 let(:params) { { proxy: { 'host' => 'localhost', 'https' => true, 'direct' => true } } }
211 expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
212 %r{Acquire::http::proxy "http://localhost:8080/";},
214 %r{Acquire::https::proxy "https://localhost:8080/";},
219 expect(subject).to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
220 %r{Acquire::http::proxy "http://localhost:8080/";},
222 %r{Acquire::https::proxy "DIRECT";},
227 context 'when ensure=absent' do
228 let(:params) { { proxy: { 'ensure' => 'absent' } } }
231 expect(subject).to contain_apt__setting('conf-proxy').with(ensure: 'absent',
237 context 'with lots of non-defaults' do
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 },
248 expect(subject).to contain_file('sources.list').with(content: nil)
252 expect(subject).to contain_file('sources.list.d').with(purge: false,
257 expect(subject).to contain_file('preferences').with(ensure: 'file')
261 expect(subject).to contain_file('preferences.d').with(purge: false,
266 expect(subject).to contain_file('apt.conf.d').with(purge: false,
271 expect(subject).to contain_exec('apt_update').with(refreshonly: false,
277 context 'with lots of non-defaults' do
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 },
288 expect(subject).to contain_file('sources.list').with(content: "# Repos managed by puppet.\n")
292 expect(subject).to contain_file('sources.list.d').with(purge: true,
297 expect(subject).to contain_file('preferences').with(ensure: 'absent')
301 expect(subject).to contain_file('preferences.d').with(purge: true,
306 expect(subject).to contain_file('apt.conf.d').with(purge: true,
311 expect(subject).to contain_exec('apt_update').with(refreshonly: false,
317 context 'with defaults for sources_list_force' do
320 update: { 'frequency' => 'always', 'timeout' => 1, 'tries' => 3 },
321 purge: { 'sources.list' => true },
322 sources_list_force: false,
327 expect(subject).to contain_file('sources.list').with(content: "# Repos managed by puppet.\n")
331 context 'with non defaults for sources_list_force' do
334 update: { 'frequency' => 'always', 'timeout' => 1, 'tries' => 3 },
335 purge: { 'sources.list' => true },
336 sources_list_force: true,
341 expect(subject).to contain_file('sources.list').with(ensure: 'absent')
345 context 'with entries for /etc/apt/auth.conf' do
391 facts_hash.each do |os, facts|
392 context "on #{os}" do
400 machine: 'deb.example.net',
405 machine: 'apt.example.com',
407 password: 'supersecret',
413 context 'with manage_auth_conf => true' do
415 super().merge(manage_auth_conf: true)
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
424 expect(subject).to contain_file('/etc/apt/auth.conf').with(ensure: 'present',
428 notify: 'Class[Apt::Update]',
429 content: sensitive(auth_conf_content))
433 context 'with manage_auth_conf => false' do
435 super().merge(manage_auth_conf: false)
439 expect(subject).not_to contain_file('/etc/apt/auth.conf')
444 context 'with improperly specified entries for /etc/apt/auth.conf' do
449 machinn: 'deb.example.net',
450 username: 'foologin',
454 machine: 'apt.example.com',
456 password: 'supersecret',
462 it { is_expected.to raise_error(Puppet::Error) }
467 context 'with sources defined on valid os.family' do
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' },
492 'include' => { 'src' => true },
495 'location' => 'http://apt.puppetlabs.com',
497 'key' => { 'id' => '6F6B15509CF8E59E6E469F327F438280EF8D349F', 'server' => 'pgp.mit.edu' },
503 expect(subject).to contain_apt__setting('list-debian_unstable').with(ensure: 'present')
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$}) }
510 expect(subject).to contain_apt__setting('list-puppetlabs').with(ensure: 'present')
513 it { is_expected.to contain_file('/etc/apt/sources.list.d/puppetlabs.list').with_content(%r{^deb http://apt.puppetlabs.com bionic main$}) }
516 context 'with confs defined on valid os.family' do
545 expect(subject).to contain_apt__conf('foo').with(content: 'foo')
549 expect(subject).to contain_apt__conf('bar').with(content: 'bar')
553 context 'with keys defined on valid os.family' do
573 'server' => 'subkeys.pgp.net',
576 'server' => 'pgp.mit.edu',
582 expect(subject).to contain_apt__key('55BE302B').with(server: 'subkeys.pgp.net')
586 expect(subject).to contain_apt__key('EF8D349F').with(server: 'pgp.mit.edu')
590 context 'with ppas defined on valid os.family' do
609 'ppa:drizzle-developers/ppa' => {},
610 'ppa:nginx/stable' => {},
614 it { is_expected.to contain_apt__ppa('ppa:drizzle-developers/ppa') }
615 it { is_expected.to contain_apt__ppa('ppa:nginx/stable') }
618 context 'with settings defined on valid os.family' do
637 'conf-banana' => { 'content' => 'banana' },
638 'pref-banana' => { 'content' => 'banana' },
642 it { is_expected.to contain_apt__setting('conf-banana') }
643 it { is_expected.to contain_apt__setting('pref-banana') }
646 context 'with pins defined on valid os.family' do
665 'stable' => { 'priority' => 600, 'order' => 50 },
666 'testing' => { 'priority' => 700, 'order' => 100 },
670 it { is_expected.to contain_apt__pin('stable') }
671 it { is_expected.to contain_apt__pin('testing') }
674 describe 'failing tests' do
675 context "with purge['sources.list']=>'banana'" do
676 let(:params) { { purge: { 'sources.list' => 'banana' } } }
679 expect(subject).to raise_error(Puppet::Error)
683 context "with purge['sources.list.d']=>'banana'" do
684 let(:params) { { purge: { 'sources.list.d' => 'banana' } } }
687 expect(subject).to raise_error(Puppet::Error)
691 context "with purge['preferences']=>'banana'" do
692 let(:params) { { purge: { 'preferences' => 'banana' } } }
695 expect(subject).to raise_error(Puppet::Error)
699 context "with purge['preferences.d']=>'banana'" do
700 let(:params) { { purge: { 'preferences.d' => 'banana' } } }
703 expect(subject).to raise_error(Puppet::Error)
707 context "with purge['apt.conf.d']=>'banana'" do
708 let(:params) { { purge: { 'apt.conf.d' => 'banana' } } }
711 expect(subject).to raise_error(Puppet::Error)