script to launch Rally bench scenario per module or all one after the other from CI
[functest.git] / testcases / VIM / OpenStack / CI / libraries / run_rally.py
1 #
2 # Copyright (c) 2015 Orange
3 # guyrodrigue.koffi@orange.com
4 # morgan.richomme@orange.com
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #!/usr/bin/python
11 import re, json, os, sys
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
47 def run_task(test_name):
48     """
49     the "main" function of the script who lunch rally for a task
50     :param test_name: name for the rally test
51     :return: void
52     """
53
54     """ get the date """
55     cmd = os.popen("date '+%d%m%Y_%H%M'")
56     test_date = cmd.read().rstrip()
57
58     """ directory for test scenarios files"""
59     test_dir = '/home/ubuntu/rally/samples/tasks/scenarios/opnfv'
60     test_file_name = "/home/ubuntu/rally/samples/tasks/scenarios/opnfv/opnfv-%s.json" % test_name
61     print test_file_name
62
63     cmd = os.popen("rally task start --abort-on-sla-failure %s" % test_file_name)
64     task_id = get_task_id(cmd.read())
65
66     if task_id is None:
67         print "./run_rally : failed to retrieve task_id"
68         exit(-1)
69
70     report_file_name = "/home/ubuntu/rally/opnfv-%s-%s.html" % (test_name, test_date)
71     
72     os.popen("rally task report %s --out %s" % (task_id, report_file_name))
73     cmd = os.popen("rally task results %s" % task_id)
74     if task_succeed(cmd.read()):
75         print "OK"
76     else:
77         print "KO"
78
79
80 def main():
81     """ configure script """
82     tests = ('authenticate','glance','heat','keystone','neutron','nova','tempest','vm', 'all',)
83
84
85     if len(sys.argv) != 2:
86         print "./run_rally [", tests, "]"
87         exit(-1)
88     test_name = sys.argv[1]
89
90     if not (test_name in tests):
91         print "argument not valid"
92         exit(-1)
93
94     if test_name == "all":
95         #run test for all tests
96         pass
97     else:
98         run_task(test_name)
99
100
101 if __name__ == '__main__':
102     main()
103
104