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