Merge "Enable Verification Results on Flake8 Reports"
[releng.git] / utils / test / result_collection_api / opnfv_testapi / tests / unit / test_result.py
1 ##############################################################################
2 # Copyright (c) 2016 ZTE Corporation
3 # feng.xiaowei@zte.com.cn
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 import copy
10 import unittest
11 from datetime import datetime, timedelta
12
13 from opnfv_testapi.common.constants import HTTP_OK, HTTP_BAD_REQUEST, \
14     HTTP_NOT_FOUND
15 from opnfv_testapi.resources.pod_models import PodCreateRequest
16 from opnfv_testapi.resources.project_models import ProjectCreateRequest
17 from opnfv_testapi.resources.result_models import ResultCreateRequest, \
18     TestResult, TestResults, ResultUpdateRequest, TI, TIHistory
19 from opnfv_testapi.resources.testcase_models import TestcaseCreateRequest
20 from test_base import TestBase
21
22
23 class Details(object):
24     def __init__(self, timestart=None, duration=None, status=None):
25         self.timestart = timestart
26         self.duration = duration
27         self.status = status
28
29     def format(self):
30         return {
31             "timestart": self.timestart,
32             "duration": self.duration,
33             "status": self.status
34         }
35
36     @staticmethod
37     def from_dict(a_dict):
38
39         if a_dict is None:
40             return None
41
42         t = Details()
43         t.timestart = a_dict.get('timestart')
44         t.duration = a_dict.get('duration')
45         t.status = a_dict.get('status')
46         return t
47
48
49 class TestResultBase(TestBase):
50     def setUp(self):
51         self.pod = 'zte-pod1'
52         self.project = 'functest'
53         self.case = 'vPing'
54         self.installer = 'fuel'
55         self.version = 'C'
56         self.build_tag = 'v3.0'
57         self.scenario = 'odl-l2'
58         self.criteria = 'passed'
59         self.trust_indicator = TI(0.7)
60         self.start_date = "2016-05-23 07:16:09.477097"
61         self.stop_date = "2016-05-23 07:16:19.477097"
62         self.update_date = "2016-05-24 07:16:19.477097"
63         self.update_step = -0.05
64         super(TestResultBase, self).setUp()
65         self.details = Details(timestart='0', duration='9s', status='OK')
66         self.req_d = ResultCreateRequest(pod_name=self.pod,
67                                          project_name=self.project,
68                                          case_name=self.case,
69                                          installer=self.installer,
70                                          version=self.version,
71                                          start_date=self.start_date,
72                                          stop_date=self.stop_date,
73                                          details=self.details.format(),
74                                          build_tag=self.build_tag,
75                                          scenario=self.scenario,
76                                          criteria=self.criteria,
77                                          trust_indicator=self.trust_indicator)
78         self.get_res = TestResult
79         self.list_res = TestResults
80         self.update_res = TestResult
81         self.basePath = '/api/v1/results'
82         self.req_pod = PodCreateRequest(self.pod, 'metal', 'zte pod 1')
83         self.req_project = ProjectCreateRequest(self.project, 'vping test')
84         self.req_testcase = TestcaseCreateRequest(self.case,
85                                                   '/cases/vping',
86                                                   'vping-ssh test')
87         self.create_help('/api/v1/pods', self.req_pod)
88         self.create_help('/api/v1/projects', self.req_project)
89         self.create_help('/api/v1/projects/%s/cases',
90                          self.req_testcase,
91                          self.project)
92
93     def assert_res(self, code, result, req=None):
94         self.assertEqual(code, HTTP_OK)
95         if req is None:
96             req = self.req_d
97         self.assertEqual(result.pod_name, req.pod_name)
98         self.assertEqual(result.project_name, req.project_name)
99         self.assertEqual(result.case_name, req.case_name)
100         self.assertEqual(result.installer, req.installer)
101         self.assertEqual(result.version, req.version)
102         details_req = Details.from_dict(req.details)
103         details_res = Details.from_dict(result.details)
104         self.assertEqual(details_res.duration, details_req.duration)
105         self.assertEqual(details_res.timestart, details_req.timestart)
106         self.assertEqual(details_res.status, details_req.status)
107         self.assertEqual(result.build_tag, req.build_tag)
108         self.assertEqual(result.scenario, req.scenario)
109         self.assertEqual(result.criteria, req.criteria)
110         self.assertEqual(result.start_date, req.start_date)
111         self.assertEqual(result.stop_date, req.stop_date)
112         self.assertIsNotNone(result._id)
113         ti = result.trust_indicator
114         self.assertEqual(ti.current, req.trust_indicator.current)
115         if ti.histories:
116             history = ti.histories[0]
117             self.assertEqual(history.date, self.update_date)
118             self.assertEqual(history.step, self.update_step)
119
120     def _create_d(self):
121         _, res = self.create_d()
122         return res.href.split('/')[-1]
123
124
125 class TestResultCreate(TestResultBase):
126     def test_nobody(self):
127         (code, body) = self.create(None)
128         self.assertEqual(code, HTTP_BAD_REQUEST)
129         self.assertIn('no body', body)
130
131     def test_podNotProvided(self):
132         req = self.req_d
133         req.pod_name = None
134         (code, body) = self.create(req)
135         self.assertEqual(code, HTTP_BAD_REQUEST)
136         self.assertIn('pod_name missing', body)
137
138     def test_projectNotProvided(self):
139         req = self.req_d
140         req.project_name = None
141         (code, body) = self.create(req)
142         self.assertEqual(code, HTTP_BAD_REQUEST)
143         self.assertIn('project_name missing', body)
144
145     def test_testcaseNotProvided(self):
146         req = self.req_d
147         req.case_name = None
148         (code, body) = self.create(req)
149         self.assertEqual(code, HTTP_BAD_REQUEST)
150         self.assertIn('case_name missing', body)
151
152     def test_noPod(self):
153         req = self.req_d
154         req.pod_name = 'notExistPod'
155         (code, body) = self.create(req)
156         self.assertEqual(code, HTTP_NOT_FOUND)
157         self.assertIn('Could not find pod', body)
158
159     def test_noProject(self):
160         req = self.req_d
161         req.project_name = 'notExistProject'
162         (code, body) = self.create(req)
163         self.assertEqual(code, HTTP_NOT_FOUND)
164         self.assertIn('Could not find project', body)
165
166     def test_noTestcase(self):
167         req = self.req_d
168         req.case_name = 'notExistTestcase'
169         (code, body) = self.create(req)
170         self.assertEqual(code, HTTP_NOT_FOUND)
171         self.assertIn('Could not find testcase', body)
172
173     def test_success(self):
174         (code, body) = self.create_d()
175         self.assertEqual(code, HTTP_OK)
176         self.assert_href(body)
177
178     def test_key_with_doc(self):
179         req = copy.deepcopy(self.req_d)
180         req.details = {'1.name': 'dot_name'}
181         (code, body) = self.create(req)
182         self.assertEqual(code, HTTP_OK)
183         self.assert_href(body)
184
185
186 class TestResultGet(TestResultBase):
187     def test_getOne(self):
188         _id = self._create_d()
189         code, body = self.get(_id)
190         self.assert_res(code, body)
191
192     def test_queryPod(self):
193         self._query_and_assert(self._set_query('pod'))
194
195     def test_queryProject(self):
196         self._query_and_assert(self._set_query('project'))
197
198     def test_queryTestcase(self):
199         self._query_and_assert(self._set_query('case'))
200
201     def test_queryVersion(self):
202         self._query_and_assert(self._set_query('version'))
203
204     def test_queryInstaller(self):
205         self._query_and_assert(self._set_query('installer'))
206
207     def test_queryBuildTag(self):
208         self._query_and_assert(self._set_query('build_tag'))
209
210     def test_queryScenario(self):
211         self._query_and_assert(self._set_query('scenario'))
212
213     def test_queryTrustIndicator(self):
214         self._query_and_assert(self._set_query('trust_indicator'))
215
216     def test_queryCriteria(self):
217         self._query_and_assert(self._set_query('criteria'))
218
219     def test_queryPeriodNotInt(self):
220         code, body = self.query(self._set_query('period=a'))
221         self.assertEqual(code, HTTP_BAD_REQUEST)
222         self.assertIn('period must be int', body)
223
224     def test_queryPeriodFail(self):
225         self._query_and_assert(self._set_query('period=1'),
226                                found=False, days=-10)
227
228     def test_queryPeriodSuccess(self):
229         self._query_and_assert(self._set_query('period=1'),
230                                found=True)
231
232     def test_queryLastNotInt(self):
233         code, body = self.query(self._set_query('last=a'))
234         self.assertEqual(code, HTTP_BAD_REQUEST)
235         self.assertIn('last must be int', body)
236
237     def test_queryLast(self):
238         self._create_changed_date()
239         req = self._create_changed_date(minutes=20)
240         self._create_changed_date(minutes=-20)
241         self._query_and_assert(self._set_query('last=1'), req=req)
242
243     def test_combination(self):
244         self._query_and_assert(self._set_query('pod',
245                                                'project',
246                                                'case',
247                                                'version',
248                                                'installer',
249                                                'build_tag',
250                                                'scenario',
251                                                'trust_indicator',
252                                                'criteria',
253                                                'period=1'))
254
255     def test_notFound(self):
256         self._query_and_assert(self._set_query('pod=notExistPod',
257                                                'project',
258                                                'case',
259                                                'version',
260                                                'installer',
261                                                'build_tag',
262                                                'scenario',
263                                                'trust_indicator',
264                                                'criteria',
265                                                'period=1'),
266                                found=False)
267
268     def _query_and_assert(self, query, found=True, req=None, **kwargs):
269         if req is None:
270             req = self._create_changed_date(**kwargs)
271         code, body = self.query(query)
272         if not found:
273             self.assertEqual(code, HTTP_OK)
274             self.assertEqual(0, len(body.results))
275         else:
276             self.assertEqual(1, len(body.results))
277             for result in body.results:
278                 self.assert_res(code, result, req)
279
280     def _create_changed_date(self, **kwargs):
281         req = copy.deepcopy(self.req_d)
282         req.start_date = datetime.now() + timedelta(**kwargs)
283         req.stop_date = str(req.start_date + timedelta(minutes=10))
284         req.start_date = str(req.start_date)
285         self.create(req)
286         return req
287
288     def _set_query(self, *args):
289         def get_value(arg):
290             return eval('self.' + arg) \
291                 if arg != 'trust_indicator' else self.trust_indicator.current
292         uri = ''
293         for arg in args:
294             if '=' in arg:
295                 uri += arg + '&'
296             else:
297                 uri += '{}={}&'.format(arg, get_value(arg))
298         return uri[0: -1]
299
300
301 class TestResultUpdate(TestResultBase):
302     def test_success(self):
303         _id = self._create_d()
304
305         new_ti = copy.deepcopy(self.trust_indicator)
306         new_ti.current += self.update_step
307         new_ti.histories.append(TIHistory(self.update_date, self.update_step))
308         new_data = copy.deepcopy(self.req_d)
309         new_data.trust_indicator = new_ti
310         update = ResultUpdateRequest(trust_indicator=new_ti)
311         code, body = self.update(update, _id)
312         self.assertEqual(_id, body._id)
313         self.assert_res(code, body, new_data)
314
315         code, new_body = self.get(_id)
316         self.assertEqual(_id, new_body._id)
317         self.assert_res(code, new_body, new_data)
318
319
320 if __name__ == '__main__':
321     unittest.main()