da5e6adca9c2bff25a03f9468affcf74f836eaf8
[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, sys, urllib2
12  
13 def get_task_id(cmd_raw):
14     """
15     get task id from command rally result
16     :param cmd_raw:
17     :return: task_id as string
18     """
19     taskid_re = re.compile('^Task +(.*): started$')
20     for line in cmd_raw.splitlines(True):
21         line = line.strip()
22         match = taskid_re.match(line)
23         if match:
24             return match.group(1)
25     return None
26  
27 def task_succeed(json_raw):
28     """
29     Parse JSON from rally JSON results
30     :param json_raw:
31     :return: Bool
32     """
33     rally_report = json.loads(json_raw)
34     rally_report = rally_report[0]
35     if rally_report is None:
36         return False
37     if rally_report.get('result') is None:
38         return False
39  
40     for result in rally_report.get('result'):
41         if len(result.get('error')) > 0:
42             return False
43  
44     return True
45  
46 def run_task(test_name):
47     """
48     the "main" function of the script who lunch rally for a task
49     :param test_name: name for the rally test
50     :return: void
51     """
52  
53     """ get the date """
54     cmd = os.popen("date '+%d%m%Y_%H%M'")
55     test_date = cmd.read().rstrip()
56  
57     """ check directory for test scenarios files or retrieve from git otherwise"""
58     tests_path = "./scenarios"
59     test_file_name = '{}/opnfv-{}.json'.format(tests_path, test_name)
60     if not os.path.exists(test_file_name):
61         retrieve_test_cases_file(test_name, tests_path)
62         print "Scenario successfully downloaded"
63  
64     print "Start test..."
65     cmd = os.popen("rally task start --abort-on-sla-failure %s" % test_file_name)
66     task_id = get_task_id(cmd.read())
67  
68     if task_id is None:
69         print "./run_rally : failed to retrieve task_id"
70         exit(-1)
71  
72     """ check for result directory and create it otherwise """
73     report_path = "./results"
74     if not os.path.exists(report_path):
75         os.makedirs(report_path)
76  
77     report_file_name = '{}/opnfv-{}-{}.html'.format(report_path, test_name, test_date)
78     
79     os.popen("rally task report %s --out %s" % (task_id, report_file_name))
80     cmd = os.popen("rally task results %s" % task_id)
81     if task_succeed(cmd.read()):
82         print "OK"
83     else:
84         print "KO"
85  
86  
87 def retrieve_test_cases_file(test_name, tests_path):
88     """
89     Retrieve from github the sample test files
90     :return: void
91     """
92  
93     """ do not add the "/" at the end """
94     url_base = "https://git.opnfv.org/cgit/functest/plain/testcases/VIM/OpenStack/CI/suites"
95  
96     test_file_name = 'opnfv-{}.json'.format(test_name)
97     print 'fetching {}/{} ...'.format(url_base, test_file_name)
98     response = urllib2.urlopen('{}/{}'.format(url_base, test_file_name))
99     file_raw = response.read()
100  
101     """ check if the test path existe otherwise we create it """
102     if not os.path.exists(tests_path):
103         os.makedirs(tests_path)
104  
105     with open('{}/{}'.format(tests_path,test_file_name), 'w') as file:
106         file.write(file_raw)
107  
108  
109 def main():
110     """ configure script """
111     tests = ['authenticate','glance','heat','keystone','neutron','nova','tempest','vm', 'all'];
112  
113  
114     if len(sys.argv) != 2:
115         options = '{d[0]} | {d[1]} | {d[2]} | {d[3]} | {d[4]} | {d[5]} | {d[6]} | {d[7]} | {d[8]}'.format(d=tests)
116         print "./run_rally [", options, "]"
117         exit(-1)
118     test_name = sys.argv[1]
119  
120     if not (test_name in tests):
121         print "argument not valid"
122         exit(-1)
123  
124     if test_name == "all":
125         for test_name in tests:
126             if not (test_name == 'all' or test_name == 'tempest'):
127                 print(test_name)
128                 run_task(test_name)
129  
130     else:
131         run_task(test_name)
132  
133  
134 if __name__ == '__main__':
135     main()
136