blob: b61f286ee025221eaa5c56d03ced92e2dc58ac45 [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 Treinishc795b9e2014-06-09 17:01:10 -040024from six import moves
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
60def verify_glance_api_versions(os, update):
Matthew Treinish99afd072013-10-22 18:03:06 +000061 # Check glance api versions
David Kranz34f18782015-01-06 13:43:55 -050062 versions = os.image_client.get_versions()
Matthew Treinish99afd072013-10-22 18:03:06 +000063 if CONF.image_feature_enabled.api_v1 != ('v1.1' in versions or 'v1.0' in
64 versions):
Matthew Treinishf0971712014-04-11 20:08:53 +000065 print_and_or_update('api_v1', 'image_feature_enabled',
66 not CONF.image_feature_enabled.api_v1, update)
Matthew Treinish99afd072013-10-22 18:03:06 +000067 if CONF.image_feature_enabled.api_v2 != ('v2.0' in versions):
Matthew Treinishf0971712014-04-11 20:08:53 +000068 print_and_or_update('api_v2', 'image_feature_enabled',
69 not CONF.image_feature_enabled.api_v2, update)
Matthew Treinish99afd072013-10-22 18:03:06 +000070
71
Matthew Treinish9b896242014-04-23 21:25:27 +000072def _get_unversioned_endpoint(base_url):
73 endpoint_parts = urlparse.urlparse(base_url)
74 endpoint = endpoint_parts.scheme + '://' + endpoint_parts.netloc
75 return endpoint
76
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 Treinish9b896242014-04-23 21:25:27 +000085 endpoint = _get_unversioned_endpoint(client_dict[service].base_url)
Matthew Treinishe1f32cd2015-02-17 15:06:13 -050086 dscv = CONF.identity.disable_ssl_certificate_validation
87 ca_certs = CONF.identity.ca_certificates_file
88 raw_http = httplib2.Http(disable_ssl_certificate_validation=dscv,
89 ca_certs=ca_certs)
90 __, body = raw_http.request(endpoint, 'GET')
Matthew Treinish864fe072014-03-02 03:47:26 +000091 client_dict[service].reset_path()
Matthew Treinish4f30eb82014-01-07 21:04:49 +000092 body = json.loads(body)
Matthew Treinish864fe072014-03-02 03:47:26 +000093 if service == 'keystone':
94 versions = map(lambda x: x['id'], body['versions']['values'])
95 else:
96 versions = map(lambda x: x['id'], body['versions'])
97 return versions
98
99
Matthew Treinishf0971712014-04-11 20:08:53 +0000100def verify_keystone_api_versions(os, update):
Matthew Treinish864fe072014-03-02 03:47:26 +0000101 # Check keystone api versions
102 versions = _get_api_versions(os, 'keystone')
103 if CONF.identity_feature_enabled.api_v2 != ('v2.0' in versions):
Matthew Treinishf0971712014-04-11 20:08:53 +0000104 print_and_or_update('api_v2', 'identity_feature_enabled',
105 not CONF.identity_feature_enabled.api_v2, update)
Matthew Treinish864fe072014-03-02 03:47:26 +0000106 if CONF.identity_feature_enabled.api_v3 != ('v3.0' in versions):
Matthew Treinishf0971712014-04-11 20:08:53 +0000107 print_and_or_update('api_v3', 'identity_feature_enabled',
108 not CONF.identity_feature_enabled.api_v3, update)
Matthew Treinish864fe072014-03-02 03:47:26 +0000109
110
Matthew Treinishf0971712014-04-11 20:08:53 +0000111def verify_cinder_api_versions(os, update):
Matthew Treinish2e439632014-03-05 21:53:33 +0000112 # Check cinder api versions
113 versions = _get_api_versions(os, 'cinder')
114 if CONF.volume_feature_enabled.api_v1 != ('v1.0' in versions):
Matthew Treinishf0971712014-04-11 20:08:53 +0000115 print_and_or_update('api_v1', 'volume_feature_enabled',
116 not CONF.volume_feature_enabled.api_v1, update)
Matthew Treinish2e439632014-03-05 21:53:33 +0000117 if CONF.volume_feature_enabled.api_v2 != ('v2.0' in versions):
Matthew Treinishf0971712014-04-11 20:08:53 +0000118 print_and_or_update('api_v2', 'volume_feature_enabled',
119 not CONF.volume_feature_enabled.api_v2, update)
Matthew Treinish2e439632014-03-05 21:53:33 +0000120
121
Adam Gandelman03af5562014-10-07 12:22:48 -0700122def verify_api_versions(os, service, update):
123 verify = {
124 'cinder': verify_cinder_api_versions,
125 'glance': verify_glance_api_versions,
126 'keystone': verify_keystone_api_versions,
Adam Gandelman03af5562014-10-07 12:22:48 -0700127 }
128 if service not in verify:
129 return
130 verify[service](os, update)
131
132
Matthew Treinish8b006d22014-01-07 15:37:20 +0000133def get_extension_client(os, service):
134 extensions_client = {
135 'nova': os.extensions_client,
Matthew Treinish8b006d22014-01-07 15:37:20 +0000136 'cinder': os.volumes_extension_client,
Matthew Treinish8c6706d2014-01-07 19:28:18 +0000137 'neutron': os.network_client,
Matthew Treinishc0120ba2014-01-31 20:10:19 +0000138 'swift': os.account_client,
Matthew Treinish8b006d22014-01-07 15:37:20 +0000139 }
140 if service not in extensions_client:
141 print('No tempest extensions client for %s' % service)
142 exit(1)
143 return extensions_client[service]
144
145
146def get_enabled_extensions(service):
147 extensions_options = {
148 'nova': CONF.compute_feature_enabled.api_extensions,
Matthew Treinish8b006d22014-01-07 15:37:20 +0000149 'cinder': CONF.volume_feature_enabled.api_extensions,
Matthew Treinish8c6706d2014-01-07 19:28:18 +0000150 'neutron': CONF.network_feature_enabled.api_extensions,
Matthew Treinishc0120ba2014-01-31 20:10:19 +0000151 'swift': CONF.object_storage_feature_enabled.discoverable_apis,
Matthew Treinish8b006d22014-01-07 15:37:20 +0000152 }
153 if service not in extensions_options:
154 print('No supported extensions list option for %s' % service)
155 exit(1)
156 return extensions_options[service]
157
158
159def verify_extensions(os, service, results):
160 extensions_client = get_extension_client(os, service)
David Kranz5cf4ba42015-02-10 14:00:50 -0500161 if service != 'swift':
David Kranz34e88122014-12-11 15:24:05 -0500162 resp = extensions_client.list_extensions()
163 else:
164 __, resp = extensions_client.list_extensions()
Matthew Treinish54176ce2014-12-08 21:28:05 +0000165 # For Nova, Cinder and Neutron we use the alias name rather than the
166 # 'name' field because the alias is considered to be the canonical
167 # name.
Matthew Treinish8b006d22014-01-07 15:37:20 +0000168 if isinstance(resp, dict):
Matthew Treinish54176ce2014-12-08 21:28:05 +0000169 if service == 'swift':
Matthew Treinishc0120ba2014-01-31 20:10:19 +0000170 # Remove Swift general information from extensions list
171 resp.pop('swift')
172 extensions = resp.keys()
Matthew Treinish8c6706d2014-01-07 19:28:18 +0000173 else:
Matthew Treinish54176ce2014-12-08 21:28:05 +0000174 extensions = map(lambda x: x['alias'], resp['extensions'])
Matthew Treinish8c6706d2014-01-07 19:28:18 +0000175
Matthew Treinish8b006d22014-01-07 15:37:20 +0000176 else:
Matthew Treinish54176ce2014-12-08 21:28:05 +0000177 extensions = map(lambda x: x['alias'], resp)
Matthew Treinish8b006d22014-01-07 15:37:20 +0000178 if not results.get(service):
179 results[service] = {}
180 extensions_opt = get_enabled_extensions(service)
181 if extensions_opt[0] == 'all':
Matthew Treinishf0971712014-04-11 20:08:53 +0000182 results[service]['extensions'] = extensions
Matthew Treinish8b006d22014-01-07 15:37:20 +0000183 return results
184 # Verify that all configured extensions are actually enabled
185 for extension in extensions_opt:
186 results[service][extension] = extension in extensions
187 # Verify that there aren't additional extensions enabled that aren't
188 # specified in the config list
189 for extension in extensions:
190 if extension not in extensions_opt:
191 results[service][extension] = False
Matthew Treinish1f7b33d2013-10-21 18:07:02 +0000192 return results
193
194
Matthew Treinishf0971712014-04-11 20:08:53 +0000195def display_results(results, update, replace):
196 update_dict = {
197 'swift': 'object-storage-feature-enabled',
198 'nova': 'compute-feature-enabled',
Matthew Treinishf0971712014-04-11 20:08:53 +0000199 'cinder': 'volume-feature-enabled',
200 'neutron': 'network-feature-enabled',
201 }
Matthew Treinish8b006d22014-01-07 15:37:20 +0000202 for service in results:
203 # If all extensions are specified as being enabled there is no way to
204 # verify this so we just assume this to be true
205 if results[service].get('extensions'):
Matthew Treinishf0971712014-04-11 20:08:53 +0000206 if replace:
207 output_list = results[service].get('extensions')
208 else:
209 output_list = ['all']
210 else:
211 extension_list = get_enabled_extensions(service)
212 output_list = []
213 for extension in results[service]:
214 if not results[service][extension]:
215 if extension in extension_list:
216 print("%s extension: %s should not be included in the "
217 "list of enabled extensions" % (service,
218 extension))
219 else:
220 print("%s extension: %s should be included in the list"
221 " of enabled extensions" % (service, extension))
222 output_list.append(extension)
Matthew Treinish8b006d22014-01-07 15:37:20 +0000223 else:
Matthew Treinishf0971712014-04-11 20:08:53 +0000224 output_list.append(extension)
225 if update:
226 # Sort List
227 output_list.sort()
228 # Convert list to a string
229 output_string = ', '.join(output_list)
230 if service == 'swift':
231 change_option('discoverable_apis', update_dict[service],
232 output_string)
Matthew Treinishf0971712014-04-11 20:08:53 +0000233 else:
234 change_option('api_extensions', update_dict[service],
235 output_string)
Matthew Treinish1f7b33d2013-10-21 18:07:02 +0000236
237
Matthew Treinishf0971712014-04-11 20:08:53 +0000238def check_service_availability(os, update):
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000239 services = []
240 avail_services = []
241 codename_match = {
242 'volume': 'cinder',
243 'network': 'neutron',
244 'image': 'glance',
245 'object_storage': 'swift',
246 'compute': 'nova',
247 'orchestration': 'heat',
248 'metering': 'ceilometer',
249 'telemetry': 'ceilometer',
Matthew Treinish42d50f62014-04-11 19:47:13 +0000250 'data_processing': 'sahara',
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000251 'baremetal': 'ironic',
Matthew Treinish42d50f62014-04-11 19:47:13 +0000252 'identity': 'keystone',
Victoria Martínez de la Cruz1173b6e2014-09-22 18:32:13 -0300253 'messaging': 'zaqar',
Matthew Treinish42d50f62014-04-11 19:47:13 +0000254 'database': 'trove'
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000255 }
256 # Get catalog list for endpoints to use for validation
David Kranz45714082015-04-01 14:47:33 -0400257 _token, auth_data = os.auth_provider.get_auth()
David Kranz799eee12015-04-08 11:18:19 -0400258 if os.auth_version == 'v2':
259 catalog_key = 'serviceCatalog'
260 else:
261 catalog_key = 'catalog'
262 for entry in auth_data[catalog_key]:
David Kranz45714082015-04-01 14:47:33 -0400263 services.append(entry['type'])
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000264 # Pull all catalog types from config file and compare against endpoint list
265 for cfgname in dir(CONF._config):
266 cfg = getattr(CONF, cfgname)
267 catalog_type = getattr(cfg, 'catalog_type', None)
268 if not catalog_type:
269 continue
270 else:
271 if cfgname == 'identity':
272 # Keystone is a required service for tempest
273 continue
274 if catalog_type not in services:
275 if getattr(CONF.service_available, codename_match[cfgname]):
276 print('Endpoint type %s not found either disable service '
277 '%s or fix the catalog_type in the config file' % (
Matthew Treinish96e9e882014-06-09 18:37:19 -0400278 catalog_type, codename_match[cfgname]))
Matthew Treinishf0971712014-04-11 20:08:53 +0000279 if update:
280 change_option(codename_match[cfgname],
281 'service_available', False)
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000282 else:
283 if not getattr(CONF.service_available,
284 codename_match[cfgname]):
285 print('Endpoint type %s is available, service %s should be'
286 ' set as available in the config file.' % (
Matthew Treinish96e9e882014-06-09 18:37:19 -0400287 catalog_type, codename_match[cfgname]))
Matthew Treinishf0971712014-04-11 20:08:53 +0000288 if update:
289 change_option(codename_match[cfgname],
290 'service_available', True)
David Kranzf20ac322014-05-02 16:46:15 -0400291 # If we are going to enable this we should allow
292 # extension checks.
293 avail_services.append(codename_match[cfgname])
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000294 else:
295 avail_services.append(codename_match[cfgname])
296 return avail_services
Matthew Treinishd44fe032014-01-31 20:07:24 +0000297
298
Matthew Treinishf0971712014-04-11 20:08:53 +0000299def parse_args():
300 parser = argparse.ArgumentParser()
301 parser.add_argument('-u', '--update', action='store_true',
302 help='Update the config file with results from api '
303 'queries. This assumes whatever is set in the '
304 'config file is incorrect. In the case of '
305 'endpoint checks where it could either be the '
306 'incorrect catalog type or the service available '
307 'option the service available option is assumed '
308 'to be incorrect and is thus changed')
309 parser.add_argument('-o', '--output',
310 help="Output file to write an updated config file to. "
311 "This has to be a separate file from the "
312 "original config file. If one isn't specified "
313 "with -u the new config file will be printed to "
314 "STDOUT")
315 parser.add_argument('-r', '--replace-ext', action='store_true',
316 help="If specified the all option will be replaced "
317 "with a full list of extensions")
318 args = parser.parse_args()
319 return args
320
321
Matthew Treinishf8b816a2014-04-23 20:35:49 +0000322def main():
Matthew Treinish8b006d22014-01-07 15:37:20 +0000323 print('Running config verification...')
Matthew Treinishf0971712014-04-11 20:08:53 +0000324 opts = parse_args()
325 update = opts.update
326 replace = opts.replace_ext
David Kranzf20ac322014-05-02 16:46:15 -0400327 global CONF_PARSER
328
329 outfile = sys.stdout
Matthew Treinishf0971712014-04-11 20:08:53 +0000330 if update:
David Kranzf20ac322014-05-02 16:46:15 -0400331 conf_file = _get_config_file()
Matthew Treinishf0971712014-04-11 20:08:53 +0000332 if opts.output:
David Kranzf20ac322014-05-02 16:46:15 -0400333 outfile = open(opts.output, 'w+')
334 CONF_PARSER = moves.configparser.SafeConfigParser()
335 CONF_PARSER.optionxform = str
336 CONF_PARSER.readfp(conf_file)
David Kranz799eee12015-04-08 11:18:19 -0400337 icreds = credentials.get_isolated_credentials('verify_tempest_config')
338 os = clients.Manager(icreds.get_primary_creds())
Matthew Treinishf0971712014-04-11 20:08:53 +0000339 services = check_service_availability(os, update)
Matthew Treinish8b006d22014-01-07 15:37:20 +0000340 results = {}
Ken'ichi Ohmichib7ee5a02014-12-12 04:07:11 +0000341 for service in ['nova', 'cinder', 'neutron', 'swift']:
342 if service not in services:
Matthew Treinishd44fe032014-01-31 20:07:24 +0000343 continue
Matthew Treinish8b006d22014-01-07 15:37:20 +0000344 results = verify_extensions(os, service, results)
Adam Gandelman03af5562014-10-07 12:22:48 -0700345
Marc Solanas Tarreca600f52015-01-29 11:31:28 -0800346 # Verify API versions of all services in the keystone catalog and keystone
Adam Gandelman03af5562014-10-07 12:22:48 -0700347 # itself.
348 services.append('keystone')
349 for service in services:
350 verify_api_versions(os, service, update)
351
Matthew Treinishf0971712014-04-11 20:08:53 +0000352 display_results(results, update, replace)
David Kranzf20ac322014-05-02 16:46:15 -0400353 if update:
354 conf_file.close()
355 CONF_PARSER.write(outfile)
356 outfile.close()
Matthew Treinish1f7b33d2013-10-21 18:07:02 +0000357
358
359if __name__ == "__main__":
Matthew Treinishf8b816a2014-04-23 20:35:49 +0000360 main()