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