Merge pull request #627 from puppetlabs/release
[puppet-modules/puppetlabs-apt.git] / spec / defines / source_spec.rb
1 require 'spec_helper'
2
3 describe 'apt::source' do
4   GPG_KEY_ID = '6F6B15509CF8E59E6E469F327F438280EF8D349F'
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_file('/etc/apt/sources.list.d/my_source.list').that_notifies('Class[Apt::Update]')}
74
75       it { is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with({
76         :ensure       => 'present',
77         :priority     => 1001,
78         :explanation  => 'wishwash',
79         :release      => 'wishwash',
80       })
81       }
82     end
83
84     context 'with simple key' do
85       let :params do
86         {
87           :comment           => 'foo',
88           :location          => 'http://debian.mirror.iweb.ca/debian/',
89           :release           => 'sid',
90           :repos             => 'testing',
91           :key               => GPG_KEY_ID,
92           :pin               => '10',
93           :architecture      => 'x86_64',
94           :allow_unsigned    => true,
95         }
96       end
97
98       it { is_expected.to contain_apt__setting('list-my_source').with({
99         :ensure => 'present',
100       }).with_content(/# foo\ndeb \[arch=x86_64 trusted=yes\] http:\/\/debian\.mirror\.iweb\.ca\/debian\/ sid testing\n/).without_content(/deb-src/)
101       }
102
103       it { is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with({
104         :ensure   => 'present',
105         :priority => '10',
106         :origin   => 'debian.mirror.iweb.ca',
107       })
108       }
109
110       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({
111         :ensure  => 'present',
112         :id      => GPG_KEY_ID,
113       })
114       }
115     end
116
117     context 'with complex key' do
118       let :params do
119         {
120           :comment           => 'foo',
121           :location          => 'http://debian.mirror.iweb.ca/debian/',
122           :release           => 'sid',
123           :repos             => 'testing',
124           :key               => { 'id' => GPG_KEY_ID, 'server' => 'pgp.mit.edu',
125                                   'content' => 'GPG key content',
126                                   'source'  => 'http://apt.puppetlabs.com/pubkey.gpg',},
127           :pin               => '10',
128           :architecture      => 'x86_64',
129           :allow_unsigned    => true,
130         }
131       end
132
133       it { is_expected.to contain_apt__setting('list-my_source').with({
134         :ensure => 'present',
135       }).with_content(/# foo\ndeb \[arch=x86_64 trusted=yes\] http:\/\/debian\.mirror\.iweb\.ca\/debian\/ sid testing\n/).without_content(/deb-src/)
136       }
137
138       it { is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with({
139         :ensure   => 'present',
140         :priority => '10',
141         :origin   => 'debian.mirror.iweb.ca',
142       })
143       }
144
145       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({
146         :ensure  => 'present',
147         :id      => GPG_KEY_ID,
148         :server  => 'pgp.mit.edu',
149         :content => 'GPG key content',
150         :source  => 'http://apt.puppetlabs.com/pubkey.gpg',
151       })
152       }
153     end
154
155     context 'with simple key' do
156       let :params do
157         {
158           :comment        => 'foo',
159           :location       => 'http://debian.mirror.iweb.ca/debian/',
160           :release        => 'sid',
161           :repos          => 'testing',
162           :key            => GPG_KEY_ID,
163           :pin            => '10',
164           :architecture   => 'x86_64',
165           :allow_unsigned => true,
166         }
167       end
168
169       it { is_expected.to contain_apt__setting('list-my_source').with({
170         :ensure => 'present',
171       }).with_content(/# foo\ndeb \[arch=x86_64 trusted=yes\] http:\/\/debian\.mirror\.iweb\.ca\/debian\/ sid testing\n/).without_content(/deb-src/)
172       }
173
174       it { is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with({
175         :ensure   => 'present',
176         :priority => '10',
177         :origin   => 'debian.mirror.iweb.ca',
178       })
179       }
180
181       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({
182         :ensure  => 'present',
183         :id      => GPG_KEY_ID,
184       })
185       }
186     end
187   end
188
189   context 'allow_unsigned true' do
190     let :facts do
191       {
192         :lsbdistid       => 'Debian',
193         :lsbdistcodename => 'wheezy',
194         :osfamily        => 'Debian',
195         :puppetversion   => Puppet.version,
196       }
197     end
198     let :params do
199       {
200         :location       => 'hello.there',
201         :allow_unsigned => true,
202       }
203     end
204
205     it { is_expected.to contain_apt__setting('list-my_source').with({
206       :ensure => 'present',
207     }).with_content(/# my_source\ndeb \[trusted=yes\] hello.there wheezy main\n/)
208     }
209   end
210
211   context 'architecture equals x86_64' do
212     let :facts do
213       {
214         :lsbdistid       => 'Debian',
215         :lsbdistcodename => 'wheezy',
216         :osfamily        => 'Debian',
217         :puppetversion   => Puppet.version,
218       }
219     end
220     let :params do
221       {
222         :location     => 'hello.there',
223         :include      => {'deb' => false, 'src' => true,},
224         :architecture => 'x86_64',
225       }
226     end
227
228     it { is_expected.to contain_apt__setting('list-my_source').with({
229       :ensure => 'present',
230     }).with_content(/# my_source\ndeb-src \[arch=x86_64\] hello.there wheezy main\n/)
231     }
232   end
233
234   context 'include_src => true' do
235     let :facts do
236       {
237         :lsbdistid       => 'Debian',
238         :lsbdistcodename => 'wheezy',
239         :osfamily        => 'Debian',
240         :puppetversion   => Puppet.version,
241       }
242     end
243     let :params do
244       {
245         :location    => 'hello.there',
246         :include_src => true,
247       }
248     end
249
250     it { is_expected.to contain_apt__setting('list-my_source').with({
251       :ensure => 'present',
252     }).with_content(/# my_source\ndeb hello.there wheezy main\ndeb-src hello.there wheezy main\n/)
253     }
254   end
255
256   context 'include_deb => false' do
257     let :facts do
258       {
259         :lsbdistid       => 'debian',
260         :lsbdistcodename => 'wheezy',
261         :osfamily        => 'debian',
262         :puppetversion   => Puppet.version,
263       }
264     end
265     let :params do
266       {
267         :location    => 'hello.there',
268         :include_deb => false,
269       }
270     end
271
272     it { is_expected.to contain_apt__setting('list-my_source').with({
273       :ensure => 'present',
274     }).without_content(/deb-src hello.there wheezy main\n/)
275     }
276     it { is_expected.to contain_apt__setting('list-my_source').without_content(/deb hello.there wheezy main\n/) }
277   end
278
279   context 'include_src => true and include_deb => false' do
280     let :facts do
281       {
282         :lsbdistid       => 'debian',
283         :lsbdistcodename => 'wheezy',
284         :osfamily        => 'debian',
285         :puppetversion   => Puppet.version,
286       }
287     end
288     let :params do
289       {
290         :location    => 'hello.there',
291         :include_deb => false,
292         :include_src => true,
293       }
294     end
295
296     it { is_expected.to contain_apt__setting('list-my_source').with({
297       :ensure => 'present',
298     }).with_content(/deb-src hello.there wheezy main\n/)
299     }
300     it { is_expected.to contain_apt__setting('list-my_source').without_content(/deb hello.there wheezy main\n/) }
301   end
302
303   context 'include precedence' do
304     let :facts do
305       {
306         :lsbdistid       => 'debian',
307         :lsbdistcodename => 'wheezy',
308         :osfamily        => 'debian',
309         :puppetversion   => Puppet.version,
310       }
311     end
312     let :params do
313       {
314         :location    => 'hello.there',
315         :include_deb => true,
316         :include_src => false,
317         :include     => { 'deb' => false, 'src' => true },
318       }
319     end
320
321     it { is_expected.to contain_apt__setting('list-my_source').with({
322       :ensure => 'present',
323     }).with_content(/deb-src hello.there wheezy main\n/)
324     }
325     it { is_expected.to contain_apt__setting('list-my_source').without_content(/deb hello.there wheezy main\n/) }
326   end
327
328   context 'ensure => absent' do
329     let :facts do
330       {
331         :lsbdistid       => 'Debian',
332         :lsbdistcodename => 'wheezy',
333         :osfamily        => 'Debian',
334         :puppetversion   => Puppet.version,
335       }
336     end
337     let :params do
338       {
339         :ensure => 'absent',
340       }
341     end
342
343     it { is_expected.to contain_apt__setting('list-my_source').with({
344       :ensure => 'absent'
345     })
346     }
347   end
348
349   describe 'validation' do
350     context 'no release' do
351       let :facts do
352         {
353           :lsbdistid       => 'Debian',
354           :osfamily        => 'Debian',
355           :puppetversion   => Puppet.version,
356         }
357       end
358       let(:params) { { :location => 'hello.there', } }
359
360       it do
361         expect {
362           subject.call
363         }.to raise_error(Puppet::Error, /lsbdistcodename fact not available: release parameter required/)
364       end
365     end
366
367     context 'invalid pin' do
368       let :facts do
369         {
370           :lsbdistid       => 'Debian',
371           :lsbdistcodename => 'wheezy',
372           :osfamily        => 'Debian',
373           :puppetversion   => Puppet.version,
374         }
375       end
376       let :params do
377         {
378           :location => 'hello.there',
379           :pin      => true,
380         }
381       end
382
383       it do
384         expect {
385           subject.call
386         }.to raise_error(Puppet::Error, /invalid value for pin/)
387       end
388     end
389
390     context "with notify_update = undef (default)" do
391       let :facts do
392         {
393           :lsbdistid       => 'Debian',
394           :lsbdistcodename => 'wheezy',
395           :osfamily        => 'Debian',
396           :puppetversion   => Puppet.version,
397         }
398       end
399       let :params do
400         {
401           :location      => 'hello.there',
402         }
403       end
404       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(true) }
405     end
406
407     context "with notify_update = true" do
408       let :facts do
409         {
410           :lsbdistid       => 'Debian',
411           :lsbdistcodename => 'wheezy',
412           :osfamily        => 'Debian',
413           :puppetversion   => Puppet.version,
414         }
415       end
416       let :params do
417         {
418           :location      => 'hello.there',
419           :notify_update => true,
420         }
421       end
422       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(true) }
423     end
424
425     context "with notify_update = false" do
426       let :facts do
427         {
428           :lsbdistid       => 'Debian',
429           :lsbdistcodename => 'wheezy',
430           :osfamily        => 'Debian',
431           :puppetversion   => Puppet.version,
432         }
433       end
434       let :params do
435         {
436           :location      => 'hello.there',
437           :notify_update => false,
438         }
439       end
440       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(false) }
441     end
442
443   end
444 end