(GH-1055) Fix hardcoded cache path
[puppet-modules/puppetlabs-apt.git] / spec / defines / ppa_spec.rb
1 # frozen_string_literal: true
2
3 require 'spec_helper'
4
5 def ppa_exec_params(user, repo, distro = 'trusty', environment = [])
6   [
7     environment: environment,
8     command: "/opt/puppetlabs/puppet/cache/add-apt-repository-#{user}-ubuntu-#{repo}-#{distro}.sh",
9     logoutput: 'on_failure',
10   ]
11 end
12
13 describe 'apt::ppa' do
14   let :pre_condition do
15     'class { "apt": }'
16   end
17
18   describe 'defaults' do
19     let :facts do
20       {
21         os: {
22           family: 'Debian',
23           name: 'Ubuntu',
24           release: {
25             major: '18',
26             full: '18.04',
27           },
28           distro: {
29             codename: 'trusty',
30             id: 'Ubuntu',
31           },
32         },
33         puppet_vardir: '/opt/puppetlabs/puppet/cache'
34       }
35     end
36
37     let(:title) { 'ppa:needs/substitution' }
38
39     it { is_expected.not_to contain_package('python-software-properties') }
40     it {
41       is_expected.to contain_exec('add-apt-repository-ppa:needs/substitution')
42         .that_notifies('Class[Apt::Update]')
43         .with(*ppa_exec_params('needs', 'substitution'))
44     }
45   end
46
47   describe 'Ubuntu 15.10 sources.list filename' do
48     let :facts do
49       {
50         os: {
51           family: 'Debian',
52           name: 'Ubuntu',
53           release: {
54             major: '15',
55             full: '15.10',
56           },
57           distro: {
58             codename: 'wily',
59             id: 'Ubuntu',
60           },
61         },
62         puppet_vardir: '/opt/puppetlabs/puppet/cache',
63       }
64     end
65
66     let(:title) { 'ppa:user/foo' }
67
68     it {
69       is_expected.to contain_exec('add-apt-repository-ppa:user/foo')
70         .that_notifies('Class[Apt::Update]')
71         .with(*ppa_exec_params('user', 'foo', 'wily'))
72     }
73   end
74
75   describe 'package_name => software-properties-common' do
76     let :pre_condition do
77       'class { "apt": }'
78     end
79
80     let :params do
81       {
82         package_name: 'software-properties-common',
83         package_manage: true,
84       }
85     end
86
87     let :facts do
88       {
89         os: {
90           family: 'Debian',
91           name: 'Ubuntu',
92           release: {
93             major: '18',
94             full: '18.04',
95           },
96           distro: {
97             codename: 'trusty',
98             id: 'Ubuntu',
99           },
100         },
101         puppet_vardir: '/opt/puppetlabs/puppet/cache',
102       }
103     end
104
105     let(:title) { 'ppa:needs/substitution' }
106
107     it { is_expected.to contain_package('software-properties-common') }
108     it {
109       is_expected.to contain_exec('add-apt-repository-ppa:needs/substitution')
110         .that_notifies('Class[Apt::Update]')
111         .with(*ppa_exec_params('needs', 'substitution'))
112     }
113   end
114
115   describe 'package_manage => false' do
116     let :pre_condition do
117       'class { "apt": }'
118     end
119
120     let :facts do
121       {
122         os: {
123           family: 'Debian',
124           name: 'Ubuntu',
125           release: {
126             major: '18',
127             full: '18.04',
128           },
129           distro: {
130             codename: 'trusty',
131             id: 'Ubuntu',
132           },
133         },
134         puppet_vardir: '/opt/puppetlabs/puppet/cache',
135       }
136     end
137
138     let :params do
139       {
140         package_manage: false,
141       }
142     end
143
144     let(:title) { 'ppa:needs/substitution' }
145
146     it { is_expected.not_to contain_package('python-software-properties') }
147     it {
148       is_expected.to contain_exec('add-apt-repository-ppa:needs/substitution')
149         .that_notifies('Class[Apt::Update]')
150         .with(*ppa_exec_params('needs', 'substitution'))
151     }
152   end
153
154   describe 'apt included, no proxy' do
155     let :pre_condition do
156       'class { "apt": }
157       apt::ppa { "ppa:user/foo2": }
158       '
159     end
160
161     let :facts do
162       {
163         os: {
164           family: 'Debian',
165           name: 'Ubuntu',
166           release: {
167             major: '18',
168             full: '18.04',
169           },
170           distro: {
171             codename: 'trusty',
172             id: 'Ubuntu',
173           },
174         },
175         puppet_vardir: '/opt/puppetlabs/puppet/cache',
176       }
177     end
178
179     let :params do
180       {
181         package_manage: true,
182         require: 'Apt::Ppa[ppa:user/foo2]',
183       }
184     end
185
186     let(:title) { 'ppa:user/foo' }
187
188     it { is_expected.to compile.with_all_deps }
189     it { is_expected.to contain_package('software-properties-common') }
190     it {
191       is_expected.to contain_exec('add-apt-repository-ppa:user/foo')
192         .that_notifies('Class[Apt::Update]')
193         .with(*ppa_exec_params('user', 'foo'))
194     }
195   end
196
197   describe 'apt included, proxy host' do
198     let :pre_condition do
199       'class { "apt":
200         proxy => { "host" => "localhost" },
201       }'
202     end
203
204     let :facts do
205       {
206         os: {
207           family: 'Debian',
208           name: 'Ubuntu',
209           release: {
210             major: '18',
211             full: '18.04',
212           },
213           distro: {
214             codename: 'trusty',
215             id: 'Ubuntu',
216           },
217         },
218         puppet_vardir: '/opt/puppetlabs/puppet/cache',
219       }
220     end
221
222     let :params do
223       {
224         'package_manage' => true,
225       }
226     end
227
228     let(:title) { 'ppa:user/foo' }
229
230     it { is_expected.to contain_package('software-properties-common') }
231     it {
232       is_expected.to contain_exec('add-apt-repository-ppa:user/foo')
233         .that_notifies('Class[Apt::Update]')
234         .with(*ppa_exec_params('user', 'foo', 'trusty', ['http_proxy=http://localhost:8080']))
235     }
236   end
237
238   describe 'apt included, proxy host and port' do
239     let :pre_condition do
240       'class { "apt":
241         proxy => { "host" => "localhost", "port" => 8180 },
242       }'
243     end
244
245     let :facts do
246       {
247         os: {
248           family: 'Debian',
249           name: 'Ubuntu',
250           release: {
251             major: '18',
252             full: '18.04',
253           },
254           distro: {
255             codename: 'trusty',
256             id: 'Ubuntu',
257           },
258         },
259         puppet_vardir: '/opt/puppetlabs/puppet/cache',
260       }
261     end
262
263     let :params do
264       {
265         package_manage: true,
266       }
267     end
268
269     let(:title) { 'ppa:user/foo' }
270
271     it { is_expected.to contain_package('software-properties-common') }
272     it {
273       is_expected.to contain_exec('add-apt-repository-ppa:user/foo')
274         .that_notifies('Class[Apt::Update]')
275         .with(*ppa_exec_params('user', 'foo', 'trusty', ['http_proxy=http://localhost:8180']))
276     }
277   end
278
279   describe 'apt included, proxy host and port and https' do
280     let :pre_condition do
281       'class { "apt":
282         proxy => { "host" => "localhost", "port" => 8180, "https" => true },
283       }'
284     end
285
286     let :facts do
287       {
288         os: {
289           family: 'Debian',
290           name: 'Ubuntu',
291           release: {
292             major: '18',
293             full: '18.04',
294           },
295           distro: {
296             codename: 'trusty',
297             id: 'Ubuntu',
298           },
299         },
300         puppet_vardir: '/opt/puppetlabs/puppet/cache',
301       }
302     end
303
304     let :params do
305       {
306         package_manage: true,
307       }
308     end
309
310     let(:title) { 'ppa:user/foo' }
311
312     it { is_expected.to contain_package('software-properties-common') }
313     it {
314       is_expected.to contain_exec('add-apt-repository-ppa:user/foo')
315         .that_notifies('Class[Apt::Update]')
316         .with(*ppa_exec_params('user', 'foo', 'trusty', ['http_proxy=http://localhost:8180', 'https_proxy=https://localhost:8180']))
317     }
318   end
319
320   describe 'ensure absent' do
321     let :pre_condition do
322       'class { "apt": }'
323     end
324
325     let :facts do
326       {
327         os: {
328           family: 'Debian',
329           name: 'Ubuntu',
330           release: {
331             major: '18',
332             full: '18.04',
333           },
334           distro: {
335             codename: 'trusty',
336             id: 'Ubuntu',
337           },
338         },
339         puppet_vardir: '/opt/puppetlabs/puppet/cache',
340       }
341     end
342
343     let(:title) { 'ppa:user/foo' }
344
345     let :params do
346       {
347         ensure: 'absent',
348       }
349     end
350
351     it {
352       is_expected.to contain_tidy("remove-apt-repository-script-#{title}")
353         .with('path' => '/opt/puppetlabs/puppet/cache/add-apt-repository-user-ubuntu-foo-trusty.sh')
354
355       is_expected.to contain_tidy("remove-apt-repository-#{title}")
356         .with('path' => '/etc/apt/sources.list.d/user-ubuntu-foo-trusty.list')
357         .that_notifies('Class[Apt::Update]')
358     }
359   end
360
361   context 'with validation' do
362     describe 'no release' do
363       let :facts do
364         {
365           os: {
366             family: 'Debian',
367             name: 'Ubuntu',
368             release: {
369               major: '18',
370               full: '18.04',
371             },
372             distro: {
373               codename: nil,
374               id: 'Ubuntu',
375             },
376           },
377         }
378       end
379
380       let(:title) { 'ppa:user/foo' }
381
382       it do
383         is_expected.to raise_error(Puppet::Error, %r{os.distro.codename fact not available: release parameter required})
384       end
385     end
386
387     describe 'not ubuntu' do
388       let :facts do
389         {
390           os: {
391             family: 'Debian',
392             name: 'Debian',
393             release: {
394               major: '6',
395               full: '6.0.7',
396             },
397             distro: {
398               codename: 'wheezy',
399               id: 'Debian',
400             },
401           },
402         }
403       end
404
405       let(:title) { 'ppa:user/foo' }
406
407       it do
408         is_expected.to raise_error(Puppet::Error, %r{not currently supported on Debian})
409       end
410     end
411   end
412 end