blob: 673da4fb84c5d9b52a7ed4d4abcc47282915271d [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
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()
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
62 __, versions = os.image_client.get_versions()
63 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 Treinisha5080812014-02-11 15:49:04 +000086 __, body = RAW_HTTP.request(endpoint, 'GET')
Matthew Treinish864fe072014-03-02 03:47:26 +000087 client_dict[service].reset_path()
Matthew Treinish4f30eb82014-01-07 21:04:49 +000088 body = json.loads(body)
Matthew Treinish864fe072014-03-02 03:47:26 +000089 if service == 'keystone':
90 versions = map(lambda x: x['id'], body['versions']['values'])
91 else:
92 versions = map(lambda x: x['id'], body['versions'])
93 return versions
94
95
Matthew Treinishf0971712014-04-11 20:08:53 +000096def verify_keystone_api_versions(os, update):
Matthew Treinish864fe072014-03-02 03:47:26 +000097 # Check keystone api versions
98 versions = _get_api_versions(os, 'keystone')
99 if CONF.identity_feature_enabled.api_v2 != ('v2.0' in versions):
Matthew Treinishf0971712014-04-11 20:08:53 +0000100 print_and_or_update('api_v2', 'identity_feature_enabled',
101 not CONF.identity_feature_enabled.api_v2, update)
Matthew Treinish864fe072014-03-02 03:47:26 +0000102 if CONF.identity_feature_enabled.api_v3 != ('v3.0' in versions):
Matthew Treinishf0971712014-04-11 20:08:53 +0000103 print_and_or_update('api_v3', 'identity_feature_enabled',
104 not CONF.identity_feature_enabled.api_v3, update)
Matthew Treinish864fe072014-03-02 03:47:26 +0000105
106
Matthew Treinishf0971712014-04-11 20:08:53 +0000107def verify_nova_api_versions(os, update):
Matthew Treinish864fe072014-03-02 03:47:26 +0000108 versions = _get_api_versions(os, 'nova')
Matthew Treinish4f30eb82014-01-07 21:04:49 +0000109 if CONF.compute_feature_enabled.api_v3 != ('v3.0' in versions):
Matthew Treinishf0971712014-04-11 20:08:53 +0000110 print_and_or_update('api_v3', 'compute_feature_enabled',
111 not CONF.compute_feature_enabled.api_v3, update)
Matthew Treinish4f30eb82014-01-07 21:04:49 +0000112
113
Matthew Treinishf0971712014-04-11 20:08:53 +0000114def verify_cinder_api_versions(os, update):
Matthew Treinish2e439632014-03-05 21:53:33 +0000115 # Check cinder api versions
116 versions = _get_api_versions(os, 'cinder')
117 if CONF.volume_feature_enabled.api_v1 != ('v1.0' in versions):
Matthew Treinishf0971712014-04-11 20:08:53 +0000118 print_and_or_update('api_v1', 'volume_feature_enabled',
119 not CONF.volume_feature_enabled.api_v1, update)
Matthew Treinish2e439632014-03-05 21:53:33 +0000120 if CONF.volume_feature_enabled.api_v2 != ('v2.0' in versions):
Matthew Treinishf0971712014-04-11 20:08:53 +0000121 print_and_or_update('api_v2', 'volume_feature_enabled',
122 not CONF.volume_feature_enabled.api_v2, update)
Matthew Treinish2e439632014-03-05 21:53:33 +0000123
124
Matthew Treinish8b006d22014-01-07 15:37:20 +0000125def get_extension_client(os, service):
126 extensions_client = {
127 'nova': os.extensions_client,
128 'nova_v3': os.extensions_v3_client,
129 'cinder': os.volumes_extension_client,
Matthew Treinish8c6706d2014-01-07 19:28:18 +0000130 'neutron': os.network_client,
Matthew Treinishc0120ba2014-01-31 20:10:19 +0000131 'swift': os.account_client,
Matthew Treinish8b006d22014-01-07 15:37:20 +0000132 }
133 if service not in extensions_client:
134 print('No tempest extensions client for %s' % service)
135 exit(1)
136 return extensions_client[service]
137
138
139def get_enabled_extensions(service):
140 extensions_options = {
141 'nova': CONF.compute_feature_enabled.api_extensions,
142 'nova_v3': CONF.compute_feature_enabled.api_v3_extensions,
143 'cinder': CONF.volume_feature_enabled.api_extensions,
Matthew Treinish8c6706d2014-01-07 19:28:18 +0000144 'neutron': CONF.network_feature_enabled.api_extensions,
Matthew Treinishc0120ba2014-01-31 20:10:19 +0000145 'swift': CONF.object_storage_feature_enabled.discoverable_apis,
Matthew Treinish8b006d22014-01-07 15:37:20 +0000146 }
147 if service not in extensions_options:
148 print('No supported extensions list option for %s' % service)
149 exit(1)
150 return extensions_options[service]
151
152
153def verify_extensions(os, service, results):
154 extensions_client = get_extension_client(os, service)
Matthew Treinish1f7b33d2013-10-21 18:07:02 +0000155 __, resp = extensions_client.list_extensions()
Matthew Treinish8b006d22014-01-07 15:37:20 +0000156 if isinstance(resp, dict):
Ken'ichi Ohmichia7e68712014-05-06 10:47:26 +0900157 # For both Nova and Neutron we use the alias name rather than the
158 # 'name' field because the alias is considered to be the canonical
159 # name.
160 if service in ['nova', 'nova_v3', 'neutron']:
Matthew Treinish8c6706d2014-01-07 19:28:18 +0000161 extensions = map(lambda x: x['alias'], resp['extensions'])
Matthew Treinishc0120ba2014-01-31 20:10:19 +0000162 elif service == 'swift':
163 # Remove Swift general information from extensions list
164 resp.pop('swift')
165 extensions = resp.keys()
Matthew Treinish8c6706d2014-01-07 19:28:18 +0000166 else:
167 extensions = map(lambda x: x['name'], resp['extensions'])
168
Matthew Treinish8b006d22014-01-07 15:37:20 +0000169 else:
170 extensions = map(lambda x: x['name'], resp)
171 if not results.get(service):
172 results[service] = {}
173 extensions_opt = get_enabled_extensions(service)
174 if extensions_opt[0] == 'all':
Matthew Treinishf0971712014-04-11 20:08:53 +0000175 results[service]['extensions'] = extensions
Matthew Treinish8b006d22014-01-07 15:37:20 +0000176 return results
177 # Verify that all configured extensions are actually enabled
178 for extension in extensions_opt:
179 results[service][extension] = extension in extensions
180 # Verify that there aren't additional extensions enabled that aren't
181 # specified in the config list
182 for extension in extensions:
183 if extension not in extensions_opt:
184 results[service][extension] = False
Matthew Treinish1f7b33d2013-10-21 18:07:02 +0000185 return results
186
187
Matthew Treinishf0971712014-04-11 20:08:53 +0000188def display_results(results, update, replace):
189 update_dict = {
190 'swift': 'object-storage-feature-enabled',
191 'nova': 'compute-feature-enabled',
192 'nova_v3': 'compute-feature-enabled',
193 'cinder': 'volume-feature-enabled',
194 'neutron': 'network-feature-enabled',
195 }
Matthew Treinish8b006d22014-01-07 15:37:20 +0000196 for service in results:
197 # If all extensions are specified as being enabled there is no way to
198 # verify this so we just assume this to be true
199 if results[service].get('extensions'):
Matthew Treinishf0971712014-04-11 20:08:53 +0000200 if replace:
201 output_list = results[service].get('extensions')
202 else:
203 output_list = ['all']
204 else:
205 extension_list = get_enabled_extensions(service)
206 output_list = []
207 for extension in results[service]:
208 if not results[service][extension]:
209 if extension in extension_list:
210 print("%s extension: %s should not be included in the "
211 "list of enabled extensions" % (service,
212 extension))
213 else:
214 print("%s extension: %s should be included in the list"
215 " of enabled extensions" % (service, extension))
216 output_list.append(extension)
Matthew Treinish8b006d22014-01-07 15:37:20 +0000217 else:
Matthew Treinishf0971712014-04-11 20:08:53 +0000218 output_list.append(extension)
219 if update:
220 # Sort List
221 output_list.sort()
222 # Convert list to a string
223 output_string = ', '.join(output_list)
224 if service == 'swift':
225 change_option('discoverable_apis', update_dict[service],
226 output_string)
227 elif service == 'nova_v3':
228 change_option('api_v3_extensions', update_dict[service],
229 output_string)
230 else:
231 change_option('api_extensions', update_dict[service],
232 output_string)
Matthew Treinish1f7b33d2013-10-21 18:07:02 +0000233
234
Matthew Treinishf0971712014-04-11 20:08:53 +0000235def check_service_availability(os, update):
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000236 services = []
237 avail_services = []
238 codename_match = {
239 'volume': 'cinder',
240 'network': 'neutron',
241 'image': 'glance',
242 'object_storage': 'swift',
243 'compute': 'nova',
244 'orchestration': 'heat',
245 'metering': 'ceilometer',
246 'telemetry': 'ceilometer',
Matthew Treinish42d50f62014-04-11 19:47:13 +0000247 'data_processing': 'sahara',
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000248 'baremetal': 'ironic',
Matthew Treinish42d50f62014-04-11 19:47:13 +0000249 'identity': 'keystone',
250 'queuing': 'marconi',
251 'database': 'trove'
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000252 }
253 # Get catalog list for endpoints to use for validation
254 __, endpoints = os.endpoints_client.list_endpoints()
255 for endpoint in endpoints:
256 __, service = os.service_client.get_service(endpoint['service_id'])
257 services.append(service['type'])
258 # Pull all catalog types from config file and compare against endpoint list
259 for cfgname in dir(CONF._config):
260 cfg = getattr(CONF, cfgname)
261 catalog_type = getattr(cfg, 'catalog_type', None)
262 if not catalog_type:
263 continue
264 else:
265 if cfgname == 'identity':
266 # Keystone is a required service for tempest
267 continue
268 if catalog_type not in services:
269 if getattr(CONF.service_available, codename_match[cfgname]):
270 print('Endpoint type %s not found either disable service '
271 '%s or fix the catalog_type in the config file' % (
272 catalog_type, codename_match[cfgname]))
Matthew Treinishf0971712014-04-11 20:08:53 +0000273 if update:
274 change_option(codename_match[cfgname],
275 'service_available', False)
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000276 else:
277 if not getattr(CONF.service_available,
278 codename_match[cfgname]):
279 print('Endpoint type %s is available, service %s should be'
280 ' set as available in the config file.' % (
281 catalog_type, codename_match[cfgname]))
Matthew Treinishf0971712014-04-11 20:08:53 +0000282 if update:
283 change_option(codename_match[cfgname],
284 'service_available', True)
David Kranzf20ac322014-05-02 16:46:15 -0400285 # If we are going to enable this we should allow
286 # extension checks.
287 avail_services.append(codename_match[cfgname])
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000288 else:
289 avail_services.append(codename_match[cfgname])
290 return avail_services
Matthew Treinishd44fe032014-01-31 20:07:24 +0000291
292
Matthew Treinishf0971712014-04-11 20:08:53 +0000293def parse_args():
294 parser = argparse.ArgumentParser()
295 parser.add_argument('-u', '--update', action='store_true',
296 help='Update the config file with results from api '
297 'queries. This assumes whatever is set in the '
298 'config file is incorrect. In the case of '
299 'endpoint checks where it could either be the '
300 'incorrect catalog type or the service available '
301 'option the service available option is assumed '
302 'to be incorrect and is thus changed')
303 parser.add_argument('-o', '--output',
304 help="Output file to write an updated config file to. "
305 "This has to be a separate file from the "
306 "original config file. If one isn't specified "
307 "with -u the new config file will be printed to "
308 "STDOUT")
309 parser.add_argument('-r', '--replace-ext', action='store_true',
310 help="If specified the all option will be replaced "
311 "with a full list of extensions")
312 args = parser.parse_args()
313 return args
314
315
Matthew Treinishf8b816a2014-04-23 20:35:49 +0000316def main():
Matthew Treinish8b006d22014-01-07 15:37:20 +0000317 print('Running config verification...')
Matthew Treinishf0971712014-04-11 20:08:53 +0000318 opts = parse_args()
319 update = opts.update
320 replace = opts.replace_ext
David Kranzf20ac322014-05-02 16:46:15 -0400321 global CONF_PARSER
322
323 outfile = sys.stdout
Matthew Treinishf0971712014-04-11 20:08:53 +0000324 if update:
David Kranzf20ac322014-05-02 16:46:15 -0400325 conf_file = _get_config_file()
Matthew Treinishf0971712014-04-11 20:08:53 +0000326 if opts.output:
David Kranzf20ac322014-05-02 16:46:15 -0400327 outfile = open(opts.output, 'w+')
328 CONF_PARSER = moves.configparser.SafeConfigParser()
329 CONF_PARSER.optionxform = str
330 CONF_PARSER.readfp(conf_file)
Matthew Treinish1f7b33d2013-10-21 18:07:02 +0000331 os = clients.ComputeAdminManager(interface='json')
Matthew Treinishf0971712014-04-11 20:08:53 +0000332 services = check_service_availability(os, update)
Matthew Treinish8b006d22014-01-07 15:37:20 +0000333 results = {}
Matthew Treinishc0120ba2014-01-31 20:10:19 +0000334 for service in ['nova', 'nova_v3', 'cinder', 'neutron', 'swift']:
Matthew Treinish221bd7f2014-02-07 21:16:09 +0000335 if service == 'nova_v3' and 'nova' not in services:
336 continue
337 elif service not in services:
Matthew Treinishd44fe032014-01-31 20:07:24 +0000338 continue
Matthew Treinish8b006d22014-01-07 15:37:20 +0000339 results = verify_extensions(os, service, results)
Matthew Treinishf0971712014-04-11 20:08:53 +0000340 verify_keystone_api_versions(os, update)
341 verify_glance_api_versions(os, update)
342 verify_nova_api_versions(os, update)
343 verify_cinder_api_versions(os, update)
344 display_results(results, update, replace)
David Kranzf20ac322014-05-02 16:46:15 -0400345 if update:
346 conf_file.close()
347 CONF_PARSER.write(outfile)
348 outfile.close()
Matthew Treinish1f7b33d2013-10-21 18:07:02 +0000349
350
351if __name__ == "__main__":
Matthew Treinishf8b816a2014-04-23 20:35:49 +0000352 main()