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