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