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
445 facts_hash.each do |os, facts|
446 context "on #{os}" do
454 machine: 'deb.example.net',
459 machine: 'apt.example.com',
461 password: 'supersecret',
467 context 'with manage_auth_conf => true' do
469 super().merge(manage_auth_conf: true)
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
475 auth_conf_owner = case os
476 when 'Ubuntu 14.04', 'Debian 7.0', 'Debian 8.0'
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
488 is_expected.to contain_file('/etc/apt/auth.conf').with(ensure: 'present',
489 owner: auth_conf_owner,
492 notify: 'Class[Apt::Update]',
493 content: sensitive(auth_conf_content))
497 context 'with manage_auth_conf => false' do
499 super().merge(manage_auth_conf: false)
503 is_expected.not_to contain_file('/etc/apt/auth.conf')
508 context 'with improperly specified entries for /etc/apt/auth.conf' do
513 machinn: 'deb.example.net',
514 username: 'foologin',
518 machine: 'apt.example.com',
520 password: 'supersecret',
526 it { is_expected.to raise_error(Puppet::Error) }
531 context 'with sources defined on valid os.family' do
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' },
556 'include' => { 'src' => true },
559 'location' => 'http://apt.puppetlabs.com',
561 'key' => { 'id' => '6F6B15509CF8E59E6E469F327F438280EF8D349F', 'server' => 'pgp.mit.edu' },
567 is_expected.to contain_apt__setting('list-debian_unstable').with(ensure: 'present')
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$}) }
574 is_expected.to contain_apt__setting('list-puppetlabs').with(ensure: 'present')
577 it { is_expected.to contain_file('/etc/apt/sources.list.d/puppetlabs.list').with_content(%r{^deb http://apt.puppetlabs.com xenial main$}) }
580 context 'with confs defined on valid os.family' do
609 is_expected.to contain_apt__conf('foo').with(content: 'foo')
613 is_expected.to contain_apt__conf('bar').with(content: 'bar')
617 context 'with keys defined on valid os.family' do
637 'server' => 'subkeys.pgp.net',
640 'server' => 'pgp.mit.edu',
646 is_expected.to contain_apt__key('55BE302B').with(server: 'subkeys.pgp.net')
650 is_expected.to contain_apt__key('EF8D349F').with(server: 'pgp.mit.edu')
654 context 'with ppas defined on valid os.family' do
673 'ppa:drizzle-developers/ppa' => {},
674 'ppa:nginx/stable' => {},
678 it { is_expected.to contain_apt__ppa('ppa:drizzle-developers/ppa') }
679 it { is_expected.to contain_apt__ppa('ppa:nginx/stable') }
682 context 'with settings defined on valid os.family' do
701 'conf-banana' => { 'content' => 'banana' },
702 'pref-banana' => { 'content' => 'banana' },
706 it { is_expected.to contain_apt__setting('conf-banana') }
707 it { is_expected.to contain_apt__setting('pref-banana') }
710 context 'with pins defined on valid os.family' do
729 'stable' => { 'priority' => 600, 'order' => 50 },
730 'testing' => { 'priority' => 700, 'order' => 100 },
734 it { is_expected.to contain_apt__pin('stable') }
735 it { is_expected.to contain_apt__pin('testing') }
738 describe 'failing tests' do
739 context "with purge['sources.list']=>'banana'" do
740 let(:params) { { purge: { 'sources.list' => 'banana' } } }
743 is_expected.to raise_error(Puppet::Error)
747 context "with purge['sources.list.d']=>'banana'" do
748 let(:params) { { purge: { 'sources.list.d' => 'banana' } } }
751 is_expected.to raise_error(Puppet::Error)
755 context "with purge['preferences']=>'banana'" do
756 let(:params) { { purge: { 'preferences' => 'banana' } } }
759 is_expected.to raise_error(Puppet::Error)
763 context "with purge['preferences.d']=>'banana'" do
764 let(:params) { { purge: { 'preferences.d' => 'banana' } } }
767 is_expected.to raise_error(Puppet::Error)
771 context "with purge['apt.conf.d']=>'banana'" do
772 let(:params) { { purge: { 'apt.conf.d' => 'banana' } } }
775 is_expected.to raise_error(Puppet::Error)