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