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
16 from functest.utils.constants import CONST
17 import functest.utils.functest_logger as ft_logger
18 import functest.utils.functest_utils as ft_utils
19 import functest.utils.openstack_utils as os_utils
24 REPO_PATH = CONST.dir_repo_functest
25 GLANCE_IMAGE_PATH = os.path.join(CONST.dir_functest_data,
26 CONST.openstack_image_file_name)
27 TEMPEST_TEST_LIST_DIR = CONST.dir_tempest_cases
28 TEMPEST_RESULTS_DIR = os.path.join(CONST.dir_results,
30 TEMPEST_CUSTOM = os.path.join(REPO_PATH, TEMPEST_TEST_LIST_DIR,
32 TEMPEST_BLACKLIST = os.path.join(REPO_PATH, TEMPEST_TEST_LIST_DIR,
34 TEMPEST_DEFCORE = os.path.join(REPO_PATH, TEMPEST_TEST_LIST_DIR,
36 TEMPEST_RAW_LIST = os.path.join(TEMPEST_RESULTS_DIR, 'test_raw_list.txt')
37 TEMPEST_LIST = os.path.join(TEMPEST_RESULTS_DIR, 'test_list.txt')
39 CI_INSTALLER_TYPE = CONST.INSTALLER_TYPE
40 CI_INSTALLER_IP = CONST.INSTALLER_IP
42 """ logging configuration """
43 logger = ft_logger.Logger("Tempest").getLogger()
46 def create_tempest_resources(use_custom_images=False,
47 use_custom_flavors=False):
48 keystone_client = os_utils.get_keystone_client()
50 logger.debug("Creating tenant and user for Tempest suite")
51 tenant_id = os_utils.create_tenant(
53 CONST.tempest_identity_tenant_name,
54 CONST.tempest_identity_tenant_description)
56 logger.error("Failed to create %s tenant"
57 % CONST.tempest_identity_tenant_name)
59 user_id = os_utils.create_user(keystone_client,
60 CONST.tempest_identity_user_name,
61 CONST.tempest_identity_user_password,
64 logger.error("Failed to create %s user" %
65 CONST.tempest_identity_user_name)
67 logger.debug("Creating private network for Tempest suite")
68 network_dic = os_utils.create_shared_network_full(
69 CONST.tempest_private_net_name,
70 CONST.tempest_private_subnet_name,
71 CONST.tempest_router_name,
72 CONST.tempest_private_subnet_cidr)
73 if network_dic is None:
74 raise Exception('Failed to create private network')
81 if CONST.tempest_use_custom_images or use_custom_images:
82 # adding alternative image should be trivial should we need it
83 logger.debug("Creating image for Tempest suite")
84 _, image_id = os_utils.get_or_create_image(
85 CONST.openstack_image_name, GLANCE_IMAGE_PATH,
86 CONST.openstack_image_disk_format)
88 raise Exception('Failed to create image')
91 logger.debug("Creating 2nd image for Tempest suite")
92 _, image_id_alt = os_utils.get_or_create_image(
93 CONST.openstack_image_name_alt, GLANCE_IMAGE_PATH,
94 CONST.openstack_image_disk_format)
95 if image_id_alt is None:
96 raise Exception('Failed to create image')
98 if CONST.tempest_use_custom_flavors or use_custom_flavors:
99 # adding alternative flavor should be trivial should we need it
100 logger.debug("Creating flavor for Tempest suite")
101 _, flavor_id = os_utils.get_or_create_flavor(
102 CONST.openstack_flavor_name,
103 CONST.openstack_flavor_ram,
104 CONST.openstack_flavor_disk,
105 CONST.openstack_flavor_vcpus)
106 if flavor_id is None:
107 raise Exception('Failed to create flavor')
109 if use_custom_flavors:
110 logger.debug("Creating 2nd flavor for tempest_defcore")
111 _, flavor_id_alt = os_utils.get_or_create_flavor(
112 CONST.openstack_flavor_name_alt,
113 CONST.openstack_flavor_ram,
114 CONST.openstack_flavor_disk,
115 CONST.openstack_flavor_vcpus)
116 if flavor_id_alt is None:
117 raise Exception('Failed to create flavor')
120 img_flavor_dict['image_id'] = image_id
121 img_flavor_dict['image_id_alt'] = image_id_alt
122 img_flavor_dict['flavor_id'] = flavor_id
123 img_flavor_dict['flavor_id_alt'] = flavor_id_alt
125 return img_flavor_dict
128 def get_verifier_id():
130 Returns verifer id for current Tempest
132 cmd = ("rally verify list-verifiers | awk '/" +
133 CONST.tempest_deployment_name +
135 p = subprocess.Popen(cmd, shell=True,
136 stdout=subprocess.PIPE,
137 stderr=subprocess.STDOUT)
138 deployment_uuid = p.stdout.readline().rstrip()
139 if deployment_uuid == "":
140 logger.error("Tempest verifier not found.")
141 raise Exception('Error with command:%s' % cmd)
142 return deployment_uuid
145 def get_verifier_deployment_id():
147 Returns deployment id for active Rally deployment
149 cmd = ("rally deployment list | awk '/" +
150 CONST.rally_deployment_name +
152 p = subprocess.Popen(cmd, shell=True,
153 stdout=subprocess.PIPE,
154 stderr=subprocess.STDOUT)
155 deployment_uuid = p.stdout.readline().rstrip()
156 if deployment_uuid == "":
157 logger.error("Rally deployment not found.")
158 raise Exception('Error with command:%s' % cmd)
159 return deployment_uuid
162 def get_verifier_repo_dir(verifier_id):
164 Returns installed verfier repo directory for Tempest
167 verifier_id = get_verifier_id()
169 return os.path.join(CONST.dir_rally_inst,
171 'verifier-{}'.format(verifier_id),
175 def get_verifier_deployment_dir(verifier_id, deployment_id):
177 Returns Rally deployment directory for current verifier
180 verifier_id = get_verifier_id()
182 if not deployment_id:
183 deployment_id = get_verifier_deployment_id()
185 return os.path.join(CONST.dir_rally_inst,
187 'verifier-{}'.format(verifier_id),
188 'for-deployment-{}'.format(deployment_id))
191 def get_repo_tag(repo):
193 Returns last tag of current branch
195 cmd = ("git -C {0} describe --abbrev=0 HEAD".format(repo))
196 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
197 tag = p.stdout.readline().rstrip()
202 def backup_tempest_config(conf_file):
204 Copy config file to tempest results directory
206 if not os.path.exists(TEMPEST_RESULTS_DIR):
207 os.makedirs(TEMPEST_RESULTS_DIR)
209 shutil.copyfile(conf_file,
210 os.path.join(TEMPEST_RESULTS_DIR, 'tempest.conf'))
213 def configure_tempest(deployment_dir, IMAGE_ID=None, FLAVOR_ID=None,
216 Calls rally verify and updates the generated tempest.conf with
219 conf_file = configure_verifier(deployment_dir)
220 configure_tempest_update_params(conf_file,
222 if MODE == 'feature_multisite':
223 configure_tempest_multisite_params(conf_file)
226 def configure_tempest_defcore(deployment_dir, img_flavor_dict):
228 Add/update needed parameters into tempest.conf file
230 conf_file = configure_verifier(deployment_dir)
231 configure_tempest_update_params(conf_file,
232 img_flavor_dict.get("image_id"),
233 img_flavor_dict.get("flavor_id"))
235 logger.debug("Updating selected tempest.conf parameters for defcore...")
236 config = ConfigParser.RawConfigParser()
237 config.read(conf_file)
238 config.set('compute', 'image_ref', img_flavor_dict.get("image_id"))
239 config.set('compute', 'image_ref_alt',
240 img_flavor_dict['image_id_alt'])
241 config.set('compute', 'flavor_ref', img_flavor_dict.get("flavor_id"))
242 config.set('compute', 'flavor_ref_alt',
243 img_flavor_dict['flavor_id_alt'])
245 with open(conf_file, 'wb') as config_file:
246 config.write(config_file)
248 confpath = os.path.join(CONST.dir_functest_test,
249 CONST.refstack_tempest_conf_path)
250 shutil.copyfile(conf_file, confpath)
253 def configure_tempest_update_params(tempest_conf_file,
254 IMAGE_ID=None, FLAVOR_ID=None):
256 Add/update needed parameters into tempest.conf file
258 logger.debug("Updating selected tempest.conf parameters...")
259 config = ConfigParser.RawConfigParser()
260 config.read(tempest_conf_file)
263 'fixed_network_name',
264 CONST.tempest_private_net_name)
265 config.set('compute', 'volume_device_name',
266 CONST.tempest_volume_device_name)
267 if CONST.tempest_use_custom_images:
268 if IMAGE_ID is not None:
269 config.set('compute', 'image_ref', IMAGE_ID)
270 if IMAGE_ID_ALT is not None:
271 config.set('compute', 'image_ref_alt', IMAGE_ID_ALT)
272 if CONST.tempest_use_custom_flavors:
273 if FLAVOR_ID is not None:
274 config.set('compute', 'flavor_ref', FLAVOR_ID)
275 if FLAVOR_ID_ALT is not None:
276 config.set('compute', 'flavor_ref_alt', FLAVOR_ID_ALT)
277 config.set('identity', 'tenant_name', CONST.tempest_identity_tenant_name)
278 config.set('identity', 'username', CONST.tempest_identity_user_name)
279 config.set('identity', 'password', CONST.tempest_identity_user_password)
281 'validation', 'ssh_timeout', CONST.tempest_validation_ssh_timeout)
282 config.set('object-storage', 'operator_role',
283 CONST.tempest_object_storage_operator_role)
285 if CONST.OS_ENDPOINT_TYPE is not None:
286 services_list = ['compute',
293 sections = config.sections()
294 for service in services_list:
295 if service not in sections:
296 config.add_section(service)
297 config.set(service, 'endpoint_type',
298 CONST.OS_ENDPOINT_TYPE)
300 with open(tempest_conf_file, 'wb') as config_file:
301 config.write(config_file)
303 backup_tempest_config(tempest_conf_file)
306 def configure_verifier(deployment_dir):
308 Execute rally verify configure-verifier, which generates tempest.conf
310 tempest_conf_file = os.path.join(deployment_dir, "tempest.conf")
311 if os.path.isfile(tempest_conf_file):
312 logger.debug("Verifier is already configured.")
313 logger.debug("Reconfiguring the current verifier...")
314 cmd = "rally verify configure-verifier --reconfigure"
316 logger.info("Configuring the verifier...")
317 cmd = "rally verify configure-verifier"
318 ft_utils.execute_command(cmd)
320 logger.debug("Looking for tempest.conf file...")
321 if not os.path.isfile(tempest_conf_file):
322 logger.error("Tempest configuration file %s NOT found."
324 raise Exception("Tempest configuration file %s NOT found."
327 return tempest_conf_file
330 def configure_tempest_multisite_params(tempest_conf_file):
332 Add/update multisite parameters into tempest.conf file generated by Rally
334 logger.debug("Updating multisite tempest.conf parameters...")
335 config = ConfigParser.RawConfigParser()
336 config.read(tempest_conf_file)
338 config.set('service_available', 'kingbird', 'true')
339 # cmd = ("openstack endpoint show kingbird | grep publicurl |"
340 # "awk '{print $4}' | awk -F '/' '{print $4}'")
341 # kingbird_api_version = os.popen(cmd).read()
342 kingbird_api_version = os_utils.get_endpoint(service_type='multisite')
344 if CI_INSTALLER_TYPE == 'fuel':
345 # For MOS based setup, the service is accessible
347 kingbird_conf_path = "/etc/kingbird/kingbird.conf"
348 installer_type = CI_INSTALLER_TYPE
349 installer_ip = CI_INSTALLER_IP
350 installer_username = CONST.__getattribute__(
351 'multisite_{}_installer_username'.format(installer_type))
352 installer_password = CONST.__getattribute__(
353 'multisite_{}_installer_password'.format(installer_type))
355 ssh_options = ("-o UserKnownHostsFile=/dev/null -o "
356 "StrictHostKeyChecking=no")
358 # Get the controller IP from the fuel node
359 cmd = 'sshpass -p %s ssh 2>/dev/null %s %s@%s \
360 \'fuel node --env 1| grep controller | grep "True\| 1" \
361 | awk -F\| "{print \$5}"\'' % (installer_password,
365 multisite_controller_ip = "".join(os.popen(cmd).read().split())
367 # Login to controller and get bind host details
368 cmd = 'sshpass -p %s ssh 2>/dev/null %s %s@%s "ssh %s \\" \
369 grep -e "^bind_" %s \\""' % (installer_password,
373 multisite_controller_ip,
375 bind_details = os.popen(cmd).read()
376 bind_details = "".join(bind_details.split())
377 # Extract port number from the bind details
378 bind_port = re.findall(r"\D(\d{4})", bind_details)[0]
379 # Extract ip address from the bind details
380 bind_host = re.findall(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}",
382 kingbird_endpoint_url = "http://%s:%s/" % (bind_host, bind_port)
384 # cmd = "openstack endpoint show kingbird | grep publicurl |\
385 # awk '{print $4}' | awk -F '/' '{print $3}'"
386 # kingbird_endpoint_url = os.popen(cmd).read()
387 kingbird_endpoint_url = os_utils.get_endpoint(service_type='kingbird')
390 config.add_section("kingbird")
392 logger.info('kingbird section exist')
393 config.set('kingbird', 'endpoint_type', 'publicURL')
394 config.set('kingbird', 'TIME_TO_SYNC', '20')
395 config.set('kingbird', 'endpoint_url', kingbird_endpoint_url)
396 config.set('kingbird', 'api_version', kingbird_api_version)
397 with open(tempest_conf_file, 'wb') as config_file:
398 config.write(config_file)
400 backup_tempest_config(tempest_conf_file)
403 def install_verifier_ext(path):
405 Install extension to active verifier
407 logger.info("Installing verifier from existing repo...")
408 tag = get_repo_tag(path)
409 cmd = ("rally verify add-verifier-ext --source {0} "
412 error_msg = ("Problem while adding verifier extension from %s" % path)
413 ft_utils.execute_command_raise(cmd, error_msg=error_msg)