3 # Copyright (c) 2015 All rights reserved
4 # This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
19 from functest.utils.constants import CONST
20 import functest.utils.functest_utils as ft_utils
21 import functest.utils.openstack_utils as os_utils
26 GLANCE_IMAGE_PATH = os.path.join(
27 CONST.__getattribute__('dir_functest_images'),
28 CONST.__getattribute__('openstack_image_file_name'))
29 TEMPEST_RESULTS_DIR = os.path.join(CONST.__getattribute__('dir_results'),
31 TEMPEST_CUSTOM = pkg_resources.resource_filename(
32 'functest', 'opnfv_tests/openstack/tempest/custom_tests/test_list.txt')
33 TEMPEST_BLACKLIST = pkg_resources.resource_filename(
34 'functest', 'opnfv_tests/openstack/tempest/custom_tests/blacklist.txt')
35 TEMPEST_DEFCORE = pkg_resources.resource_filename(
37 'opnfv_tests/openstack/tempest/custom_tests/defcore_req.txt')
38 TEMPEST_RAW_LIST = os.path.join(TEMPEST_RESULTS_DIR, 'test_raw_list.txt')
39 TEMPEST_LIST = os.path.join(TEMPEST_RESULTS_DIR, 'test_list.txt')
40 REFSTACK_RESULTS_DIR = os.path.join(CONST.__getattribute__('dir_results'),
42 TEMPEST_CONF_YAML = pkg_resources.resource_filename(
43 'functest', 'opnfv_tests/openstack/tempest/custom_tests/tempest_conf.yaml')
44 TEST_ACCOUNTS_FILE = pkg_resources.resource_filename(
46 'opnfv_tests/openstack/tempest/custom_tests/test_accounts.yaml')
48 CI_INSTALLER_TYPE = CONST.__getattribute__('INSTALLER_TYPE')
49 CI_INSTALLER_IP = CONST.__getattribute__('INSTALLER_IP')
51 """ logging configuration """
52 logger = logging.getLogger(__name__)
55 def create_tempest_resources(use_custom_images=False,
56 use_custom_flavors=False):
58 logger.debug("Creating private network for Tempest suite")
59 network_dic = os_utils.create_shared_network_full(
60 CONST.__getattribute__('tempest_private_net_name'),
61 CONST.__getattribute__('tempest_private_subnet_name'),
62 CONST.__getattribute__('tempest_router_name'),
63 CONST.__getattribute__('tempest_private_subnet_cidr'))
64 if network_dic is None:
65 raise Exception('Failed to create private network')
72 if (CONST.__getattribute__('tempest_use_custom_images') or
74 # adding alternative image should be trivial should we need it
75 logger.debug("Creating image for Tempest suite")
76 _, image_id = os_utils.get_or_create_image(
77 CONST.__getattribute__('openstack_image_name'),
79 CONST.__getattribute__('openstack_image_disk_format'))
81 raise Exception('Failed to create image')
84 logger.debug("Creating 2nd image for Tempest suite")
85 _, image_id_alt = os_utils.get_or_create_image(
86 CONST.__getattribute__('openstack_image_name_alt'),
88 CONST.__getattribute__('openstack_image_disk_format'))
89 if image_id_alt is None:
90 raise Exception('Failed to create image')
92 if (CONST.__getattribute__('tempest_use_custom_flavors') or
94 # adding alternative flavor should be trivial should we need it
95 logger.debug("Creating flavor for Tempest suite")
96 _, flavor_id = os_utils.get_or_create_flavor(
97 CONST.__getattribute__('openstack_flavor_name'),
98 CONST.__getattribute__('openstack_flavor_ram'),
99 CONST.__getattribute__('openstack_flavor_disk'),
100 CONST.__getattribute__('openstack_flavor_vcpus'))
101 if flavor_id is None:
102 raise Exception('Failed to create flavor')
104 if use_custom_flavors:
105 logger.debug("Creating 2nd flavor for tempest_defcore")
106 _, flavor_id_alt = os_utils.get_or_create_flavor(
107 CONST.__getattribute__('openstack_flavor_name_alt'),
108 CONST.__getattribute__('openstack_flavor_ram'),
109 CONST.__getattribute__('openstack_flavor_disk'),
110 CONST.__getattribute__('openstack_flavor_vcpus'))
111 if flavor_id_alt is None:
112 raise Exception('Failed to create flavor')
115 img_flavor_dict['image_id'] = image_id
116 img_flavor_dict['image_id_alt'] = image_id_alt
117 img_flavor_dict['flavor_id'] = flavor_id
118 img_flavor_dict['flavor_id_alt'] = flavor_id_alt
120 return img_flavor_dict
123 def create_tenant_user():
124 keystone_client = os_utils.get_keystone_client()
126 logger.debug("Creating tenant and user for Tempest suite")
127 tenant_id = os_utils.create_tenant(
129 CONST.__getattribute__('tempest_identity_tenant_name'),
130 CONST.__getattribute__('tempest_identity_tenant_description'))
132 logger.error("Failed to create %s tenant"
133 % CONST.__getattribute__('tempest_identity_tenant_name'))
135 user_id = os_utils.create_user(
137 CONST.__getattribute__('tempest_identity_user_name'),
138 CONST.__getattribute__('tempest_identity_user_password'),
141 logger.error("Failed to create %s user" %
142 CONST.__getattribute__('tempest_identity_user_name'))
147 def get_verifier_id():
149 Returns verifer id for current Tempest
151 cmd = ("rally verify list-verifiers | awk '/" +
152 CONST.__getattribute__('tempest_deployment_name') +
154 p = subprocess.Popen(cmd, shell=True,
155 stdout=subprocess.PIPE,
156 stderr=subprocess.STDOUT)
157 deployment_uuid = p.stdout.readline().rstrip()
158 if deployment_uuid == "":
159 logger.error("Tempest verifier not found.")
160 raise Exception('Error with command:%s' % cmd)
161 return deployment_uuid
164 def get_verifier_deployment_id():
166 Returns deployment id for active Rally deployment
168 cmd = ("rally deployment list | awk '/" +
169 CONST.__getattribute__('rally_deployment_name') +
171 p = subprocess.Popen(cmd, shell=True,
172 stdout=subprocess.PIPE,
173 stderr=subprocess.STDOUT)
174 deployment_uuid = p.stdout.readline().rstrip()
175 if deployment_uuid == "":
176 logger.error("Rally deployment not found.")
177 raise Exception('Error with command:%s' % cmd)
178 return deployment_uuid
181 def get_verifier_repo_dir(verifier_id):
183 Returns installed verfier repo directory for Tempest
186 verifier_id = get_verifier_id()
188 return os.path.join(CONST.__getattribute__('dir_rally_inst'),
190 'verifier-{}'.format(verifier_id),
194 def get_verifier_deployment_dir(verifier_id, deployment_id):
196 Returns Rally deployment directory for current verifier
199 verifier_id = get_verifier_id()
201 if not deployment_id:
202 deployment_id = get_verifier_deployment_id()
204 return os.path.join(CONST.__getattribute__('dir_rally_inst'),
206 'verifier-{}'.format(verifier_id),
207 'for-deployment-{}'.format(deployment_id))
210 def get_repo_tag(repo):
212 Returns last tag of current branch
214 cmd = ("git -C {0} describe --abbrev=0 HEAD".format(repo))
215 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
216 tag = p.stdout.readline().rstrip()
221 def backup_tempest_config(conf_file):
223 Copy config file to tempest results directory
225 if not os.path.exists(TEMPEST_RESULTS_DIR):
226 os.makedirs(TEMPEST_RESULTS_DIR)
228 shutil.copyfile(conf_file,
229 os.path.join(TEMPEST_RESULTS_DIR, 'tempest.conf'))
232 def configure_tempest(deployment_dir, IMAGE_ID=None, FLAVOR_ID=None,
235 Calls rally verify and updates the generated tempest.conf with
238 conf_file = configure_verifier(deployment_dir)
239 configure_tempest_update_params(conf_file,
243 def configure_tempest_defcore(deployment_dir, img_flavor_dict):
245 Add/update needed parameters into tempest.conf file
247 conf_file = configure_verifier(deployment_dir)
248 configure_tempest_update_params(conf_file,
249 img_flavor_dict.get("image_id"),
250 img_flavor_dict.get("flavor_id"))
252 logger.debug("Updating selected tempest.conf parameters for defcore...")
253 config = ConfigParser.RawConfigParser()
254 config.read(conf_file)
255 config.set('DEFAULT', 'log_file', '{}/tempest.log'.format(deployment_dir))
256 config.set('oslo_concurrency', 'lock_path',
257 '{}/lock_files'.format(deployment_dir))
258 generate_test_accounts_file()
259 config.set('auth', 'test_accounts_file', TEST_ACCOUNTS_FILE)
260 config.set('scenario', 'img_dir', '{}'.format(deployment_dir))
261 config.set('scenario', 'img_file', 'tempest-image')
262 config.set('compute', 'image_ref', img_flavor_dict.get("image_id"))
263 config.set('compute', 'image_ref_alt',
264 img_flavor_dict['image_id_alt'])
265 config.set('compute', 'flavor_ref', img_flavor_dict.get("flavor_id"))
266 config.set('compute', 'flavor_ref_alt',
267 img_flavor_dict['flavor_id_alt'])
269 with open(conf_file, 'wb') as config_file:
270 config.write(config_file)
272 confpath = pkg_resources.resource_filename(
274 'opnfv_tests/openstack/refstack_client/refstack_tempest.conf')
275 shutil.copyfile(conf_file, confpath)
278 def generate_test_accounts_file():
280 Add needed tenant and user params into test_accounts.yaml
283 logger.debug("Add needed params into test_accounts.yaml...")
284 tenant_id = create_tenant_user()
288 CONST.__getattribute__('tempest_identity_tenant_name'),
289 'tenant_id': str(tenant_id),
290 'username': CONST.__getattribute__('tempest_identity_user_name'),
292 CONST.__getattribute__('tempest_identity_user_password')
296 with open(TEST_ACCOUNTS_FILE, "w") as f:
297 yaml.dump(accounts_list, f, default_flow_style=False)
300 def configure_tempest_update_params(tempest_conf_file,
301 IMAGE_ID=None, FLAVOR_ID=None):
303 Add/update needed parameters into tempest.conf file
305 logger.debug("Updating selected tempest.conf parameters...")
306 config = ConfigParser.RawConfigParser()
307 config.read(tempest_conf_file)
310 'fixed_network_name',
311 CONST.__getattribute__('tempest_private_net_name'))
312 config.set('compute', 'volume_device_name',
313 CONST.__getattribute__('tempest_volume_device_name'))
314 if CONST.__getattribute__('tempest_use_custom_images'):
315 if IMAGE_ID is not None:
316 config.set('compute', 'image_ref', IMAGE_ID)
317 if IMAGE_ID_ALT is not None:
318 config.set('compute', 'image_ref_alt', IMAGE_ID_ALT)
319 if CONST.__getattribute__('tempest_use_custom_flavors'):
320 if FLAVOR_ID is not None:
321 config.set('compute', 'flavor_ref', FLAVOR_ID)
322 if FLAVOR_ID_ALT is not None:
323 config.set('compute', 'flavor_ref_alt', FLAVOR_ID_ALT)
324 config.set('identity', 'region', 'RegionOne')
325 if os_utils.is_keystone_v3():
329 config.set('identity', 'auth_version', auth_version)
331 'validation', 'ssh_timeout',
332 CONST.__getattribute__('tempest_validation_ssh_timeout'))
333 config.set('object-storage', 'operator_role',
334 CONST.__getattribute__('tempest_object_storage_operator_role'))
336 if CONST.__getattribute__('OS_ENDPOINT_TYPE') is not None:
337 sections = config.sections()
338 if os_utils.is_keystone_v3():
339 config.set('identity', 'v3_endpoint_type',
340 CONST.__getattribute__('OS_ENDPOINT_TYPE'))
341 if 'identity-feature-enabled' not in sections:
342 config.add_section('identity-feature-enabled')
343 config.set('identity-feature-enabled', 'api_v2', False)
344 config.set('identity-feature-enabled', 'api_v2_admin', False)
345 services_list = ['compute',
352 for service in services_list:
353 if service not in sections:
354 config.add_section(service)
355 config.set(service, 'endpoint_type',
356 CONST.__getattribute__('OS_ENDPOINT_TYPE'))
358 logger.debug('Add/Update required params defined in tempest_conf.yaml '
359 'into tempest.conf file')
360 with open(TEMPEST_CONF_YAML) as f:
361 conf_yaml = yaml.safe_load(f)
363 sections = config.sections()
364 for section in conf_yaml:
365 if section not in sections:
366 config.add_section(section)
367 sub_conf = conf_yaml.get(section)
368 for key, value in sub_conf.items():
369 config.set(section, key, value)
371 with open(tempest_conf_file, 'wb') as config_file:
372 config.write(config_file)
374 backup_tempest_config(tempest_conf_file)
377 def configure_verifier(deployment_dir):
379 Execute rally verify configure-verifier, which generates tempest.conf
381 tempest_conf_file = os.path.join(deployment_dir, "tempest.conf")
382 if os.path.isfile(tempest_conf_file):
383 logger.debug("Verifier is already configured.")
384 logger.debug("Reconfiguring the current verifier...")
385 cmd = "rally verify configure-verifier --reconfigure"
387 logger.info("Configuring the verifier...")
388 cmd = "rally verify configure-verifier"
389 ft_utils.execute_command(cmd)
391 logger.debug("Looking for tempest.conf file...")
392 if not os.path.isfile(tempest_conf_file):
393 logger.error("Tempest configuration file %s NOT found."
395 raise Exception("Tempest configuration file %s NOT found."
398 return tempest_conf_file