Added smoke bench scenarios: complete smoke and green list of working tests.
[functest.git] / testcases / VIM / OpenStack / CI / libraries / run_rally.py
1 #!/usr/bin/env python
2 #
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
10 #
11 import re, json, os, urllib2, argparse, logging
12
13 """ tests configuration """
14 tests = ['authenticate', 'glance', 'cinder', 'heat', 'keystone', 'neutron', 'nova', 'quotas', 'requests', 'tempest', 'vm', 'all', 'smoke']
15 parser = argparse.ArgumentParser()
16 parser.add_argument("test_name", help="The name of the test you want to perform with rally. "
17                                       "Possible values are : "
18                                       "[ {d[0]} | {d[1]} | {d[2]} | {d[3]} | {d[4]} | {d[5]} | {d[6]} "
19                                       "| {d[7]} | {d[8]} | {d[9]} | {d[10]} | {d[11]} | {d[12]}]. The 'all' value performs all the tests scenarios "
20                                       "except 'tempest'".format(d=tests))
21
22 parser.add_argument("-d", "--debug", help="Debug mode",  action="store_true")
23 parser.add_argument("test_mode", help="Tempest test mode", nargs='?', default="smoke")
24 args = parser.parse_args()
25 test_mode=args.test_mode
26
27 if not args.test_name == "tempest":
28     if not args.test_mode == "smoke":
29         parser.error("test_mode is only used with tempest")
30
31 """ logging configuration """
32 logger = logging.getLogger('run_rally')
33 logger.setLevel(logging.DEBUG)
34
35 ch = logging.StreamHandler()
36 if args.debug:
37     ch.setLevel(logging.DEBUG)
38 else:
39     ch.setLevel(logging.INFO)
40
41 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
42 ch.setFormatter(formatter)
43 logger.addHandler(ch)
44
45
46 def get_tempest_id(cmd_raw):
47     """
48     get task id from command rally result
49     :param cmd_raw:
50     :return: task_id as string
51     """
52     taskid_re = re.compile('^Verification UUID: (.*)$')
53     for line in cmd_raw.splitlines(True):
54         line = line.strip()
55     match = taskid_re.match(line)
56
57     if match:
58                 return match.group(1)
59     return None
60
61 def get_task_id(cmd_raw):
62     """
63     get task id from command rally result
64     :param cmd_raw:
65     :return: task_id as string
66     """
67     taskid_re = re.compile('^Task +(.*): started$')
68     for line in cmd_raw.splitlines(True):
69         line = line.strip()
70         match = taskid_re.match(line)
71         if match:
72             return match.group(1)
73     return None
74
75
76 def task_succeed(json_raw):
77     """
78     Parse JSON from rally JSON results
79     :param json_raw:
80     :return: Bool
81     """
82     rally_report = json.loads(json_raw)
83     rally_report = rally_report[0]
84     if rally_report is None:
85         return False
86     if rally_report.get('result') is None:
87         return False
88
89     for result in rally_report.get('result'):
90         if len(result.get('errors')) > 0:
91             return False
92
93     return True
94
95 def run_tempest():
96     """
97     the function dedicated to Tempest (functional tests for OpenStack)
98     :param test_mode: Tempest mode smoke (default), full, ..
99     :return: void
100     """
101     logger.info('starting {} Tempest ...'.format(test_mode))
102
103     """ get the date """
104     cmd = os.popen("date '+%d%m%Y_%H%M'")
105     test_date = cmd.read().rstrip()
106
107     cmd_line = "rally verify start {}".format(test_mode)
108     logger.debug('running command line : {}'.format(cmd_line))
109     cmd = os.popen(cmd_line)
110     task_id = get_tempest_id(cmd.read())
111     logger.debug('task_id : {}'.format(task_id))
112
113     if task_id is None:
114                 logger.error("failed to retrieve task_id")
115                 exit(-1)
116
117     """ check for result directory and create it otherwise """
118     report_path = "./results"
119     if not os.path.exists(report_path):
120         logger.debug('does not exists, we create it'.format(report_path))
121         os.makedirs(report_path)
122
123     """ write log report file """
124     report_file_name = '{}/opnfv-tempest-{}.log'.format(report_path, test_date)
125     cmd_line = "rally verify detailed {} > {} ".format(task_id, report_file_name)
126     logger.debug('running command line : {}'.format(cmd_line))
127     os.popen(cmd_line)
128
129
130 def run_task(test_name):
131     """
132     the "main" function of the script who lunch rally for a task
133     :param test_name: name for the rally test
134     :return: void
135     """
136     logger.info('starting {} test ...'.format(test_name))
137
138     """ get the date """
139     cmd = os.popen("date '+%d%m%Y_%H%M'")
140     test_date = cmd.read().rstrip()
141
142     """ check directory for scenarios test files or retrieve from git otherwise"""
143     proceed_test = True
144     tests_path = "./scenarios"
145     test_file_name = '{}/opnfv-{}.json'.format(tests_path, test_name)
146     if not os.path.exists(test_file_name):
147         logger.debug('{} does not exists'.format(test_file_name))
148         proceed_test = retrieve_test_cases_file(test_name, tests_path)
149
150     """ we do the test only if we have a scenario test file """
151     if proceed_test:
152         logger.debug('successfully downloaded to : {}'.format(test_file_name))
153         cmd_line = "rally task start --abort-on-sla-failure %s" % test_file_name
154         logger.debug('running command line : {}'.format(cmd_line))
155         cmd = os.popen(cmd_line)
156         task_id = get_task_id(cmd.read())
157         logger.debug('task_id : {}'.format(task_id))
158
159         if task_id is None:
160             logger.error("failed to retrieve task_id")
161             exit(-1)
162
163         """ check for result directory and create it otherwise """
164         report_path = "./results"
165         if not os.path.exists(report_path):
166             logger.debug('does not exists, we create it'.format(report_path))
167             os.makedirs(report_path)
168
169         """ write html report file """
170         report_file_name = '{}/opnfv-{}-{}.html'.format(report_path, test_name, test_date)
171         cmd_line = "rally task report %s --out %s" % (task_id, report_file_name)
172         logger.debug('running command line : {}'.format(cmd_line))
173         os.popen(cmd_line)
174
175         """ get and save rally operation JSON result """
176         cmd_line = "rally task results %s" % task_id
177         logger.debug('running command line : {}'.format(cmd_line))
178         cmd = os.popen(cmd_line)
179         json_results = cmd.read()
180         with open('{}/opnfv-{}-{}.json'.format(report_path, test_name, test_date), 'w') as f:
181             logger.debug('saving json file')
182             f.write(json_results)
183             logger.debug('saving json file2')
184
185         """ parse JSON operation result """
186         if task_succeed(json_results):
187             print 'Test OK'
188         else:
189             print 'Test KO'
190     else:
191         logger.error('{} test failed, unable to download a scenario test file'.format(test_name))
192
193
194 def retrieve_test_cases_file(test_name, tests_path):
195     """
196     Retrieve from github the sample test files
197     :return: Boolean that indicates the retrieval status
198     """
199
200     """ do not add the "/" at the end """
201     url_base = "https://git.opnfv.org/cgit/functest/plain/testcases/VIM/OpenStack/CI/suites"
202
203     test_file_name = 'opnfv-{}.json'.format(test_name)
204     logger.info('fetching {}/{} ...'.format(url_base, test_file_name))
205
206     try:
207         response = urllib2.urlopen('{}/{}'.format(url_base, test_file_name))
208     except (urllib2.HTTPError, urllib2.URLError):
209         return False
210     file_raw = response.read()
211
212     """ check if the test path exist otherwise we create it """
213     if not os.path.exists(tests_path):
214         os.makedirs(tests_path)
215
216     with open('{}/{}'.format(tests_path, test_file_name), 'w') as f:
217         f.write(file_raw)
218     return True
219
220
221 def main():
222     """ configure script """
223     if not (args.test_name in tests):
224         logger.error('argument not valid')
225         exit(-1)
226
227     if args.test_name == "all":
228         for test_name in tests:
229             if not (test_name == 'all' or test_name == 'tempest'):
230                 print(test_name)
231                 run_task(test_name)
232     else:
233         print(args.test_name)
234         if args.test_name == 'tempest':
235                         run_tempest()
236         else:
237             run_task(args.test_name)
238
239 if __name__ == '__main__':
240     main()