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, sys, urllib2
13 def get_task_id(cmd_raw):
15 get task id from command rally result
17 :return: task_id as string
19 taskid_re = re.compile('^Task +(.*): started$')
20 for line in cmd_raw.splitlines(True):
22 match = taskid_re.match(line)
27 def task_succeed(json_raw):
29 Parse JSON from rally JSON results
33 rally_report = json.loads(json_raw)
34 rally_report = rally_report[0]
35 if rally_report is None:
37 if rally_report.get('result') is None:
40 for result in rally_report.get('result'):
41 if len(result.get('error')) > 0:
46 def run_task(test_name):
48 the "main" function of the script who lunch rally for a task
49 :param test_name: name for the rally test
54 cmd = os.popen("date '+%d%m%Y_%H%M'")
55 test_date = cmd.read().rstrip()
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"
65 cmd = os.popen("rally task start --abort-on-sla-failure %s" % test_file_name)
66 task_id = get_task_id(cmd.read())
69 print "./run_rally : failed to retrieve task_id"
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)
77 report_file_name = '{}/opnfv-{}-{}.html'.format(report_path, test_name, test_date)
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()):
87 def retrieve_test_cases_file(test_name, tests_path):
89 Retrieve from github the sample test files
93 """ do not add the "/" at the end """
94 url_base = "https://git.opnfv.org/cgit/functest/plain/testcases/VIM/OpenStack/CI/suites"
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()
101 """ check if the test path existe otherwise we create it """
102 if not os.path.exists(tests_path):
103 os.makedirs(tests_path)
105 with open('{}/{}'.format(tests_path,test_file_name), 'w') as file:
110 """ configure script """
111 tests = ['authenticate','glance','heat','keystone','neutron','nova','tempest','vm', 'all'];
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, "]"
118 test_name = sys.argv[1]
120 if not (test_name in tests):
121 print "argument not valid"
124 if test_name == "all":
125 for test_name in tests:
126 if not (test_name == 'all' or test_name == 'tempest'):
134 if __name__ == '__main__':