blob: 2269d4159bb10d63c7ee829e9d7b4423f2640d38 [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
21
Matthew Treinish4f30eb82014-01-07 21:04:49 +000022import httplib2
Matthew Treinishc795b9e2014-06-09 17:01:10 -040023from six import moves
Matthew Treinishf077dd22015-04-23 09:37:41 -040024from six.moves.urllib import parse as urlparse
Matthew Treinish4f30eb82014-01-07 21:04:49 +000025
Matthew Treinish1f7b33d2013-10-21 18:07:02 +000026from tempest import clients
David Kranz799eee12015-04-08 11:18:19 -040027from tempest.common import credentials
Matthew Treinish1f7b33d2013-10-21 18:07:02 +000028from tempest import config
29
30
Sean Dague86bd8422013-12-20 09:56:44 -050031CONF = config.CONF
David Kranzf20ac322014-05-02 16:46:15 -040032CONF_PARSER = None
Matthew Treinish1f7b33d2013-10-21 18:07:02 +000033
Matthew Treinish1f7b33d2013-10-21 18:07:02 +000034
Matthew Treinishf0971712014-04-11 20:08:53 +000035def _get_config_file():
36 default_config_dir = os.path.join(os.path.abspath(
David Kranzf20ac322014-05-02 16:46:15 -040037 os.path.dirname(os.path.dirname(os.path.dirname(__file__)))), "etc")
Matthew Treinishf0971712014-04-11 20:08:53 +000038 default_config_file = "tempest.conf"
39
40 conf_dir = os.environ.get('TEMPEST_CONFIG_DIR', default_config_dir)
41 conf_file = os.environ.get('TEMPEST_CONFIG', default_config_file)
42 path = os.path.join(conf_dir, conf_file)
43 fd = open(path, 'rw')
44 return fd
45
46
47def change_option(option, group, value):
David Kranzf20ac322014-05-02 16:46:15 -040048 if not CONF_PARSER.has_section(group):
49 CONF_PARSER.add_section(group)
50 CONF_PARSER.set(group, option, str(value))
Matthew Treinishf0971712014-04-11 20:08:53 +000051
52
53def print_and_or_update(option, group, value, update):
54 print('Config option %s in group %s should be changed to: %s'
55 % (option, group, value))
56 if update:
57 change_option(option, group, value)
58
59
David Kranz0df154d2015-06-02 17:02:27 -040060def contains_version(prefix, versions):
61 return any([x for x in versions if x.startswith(prefix)])
62
63
Matthew Treinishf0971712014-04-11 20:08:53 +000064def verify_glance_api_versions(os, update):
Matthew Treinish99afd072013-10-22 18:03:06 +000065 # Check glance api versions
David Kranz0df154d2015-06-02 17:02:27 -040066 _, versions = os.image_client.get_versions()
67 if CONF.image_feature_enabled.api_v1 != contains_version('v1.', versions):
Matthew Treinishf0971712014-04-11 20:08:53 +000068 print_and_or_update('api_v1', 'image_feature_enabled',
69 not CONF.image_feature_enabled.api_v1, update)
David Kranz0df154d2015-06-02 17:02:27 -040070 if CONF.image_feature_enabled.api_v2 != contains_version('v2.', versions):
Matthew Treinishf0971712014-04-11 20:08:53 +000071 print_and_or_update('api_v2', 'image_feature_enabled',
72 not CONF.image_feature_enabled.api_v2, update)
Matthew Treinish99afd072013-10-22 18:03:06 +000073
74
Matthew Treinish9b896242014-04-23 21:25:27 +000075def _get_unversioned_endpoint(base_url):
76 endpoint_parts = urlparse.urlparse(base_url)
77 endpoint = endpoint_parts.scheme + '://' + endpoint_parts.netloc
78 return endpoint
79
80
Matthew Treinish864fe072014-03-02 03:47:26 +000081def _get_api_versions(os, service):
82 client_dict = {
83 'nova': os.servers_client,
84 'keystone': os.identity_client,
Matthew Treinish2e439632014-03-05 21:53:33 +000085 'cinder': os.volumes_client,
Matthew Treinish864fe072014-03-02 03:47:26 +000086 }
87 client_dict[service].skip_path()
Matthew Treinish9b896242014-04-23 21:25:27 +000088 endpoint = _get_unversioned_endpoint(client_dict[service].base_url)
Matthew Treinishe1f32cd2015-02-17 15:06:13 -050089 dscv = CONF.identity.disable_ssl_certificate_validation
90 ca_certs = CONF.identity.ca_certificates_file
91 raw_http = httplib2.Http(disable_ssl_certificate_validation=dscv,
92 ca_certs=ca_certs)
93 __, body = raw_http.request(endpoint, 'GET')
Matthew Treinish864fe072014-03-02 03:47:26 +000094 client_dict[service].reset_path()
Matthew Treinish4f30eb82014-01-07 21:04:49 +000095 body = json.loads(body)
Matthew Treinish864fe072014-03-02 03:47:26 +000096 if service == 'keystone':
97 versions = map(lambda x: x['id'], body['versions']['values'])
98 else:
99 versions = map(lambda x: x['id'], body['versions'])
Matthew Treinish09487242015-05-10 12:43:58 -0400100 return list(versions)
Matthew Treinish864fe072014-03-02 03:47:26 +0000101
102
Matthew Treinishf0971712014-04-11 20:08:53 +0000103def verify_keystone_api_versions(os, update):
Matthew Treinish864fe072014-03-02 03:47:26 +0000104 # Check keystone api versions
105 versions = _get_api_versions(os, 'keystone')
David Kranz0df154d2015-06-02 17:02:27 -0400106 if (CONF.identity_feature_enabled.api_v2 !=
107 contains_version('v2.', versions)):
Matthew Treinishf0971712014-04-11 20:08:53 +0000108 print_and_or_update('api_v2', 'identity_feature_enabled',
109 not CONF.identity_feature_enabled.api_v2, update)
David Kranz0df154d2015-06-02 17:02:27 -0400110 if (CONF.identity_feature_enabled.api_v3 !=
111 contains_version('v3.', versions)):
Matthew Treinishf0971712014-04-11 20:08:53 +0000112 print_and_or_update('api_v3', 'identity_feature_enabled',
113 not CONF.identity_feature_enabled.api_v3, update)
Matthew Treinish864fe072014-03-02 03:47:26 +0000114
115
Matthew Treinishf0971712014-04-11 20:08:53 +0000116def verify_cinder_api_versions(os, update):
Matthew Treinish2e439632014-03-05 21:53:33 +0000117 # Check cinder api versions
118 versions = _get_api_versions(os, 'cinder')
David Kranz0df154d2015-06-02 17:02:27 -0400119 if (CONF.volume_feature_enabled.api_v1 !=
120 contains_version('v1.', versions)):
Matthew Treinishf0971712014-04-11 20:08:53 +0000121 print_and_or_update('api_v1', 'volume_feature_enabled',
122 not CONF.volume_feature_enabled.api_v1, update)
David Kranz0df154d2015-06-02 17:02:27 -0400123 if (CONF.volume_feature_enabled.api_v2 !=
124 contains_version('v2.', versions)):
Matthew Treinishf0971712014-04-11 20:08:53 +0000125 print_and_or_update('api_v2', 'volume_feature_enabled',
126 not CONF.volume_feature_enabled.api_v2, update)
Matthew Treinish2e439632014-03-05 21:53:33 +0000127
128
Adam Gandelman03af5562014-10-07 12:22:48 -0700129def verify_api_versions(os, service, update):
130 verify = {
131 'cinder': verify_cinder_api_versions,
132 'glance': verify_glance_api_versions,
133 'keystone': verify_keystone_api_versions,
Adam Gandelman03af5562014-10-07 12:22:48 -0700134 }
135 if service not in verify:
136 return
137 verify[service](os, update)
138
139
Matthew Treinish8b006d22014-01-07 15:37:20 +0000140def get_extension_client(os, service):
141 extensions_client = {
142 'nova': os.extensions_client,
Matthew Treinish8b006d22014-01-07 15:37:20 +0000143 'cinder': os.volumes_extension_client,
Matthew Treinish8c6706d2014-01-07 19:28:18 +0000144 'neutron': os.network_client,
Matthew Treinishc0120ba2014-01-31 20:10:19 +0000145 'swift': os.account_client,
Matthew Treinish8b006d22014-01-07 15:37:20 +0000146 }
147 if service not in extensions_client:
148 print('No tempest extensions client for %s' % service)
149 exit(1)
150 return extensions_client[service]
151
152
153def get_enabled_extensions(service):
154 extensions_options = {
155 'nova': CONF.compute_feature_enabled.api_extensions,
Matthew Treinish8b006d22014-01-07 15:37:20 +0000156 'cinder': CONF.volume_feature_enabled.api_extensions,
Matthew Treinish8c6706d2014-01-07 19:28:18 +0000157 'neutron': CONF.network_feature_enabled.api_extensions,
Matthew Treinishc0120ba2014-01-31 20:10:19 +0000158 'swift': CONF.object_storage_feature_enabled.discoverable_apis,
Matthew Treinish8b006d22014-01-07 15:37:20 +0000159 }
160 if service not in extensions_options:
161 print('No supported extensions list option for %s' % service)
162 exit(1)
163 return extensions_options[service]
164
165
166def verify_extensions(os, service, results):
167 extensions_client = get_extension_client(os, service)
David Kranz5cf4ba42015-02-10 14:00:50 -0500168 if service != 'swift':
David Kranz34e88122014-12-11 15:24:05 -0500169 resp = extensions_client.list_extensions()
170 else:
171 __, resp = extensions_client.list_extensions()
Matthew Treinish54176ce2014-12-08 21:28:05 +0000172 # For Nova, Cinder and Neutron we use the alias name rather than the
173 # 'name' field because the alias is considered to be the canonical
174 # name.
Matthew Treinish8b006d22014-01-07 15:37:20 +0000175 if isinstance(resp, dict):
Matthew Treinish54176ce2014-12-08 21:28:05 +0000176 if service == 'swift':
Matthew Treinishc0120ba2014-01-31 20:10:19 +0000177 # Remove Swift general information from extensions list
178 resp.pop('swift')
179 extensions = resp.keys()
Matthew Treinish8c6706d2014-01-07 19:28:18 +0000180 else:
Matthew Treinish54176ce2014-12-08 21:28:05 +0000181 extensions = map(lambda x: x['alias'], resp['extensions'])
Matthew Treinish8c6706d2014-01-07 19:28:18 +0000182
Matthew Treinish8b006d22014-01-07 15:37:20 +0000183 else:
Matthew Treinish54176ce2014-12-08 21:28:05 +0000184 extensions = map(lambda x: x['alias'], resp)
Matthew Treinish09487242015-05-10 12:43:58 -0400185 extensions = list(extensions)
Matthew Treinish8b006d22014-01-07 15:37:20 +0000186 if not results.get(service):
187 results[service] = {}
188 extensions_opt = get_enabled_extensions(service)
189 if extensions_opt[0] == 'all':
Matthew Treinishf0971712014-04-11 20:08:53 +0000190 results[service]['extensions'] = extensions
Matthew Treinish8b006d22014-01-07 15:37:20 +0000191 return results
192 # Verify that all configured extensions are actually enabled
193 for extension in extensions_opt:
194 results[service][extension] = extension in extensions
195 # Verify that there aren't additional extensions enabled that aren't
196 # specified in the config list
197 for extension in extensions:
198 if extension not in extensions_opt:
199 results[service][extension] = False
Matthew Treinish1f7b33d2013-10-21 18:07:02 +0000200 return results
201
202
Matthew Treinishf0971712014-04-11 20:08:53 +0000203def display_results(results, update, replace):
204 update_dict = {
205 'swift': 'object-storage-feature-enabled',
206 'nova': 'compute-feature-enabled',
Matthew Treinishf0971712014-04-11 20:08:53 +0000207 'cinder': 'volume-feature-enabled',
208 'neutron': 'network-feature-enabled',
209 }
Matthew Treinish8b006d22014-01-07 15:37:20 +0000210 for service in results:
211 # If all extensions are specified as being enabled there is no way to
212 # verify this so we just assume this to be true
213 if results[service].get('extensions'):
Matthew Treinishf0971712014-04-11 20:08:53 +0000214 if replace:
215 output_list = results[service].get('extensions')
216 else:
217 output_list = ['all']
218 else:
219 extension_list = get_enabled_extensions(service)
220 output_list = []
221 for extension in results[service]:
222 if not results[service][extension]:
223 if extension in extension_list:
224 print("%s extension: %s should not be included in the "
225 "list of enabled extensions" % (service,
226 extension))
227 else:
228 print("%s extension: %s should be included in the list"
229 " of enabled extensions" % (service, extension))
230 output_list.append(extension)
Matthew Treinish8b006d22014-01-07 15:37:20 +0000231 else:
Matthew Treinishf0971712014-04-11 20:08:53 +0000232 output_list.append(extension)
233 if update:
234 # Sort List
235 output_list.sort()
236 # Convert list to a string
237 output_string = ', '.join(output_list)
238 if service == 'swift':
239 change_option('discoverable_apis', update_dict[service],
240 output_string)
Matthew Treinishf0971712014-04-11 20:08:53 +0000241 else:
242 change_option('api_extensions', update_dict[service],
243 output_string)
Matthew Treinish1f7b33d2013-10-21 18:07:02 +0000244
245
Matthew Treinishf0971712014-04-11 20:08:53 +0000246def check_service_availability(os, update):
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000247 services = []
248 avail_services = []
249 codename_match = {
250 'volume': 'cinder',
251 'network': 'neutron',
252 'image': 'glance',
253 'object_storage': 'swift',
254 'compute': 'nova',
255 'orchestration': 'heat',
256 'metering': 'ceilometer',
257 'telemetry': 'ceilometer',
Matthew Treinish42d50f62014-04-11 19:47:13 +0000258 'data_processing': 'sahara',
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000259 'baremetal': 'ironic',
Matthew Treinish42d50f62014-04-11 19:47:13 +0000260 'identity': 'keystone',
Victoria Martínez de la Cruz1173b6e2014-09-22 18:32:13 -0300261 'messaging': 'zaqar',
Matthew Treinish42d50f62014-04-11 19:47:13 +0000262 'database': 'trove'
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000263 }
264 # Get catalog list for endpoints to use for validation
David Kranz45714082015-04-01 14:47:33 -0400265 _token, auth_data = os.auth_provider.get_auth()
David Kranz799eee12015-04-08 11:18:19 -0400266 if os.auth_version == 'v2':
267 catalog_key = 'serviceCatalog'
268 else:
269 catalog_key = 'catalog'
270 for entry in auth_data[catalog_key]:
David Kranz45714082015-04-01 14:47:33 -0400271 services.append(entry['type'])
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000272 # Pull all catalog types from config file and compare against endpoint list
273 for cfgname in dir(CONF._config):
274 cfg = getattr(CONF, cfgname)
275 catalog_type = getattr(cfg, 'catalog_type', None)
276 if not catalog_type:
277 continue
278 else:
279 if cfgname == 'identity':
280 # Keystone is a required service for tempest
281 continue
282 if catalog_type not in services:
283 if getattr(CONF.service_available, codename_match[cfgname]):
284 print('Endpoint type %s not found either disable service '
285 '%s or fix the catalog_type in the config file' % (
Matthew Treinish96e9e882014-06-09 18:37:19 -0400286 catalog_type, codename_match[cfgname]))
Matthew Treinishf0971712014-04-11 20:08:53 +0000287 if update:
288 change_option(codename_match[cfgname],
289 'service_available', False)
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000290 else:
291 if not getattr(CONF.service_available,
292 codename_match[cfgname]):
293 print('Endpoint type %s is available, service %s should be'
294 ' set as available in the config file.' % (
Matthew Treinish96e9e882014-06-09 18:37:19 -0400295 catalog_type, codename_match[cfgname]))
Matthew Treinishf0971712014-04-11 20:08:53 +0000296 if update:
297 change_option(codename_match[cfgname],
298 'service_available', True)
David Kranzf20ac322014-05-02 16:46:15 -0400299 # If we are going to enable this we should allow
300 # extension checks.
301 avail_services.append(codename_match[cfgname])
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000302 else:
303 avail_services.append(codename_match[cfgname])
304 return avail_services
Matthew Treinishd44fe032014-01-31 20:07:24 +0000305
306
Matthew Treinishf0971712014-04-11 20:08:53 +0000307def parse_args():
308 parser = argparse.ArgumentParser()
309 parser.add_argument('-u', '--update', action='store_true',
310 help='Update the config file with results from api '
311 'queries. This assumes whatever is set in the '
312 'config file is incorrect. In the case of '
313 'endpoint checks where it could either be the '
314 'incorrect catalog type or the service available '
315 'option the service available option is assumed '
316 'to be incorrect and is thus changed')
317 parser.add_argument('-o', '--output',
318 help="Output file to write an updated config file to. "
319 "This has to be a separate file from the "
320 "original config file. If one isn't specified "
321 "with -u the new config file will be printed to "
322 "STDOUT")
323 parser.add_argument('-r', '--replace-ext', action='store_true',
324 help="If specified the all option will be replaced "
325 "with a full list of extensions")
326 args = parser.parse_args()
327 return args
328
329
Matthew Treinishf8b816a2014-04-23 20:35:49 +0000330def main():
Matthew Treinish8b006d22014-01-07 15:37:20 +0000331 print('Running config verification...')
Matthew Treinishf0971712014-04-11 20:08:53 +0000332 opts = parse_args()
333 update = opts.update
334 replace = opts.replace_ext
David Kranzf20ac322014-05-02 16:46:15 -0400335 global CONF_PARSER
336
337 outfile = sys.stdout
Matthew Treinishf0971712014-04-11 20:08:53 +0000338 if update:
David Kranzf20ac322014-05-02 16:46:15 -0400339 conf_file = _get_config_file()
Matthew Treinishf0971712014-04-11 20:08:53 +0000340 if opts.output:
David Kranzf20ac322014-05-02 16:46:15 -0400341 outfile = open(opts.output, 'w+')
342 CONF_PARSER = moves.configparser.SafeConfigParser()
343 CONF_PARSER.optionxform = str
344 CONF_PARSER.readfp(conf_file)
David Kranz799eee12015-04-08 11:18:19 -0400345 icreds = credentials.get_isolated_credentials('verify_tempest_config')
David Kranz5fcac942015-05-08 17:43:45 -0400346 try:
347 os = clients.Manager(icreds.get_primary_creds())
348 services = check_service_availability(os, update)
349 results = {}
350 for service in ['nova', 'cinder', 'neutron', 'swift']:
351 if service not in services:
352 continue
353 results = verify_extensions(os, service, results)
Adam Gandelman03af5562014-10-07 12:22:48 -0700354
David Kranz5fcac942015-05-08 17:43:45 -0400355 # Verify API versions of all services in the keystone catalog and
356 # keystone itself.
357 services.append('keystone')
358 for service in services:
359 verify_api_versions(os, service, update)
Adam Gandelman03af5562014-10-07 12:22:48 -0700360
David Kranz5fcac942015-05-08 17:43:45 -0400361 display_results(results, update, replace)
362 if update:
363 conf_file.close()
364 CONF_PARSER.write(outfile)
365 outfile.close()
366 finally:
367 icreds.clear_isolated_creds()
Matthew Treinish1f7b33d2013-10-21 18:07:02 +0000368
369
370if __name__ == "__main__":
Matthew Treinishf8b816a2014-04-23 20:35:49 +0000371 main()