Merge pull request #754 from paladox/patch-1
[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: '7', full: '7.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     auth_conf_content = "// This file is managed by Puppet. DO NOT EDIT.
206 machine deb.example.net login foologin password secret
207 machine apt.example.com login aptlogin password supersecret
208 "
209
210     it {
211       is_expected.to contain_file('/etc/apt/auth.conf').with(ensure: 'present',
212                                                              owner: 'root',
213                                                              group: 'root',
214                                                              mode: '0600',
215                                                              notify: 'Class[Apt::Update]',
216                                                              content: auth_conf_content)
217     }
218   end
219
220   context 'with improperly specified entries for /etc/apt/auth.conf' do
221     let(:params) do
222       {
223         auth_conf_entries: [
224           { machinn: 'deb.example.net',
225             username: 'foologin',
226             password: 'secret' },
227           { machine: 'apt.example.com',
228             login: 'aptlogin',
229             password: 'supersecret' },
230         ],
231       }
232     end
233
234     it { is_expected.to raise_error(Puppet::Error) }
235   end
236
237   context 'with sources defined on valid osfamily' do
238     let :facts do
239       { os: { family: 'Debian', name: 'Ubuntu', release: { major: '16', full: '16.04' } },
240         osfamily: 'Debian',
241         lsbdistcodename: 'xenial',
242         lsbdistid: 'Ubuntu',
243         lsbdistrelease: '16.04',
244         puppetversion: Puppet.version }
245     end
246     let(:params) do
247       { sources: {
248         'debian_unstable' => {
249           'location'          => 'http://debian.mirror.iweb.ca/debian/',
250           'release'           => 'unstable',
251           'repos'             => 'main contrib non-free',
252           'key'               => { 'id' => '150C8614919D8446E01E83AF9AA38DCD55BE302B', 'server' => 'subkeys.pgp.net' },
253           'pin'               => '-10',
254           'include'           => { 'src' => true },
255         },
256         'puppetlabs' => {
257           'location' => 'http://apt.puppetlabs.com',
258           'repos'      => 'main',
259           'key'        => { 'id' => '6F6B15509CF8E59E6E469F327F438280EF8D349F', 'server' => 'pgp.mit.edu' },
260         },
261       } }
262     end
263
264     it {
265       is_expected.to contain_apt__setting('list-debian_unstable').with(ensure: 'present')
266     }
267
268     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$}) }
269     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$}) }
270
271     it {
272       is_expected.to contain_apt__setting('list-puppetlabs').with(ensure: 'present')
273     }
274
275     it { is_expected.to contain_file('/etc/apt/sources.list.d/puppetlabs.list').with_content(%r{^deb http://apt.puppetlabs.com xenial main$}) }
276   end
277
278   context 'with confs defined on valid osfamily' do
279     let :facts do
280       {
281         os: { family: 'Debian', name: 'Ubuntu', release: { major: '16', full: '16.04' } },
282         osfamily: 'Debian',
283         lsbdistcodename: 'xenial',
284         lsbdistid: 'Debian',
285         puppetversion: Puppet.version,
286       }
287     end
288     let(:params) do
289       { confs: {
290         'foo' => {
291           'content' => 'foo',
292         },
293         'bar' => {
294           'content' => 'bar',
295         },
296       } }
297     end
298
299     it {
300       is_expected.to contain_apt__conf('foo').with(content: 'foo')
301     }
302
303     it {
304       is_expected.to contain_apt__conf('bar').with(content: 'bar')
305     }
306   end
307
308   context 'with keys defined on valid osfamily' do
309     let :facts do
310       {
311         os: { family: 'Debian', name: 'Ubuntu', release: { major: '16', full: '16.04' } },
312         osfamily: 'Debian',
313         lsbdistcodename: 'xenial',
314         lsbdistid: 'Debian',
315         puppetversion: Puppet.version,
316       }
317     end
318     let(:params) do
319       { keys: {
320         '55BE302B' => {
321           'server' => 'subkeys.pgp.net',
322         },
323         'EF8D349F' => {
324           'server' => 'pgp.mit.edu',
325         },
326       } }
327     end
328
329     it {
330       is_expected.to contain_apt__key('55BE302B').with(server: 'subkeys.pgp.net')
331     }
332
333     it {
334       is_expected.to contain_apt__key('EF8D349F').with(server: 'pgp.mit.edu')
335     }
336   end
337
338   context 'with ppas defined on valid osfamily' do
339     let :facts do
340       {
341         os: { family: 'Debian', name: 'Ubuntu', release: { major: '16', full: '16.04' } },
342         osfamily: 'Debian',
343         lsbdistcodename: 'xenial',
344         lsbdistid: 'ubuntu',
345         lsbdistrelease: '16.04',
346         puppetversion: Puppet.version,
347       }
348     end
349     let(:params) do
350       { ppas: {
351         'ppa:drizzle-developers/ppa' => {},
352         'ppa:nginx/stable' => {},
353       } }
354     end
355
356     it { is_expected.to contain_apt__ppa('ppa:drizzle-developers/ppa') }
357     it { is_expected.to contain_apt__ppa('ppa:nginx/stable') }
358   end
359
360   context 'with settings defined on valid osfamily' do
361     let :facts do
362       {
363         os: { family: 'Debian', name: 'Ubuntu', release: { major: '16', full: '16.04' } },
364         osfamily: 'Debian',
365         lsbdistcodename: 'xenial',
366         lsbdistid: 'Debian',
367         puppetversion: Puppet.version,
368       }
369     end
370     let(:params) do
371       { settings: {
372         'conf-banana' => { 'content' => 'banana' },
373         'pref-banana' => { 'content' => 'banana' },
374       } }
375     end
376
377     it { is_expected.to contain_apt__setting('conf-banana') }
378     it { is_expected.to contain_apt__setting('pref-banana') }
379   end
380
381   context 'with pins defined on valid osfamily' do
382     let :facts do
383       {
384         os: { family: 'Debian', name: 'Ubuntu', release: { major: '16', full: '16.04' } },
385         osfamily: 'Debian',
386         lsbdistcodename: 'xenial',
387         lsbdistid: 'Debian',
388         puppetversion: Puppet.version,
389       }
390     end
391     let(:params) do
392       { pins: {
393         'stable' => { 'priority' => 600, 'order' => 50 },
394         'testing' =>  { 'priority' => 700, 'order' => 100 },
395       } }
396     end
397
398     it { is_expected.to contain_apt__pin('stable') }
399     it { is_expected.to contain_apt__pin('testing') }
400   end
401
402   describe 'failing tests' do
403     context "with purge['sources.list']=>'banana'" do
404       let(:params) { { purge: { 'sources.list' => 'banana' } } }
405
406       it do
407         is_expected.to raise_error(Puppet::Error)
408       end
409     end
410
411     context "with purge['sources.list.d']=>'banana'" do
412       let(:params) { { purge: { 'sources.list.d' => 'banana' } } }
413
414       it do
415         is_expected.to raise_error(Puppet::Error)
416       end
417     end
418
419     context "with purge['preferences']=>'banana'" do
420       let(:params) { { purge: { 'preferences' => 'banana' } } }
421
422       it do
423         is_expected.to raise_error(Puppet::Error)
424       end
425     end
426
427     context "with purge['preferences.d']=>'banana'" do
428       let(:params) { { purge: { 'preferences.d' => 'banana' } } }
429
430       it do
431         is_expected.to raise_error(Puppet::Error)
432       end
433     end
434   end
435 end