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
11 """Tempest configuration utilities."""
23 from functest.utils.constants import CONST
24 import functest.utils.functest_utils as ft_utils
29 RALLY_CONF_PATH = "/etc/rally/rally.conf"
30 RALLY_AARCH64_PATCH_PATH = pkg_resources.resource_filename(
31 'functest', 'ci/rally_aarch64_patch.conf')
32 GLANCE_IMAGE_PATH = os.path.join(
33 CONST.__getattribute__('dir_functest_images'),
34 CONST.__getattribute__('openstack_image_file_name'))
35 TEMPEST_RESULTS_DIR = os.path.join(CONST.__getattribute__('dir_results'),
37 TEMPEST_CUSTOM = pkg_resources.resource_filename(
38 'functest', 'opnfv_tests/openstack/tempest/custom_tests/test_list.txt')
39 TEMPEST_BLACKLIST = pkg_resources.resource_filename(
40 'functest', 'opnfv_tests/openstack/tempest/custom_tests/blacklist.txt')
41 TEMPEST_DEFCORE = pkg_resources.resource_filename(
43 'opnfv_tests/openstack/tempest/custom_tests/defcore_req.txt')
44 TEMPEST_RAW_LIST = os.path.join(TEMPEST_RESULTS_DIR, 'test_raw_list.txt')
45 TEMPEST_LIST = os.path.join(TEMPEST_RESULTS_DIR, 'test_list.txt')
46 REFSTACK_RESULTS_DIR = os.path.join(CONST.__getattribute__('dir_results'),
48 TEMPEST_CONF_YAML = pkg_resources.resource_filename(
49 'functest', 'opnfv_tests/openstack/tempest/custom_tests/tempest_conf.yaml')
50 TEST_ACCOUNTS_FILE = pkg_resources.resource_filename(
52 'opnfv_tests/openstack/tempest/custom_tests/test_accounts.yaml')
54 CI_INSTALLER_TYPE = CONST.__getattribute__('INSTALLER_TYPE')
56 """ logging configuration """
57 LOGGER = logging.getLogger(__name__)
60 def create_rally_deployment():
61 """Create new rally deployment"""
62 # set the architecture to default
63 pod_arch = os.getenv("POD_ARCH", None)
64 arch_filter = ['aarch64']
66 if pod_arch and pod_arch in arch_filter:
67 LOGGER.info("Apply aarch64 specific to rally config...")
68 with open(RALLY_AARCH64_PATCH_PATH, "r") as pfile:
69 rally_patch_conf = pfile.read()
71 for line in fileinput.input(RALLY_CONF_PATH, inplace=1):
73 if "cirros|testvm" in line:
74 print rally_patch_conf
76 LOGGER.info("Creating Rally environment...")
78 cmd = "rally deployment destroy opnfv-rally"
79 ft_utils.execute_command(cmd, error_msg=(
80 "Deployment %s does not exist."
81 % CONST.__getattribute__('rally_deployment_name')),
84 cmd = ("rally deployment create --fromenv --name={0}"
85 .format(CONST.__getattribute__('rally_deployment_name')))
86 error_msg = "Problem while creating Rally deployment"
87 ft_utils.execute_command_raise(cmd, error_msg=error_msg)
89 cmd = "rally deployment check"
90 error_msg = "OpenStack not responding or faulty Rally deployment."
91 ft_utils.execute_command_raise(cmd, error_msg=error_msg)
94 def create_verifier():
95 """Create new verifier"""
96 LOGGER.info("Create verifier from existing repo...")
97 cmd = ("rally verify delete-verifier --id '{0}' --force").format(
98 CONST.__getattribute__('tempest_verifier_name'))
99 ft_utils.execute_command(cmd, error_msg=(
100 "Verifier %s does not exist."
101 % CONST.__getattribute__('tempest_verifier_name')),
103 cmd = ("rally verify create-verifier --source {0} "
104 "--name {1} --type tempest --system-wide"
105 .format(CONST.__getattribute__('dir_repo_tempest'),
106 CONST.__getattribute__('tempest_verifier_name')))
107 ft_utils.execute_command_raise(cmd,
108 error_msg='Problem while creating verifier')
111 def get_verifier_id():
113 Returns verifier id for current Tempest
115 create_rally_deployment()
117 cmd = ("rally verify list-verifiers | awk '/" +
118 CONST.__getattribute__('tempest_verifier_name') +
120 proc = subprocess.Popen(cmd, shell=True,
121 stdout=subprocess.PIPE,
122 stderr=subprocess.STDOUT)
123 deployment_uuid = proc.stdout.readline().rstrip()
124 if deployment_uuid == "":
125 LOGGER.error("Tempest verifier not found.")
126 raise Exception('Error with command:%s' % cmd)
127 return deployment_uuid
130 def get_verifier_deployment_id():
132 Returns deployment id for active Rally deployment
134 cmd = ("rally deployment list | awk '/" +
135 CONST.__getattribute__('rally_deployment_name') +
137 proc = subprocess.Popen(cmd, shell=True,
138 stdout=subprocess.PIPE,
139 stderr=subprocess.STDOUT)
140 deployment_uuid = proc.stdout.readline().rstrip()
141 if deployment_uuid == "":
142 LOGGER.error("Rally deployment not found.")
143 raise Exception('Error with command:%s' % cmd)
144 return deployment_uuid
147 def get_verifier_repo_dir(verifier_id):
149 Returns installed verifier repo directory for Tempest
152 verifier_id = get_verifier_id()
154 return os.path.join(CONST.__getattribute__('dir_rally_inst'),
156 'verifier-{}'.format(verifier_id),
160 def get_verifier_deployment_dir(verifier_id, deployment_id):
162 Returns Rally deployment directory for current verifier
165 verifier_id = get_verifier_id()
167 if not deployment_id:
168 deployment_id = get_verifier_deployment_id()
170 return os.path.join(CONST.__getattribute__('dir_rally_inst'),
172 'verifier-{}'.format(verifier_id),
173 'for-deployment-{}'.format(deployment_id))
176 def backup_tempest_config(conf_file):
178 Copy config file to tempest results directory
180 if not os.path.exists(TEMPEST_RESULTS_DIR):
181 os.makedirs(TEMPEST_RESULTS_DIR)
182 shutil.copyfile(conf_file,
183 os.path.join(TEMPEST_RESULTS_DIR, 'tempest.conf'))
186 def configure_tempest(deployment_dir, network_name=None, image_id=None,
187 flavor_id=None, compute_cnt=None):
189 Calls rally verify and updates the generated tempest.conf with
192 conf_file = configure_verifier(deployment_dir)
193 configure_tempest_update_params(conf_file, network_name, image_id,
194 flavor_id, compute_cnt)
197 def configure_tempest_defcore(deployment_dir, network_name, image_id,
198 flavor_id, image_id_alt, flavor_id_alt,
200 # pylint: disable=too-many-arguments
202 Add/update needed parameters into tempest.conf file
204 conf_file = configure_verifier(deployment_dir)
205 configure_tempest_update_params(conf_file, network_name, image_id,
208 LOGGER.debug("Updating selected tempest.conf parameters for defcore...")
209 config = ConfigParser.RawConfigParser()
210 config.read(conf_file)
211 config.set('DEFAULT', 'log_file', '{}/tempest.log'.format(deployment_dir))
212 config.set('oslo_concurrency', 'lock_path',
213 '{}/lock_files'.format(deployment_dir))
214 generate_test_accounts_file(tenant_id=tenant_id)
215 config.set('auth', 'test_accounts_file', TEST_ACCOUNTS_FILE)
216 config.set('scenario', 'img_dir', '{}'.format(deployment_dir))
217 config.set('scenario', 'img_file', 'tempest-image')
218 config.set('compute', 'image_ref', image_id)
219 config.set('compute', 'image_ref_alt', image_id_alt)
220 config.set('compute', 'flavor_ref', flavor_id)
221 config.set('compute', 'flavor_ref_alt', flavor_id_alt)
223 with open(conf_file, 'wb') as config_file:
224 config.write(config_file)
226 confpath = pkg_resources.resource_filename(
228 'opnfv_tests/openstack/refstack_client/refstack_tempest.conf')
229 shutil.copyfile(conf_file, confpath)
232 def generate_test_accounts_file(tenant_id):
234 Add needed tenant and user params into test_accounts.yaml
237 LOGGER.debug("Add needed params into test_accounts.yaml...")
241 CONST.__getattribute__('tempest_identity_tenant_name'),
242 'tenant_id': str(tenant_id),
243 'username': CONST.__getattribute__('tempest_identity_user_name'),
245 CONST.__getattribute__('tempest_identity_user_password')
249 with open(TEST_ACCOUNTS_FILE, "w") as tfile:
250 yaml.dump(accounts_list, tfile, default_flow_style=False)
253 def update_tempest_conf_file(conf_file, config):
254 """Update defined paramters into tempest config file"""
255 with open(TEMPEST_CONF_YAML) as yfile:
256 conf_yaml = yaml.safe_load(yfile)
258 sections = config.sections()
259 for section in conf_yaml:
260 if section not in sections:
261 config.add_section(section)
262 sub_conf = conf_yaml.get(section)
263 for key, value in sub_conf.items():
264 config.set(section, key, value)
266 with open(conf_file, 'wb') as config_file:
267 config.write(config_file)
270 def configure_tempest_update_params(tempest_conf_file, network_name=None,
271 image_id=None, flavor_id=None,
274 Add/update needed parameters into tempest.conf file
276 LOGGER.debug("Updating selected tempest.conf parameters...")
277 config = ConfigParser.RawConfigParser()
278 config.read(tempest_conf_file)
279 config.set('compute', 'fixed_network_name', network_name)
280 config.set('compute', 'volume_device_name',
281 CONST.__getattribute__('tempest_volume_device_name'))
283 if image_id is not None:
284 config.set('compute', 'image_ref', image_id)
285 if IMAGE_ID_ALT is not None:
286 config.set('compute', 'image_ref_alt', IMAGE_ID_ALT)
287 if CONST.__getattribute__('tempest_use_custom_flavors'):
288 if flavor_id is not None:
289 config.set('compute', 'flavor_ref', flavor_id)
290 if FLAVOR_ID_ALT is not None:
291 config.set('compute', 'flavor_ref_alt', FLAVOR_ID_ALT)
293 # enable multinode tests
294 config.set('compute', 'min_compute_nodes', compute_cnt)
295 config.set('compute-feature-enabled', 'live_migration', True)
297 config.set('identity', 'region',
298 CONST.__getattribute__('OS_REGION_NAME'))
299 identity_api_version = os.getenv(
300 "OS_IDENTITY_API_VERSION", os.getenv("IDENTITY_API_VERSION"))
301 if identity_api_version == '3':
303 config.set('identity-feature-enabled', 'api_v2', False)
306 config.set('identity', 'auth_version', auth_version)
308 'validation', 'ssh_timeout',
309 CONST.__getattribute__('tempest_validation_ssh_timeout'))
310 config.set('object-storage', 'operator_role',
311 CONST.__getattribute__('tempest_object_storage_operator_role'))
313 if CONST.__getattribute__('OS_ENDPOINT_TYPE') is not None:
314 config.set('identity', 'v3_endpoint_type',
315 CONST.__getattribute__('OS_ENDPOINT_TYPE'))
317 if CONST.__getattribute__('OS_ENDPOINT_TYPE') is not None:
318 sections = config.sections()
319 services_list = ['compute',
326 for service in services_list:
327 if service not in sections:
328 config.add_section(service)
329 config.set(service, 'endpoint_type',
330 CONST.__getattribute__('OS_ENDPOINT_TYPE'))
332 LOGGER.debug('Add/Update required params defined in tempest_conf.yaml '
333 'into tempest.conf file')
334 update_tempest_conf_file(tempest_conf_file, config)
336 backup_tempest_config(tempest_conf_file)
339 def configure_verifier(deployment_dir):
341 Execute rally verify configure-verifier, which generates tempest.conf
343 tempest_conf_file = os.path.join(deployment_dir, "tempest.conf")
344 if os.path.isfile(tempest_conf_file):
345 LOGGER.debug("Verifier is already configured.")
346 LOGGER.debug("Reconfiguring the current verifier...")
347 cmd = "rally verify configure-verifier --reconfigure"
349 LOGGER.info("Configuring the verifier...")
350 cmd = "rally verify configure-verifier"
351 ft_utils.execute_command(cmd)
353 LOGGER.debug("Looking for tempest.conf file...")
354 if not os.path.isfile(tempest_conf_file):
355 LOGGER.error("Tempest configuration file %s NOT found.",
357 raise Exception("Tempest configuration file %s NOT found."
360 return tempest_conf_file