Merge pull request #712 from puppetlabs/release
[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(/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           /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           /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           /Acquire::http::proxy "http:\/\/localhost:8080\/";/,
98         ).with_content(
99           /Acquire::https::proxy "https:\/\/localhost:8080\/";/,
100         )
101       }
102     end
103
104     context 'ensure=absent' do
105       let(:params) { { proxy: { 'ensure' => 'absent' } } }
106
107       it {
108         is_expected.to contain_apt__setting('conf-proxy').with(ensure: 'absent',
109                                                                priority: '01')
110       }
111     end
112   end
113   context 'lots of non-defaults' do
114     let :params do
115       {
116         update: { 'frequency' => 'always', 'timeout' => 1, 'tries' => 3 },
117         purge: { 'sources.list' => false, 'sources.list.d' => false,
118                  'preferences' => false, 'preferences.d' => false },
119       }
120     end
121
122     it {
123       is_expected.to contain_file('sources.list').with(content: nil)
124     }
125
126     it {
127       is_expected.to contain_file('sources.list.d').with(purge: false,
128                                                          recurse: false)
129     }
130
131     it {
132       is_expected.to contain_file('preferences').with(ensure: 'file')
133     }
134
135     it {
136       is_expected.to contain_file('preferences.d').with(purge: false,
137                                                         recurse: false)
138     }
139
140     it {
141       is_expected.to contain_exec('apt_update').with(refreshonly: false,
142                                                      timeout: 1,
143                                                      tries: 3)
144     }
145   end
146
147   context 'with sources defined on valid osfamily' do
148     let :facts do
149       { os: { family: 'Debian', name: 'Ubuntu', release: { major: '12', full: '12.04' } },
150         osfamily: 'Debian',
151         lsbdistcodename: 'precise',
152         lsbdistid: 'Ubuntu',
153         lsbdistrelease: '12.04',
154         puppetversion: Puppet.version }
155     end
156     let(:params) do
157       { sources: {
158         'debian_unstable' => {
159           'location'          => 'http://debian.mirror.iweb.ca/debian/',
160           'release'           => 'unstable',
161           'repos'             => 'main contrib non-free',
162           'key'               => { 'id' => '150C8614919D8446E01E83AF9AA38DCD55BE302B', 'server' => 'subkeys.pgp.net' },
163           'pin'               => '-10',
164           'include'           => { 'src' => true },
165         },
166         'puppetlabs' => {
167           'location' => 'http://apt.puppetlabs.com',
168           'repos'      => 'main',
169           'key'        => { 'id' => '6F6B15509CF8E59E6E469F327F438280EF8D349F', 'server' => 'pgp.mit.edu' },
170         },
171       } }
172     end
173
174     it {
175       is_expected.to contain_apt__setting('list-debian_unstable').with(ensure: 'present')
176     }
177
178     it { is_expected.to contain_file('/etc/apt/sources.list.d/debian_unstable.list').with_content(/^deb http:\/\/debian.mirror.iweb.ca\/debian\/ unstable main contrib non-free$/) }
179     it { is_expected.to contain_file('/etc/apt/sources.list.d/debian_unstable.list').with_content(/^deb-src http:\/\/debian.mirror.iweb.ca\/debian\/ unstable main contrib non-free$/) }
180
181     it {
182       is_expected.to contain_apt__setting('list-puppetlabs').with(ensure: 'present')
183     }
184
185     it { is_expected.to contain_file('/etc/apt/sources.list.d/puppetlabs.list').with_content(/^deb http:\/\/apt.puppetlabs.com precise main$/) }
186   end
187
188   context 'with confs defined on valid osfamily' do
189     let :facts do
190       {
191         os: { family: 'Debian', name: 'Ubuntu', release: { major: '12', full: '12.04.5' } },
192         osfamily: 'Debian',
193         lsbdistcodename: 'precise',
194         lsbdistid: 'Debian',
195         puppetversion: Puppet.version,
196       }
197     end
198     let(:params) do
199       { confs: {
200         'foo' => {
201           'content' => 'foo',
202         },
203         'bar' => {
204           'content' => 'bar',
205         },
206       } }
207     end
208
209     it {
210       is_expected.to contain_apt__conf('foo').with(content: 'foo')
211     }
212
213     it {
214       is_expected.to contain_apt__conf('bar').with(content: 'bar')
215     }
216   end
217
218   context 'with keys defined on valid osfamily' do
219     let :facts do
220       {
221         os: { family: 'Debian', name: 'Ubuntu', release: { major: '12', full: '12.04.5' } },
222         osfamily: 'Debian',
223         lsbdistcodename: 'precise',
224         lsbdistid: 'Debian',
225         puppetversion: Puppet.version,
226       }
227     end
228     let(:params) do
229       { keys: {
230         '55BE302B' => {
231           'server' => 'subkeys.pgp.net',
232         },
233         'EF8D349F' => {
234           'server' => 'pgp.mit.edu',
235         },
236       } }
237     end
238
239     it {
240       is_expected.to contain_apt__key('55BE302B').with(server: 'subkeys.pgp.net')
241     }
242
243     it {
244       is_expected.to contain_apt__key('EF8D349F').with(server: 'pgp.mit.edu')
245     }
246   end
247
248   context 'with ppas defined on valid osfamily' do
249     let :facts do
250       {
251         os: { family: 'Debian', name: 'Ubuntu', release: { major: '12', full: '12.04.5' } },
252         osfamily: 'Debian',
253         lsbdistcodename: 'precise',
254         lsbdistid: 'ubuntu',
255         lsbdistrelease: '12.04',
256         puppetversion: Puppet.version,
257       }
258     end
259     let(:params) do
260       { ppas: {
261         'ppa:drizzle-developers/ppa' => {},
262         'ppa:nginx/stable' => {},
263       } }
264     end
265
266     it { is_expected.to contain_apt__ppa('ppa:drizzle-developers/ppa') }
267     it { is_expected.to contain_apt__ppa('ppa:nginx/stable') }
268   end
269
270   context 'with settings defined on valid osfamily' do
271     let :facts do
272       {
273         os: { family: 'Debian', name: 'Ubuntu', release: { major: '12', full: '12.04.5' } },
274         osfamily: 'Debian',
275         lsbdistcodename: 'precise',
276         lsbdistid: 'Debian',
277         puppetversion: Puppet.version,
278       }
279     end
280     let(:params) do
281       { settings: {
282         'conf-banana' => { 'content' => 'banana' },
283         'pref-banana' => { 'content' => 'banana' },
284       } }
285     end
286
287     it { is_expected.to contain_apt__setting('conf-banana') }
288     it { is_expected.to contain_apt__setting('pref-banana') }
289   end
290
291   context 'with pins defined on valid osfamily' do
292     let :facts do
293       {
294         os: { family: 'Debian', name: 'Ubuntu', release: { major: '12', full: '12.04.5' } },
295         osfamily: 'Debian',
296         lsbdistcodename: 'precise',
297         lsbdistid: 'Debian',
298         puppetversion: Puppet.version,
299       }
300     end
301     let(:params) do
302       { pins: {
303         'stable' => { 'priority' => 600, 'order' => 50 },
304         'testing' =>  { 'priority' => 700, 'order' => 100 },
305       } }
306     end
307
308     it { is_expected.to contain_apt__pin('stable') }
309     it { is_expected.to contain_apt__pin('testing') }
310   end
311
312   describe 'failing tests' do
313     context "purge['sources.list']=>'banana'" do
314       let(:params) { { purge: { 'sources.list' => 'banana' } } }
315
316       it do
317         expect {
318           subject.call
319         }.to raise_error(Puppet::Error)
320       end
321     end
322
323     context "purge['sources.list.d']=>'banana'" do
324       let(:params) { { purge: { 'sources.list.d' => 'banana' } } }
325
326       it do
327         expect {
328           subject.call
329         }.to raise_error(Puppet::Error)
330       end
331     end
332
333     context "purge['preferences']=>'banana'" do
334       let(:params) { { purge: { 'preferences' => 'banana' } } }
335
336       it do
337         expect {
338           subject.call
339         }.to raise_error(Puppet::Error)
340       end
341     end
342
343     context "purge['preferences.d']=>'banana'" do
344       let(:params) { { purge: { 'preferences.d' => 'banana' } } }
345
346       it do
347         expect {
348           subject.call
349         }.to raise_error(Puppet::Error)
350       end
351     end
352   end
353 end