fb84c51cfbfe504ea21597f60c37e49305f85018
[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 '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 'lays down /etc/apt/apt.conf.d/15update-stamp' do
64       is_expected.to contain_file('/etc/apt/apt.conf.d/15update-stamp').with(group: 'root',
65                                                                              mode: '0644',
66                                                                              owner: 'root').with_content(
67                                                                                %r{APT::Update::Post-Invoke-Success {"touch /var/lib/apt/periodic/update-success-stamp 2>/dev/null || true";};},
68                                                                              )
69     end
70
71     it {
72       is_expected.to contain_exec('apt_update').with(refreshonly: 'true')
73     }
74
75     it { is_expected.not_to contain_apt__setting('conf-proxy') }
76   end
77
78   describe 'proxy=' do
79     context 'host=localhost' do
80       let(:params) { { proxy: { 'host' => 'localhost' } } }
81
82       it {
83         is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
84           %r{Acquire::http::proxy "http://localhost:8080/";},
85         ).without_content(
86           %r{Acquire::https::proxy},
87         )
88       }
89     end
90
91     context 'host=localhost and port=8180' do
92       let(:params) { { proxy: { 'host' => 'localhost', 'port' => 8180 } } }
93
94       it {
95         is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
96           %r{Acquire::http::proxy "http://localhost:8180/";},
97         ).without_content(
98           %r{Acquire::https::proxy},
99         )
100       }
101     end
102
103     context 'host=localhost and https=true' do
104       let(:params) { { proxy: { 'host' => 'localhost', 'https' => true } } }
105
106       it {
107         is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
108           %r{Acquire::http::proxy "http://localhost:8080/";},
109         ).with_content(
110           %r{Acquire::https::proxy "https://localhost:8080/";},
111         )
112       }
113     end
114
115     context 'host=localhost and direct=true' do
116       let(:params) { { proxy: { 'host' => 'localhost', 'direct' => true } } }
117
118       it {
119         is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
120           %r{Acquire::http::proxy "http://localhost:8080/";},
121         ).with_content(
122           %r{Acquire::https::proxy "DIRECT";},
123         )
124       }
125     end
126
127     context 'host=localhost and https=true and direct=true' do
128       let(:params) { { proxy: { 'host' => 'localhost', 'https' => true, 'direct' => true } } }
129
130       it {
131         is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
132           %r{Acquire::http::proxy "http://localhost:8080/";},
133         ).with_content(
134           %r{Acquire::https::proxy "https://localhost:8080/";},
135         )
136       }
137       it {
138         is_expected.to contain_apt__setting('conf-proxy').with(priority: '01').with_content(
139           %r{Acquire::http::proxy "http://localhost:8080/";},
140         ).without_content(
141           %r{Acquire::https::proxy "DIRECT";},
142         )
143       }
144     end
145
146     context 'ensure=absent' do
147       let(:params) { { proxy: { 'ensure' => 'absent' } } }
148
149       it {
150         is_expected.to contain_apt__setting('conf-proxy').with(ensure: 'absent',
151                                                                priority: '01')
152       }
153     end
154   end
155   context 'lots of non-defaults' do
156     let :params do
157       {
158         update: { 'frequency' => 'always', 'timeout' => 1, 'tries' => 3 },
159         purge: { 'sources.list' => false, 'sources.list.d' => false,
160                  'preferences' => false, 'preferences.d' => false },
161       }
162     end
163
164     it {
165       is_expected.to contain_file('sources.list').with(content: nil)
166     }
167
168     it {
169       is_expected.to contain_file('sources.list.d').with(purge: false,
170                                                          recurse: false)
171     }
172
173     it {
174       is_expected.to contain_file('preferences').with(ensure: 'file')
175     }
176
177     it {
178       is_expected.to contain_file('preferences.d').with(purge: false,
179                                                         recurse: false)
180     }
181
182     it {
183       is_expected.to contain_exec('apt_update').with(refreshonly: false,
184                                                      timeout: 1,
185                                                      tries: 3)
186     }
187   end
188
189   context 'with sources defined on valid osfamily' do
190     let :facts do
191       { os: { family: 'Debian', name: 'Ubuntu', release: { major: '12', full: '12.04' } },
192         osfamily: 'Debian',
193         lsbdistcodename: 'precise',
194         lsbdistid: 'Ubuntu',
195         lsbdistrelease: '12.04',
196         puppetversion: Puppet.version }
197     end
198     let(:params) do
199       { sources: {
200         'debian_unstable' => {
201           'location'          => 'http://debian.mirror.iweb.ca/debian/',
202           'release'           => 'unstable',
203           'repos'             => 'main contrib non-free',
204           'key'               => { 'id' => '150C8614919D8446E01E83AF9AA38DCD55BE302B', 'server' => 'subkeys.pgp.net' },
205           'pin'               => '-10',
206           'include'           => { 'src' => true },
207         },
208         'puppetlabs' => {
209           'location' => 'http://apt.puppetlabs.com',
210           'repos'      => 'main',
211           'key'        => { 'id' => '6F6B15509CF8E59E6E469F327F438280EF8D349F', 'server' => 'pgp.mit.edu' },
212         },
213       } }
214     end
215
216     it {
217       is_expected.to contain_apt__setting('list-debian_unstable').with(ensure: 'present')
218     }
219
220     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$}) }
221     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$}) }
222
223     it {
224       is_expected.to contain_apt__setting('list-puppetlabs').with(ensure: 'present')
225     }
226
227     it { is_expected.to contain_file('/etc/apt/sources.list.d/puppetlabs.list').with_content(%r{^deb http://apt.puppetlabs.com precise main$}) }
228   end
229
230   context 'with confs defined on valid osfamily' do
231     let :facts do
232       {
233         os: { family: 'Debian', name: 'Ubuntu', release: { major: '12', full: '12.04.5' } },
234         osfamily: 'Debian',
235         lsbdistcodename: 'precise',
236         lsbdistid: 'Debian',
237         puppetversion: Puppet.version,
238       }
239     end
240     let(:params) do
241       { confs: {
242         'foo' => {
243           'content' => 'foo',
244         },
245         'bar' => {
246           'content' => 'bar',
247         },
248       } }
249     end
250
251     it {
252       is_expected.to contain_apt__conf('foo').with(content: 'foo')
253     }
254
255     it {
256       is_expected.to contain_apt__conf('bar').with(content: 'bar')
257     }
258   end
259
260   context 'with keys defined on valid osfamily' do
261     let :facts do
262       {
263         os: { family: 'Debian', name: 'Ubuntu', release: { major: '12', full: '12.04.5' } },
264         osfamily: 'Debian',
265         lsbdistcodename: 'precise',
266         lsbdistid: 'Debian',
267         puppetversion: Puppet.version,
268       }
269     end
270     let(:params) do
271       { keys: {
272         '55BE302B' => {
273           'server' => 'subkeys.pgp.net',
274         },
275         'EF8D349F' => {
276           'server' => 'pgp.mit.edu',
277         },
278       } }
279     end
280
281     it {
282       is_expected.to contain_apt__key('55BE302B').with(server: 'subkeys.pgp.net')
283     }
284
285     it {
286       is_expected.to contain_apt__key('EF8D349F').with(server: 'pgp.mit.edu')
287     }
288   end
289
290   context 'with ppas defined on valid osfamily' do
291     let :facts do
292       {
293         os: { family: 'Debian', name: 'Ubuntu', release: { major: '12', full: '12.04.5' } },
294         osfamily: 'Debian',
295         lsbdistcodename: 'precise',
296         lsbdistid: 'ubuntu',
297         lsbdistrelease: '12.04',
298         puppetversion: Puppet.version,
299       }
300     end
301     let(:params) do
302       { ppas: {
303         'ppa:drizzle-developers/ppa' => {},
304         'ppa:nginx/stable' => {},
305       } }
306     end
307
308     it { is_expected.to contain_apt__ppa('ppa:drizzle-developers/ppa') }
309     it { is_expected.to contain_apt__ppa('ppa:nginx/stable') }
310   end
311
312   context 'with settings defined on valid osfamily' do
313     let :facts do
314       {
315         os: { family: 'Debian', name: 'Ubuntu', release: { major: '12', full: '12.04.5' } },
316         osfamily: 'Debian',
317         lsbdistcodename: 'precise',
318         lsbdistid: 'Debian',
319         puppetversion: Puppet.version,
320       }
321     end
322     let(:params) do
323       { settings: {
324         'conf-banana' => { 'content' => 'banana' },
325         'pref-banana' => { 'content' => 'banana' },
326       } }
327     end
328
329     it { is_expected.to contain_apt__setting('conf-banana') }
330     it { is_expected.to contain_apt__setting('pref-banana') }
331   end
332
333   context 'with pins defined on valid osfamily' do
334     let :facts do
335       {
336         os: { family: 'Debian', name: 'Ubuntu', release: { major: '12', full: '12.04.5' } },
337         osfamily: 'Debian',
338         lsbdistcodename: 'precise',
339         lsbdistid: 'Debian',
340         puppetversion: Puppet.version,
341       }
342     end
343     let(:params) do
344       { pins: {
345         'stable' => { 'priority' => 600, 'order' => 50 },
346         'testing' =>  { 'priority' => 700, 'order' => 100 },
347       } }
348     end
349
350     it { is_expected.to contain_apt__pin('stable') }
351     it { is_expected.to contain_apt__pin('testing') }
352   end
353
354   describe 'failing tests' do
355     context "purge['sources.list']=>'banana'" do
356       let(:params) { { purge: { 'sources.list' => 'banana' } } }
357
358       it do
359         is_expected.to raise_error(Puppet::Error)
360       end
361     end
362
363     context "purge['sources.list.d']=>'banana'" do
364       let(:params) { { purge: { 'sources.list.d' => 'banana' } } }
365
366       it do
367         is_expected.to raise_error(Puppet::Error)
368       end
369     end
370
371     context "purge['preferences']=>'banana'" do
372       let(:params) { { purge: { 'preferences' => 'banana' } } }
373
374       it do
375         is_expected.to raise_error(Puppet::Error)
376       end
377     end
378
379     context "purge['preferences.d']=>'banana'" do
380       let(:params) { { purge: { 'preferences.d' => 'banana' } } }
381
382       it do
383         is_expected.to raise_error(Puppet::Error)
384       end
385     end
386   end
387 end