1 ##############################################################################
2 # Copyright (c) 2015 Orange
3 # guyrodrigue.koffi@orange.com / koffirodrigue@gmail.com
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from datetime import datetime
10 from datetime import timedelta
13 from bson import objectid
14 from tornado import web
16 from opnfv_testapi.resources import handlers
17 from opnfv_testapi.resources import result_models
18 from opnfv_testapi.tornado_swagger import swagger
21 class GenericResultHandler(handlers.GenericApiHandler):
22 def __init__(self, application, request, **kwargs):
23 super(GenericResultHandler, self).__init__(application,
26 self.table = self.db_results
27 self.table_cls = result_models.TestResult
29 def get_int(self, key, value):
33 raise web.HTTPError(httplib.BAD_REQUEST,
34 '{} must be int'.format(key))
39 for k in self.request.query_arguments.keys():
40 v = self.get_query_argument(k)
41 if k == 'project' or k == 'pod' or k == 'case':
42 query[k + '_name'] = v
44 v = self.get_int(k, v)
46 period = datetime.now() - timedelta(days=v)
47 obj = {"$gte": str(period)}
48 query['start_date'] = obj
49 elif k == 'trust_indicator':
50 query[k + '.current'] = float(v)
56 class ResultsCLHandler(GenericResultHandler):
57 @swagger.operation(nickname="queryTestResults")
60 @description: Retrieve result(s) for a test project
62 @notes: Retrieve result(s) for a test project on a specific pod.
63 Available filters for this request are :
64 - project : project name
67 - version : platform version (Arno-R1, ...)
68 - installer (fuel, ...)
69 - build_tag : Jenkins build tag name
70 - period : x (x last days)
71 - scenario : the test scenario (previously version)
72 - criteria : the global criteria status passed or failed
73 - trust_indicator : evaluate the stability of the test case
74 to avoid running systematically long and stable test case
76 GET /results/project=functest&case=vPing&version=Arno-R1 \
77 &pod=pod_name&period=15
78 @return 200: all test results consist with query,
79 empty list if no result is found
80 @rtype: L{TestResults}
85 @param project: project name
86 @type project: L{string}
88 @required project: False
89 @param case: case name
93 @param version: i.e. Colorado
94 @type version: L{string}
96 @required version: False
97 @param installer: fuel/apex/joid/compass
98 @type installer: L{string}
100 @required installer: False
101 @param build_tag: i.e. v3.0
102 @type build_tag: L{string}
104 @required build_tag: False
105 @param scenario: i.e. odl
106 @type scenario: L{string}
108 @required scenario: False
109 @param criteria: i.e. passed
110 @type criteria: L{string}
112 @required criteria: False
113 @param period: last days
114 @type period: L{string}
116 @required period: False
117 @param last: last records stored until now
118 @type last: L{string}
120 @required last: False
121 @param trust_indicator: must be float
122 @type trust_indicator: L{float}
123 @in trust_indicator: query
124 @required trust_indicator: False
126 last = self.get_query_argument('last', 0)
128 last = self.get_int('last', last)
130 self._list(self.set_query(), sort=[('start_date', -1)], last=last)
132 @swagger.operation(nickname="createTestResult")
135 @description: create a test result
136 @param body: result to be created
137 @type body: L{ResultCreateRequest}
139 @rtype: L{CreateResponse}
140 @return 200: result is created.
141 @raise 404: pod/project/testcase not exist
142 @raise 400: body/pod_name/project_name/case_name not provided
145 return {'name': data.pod_name}
148 message = 'Could not find pod [{}]'.format(data.pod_name)
149 return httplib.NOT_FOUND, message
151 def project_query(data):
152 return {'name': data.project_name}
154 def project_error(data):
155 message = 'Could not find project [{}]'.format(data.project_name)
156 return httplib.NOT_FOUND, message
158 def testcase_query(data):
159 return {'project_name': data.project_name, 'name': data.case_name}
161 def testcase_error(data):
162 message = 'Could not find testcase [{}] in project [{}]'\
163 .format(data.case_name, data.project_name)
164 return httplib.NOT_FOUND, message
166 miss_checks = ['pod_name', 'project_name', 'case_name']
167 db_checks = [('pods', True, pod_query, pod_error),
168 ('projects', True, project_query, project_error),
169 ('testcases', True, testcase_query, testcase_error)]
170 self._create(miss_checks, db_checks)
173 class ResultsGURHandler(GenericResultHandler):
174 @swagger.operation(nickname='getTestResultById')
175 def get(self, result_id):
177 @description: get a single result by result_id
178 @rtype: L{TestResult}
179 @return 200: test result exist
180 @raise 404: test result not exist
183 query["_id"] = objectid.ObjectId(result_id)
186 @swagger.operation(nickname="updateTestResultById")
187 def put(self, result_id):
189 @description: update a single result by _id
190 @param body: fields to be updated
191 @type body: L{ResultUpdateRequest}
194 @return 200: update success
195 @raise 404: result not exist
196 @raise 403: nothing to update
198 query = {'_id': objectid.ObjectId(result_id)}
200 self._update(query, db_keys)