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