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