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