Merge pull request #258 from sodabrew/patch-1
[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       let :params do {
146         :key_server => 'pgp.mit.edu',
147       } end
148
149       it 'contains the apt::key' do
150         should contain_apt__key(title).with({
151           :key         => title,
152           :ensure      => 'present',
153           :key_server  => 'pgp.mit.edu',
154         })
155       end
156       it 'contains the apt_key' do
157         should contain_apt_key(title).with({
158           :id                => title,
159           :ensure            => 'present',
160           :source            => nil,
161           :server            => params[:key_server],
162           :content           => nil,
163           :keyserver_options => nil,
164         })
165       end
166       it 'contains the apt_key present anchor' do
167         should contain_anchor("apt_key #{title} present")
168       end
169     end
170
171     describe 'key_options =>' do
172       let :params do {
173         :key_options => 'debug',
174       } end
175
176       it 'contains the apt::key' do
177         should contain_apt__key(title).with({
178           :key          => title,
179           :ensure       => 'present',
180           :key_options  => 'debug',
181         })
182       end
183       it 'contains the apt_key' do
184         should contain_apt_key(title).with({
185           :id                => title,
186           :ensure            => 'present',
187           :source            => nil,
188           :server            => nil,
189           :content           => nil,
190           :keyserver_options => params[:key_options],
191         })
192       end
193       it 'contains the apt_key present anchor' do
194         should contain_anchor("apt_key #{title} present")
195       end
196     end
197   end
198
199   describe 'validation' do
200     context 'invalid key' do
201       let :title do
202         'Out of rum. Why? Why are we out of rum?'
203       end
204       it 'fails' do
205         expect { subject }.to raise_error(/does not match/)
206       end
207     end
208
209     context 'invalid source' do
210       let :params do {
211         :key_source => 'afp://puppetlabs.com/key.gpg',
212       } end
213       it 'fails' do
214         expect { subject }.to raise_error(/does not match/)
215       end
216     end
217
218     context 'invalid content' do
219       let :params do {
220         :key_content => [],
221       } end
222       it 'fails' do
223         expect { subject }.to raise_error(/is not a string/)
224       end
225     end
226
227     context 'invalid server' do
228       let :params do {
229         :key_server => 'two bottles of rum',
230       } end
231       it 'fails' do
232         expect { subject }.to raise_error(/must be a valid domain name/)
233       end
234     end
235
236     context 'invalid keyserver_options' do
237       let :params do {
238         :key_options => {},
239       } end
240       it 'fails' do
241         expect { subject }.to raise_error(/is not a string/)
242       end
243     end
244   end
245
246   describe 'duplication' do
247     context 'two apt::key resources for same key, different titles' do
248       let :pre_condition do
249         "apt::key { 'duplicate': key => #{title}, }"
250       end
251
252       it 'contains two apt::key resources' do
253         should contain_apt__key('duplicate').with({
254           :key    => title,
255           :ensure => 'present',
256         })
257         should contain_apt__key(title).with({
258           :key    => title,
259           :ensure => 'present',
260         })
261       end
262
263       it 'contains only a single apt_key' do
264         should contain_apt_key('duplicate').with({
265           :id                => title,
266           :ensure            => 'present',
267           :source            => nil,
268           :server            => nil,
269           :content           => nil,
270           :keyserver_options => nil,
271         })
272         should_not contain_apt_key(title)
273       end
274     end
275
276     context 'two apt::key resources, different ensure' do
277       let :pre_condition do
278         "apt::key { 'duplicate': key => #{title}, ensure => 'absent', }"
279       end
280       it 'informs the user of the impossibility' do
281         expect { subject }.to raise_error(/already ensured as absent/)
282       end
283     end
284   end
285 end