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
12 from bson import objectid
14 from opnfv_testapi.common import config
15 from opnfv_testapi.common import message
16 from opnfv_testapi.common import raises
17 from opnfv_testapi.resources import handlers
18 from opnfv_testapi.resources import result_models
19 from opnfv_testapi.tornado_swagger import swagger
21 CONF = config.Config()
24 class GenericResultHandler(handlers.GenericApiHandler):
25 def __init__(self, application, request, **kwargs):
26 super(GenericResultHandler, self).__init__(application,
29 self.table = self.db_results
30 self.table_cls = result_models.TestResult
32 def get_int(self, key, value):
36 raises.BadRequest(message.must_int(key))
43 for k in self.request.query_arguments.keys():
44 v = self.get_query_argument(k)
45 if k == 'project' or k == 'pod' or k == 'case':
46 query[k + '_name'] = v
48 v = self.get_int(k, v)
50 period = datetime.now() - timedelta(days=v)
51 obj = {"$gte": str(period)}
52 query['start_date'] = obj
53 elif k == 'trust_indicator':
54 query[k + '.current'] = float(v)
56 date_range.update({'$gte': str(v)})
58 date_range.update({'$lt': str(v)})
59 elif k != 'last' and k != 'page':
62 query['start_date'] = date_range
64 # if $lt is not provided,
65 # empty/None/null/'' start_date will also be returned
66 if 'start_date' in query and '$lt' not in query['start_date']:
67 query['start_date'].update({'$lt': str(datetime.now())})
72 class ResultsCLHandler(GenericResultHandler):
73 @swagger.operation(nickname="queryTestResults")
76 @description: Retrieve result(s) for a test project
78 @notes: Retrieve result(s) for a test project on a specific pod.
79 Available filters for this request are :
80 - project : project name
83 - version : platform version (Arno-R1, ...)
84 - installer : fuel/apex/compass/joid/daisy
85 - build_tag : Jenkins build tag name
86 - period : x last days, incompatible with from/to
87 - from : starting time in 2016-01-01 or 2016-01-01 00:01:23
88 - to : ending time in 2016-01-01 or 2016-01-01 00:01:23
89 - scenario : the test scenario (previously version)
90 - criteria : the global criteria status passed or failed
91 - trust_indicator : evaluate the stability of the test case
92 to avoid running systematically long and stable test case
94 GET /results/project=functest&case=vPing&version=Arno-R1 \
95 &pod=pod_name&period=15
96 @return 200: all test results consist with query,
97 empty list if no result is found
98 @rtype: L{TestResults}
103 @param project: project name
104 @type project: L{string}
106 @required project: False
107 @param case: case name
108 @type case: L{string}
110 @required case: False
111 @param version: i.e. Colorado
112 @type version: L{string}
114 @required version: False
115 @param installer: fuel/apex/joid/compass
116 @type installer: L{string}
118 @required installer: False
119 @param build_tag: i.e. v3.0
120 @type build_tag: L{string}
122 @required build_tag: False
123 @param scenario: i.e. odl
124 @type scenario: L{string}
126 @required scenario: False
127 @param criteria: i.e. passed
128 @type criteria: L{string}
130 @required criteria: False
131 @param period: last days
132 @type period: L{string}
134 @required period: False
135 @param from: i.e. 2016-01-01 or 2016-01-01 00:01:23
136 @type from: L{string}
138 @required from: False
139 @param to: i.e. 2016-01-01 or 2016-01-01 00:01:23
143 @param last: last records stored until now
144 @type last: L{string}
146 @required last: False
147 @param page: which page to list
150 @required page: False
151 @param trust_indicator: must be float
152 @type trust_indicator: L{float}
153 @in trust_indicator: query
154 @required trust_indicator: False
156 limitations = {'sort': {'_id': -1}}
157 last = self.get_query_argument('last', 0)
159 last = self.get_int('last', last)
160 limitations.update({'last': last})
162 page = self.get_query_argument('page', None)
164 page = self.get_int('page', page)
165 limitations.update({'page': page,
166 'per_page': CONF.api_results_per_page})
168 self._list(query=self.set_query(), **limitations)
170 @swagger.operation(nickname="createTestResult")
173 @description: create a test result
174 @param body: result to be created
175 @type body: L{ResultCreateRequest}
177 @rtype: L{CreateResponse}
178 @return 200: result is created.
179 @raise 404: pod/project/testcase not exist
180 @raise 400: body/pod_name/project_name/case_name not provided
183 return {'name': self.json_args.get('pod_name')}
186 return {'name': self.json_args.get('project_name')}
188 def testcase_query():
189 return {'project_name': self.json_args.get('project_name'),
190 'name': self.json_args.get('case_name')}
192 miss_fields = ['pod_name', 'project_name', 'case_name']
193 carriers = [('pods', pod_query),
194 ('projects', project_query),
195 ('testcases', testcase_query)]
196 self._create(miss_fields=miss_fields, carriers=carriers)
199 class ResultsGURHandler(GenericResultHandler):
200 @swagger.operation(nickname='getTestResultById')
201 def get(self, result_id):
203 @description: get a single result by result_id
204 @rtype: L{TestResult}
205 @return 200: test result exist
206 @raise 404: test result not exist
209 query["_id"] = objectid.ObjectId(result_id)
210 self._get_one(query=query)
212 @swagger.operation(nickname="updateTestResultById")
213 def put(self, result_id):
215 @description: update a single result by _id
216 @param body: fields to be updated
217 @type body: L{ResultUpdateRequest}
220 @return 200: update success
221 @raise 404: result not exist
222 @raise 403: nothing to update
224 query = {'_id': objectid.ObjectId(result_id)}
226 self._update(query=query, db_keys=db_keys)