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