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 trust_indicator: must be int/long/float
112 @type trust_indicator: L{string}
113 @in trust_indicator: query
114 @required trust_indicator: False
116 self._list(self.set_query())
118 @swagger.operation(nickname="create")
121 @description: create a test result
122 @param body: result to be created
123 @type body: L{ResultCreateRequest}
125 @rtype: L{TestResult}
126 @return 200: result is created.
127 @raise 404: pod/project/testcase not exist
128 @raise 400: body/pod_name/project_name/case_name not provided
131 return {'name': data.pod_name}
134 message = 'Could not find pod [{}]'.format(data.pod_name)
135 return HTTP_NOT_FOUND, message
137 def project_query(data):
138 return {'name': data.project_name}
140 def project_error(data):
141 message = 'Could not find project [{}]'.format(data.project_name)
142 return HTTP_NOT_FOUND, message
144 def testcase_query(data):
145 return {'project_name': data.project_name, 'name': data.case_name}
147 def testcase_error(data):
148 message = 'Could not find testcase [{}] in project [{}]'\
149 .format(data.case_name, data.project_name)
150 return HTTP_NOT_FOUND, message
152 miss_checks = ['pod_name', 'project_name', 'case_name']
153 db_checks = [('pods', True, pod_query, pod_error),
154 ('projects', True, project_query, project_error),
155 ('testcases', True, testcase_query, testcase_error)]
156 self._create(miss_checks, db_checks)
159 class ResultsGURHandler(GenericResultHandler):
160 @swagger.operation(nickname='get-one')
161 def get(self, result_id):
163 @description: get a single result by result_id
164 @rtype: L{TestResult}
165 @return 200: test result exist
166 @raise 404: test result not exist
169 query["_id"] = ObjectId(result_id)