Add automatic status reporting
[releng.git] / utils / test / reporting / reporting-status.py
1 from urllib2 import Request, urlopen, URLError
2 import urllib2
3 import json
4 import jinja2
5 import os
6 import random
7
8
9 class TestCase(object):
10     def __init__(self, name, project, criteria=-1):
11         self.name = name
12         self.project = project
13         self.criteria = criteria
14
15     def getName(self):
16         return self.name
17
18     def getProject(self):
19         return self.project
20
21     def getCriteria(self):
22         return self.criteria
23
24     def setCriteria(self, criteria):
25         self.criteria = criteria
26
27
28 def getApiResults(case, installer):
29     case = case.getName()
30
31     # to remove proxy (to be removed at the end for local test only)
32     # proxy_handler = urllib2.ProxyHandler({})
33     # opener = urllib2.build_opener(proxy_handler)
34     # urllib2.install_opener(opener)
35     url = "http://testresults.opnfv.org/testapi/results?case=" + case + "&period=30&installer=" + installer
36     #url = "http://127.0.0.1:8000/results?case=" + case + "&period=30&installer=" + installer
37     request = Request(url)
38
39     try:
40         response = urlopen(request)
41         k = response.read()
42         results = json.loads(k)
43     except URLError, e:
44         print 'No kittez. Got an error code:', e
45
46     return results
47
48
49 def getScenarios(case, installer):
50
51     results = getApiResults(case, installer)
52     test_results = results['test_results']
53
54     if test_results is not None:
55         test_results.reverse()
56
57         scenario_results = {}
58
59         for r in test_results:
60             # Retrieve all the scenarios per installer
61             if not r['version'] in scenario_results.keys():
62                 scenario_results[r['version']] = []
63             scenario_results[r['version']].append(r)
64
65     return scenario_results
66
67
68 def getScenarioStats(scenario_results):
69     scenario_stats = {}
70     for k, v in scenario_results.iteritems():
71         scenario_stats[k] = len(v)
72
73     return scenario_stats
74
75
76 def getResult(testCase, installer):
77
78     # retrieve raw results
79     results = getApiResults(testCase, installer)
80     # let's concentrate on test results only
81     test_results = results['test_results']
82
83     # if results found, analyze them
84     if test_results is not None:
85         test_results.reverse()
86
87         scenario_results = {}
88
89         for r in test_results:
90             if not r['version'] in scenario_results.keys():
91                 scenario_results[r['version']] = []
92             scenario_results[r['version']].append(r)
93
94         for s, s_result in scenario_results.items():
95             scenario_results[s] = s_result[0:5]
96             # For each scenario, we build a result object to deal with
97             # results, criteria and error handling
98             for result in scenario_results[s]:
99                 result["creation_date"] = result["creation_date"].split(".")[0]
100
101                 # Cannot be fully generic
102                 # need to look for specific criteria case by case
103                 # TODO add a criteria passed/failed in DB??
104                 # TODO result["Success_criteria"] = result["success_criteria"]
105                 # meanwhile just random....
106                 # and consider the last random arbitrarily
107                 # 4 levels for the results
108                 # 3: 4+ consecutive runs passing the success criteria
109                 # 2: <4 successful consecutive runs but passing the criteria
110                 # 1: close to pass the success criteria
111                 # 0: 0% success, not passing
112                 #
113
114     return int(random.random()*4)+1
115
116 # ******************************************************************************
117 # ******************************************************************************
118 # ******************************************************************************
119 # ******************************************************************************
120 # ******************************************************************************
121
122 # as the criteria are all difference, we shall use a common way to indicate
123 # the criteria
124 # 100 = 100% = all the test must be OK
125 # 90 = 90% = all the test must be above 90% of success rate
126 # TODO harmonize success criteria
127 # some criteria could be the duration, the success rate, the packet loss,...
128 # to be done case by case
129 # TODo create TestCriteria Object
130
131
132 installers = ["apex", "compass", "fuel", "joid"]
133 # init just tempest to get the scenario as all the scenarios run Temepst
134 tempest = TestCase("Tempest", "functest", -1)
135
136 for installer in installers:
137
138     scenario_results = getScenarios(tempest, installer)
139     scenario_stats = getScenarioStats(scenario_results)
140
141     items = {}
142
143     for s, s_result in scenario_results.items():
144
145         vPing = TestCase("vPing", "functest")
146         vPing_userdata = TestCase("vPing_userdata", "functest")
147         tempest = TestCase("Tempest", "functest")
148         rally = TestCase("Rally", "functest")
149         odl = TestCase("ODL", "functest")
150         onos = TestCase("ONOS", "functest")
151         ovno = TestCase("OVNO", "functest")
152         vIMS = TestCase("vIMS", "functest")
153         doctor = TestCase("doctor-notification", "doctor")
154         promise = TestCase("promise", "promise")
155         odl_vpn = TestCase("ODL VPN Service tests", "sdnvpn")
156         bgpvpn_api = TestCase("OpenStack Neutron BGPVPN API extension tests",
157                               "sdnvpn")
158         testCases = [vPing, vPing_userdata, tempest, rally, odl, onos, vIMS,
159                      doctor, promise]
160
161         for testCase in testCases:
162             result = getResult(testCase, installer)
163             testCase.setCriteria(result)
164             # print "case %s (%s) = %s " % (testCase.getName(), s, result)
165         items[s] = testCases
166
167     templateLoader = jinja2.FileSystemLoader(os.path.dirname(os.path.abspath(__file__)))
168     templateEnv = jinja2.Environment(loader=templateLoader)
169
170     TEMPLATE_FILE = "index-status-tmpl.html"
171     template = templateEnv.get_template(TEMPLATE_FILE)
172
173     outputText = template.render(scenario_stats=scenario_stats,
174                                  items=items,
175                                  installer=installer)
176
177     with open("index-status-" + installer + ".html", "wb") as fh:
178         fh.write(outputText)