ffb265505ec2e377f8c7381932807ad141adda51
[mirror/dsa-puppet.git] / 3rdparty / modules / glance / spec / classes / glance_api_spec.rb
1 require 'spec_helper'
2
3 describe 'glance::api' do
4
5   let :facts do
6     {
7       :osfamily => 'Debian',
8       :processorcount => '7',
9     }
10   end
11
12   let :default_params do
13     {
14       :verbose                  => false,
15       :debug                    => false,
16       :bind_host                => '0.0.0.0',
17       :bind_port                => '9292',
18       :registry_host            => '0.0.0.0',
19       :registry_port            => '9191',
20       :registry_client_protocol => 'http',
21       :log_file                 => '/var/log/glance/api.log',
22       :log_dir                  => '/var/log/glance',
23       :auth_type                => 'keystone',
24       :enabled                  => true,
25       :manage_service           => true,
26       :backlog                  => '4096',
27       :workers                  => '7',
28       :auth_host                => '127.0.0.1',
29       :auth_port                => '35357',
30       :auth_protocol            => 'http',
31       :auth_uri                 => 'http://127.0.0.1:5000/',
32       :keystone_tenant          => 'services',
33       :keystone_user            => 'glance',
34       :keystone_password        => 'ChangeMe',
35       :database_idle_timeout    => '3600',
36       :database_connection      => 'sqlite:///var/lib/glance/glance.sqlite',
37       :show_image_direct_url    => false,
38       :purge_config             => false,
39       :known_stores             => false,
40       :image_cache_dir          => '/var/lib/glance/image-cache',
41       :os_region_name           => 'RegionOne',
42     }
43   end
44
45   [{:keystone_password => 'ChangeMe'},
46    {
47       :verbose                  => true,
48       :debug                    => true,
49       :bind_host                => '127.0.0.1',
50       :bind_port                => '9222',
51       :registry_host            => '127.0.0.1',
52       :registry_port            => '9111',
53       :registry_client_protocol => 'https',
54       :auth_type                => 'not_keystone',
55       :enabled                  => false,
56       :backlog                  => '4095',
57       :workers                  => '5',
58       :auth_host                => '127.0.0.2',
59       :auth_port                => '35358',
60       :auth_protocol            => 'https',
61       :auth_uri                 => 'https://127.0.0.2:5000/v2.0/',
62       :keystone_tenant          => 'admin2',
63       :keystone_user            => 'admin2',
64       :keystone_password        => 'ChangeMe2',
65       :database_idle_timeout    => '36002',
66       :database_connection      => 'mysql:///var:lib@glance/glance',
67       :show_image_direct_url    => true,
68       :image_cache_dir          => '/tmp/glance',
69       :os_region_name           => 'RegionOne2',
70     }
71   ].each do |param_set|
72
73     describe "when #{param_set == {:keystone_password => 'ChangeMe'} ? "using default" : "specifying"} class parameters" do
74
75       let :param_hash do
76         default_params.merge(param_set)
77       end
78
79       let :params do
80         param_set
81       end
82
83       it { should contain_class 'glance' }
84       it { should contain_class 'glance::policy' }
85
86       it { should contain_service('glance-api').with(
87         'ensure'     => (param_hash[:manage_service] && param_hash[:enabled]) ? 'running': 'stopped',
88         'enable'     => param_hash[:enabled],
89         'hasstatus'  => true,
90         'hasrestart' => true
91       ) }
92
93       it { should_not contain_exec('validate_nova_api') }
94
95       it 'should lay down default api config' do
96         [
97           'verbose',
98           'debug',
99           'bind_host',
100           'bind_port',
101           'registry_host',
102           'registry_port',
103           'registry_client_protocol',
104           'show_image_direct_url',
105           'os_region_name',
106         ].each do |config|
107           should contain_glance_api_config("DEFAULT/#{config}").with_value(param_hash[config.intern])
108         end
109       end
110
111       it 'should lay down default cache config' do
112         [
113           'verbose',
114           'debug',
115           'registry_host',
116           'registry_port',
117           'os_region_name',
118         ].each do |config|
119           should contain_glance_cache_config("DEFAULT/#{config}").with_value(param_hash[config.intern])
120         end
121       end
122
123       it 'should config db' do
124         should contain_glance_api_config('database/connection').with_value(param_hash[:database_connection])
125         should contain_glance_api_config('database/connection').with_value(param_hash[:database_connection]).with_secret(true)
126         should contain_glance_api_config('database/idle_timeout').with_value(param_hash[:database_idle_timeout])
127       end
128
129       it 'should have  no ssl options' do
130         should contain_glance_api_config('DEFAULT/ca_file').with_ensure('absent')
131         should contain_glance_api_config('DEFAULT/cert_file').with_ensure('absent')
132         should contain_glance_api_config('DEFAULT/key_file').with_ensure('absent')
133       end
134
135       it 'should lay down default auth config' do
136         [
137           'auth_host',
138           'auth_port',
139           'auth_protocol'
140         ].each do |config|
141           should contain_glance_api_config("keystone_authtoken/#{config}").with_value(param_hash[config.intern])
142         end
143       end
144       it { should contain_glance_api_config('keystone_authtoken/auth_admin_prefix').with_ensure('absent') }
145
146       it 'should configure itself for keystone if that is the auth_type' do
147         if params[:auth_type] == 'keystone'
148           should contain('paste_deploy/flavor').with_value('keystone+cachemanagement')
149
150           ['admin_tenant_name', 'admin_user', 'admin_password'].each do |config|
151             should contain_glance_api_config("keystone_authtoken/#{config}").with_value(param_hash[config.intern])
152           end
153           should contain_glance_api_config('keystone_authtoken/admin_password').with_value(param_hash[:keystone_password]).with_secret(true)
154
155           ['admin_tenant_name', 'admin_user', 'admin_password'].each do |config|
156             should contain_glance_cache_config("keystone_authtoken/#{config}").with_value(param_hash[config.intern])
157           end
158           should contain_glance_cache_config('keystone_authtoken/admin_password').with_value(param_hash[:keystone_password]).with_secret(true)
159         end
160       end
161     end
162
163   end
164
165   describe 'with disabled service managing' do
166     let :params do
167       {
168         :keystone_password => 'ChangeMe',
169         :manage_service => false,
170         :enabled        => false,
171       }
172     end
173
174     it { should contain_service('glance-api').with(
175         'ensure'     => nil,
176         'enable'     => false,
177         'hasstatus'  => true,
178         'hasrestart' => true
179       ) }
180   end
181
182   describe 'with overridden pipeline' do
183     let :params do
184       {
185         :keystone_password => 'ChangeMe',
186         :pipeline          => 'keystone',
187       }
188     end
189
190     it { should contain_glance_api_config('paste_deploy/flavor').with_value('keystone') }
191   end
192
193   describe 'with blank pipeline' do
194     let :params do
195       {
196         :keystone_password => 'ChangeMe',
197         :pipeline          => '',
198       }
199     end
200
201     it { should contain_glance_api_config('paste_deploy/flavor').with_ensure('absent') }
202   end
203
204   [
205     'keystone/',
206     'keystone+',
207     '+keystone',
208     'keystone+cachemanagement+',
209     '+'
210   ].each do |pipeline|
211     describe "with pipeline incorrect value #{pipeline}" do
212       let :params do
213         {
214           :keystone_password => 'ChangeMe',
215           :pipeline          => pipeline
216         }
217       end
218
219       it { expect { should contain_glance_api_config('filter:paste_deploy/flavor') }.to\
220         raise_error(Puppet::Error, /validate_re\(\): .* does not match/) }
221     end
222   end
223
224   describe 'with overriden auth_admin_prefix' do
225     let :params do
226       {
227         :keystone_password => 'ChangeMe',
228         :auth_admin_prefix => '/keystone/main'
229       }
230     end
231
232     it { should contain_glance_api_config('keystone_authtoken/auth_admin_prefix').with_value('/keystone/main') }
233   end
234
235   [
236     '/keystone/',
237     'keystone/',
238     'keystone',
239     '/keystone/admin/',
240     'keystone/admin/',
241     'keystone/admin'
242   ].each do |auth_admin_prefix|
243     describe "with auth_admin_prefix_containing incorrect value #{auth_admin_prefix}" do
244       let :params do
245         {
246           :keystone_password => 'ChangeMe',
247           :auth_admin_prefix => auth_admin_prefix
248         }
249       end
250
251       it { expect { should contain_glance_api_config('filter:authtoken/auth_admin_prefix') }.to\
252         raise_error(Puppet::Error, /validate_re\(\): "#{auth_admin_prefix}" does not match/) }
253     end
254   end
255
256   describe 'with syslog disabled by default' do
257     let :params do
258       default_params
259     end
260
261     it { should contain_glance_api_config('DEFAULT/use_syslog').with_value(false) }
262     it { should_not contain_glance_api_config('DEFAULT/syslog_log_facility') }
263   end
264
265   describe 'with syslog enabled' do
266     let :params do
267       default_params.merge({
268         :use_syslog   => 'true',
269       })
270     end
271
272     it { should contain_glance_api_config('DEFAULT/use_syslog').with_value(true) }
273     it { should contain_glance_api_config('DEFAULT/syslog_log_facility').with_value('LOG_USER') }
274   end
275
276   describe 'with syslog enabled and custom settings' do
277     let :params do
278       default_params.merge({
279         :use_syslog   => 'true',
280         :log_facility => 'LOG_LOCAL0'
281      })
282     end
283
284     it { should contain_glance_api_config('DEFAULT/use_syslog').with_value(true) }
285     it { should contain_glance_api_config('DEFAULT/syslog_log_facility').with_value('LOG_LOCAL0') }
286   end
287
288   describe 'with log_file enabled by default' do
289     let(:params) { default_params }
290
291     it { should contain_glance_api_config('DEFAULT/log_file').with_value(default_params[:log_file]) }
292
293     context 'with log_file disabled' do
294       let(:params) { default_params.merge!({ :log_file => false }) }
295       it { should contain_glance_api_config('DEFAULT/log_file').with_ensure('absent') }
296     end
297   end
298
299   describe 'with log_dir enabled by default' do
300     let(:params) { default_params }
301
302     it { should contain_glance_api_config('DEFAULT/log_dir').with_value(default_params[:log_dir]) }
303
304     context 'with log_dir disabled' do
305       let(:params) { default_params.merge!({ :log_dir => false }) }
306       it { should contain_glance_api_config('DEFAULT/log_dir').with_ensure('absent') }
307     end
308   end
309
310   describe 'with ssl options' do
311     let :params do
312       default_params.merge({
313         :ca_file     => '/tmp/ca_file',
314         :cert_file   => '/tmp/cert_file',
315         :key_file    => '/tmp/key_file'
316       })
317     end
318
319     context 'with ssl options' do
320       it { should contain_glance_api_config('DEFAULT/ca_file').with_value('/tmp/ca_file') }
321       it { should contain_glance_api_config('DEFAULT/cert_file').with_value('/tmp/cert_file') }
322       it { should contain_glance_api_config('DEFAULT/key_file').with_value('/tmp/key_file') }
323     end
324   end
325   describe 'with known_stores by default' do
326     let :params do
327       default_params
328     end
329
330     it { should_not contain_glance_api_config('glance_store/stores').with_value('false') }
331   end
332
333   describe 'with known_stores override' do
334     let :params do
335       default_params.merge({
336         :known_stores   => ['glance.store.filesystem.Store','glance.store.http.Store'],
337       })
338     end
339
340     it { should contain_glance_api_config('glance_store/stores').with_value("glance.store.filesystem.Store,glance.store.http.Store") }
341   end
342
343   describe 'with deprecated sql parameters' do
344     let :params do
345       default_params.merge({
346         :sql_connection   => 'mysql://user:pass@db/db',
347         :sql_idle_timeout => '30'
348       })
349     end
350
351     it 'configures database' do
352       should contain_glance_api_config('database/connection').with_value('mysql://user:pass@db/db')
353       should contain_glance_api_config('database/idle_timeout').with_value('30')
354     end
355   end
356
357   describe 'while validating the service with default command' do
358     let :params do
359       default_params.merge({
360         :validate => true,
361       })
362     end
363     it { should contain_exec('execute glance-api validation').with(
364       :path        => '/usr/bin:/bin:/usr/sbin:/sbin',
365       :provider    => 'shell',
366       :tries       => '10',
367       :try_sleep   => '2',
368       :command     => 'glance --os-auth-url http://localhost:5000/v2.0 --os-tenant-name services --os-username glance --os-password ChangeMe image-list',
369     )}
370
371     it { should contain_anchor('create glance-api anchor').with(
372       :require => 'Exec[execute glance-api validation]',
373     )}
374   end
375
376   describe 'while validating the service with custom command' do
377     let :params do
378       default_params.merge({
379         :validate            => true,
380         :validation_options  => { 'glance-api' => { 'command' => 'my-script' } }
381       })
382     end
383     it { should contain_exec('execute glance-api validation').with(
384       :path        => '/usr/bin:/bin:/usr/sbin:/sbin',
385       :provider    => 'shell',
386       :tries       => '10',
387       :try_sleep   => '2',
388       :command     => 'my-script',
389     )}
390
391     it { should contain_anchor('create glance-api anchor').with(
392       :require => 'Exec[execute glance-api validation]',
393     )}
394   end
395
396   describe 'on Debian platforms' do
397     let :facts do
398       { :osfamily => 'Debian' }
399     end
400     let(:params) { default_params }
401
402     it { should contain_package('glance-api').with(
403         :tag => ['openstack'],
404     )}
405   end
406
407   describe 'on RedHat platforms' do
408     let :facts do
409       { :osfamily => 'RedHat' }
410     end
411     let(:params) { default_params }
412
413     it { should contain_package('openstack-glance').with(
414         :tag => ['openstack'],
415     )}
416   end
417
418   describe 'on unknown platforms' do
419     let :facts do
420       { :osfamily => 'unknown' }
421     end
422     let(:params) { default_params }
423
424     it 'should fails to configure glance-api' do
425         expect { subject }.to raise_error(Puppet::Error, /module glance only support osfamily RedHat and Debian/)
426     end
427   end
428
429 end