Merge pull request #564 from rfdrake/apt_pin
[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         :puppetversion   => Puppet.version,
212       }
213     end
214     let(:params) { { :ppas => {
215       'ppa:drizzle-developers/ppa' => {},
216       'ppa:nginx/stable' => {},
217     } } }
218
219     it { is_expected.to contain_apt__ppa('ppa:drizzle-developers/ppa')}
220     it { is_expected.to contain_apt__ppa('ppa:nginx/stable')}
221   end
222
223   context 'with settings defined on valid osfamily' do
224     let :facts do
225       { :osfamily        => 'Debian',
226         :lsbdistcodename => 'precise',
227         :lsbdistid       => 'Debian',
228         :puppetversion   => Puppet.version,
229       }
230     end
231     let(:params) { { :settings => {
232       'conf-banana' => { 'content' => 'banana' },
233       'pref-banana' => { 'content' => 'banana' },
234     } } }
235
236     it { is_expected.to contain_apt__setting('conf-banana')}
237     it { is_expected.to contain_apt__setting('pref-banana')}
238   end
239
240   context 'with pins defined on valid osfamily' do
241     let :facts do
242       { :osfamily        => 'Debian',
243         :lsbdistcodename => 'precise',
244         :lsbdistid       => 'Debian',
245         :puppetversion   => Puppet.version,
246       }
247     end
248     let(:params) { { :pins => {
249       'stable' => { 'priority' => 600, 'order' => 50 },
250       'testing' =>  { 'priority' => 700, 'order' => 100 },
251     } } }
252
253     it { is_expected.to contain_apt__pin('stable') }
254     it { is_expected.to contain_apt__pin('testing') }
255   end
256
257   describe 'failing tests' do
258     context "purge['sources.list']=>'banana'" do
259       let(:params) { { :purge => { 'sources.list' => 'banana' }, } }
260       it do
261         expect {
262           subject.call
263         }.to raise_error(Puppet::Error)
264       end
265     end
266
267     context "purge['sources.list.d']=>'banana'" do
268       let(:params) { { :purge => { 'sources.list.d' => 'banana' }, } }
269       it do
270         expect {
271           subject.call
272         }.to raise_error(Puppet::Error)
273       end
274     end
275
276     context "purge['preferences']=>'banana'" do
277       let(:params) { { :purge => { 'preferences' => 'banana' }, } }
278       it do
279         expect {
280           subject.call
281         }.to raise_error(Puppet::Error)
282       end
283     end
284
285     context "purge['preferences.d']=>'banana'" do
286       let(:params) { { :purge => { 'preferences.d' => 'banana' }, } }
287       it do
288         expect {
289           subject.call
290         }.to raise_error(Puppet::Error)
291       end
292     end
293
294     context 'with unsupported osfamily' do
295       let :facts do
296         { :osfamily => 'Darwin', :puppetversion   => Puppet.version,}
297       end
298
299       it do
300         expect {
301           subject.call
302         }.to raise_error(Puppet::Error, /This module only works on Debian or derivatives like Ubuntu/)
303       end
304     end
305   end
306 end