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
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
22 class GenericResultHandler(handlers.GenericApiHandler):
23 def __init__(self, application, request, **kwargs):
24 super(GenericResultHandler, self).__init__(application,
27 self.table = self.db_results
28 self.table_cls = result_models.TestResult
30 def get_int(self, key, value):
34 raises.BadRequest(message.must_int(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 return httplib.FORBIDDEN, message.not_found('pod', data.pod_name)
150 def project_query(data):
151 return {'name': data.project_name}
153 def project_error(data):
154 return httplib.FORBIDDEN, message.not_found('project',
157 def testcase_query(data):
158 return {'project_name': data.project_name, 'name': data.case_name}
160 def testcase_error(data):
161 return httplib.FORBIDDEN, message.not_found('testcase',
164 miss_checks = ['pod_name', 'project_name', 'case_name']
165 db_checks = [('pods', True, pod_query, pod_error),
166 ('projects', True, project_query, project_error),
167 ('testcases', True, testcase_query, testcase_error)]
168 self._create(miss_checks, db_checks)
171 class ResultsGURHandler(GenericResultHandler):
172 @swagger.operation(nickname='getTestResultById')
173 def get(self, result_id):
175 @description: get a single result by result_id
176 @rtype: L{TestResult}
177 @return 200: test result exist
178 @raise 404: test result not exist
181 query["_id"] = objectid.ObjectId(result_id)
184 @swagger.operation(nickname="updateTestResultById")
185 def put(self, result_id):
187 @description: update a single result by _id
188 @param body: fields to be updated
189 @type body: L{ResultUpdateRequest}
192 @return 200: update success
193 @raise 404: result not exist
194 @raise 403: nothing to update
196 query = {'_id': objectid.ObjectId(result_id)}
198 self._update(query, db_keys)