cb9803557d124ad5cfa840912c511b44ca201235
[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         :include => { 'deb' => false },
276         :location    => 'hello.there',
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         :include => { 'deb' => false, 'src' => true },
300         :location    => 'hello.there',
301       }
302     end
303
304     it { is_expected.to contain_apt__setting('list-my_source').with({
305       :ensure => 'present',
306     }).with_content(/deb-src hello.there wheezy main\n/)
307     }
308     it { is_expected.to contain_apt__setting('list-my_source').without_content(/deb hello.there wheezy main\n/) }
309   end
310
311   context 'ensure => absent' do
312     let :facts do
313       {
314         :os => { :family => 'Debian', :name => 'Debian', :release => { :major => '7', :full => '7.0' }},
315         :lsbdistid       => 'Debian',
316         :lsbdistcodename => 'wheezy',
317         :osfamily        => 'Debian',
318         :puppetversion   => Puppet.version,
319       }
320     end
321     let :params do
322       {
323         :ensure => 'absent',
324       }
325     end
326
327     it { is_expected.to contain_apt__setting('list-my_source').with({
328       :ensure => 'absent'
329     })
330     }
331   end
332
333   describe 'validation' do
334     context 'no release' do
335       let :facts do
336         {
337           :os => { :family => 'Debian', :name => 'Debian', :release => { :major => '7', :full => '7.0' }},
338           :lsbdistid       => 'Debian',
339           :osfamily        => 'Debian',
340           :puppetversion   => Puppet.version,
341         }
342       end
343       let(:params) { { :location => 'hello.there', } }
344
345       it do
346         expect {
347           subject.call
348         }.to raise_error(Puppet::Error, /lsbdistcodename fact not available: release parameter required/)
349       end
350     end
351
352     context 'invalid pin' do
353       let :facts do
354         {
355           :os => { :family => 'Debian', :name => 'Debian', :release => { :major => '7', :full => '7.0' }},
356           :lsbdistid       => 'Debian',
357           :lsbdistcodename => 'wheezy',
358           :osfamily        => 'Debian',
359           :puppetversion   => Puppet.version,
360         }
361       end
362       let :params do
363         {
364           :location => 'hello.there',
365           :pin      => true,
366         }
367       end
368
369       it do
370         expect {
371           subject.call
372         }.to raise_error(Puppet::Error, /invalid value for pin/)
373       end
374     end
375
376     context "with notify_update = undef (default)" do
377       let :facts do
378         {
379           :os => { :family => 'Debian', :name => 'Debian', :release => { :major => '7', :full => '7.0' }},
380           :lsbdistid       => 'Debian',
381           :lsbdistcodename => 'wheezy',
382           :osfamily        => 'Debian',
383           :puppetversion   => Puppet.version,
384         }
385       end
386       let :params do
387         {
388           :location      => 'hello.there',
389         }
390       end
391       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(true) }
392     end
393
394     context "with notify_update = true" do
395       let :facts do
396         {
397           :os => { :family => 'Debian', :name => 'Debian', :release => { :major => '7', :full => '7.0' }},
398           :lsbdistid       => 'Debian',
399           :lsbdistcodename => 'wheezy',
400           :osfamily        => 'Debian',
401           :puppetversion   => Puppet.version,
402         }
403       end
404       let :params do
405         {
406           :location      => 'hello.there',
407           :notify_update => true,
408         }
409       end
410       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(true) }
411     end
412
413     context "with notify_update = false" do
414       let :facts do
415         {
416           :os => { :family => 'Debian', :name => 'Debian', :release => { :major => '7', :full => '7.0' }},
417           :lsbdistid       => 'Debian',
418           :lsbdistcodename => 'wheezy',
419           :osfamily        => 'Debian',
420           :puppetversion   => Puppet.version,
421         }
422       end
423       let :params do
424         {
425           :location      => 'hello.there',
426           :notify_update => false,
427         }
428       end
429       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(false) }
430     end
431
432   end
433 end