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 is_expected.to contain_file('sources.list').that_notifies('Class[Apt::Update]').only_with(sources_list)
65 is_expected.to contain_file('sources.list.d').that_notifies('Class[Apt::Update]').only_with(sources_list_d)
69 is_expected.to contain_file('preferences').that_notifies('Class[Apt::Update]').only_with(preferences)
73 is_expected.to contain_file('preferences.d').that_notifies('Class[Apt::Update]').only_with(preferences_d)
77 is_expected.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 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";};},
90 is_expected.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 is_expected.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 is_expected.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 is_expected.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 is_expected.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 is_expected.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 is_expected.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 is_expected.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 is_expected.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 is_expected.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 is_expected.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 is_expected.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/";},
218 is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
219 %r{Acquire::http::proxy "http://localhost:8080/";},
221 %r{Acquire::https::proxy "DIRECT";},
226 context 'when ensure=absent' do
227 let(:params) { { proxy: { 'ensure' => 'absent' } } }
230 is_expected.to contain_apt__setting('conf-proxy').with(ensure: 'absent',
235 context 'with lots of non-defaults' do
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 },
246 is_expected.to contain_file('sources.list').with(content: nil)
250 is_expected.to contain_file('sources.list.d').with(purge: false,
255 is_expected.to contain_file('preferences').with(ensure: 'file')
259 is_expected.to contain_file('preferences.d').with(purge: false,
264 is_expected.to contain_file('apt.conf.d').with(purge: false,
269 is_expected.to contain_exec('apt_update').with(refreshonly: false,
275 context 'with lots of non-defaults' do
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 },
286 is_expected.to contain_file('sources.list').with(content: "# Repos managed by puppet.\n")
290 is_expected.to contain_file('sources.list.d').with(purge: true,
295 is_expected.to contain_file('preferences').with(ensure: 'absent')
299 is_expected.to contain_file('preferences.d').with(purge: true,
304 is_expected.to contain_file('apt.conf.d').with(purge: true,
309 is_expected.to contain_exec('apt_update').with(refreshonly: false,
315 context 'with defaults for sources_list_force' do
318 update: { 'frequency' => 'always', 'timeout' => 1, 'tries' => 3 },
319 purge: { 'sources.list' => true },
320 sources_list_force: false,
325 is_expected.to contain_file('sources.list').with(content: "# Repos managed by puppet.\n")
329 context 'with non defaults for sources_list_force' do
332 update: { 'frequency' => 'always', 'timeout' => 1, 'tries' => 3 },
333 purge: { 'sources.list' => true },
334 sources_list_force: true,
339 is_expected.to contain_file('sources.list').with(ensure: 'absent')
343 context 'with entries for /etc/apt/auth.conf' do
389 facts_hash.each do |os, facts|
390 context "on #{os}" do
398 machine: 'deb.example.net',
403 machine: 'apt.example.com',
405 password: 'supersecret',
411 context 'with manage_auth_conf => true' do
413 super().merge(manage_auth_conf: true)
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
422 is_expected.to contain_file('/etc/apt/auth.conf').with(ensure: 'present',
426 notify: 'Class[Apt::Update]',
427 content: sensitive(auth_conf_content))
431 context 'with manage_auth_conf => false' do
433 super().merge(manage_auth_conf: false)
437 is_expected.not_to contain_file('/etc/apt/auth.conf')
442 context 'with improperly specified entries for /etc/apt/auth.conf' do
447 machinn: 'deb.example.net',
448 username: 'foologin',
452 machine: 'apt.example.com',
454 password: 'supersecret',
460 it { is_expected.to raise_error(Puppet::Error) }
465 context 'with sources defined on valid os.family' do
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' },
490 'include' => { 'src' => true },
493 'location' => 'http://apt.puppetlabs.com',
495 'key' => { 'id' => '6F6B15509CF8E59E6E469F327F438280EF8D349F', 'server' => 'pgp.mit.edu' },
501 is_expected.to contain_apt__setting('list-debian_unstable').with(ensure: 'present')
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$}) }
508 is_expected.to contain_apt__setting('list-puppetlabs').with(ensure: 'present')
511 it { is_expected.to contain_file('/etc/apt/sources.list.d/puppetlabs.list').with_content(%r{^deb http://apt.puppetlabs.com bionic main$}) }
514 context 'with confs defined on valid os.family' do
543 is_expected.to contain_apt__conf('foo').with(content: 'foo')
547 is_expected.to contain_apt__conf('bar').with(content: 'bar')
551 context 'with keys defined on valid os.family' do
571 'server' => 'subkeys.pgp.net',
574 'server' => 'pgp.mit.edu',
580 is_expected.to contain_apt__key('55BE302B').with(server: 'subkeys.pgp.net')
584 is_expected.to contain_apt__key('EF8D349F').with(server: 'pgp.mit.edu')
588 context 'with ppas defined on valid os.family' do
607 'ppa:drizzle-developers/ppa' => {},
608 'ppa:nginx/stable' => {},
612 it { is_expected.to contain_apt__ppa('ppa:drizzle-developers/ppa') }
613 it { is_expected.to contain_apt__ppa('ppa:nginx/stable') }
616 context 'with settings defined on valid os.family' do
635 'conf-banana' => { 'content' => 'banana' },
636 'pref-banana' => { 'content' => 'banana' },
640 it { is_expected.to contain_apt__setting('conf-banana') }
641 it { is_expected.to contain_apt__setting('pref-banana') }
644 context 'with pins defined on valid os.family' do
663 'stable' => { 'priority' => 600, 'order' => 50 },
664 'testing' => { 'priority' => 700, 'order' => 100 },
668 it { is_expected.to contain_apt__pin('stable') }
669 it { is_expected.to contain_apt__pin('testing') }
672 describe 'failing tests' do
673 context "with purge['sources.list']=>'banana'" do
674 let(:params) { { purge: { 'sources.list' => 'banana' } } }
677 is_expected.to raise_error(Puppet::Error)
681 context "with purge['sources.list.d']=>'banana'" do
682 let(:params) { { purge: { 'sources.list.d' => 'banana' } } }
685 is_expected.to raise_error(Puppet::Error)
689 context "with purge['preferences']=>'banana'" do
690 let(:params) { { purge: { 'preferences' => 'banana' } } }
693 is_expected.to raise_error(Puppet::Error)
697 context "with purge['preferences.d']=>'banana'" do
698 let(:params) { { purge: { 'preferences.d' => 'banana' } } }
701 is_expected.to raise_error(Puppet::Error)
705 context "with purge['apt.conf.d']=>'banana'" do
706 let(:params) { { purge: { 'apt.conf.d' => 'banana' } } }
709 is_expected.to raise_error(Puppet::Error)