Merge pull request #582 from imphil/master
[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' => '47B320EB4C7C375AA9DAE1A01054B7A24BD6EC30', '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 keys 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) { { :keys => {
189       '55BE302B' => {
190         'server' => 'subkeys.pgp.net',
191       },
192       '4BD6EC30' => {
193         'server' => 'pgp.mit.edu',
194       }
195     } } }
196
197     it { is_expected.to contain_apt__key('55BE302B').with({
198         :server => 'subkeys.pgp.net',
199     })}
200
201     it { is_expected.to contain_apt__key('4BD6EC30').with({
202         :server => 'pgp.mit.edu',
203     })}
204   end
205
206   context 'with ppas defined on valid osfamily' do
207     let :facts do
208       { :osfamily        => 'Debian',
209         :lsbdistcodename => 'precise',
210         :lsbdistid       => 'ubuntu',
211         :lsbdistrelease  => '12.04',
212         :puppetversion   => Puppet.version,
213       }
214     end
215     let(:params) { { :ppas => {
216       'ppa:drizzle-developers/ppa' => {},
217       'ppa:nginx/stable' => {},
218     } } }
219
220     it { is_expected.to contain_apt__ppa('ppa:drizzle-developers/ppa')}
221     it { is_expected.to contain_apt__ppa('ppa:nginx/stable')}
222   end
223
224   context 'with settings defined on valid osfamily' do
225     let :facts do
226       { :osfamily        => 'Debian',
227         :lsbdistcodename => 'precise',
228         :lsbdistid       => 'Debian',
229         :puppetversion   => Puppet.version,
230       }
231     end
232     let(:params) { { :settings => {
233       'conf-banana' => { 'content' => 'banana' },
234       'pref-banana' => { 'content' => 'banana' },
235     } } }
236
237     it { is_expected.to contain_apt__setting('conf-banana')}
238     it { is_expected.to contain_apt__setting('pref-banana')}
239   end
240
241   context 'with pins defined on valid osfamily' do
242     let :facts do
243       { :osfamily        => 'Debian',
244         :lsbdistcodename => 'precise',
245         :lsbdistid       => 'Debian',
246         :puppetversion   => Puppet.version,
247       }
248     end
249     let(:params) { { :pins => {
250       'stable' => { 'priority' => 600, 'order' => 50 },
251       'testing' =>  { 'priority' => 700, 'order' => 100 },
252     } } }
253
254     it { is_expected.to contain_apt__pin('stable') }
255     it { is_expected.to contain_apt__pin('testing') }
256   end
257
258   describe 'failing tests' do
259     context "purge['sources.list']=>'banana'" do
260       let(:params) { { :purge => { 'sources.list' => 'banana' }, } }
261       it do
262         expect {
263           subject.call
264         }.to raise_error(Puppet::Error)
265       end
266     end
267
268     context "purge['sources.list.d']=>'banana'" do
269       let(:params) { { :purge => { 'sources.list.d' => 'banana' }, } }
270       it do
271         expect {
272           subject.call
273         }.to raise_error(Puppet::Error)
274       end
275     end
276
277     context "purge['preferences']=>'banana'" do
278       let(:params) { { :purge => { 'preferences' => 'banana' }, } }
279       it do
280         expect {
281           subject.call
282         }.to raise_error(Puppet::Error)
283       end
284     end
285
286     context "purge['preferences.d']=>'banana'" do
287       let(:params) { { :purge => { 'preferences.d' => 'banana' }, } }
288       it do
289         expect {
290           subject.call
291         }.to raise_error(Puppet::Error)
292       end
293     end
294
295     context 'with unsupported osfamily' do
296       let :facts do
297         { :osfamily => 'Darwin', :puppetversion   => Puppet.version,}
298       end
299
300       it do
301         expect {
302           subject.call
303         }.to raise_error(Puppet::Error, /This module only works on Debian or derivatives like Ubuntu/)
304       end
305     end
306   end
307 end