diff options
Diffstat (limited to 'utils/test/testapi/opnfv_testapi/resources/result_handlers.py')
-rw-r--r-- | utils/test/testapi/opnfv_testapi/resources/result_handlers.py | 100 |
1 files changed, 81 insertions, 19 deletions
diff --git a/utils/test/testapi/opnfv_testapi/resources/result_handlers.py b/utils/test/testapi/opnfv_testapi/resources/result_handlers.py index 1773216c0..e202f5c2c 100644 --- a/utils/test/testapi/opnfv_testapi/resources/result_handlers.py +++ b/utils/test/testapi/opnfv_testapi/resources/result_handlers.py @@ -6,20 +6,21 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -from datetime import datetime -from datetime import timedelta +import json +import logging from bson import objectid +from datetime import datetime +from datetime import timedelta -from opnfv_testapi.common import config +from opnfv_testapi.common import constants from opnfv_testapi.common import message from opnfv_testapi.common import raises +from opnfv_testapi.common.config import CONF from opnfv_testapi.resources import handlers from opnfv_testapi.resources import result_models from opnfv_testapi.tornado_swagger import swagger -CONF = config.Config() - class GenericResultHandler(handlers.GenericApiHandler): def __init__(self, application, request, **kwargs): @@ -40,6 +41,7 @@ class GenericResultHandler(handlers.GenericApiHandler): query = dict() date_range = dict() + query['public'] = {'$not': {'$eq': 'false'}} for k in self.request.query_arguments.keys(): v = self.get_query_argument(k) if k == 'project' or k == 'pod' or k == 'case': @@ -56,10 +58,23 @@ class GenericResultHandler(handlers.GenericApiHandler): date_range.update({'$gte': str(v)}) elif k == 'to': date_range.update({'$lt': str(v)}) - elif k != 'last' and k != 'page': + elif k == 'signed': + username = self.get_secure_cookie(constants.TESTAPI_ID) + role = self.get_secure_cookie(constants.ROLE) + if role: + del query['public'] + if role != "reviewer": + query['user'] = username + elif k not in ['last', 'page', 'descend']: query[k] = v if date_range: query['start_date'] = date_range + + # if $lt is not provided, + # empty/None/null/'' start_date will also be returned + if 'start_date' in query and '$lt' not in query['start_date']: + query['start_date'].update({'$lt': str(datetime.now())}) + return query @@ -84,9 +99,10 @@ class ResultsCLHandler(GenericResultHandler): - criteria : the global criteria status passed or failed - trust_indicator : evaluate the stability of the test case to avoid running systematically long and stable test case + - signed : get logined user result GET /results/project=functest&case=vPing&version=Arno-R1 \ - &pod=pod_name&period=15 + &pod=pod_name&period=15&signed @return 200: all test results consist with query, empty list if no result is found @rtype: L{TestResults} @@ -138,7 +154,7 @@ class ResultsCLHandler(GenericResultHandler): @type last: L{string} @in last: query @required last: False - @param page: which page to list + @param page: which page to list, default to 1 @type page: L{int} @in page: query @required page: False @@ -146,18 +162,31 @@ class ResultsCLHandler(GenericResultHandler): @type trust_indicator: L{float} @in trust_indicator: query @required trust_indicator: False + @param signed: user results or all results + @type signed: L{string} + @in signed: query + @required signed: False + @param descend: true, newest2oldest; false, oldest2newest + @type descend: L{string} + @in descend: query + @required descend: False """ - limitations = {'sort': {'start_date': -1}} - last = self.get_query_argument('last', 0) - if last is not None: - last = self.get_int('last', last) - limitations.update({'last': last}) - - page = self.get_query_argument('page', None) - if page is not None: - page = self.get_int('page', page) - limitations.update({'page': page, - 'per_page': CONF.api_results_per_page}) + def descend_limit(): + descend = self.get_query_argument('descend', 'true') + return -1 if descend.lower() == 'true' else 1 + + def last_limit(): + return self.get_int('last', self.get_query_argument('last', 0)) + + def page_limit(): + return self.get_int('page', self.get_query_argument('page', 1)) + + limitations = { + 'sort': {'_id': descend_limit()}, + 'last': last_limit(), + 'page': page_limit(), + 'per_page': CONF.api_results_per_page + } self._list(query=self.set_query(), **limitations) @@ -173,6 +202,9 @@ class ResultsCLHandler(GenericResultHandler): @raise 404: pod/project/testcase not exist @raise 400: body/pod_name/project_name/case_name not provided """ + self._post() + + def _post(self): def pod_query(): return {'name': self.json_args.get('pod_name')} @@ -187,9 +219,39 @@ class ResultsCLHandler(GenericResultHandler): carriers = [('pods', pod_query), ('projects', project_query), ('testcases', testcase_query)] + self._create(miss_fields=miss_fields, carriers=carriers) +class ResultsUploadHandler(ResultsCLHandler): + @swagger.operation(nickname="uploadTestResult") + def post(self): + """ + @description: upload and create a test result + @param body: result to be created + @type body: L{ResultCreateRequest} + @in body: body + @rtype: L{CreateResponse} + @return 200: result is created. + @raise 404: pod/project/testcase not exist + @raise 400: body/pod_name/project_name/case_name not provided + """ + logging.info('file upload') + fileinfo = self.request.files['file'][0] + is_public = self.get_body_argument('public') + logging.warning('public:%s', is_public) + logging.info('results is :%s', fileinfo['filename']) + logging.info('results is :%s', fileinfo['body']) + self.json_args = json.loads(fileinfo['body']).copy() + self.json_args['public'] = is_public + + openid = self.get_secure_cookie(constants.TESTAPI_ID) + if openid: + self.json_args['user'] = openid + + super(ResultsUploadHandler, self)._post() + + class ResultsGURHandler(GenericResultHandler): @swagger.operation(nickname='getTestResultById') def get(self, result_id): |