JIRA:FUNCTEST-1 -run_rally: added cinder as input parameter and other minor changes
[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', 'tempest', 'vm', 'all']
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]} ]. 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 args = parser.parse_args()
24
25 """ logging configuration """
26 logger = logging.getLogger('run_rally')
27 logger.setLevel(logging.DEBUG)
28
29 ch = logging.StreamHandler()
30 if args.debug:
31     ch.setLevel(logging.DEBUG)
32 else:
33     ch.setLevel(logging.INFO)
34
35 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
36 ch.setFormatter(formatter)
37 logger.addHandler(ch)
38
39
40 def get_task_id(cmd_raw):
41     """
42     get task id from command rally result
43     :param cmd_raw:
44     :return: task_id as string
45     """
46     taskid_re = re.compile('^Task +(.*): started$')
47     for line in cmd_raw.splitlines(True):
48         line = line.strip()
49         match = taskid_re.match(line)
50         if match:
51             return match.group(1)
52     return None
53
54
55 def task_succeed(json_raw):
56     """
57     Parse JSON from rally JSON results
58     :param json_raw:
59     :return: Bool
60     """
61     rally_report = json.loads(json_raw)
62     rally_report = rally_report[0]
63     if rally_report is None:
64         return False
65     if rally_report.get('result') is None:
66         return False
67
68     for result in rally_report.get('result'):
69         if len(result.get('error')) > 0:
70             return False
71
72     return True
73
74
75 def run_task(test_name):
76     """
77     the "main" function of the script who lunch rally for a task
78     :param test_name: name for the rally test
79     :return: void
80     """
81     logger.info('starting {} test ...'.format(test_name))
82
83     """ get the date """
84     cmd = os.popen("date '+%d%m%Y_%H%M'")
85     test_date = cmd.read().rstrip()
86  
87     """ check directory for scenarios test files or retrieve from git otherwise"""
88     proceed_test = True
89     tests_path = "./scenarios"
90     test_file_name = '{}/opnfv-{}.json'.format(tests_path, test_name)
91     if not os.path.exists(test_file_name):
92         logger.debug('{} does not exists'.format(test_file_name))
93         proceed_test = retrieve_test_cases_file(test_name, tests_path)
94  
95     """ we do the test only if we have a scenario test file """
96     if proceed_test:
97         logger.debug('successfully downloaded to : {}'.format(test_file_name))
98         cmd_line = "rally task start --abort-on-sla-failure %s" % test_file_name
99         logger.debug('running command line : {}'.format(cmd_line))
100         cmd = os.popen(cmd_line)
101         task_id = get_task_id(cmd.read())
102         logger.debug('task_id : {}'.format(task_id))
103  
104         if task_id is None:
105             logger.error("failed to retrieve task_id")
106             exit(-1)
107  
108         """ check for result directory and create it otherwise """
109         report_path = "./results"
110         if not os.path.exists(report_path):
111             logger.debug('does not exists, we create it'.format(report_path))
112             os.makedirs(report_path)
113
114         """ write html report file """
115         report_file_name = '{}/opnfv-{}-{}.html'.format(report_path, test_name, test_date)
116         cmd_line = "rally task report %s --out %s" % (task_id, report_file_name)
117         logger.debug('running command line : {}'.format(cmd_line))
118         os.popen(cmd_line)
119
120         """ get and save rally operation JSON result """
121         cmd_line = "rally task results %s" % task_id
122         logger.debug('running command line : {}'.format(cmd_line))
123         cmd = os.popen(cmd_line)
124         json_results = cmd.read()
125         with open('{}/opnfv-{}-{}.json'.format(report_path, test_name, test_date), 'w') as f:
126             logger.debug('saving json file')
127             f.write(json_results)
128             logger.debug('saving json file2')
129  
130         """ parse JSON operation result """
131         if task_succeed(json_results):
132             print '{} OK'.format(test_date)
133         else:
134             print '{} KO'.format(test_date)
135     else:
136         logger.error('{} test failed, unable to download a scenario test file'.format(test_name))
137  
138  
139 def retrieve_test_cases_file(test_name, tests_path):
140     """
141     Retrieve from github the sample test files
142     :return: Boolean that indicates the retrieval status
143     """
144  
145     """ do not add the "/" at the end """
146     url_base = "https://git.opnfv.org/cgit/functest/plain/testcases/VIM/OpenStack/CI/suites"
147
148     test_file_name = 'opnfv-{}.json'.format(test_name)
149     logger.info('fetching {}/{} ...'.format(url_base, test_file_name))
150
151     try:
152         response = urllib2.urlopen('{}/{}'.format(url_base, test_file_name))
153     except (urllib2.HTTPError, urllib2.URLError):
154         return False
155     file_raw = response.read()
156
157     """ check if the test path exist otherwise we create it """
158     if not os.path.exists(tests_path):
159         os.makedirs(tests_path)
160
161     with open('{}/{}'.format(tests_path, test_file_name), 'w') as f:
162         f.write(file_raw)
163     return True
164
165
166 def main():
167     """ configure script """
168     if not (args.test_name in tests):
169         logger.error('argument not valid')
170         exit(-1)
171
172     if args.test_name == "all":
173         for test_name in tests:
174             if not (test_name == 'all' or test_name == 'tempest'):
175                 print(test_name)
176                 run_task(test_name)
177     else:
178         run_task(args.test_name)
179
180 if __name__ == '__main__':
181     main()