3 # Copyright (c) 2015 Orange
4 # guyrodrigue.koffi@orange.com
5 # morgan.richomme@orange.com
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 import re, json, os, urllib2, argparse, logging, yaml
13 with open('../functest.yaml') as f:
14 functest_yaml = yaml.safe_load(f)
18 cmd = os.popen("date '+%d%m%Y_%H%M'")
19 test_date = cmd.read().rstrip()
21 HOME = os.environ['HOME']+"/"
22 SCENARIOS_DIR = HOME + functest_yaml.get("general").get("directories").get("dir_rally_scn")
23 RESULTS_DIR = HOME + functest_yaml.get("general").get("directories").get("dir_rally_res") + test_date + "/"
25 """ tests configuration """
26 tests = ['authenticate', 'glance', 'cinder', 'heat', 'keystone', 'neutron', 'nova', 'quotas', 'requests', 'tempest', 'vm', 'all', 'smoke']
27 parser = argparse.ArgumentParser()
28 parser.add_argument("test_name", help="The name of the test you want to perform with rally. "
29 "Possible values are : "
30 "[ {d[0]} | {d[1]} | {d[2]} | {d[3]} | {d[4]} | {d[5]} | {d[6]} "
31 "| {d[7]} | {d[8]} | {d[9]} | {d[10]} | {d[11]} | {d[12]}]. The 'all' value performs all the possible tests scenarios"
32 "except 'tempest'".format(d=tests))
34 parser.add_argument("-d", "--debug", help="Debug mode", action="store_true")
35 parser.add_argument("test_mode", help="Tempest test mode", nargs='?', default="smoke")
36 args = parser.parse_args()
37 test_mode=args.test_mode
39 if not args.test_name == "tempest":
40 if not args.test_mode == "smoke":
41 parser.error("test_mode is only used with tempest")
43 """ logging configuration """
44 logger = logging.getLogger('run_rally')
45 logger.setLevel(logging.DEBUG)
47 ch = logging.StreamHandler()
49 ch.setLevel(logging.DEBUG)
51 ch.setLevel(logging.INFO)
53 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
54 ch.setFormatter(formatter)
58 def get_tempest_id(cmd_raw):
60 get task id from command rally result
62 :return: task_id as string
64 taskid_re = re.compile('^Verification UUID: (.*)$')
65 for line in cmd_raw.splitlines(True):
67 match = taskid_re.match(line)
73 def get_task_id(cmd_raw):
75 get task id from command rally result
77 :return: task_id as string
79 taskid_re = re.compile('^Task +(.*): started$')
80 for line in cmd_raw.splitlines(True):
82 match = taskid_re.match(line)
88 def task_succeed(json_raw):
90 Parse JSON from rally JSON results
94 rally_report = json.loads(json_raw)
95 rally_report = rally_report[0]
96 if rally_report is None:
98 if rally_report.get('result') is None:
101 for result in rally_report.get('result'):
102 if len(result.get('error')) > 0:
109 the function dedicated to Tempest (functional tests for OpenStack)
110 :param test_mode: Tempest mode smoke (default), full, ..
113 logger.info('starting {} Tempest ...'.format(test_mode))
115 cmd_line = "rally verify start {}".format(test_mode)
116 logger.debug('running command line : {}'.format(cmd_line))
117 cmd = os.popen(cmd_line)
118 task_id = get_tempest_id(cmd.read())
119 logger.debug('task_id : {}'.format(task_id))
122 logger.error("failed to retrieve task_id")
125 """ check for result directory and create it otherwise """
126 if not os.path.exists(RESULTS_DIR):
127 logger.debug('does not exists, we create it'.format(RESULTS_DIR))
128 os.makedirs(RESULTS_DIR)
130 """ write log report file """
131 report_file_name = '{}opnfv-tempest.log'.format(RESULTS_DIR)
132 cmd_line = "rally verify detailed {} > {} ".format(task_id, report_file_name)
133 logger.debug('running command line : {}'.format(cmd_line))
137 def run_task(test_name):
139 the "main" function of the script who lunch rally for a task
140 :param test_name: name for the rally test
143 logger.info('starting {} test ...'.format(test_name))
145 """ check directory for scenarios test files or retrieve from git otherwise"""
147 test_file_name = '{}opnfv-{}.json'.format(SCENARIOS_DIR, test_name)
148 if not os.path.exists(test_file_name):
149 logger.debug('{} does not exists'.format(test_file_name))
150 proceed_test = retrieve_test_cases_file(test_name, SCENARIOS_DIR)
152 """ we do the test only if we have a scenario test file """
154 logger.debug('Scenario fetched from : {}'.format(test_file_name))
155 cmd_line = "rally task start --abort-on-sla-failure %s" % test_file_name
156 logger.debug('running command line : {}'.format(cmd_line))
157 cmd = os.popen(cmd_line)
158 task_id = get_task_id(cmd.read())
159 logger.debug('task_id : {}'.format(task_id))
162 logger.error("failed to retrieve task_id")
165 """ check for result directory and create it otherwise """
166 if not os.path.exists(RESULTS_DIR):
167 logger.debug('does not exists, we create it'.format(RESULTS_DIR))
168 os.makedirs(RESULTS_DIR)
170 """ write html report file """
171 report_file_name = '{}opnfv-{}.html'.format(RESULTS_DIR, test_name)
172 cmd_line = "rally task report %s --out %s" % (task_id, report_file_name)
173 logger.debug('running command line : {}'.format(cmd_line))
176 """ get and save rally operation JSON result """
177 cmd_line = "rally task results %s" % task_id
178 logger.debug('running command line : {}'.format(cmd_line))
179 cmd = os.popen(cmd_line)
180 json_results = cmd.read()
181 with open('{}opnfv-{}.json'.format(RESULTS_DIR, test_name), 'w') as f:
182 logger.debug('saving json file')
183 f.write(json_results)
184 logger.debug('saving json file2')
186 """ parse JSON operation result """
187 if task_succeed(json_results):
192 logger.error('{} test failed, unable to fetch a scenario test file'.format(test_name))
195 def retrieve_test_cases_file(test_name, tests_path):
197 Retrieve from github the sample test files
198 :return: Boolean that indicates the retrieval status
201 """ do not add the "/" at the end """
202 url_base = "https://git.opnfv.org/cgit/functest/plain/testcases/VIM/OpenStack/CI/suites"
204 test_file_name = 'opnfv-{}.json'.format(test_name)
205 logger.info('fetching {}/{} ...'.format(url_base, test_file_name))
208 response = urllib2.urlopen('{}/{}'.format(url_base, test_file_name))
209 except (urllib2.HTTPError, urllib2.URLError):
211 file_raw = response.read()
213 """ check if the test path exist otherwise we create it """
214 if not os.path.exists(tests_path):
215 os.makedirs(tests_path)
217 with open('{}/{}'.format(tests_path, test_file_name), 'w') as f:
223 """ configure script """
224 if not (args.test_name in tests):
225 logger.error('argument not valid')
228 if args.test_name == "all":
229 for test_name in tests:
230 if not (test_name == 'all' or test_name == 'tempest' or test_name == 'heat' or test_name == 'smoke' ):
234 print(args.test_name)
235 if args.test_name == 'tempest':
238 run_task(args.test_name)
240 if __name__ == '__main__':