4 # Runs tempest and pushes the results to the DB
7 # morgan.richomme@orange.com
8 # jose.lausuch@ericsson.com
9 # viktor.tikkanen@nokia.com
11 # All rights reserved. This program and the accompanying materials
12 # are made available under the terms of the Apache License, Version 2.0
13 # which accompanies this distribution, and is available at
14 # http://www.apache.org/licenses/LICENSE-2.0
27 import functest.utils.functest_logger as ft_logger
28 import functest.utils.functest_utils as ft_utils
29 import functest.utils.openstack_utils as os_utils
30 import functest.utils.functest_constants as ft_constants
32 modes = ['full', 'smoke', 'baremetal', 'compute', 'data_processing',
33 'identity', 'image', 'network', 'object_storage', 'orchestration',
34 'telemetry', 'volume', 'custom', 'defcore', 'feature_multisite']
36 """ tests configuration """
37 parser = argparse.ArgumentParser()
38 parser.add_argument("-d", "--debug",
41 parser.add_argument("-s", "--serial",
42 help="Run tests in one thread",
44 parser.add_argument("-m", "--mode",
45 help="Tempest test mode [smoke, all]",
47 parser.add_argument("-r", "--report",
48 help="Create json result file",
50 parser.add_argument("-n", "--noclean",
51 help="Don't clean the created resources for this test.",
53 parser.add_argument("-c", "--conf",
54 help="User-specified Tempest config file location",
57 args = parser.parse_args()
59 """ logging configuration """
60 logger = ft_logger.Logger("run_tempest").getLogger()
62 GLANCE_IMAGE_NAME = ft_constants.GLANCE_IMAGE_NAME
63 GLANCE_IMAGE_FILENAME = ft_constants.GLANCE_IMAGE_FILENAME
64 GLANCE_IMAGE_FORMAT = ft_constants.GLANCE_IMAGE_FORMAT
65 GLANCE_IMAGE_PATH = os.path.join(ft_constants.FUNCTEST_DATA_DIR,
66 GLANCE_IMAGE_FILENAME)
70 FLAVOR_NAME = ft_constants.FLAVOR_NAME
71 FLAVOR_RAM = ft_constants.FLAVOR_RAM
72 FLAVOR_DISK = ft_constants.FLAVOR_DISK
73 FLAVOR_VCPUS = ft_constants.FLAVOR_VCPUS
76 TEMPEST_PRIVATE_NET_NAME = ft_constants.TEMPEST_PRIVATE_NET_NAME
77 TEMPEST_PRIVATE_SUBNET_NAME = ft_constants.TEMPEST_PRIVATE_SUBNET_NAME
78 TEMPEST_PRIVATE_SUBNET_CIDR = ft_constants.TEMPEST_PRIVATE_SUBNET_CIDR
79 TEMPEST_ROUTER_NAME = ft_constants.TEMPEST_ROUTER_NAME
80 TEMPEST_TENANT_NAME = ft_constants.TEMPEST_TENANT_NAME
81 TEMPEST_TENANT_DESCRIPTION = ft_constants.TEMPEST_TENANT_DESCRIPTION
82 TEMPEST_USER_NAME = ft_constants.TEMPEST_USER_NAME
83 TEMPEST_USER_PASSWORD = ft_constants.TEMPEST_USER_PASSWORD
84 TEMPEST_SSH_TIMEOUT = ft_constants.TEMPEST_SSH_TIMEOUT
85 TEMPEST_USE_CUSTOM_IMAGES = ft_constants.TEMPEST_USE_CUSTOM_IMAGES
86 TEMPEST_USE_CUSTOM_FLAVORS = ft_constants.TEMPEST_USE_CUSTOM_FLAVORS
88 RESULTS_DIR = ft_constants.FUNCTEST_RESULTS_DIR
89 TEMPEST_RESULTS_DIR = os.path.join(RESULTS_DIR, 'tempest')
91 REPO_PATH = ft_constants.FUNCTEST_REPO_DIR
92 TEMPEST_TEST_LIST_DIR = ft_constants.TEMPEST_TEST_LIST_DIR
93 TEMPEST_CUSTOM = os.path.join(REPO_PATH, TEMPEST_TEST_LIST_DIR,
95 TEMPEST_BLACKLIST = os.path.join(REPO_PATH, TEMPEST_TEST_LIST_DIR,
97 TEMPEST_DEFCORE = os.path.join(REPO_PATH, TEMPEST_TEST_LIST_DIR,
99 TEMPEST_RAW_LIST = os.path.join(TEMPEST_RESULTS_DIR, 'test_raw_list.txt')
100 TEMPEST_LIST = os.path.join(TEMPEST_RESULTS_DIR, 'test_list.txt')
103 class GlobalVariables:
109 def get_info(file_result):
114 p = subprocess.Popen('cat tempest.log',
115 shell=True, stdout=subprocess.PIPE,
116 stderr=subprocess.STDOUT)
117 for line in p.stdout.readlines():
119 if (len(test_run) < 1):
120 test_run = re.findall("[0-9]*\.[0-9]*s", line)
121 if (len(duration) < 1):
122 duration = re.findall("[0-9]*\ tests", line)
123 regexp = r"(failures=[0-9]+)"
124 if (len(test_failed) < 1):
125 test_failed = re.findall(regexp, line)
127 logger.debug("test_run:" + test_run)
128 logger.debug("duration:" + duration)
131 def create_tempest_resources():
132 keystone_client = os_utils.get_keystone_client()
134 logger.debug("Creating tenant and user for Tempest suite")
135 tenant_id = os_utils.create_tenant(keystone_client,
137 TEMPEST_TENANT_DESCRIPTION)
139 logger.error("Error : Failed to create %s tenant"
140 % TEMPEST_TENANT_NAME)
142 user_id = os_utils.create_user(keystone_client, TEMPEST_USER_NAME,
143 TEMPEST_USER_PASSWORD,
146 logger.error("Error : Failed to create %s user" % TEMPEST_USER_NAME)
148 logger.debug("Creating private network for Tempest suite")
150 os_utils.create_shared_network_full(TEMPEST_PRIVATE_NET_NAME,
151 TEMPEST_PRIVATE_SUBNET_NAME,
153 TEMPEST_PRIVATE_SUBNET_CIDR)
157 if TEMPEST_USE_CUSTOM_IMAGES:
158 # adding alternative image should be trivial should we need it
159 logger.debug("Creating image for Tempest suite")
160 _, GlobalVariables.IMAGE_ID = os_utils.get_or_create_image(
161 GLANCE_IMAGE_NAME, GLANCE_IMAGE_PATH, GLANCE_IMAGE_FORMAT)
162 if not GlobalVariables.IMAGE_ID:
165 if TEMPEST_USE_CUSTOM_FLAVORS:
166 # adding alternative flavor should be trivial should we need it
167 logger.debug("Creating flavor for Tempest suite")
168 _, GlobalVariables.FLAVOR_ID = os_utils.get_or_create_flavor(
169 FLAVOR_NAME, FLAVOR_RAM, FLAVOR_DISK, FLAVOR_VCPUS)
170 if not GlobalVariables.FLAVOR_ID:
174 def configure_tempest(deployment_dir):
176 Add/update needed parameters into tempest.conf file generated by Rally
179 tempest_conf_file = deployment_dir + "/tempest.conf"
180 if os.path.isfile(tempest_conf_file):
181 logger.debug("Deleting old tempest.conf file...")
182 os.remove(tempest_conf_file)
184 logger.debug("Generating new tempest.conf file...")
185 cmd = "rally verify genconfig"
186 ft_utils.execute_command(cmd)
188 logger.debug("Finding tempest.conf file...")
189 if not os.path.isfile(tempest_conf_file):
190 logger.error("Tempest configuration file %s NOT found."
194 logger.debug("Updating selected tempest.conf parameters...")
195 config = ConfigParser.RawConfigParser()
196 config.read(tempest_conf_file)
197 config.set('compute', 'fixed_network_name', TEMPEST_PRIVATE_NET_NAME)
198 if TEMPEST_USE_CUSTOM_IMAGES:
199 if GlobalVariables.IMAGE_ID is not None:
200 config.set('compute', 'image_ref', GlobalVariables.IMAGE_ID)
201 if IMAGE_ID_ALT is not None:
202 config.set('compute', 'image_ref_alt', IMAGE_ID_ALT)
203 if TEMPEST_USE_CUSTOM_FLAVORS:
204 if GlobalVariables.FLAVOR_ID is not None:
205 config.set('compute', 'flavor_ref', GlobalVariables.FLAVOR_ID)
206 if FLAVOR_ID_ALT is not None:
207 config.set('compute', 'flavor_ref_alt', FLAVOR_ID_ALT)
208 config.set('identity', 'tenant_name', TEMPEST_TENANT_NAME)
209 config.set('identity', 'username', TEMPEST_USER_NAME)
210 config.set('identity', 'password', TEMPEST_USER_PASSWORD)
211 config.set('validation', 'ssh_timeout', TEMPEST_SSH_TIMEOUT)
213 if ft_constants.OS_ENDPOINT_TYPE is not None:
214 services_list = ['compute', 'volume', 'image', 'network',
215 'data-processing', 'object-storage', 'orchestration']
216 sections = config.sections()
217 for service in services_list:
218 if service not in sections:
219 config.add_section(service)
220 config.set(service, 'endpoint_type',
221 ft_constants.OS_ENDPOINT_TYPE)
223 with open(tempest_conf_file, 'wb') as config_file:
224 config.write(config_file)
226 # Copy tempest.conf to /home/opnfv/functest/results/tempest/
227 shutil.copyfile(tempest_conf_file, TEMPEST_RESULTS_DIR + '/tempest.conf')
231 def read_file(filename):
232 with open(filename) as src:
233 return [line.strip() for line in src.readlines()]
236 def generate_test_list(deployment_dir, mode):
237 logger.debug("Generating test case list...")
238 if mode == 'defcore':
239 shutil.copyfile(TEMPEST_DEFCORE, TEMPEST_RAW_LIST)
240 elif mode == 'custom':
241 if os.path.isfile(TEMPEST_CUSTOM):
242 shutil.copyfile(TEMPEST_CUSTOM, TEMPEST_RAW_LIST)
244 logger.error("Tempest test list file %s NOT found."
250 elif mode == 'feature_multisite':
251 testr_mode = " | grep -i kingbird "
255 testr_mode = 'tempest.api.' + mode
256 cmd = ("cd " + deployment_dir + ";" + "testr list-tests " +
257 testr_mode + ">" + TEMPEST_RAW_LIST + ";cd")
258 ft_utils.execute_command(cmd)
261 def apply_tempest_blacklist():
262 logger.debug("Applying tempest blacklist...")
263 cases_file = read_file(TEMPEST_RAW_LIST)
264 result_file = open(TEMPEST_LIST, 'w')
267 installer_type = ft_constants.CI_INSTALLER_TYPE
268 deploy_scenario = ft_constants.CI_SCENARIO
269 if (bool(installer_type) * bool(deploy_scenario)):
270 # if INSTALLER_TYPE and DEPLOY_SCENARIO are set we read the file
271 black_list_file = open(TEMPEST_BLACKLIST)
272 black_list_yaml = yaml.safe_load(black_list_file)
273 black_list_file.close()
274 for item in black_list_yaml:
275 scenarios = item['scenarios']
276 installers = item['installers']
277 if (deploy_scenario in scenarios and
278 installer_type in installers):
279 tests = item['tests']
281 black_tests.append(test)
285 logger.debug("Tempest blacklist file does not exist.")
287 for cases_line in cases_file:
288 for black_tests_line in black_tests:
289 if black_tests_line in cases_line:
292 result_file.write(str(cases_line) + '\n')
296 def run_tempest(OPTION):
298 # the "main" function of the script which launches Rally to run Tempest
299 # :param option: tempest option (smoke, ..)
302 logger.info("Starting Tempest test suite: '%s'." % OPTION)
303 start_time = time.time()
304 stop_time = start_time
305 cmd_line = "rally verify start " + OPTION + " --system-wide"
307 header = ("Tempest environment:\n"
308 " Installer: %s\n Scenario: %s\n Node: %s\n Date: %s\n" %
309 (ft_constants.CI_INSTALLER_TYPE,
310 ft_constants.CI_SCENARIO,
311 ft_constants.CI_NODE,
312 time.strftime("%a %b %d %H:%M:%S %Z %Y")))
314 f_stdout = open(TEMPEST_RESULTS_DIR + "/tempest.log", 'w+')
315 f_stderr = open(TEMPEST_RESULTS_DIR + "/tempest-error.log", 'w+')
316 f_env = open(TEMPEST_RESULTS_DIR + "/environment.log", 'w+')
319 # subprocess.call(cmd_line, shell=True, stdout=f_stdout, stderr=f_stderr)
320 p = subprocess.Popen(
321 cmd_line, shell=True,
322 stdout=subprocess.PIPE,
327 for line in iter(p.stdout.readline, b''):
328 if re.search("\} tempest\.", line):
329 logger.info(line.replace('\n', ''))
337 cmd_line = "rally verify show"
339 p = subprocess.Popen(
340 cmd_line, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
341 for line in p.stdout:
342 if re.search("Tests\:", line):
347 cmd_line = "rally verify list"
348 cmd = os.popen(cmd_line)
349 output = (((cmd.read()).splitlines()[-2]).replace(" ", "")).split("|")
351 # | UUID | Deployment UUID | smoke | tests | failures | Created at |
352 # Duration | Status |
353 num_tests = output[4]
354 num_failures = output[5]
355 time_start = output[6]
357 # Compute duration (lets assume it does not take more than 60 min)
358 dur_min = int(duration.split(':')[1])
359 dur_sec_float = float(duration.split(':')[2])
360 dur_sec_int = int(round(dur_sec_float, 0))
361 dur_sec_int = dur_sec_int + 60 * dur_min
362 stop_time = time.time()
365 diff = (int(num_tests) - int(num_failures))
366 success_rate = 100 * diff / int(num_tests)
370 if 'smoke' in args.mode:
371 case_name = 'tempest_smoke_serial'
372 elif 'feature' in args.mode:
373 case_name = args.mode.replace("feature_", "")
375 case_name = 'tempest_full_parallel'
377 status = ft_utils.check_success_rate(case_name, success_rate)
378 logger.info("Tempest %s success_rate is %s%%, is marked as %s"
379 % (case_name, success_rate, status))
381 # Push results in payload of testcase
383 # add the test in error in the details sections
384 # should be possible to do it during the test
385 logger.debug("Pushing tempest results into DB...")
386 with open(TEMPEST_RESULTS_DIR + "/tempest.log", 'r') as myfile:
387 output = myfile.read()
390 for match in re.findall('(.*?)[. ]*FAILED', output):
393 # Generate json results for DB
394 json_results = {"timestart": time_start, "duration": dur_sec_int,
395 "tests": int(num_tests), "failures": int(num_failures),
396 "errors": error_logs}
397 logger.info("Results: " + str(json_results))
398 # split Tempest smoke and full
401 ft_utils.push_results_to_db("functest",
408 logger.error("Error pushing results into Database '%s'"
419 if not (args.mode in modes):
420 logger.error("Tempest mode not valid. "
421 "Possible values are:\n" + str(modes))
424 if not os.path.exists(TEMPEST_RESULTS_DIR):
425 os.makedirs(TEMPEST_RESULTS_DIR)
427 deployment_dir = ft_utils.get_deployment_dir()
428 create_tempest_resources()
431 GlobalVariables.MODE = ""
432 configure_tempest(deployment_dir)
434 GlobalVariables.MODE = " --tempest-config " + args.conf
436 generate_test_list(deployment_dir, args.mode)
437 apply_tempest_blacklist()
439 GlobalVariables.MODE += " --tests-file " + TEMPEST_LIST
441 GlobalVariables.MODE += " --concur 1"
443 ret_val = run_tempest(GlobalVariables.MODE)
450 if __name__ == '__main__':