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