Update tests to work with rspec-puppet 2.x
[puppet-modules/puppetlabs-apt.git] / spec / defines / source_spec.rb
1 require 'spec_helper'
2
3 describe 'apt::source' do
4   GPG_KEY_ID = '47B320EB4C7C375AA9DAE1A01054B7A24BD6EC30'
5
6   let :pre_condition do
7     'class { "apt": }'
8   end
9
10   let :title do
11     'my_source'
12   end
13
14   context 'defaults' do
15     context 'without location' do
16       let :facts do
17         {
18           :lsbdistid       => 'Debian',
19           :lsbdistcodename => 'wheezy',
20           :osfamily        => 'Debian'
21         }
22       end
23       it do
24         expect {
25           subject.call
26         }.to raise_error(Puppet::Error, /source entry without specifying a location/)
27       end
28     end
29     context 'with location' do
30       let :facts do
31         {
32           :lsbdistid       => 'Debian',
33           :lsbdistcodename => 'wheezy',
34           :osfamily        => 'Debian'
35         }
36       end
37       let(:params) { { :location => 'hello.there', } }
38
39       it { is_expected.to contain_apt__setting('list-my_source').with({
40         :ensure  => 'present',
41       }).without_content(/# my_source\ndeb-src hello.there wheezy main\n/)
42       }
43     end
44   end
45
46   describe 'no defaults' do
47     let :facts do
48       {
49         :lsbdistid       => 'Debian',
50         :lsbdistcodename => 'wheezy',
51         :osfamily        => 'Debian'
52       }
53     end
54
55     context 'with complex pin' do
56       let :params do
57         {
58           :location => 'hello.there',
59           :pin      => { 'release'     => 'wishwash',
60                          'explanation' => 'wishwash',
61                          'priority'    => 1001, },
62         }
63       end
64
65       it { is_expected.to contain_apt__setting('list-my_source').with({
66         :ensure => 'present',
67       }).with_content(/hello.there wheezy main\n/)
68       }
69
70       it { is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with({
71         :ensure       => 'present',
72         :priority     => 1001,
73         :explanation  => 'wishwash',
74         :release      => 'wishwash',
75       })
76       }
77     end
78
79     context 'with simple key' do
80       let :params do
81         {
82           :comment           => 'foo',
83           :location          => 'http://debian.mirror.iweb.ca/debian/',
84           :release           => 'sid',
85           :repos             => 'testing',
86           :key               => GPG_KEY_ID,
87           :pin               => '10',
88           :architecture      => 'x86_64',
89           :allow_unsigned    => true,
90         }
91       end
92
93       it { is_expected.to contain_apt__setting('list-my_source').with({
94         :ensure => 'present',
95       }).with_content(/# foo\ndeb \[arch=x86_64 trusted=yes\] http:\/\/debian\.mirror\.iweb\.ca\/debian\/ sid testing\n/).without_content(/deb-src/)
96       }
97
98       it { is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with({
99         :ensure   => 'present',
100         :priority => '10',
101         :origin   => 'debian.mirror.iweb.ca',
102       })
103       }
104
105       it { is_expected.to contain_apt__key("Add key: #{GPG_KEY_ID} from Apt::Source my_source").that_comes_before('Apt::Setting[list-my_source]').with({
106         :ensure  => 'present',
107         :id      => GPG_KEY_ID,
108       })
109       }
110     end
111
112     context 'with complex key' do
113       let :params do
114         {
115           :comment           => 'foo',
116           :location          => 'http://debian.mirror.iweb.ca/debian/',
117           :release           => 'sid',
118           :repos             => 'testing',
119           :key               => { 'id' => GPG_KEY_ID, 'server' => 'pgp.mit.edu',
120                                   'content' => 'GPG key content',
121                                   'source'  => 'http://apt.puppetlabs.com/pubkey.gpg',},
122           :pin               => '10',
123           :architecture      => 'x86_64',
124           :allow_unsigned    => true,
125         }
126       end
127
128       it { is_expected.to contain_apt__setting('list-my_source').with({
129         :ensure => 'present',
130       }).with_content(/# foo\ndeb \[arch=x86_64 trusted=yes\] http:\/\/debian\.mirror\.iweb\.ca\/debian\/ sid testing\n/).without_content(/deb-src/)
131       }
132
133       it { is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with({
134         :ensure   => 'present',
135         :priority => '10',
136         :origin   => 'debian.mirror.iweb.ca',
137       })
138       }
139
140       it { is_expected.to contain_apt__key("Add key: #{GPG_KEY_ID} from Apt::Source my_source").that_comes_before('Apt::Setting[list-my_source]').with({
141         :ensure  => 'present',
142         :id      => GPG_KEY_ID,
143         :server  => 'pgp.mit.edu',
144         :content => 'GPG key content',
145         :source  => 'http://apt.puppetlabs.com/pubkey.gpg',
146       })
147       }
148     end
149
150     context 'with simple key' do
151       let :params do
152         {
153           :comment        => 'foo',
154           :location       => 'http://debian.mirror.iweb.ca/debian/',
155           :release        => 'sid',
156           :repos          => 'testing',
157           :key            => GPG_KEY_ID,
158           :pin            => '10',
159           :architecture   => 'x86_64',
160           :allow_unsigned => true,
161         }
162       end
163
164       it { is_expected.to contain_apt__setting('list-my_source').with({
165         :ensure => 'present',
166       }).with_content(/# foo\ndeb \[arch=x86_64 trusted=yes\] http:\/\/debian\.mirror\.iweb\.ca\/debian\/ sid testing\n/).without_content(/deb-src/)
167       }
168
169       it { is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with({
170         :ensure   => 'present',
171         :priority => '10',
172         :origin   => 'debian.mirror.iweb.ca',
173       })
174       }
175
176       it { is_expected.to contain_apt__key("Add key: #{GPG_KEY_ID} from Apt::Source my_source").that_comes_before('Apt::Setting[list-my_source]').with({
177         :ensure  => 'present',
178         :id      => GPG_KEY_ID,
179       })
180       }
181     end
182   end
183
184   context 'allow_unsigned true' do
185     let :facts do
186       {
187         :lsbdistid       => 'Debian',
188         :lsbdistcodename => 'wheezy',
189         :osfamily        => 'Debian'
190       }
191     end
192     let :params do
193       {
194         :location       => 'hello.there',
195         :allow_unsigned => true,
196       }
197     end
198
199     it { is_expected.to contain_apt__setting('list-my_source').with({
200       :ensure => 'present',
201     }).with_content(/# my_source\ndeb \[trusted=yes\] hello.there wheezy main\n/)
202     }
203   end
204
205   context 'architecture equals x86_64' do
206     let :facts do
207       {
208         :lsbdistid       => 'Debian',
209         :lsbdistcodename => 'wheezy',
210         :osfamily        => 'Debian'
211       }
212     end
213     let :params do
214       {
215         :location     => 'hello.there',
216         :include      => {'deb' => false, 'src' => true,},
217         :architecture => 'x86_64',
218       }
219     end
220
221     it { is_expected.to contain_apt__setting('list-my_source').with({
222       :ensure => 'present',
223     }).with_content(/# my_source\ndeb-src \[arch=x86_64 \] hello.there wheezy main\n/)
224     }
225   end
226
227   context 'ensure => absent' do
228     let :facts do
229       {
230         :lsbdistid       => 'Debian',
231         :lsbdistcodename => 'wheezy',
232         :osfamily        => 'Debian'
233       }
234     end
235     let :params do
236       {
237         :ensure => 'absent',
238       }
239     end
240
241     it { is_expected.to contain_apt__setting('list-my_source').with({
242       :ensure => 'absent'
243     })
244     }
245   end
246
247   describe 'validation' do
248     context 'no release' do
249       let :facts do
250         {
251           :lsbdistid       => 'Debian',
252           :osfamily        => 'Debian'
253         }
254       end
255       let(:params) { { :location => 'hello.there', } }
256
257       it do
258         expect {
259           subject.call
260         }.to raise_error(Puppet::Error, /lsbdistcodename fact not available: release parameter required/)
261       end
262     end
263
264     context 'invalid pin' do
265       let :facts do
266         {
267           :lsbdistid       => 'Debian',
268           :lsbdistcodename => 'wheezy',
269           :osfamily        => 'Debian'
270         }
271       end
272       let :params do
273         {
274           :location => 'hello.there',
275           :pin      => true,
276         }
277       end
278
279       it do
280         expect {
281           subject.call
282         }.to raise_error(Puppet::Error, /invalid value for pin/)
283       end
284     end
285
286   end
287 end