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