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
15 """ tests configuration """
16 tests = ['authenticate', 'glance', 'cinder', 'heat', 'keystone', 'neutron', 'nova', 'quotas', 'requests', 'vm', 'tempest', 'all', 'smoke']
17 parser = argparse.ArgumentParser()
18 parser.add_argument("repo_path", help="Path to the repository")
19 parser.add_argument("test_name", help="The name of the test you want to perform with rally. "
20 "Possible values are : "
21 "[ {d[0]} | {d[1]} | {d[2]} | {d[3]} | {d[4]} | {d[5]} | {d[6]} "
22 "| {d[7]} | {d[8]} | {d[9]} | {d[10]} | {d[11]} | {d[12]}]. The 'all' value performs all the possible tests scenarios"
23 "except 'tempest'".format(d=tests))
24 parser.add_argument("-d", "--debug", help="Debug mode", action="store_true")
26 parser.add_argument("test_mode", help="Tempest test mode", nargs='?', default="smoke")
27 args = parser.parse_args()
28 test_mode=args.test_mode
30 if not args.test_name == "tempest":
31 if not args.test_mode == "smoke":
32 parser.error("test_mode is only used with tempest")
34 """ logging configuration """
35 logger = logging.getLogger('run_rally')
36 logger.setLevel(logging.DEBUG)
38 ch = logging.StreamHandler()
40 ch.setLevel(logging.DEBUG)
42 ch.setLevel(logging.INFO)
44 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
45 ch.setFormatter(formatter)
48 with open(args.repo_path+"testcases/config_functest.yaml") as f:
49 functest_yaml = yaml.safe_load(f)
52 HOME = os.environ['HOME']+"/"
53 REPO_PATH = args.repo_path
54 SCENARIOS_DIR = REPO_PATH + functest_yaml.get("general").get("directories").get("dir_rally_scn")
55 RESULTS_DIR = HOME + functest_yaml.get("general").get("directories").get("dir_rally_res") + "/rally/"
60 def get_tempest_id(cmd_raw):
62 get task id from command rally result
64 :return: task_id as string
66 taskid_re = re.compile('^Verification UUID: (.*)$')
67 for line in cmd_raw.splitlines(True):
69 match = taskid_re.match(line)
75 def get_task_id(cmd_raw):
77 get task id from command rally result
79 :return: task_id as string
81 taskid_re = re.compile('^Task +(.*): started$')
82 for line in cmd_raw.splitlines(True):
84 match = taskid_re.match(line)
90 def task_succeed(json_raw):
92 Parse JSON from rally JSON results
96 rally_report = json.loads(json_raw)
97 rally_report = rally_report[0]
98 if rally_report is None:
100 if rally_report.get('result') is None:
103 for result in rally_report.get('result'):
104 if len(result.get('error')) > 0:
111 the function dedicated to Tempest (functional tests for OpenStack)
112 :param test_mode: Tempest mode smoke (default), full, ..
115 logger.info('starting {} Tempest ...'.format(test_mode))
117 cmd_line = "rally verify start {}".format(test_mode)
118 logger.debug('running command line : {}'.format(cmd_line))
119 cmd = os.popen(cmd_line)
120 task_id = get_tempest_id(cmd.read())
121 logger.debug('task_id : {}'.format(task_id))
124 logger.error("failed to retrieve task_id")
127 """ check for result directory and create it otherwise """
128 if not os.path.exists(RESULTS_DIR):
129 logger.debug('does not exists, we create it'.format(RESULTS_DIR))
130 os.makedirs(RESULTS_DIR)
132 """ write log report file """
133 report_file_name = '{}opnfv-tempest.log'.format(RESULTS_DIR)
134 cmd_line = "rally verify detailed {} > {} ".format(task_id, report_file_name)
135 logger.debug('running command line : {}'.format(cmd_line))
139 def run_task(test_name):
141 the "main" function of the script who lunch rally for a task
142 :param test_name: name for the rally test
145 logger.info('starting {} test ...'.format(test_name))
147 """ check directory for scenarios test files or retrieve from git otherwise"""
149 test_file_name = '{}opnfv-{}.json'.format(SCENARIOS_DIR, test_name)
150 if not os.path.exists(test_file_name):
151 logger.error("The scenario '%s' does not exist." %test_file_name)
154 """ we do the test only if we have a scenario test file """
156 logger.debug('Scenario fetched from : {}'.format(test_file_name))
157 cmd_line = "rally task start --abort-on-sla-failure %s" % test_file_name
158 logger.debug('running command line : {}'.format(cmd_line))
159 cmd = os.popen(cmd_line)
160 task_id = get_task_id(cmd.read())
161 logger.debug('task_id : {}'.format(task_id))
164 logger.error("failed to retrieve task_id")
167 """ check for result directory and create it otherwise """
168 if not os.path.exists(RESULTS_DIR):
169 logger.debug('does not exists, we create it'.format(RESULTS_DIR))
170 os.makedirs(RESULTS_DIR)
172 """ write html report file """
173 report_file_name = '{}opnfv-{}.html'.format(RESULTS_DIR, test_name)
174 cmd_line = "rally task report %s --out %s" % (task_id, report_file_name)
175 logger.debug('running command line : {}'.format(cmd_line))
178 """ get and save rally operation JSON result """
179 cmd_line = "rally task results %s" % task_id
180 logger.debug('running command line : {}'.format(cmd_line))
181 cmd = os.popen(cmd_line)
182 json_results = cmd.read()
183 with open('{}opnfv-{}.json'.format(RESULTS_DIR, test_name), 'w') as f:
184 logger.debug('saving json file')
185 f.write(json_results)
186 logger.debug('saving json file2')
188 """ parse JSON operation result """
189 if task_succeed(json_results):
194 logger.error('{} test failed, unable to fetch a scenario test file'.format(test_name))
199 """ configure script """
200 if not (args.test_name in tests):
201 logger.error('argument not valid')
204 if args.test_name == "all":
205 for test_name in tests:
206 if not (test_name == 'all' or test_name == 'tempest' or test_name == 'heat' or test_name == 'smoke' or test_name == 'vm' ):
210 print(args.test_name)
211 if args.test_name == 'tempest':
214 run_task(args.test_name)
216 if __name__ == '__main__':