blob: 14dd0c280aa4799aa2c7fcf160fcabfa3c49bef4 [file] [log] [blame]
Matthew Treinish1f7b33d2013-10-21 18:07:02 +00001#!/usr/bin/env python
Matthew Treinish1f7b33d2013-10-21 18:07:02 +00002
3# Copyright 2013 IBM Corp.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
Matthew Treinishf0971712014-04-11 20:08:53 +000017import argparse
Matthew Treinish4f30eb82014-01-07 21:04:49 +000018import json
Matthew Treinishf0971712014-04-11 20:08:53 +000019import os
Matthew Treinish1f7b33d2013-10-21 18:07:02 +000020import sys
Matthew Treinish864fe072014-03-02 03:47:26 +000021import urlparse
Matthew Treinish1f7b33d2013-10-21 18:07:02 +000022
Matthew Treinish4f30eb82014-01-07 21:04:49 +000023import httplib2
Matthew Treinishf0971712014-04-11 20:08:53 +000024from six.moves import configparser
Matthew Treinish4f30eb82014-01-07 21:04:49 +000025
Matthew Treinish1f7b33d2013-10-21 18:07:02 +000026from tempest import clients
27from tempest import config
28
29
Sean Dague86bd8422013-12-20 09:56:44 -050030CONF = config.CONF
Matthew Treinish4f30eb82014-01-07 21:04:49 +000031RAW_HTTP = httplib2.Http()
Matthew Treinishf0971712014-04-11 20:08:53 +000032CONF_FILE = None
33OUTFILE = sys.stdout
Matthew Treinish1f7b33d2013-10-21 18:07:02 +000034
Matthew Treinish1f7b33d2013-10-21 18:07:02 +000035
Matthew Treinishf0971712014-04-11 20:08:53 +000036def _get_config_file():
37 default_config_dir = os.path.join(os.path.abspath(
38 os.path.dirname(os.path.dirname(__file__))), "etc")
39 default_config_file = "tempest.conf"
40
41 conf_dir = os.environ.get('TEMPEST_CONFIG_DIR', default_config_dir)
42 conf_file = os.environ.get('TEMPEST_CONFIG', default_config_file)
43 path = os.path.join(conf_dir, conf_file)
44 fd = open(path, 'rw')
45 return fd
46
47
48def change_option(option, group, value):
49 config_parse = configparser.SafeConfigParser()
50 config_parse.optionxform = str
51 config_parse.readfp(CONF_FILE)
52 if not config_parse.has_section(group):
53 config_parse.add_section(group)
54 config_parse.set(group, option, str(value))
55 global OUTFILE
56 config_parse.write(OUTFILE)
57
58
59def print_and_or_update(option, group, value, update):
60 print('Config option %s in group %s should be changed to: %s'
61 % (option, group, value))
62 if update:
63 change_option(option, group, value)
64
65
66def verify_glance_api_versions(os, update):
Matthew Treinish99afd072013-10-22 18:03:06 +000067 # Check glance api versions
68 __, versions = os.image_client.get_versions()
69 if CONF.image_feature_enabled.api_v1 != ('v1.1' in versions or 'v1.0' in
70 versions):
Matthew Treinishf0971712014-04-11 20:08:53 +000071 print_and_or_update('api_v1', 'image_feature_enabled',
72 not CONF.image_feature_enabled.api_v1, update)
Matthew Treinish99afd072013-10-22 18:03:06 +000073 if CONF.image_feature_enabled.api_v2 != ('v2.0' in versions):
Matthew Treinishf0971712014-04-11 20:08:53 +000074 print_and_or_update('api_v2', 'image_feature_enabled',
75 not CONF.image_feature_enabled.api_v2, update)
Matthew Treinish99afd072013-10-22 18:03:06 +000076
77
Matthew Treinish864fe072014-03-02 03:47:26 +000078def _get_api_versions(os, service):
79 client_dict = {
80 'nova': os.servers_client,
81 'keystone': os.identity_client,
Matthew Treinish2e439632014-03-05 21:53:33 +000082 'cinder': os.volumes_client,
Matthew Treinish864fe072014-03-02 03:47:26 +000083 }
84 client_dict[service].skip_path()
Matthew Treinish1722f0b2014-04-11 20:09:17 +000085 endpoint_parts = urlparse.urlparse(client_dict[service].base_url)
86 endpoint = endpoint_parts.scheme + '://' + endpoint_parts.netloc
Matthew Treinisha5080812014-02-11 15:49:04 +000087 __, body = RAW_HTTP.request(endpoint, 'GET')
Matthew Treinish864fe072014-03-02 03:47:26 +000088 client_dict[service].reset_path()
Matthew Treinish4f30eb82014-01-07 21:04:49 +000089 body = json.loads(body)
Matthew Treinish864fe072014-03-02 03:47:26 +000090 if service == 'keystone':
91 versions = map(lambda x: x['id'], body['versions']['values'])
92 else:
93 versions = map(lambda x: x['id'], body['versions'])
94 return versions
95
96
Matthew Treinishf0971712014-04-11 20:08:53 +000097def verify_keystone_api_versions(os, update):
Matthew Treinish864fe072014-03-02 03:47:26 +000098 # Check keystone api versions
99 versions = _get_api_versions(os, 'keystone')
100 if CONF.identity_feature_enabled.api_v2 != ('v2.0' in versions):
Matthew Treinishf0971712014-04-11 20:08:53 +0000101 print_and_or_update('api_v2', 'identity_feature_enabled',
102 not CONF.identity_feature_enabled.api_v2, update)
Matthew Treinish864fe072014-03-02 03:47:26 +0000103 if CONF.identity_feature_enabled.api_v3 != ('v3.0' in versions):
Matthew Treinishf0971712014-04-11 20:08:53 +0000104 print_and_or_update('api_v3', 'identity_feature_enabled',
105 not CONF.identity_feature_enabled.api_v3, update)
Matthew Treinish864fe072014-03-02 03:47:26 +0000106
107
Matthew Treinishf0971712014-04-11 20:08:53 +0000108def verify_nova_api_versions(os, update):
Matthew Treinish864fe072014-03-02 03:47:26 +0000109 versions = _get_api_versions(os, 'nova')
Matthew Treinish4f30eb82014-01-07 21:04:49 +0000110 if CONF.compute_feature_enabled.api_v3 != ('v3.0' in versions):
Matthew Treinishf0971712014-04-11 20:08:53 +0000111 print_and_or_update('api_v3', 'compute_feature_enabled',
112 not CONF.compute_feature_enabled.api_v3, update)
Matthew Treinish4f30eb82014-01-07 21:04:49 +0000113
114
Matthew Treinishf0971712014-04-11 20:08:53 +0000115def verify_cinder_api_versions(os, update):
Matthew Treinish2e439632014-03-05 21:53:33 +0000116 # Check cinder api versions
117 versions = _get_api_versions(os, 'cinder')
118 if CONF.volume_feature_enabled.api_v1 != ('v1.0' in versions):
Matthew Treinishf0971712014-04-11 20:08:53 +0000119 print_and_or_update('api_v1', 'volume_feature_enabled',
120 not CONF.volume_feature_enabled.api_v1, update)
Matthew Treinish2e439632014-03-05 21:53:33 +0000121 if CONF.volume_feature_enabled.api_v2 != ('v2.0' in versions):
Matthew Treinishf0971712014-04-11 20:08:53 +0000122 print_and_or_update('api_v2', 'volume_feature_enabled',
123 not CONF.volume_feature_enabled.api_v2, update)
Matthew Treinish2e439632014-03-05 21:53:33 +0000124
125
Matthew Treinish8b006d22014-01-07 15:37:20 +0000126def get_extension_client(os, service):
127 extensions_client = {
128 'nova': os.extensions_client,
129 'nova_v3': os.extensions_v3_client,
130 'cinder': os.volumes_extension_client,
Matthew Treinish8c6706d2014-01-07 19:28:18 +0000131 'neutron': os.network_client,
Matthew Treinishc0120ba2014-01-31 20:10:19 +0000132 'swift': os.account_client,
Matthew Treinish8b006d22014-01-07 15:37:20 +0000133 }
134 if service not in extensions_client:
135 print('No tempest extensions client for %s' % service)
136 exit(1)
137 return extensions_client[service]
138
139
140def get_enabled_extensions(service):
141 extensions_options = {
142 'nova': CONF.compute_feature_enabled.api_extensions,
143 'nova_v3': CONF.compute_feature_enabled.api_v3_extensions,
144 'cinder': CONF.volume_feature_enabled.api_extensions,
Matthew Treinish8c6706d2014-01-07 19:28:18 +0000145 'neutron': CONF.network_feature_enabled.api_extensions,
Matthew Treinishc0120ba2014-01-31 20:10:19 +0000146 'swift': CONF.object_storage_feature_enabled.discoverable_apis,
Matthew Treinish8b006d22014-01-07 15:37:20 +0000147 }
148 if service not in extensions_options:
149 print('No supported extensions list option for %s' % service)
150 exit(1)
151 return extensions_options[service]
152
153
154def verify_extensions(os, service, results):
155 extensions_client = get_extension_client(os, service)
Matthew Treinish1f7b33d2013-10-21 18:07:02 +0000156 __, resp = extensions_client.list_extensions()
Matthew Treinish8b006d22014-01-07 15:37:20 +0000157 if isinstance(resp, dict):
Matthew Treinish8c6706d2014-01-07 19:28:18 +0000158 # Neutron's extension 'name' field has is not a single word (it has
159 # spaces in the string) Since that can't be used for list option the
160 # api_extension option in the network-feature-enabled group uses alias
161 # instead of name.
162 if service == 'neutron':
163 extensions = map(lambda x: x['alias'], resp['extensions'])
Matthew Treinishc0120ba2014-01-31 20:10:19 +0000164 elif service == 'swift':
165 # Remove Swift general information from extensions list
166 resp.pop('swift')
167 extensions = resp.keys()
Matthew Treinish8c6706d2014-01-07 19:28:18 +0000168 else:
169 extensions = map(lambda x: x['name'], resp['extensions'])
170
Matthew Treinish8b006d22014-01-07 15:37:20 +0000171 else:
172 extensions = map(lambda x: x['name'], resp)
173 if not results.get(service):
174 results[service] = {}
175 extensions_opt = get_enabled_extensions(service)
176 if extensions_opt[0] == 'all':
Matthew Treinishf0971712014-04-11 20:08:53 +0000177 results[service]['extensions'] = extensions
Matthew Treinish8b006d22014-01-07 15:37:20 +0000178 return results
179 # Verify that all configured extensions are actually enabled
180 for extension in extensions_opt:
181 results[service][extension] = extension in extensions
182 # Verify that there aren't additional extensions enabled that aren't
183 # specified in the config list
184 for extension in extensions:
185 if extension not in extensions_opt:
186 results[service][extension] = False
Matthew Treinish1f7b33d2013-10-21 18:07:02 +0000187 return results
188
189
Matthew Treinishf0971712014-04-11 20:08:53 +0000190def display_results(results, update, replace):
191 update_dict = {
192 'swift': 'object-storage-feature-enabled',
193 'nova': 'compute-feature-enabled',
194 'nova_v3': 'compute-feature-enabled',
195 'cinder': 'volume-feature-enabled',
196 'neutron': 'network-feature-enabled',
197 }
Matthew Treinish8b006d22014-01-07 15:37:20 +0000198 for service in results:
199 # If all extensions are specified as being enabled there is no way to
200 # verify this so we just assume this to be true
201 if results[service].get('extensions'):
Matthew Treinishf0971712014-04-11 20:08:53 +0000202 if replace:
203 output_list = results[service].get('extensions')
204 else:
205 output_list = ['all']
206 else:
207 extension_list = get_enabled_extensions(service)
208 output_list = []
209 for extension in results[service]:
210 if not results[service][extension]:
211 if extension in extension_list:
212 print("%s extension: %s should not be included in the "
213 "list of enabled extensions" % (service,
214 extension))
215 else:
216 print("%s extension: %s should be included in the list"
217 " of enabled extensions" % (service, extension))
218 output_list.append(extension)
Matthew Treinish8b006d22014-01-07 15:37:20 +0000219 else:
Matthew Treinishf0971712014-04-11 20:08:53 +0000220 output_list.append(extension)
221 if update:
222 # Sort List
223 output_list.sort()
224 # Convert list to a string
225 output_string = ', '.join(output_list)
226 if service == 'swift':
227 change_option('discoverable_apis', update_dict[service],
228 output_string)
229 elif service == 'nova_v3':
230 change_option('api_v3_extensions', update_dict[service],
231 output_string)
232 else:
233 change_option('api_extensions', update_dict[service],
234 output_string)
Matthew Treinish1f7b33d2013-10-21 18:07:02 +0000235
236
Matthew Treinishf0971712014-04-11 20:08:53 +0000237def check_service_availability(os, update):
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000238 services = []
239 avail_services = []
240 codename_match = {
241 'volume': 'cinder',
242 'network': 'neutron',
243 'image': 'glance',
244 'object_storage': 'swift',
245 'compute': 'nova',
246 'orchestration': 'heat',
247 'metering': 'ceilometer',
248 'telemetry': 'ceilometer',
Matthew Treinish42d50f62014-04-11 19:47:13 +0000249 'data_processing': 'sahara',
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000250 'baremetal': 'ironic',
Matthew Treinish42d50f62014-04-11 19:47:13 +0000251 'identity': 'keystone',
252 'queuing': 'marconi',
253 'database': 'trove'
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000254 }
255 # Get catalog list for endpoints to use for validation
256 __, endpoints = os.endpoints_client.list_endpoints()
257 for endpoint in endpoints:
258 __, service = os.service_client.get_service(endpoint['service_id'])
259 services.append(service['type'])
260 # Pull all catalog types from config file and compare against endpoint list
261 for cfgname in dir(CONF._config):
262 cfg = getattr(CONF, cfgname)
263 catalog_type = getattr(cfg, 'catalog_type', None)
264 if not catalog_type:
265 continue
266 else:
267 if cfgname == 'identity':
268 # Keystone is a required service for tempest
269 continue
270 if catalog_type not in services:
271 if getattr(CONF.service_available, codename_match[cfgname]):
272 print('Endpoint type %s not found either disable service '
273 '%s or fix the catalog_type in the config file' % (
274 catalog_type, codename_match[cfgname]))
Matthew Treinishf0971712014-04-11 20:08:53 +0000275 if update:
276 change_option(codename_match[cfgname],
277 'service_available', False)
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000278 else:
279 if not getattr(CONF.service_available,
280 codename_match[cfgname]):
281 print('Endpoint type %s is available, service %s should be'
282 ' set as available in the config file.' % (
283 catalog_type, codename_match[cfgname]))
Matthew Treinishf0971712014-04-11 20:08:53 +0000284 if update:
285 change_option(codename_match[cfgname],
286 'service_available', True)
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000287 else:
288 avail_services.append(codename_match[cfgname])
289 return avail_services
Matthew Treinishd44fe032014-01-31 20:07:24 +0000290
291
Matthew Treinishf0971712014-04-11 20:08:53 +0000292def parse_args():
293 parser = argparse.ArgumentParser()
294 parser.add_argument('-u', '--update', action='store_true',
295 help='Update the config file with results from api '
296 'queries. This assumes whatever is set in the '
297 'config file is incorrect. In the case of '
298 'endpoint checks where it could either be the '
299 'incorrect catalog type or the service available '
300 'option the service available option is assumed '
301 'to be incorrect and is thus changed')
302 parser.add_argument('-o', '--output',
303 help="Output file to write an updated config file to. "
304 "This has to be a separate file from the "
305 "original config file. If one isn't specified "
306 "with -u the new config file will be printed to "
307 "STDOUT")
308 parser.add_argument('-r', '--replace-ext', action='store_true',
309 help="If specified the all option will be replaced "
310 "with a full list of extensions")
311 args = parser.parse_args()
312 return args
313
314
Matthew Treinish1f7b33d2013-10-21 18:07:02 +0000315def main(argv):
Matthew Treinish8b006d22014-01-07 15:37:20 +0000316 print('Running config verification...')
Matthew Treinishf0971712014-04-11 20:08:53 +0000317 opts = parse_args()
318 update = opts.update
319 replace = opts.replace_ext
320 global CONF_FILE
321 global OUTFILE
322 if update:
323 CONF_FILE = _get_config_file()
324 if opts.output:
325 OUTFILE = open(opts.output, 'w+')
Matthew Treinish1f7b33d2013-10-21 18:07:02 +0000326 os = clients.ComputeAdminManager(interface='json')
Matthew Treinishf0971712014-04-11 20:08:53 +0000327 services = check_service_availability(os, update)
Matthew Treinish8b006d22014-01-07 15:37:20 +0000328 results = {}
Matthew Treinishc0120ba2014-01-31 20:10:19 +0000329 for service in ['nova', 'nova_v3', 'cinder', 'neutron', 'swift']:
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000330 if service == 'nova_v3' and 'nova' not in services:
331 continue
332 elif service not in services:
Matthew Treinishd44fe032014-01-31 20:07:24 +0000333 continue
Matthew Treinish8b006d22014-01-07 15:37:20 +0000334 results = verify_extensions(os, service, results)
Matthew Treinishf0971712014-04-11 20:08:53 +0000335 verify_keystone_api_versions(os, update)
336 verify_glance_api_versions(os, update)
337 verify_nova_api_versions(os, update)
338 verify_cinder_api_versions(os, update)
339 display_results(results, update, replace)
340 if CONF_FILE:
341 CONF_FILE.close()
342 OUTFILE.close()
Matthew Treinish1f7b33d2013-10-21 18:07:02 +0000343
344
345if __name__ == "__main__":
346 main(sys.argv)