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