]> review.fuel-infra Code Review - puppet-modules/puppetlabs-apt.git/blob - spec/defines/key_spec.rb
Allow url or domain name for key_server parameter
[puppet-modules/puppetlabs-apt.git] / spec / defines / key_spec.rb
1 require 'spec_helper'
2
3 describe 'apt::key', :type => :define do
4   let(:facts) { { :lsbdistid => 'Debian' } }
5   GPG_KEY_ID = '4BD6EC30'
6
7   let :title do
8     GPG_KEY_ID
9   end
10
11   describe 'normal operation' do
12     describe 'default options' do
13       it 'contains the apt::key' do
14         should contain_apt__key(title).with({
15           :key    => title,
16           :ensure => 'present',
17         })
18       end
19       it 'contains the apt_key' do
20         should contain_apt_key(title).with({
21           :id                => title,
22           :ensure            => 'present',
23           :source            => nil,
24           :server            => nil,
25           :content           => nil,
26           :keyserver_options => nil,
27         })
28       end
29       it 'contains the apt_key present anchor' do
30         should contain_anchor("apt_key #{title} present")
31       end
32     end
33
34     describe 'title and key =>' do
35       let :title do
36         'puppetlabs'
37       end
38
39       let :params do {
40         :key => GPG_KEY_ID,
41       } end
42
43       it 'contains the apt::key' do
44         should contain_apt__key(title).with({
45           :key    => GPG_KEY_ID,
46           :ensure => 'present',
47         })
48       end
49       it 'contains the apt_key' do
50         should contain_apt_key(title).with({
51           :id                => GPG_KEY_ID,
52           :ensure            => 'present',
53           :source            => nil,
54           :server            => nil,
55           :content           => nil,
56           :keyserver_options => nil,
57         })
58       end
59       it 'contains the apt_key present anchor' do
60         should contain_anchor("apt_key #{GPG_KEY_ID} present")
61       end
62     end
63
64     describe 'ensure => absent' do
65       let :params do {
66         :ensure => 'absent',
67       } end
68
69       it 'contains the apt::key' do
70         should contain_apt__key(title).with({
71           :key          => title,
72           :ensure       => 'absent',
73         })
74       end
75       it 'contains the apt_key' do
76         should contain_apt_key(title).with({
77           :id                => title,
78           :ensure            => 'absent',
79           :source            => nil,
80           :server            => nil,
81           :content           => nil,
82           :keyserver_options => nil,
83         })
84       end
85       it 'contains the apt_key absent anchor' do
86         should contain_anchor("apt_key #{title} absent")
87       end
88     end
89
90     describe 'key_content =>' do
91       let :params do {
92         :key_content => 'GPG key content',
93       } end
94
95       it 'contains the apt::key' do
96         should contain_apt__key(title).with({
97           :key         => title,
98           :ensure      => 'present',
99           :key_content => params[:key_content],
100         })
101       end
102       it 'contains the apt_key' do
103         should contain_apt_key(title).with({
104           :id                => title,
105           :ensure            => 'present',
106           :source            => nil,
107           :server            => nil,
108           :content           => params[:key_content],
109           :keyserver_options => nil,
110         })
111       end
112       it 'contains the apt_key present anchor' do
113         should contain_anchor("apt_key #{title} present")
114       end
115     end
116
117     describe 'key_source =>' do
118       let :params do {
119         :key_source => 'http://apt.puppetlabs.com/pubkey.gpg',
120       } end
121
122       it 'contains the apt::key' do
123         should contain_apt__key(title).with({
124           :key         => title,
125           :ensure      => 'present',
126           :key_source => params[:key_source],
127         })
128       end
129       it 'contains the apt_key' do
130         should contain_apt_key(title).with({
131           :id                => title,
132           :ensure            => 'present',
133           :source            => params[:key_source],
134           :server            => nil,
135           :content           => nil,
136           :keyserver_options => nil,
137         })
138       end
139       it 'contains the apt_key present anchor' do
140         should contain_anchor("apt_key #{title} present")
141       end
142     end
143
144     describe 'key_server =>' do
145         context 'domain name' do
146         let :params do {
147           :key_server => 'pgp.mit.edu',
148         } end
149
150         it 'contains the apt::key' do
151           should contain_apt__key(title).with({
152             :key         => title,
153             :ensure      => 'present',
154             :key_server  => 'pgp.mit.edu',
155           })
156         end
157         it 'contains the apt_key' do
158           should contain_apt_key(title).with({
159             :id                => title,
160             :ensure            => 'present',
161             :source            => nil,
162             :server            => params[:key_server],
163             :content           => nil,
164             :keyserver_options => nil,
165           })
166         end
167         it 'contains the apt_key present anchor' do
168           should contain_anchor("apt_key #{title} present")
169         end
170                         end
171
172       context "url" do
173         let (:params) do{
174           :key_server => 'hkp://pgp.mit.edu',
175         } end
176         it "should contain apt::key" do
177          should contain_apt__key(title).with({
178            :key         => title,
179            :ensure      => 'present',
180            :key_server  => 'hkp://pgp.mit.edu',
181          })
182         end
183       end
184       context "url with port number" do
185         let (:params) do{
186           :key_server => 'hkp://pgp.mit.edu:80',
187         } end
188         it "should contain apt::key" do
189          should contain_apt__key(title).with({
190             :key        => title,
191             :ensure     => 'present',
192             :key_server => 'hkp://pgp.mit.edu:80',
193          })
194         end
195       end
196
197       context "incorrect port number url" do
198         let (:params) do{
199           :key_server => 'hkp://pgp.mit.edu:8008080'
200         } end
201         it 'fails' do
202           expect { subject }.to raise_error(/does not match/)
203         end
204       end
205       context "incorrect protocol for  url" do
206         let (:params) do{
207           :key_server => 'abc://pgp.mit.edu:80'
208         } end
209         it 'fails' do
210           expect { subject }.to raise_error(/does not match/)
211         end
212       end
213       context "missing port number url" do
214         let (:params) do{
215           :key_server => 'hkp://pgp.mit.edu:'
216         } end
217         it 'fails' do
218           expect { subject }.to raise_error(/does not match/)
219         end
220       end
221       context "malform url" do
222         let (:params) do{
223           :key_server => 'hkp://pgp.mit.edu.'
224         } end
225         it 'fails' do
226           expect { subject }.to raise_error(/does not match/)
227         end
228       end
229       context "exceed characher url" do
230         let (:params) do{
231           :key_server => 'hkp://pgpiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii.mit.edu'
232         } end
233         it 'fails' do
234           expect { subject }.to raise_error(/does not match/)
235         end
236       end
237
238                 end
239
240     describe 'key_options =>' do
241       let :params do {
242         :key_options => 'debug',
243       } end
244
245       it 'contains the apt::key' do
246         should contain_apt__key(title).with({
247           :key          => title,
248           :ensure       => 'present',
249           :key_options  => 'debug',
250         })
251       end
252       it 'contains the apt_key' do
253         should contain_apt_key(title).with({
254           :id                => title,
255           :ensure            => 'present',
256           :source            => nil,
257           :server            => nil,
258           :content           => nil,
259           :keyserver_options => params[:key_options],
260         })
261       end
262       it 'contains the apt_key present anchor' do
263         should contain_anchor("apt_key #{title} present")
264       end
265     end
266   end
267
268   describe 'validation' do
269     context 'invalid key' do
270       let :title do
271         'Out of rum. Why? Why are we out of rum?'
272       end
273       it 'fails' do
274         expect { subject }.to raise_error(/does not match/)
275       end
276     end
277
278     context 'invalid source' do
279       let :params do {
280         :key_source => 'afp://puppetlabs.com/key.gpg',
281       } end
282       it 'fails' do
283         expect { subject }.to raise_error(/does not match/)
284       end
285     end
286
287     context 'invalid content' do
288       let :params do {
289         :key_content => [],
290       } end
291       it 'fails' do
292         expect { subject }.to raise_error(/is not a string/)
293       end
294     end
295
296     context 'invalid server' do
297       let :params do {
298         :key_server => 'two bottles of rum',
299       } end
300       it 'fails' do
301         expect { subject }.to raise_error(/does not match/)
302       end
303     end
304
305     context 'invalid keyserver_options' do
306       let :params do {
307         :key_options => {},
308       } end
309       it 'fails' do
310         expect { subject }.to raise_error(/is not a string/)
311       end
312     end
313   end
314
315   describe 'duplication' do
316     context 'two apt::key resources for same key, different titles' do
317       let :pre_condition do
318         "apt::key { 'duplicate': key => #{title}, }"
319       end
320
321       it 'contains two apt::key resources' do
322         should contain_apt__key('duplicate').with({
323           :key    => title,
324           :ensure => 'present',
325         })
326         should contain_apt__key(title).with({
327           :key    => title,
328           :ensure => 'present',
329         })
330       end
331
332       it 'contains only a single apt_key' do
333         should contain_apt_key('duplicate').with({
334           :id                => title,
335           :ensure            => 'present',
336           :source            => nil,
337           :server            => nil,
338           :content           => nil,
339           :keyserver_options => nil,
340         })
341         should_not contain_apt_key(title)
342       end
343     end
344
345     context 'two apt::key resources, different ensure' do
346       let :pre_condition do
347         "apt::key { 'duplicate': key => #{title}, ensure => 'absent', }"
348       end
349       it 'informs the user of the impossibility' do
350         expect { subject }.to raise_error(/already ensured as absent/)
351       end
352     end
353   end
354 end