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
16 cmd = os.popen("date '+%d%m%Y_%H%M'")
17 test_date = cmd.read().rstrip()
20 """ tests configuration """
21 tests = ['authenticate', 'glance', 'cinder', 'heat', 'keystone', 'neutron', 'nova', 'quotas', 'requests', 'vm', 'tempest', 'all', 'smoke']
22 parser = argparse.ArgumentParser()
23 parser.add_argument("repo_path", help="Path to the repository")
24 parser.add_argument("test_name", help="The name of the test you want to perform with rally. "
25 "Possible values are : "
26 "[ {d[0]} | {d[1]} | {d[2]} | {d[3]} | {d[4]} | {d[5]} | {d[6]} "
27 "| {d[7]} | {d[8]} | {d[9]} | {d[10]} | {d[11]} | {d[12]}]. The 'all' value performs all the possible tests scenarios"
28 "except 'tempest'".format(d=tests))
29 parser.add_argument("-d", "--debug", help="Debug mode", action="store_true")
31 parser.add_argument("test_mode", help="Tempest test mode", nargs='?', default="smoke")
32 args = parser.parse_args()
33 test_mode=args.test_mode
35 if not args.test_name == "tempest":
36 if not args.test_mode == "smoke":
37 parser.error("test_mode is only used with tempest")
39 """ logging configuration """
40 logger = logging.getLogger('run_rally')
41 logger.setLevel(logging.DEBUG)
43 ch = logging.StreamHandler()
45 ch.setLevel(logging.DEBUG)
47 ch.setLevel(logging.INFO)
49 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
50 ch.setFormatter(formatter)
53 with open(args.repo_path+"testcases/config_functest.yaml") as f:
54 functest_yaml = yaml.safe_load(f)
57 HOME = os.environ['HOME']+"/"
58 REPO_PATH = args.repo_path
59 SCENARIOS_DIR = REPO_PATH + functest_yaml.get("general").get("directories").get("dir_rally_scn")
60 RESULTS_DIR = HOME + functest_yaml.get("general").get("directories").get("dir_rally_res") + test_date + "/"
65 def get_tempest_id(cmd_raw):
67 get task id from command rally result
69 :return: task_id as string
71 taskid_re = re.compile('^Verification UUID: (.*)$')
72 for line in cmd_raw.splitlines(True):
74 match = taskid_re.match(line)
80 def get_task_id(cmd_raw):
82 get task id from command rally result
84 :return: task_id as string
86 taskid_re = re.compile('^Task +(.*): started$')
87 for line in cmd_raw.splitlines(True):
89 match = taskid_re.match(line)
95 def task_succeed(json_raw):
97 Parse JSON from rally JSON results
101 rally_report = json.loads(json_raw)
102 rally_report = rally_report[0]
103 if rally_report is None:
105 if rally_report.get('result') is None:
108 for result in rally_report.get('result'):
109 if len(result.get('error')) > 0:
116 the function dedicated to Tempest (functional tests for OpenStack)
117 :param test_mode: Tempest mode smoke (default), full, ..
120 logger.info('starting {} Tempest ...'.format(test_mode))
122 cmd_line = "rally verify start {}".format(test_mode)
123 logger.debug('running command line : {}'.format(cmd_line))
124 cmd = os.popen(cmd_line)
125 task_id = get_tempest_id(cmd.read())
126 logger.debug('task_id : {}'.format(task_id))
129 logger.error("failed to retrieve task_id")
132 """ check for result directory and create it otherwise """
133 if not os.path.exists(RESULTS_DIR):
134 logger.debug('does not exists, we create it'.format(RESULTS_DIR))
135 os.makedirs(RESULTS_DIR)
137 """ write log report file """
138 report_file_name = '{}opnfv-tempest.log'.format(RESULTS_DIR)
139 cmd_line = "rally verify detailed {} > {} ".format(task_id, report_file_name)
140 logger.debug('running command line : {}'.format(cmd_line))
144 def run_task(test_name):
146 the "main" function of the script who lunch rally for a task
147 :param test_name: name for the rally test
150 logger.info('starting {} test ...'.format(test_name))
152 """ check directory for scenarios test files or retrieve from git otherwise"""
154 test_file_name = '{}opnfv-{}.json'.format(SCENARIOS_DIR, test_name)
155 if not os.path.exists(test_file_name):
156 logger.error("The scenario '%s' does not exist." %test_file_name)
159 """ we do the test only if we have a scenario test file """
161 logger.debug('Scenario fetched from : {}'.format(test_file_name))
162 cmd_line = "rally task start --abort-on-sla-failure %s" % test_file_name
163 logger.debug('running command line : {}'.format(cmd_line))
164 cmd = os.popen(cmd_line)
165 task_id = get_task_id(cmd.read())
166 logger.debug('task_id : {}'.format(task_id))
169 logger.error("failed to retrieve task_id")
172 """ check for result directory and create it otherwise """
173 if not os.path.exists(RESULTS_DIR):
174 logger.debug('does not exists, we create it'.format(RESULTS_DIR))
175 os.makedirs(RESULTS_DIR)
177 """ write html report file """
178 report_file_name = '{}opnfv-{}.html'.format(RESULTS_DIR, test_name)
179 cmd_line = "rally task report %s --out %s" % (task_id, report_file_name)
180 logger.debug('running command line : {}'.format(cmd_line))
183 """ get and save rally operation JSON result """
184 cmd_line = "rally task results %s" % task_id
185 logger.debug('running command line : {}'.format(cmd_line))
186 cmd = os.popen(cmd_line)
187 json_results = cmd.read()
188 with open('{}opnfv-{}.json'.format(RESULTS_DIR, test_name), 'w') as f:
189 logger.debug('saving json file')
190 f.write(json_results)
191 logger.debug('saving json file2')
193 """ parse JSON operation result """
194 if task_succeed(json_results):
199 logger.error('{} test failed, unable to fetch a scenario test file'.format(test_name))
204 """ configure script """
205 if not (args.test_name in tests):
206 logger.error('argument not valid')
209 if args.test_name == "all":
210 for test_name in tests:
211 if not (test_name == 'all' or test_name == 'tempest' or test_name == 'heat' or test_name == 'smoke' or test_name == 'vm' ):
215 print(args.test_name)
216 if args.test_name == 'tempest':
219 run_task(args.test_name)
221 if __name__ == '__main__':