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