Release prep v8.0.2
[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: { 'ensure' => 'refreshed',
113                  'id' => GPG_KEY_ID,
114                  'server' => 'pgp.mit.edu',
115                  'content' => 'GPG key content',
116                  'source'  => 'http://apt.puppetlabs.com/pubkey.gpg' },
117           pin: '10',
118           architecture: 'x86_64',
119           allow_unsigned: true,
120         }
121       end
122
123       it {
124         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})
125                                                              .without_content(%r{deb-src})
126       }
127
128       it {
129         is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with(ensure: 'present',
130                                                                                                             priority: '10',
131                                                                                                             origin: 'debian.mirror.iweb.ca')
132       }
133
134       it {
135         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',
136                                                                                                                                                     id: GPG_KEY_ID,
137                                                                                                                                                     server: 'pgp.mit.edu',
138                                                                                                                                                     content: 'GPG key content',
139                                                                                                                                                     source: 'http://apt.puppetlabs.com/pubkey.gpg')
140       }
141     end
142   end
143
144   context 'with allow_unsigned true' do
145     let :params do
146       {
147         location: 'hello.there',
148         allow_unsigned: true,
149       }
150     end
151
152     it {
153       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})
154     }
155   end
156
157   context 'with a https location, install apt-transport-https' do
158     let :params do
159       {
160         location: 'HTTPS://foo.bar',
161         allow_unsigned: false,
162       }
163     end
164
165     it {
166       is_expected.to contain_package('apt-transport-https')
167     }
168   end
169
170   context 'with a https location and custom release, install apt-transport-https' do
171     let :facts do
172       {
173         os: {
174           family: 'Debian',
175           name: 'Debian',
176           release: {
177             major: '8',
178             full: '8.0',
179           },
180           distro: {
181             codename: 'jessie',
182             id: 'Debian',
183           },
184         },
185         puppetversion: Puppet.version,
186       }
187     end
188     let :params do
189       {
190         location: 'HTTPS://foo.bar',
191         allow_unsigned: false,
192         release: 'customrelease',
193       }
194     end
195
196     it {
197       is_expected.to contain_package('apt-transport-https')
198     }
199   end
200
201   context 'with a https location, do not install apt-transport-https on oses not in list eg buster' do
202     let :facts do
203       {
204         os: {
205           family: 'Debian',
206           name: 'Debian',
207           release: {
208             major: '10',
209             full: '10.0',
210           },
211           distro: {
212             codename: 'buster',
213             id: 'Debian',
214           },
215         },
216       }
217     end
218     let :params do
219       {
220         location: 'https://foo.bar',
221         allow_unsigned: false,
222       }
223     end
224
225     it {
226       is_expected.not_to contain_package('apt-transport-https')
227     }
228   end
229
230   context 'with architecture equals x86_64' do
231     let :facts do
232       {
233         os: {
234           family: 'Debian',
235           name: 'Debian',
236           release: {
237             major: '7',
238             full: '7.0',
239           },
240           distro: {
241             codename: 'wheezy',
242             id: 'Debian',
243           },
244         },
245       }
246     end
247     let :params do
248       {
249         location: 'hello.there',
250         include: { 'deb' => false, 'src' => true },
251         architecture: 'x86_64',
252       }
253     end
254
255     it {
256       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})
257     }
258   end
259
260   context 'with architecture fact and unset architecture parameter' do
261     let :facts do
262       super().merge(architecture: 'amd64')
263     end
264     let :params do
265       {
266         location: 'hello.there',
267         include: { 'deb' => false, 'src' => true },
268       }
269     end
270
271     it {
272       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{# my_source\ndeb-src hello.there jessie main\n})
273     }
274   end
275
276   context 'with include_src => true' do
277     let :params do
278       {
279         location: 'hello.there',
280         include: { 'src' => true },
281       }
282     end
283
284     it {
285       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})
286     }
287   end
288
289   context 'with include deb => false' do
290     let :params do
291       {
292         include: { 'deb' => false },
293         location: 'hello.there',
294       }
295     end
296
297     it {
298       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').without_content(%r{deb-src hello.there wheezy main\n})
299     }
300     it { is_expected.to contain_apt__setting('list-my_source').without_content(%r{deb hello.there wheezy main\n}) }
301   end
302
303   context 'with include src => true and include deb => false' do
304     let :params do
305       {
306         include: { 'deb' => false, 'src' => true },
307         location: 'hello.there',
308       }
309     end
310
311     it {
312       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'present').with_content(%r{deb-src hello.there jessie main\n})
313     }
314     it { is_expected.to contain_apt__setting('list-my_source').without_content(%r{deb hello.there jessie main\n}) }
315   end
316
317   context 'with ensure => absent' do
318     let :params do
319       {
320         ensure: 'absent',
321       }
322     end
323
324     it {
325       is_expected.to contain_apt__setting('list-my_source').with(ensure: 'absent')
326     }
327   end
328
329   describe 'validation' do
330     context 'with no release' do
331       let :facts do
332         {
333           os: {
334             family: 'Debian',
335             name: 'Debian',
336             release: {
337               major: '8',
338               full: '8.0',
339             },
340             distro: {
341               id: 'Debian',
342             },
343           },
344         }
345       end
346       let(:params) { { location: 'hello.there' } }
347
348       it do
349         is_expected.to raise_error(Puppet::Error, %r{os.distro.codename fact not available: release parameter required})
350       end
351     end
352
353     context 'with release is empty string' do
354       let(:params) { { location: 'hello.there', release: '' } }
355
356       it { is_expected.to contain_apt__setting('list-my_source').with_content(%r{hello\.there  main}) }
357     end
358
359     context 'with invalid pin' do
360       let :params do
361         {
362           location: 'hello.there',
363           pin: true,
364         }
365       end
366
367       it do
368         is_expected.to raise_error(Puppet::Error, %r{expects a value})
369       end
370     end
371
372     context 'with notify_update = undef (default)' do
373       let :params do
374         {
375           location: 'hello.there',
376         }
377       end
378
379       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(true) }
380     end
381
382     context 'with notify_update = true' do
383       let :params do
384         {
385           location: 'hello.there',
386           notify_update: true,
387         }
388       end
389
390       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(true) }
391     end
392
393     context 'with notify_update = false' do
394       let :params do
395         {
396           location: 'hello.there',
397           notify_update: false,
398         }
399       end
400
401       it { is_expected.to contain_apt__setting("list-#{title}").with_notify_update(false) }
402     end
403   end
404 end