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 message
15 from opnfv_testapi.common import raises
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 raises.BadRequest(message.must_int(key))
40 for k in self.request.query_arguments.keys():
41 v = self.get_query_argument(k)
42 if k == 'project' or k == 'pod' or k == 'case':
43 query[k + '_name'] = v
45 v = self.get_int(k, v)
47 period = datetime.now() - timedelta(days=v)
48 obj = {"$gte": str(period)}
49 query['start_date'] = obj
50 elif k == 'trust_indicator':
51 query[k + '.current'] = float(v)
53 date_range.update({'$gte': str(v)})
55 date_range.update({'$lt': str(v)})
56 elif k != 'last' and k != 'page':
59 query['start_date'] = date_range
63 class ResultsCLHandler(GenericResultHandler):
64 @swagger.operation(nickname="queryTestResults")
67 @description: Retrieve result(s) for a test project
69 @notes: Retrieve result(s) for a test project on a specific pod.
70 Available filters for this request are :
71 - project : project name
74 - version : platform version (Arno-R1, ...)
75 - installer : fuel/apex/compass/joid/daisy
76 - build_tag : Jenkins build tag name
77 - period : x last days, incompatible with from/to
78 - from : starting time in 2016-01-01 or 2016-01-01 00:01:23
79 - to : ending time in 2016-01-01 or 2016-01-01 00:01:23
80 - scenario : the test scenario (previously version)
81 - criteria : the global criteria status passed or failed
82 - trust_indicator : evaluate the stability of the test case
83 to avoid running systematically long and stable test case
85 GET /results/project=functest&case=vPing&version=Arno-R1 \
86 &pod=pod_name&period=15
87 @return 200: all test results consist with query,
88 empty list if no result is found
89 @rtype: L{TestResults}
94 @param project: project name
95 @type project: L{string}
97 @required project: False
98 @param case: case name
101 @required case: False
102 @param version: i.e. Colorado
103 @type version: L{string}
105 @required version: False
106 @param installer: fuel/apex/joid/compass
107 @type installer: L{string}
109 @required installer: False
110 @param build_tag: i.e. v3.0
111 @type build_tag: L{string}
113 @required build_tag: False
114 @param scenario: i.e. odl
115 @type scenario: L{string}
117 @required scenario: False
118 @param criteria: i.e. passed
119 @type criteria: L{string}
121 @required criteria: False
122 @param period: last days
123 @type period: L{string}
125 @required period: False
126 @param from: i.e. 2016-01-01 or 2016-01-01 00:01:23
127 @type from: L{string}
129 @required from: False
130 @param to: i.e. 2016-01-01 or 2016-01-01 00:01:23
134 @param last: last records stored until now
135 @type last: L{string}
137 @required last: False
138 @param trust_indicator: must be float
139 @type trust_indicator: L{float}
140 @in trust_indicator: query
141 @required trust_indicator: False
143 last = self.get_query_argument('last', 0)
145 last = self.get_int('last', last)
147 page = self.get_query_argument('page', 0)
151 self._list(query=self.set_query(),
152 sort=[('start_date', -1)],
155 @swagger.operation(nickname="createTestResult")
158 @description: create a test result
159 @param body: result to be created
160 @type body: L{ResultCreateRequest}
162 @rtype: L{CreateResponse}
163 @return 200: result is created.
164 @raise 404: pod/project/testcase not exist
165 @raise 400: body/pod_name/project_name/case_name not provided
168 return {'name': self.json_args.get('pod_name')}
171 return {'name': self.json_args.get('project_name')}
173 def testcase_query():
174 return {'project_name': self.json_args.get('project_name'),
175 'name': self.json_args.get('case_name')}
177 miss_fields = ['pod_name', 'project_name', 'case_name']
178 carriers = [('pods', pod_query),
179 ('projects', project_query),
180 ('testcases', testcase_query)]
181 self._create(miss_fields=miss_fields, carriers=carriers)
184 class ResultsGURHandler(GenericResultHandler):
185 @swagger.operation(nickname='getTestResultById')
186 def get(self, result_id):
188 @description: get a single result by result_id
189 @rtype: L{TestResult}
190 @return 200: test result exist
191 @raise 404: test result not exist
194 query["_id"] = objectid.ObjectId(result_id)
195 self._get_one(query=query)
197 @swagger.operation(nickname="updateTestResultById")
198 def put(self, result_id):
200 @description: update a single result by _id
201 @param body: fields to be updated
202 @type body: L{ResultUpdateRequest}
205 @return 200: update success
206 @raise 404: result not exist
207 @raise 403: nothing to update
209 query = {'_id': objectid.ObjectId(result_id)}
211 self._update(query=query, db_keys=db_keys)