Merge pull request #456 from johanfleury/cleaning/template/unattended-upgrades
[puppet-modules/puppetlabs-apt.git] / spec / defines / ppa_spec.rb
1 require 'spec_helper'
2 describe 'apt::ppa', :type => :define do
3
4   describe 'defaults' do
5     let :pre_condition do
6       'class { "apt": }'
7     end
8     let :facts do
9       {
10         :lsbdistrelease  => '11.04',
11         :lsbdistcodename => 'natty',
12         :operatingsystem => 'Ubuntu',
13         :osfamily        => 'Debian',
14         :lsbdistid       => 'Ubuntu',
15       }
16     end
17
18     let(:title) { 'ppa:needs/such.substitution/wow' }
19     it { is_expected.to contain_package('python-software-properties') }
20     it { is_expected.to contain_exec('add-apt-repository-ppa:needs/such.substitution/wow').that_notifies('Exec[apt_update]').with({
21       'environment' => [],
22       'command'     => '/usr/bin/add-apt-repository -y ppa:needs/such.substitution/wow',
23       'unless'      => '/usr/bin/test -s /etc/apt/sources.list.d/needs-such_substitution-wow-natty.list',
24       'user'        => 'root',
25       'logoutput'   => 'on_failure',
26     })
27     }
28
29     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({
30       'ensure' => 'file',
31     })
32     }
33   end
34
35   describe 'package_name => software-properties-common' do
36     let :pre_condition do
37       'class { "apt": }'
38     end
39     let :params do
40       {
41         :package_name => 'software-properties-common'
42       }
43     end
44     let :facts do
45       {
46         :lsbdistrelease  => '11.04',
47         :lsbdistcodename => 'natty',
48         :operatingsystem => 'Ubuntu',
49         :osfamily        => 'Debian',
50         :lsbdistid       => 'Ubuntu',
51       }
52     end
53
54     let(:title) { 'ppa:needs/such.substitution/wow' }
55     it { is_expected.to contain_package('software-properties-common') }
56     it { is_expected.to contain_exec('add-apt-repository-ppa:needs/such.substitution/wow').that_notifies('Exec[apt_update]').with({
57       'environment' => [],
58       'command'     => '/usr/bin/add-apt-repository -y ppa:needs/such.substitution/wow',
59       'unless'      => '/usr/bin/test -s /etc/apt/sources.list.d/needs-such_substitution-wow-natty.list',
60       'user'        => 'root',
61       'logoutput'   => 'on_failure',
62     })
63     }
64
65     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({
66       'ensure' => 'file',
67     })
68     }
69   end
70
71   describe 'package_manage => false' do
72     let :pre_condition do
73       'class { "apt": }'
74     end
75     let :facts do
76       {
77         :lsbdistrelease  => '11.04',
78         :lsbdistcodename => 'natty',
79         :operatingsystem => 'Ubuntu',
80         :osfamily        => 'Debian',
81         :lsbdistid       => 'Ubuntu',
82       }
83     end
84     let :params do
85       {
86         :package_manage => false,
87       }
88     end
89
90     let(:title) { 'ppa:needs/such.substitution/wow' }
91     it { is_expected.to_not contain_package('python-software-properties') }
92     it { is_expected.to contain_exec('add-apt-repository-ppa:needs/such.substitution/wow').that_notifies('Exec[apt_update]').with({
93       'environment' => [],
94       'command'     => '/usr/bin/add-apt-repository -y ppa:needs/such.substitution/wow',
95       'unless'      => '/usr/bin/test -s /etc/apt/sources.list.d/needs-such_substitution-wow-natty.list',
96       'user'        => 'root',
97       'logoutput'   => 'on_failure',
98     })
99     }
100
101     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({
102       'ensure' => 'file',
103     })
104     }
105   end
106
107   describe 'apt included, no proxy' do
108     let :pre_condition do
109       'class { "apt": }'
110     end
111     let :facts do
112       {
113         :lsbdistrelease  => '14.04',
114         :lsbdistcodename => 'trusty',
115         :operatingsystem => 'Ubuntu',
116         :lsbdistid       => 'Ubuntu',
117         :osfamily        => 'Debian',
118       }
119     end
120     let :params do
121       {
122         'options' => '',
123       }
124     end
125     let(:title) { 'ppa:foo' }
126     it { is_expected.to contain_package('software-properties-common') }
127     it { is_expected.to contain_exec('add-apt-repository-ppa:foo').that_notifies('Exec[apt_update]').with({
128       'environment' => [],
129       'command'     => '/usr/bin/add-apt-repository  ppa:foo',
130       'unless'      => '/usr/bin/test -s /etc/apt/sources.list.d/foo-trusty.list',
131       'user'        => 'root',
132       'logoutput'   => 'on_failure',
133     })
134     }
135
136     it { is_expected.to contain_file('/etc/apt/sources.list.d/foo-trusty.list').that_requires('Exec[add-apt-repository-ppa:foo]').with({
137       'ensure' => 'file',
138     })
139     }
140   end
141
142   describe 'apt included, proxy' do
143     let :pre_condition do
144       'class { "apt": proxy_host => "example.com" }'
145     end
146     let :facts do
147       {
148         :lsbdistrelease  => '14.04',
149         :lsbdistcodename => 'trusty',
150         :operatingsystem => 'Ubuntu',
151         :lsbdistid       => 'Ubuntu',
152         :osfamily        => 'Debian',
153       }
154     end
155     let :params do
156       {
157         'release' => 'lucid',
158       }
159     end
160     let(:title) { 'ppa:foo' }
161     it { is_expected.to contain_package('software-properties-common') }
162     it { is_expected.to contain_exec('add-apt-repository-ppa:foo').that_notifies('Exec[apt_update]').with({
163       'environment' => ['http_proxy=http://example.com:8080', 'https_proxy=http://example.com:8080'],
164       'command'     => '/usr/bin/add-apt-repository -y ppa:foo',
165       'unless'      => '/usr/bin/test -s /etc/apt/sources.list.d/foo-lucid.list',
166       'user'        => 'root',
167       'logoutput'   => 'on_failure',
168     })
169     }
170
171     it { is_expected.to contain_file('/etc/apt/sources.list.d/foo-lucid.list').that_requires('Exec[add-apt-repository-ppa:foo]').with({
172       'ensure' => 'file',
173     })
174     }
175   end
176
177   describe 'ensure absent' do
178     let :facts do
179       {
180         :lsbdistrelease  => '14.04',
181         :lsbdistcodename => 'trusty',
182         :operatingsystem => 'Ubuntu',
183         :lsbdistid       => 'Ubuntu',
184         :osfamily        => 'Debian',
185       }
186     end
187     let(:title) { 'ppa:foo' }
188     let :params do
189       {
190         'ensure' => 'absent'
191       }
192     end
193     it { is_expected.to contain_file('/etc/apt/sources.list.d/foo-trusty.list').that_notifies('Exec[apt_update]').with({
194       'ensure' => 'absent',
195     })
196     }
197   end
198
199   context 'validation' do
200     describe 'no release' do
201       let :facts do
202         {
203           :lsbdistrelease  => '14.04',
204           :operatingsystem => 'Ubuntu',
205           :lsbdistid       => 'Ubuntu',
206           :osfamily        => 'Debian',
207         }
208       end
209       let(:title) { 'ppa:foo' }
210       it do
211         expect {
212           should compile
213         }.to raise_error(Puppet::Error, /lsbdistcodename fact not available: release parameter required/)
214       end
215     end
216
217     describe 'not ubuntu' do
218       let :facts do
219         {
220           :lsbdistrelease  => '14.04',
221           :lsbdistcodename => 'trusty',
222           :operatingsystem => 'Debian',
223           :lsbdistid       => 'Ubuntu',
224           :osfamily        => 'Debian',
225         }
226       end
227       let(:title) { 'ppa:foo' }
228       it do
229         expect {
230           should compile
231         }.to raise_error(Puppet::Error, /apt::ppa is currently supported on Ubuntu only./)
232       end
233     end
234   end
235 end