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, timedelta
11 from bson.objectid import ObjectId
12 from tornado.web import HTTPError
14 from opnfv_testapi.common.constants import HTTP_BAD_REQUEST, HTTP_NOT_FOUND
15 from opnfv_testapi.resources.handlers import GenericApiHandler
16 from opnfv_testapi.resources.result_models import TestResult
17 from opnfv_testapi.tornado_swagger import swagger
20 class GenericResultHandler(GenericApiHandler):
21 def __init__(self, application, request, **kwargs):
22 super(GenericResultHandler, self).__init__(application,
25 self.table = self.db_results
26 self.table_cls = TestResult
30 for k in self.request.query_arguments.keys():
31 v = self.get_query_argument(k)
32 if k == 'project' or k == 'pod' or k == 'case':
33 query[k + '_name'] = v
38 raise HTTPError(HTTP_BAD_REQUEST, 'period must be int')
40 period = datetime.now() - timedelta(days=v)
41 obj = {"$gte": str(period)}
42 query['start_date'] = obj
43 elif k == 'trust_indicator':
50 class ResultsCLHandler(GenericResultHandler):
51 @swagger.operation(nickname="list-all")
54 @description: Retrieve result(s) for a test project
56 @notes: Retrieve result(s) for a test project on a specific pod.
57 Available filters for this request are :
58 - project : project name
61 - version : platform version (Arno-R1, ...)
62 - installer (fuel, ...)
63 - build_tag : Jenkins build tag name
64 - period : x (x last days)
65 - scenario : the test scenario (previously version)
66 - criteria : the global criteria status passed or failed
67 - trust_indicator : evaluate the stability of the test case
68 to avoid running systematically long and stable test case
70 GET /results/project=functest&case=vPing&version=Arno-R1 \
71 &pod=pod_name&period=15
72 @return 200: all test results consist with query,
73 empty list if no result is found
74 @rtype: L{TestResults}
79 @param project: project name
80 @type project: L{string}
82 @required project: False
83 @param case: case name
87 @param version: i.e. Colorado
88 @type version: L{string}
90 @required version: False
91 @param installer: fuel/apex/joid/compass
92 @type installer: L{string}
94 @required installer: False
95 @param build_tag: i.e. v3.0
96 @type build_tag: L{string}
98 @required build_tag: False
99 @param scenario: i.e. odl
100 @type scenario: L{string}
102 @required scenario: False
103 @param criteria: i.e. passed
104 @type criteria: L{string}
106 @required criteria: False
107 @param period: last days
108 @type period: L{string}
110 @required period: False
111 @param last: last days
112 @type last: L{string}
114 @required last: False
115 @param trust_indicator: must be int/long/float
116 @type trust_indicator: L{string}
117 @in trust_indicator: query
118 @required trust_indicator: False
120 last = self.get_query_argument('last', 0)
125 raise HTTPError(HTTP_BAD_REQUEST, 'last must be int')
127 self._list(self.set_query(), sort=[{'start_date', -1}], last=last)
129 @swagger.operation(nickname="create")
132 @description: create a test result
133 @param body: result to be created
134 @type body: L{ResultCreateRequest}
136 @rtype: L{TestResult}
137 @return 200: result is created.
138 @raise 404: pod/project/testcase not exist
139 @raise 400: body/pod_name/project_name/case_name not provided
142 return {'name': data.pod_name}
145 message = 'Could not find pod [{}]'.format(data.pod_name)
146 return HTTP_NOT_FOUND, message
148 def project_query(data):
149 return {'name': data.project_name}
151 def project_error(data):
152 message = 'Could not find project [{}]'.format(data.project_name)
153 return HTTP_NOT_FOUND, message
155 def testcase_query(data):
156 return {'project_name': data.project_name, 'name': data.case_name}
158 def testcase_error(data):
159 message = 'Could not find testcase [{}] in project [{}]'\
160 .format(data.case_name, data.project_name)
161 return HTTP_NOT_FOUND, message
163 miss_checks = ['pod_name', 'project_name', 'case_name']
164 db_checks = [('pods', True, pod_query, pod_error),
165 ('projects', True, project_query, project_error),
166 ('testcases', True, testcase_query, testcase_error)]
167 self._create(miss_checks, db_checks)
170 class ResultsGURHandler(GenericResultHandler):
171 @swagger.operation(nickname='get-one')
172 def get(self, result_id):
174 @description: get a single result by result_id
175 @rtype: L{TestResult}
176 @return 200: test result exist
177 @raise 404: test result not exist
180 query["_id"] = ObjectId(result_id)