add copyright to files in testAPI
[releng.git] / utils / test / result_collection_api / opnfv_testapi / resources / result_handlers.py
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
10
11 from bson.objectid import ObjectId
12 from tornado.web import HTTPError
13
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
18
19
20 class GenericResultHandler(GenericApiHandler):
21     def __init__(self, application, request, **kwargs):
22         super(GenericResultHandler, self).__init__(application,
23                                                    request,
24                                                    **kwargs)
25         self.table = self.db_results
26         self.table_cls = TestResult
27
28     def set_query(self):
29         query = dict()
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
34             elif k == 'period':
35                 try:
36                     v = int(v)
37                 except:
38                     raise HTTPError(HTTP_BAD_REQUEST, 'period must be int')
39                 if v > 0:
40                     period = datetime.now() - timedelta(days=v)
41                     obj = {"$gte": str(period)}
42                     query['start_date'] = obj
43             elif k == 'trust_indicator':
44                 query[k] = float(v)
45             else:
46                 query[k] = v
47         return query
48
49
50 class ResultsCLHandler(GenericResultHandler):
51     @swagger.operation(nickname="list-all")
52     def get(self):
53         """
54             @description: Retrieve result(s) for a test project
55                           on a specific pod.
56             @notes: Retrieve result(s) for a test project on a specific pod.
57                 Available filters for this request are :
58                  - project : project name
59                  - case : case name
60                  - pod : pod 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
69
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}
75             @param pod: pod name
76             @type pod: L{string}
77             @in pod: query
78             @required pod: False
79             @param project: project name
80             @type project: L{string}
81             @in project: query
82             @required project: True
83             @param case: case name
84             @type case: L{string}
85             @in case: query
86             @required case: True
87             @param version: i.e. Colorado
88             @type version: L{string}
89             @in version: query
90             @required version: False
91             @param installer: fuel/apex/joid/compass
92             @type installer: L{string}
93             @in installer: query
94             @required installer: False
95             @param build_tag: i.e. v3.0
96             @type build_tag: L{string}
97             @in build_tag: query
98             @required build_tag: False
99             @param scenario: i.e. odl
100             @type scenario: L{string}
101             @in scenario: query
102             @required scenario: False
103             @param criteria: i.e. passed
104             @type criteria: L{string}
105             @in criteria: query
106             @required criteria: False
107             @param period: last days
108             @type period: L{string}
109             @in period: query
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
115         """
116         self._list(self.set_query())
117
118     @swagger.operation(nickname="create")
119     def post(self):
120         """
121             @description: create a test result
122             @param body: result to be created
123             @type body: L{ResultCreateRequest}
124             @in body: body
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
129         """
130         def pod_query(data):
131             return {'name': data.pod_name}
132
133         def pod_error(data):
134             message = 'Could not find pod [{}]'.format(data.pod_name)
135             return HTTP_NOT_FOUND, message
136
137         def project_query(data):
138             return {'name': data.project_name}
139
140         def project_error(data):
141             message = 'Could not find project [{}]'.format(data.project_name)
142             return HTTP_NOT_FOUND, message
143
144         def testcase_query(data):
145             return {'project_name': data.project_name, 'name': data.case_name}
146
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
151
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)
157
158
159 class ResultsGURHandler(GenericResultHandler):
160     @swagger.operation(nickname='get-one')
161     def get(self, result_id):
162         """
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
167         """
168         query = dict()
169         query["_id"] = ObjectId(result_id)
170         self._get_one(query)