69cbb8c1131b04fdd05973788723d24034d34e64
[puppet-modules/puppetlabs-apt.git] / spec / classes / apt_spec.rb
1 require 'spec_helper'
2
3 sources_list = {  ensure: 'file',
4                   path: '/etc/apt/sources.list',
5                   owner: 'root',
6                   group: 'root',
7                   mode: '0644',
8                   notify: 'Class[Apt::Update]' }
9
10 sources_list_d = { ensure: 'directory',
11                    path: '/etc/apt/sources.list.d',
12                    owner: 'root',
13                    group: 'root',
14                    mode: '0644',
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                 mode: '0644',
24                 notify: 'Class[Apt::Update]' }
25
26 preferences_d = { ensure: 'directory',
27                   path: '/etc/apt/preferences.d',
28                   owner: 'root',
29                   group: 'root',
30                   mode: '0644',
31                   purge: false,
32                   recurse: false,
33                   notify: 'Class[Apt::Update]' }
34
35 describe 'apt' do
36   let(:facts) do
37     {
38       os: { family: 'Debian', name: 'Debian', release: { major: '8', full: '8.0' } },
39       lsbdistid: 'Debian',
40       osfamily: 'Debian',
41       lsbdistcodename: 'wheezy',
42       puppetversion: Puppet.version,
43     }
44   end
45
46   context 'with defaults' do
47     it {
48       is_expected.to contain_file('sources.list').that_notifies('Class[Apt::Update]').only_with(sources_list)
49     }
50
51     it {
52       is_expected.to contain_file('sources.list.d').that_notifies('Class[Apt::Update]').only_with(sources_list_d)
53     }
54
55     it {
56       is_expected.to contain_file('preferences').that_notifies('Class[Apt::Update]').only_with(preferences)
57     }
58
59     it {
60       is_expected.to contain_file('preferences.d').that_notifies('Class[Apt::Update]').only_with(preferences_d)
61     }
62
63     it { is_expected.to contain_file('/etc/apt/auth.conf').with_ensure('absent') }
64
65     it 'lays down /etc/apt/apt.conf.d/15update-stamp' do
66       is_expected.to contain_file('/etc/apt/apt.conf.d/15update-stamp').with(group: 'root',
67                                                                              mode: '0644',
68                                                                              owner: 'root').with_content(
69                                                                                %r{APT::Update::Post-Invoke-Success {"touch /var/lib/apt/periodic/update-success-stamp 2>/dev/null || true";};},
70                                                                              )
71     end
72
73     it {
74       is_expected.to contain_exec('apt_update').with(refreshonly: 'true')
75     }
76
77     it { is_expected.not_to contain_apt__setting('conf-proxy') }
78   end
79
80   describe 'proxy=' do
81     context 'when host=localhost' do
82       let(:params) { { proxy: { 'host' => 'localhost' } } }
83
84       it {
85         is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
86           %r{Acquire::http::proxy "http://localhost:8080/";},
87         ).without_content(
88           %r{Acquire::https::proxy},
89         )
90       }
91     end
92
93     context 'when host=localhost and port=8180' do
94       let(:params) { { proxy: { 'host' => 'localhost', 'port' => 8180 } } }
95
96       it {
97         is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
98           %r{Acquire::http::proxy "http://localhost:8180/";},
99         ).without_content(
100           %r{Acquire::https::proxy},
101         )
102       }
103     end
104
105     context 'when host=localhost and https=true' do
106       let(:params) { { proxy: { 'host' => 'localhost', 'https' => true } } }
107
108       it {
109         is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
110           %r{Acquire::http::proxy "http://localhost:8080/";},
111         ).with_content(
112           %r{Acquire::https::proxy "https://localhost:8080/";},
113         )
114       }
115     end
116
117     context 'when host=localhost and direct=true' do
118       let(:params) { { proxy: { 'host' => 'localhost', 'direct' => true } } }
119
120       it {
121         is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
122           %r{Acquire::http::proxy "http://localhost:8080/";},
123         ).with_content(
124           %r{Acquire::https::proxy "DIRECT";},
125         )
126       }
127     end
128
129     context 'when host=localhost and https=true and direct=true' do
130       let(:params) { { proxy: { 'host' => 'localhost', 'https' => true, 'direct' => true } } }
131
132       it {
133         is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
134           %r{Acquire::http::proxy "http://localhost:8080/";},
135         ).with_content(
136           %r{Acquire::https::proxy "https://localhost:8080/";},
137         )
138       }
139       it {
140         is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
141           %r{Acquire::http::proxy "http://localhost:8080/";},
142         ).without_content(
143           %r{Acquire::https::proxy "DIRECT";},
144         )
145       }
146     end
147
148     context 'when ensure=absent' do
149       let(:params) { { proxy: { 'ensure' => 'absent' } } }
150
151       it {
152         is_expected.to contain_apt__setting('conf-proxy').with(ensure: 'absent',
153                                                                priority: '01')
154       }
155     end
156   end
157   context 'with lots of non-defaults' do
158     let :params do
159       {
160         update: { 'frequency' => 'always', 'timeout' => 1, 'tries' => 3 },
161         purge: { 'sources.list' => false, 'sources.list.d' => false,
162                  'preferences' => false, 'preferences.d' => false },
163       }
164     end
165
166     it {
167       is_expected.to contain_file('sources.list').with(content: nil)
168     }
169
170     it {
171       is_expected.to contain_file('sources.list.d').with(purge: false,
172                                                          recurse: false)
173     }
174
175     it {
176       is_expected.to contain_file('preferences').with(ensure: 'file')
177     }
178
179     it {
180       is_expected.to contain_file('preferences.d').with(purge: false,
181                                                         recurse: false)
182     }
183
184     it {
185       is_expected.to contain_exec('apt_update').with(refreshonly: false,
186                                                      timeout: 1,
187                                                      tries: 3)
188     }
189   end
190
191   context 'with entries for /etc/apt/auth.conf' do
192     let(:params) do
193       {
194         auth_conf_entries: [
195           { machine: 'deb.example.net',
196             login: 'foologin',
197             password: 'secret' },
198           { machine: 'apt.example.com',
199             login: 'aptlogin',
200             password: 'supersecret' },
201         ],
202       }
203     end
204
205     context 'with manage_auth_conf => true' do
206       let(:params) do
207         super().merge(manage_auth_conf: true)
208       end
209
210       auth_conf_content = "// This file is managed by Puppet. DO NOT EDIT.
211 machine deb.example.net login foologin password secret
212 machine apt.example.com login aptlogin password supersecret
213 "
214
215       it {
216         is_expected.to contain_file('/etc/apt/auth.conf').with(ensure: 'present',
217                                                                owner: 'root',
218                                                                group: 'root',
219                                                                mode: '0600',
220                                                                notify: 'Class[Apt::Update]',
221                                                                content: auth_conf_content)
222       }
223     end
224
225     context 'with manage_auth_conf => false' do
226       let(:params) do
227         super().merge(manage_auth_conf: false)
228       end
229
230       it {
231         is_expected.not_to contain_file('/etc/apt/auth.conf')
232       }
233     end
234   end
235
236   context 'with improperly specified entries for /etc/apt/auth.conf' do
237     let(:params) do
238       {
239         auth_conf_entries: [
240           { machinn: 'deb.example.net',
241             username: 'foologin',
242             password: 'secret' },
243           { machine: 'apt.example.com',
244             login: 'aptlogin',
245             password: 'supersecret' },
246         ],
247       }
248     end
249
250     it { is_expected.to raise_error(Puppet::Error) }
251   end
252
253   context 'with sources defined on valid osfamily' do
254     let :facts do
255       { os: { family: 'Debian', name: 'Ubuntu', release: { major: '16', full: '16.04' } },
256         osfamily: 'Debian',
257         lsbdistcodename: 'xenial',
258         lsbdistid: 'Ubuntu',
259         lsbdistrelease: '16.04',
260         puppetversion: Puppet.version }
261     end
262     let(:params) do
263       { sources: {
264         'debian_unstable' => {
265           'location'          => 'http://debian.mirror.iweb.ca/debian/',
266           'release'           => 'unstable',
267           'repos'             => 'main contrib non-free',
268           'key'               => { 'id' => '150C8614919D8446E01E83AF9AA38DCD55BE302B', 'server' => 'subkeys.pgp.net' },
269           'pin'               => '-10',
270           'include'           => { 'src' => true },
271         },
272         'puppetlabs' => {
273           'location' => 'http://apt.puppetlabs.com',
274           'repos'      => 'main',
275           'key'        => { 'id' => '6F6B15509CF8E59E6E469F327F438280EF8D349F', 'server' => 'pgp.mit.edu' },
276         },
277       } }
278     end
279
280     it {
281       is_expected.to contain_apt__setting('list-debian_unstable').with(ensure: 'present')
282     }
283
284     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$}) }
285     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$}) }
286
287     it {
288       is_expected.to contain_apt__setting('list-puppetlabs').with(ensure: 'present')
289     }
290
291     it { is_expected.to contain_file('/etc/apt/sources.list.d/puppetlabs.list').with_content(%r{^deb http://apt.puppetlabs.com xenial main$}) }
292   end
293
294   context 'with confs defined on valid osfamily' do
295     let :facts do
296       {
297         os: { family: 'Debian', name: 'Ubuntu', release: { major: '16', full: '16.04' } },
298         osfamily: 'Debian',
299         lsbdistcodename: 'xenial',
300         lsbdistid: 'Debian',
301         puppetversion: Puppet.version,
302       }
303     end
304     let(:params) do
305       { confs: {
306         'foo' => {
307           'content' => 'foo',
308         },
309         'bar' => {
310           'content' => 'bar',
311         },
312       } }
313     end
314
315     it {
316       is_expected.to contain_apt__conf('foo').with(content: 'foo')
317     }
318
319     it {
320       is_expected.to contain_apt__conf('bar').with(content: 'bar')
321     }
322   end
323
324   context 'with keys defined on valid osfamily' do
325     let :facts do
326       {
327         os: { family: 'Debian', name: 'Ubuntu', release: { major: '16', full: '16.04' } },
328         osfamily: 'Debian',
329         lsbdistcodename: 'xenial',
330         lsbdistid: 'Debian',
331         puppetversion: Puppet.version,
332       }
333     end
334     let(:params) do
335       { keys: {
336         '55BE302B' => {
337           'server' => 'subkeys.pgp.net',
338         },
339         'EF8D349F' => {
340           'server' => 'pgp.mit.edu',
341         },
342       } }
343     end
344
345     it {
346       is_expected.to contain_apt__key('55BE302B').with(server: 'subkeys.pgp.net')
347     }
348
349     it {
350       is_expected.to contain_apt__key('EF8D349F').with(server: 'pgp.mit.edu')
351     }
352   end
353
354   context 'with ppas defined on valid osfamily' do
355     let :facts do
356       {
357         os: { family: 'Debian', name: 'Ubuntu', release: { major: '16', full: '16.04' } },
358         osfamily: 'Debian',
359         lsbdistcodename: 'xenial',
360         lsbdistid: 'ubuntu',
361         lsbdistrelease: '16.04',
362         puppetversion: Puppet.version,
363       }
364     end
365     let(:params) do
366       { ppas: {
367         'ppa:drizzle-developers/ppa' => {},
368         'ppa:nginx/stable' => {},
369       } }
370     end
371
372     it { is_expected.to contain_apt__ppa('ppa:drizzle-developers/ppa') }
373     it { is_expected.to contain_apt__ppa('ppa:nginx/stable') }
374   end
375
376   context 'with settings defined on valid osfamily' do
377     let :facts do
378       {
379         os: { family: 'Debian', name: 'Ubuntu', release: { major: '16', full: '16.04' } },
380         osfamily: 'Debian',
381         lsbdistcodename: 'xenial',
382         lsbdistid: 'Debian',
383         puppetversion: Puppet.version,
384       }
385     end
386     let(:params) do
387       { settings: {
388         'conf-banana' => { 'content' => 'banana' },
389         'pref-banana' => { 'content' => 'banana' },
390       } }
391     end
392
393     it { is_expected.to contain_apt__setting('conf-banana') }
394     it { is_expected.to contain_apt__setting('pref-banana') }
395   end
396
397   context 'with pins defined on valid osfamily' do
398     let :facts do
399       {
400         os: { family: 'Debian', name: 'Ubuntu', release: { major: '16', full: '16.04' } },
401         osfamily: 'Debian',
402         lsbdistcodename: 'xenial',
403         lsbdistid: 'Debian',
404         puppetversion: Puppet.version,
405       }
406     end
407     let(:params) do
408       { pins: {
409         'stable' => { 'priority' => 600, 'order' => 50 },
410         'testing' =>  { 'priority' => 700, 'order' => 100 },
411       } }
412     end
413
414     it { is_expected.to contain_apt__pin('stable') }
415     it { is_expected.to contain_apt__pin('testing') }
416   end
417
418   describe 'failing tests' do
419     context "with purge['sources.list']=>'banana'" do
420       let(:params) { { purge: { 'sources.list' => 'banana' } } }
421
422       it do
423         is_expected.to raise_error(Puppet::Error)
424       end
425     end
426
427     context "with purge['sources.list.d']=>'banana'" do
428       let(:params) { { purge: { 'sources.list.d' => 'banana' } } }
429
430       it do
431         is_expected.to raise_error(Puppet::Error)
432       end
433     end
434
435     context "with purge['preferences']=>'banana'" do
436       let(:params) { { purge: { 'preferences' => 'banana' } } }
437
438       it do
439         is_expected.to raise_error(Puppet::Error)
440       end
441     end
442
443     context "with purge['preferences.d']=>'banana'" do
444       let(:params) { { purge: { 'preferences.d' => 'banana' } } }
445
446       it do
447         is_expected.to raise_error(Puppet::Error)
448       end
449     end
450   end
451 end