Merge pull request #525 from ianmacl/MODULES-2070
[puppet-modules/puppetlabs-apt.git] / spec / defines / source_spec.rb
1 require 'spec_helper'
2
3 describe 'apt::source' do
4   GPG_KEY_ID = '47B320EB4C7C375AA9DAE1A01054B7A24BD6EC30'
5
6   let :pre_condition do
7     'class { "apt": }'
8   end
9
10   let :title do
11     'my_source'
12   end
13
14   context 'defaults' do
15     context 'without location' do
16       let :facts do
17         {
18           :lsbdistid       => 'Debian',
19           :lsbdistcodename => 'wheezy',
20           :osfamily        => 'Debian',
21           :puppetversion   => Puppet.version,
22         }
23       end
24       it do
25         expect {
26           subject.call
27         }.to raise_error(Puppet::Error, /source entry without specifying a location/)
28       end
29     end
30     context 'with location' do
31       let :facts do
32         {
33           :lsbdistid       => 'Debian',
34           :lsbdistcodename => 'wheezy',
35           :osfamily        => 'Debian',
36           :puppetversion   => Puppet.version,
37         }
38       end
39       let(:params) { { :location => 'hello.there', } }
40
41       it { is_expected.to contain_apt__setting('list-my_source').with({
42         :ensure  => 'present',
43       }).without_content(/# my_source\ndeb-src hello.there wheezy main\n/)
44       }
45     end
46   end
47
48   describe 'no defaults' do
49     let :facts do
50       {
51         :lsbdistid       => 'Debian',
52         :lsbdistcodename => 'wheezy',
53         :osfamily        => 'Debian',
54         :puppetversion   => Puppet.version,
55       }
56     end
57
58     context 'with complex pin' do
59       let :params do
60         {
61           :location => 'hello.there',
62           :pin      => { 'release'     => 'wishwash',
63                          'explanation' => 'wishwash',
64                          'priority'    => 1001, },
65         }
66       end
67
68       it { is_expected.to contain_apt__setting('list-my_source').with({
69         :ensure => 'present',
70       }).with_content(/hello.there wheezy main\n/)
71       }
72
73       it { is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with({
74         :ensure       => 'present',
75         :priority     => 1001,
76         :explanation  => 'wishwash',
77         :release      => 'wishwash',
78       })
79       }
80     end
81
82     context 'with simple key' do
83       let :params do
84         {
85           :comment           => 'foo',
86           :location          => 'http://debian.mirror.iweb.ca/debian/',
87           :release           => 'sid',
88           :repos             => 'testing',
89           :key               => GPG_KEY_ID,
90           :pin               => '10',
91           :architecture      => 'x86_64',
92           :allow_unsigned    => true,
93         }
94       end
95
96       it { is_expected.to contain_apt__setting('list-my_source').with({
97         :ensure => 'present',
98       }).with_content(/# foo\ndeb \[arch=x86_64 trusted=yes\] http:\/\/debian\.mirror\.iweb\.ca\/debian\/ sid testing\n/).without_content(/deb-src/)
99       }
100
101       it { is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with({
102         :ensure   => 'present',
103         :priority => '10',
104         :origin   => 'debian.mirror.iweb.ca',
105       })
106       }
107
108       it { is_expected.to contain_apt__key("Add key: #{GPG_KEY_ID} from Apt::Source my_source").that_comes_before('Apt::Setting[list-my_source]').with({
109         :ensure  => 'present',
110         :id      => GPG_KEY_ID,
111       })
112       }
113     end
114
115     context 'with complex key' do
116       let :params do
117         {
118           :comment           => 'foo',
119           :location          => 'http://debian.mirror.iweb.ca/debian/',
120           :release           => 'sid',
121           :repos             => 'testing',
122           :key               => { 'id' => GPG_KEY_ID, 'server' => 'pgp.mit.edu',
123                                   'content' => 'GPG key content',
124                                   'source'  => 'http://apt.puppetlabs.com/pubkey.gpg',},
125           :pin               => '10',
126           :architecture      => 'x86_64',
127           :allow_unsigned    => true,
128         }
129       end
130
131       it { is_expected.to contain_apt__setting('list-my_source').with({
132         :ensure => 'present',
133       }).with_content(/# foo\ndeb \[arch=x86_64 trusted=yes\] http:\/\/debian\.mirror\.iweb\.ca\/debian\/ sid testing\n/).without_content(/deb-src/)
134       }
135
136       it { is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with({
137         :ensure   => 'present',
138         :priority => '10',
139         :origin   => 'debian.mirror.iweb.ca',
140       })
141       }
142
143       it { is_expected.to contain_apt__key("Add key: #{GPG_KEY_ID} from Apt::Source my_source").that_comes_before('Apt::Setting[list-my_source]').with({
144         :ensure  => 'present',
145         :id      => GPG_KEY_ID,
146         :server  => 'pgp.mit.edu',
147         :content => 'GPG key content',
148         :source  => 'http://apt.puppetlabs.com/pubkey.gpg',
149       })
150       }
151     end
152
153     context 'with simple key' do
154       let :params do
155         {
156           :comment        => 'foo',
157           :location       => 'http://debian.mirror.iweb.ca/debian/',
158           :release        => 'sid',
159           :repos          => 'testing',
160           :key            => GPG_KEY_ID,
161           :pin            => '10',
162           :architecture   => 'x86_64',
163           :allow_unsigned => true,
164         }
165       end
166
167       it { is_expected.to contain_apt__setting('list-my_source').with({
168         :ensure => 'present',
169       }).with_content(/# foo\ndeb \[arch=x86_64 trusted=yes\] http:\/\/debian\.mirror\.iweb\.ca\/debian\/ sid testing\n/).without_content(/deb-src/)
170       }
171
172       it { is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with({
173         :ensure   => 'present',
174         :priority => '10',
175         :origin   => 'debian.mirror.iweb.ca',
176       })
177       }
178
179       it { is_expected.to contain_apt__key("Add key: #{GPG_KEY_ID} from Apt::Source my_source").that_comes_before('Apt::Setting[list-my_source]').with({
180         :ensure  => 'present',
181         :id      => GPG_KEY_ID,
182       })
183       }
184     end
185   end
186
187   context 'allow_unsigned true' do
188     let :facts do
189       {
190         :lsbdistid       => 'Debian',
191         :lsbdistcodename => 'wheezy',
192         :osfamily        => 'Debian',
193         :puppetversion   => Puppet.version,
194       }
195     end
196     let :params do
197       {
198         :location       => 'hello.there',
199         :allow_unsigned => true,
200       }
201     end
202
203     it { is_expected.to contain_apt__setting('list-my_source').with({
204       :ensure => 'present',
205     }).with_content(/# my_source\ndeb \[trusted=yes\] hello.there wheezy main\n/)
206     }
207   end
208
209   context 'architecture equals x86_64' do
210     let :facts do
211       {
212         :lsbdistid       => 'Debian',
213         :lsbdistcodename => 'wheezy',
214         :osfamily        => 'Debian',
215         :puppetversion   => Puppet.version,
216       }
217     end
218     let :params do
219       {
220         :location     => 'hello.there',
221         :include      => {'deb' => false, 'src' => true,},
222         :architecture => 'x86_64',
223       }
224     end
225
226     it { is_expected.to contain_apt__setting('list-my_source').with({
227       :ensure => 'present',
228     }).with_content(/# my_source\ndeb-src \[arch=x86_64 \] hello.there wheezy main\n/)
229     }
230   end
231
232   context 'include_src => true' do
233     let :facts do
234       {
235         :lsbdistid       => 'Debian',
236         :lsbdistcodename => 'wheezy',
237         :osfamily        => 'Debian',
238         :puppetversion   => Puppet.version,
239       }
240     end
241     let :params do
242       {
243         :location    => 'hello.there',
244         :include_src => true,
245       }
246     end
247
248     it { is_expected.to contain_apt__setting('list-my_source').with({
249       :ensure => 'present',
250     }).with_content(/# my_source\ndeb hello.there wheezy main\ndeb-src hello.there wheezy main\n/)
251     }
252   end
253
254   context 'include_deb => false' do
255     let :facts do
256       {
257         :lsbdistid       => 'debian',
258         :lsbdistcodename => 'wheezy',
259         :osfamily        => 'debian',
260         :puppetversion   => Puppet.version,
261       }
262     end
263     let :params do
264       {
265         :location    => 'hello.there',
266         :include_deb => false,
267       }
268     end
269
270     it { is_expected.to contain_apt__setting('list-my_source').with({
271       :ensure => 'present',
272     }).without_content(/deb-src hello.there wheezy main\n/)
273     }
274     it { is_expected.to contain_apt__setting('list-my_source').without_content(/deb hello.there wheezy main\n/) }
275   end
276
277   context 'include_src => true and include_deb => false' do
278     let :facts do
279       {
280         :lsbdistid       => 'debian',
281         :lsbdistcodename => 'wheezy',
282         :osfamily        => 'debian',
283         :puppetversion   => Puppet.version,
284       }
285     end
286     let :params do
287       {
288         :location    => 'hello.there',
289         :include_deb => false,
290         :include_src => true,
291       }
292     end
293
294     it { is_expected.to contain_apt__setting('list-my_source').with({
295       :ensure => 'present',
296     }).with_content(/deb-src hello.there wheezy main\n/)
297     }
298     it { is_expected.to contain_apt__setting('list-my_source').without_content(/deb hello.there wheezy main\n/) }
299   end
300
301   context 'include precedence' do
302     let :facts do
303       {
304         :lsbdistid       => 'debian',
305         :lsbdistcodename => 'wheezy',
306         :osfamily        => 'debian',
307         :puppetversion   => Puppet.version,
308       }
309     end
310     let :params do
311       {
312         :location    => 'hello.there',
313         :include_deb => true,
314         :include_src => false,
315         :include     => { 'deb' => false, 'src' => true },
316       }
317     end
318
319     it { is_expected.to contain_apt__setting('list-my_source').with({
320       :ensure => 'present',
321     }).with_content(/deb-src hello.there wheezy main\n/)
322     }
323     it { is_expected.to contain_apt__setting('list-my_source').without_content(/deb hello.there wheezy main\n/) }
324   end
325
326   context 'ensure => absent' do
327     let :facts do
328       {
329         :lsbdistid       => 'Debian',
330         :lsbdistcodename => 'wheezy',
331         :osfamily        => 'Debian',
332         :puppetversion   => Puppet.version,
333       }
334     end
335     let :params do
336       {
337         :ensure => 'absent',
338       }
339     end
340
341     it { is_expected.to contain_apt__setting('list-my_source').with({
342       :ensure => 'absent'
343     })
344     }
345   end
346
347   describe 'validation' do
348     context 'no release' do
349       let :facts do
350         {
351           :lsbdistid       => 'Debian',
352           :osfamily        => 'Debian',
353           :puppetversion   => Puppet.version,
354         }
355       end
356       let(:params) { { :location => 'hello.there', } }
357
358       it do
359         expect {
360           subject.call
361         }.to raise_error(Puppet::Error, /lsbdistcodename fact not available: release parameter required/)
362       end
363     end
364
365     context 'invalid pin' do
366       let :facts do
367         {
368           :lsbdistid       => 'Debian',
369           :lsbdistcodename => 'wheezy',
370           :osfamily        => 'Debian',
371           :puppetversion   => Puppet.version,
372         }
373       end
374       let :params do
375         {
376           :location => 'hello.there',
377           :pin      => true,
378         }
379       end
380
381       it do
382         expect {
383           subject.call
384         }.to raise_error(Puppet::Error, /invalid value for pin/)
385       end
386     end
387
388   end
389 end