Merge pull request #324 from cmurphy/master
[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 "domain with dash" do
173         let(:params) do{
174           :key_server => 'p-gp.m-it.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 => 'p-gp.m-it.edu',
181           })
182         end
183       end
184
185       context "domain begin with dash" do
186         let(:params) do{
187           :key_server => '-pgp.mit.edu',
188         } end
189         it 'fails' do
190           expect { subject } .to raise_error(/does not match/)
191         end
192       end
193
194       context "domain begin with dot" do
195         let(:params) do{
196           :key_server => '.pgp.mit.edu',
197         } end
198         it 'fails' do
199           expect { subject } .to raise_error(/does not match/)
200         end
201       end
202
203       context "domain end with dot" do
204         let(:params) do{
205           :key_server => "pgp.mit.edu.",
206         } end
207         it 'fails' do
208           expect { subject } .to raise_error(/does not match/)
209         end
210       end
211
212       context "url" do
213         let (:params) do{
214           :key_server => 'hkp://pgp.mit.edu',
215         } end
216         it "should contain apt::key" do
217          should contain_apt__key(title).with({
218            :key         => title,
219            :ensure      => 'present',
220            :key_server  => 'hkp://pgp.mit.edu',
221          })
222         end
223       end
224       context "url with port number" do
225         let (:params) do{
226           :key_server => 'hkp://pgp.mit.edu:80',
227         } end
228         it "should contain apt::key" do
229          should contain_apt__key(title).with({
230             :key        => title,
231             :ensure     => 'present',
232             :key_server => 'hkp://pgp.mit.edu:80',
233          })
234         end
235       end
236
237       context "incorrect port number url" do
238         let (:params) do{
239           :key_server => 'hkp://pgp.mit.edu:8008080'
240         } end
241         it 'fails' do
242           expect { subject }.to raise_error(/does not match/)
243         end
244       end
245       context "incorrect protocol for  url" do
246         let (:params) do{
247           :key_server => 'abc://pgp.mit.edu:80'
248         } end
249         it 'fails' do
250           expect { subject }.to raise_error(/does not match/)
251         end
252       end
253       context "missing port number url" do
254         let (:params) do{
255           :key_server => 'hkp://pgp.mit.edu:'
256         } end
257         it 'fails' do
258           expect { subject }.to raise_error(/does not match/)
259         end
260       end
261       context "url ending with a dot" do
262         let (:params) do{
263           :key_server => 'hkp://pgp.mit.edu.'
264         } end
265         it 'fails' do
266           expect { subject }.to raise_error(/does not match/)
267         end
268       end
269       context "url begin with a dash" do
270         let(:params) do{
271           :key_server => "hkp://-pgp.mit.edu",
272         } end
273         it 'fails' do
274           expect { subject }.to raise_error(/does not match/)
275         end
276       end
277       context "url with dash" do
278         let(:params) do{
279           :key_server => 'hkp://p-gp.m-it.edu',
280         } end
281         it "should contain apt::key" do
282           should contain_apt__key(title).with({
283             :key        => title,
284             :ensure     => 'present',
285             :key_server => 'hkp://p-gp.m-it.edu',
286           })
287         end
288       end
289       context "exceed characher url" do
290         let (:params) do{
291           :key_server => 'hkp://pgpiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii.mit.edu'
292         } end
293         it 'fails' do
294           expect { subject }.to raise_error(/does not match/)
295         end
296       end
297                 end
298
299     describe 'key_options =>' do
300       let :params do {
301         :key_options => 'debug',
302       } end
303
304       it 'contains the apt::key' do
305         should contain_apt__key(title).with({
306           :key          => title,
307           :ensure       => 'present',
308           :key_options  => 'debug',
309         })
310       end
311       it 'contains the apt_key' do
312         should contain_apt_key(title).with({
313           :id                => title,
314           :ensure            => 'present',
315           :source            => nil,
316           :server            => nil,
317           :content           => nil,
318           :keyserver_options => params[:key_options],
319         })
320       end
321       it 'contains the apt_key present anchor' do
322         should contain_anchor("apt_key #{title} present")
323       end
324     end
325   end
326
327   describe 'validation' do
328     context 'invalid key' do
329       let :title do
330         'Out of rum. Why? Why are we out of rum?'
331       end
332       it 'fails' do
333         expect { subject }.to raise_error(/does not match/)
334       end
335     end
336
337     context 'invalid source' do
338       let :params do {
339         :key_source => 'afp://puppetlabs.com/key.gpg',
340       } end
341       it 'fails' do
342         expect { subject }.to raise_error(/does not match/)
343       end
344     end
345
346     context 'invalid content' do
347       let :params do {
348         :key_content => [],
349       } end
350       it 'fails' do
351         expect { subject }.to raise_error(/is not a string/)
352       end
353     end
354
355     context 'invalid server' do
356       let :params do {
357         :key_server => 'two bottles of rum',
358       } end
359       it 'fails' do
360         expect { subject }.to raise_error(/does not match/)
361       end
362     end
363
364     context 'invalid keyserver_options' do
365       let :params do {
366         :key_options => {},
367       } end
368       it 'fails' do
369         expect { subject }.to raise_error(/is not a string/)
370       end
371     end
372   end
373
374   describe 'duplication' do
375     context 'two apt::key resources for same key, different titles' do
376       let :pre_condition do
377         "apt::key { 'duplicate': key => #{title}, }"
378       end
379
380       it 'contains two apt::key resources' do
381         should contain_apt__key('duplicate').with({
382           :key    => title,
383           :ensure => 'present',
384         })
385         should contain_apt__key(title).with({
386           :key    => title,
387           :ensure => 'present',
388         })
389       end
390
391       it 'contains only a single apt_key' do
392         should contain_apt_key('duplicate').with({
393           :id                => title,
394           :ensure            => 'present',
395           :source            => nil,
396           :server            => nil,
397           :content           => nil,
398           :keyserver_options => nil,
399         })
400         should_not contain_apt_key(title)
401       end
402     end
403
404     context 'two apt::key resources, different ensure' do
405       let :pre_condition do
406         "apt::key { 'duplicate': key => #{title}, ensure => 'absent', }"
407       end
408       it 'informs the user of the impossibility' do
409         expect { subject }.to raise_error(/already ensured as absent/)
410       end
411     end
412   end
413 end