blob: 9049886c45b911c94cf36329b0e162f14004ca7f [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
David Patersone45aa842015-11-18 16:12:27 -080018import httplib2
Matthew Treinishf0971712014-04-11 20:08:53 +000019import os
Matthew Treinish1f7b33d2013-10-21 18:07:02 +000020import sys
David Patersone45aa842015-11-18 16:12:27 -080021import traceback
Matthew Treinish1f7b33d2013-10-21 18:07:02 +000022
David Patersone45aa842015-11-18 16:12:27 -080023from cliff import command
24from oslo_log import log as logging
Matthew Treinish21905512015-07-13 10:33:35 -040025from oslo_serialization import jsonutils as json
Matthew Treinishc795b9e2014-06-09 17:01:10 -040026from six import moves
Matthew Treinishf077dd22015-04-23 09:37:41 -040027from six.moves.urllib import parse as urlparse
Matthew Treinish4f30eb82014-01-07 21:04:49 +000028
Matthew Treinish1f7b33d2013-10-21 18:07:02 +000029from tempest import clients
Andrea Frittoli (andreaf)290b3e12015-10-08 10:25:02 +010030from tempest.common import credentials_factory as credentials
Matthew Treinish1f7b33d2013-10-21 18:07:02 +000031from tempest import config
32
33
Sean Dague86bd8422013-12-20 09:56:44 -050034CONF = config.CONF
David Kranzf20ac322014-05-02 16:46:15 -040035CONF_PARSER = None
Matthew Treinish1f7b33d2013-10-21 18:07:02 +000036
David Patersone45aa842015-11-18 16:12:27 -080037LOG = logging.getLogger(__name__)
38
Matthew Treinish1f7b33d2013-10-21 18:07:02 +000039
Matthew Treinishf0971712014-04-11 20:08:53 +000040def _get_config_file():
41 default_config_dir = os.path.join(os.path.abspath(
David Kranzf20ac322014-05-02 16:46:15 -040042 os.path.dirname(os.path.dirname(os.path.dirname(__file__)))), "etc")
Matthew Treinishf0971712014-04-11 20:08:53 +000043 default_config_file = "tempest.conf"
44
45 conf_dir = os.environ.get('TEMPEST_CONFIG_DIR', default_config_dir)
46 conf_file = os.environ.get('TEMPEST_CONFIG', default_config_file)
47 path = os.path.join(conf_dir, conf_file)
48 fd = open(path, 'rw')
49 return fd
50
51
52def change_option(option, group, value):
David Kranzf20ac322014-05-02 16:46:15 -040053 if not CONF_PARSER.has_section(group):
54 CONF_PARSER.add_section(group)
55 CONF_PARSER.set(group, option, str(value))
Matthew Treinishf0971712014-04-11 20:08:53 +000056
57
58def print_and_or_update(option, group, value, update):
59 print('Config option %s in group %s should be changed to: %s'
60 % (option, group, value))
61 if update:
62 change_option(option, group, value)
63
64
David Kranz0df154d2015-06-02 17:02:27 -040065def contains_version(prefix, versions):
66 return any([x for x in versions if x.startswith(prefix)])
67
68
Matthew Treinishf0971712014-04-11 20:08:53 +000069def verify_glance_api_versions(os, update):
Matthew Treinish99afd072013-10-22 18:03:06 +000070 # Check glance api versions
David Kranz0df154d2015-06-02 17:02:27 -040071 _, versions = os.image_client.get_versions()
72 if CONF.image_feature_enabled.api_v1 != contains_version('v1.', versions):
Nikita Gerasimov35fbdc12015-08-07 19:58:24 +030073 print_and_or_update('api_v1', 'image-feature-enabled',
Matthew Treinishf0971712014-04-11 20:08:53 +000074 not CONF.image_feature_enabled.api_v1, update)
David Kranz0df154d2015-06-02 17:02:27 -040075 if CONF.image_feature_enabled.api_v2 != contains_version('v2.', versions):
Nikita Gerasimov35fbdc12015-08-07 19:58:24 +030076 print_and_or_update('api_v2', 'image-feature-enabled',
Matthew Treinishf0971712014-04-11 20:08:53 +000077 not CONF.image_feature_enabled.api_v2, update)
Matthew Treinish99afd072013-10-22 18:03:06 +000078
79
Matthew Treinish9b896242014-04-23 21:25:27 +000080def _get_unversioned_endpoint(base_url):
81 endpoint_parts = urlparse.urlparse(base_url)
82 endpoint = endpoint_parts.scheme + '://' + endpoint_parts.netloc
83 return endpoint
84
85
Matthew Treinish864fe072014-03-02 03:47:26 +000086def _get_api_versions(os, service):
87 client_dict = {
88 'nova': os.servers_client,
89 'keystone': os.identity_client,
Matthew Treinish2e439632014-03-05 21:53:33 +000090 'cinder': os.volumes_client,
Matthew Treinish864fe072014-03-02 03:47:26 +000091 }
92 client_dict[service].skip_path()
Matthew Treinish9b896242014-04-23 21:25:27 +000093 endpoint = _get_unversioned_endpoint(client_dict[service].base_url)
Matthew Treinishe1f32cd2015-02-17 15:06:13 -050094 dscv = CONF.identity.disable_ssl_certificate_validation
95 ca_certs = CONF.identity.ca_certificates_file
96 raw_http = httplib2.Http(disable_ssl_certificate_validation=dscv,
97 ca_certs=ca_certs)
98 __, body = raw_http.request(endpoint, 'GET')
Matthew Treinish864fe072014-03-02 03:47:26 +000099 client_dict[service].reset_path()
Matthew Treinish4f30eb82014-01-07 21:04:49 +0000100 body = json.loads(body)
Matthew Treinish864fe072014-03-02 03:47:26 +0000101 if service == 'keystone':
102 versions = map(lambda x: x['id'], body['versions']['values'])
103 else:
104 versions = map(lambda x: x['id'], body['versions'])
Matthew Treinish09487242015-05-10 12:43:58 -0400105 return list(versions)
Matthew Treinish864fe072014-03-02 03:47:26 +0000106
107
Matthew Treinishf0971712014-04-11 20:08:53 +0000108def verify_keystone_api_versions(os, update):
Matthew Treinish864fe072014-03-02 03:47:26 +0000109 # Check keystone api versions
110 versions = _get_api_versions(os, 'keystone')
David Kranz0df154d2015-06-02 17:02:27 -0400111 if (CONF.identity_feature_enabled.api_v2 !=
112 contains_version('v2.', versions)):
Nikita Gerasimov35fbdc12015-08-07 19:58:24 +0300113 print_and_or_update('api_v2', 'identity-feature-enabled',
Matthew Treinishf0971712014-04-11 20:08:53 +0000114 not CONF.identity_feature_enabled.api_v2, update)
David Kranz0df154d2015-06-02 17:02:27 -0400115 if (CONF.identity_feature_enabled.api_v3 !=
116 contains_version('v3.', versions)):
Nikita Gerasimov35fbdc12015-08-07 19:58:24 +0300117 print_and_or_update('api_v3', 'identity-feature-enabled',
Matthew Treinishf0971712014-04-11 20:08:53 +0000118 not CONF.identity_feature_enabled.api_v3, update)
Matthew Treinish864fe072014-03-02 03:47:26 +0000119
120
Matthew Treinishf0971712014-04-11 20:08:53 +0000121def verify_cinder_api_versions(os, update):
Matthew Treinish2e439632014-03-05 21:53:33 +0000122 # Check cinder api versions
123 versions = _get_api_versions(os, 'cinder')
David Kranz0df154d2015-06-02 17:02:27 -0400124 if (CONF.volume_feature_enabled.api_v1 !=
125 contains_version('v1.', versions)):
Nikita Gerasimov35fbdc12015-08-07 19:58:24 +0300126 print_and_or_update('api_v1', 'volume-feature-enabled',
Matthew Treinishf0971712014-04-11 20:08:53 +0000127 not CONF.volume_feature_enabled.api_v1, update)
David Kranz0df154d2015-06-02 17:02:27 -0400128 if (CONF.volume_feature_enabled.api_v2 !=
129 contains_version('v2.', versions)):
Nikita Gerasimov35fbdc12015-08-07 19:58:24 +0300130 print_and_or_update('api_v2', 'volume-feature-enabled',
Matthew Treinishf0971712014-04-11 20:08:53 +0000131 not CONF.volume_feature_enabled.api_v2, update)
Matthew Treinish2e439632014-03-05 21:53:33 +0000132
133
Adam Gandelman03af5562014-10-07 12:22:48 -0700134def verify_api_versions(os, service, update):
135 verify = {
136 'cinder': verify_cinder_api_versions,
137 'glance': verify_glance_api_versions,
138 'keystone': verify_keystone_api_versions,
Adam Gandelman03af5562014-10-07 12:22:48 -0700139 }
140 if service not in verify:
141 return
142 verify[service](os, update)
143
144
Matthew Treinish8b006d22014-01-07 15:37:20 +0000145def get_extension_client(os, service):
146 extensions_client = {
147 'nova': os.extensions_client,
Matthew Treinish8b006d22014-01-07 15:37:20 +0000148 'cinder': os.volumes_extension_client,
Ken'ichi Ohmichi52bb8122016-01-26 01:43:06 +0000149 'neutron': os.network_extensions_client,
Matthew Treinishc0120ba2014-01-31 20:10:19 +0000150 'swift': os.account_client,
Matthew Treinish8b006d22014-01-07 15:37:20 +0000151 }
Ivan Kolodyazhnybcfc32e2015-08-06 13:31:36 +0300152 # NOTE (e0ne): Use Cinder API v2 by default because v1 is deprecated
153 if CONF.volume_feature_enabled.api_v2:
154 extensions_client['cinder'] = os.volumes_v2_extension_client
155 else:
156 extensions_client['cinder'] = os.volumes_extension_client
157
Matthew Treinish8b006d22014-01-07 15:37:20 +0000158 if service not in extensions_client:
159 print('No tempest extensions client for %s' % service)
caoyueab333022016-01-25 16:45:21 +0800160 sys.exit(1)
Matthew Treinish8b006d22014-01-07 15:37:20 +0000161 return extensions_client[service]
162
163
164def get_enabled_extensions(service):
165 extensions_options = {
166 'nova': CONF.compute_feature_enabled.api_extensions,
Matthew Treinish8b006d22014-01-07 15:37:20 +0000167 'cinder': CONF.volume_feature_enabled.api_extensions,
Matthew Treinish8c6706d2014-01-07 19:28:18 +0000168 'neutron': CONF.network_feature_enabled.api_extensions,
Matthew Treinishc0120ba2014-01-31 20:10:19 +0000169 'swift': CONF.object_storage_feature_enabled.discoverable_apis,
Matthew Treinish8b006d22014-01-07 15:37:20 +0000170 }
171 if service not in extensions_options:
172 print('No supported extensions list option for %s' % service)
caoyueab333022016-01-25 16:45:21 +0800173 sys.exit(1)
Matthew Treinish8b006d22014-01-07 15:37:20 +0000174 return extensions_options[service]
175
176
177def verify_extensions(os, service, results):
178 extensions_client = get_extension_client(os, service)
David Kranz5cf4ba42015-02-10 14:00:50 -0500179 if service != 'swift':
David Kranz34e88122014-12-11 15:24:05 -0500180 resp = extensions_client.list_extensions()
181 else:
182 __, resp = extensions_client.list_extensions()
Matthew Treinish54176ce2014-12-08 21:28:05 +0000183 # For Nova, Cinder and Neutron we use the alias name rather than the
184 # 'name' field because the alias is considered to be the canonical
185 # name.
Matthew Treinish8b006d22014-01-07 15:37:20 +0000186 if isinstance(resp, dict):
Matthew Treinish54176ce2014-12-08 21:28:05 +0000187 if service == 'swift':
Matthew Treinishc0120ba2014-01-31 20:10:19 +0000188 # Remove Swift general information from extensions list
189 resp.pop('swift')
190 extensions = resp.keys()
Matthew Treinish8c6706d2014-01-07 19:28:18 +0000191 else:
Matthew Treinish54176ce2014-12-08 21:28:05 +0000192 extensions = map(lambda x: x['alias'], resp['extensions'])
Matthew Treinish8c6706d2014-01-07 19:28:18 +0000193
Matthew Treinish8b006d22014-01-07 15:37:20 +0000194 else:
Matthew Treinish54176ce2014-12-08 21:28:05 +0000195 extensions = map(lambda x: x['alias'], resp)
Matthew Treinish09487242015-05-10 12:43:58 -0400196 extensions = list(extensions)
Matthew Treinish8b006d22014-01-07 15:37:20 +0000197 if not results.get(service):
198 results[service] = {}
199 extensions_opt = get_enabled_extensions(service)
200 if extensions_opt[0] == 'all':
Matthew Treinishf0971712014-04-11 20:08:53 +0000201 results[service]['extensions'] = extensions
Matthew Treinish8b006d22014-01-07 15:37:20 +0000202 return results
203 # Verify that all configured extensions are actually enabled
204 for extension in extensions_opt:
205 results[service][extension] = extension in extensions
206 # Verify that there aren't additional extensions enabled that aren't
207 # specified in the config list
208 for extension in extensions:
209 if extension not in extensions_opt:
210 results[service][extension] = False
Matthew Treinish1f7b33d2013-10-21 18:07:02 +0000211 return results
212
213
Matthew Treinishf0971712014-04-11 20:08:53 +0000214def display_results(results, update, replace):
215 update_dict = {
216 'swift': 'object-storage-feature-enabled',
217 'nova': 'compute-feature-enabled',
Matthew Treinishf0971712014-04-11 20:08:53 +0000218 'cinder': 'volume-feature-enabled',
219 'neutron': 'network-feature-enabled',
220 }
Matthew Treinish8b006d22014-01-07 15:37:20 +0000221 for service in results:
222 # If all extensions are specified as being enabled there is no way to
223 # verify this so we just assume this to be true
224 if results[service].get('extensions'):
Matthew Treinishf0971712014-04-11 20:08:53 +0000225 if replace:
226 output_list = results[service].get('extensions')
227 else:
228 output_list = ['all']
229 else:
230 extension_list = get_enabled_extensions(service)
231 output_list = []
232 for extension in results[service]:
233 if not results[service][extension]:
234 if extension in extension_list:
235 print("%s extension: %s should not be included in the "
236 "list of enabled extensions" % (service,
237 extension))
238 else:
239 print("%s extension: %s should be included in the list"
240 " of enabled extensions" % (service, extension))
241 output_list.append(extension)
Matthew Treinish8b006d22014-01-07 15:37:20 +0000242 else:
Matthew Treinishf0971712014-04-11 20:08:53 +0000243 output_list.append(extension)
244 if update:
245 # Sort List
246 output_list.sort()
247 # Convert list to a string
248 output_string = ', '.join(output_list)
249 if service == 'swift':
250 change_option('discoverable_apis', update_dict[service],
251 output_string)
Matthew Treinishf0971712014-04-11 20:08:53 +0000252 else:
253 change_option('api_extensions', update_dict[service],
254 output_string)
Matthew Treinish1f7b33d2013-10-21 18:07:02 +0000255
256
Matthew Treinishf0971712014-04-11 20:08:53 +0000257def check_service_availability(os, update):
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000258 services = []
259 avail_services = []
260 codename_match = {
261 'volume': 'cinder',
262 'network': 'neutron',
263 'image': 'glance',
264 'object_storage': 'swift',
265 'compute': 'nova',
266 'orchestration': 'heat',
267 'metering': 'ceilometer',
268 'telemetry': 'ceilometer',
Matthew Treinish42d50f62014-04-11 19:47:13 +0000269 'data_processing': 'sahara',
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000270 'baremetal': 'ironic',
Matthew Treinish42d50f62014-04-11 19:47:13 +0000271 'identity': 'keystone',
Matthew Treinish42d50f62014-04-11 19:47:13 +0000272 'database': 'trove'
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000273 }
274 # Get catalog list for endpoints to use for validation
David Kranz45714082015-04-01 14:47:33 -0400275 _token, auth_data = os.auth_provider.get_auth()
David Kranz799eee12015-04-08 11:18:19 -0400276 if os.auth_version == 'v2':
277 catalog_key = 'serviceCatalog'
278 else:
279 catalog_key = 'catalog'
280 for entry in auth_data[catalog_key]:
David Kranz45714082015-04-01 14:47:33 -0400281 services.append(entry['type'])
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000282 # Pull all catalog types from config file and compare against endpoint list
283 for cfgname in dir(CONF._config):
284 cfg = getattr(CONF, cfgname)
285 catalog_type = getattr(cfg, 'catalog_type', None)
286 if not catalog_type:
287 continue
288 else:
289 if cfgname == 'identity':
290 # Keystone is a required service for tempest
291 continue
292 if catalog_type not in services:
293 if getattr(CONF.service_available, codename_match[cfgname]):
294 print('Endpoint type %s not found either disable service '
295 '%s or fix the catalog_type in the config file' % (
Matthew Treinish96e9e882014-06-09 18:37:19 -0400296 catalog_type, codename_match[cfgname]))
Matthew Treinishf0971712014-04-11 20:08:53 +0000297 if update:
298 change_option(codename_match[cfgname],
299 'service_available', False)
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000300 else:
301 if not getattr(CONF.service_available,
302 codename_match[cfgname]):
303 print('Endpoint type %s is available, service %s should be'
304 ' set as available in the config file.' % (
Matthew Treinish96e9e882014-06-09 18:37:19 -0400305 catalog_type, codename_match[cfgname]))
Matthew Treinishf0971712014-04-11 20:08:53 +0000306 if update:
307 change_option(codename_match[cfgname],
308 'service_available', True)
David Kranzf20ac322014-05-02 16:46:15 -0400309 # If we are going to enable this we should allow
310 # extension checks.
311 avail_services.append(codename_match[cfgname])
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000312 else:
313 avail_services.append(codename_match[cfgname])
314 return avail_services
Matthew Treinishd44fe032014-01-31 20:07:24 +0000315
316
David Patersone45aa842015-11-18 16:12:27 -0800317def _parser_add_args(parser):
Matthew Treinishf0971712014-04-11 20:08:53 +0000318 parser.add_argument('-u', '--update', action='store_true',
319 help='Update the config file with results from api '
320 'queries. This assumes whatever is set in the '
321 'config file is incorrect. In the case of '
322 'endpoint checks where it could either be the '
323 'incorrect catalog type or the service available '
324 'option the service available option is assumed '
325 'to be incorrect and is thus changed')
326 parser.add_argument('-o', '--output',
327 help="Output file to write an updated config file to. "
328 "This has to be a separate file from the "
329 "original config file. If one isn't specified "
330 "with -u the new config file will be printed to "
331 "STDOUT")
332 parser.add_argument('-r', '--replace-ext', action='store_true',
333 help="If specified the all option will be replaced "
334 "with a full list of extensions")
Matthew Treinishf0971712014-04-11 20:08:53 +0000335
336
David Patersone45aa842015-11-18 16:12:27 -0800337def parse_args():
338 parser = argparse.ArgumentParser()
339 _parser_add_args(parser)
340 opts = parser.parse_args()
341 return opts
342
343
344def main(opts=None):
Matthew Treinish8b006d22014-01-07 15:37:20 +0000345 print('Running config verification...')
David Patersone45aa842015-11-18 16:12:27 -0800346 if opts is None:
347 print("Use of: 'verify-tempest-config' is deprecated, "
348 "please use: 'tempest verify-config'")
349 opts = parse_args()
Matthew Treinishf0971712014-04-11 20:08:53 +0000350 update = opts.update
351 replace = opts.replace_ext
David Kranzf20ac322014-05-02 16:46:15 -0400352 global CONF_PARSER
353
354 outfile = sys.stdout
Matthew Treinishf0971712014-04-11 20:08:53 +0000355 if update:
David Kranzf20ac322014-05-02 16:46:15 -0400356 conf_file = _get_config_file()
David Kranzf20ac322014-05-02 16:46:15 -0400357 CONF_PARSER = moves.configparser.SafeConfigParser()
358 CONF_PARSER.optionxform = str
359 CONF_PARSER.readfp(conf_file)
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700360 icreds = credentials.get_credentials_provider('verify_tempest_config')
David Kranz5fcac942015-05-08 17:43:45 -0400361 try:
362 os = clients.Manager(icreds.get_primary_creds())
363 services = check_service_availability(os, update)
364 results = {}
365 for service in ['nova', 'cinder', 'neutron', 'swift']:
366 if service not in services:
367 continue
368 results = verify_extensions(os, service, results)
Adam Gandelman03af5562014-10-07 12:22:48 -0700369
David Kranz5fcac942015-05-08 17:43:45 -0400370 # Verify API versions of all services in the keystone catalog and
371 # keystone itself.
372 services.append('keystone')
373 for service in services:
374 verify_api_versions(os, service, update)
Adam Gandelman03af5562014-10-07 12:22:48 -0700375
David Kranz5fcac942015-05-08 17:43:45 -0400376 display_results(results, update, replace)
377 if update:
378 conf_file.close()
zhang.leia4b1cef2016-03-01 10:50:01 +0800379 if opts.output:
380 with open(opts.output, 'w+') as outfile:
381 CONF_PARSER.write(outfile)
David Kranz5fcac942015-05-08 17:43:45 -0400382 finally:
Andrea Frittoli (andreaf)17209bb2015-05-22 10:16:57 -0700383 icreds.clear_creds()
Matthew Treinish1f7b33d2013-10-21 18:07:02 +0000384
385
David Patersone45aa842015-11-18 16:12:27 -0800386class TempestVerifyConfig(command.Command):
387 """Verify your current tempest configuration"""
388
389 def get_parser(self, prog_name):
390 parser = super(TempestVerifyConfig, self).get_parser(prog_name)
391 _parser_add_args(parser)
392 return parser
393
394 def take_action(self, parsed_args):
395 try:
396 return main(parsed_args)
397 except Exception:
398 LOG.exception("Failure verifying configuration.")
399 traceback.print_exc()
400 raise
401 return 0
402
Matthew Treinish1f7b33d2013-10-21 18:07:02 +0000403if __name__ == "__main__":
Matthew Treinishf8b816a2014-04-23 20:35:49 +0000404 main()