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