Add Euphrates pages for reporting
[releng.git] / utils / test / testapi / opnfv_testapi / tests / unit / resources / 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 from datetime import datetime
11 from datetime import timedelta
12 import httplib
13 import json
14 import unittest
15
16 from opnfv_testapi.common import message
17 from opnfv_testapi.resources import project_models
18 from opnfv_testapi.resources import result_models
19 from opnfv_testapi.resources import testcase_models
20 from opnfv_testapi.tests.unit import executor
21 from opnfv_testapi.tests.unit import fake_pymongo
22 from opnfv_testapi.tests.unit.resources import test_base as base
23
24
25 class Details(object):
26     def __init__(self, timestart=None, duration=None, status=None):
27         self.timestart = timestart
28         self.duration = duration
29         self.status = status
30         self.items = [{'item1': 1}, {'item2': 2}]
31
32     def format(self):
33         return {
34             "timestart": self.timestart,
35             "duration": self.duration,
36             "status": self.status,
37             'items': [{'item1': 1}, {'item2': 2}]
38         }
39
40     @staticmethod
41     def from_dict(a_dict):
42
43         if a_dict is None:
44             return None
45
46         t = Details()
47         t.timestart = a_dict.get('timestart')
48         t.duration = a_dict.get('duration')
49         t.status = a_dict.get('status')
50         t.items = a_dict.get('items')
51         return t
52
53
54 class TestResultBase(base.TestBase):
55     def setUp(self):
56         super(TestResultBase, self).setUp()
57         self.pod = self.pod_d.name
58         self.project = 'functest'
59         self.case = 'vPing'
60         self.installer = 'fuel'
61         self.version = 'C'
62         self.build_tag = 'v3.0'
63         self.scenario = 'odl-l2'
64         self.criteria = 'passed'
65         self.trust_indicator = result_models.TI(0.7)
66         self.start_date = str(datetime.now())
67         self.stop_date = str(datetime.now() + timedelta(minutes=1))
68         self.update_date = str(datetime.now() + timedelta(days=1))
69         self.update_step = -0.05
70         self.details = Details(timestart='0', duration='9s', status='OK')
71         self.req_d = result_models.ResultCreateRequest(
72             pod_name=self.pod,
73             project_name=self.project,
74             case_name=self.case,
75             installer=self.installer,
76             version=self.version,
77             start_date=self.start_date,
78             stop_date=self.stop_date,
79             details=self.details.format(),
80             build_tag=self.build_tag,
81             scenario=self.scenario,
82             criteria=self.criteria,
83             trust_indicator=self.trust_indicator)
84         self.get_res = result_models.TestResult
85         self.list_res = result_models.TestResults
86         self.update_res = result_models.TestResult
87         self.basePath = '/api/v1/results'
88         self.req_project = project_models.ProjectCreateRequest(
89             self.project,
90             'vping test')
91         self.req_testcase = testcase_models.TestcaseCreateRequest(
92             self.case,
93             '/cases/vping',
94             'vping-ssh test')
95         fake_pymongo.pods.insert(self.pod_d.format())
96         self.create_help('/api/v1/projects', self.req_project)
97         self.create_help('/api/v1/projects/%s/cases',
98                          self.req_testcase,
99                          self.project)
100
101     def assert_res(self, result, req=None):
102         if req is None:
103             req = self.req_d
104         self.assertEqual(result.pod_name, req.pod_name)
105         self.assertEqual(result.project_name, req.project_name)
106         self.assertEqual(result.case_name, req.case_name)
107         self.assertEqual(result.installer, req.installer)
108         self.assertEqual(result.version, req.version)
109         details_req = Details.from_dict(req.details)
110         details_res = Details.from_dict(result.details)
111         self.assertEqual(details_res.duration, details_req.duration)
112         self.assertEqual(details_res.timestart, details_req.timestart)
113         self.assertEqual(details_res.status, details_req.status)
114         self.assertEqual(details_res.items, details_req.items)
115         self.assertEqual(result.build_tag, req.build_tag)
116         self.assertEqual(result.scenario, req.scenario)
117         self.assertEqual(result.criteria, req.criteria)
118         self.assertEqual(result.start_date, req.start_date)
119         self.assertEqual(result.stop_date, req.stop_date)
120         self.assertIsNotNone(result._id)
121         ti = result.trust_indicator
122         self.assertEqual(ti.current, req.trust_indicator.current)
123         if ti.histories:
124             history = ti.histories[0]
125             self.assertEqual(history.date, self.update_date)
126             self.assertEqual(history.step, self.update_step)
127
128     def _create_d(self):
129         _, res = self.create_d()
130         return res.href.split('/')[-1]
131
132     def upload(self, req):
133         if req and not isinstance(req, str) and hasattr(req, 'format'):
134             req = req.format()
135         res = self.fetch(self.basePath + '/upload',
136                          method='POST',
137                          body=json.dumps(req),
138                          headers=self.headers)
139
140         return self._get_return(res, self.create_res)
141
142
143 class TestResultUpload(TestResultBase):
144     @executor.upload(httplib.BAD_REQUEST, message.key_error('file'))
145     def test_filenotfind(self):
146         return None
147
148
149 class TestResultCreate(TestResultBase):
150     @executor.create(httplib.BAD_REQUEST, message.no_body())
151     def test_nobody(self):
152         return None
153
154     @executor.create(httplib.BAD_REQUEST, message.missing('pod_name'))
155     def test_podNotProvided(self):
156         req = self.req_d
157         req.pod_name = None
158         return req
159
160     @executor.create(httplib.BAD_REQUEST, message.missing('project_name'))
161     def test_projectNotProvided(self):
162         req = self.req_d
163         req.project_name = None
164         return req
165
166     @executor.create(httplib.BAD_REQUEST, message.missing('case_name'))
167     def test_testcaseNotProvided(self):
168         req = self.req_d
169         req.case_name = None
170         return req
171
172     @executor.create(httplib.FORBIDDEN, message.not_found_base)
173     def test_noPod(self):
174         req = self.req_d
175         req.pod_name = 'notExistPod'
176         return req
177
178     @executor.create(httplib.FORBIDDEN, message.not_found_base)
179     def test_noProject(self):
180         req = self.req_d
181         req.project_name = 'notExistProject'
182         return req
183
184     @executor.create(httplib.FORBIDDEN, message.not_found_base)
185     def test_noTestcase(self):
186         req = self.req_d
187         req.case_name = 'notExistTestcase'
188         return req
189
190     @executor.create(httplib.OK, 'assert_href')
191     def test_success(self):
192         return self.req_d
193
194     @executor.create(httplib.OK, 'assert_href')
195     def test_key_with_doc(self):
196         req = copy.deepcopy(self.req_d)
197         req.details = {'1.name': 'dot_name'}
198         return req
199
200     @executor.create(httplib.OK, '_assert_no_ti')
201     def test_no_ti(self):
202         req = result_models.ResultCreateRequest(pod_name=self.pod,
203                                                 project_name=self.project,
204                                                 case_name=self.case,
205                                                 installer=self.installer,
206                                                 version=self.version,
207                                                 start_date=self.start_date,
208                                                 stop_date=self.stop_date,
209                                                 details=self.details.format(),
210                                                 build_tag=self.build_tag,
211                                                 scenario=self.scenario,
212                                                 criteria=self.criteria)
213         self.actual_req = req
214         return req
215
216     def _assert_no_ti(self, body):
217         _id = body.href.split('/')[-1]
218         code, body = self.get(_id)
219         self.assert_res(body, self.actual_req)
220
221
222 class TestResultGet(TestResultBase):
223     def setUp(self):
224         super(TestResultGet, self).setUp()
225         self.req_10d_before = self._create_changed_date(days=-10)
226         self.req_d_id = self._create_d()
227         self.req_10d_later = self._create_changed_date(days=10)
228
229     @executor.get(httplib.OK, 'assert_res')
230     def test_getOne(self):
231         return self.req_d_id
232
233     @executor.query(httplib.OK, '_query_success', 3)
234     def test_queryPod(self):
235         return self._set_query('pod')
236
237     @executor.query(httplib.OK, '_query_success', 3)
238     def test_queryProject(self):
239         return self._set_query('project')
240
241     @executor.query(httplib.OK, '_query_success', 3)
242     def test_queryTestcase(self):
243         return self._set_query('case')
244
245     @executor.query(httplib.OK, '_query_success', 3)
246     def test_queryVersion(self):
247         return self._set_query('version')
248
249     @executor.query(httplib.OK, '_query_success', 3)
250     def test_queryInstaller(self):
251         return self._set_query('installer')
252
253     @executor.query(httplib.OK, '_query_success', 3)
254     def test_queryBuildTag(self):
255         return self._set_query('build_tag')
256
257     @executor.query(httplib.OK, '_query_success', 3)
258     def test_queryScenario(self):
259         return self._set_query('scenario')
260
261     @executor.query(httplib.OK, '_query_success', 3)
262     def test_queryTrustIndicator(self):
263         return self._set_query('trust_indicator')
264
265     @executor.query(httplib.OK, '_query_success', 3)
266     def test_queryCriteria(self):
267         return self._set_query('criteria')
268
269     @executor.query(httplib.BAD_REQUEST, message.must_int('period'))
270     def test_queryPeriodNotInt(self):
271         return self._set_query('period=a')
272
273     @executor.query(httplib.OK, '_query_period_one', 1)
274     def test_queryPeriodSuccess(self):
275         return self._set_query('period=5')
276
277     @executor.query(httplib.BAD_REQUEST, message.must_int('last'))
278     def test_queryLastNotInt(self):
279         return self._set_query('last=a')
280
281     @executor.query(httplib.OK, '_query_last_one', 1)
282     def test_queryLast(self):
283         return self._set_query('last=1')
284
285     @executor.query(httplib.OK, '_query_success', 4)
286     def test_queryPublic(self):
287         self._create_public_data()
288         return self._set_query('')
289
290     @executor.query(httplib.OK, '_query_success', 1)
291     def test_queryPrivate(self):
292         self._create_private_data()
293         return self._set_query('public=false')
294
295     @executor.query(httplib.OK, '_query_period_one', 1)
296     def test_combination(self):
297         return self._set_query('pod',
298                                'project',
299                                'case',
300                                'version',
301                                'installer',
302                                'build_tag',
303                                'scenario',
304                                'trust_indicator',
305                                'criteria',
306                                'period=5')
307
308     @executor.query(httplib.OK, '_query_success', 0)
309     def test_notFound(self):
310         return self._set_query('pod=notExistPod',
311                                'project',
312                                'case',
313                                'version',
314                                'installer',
315                                'build_tag',
316                                'scenario',
317                                'trust_indicator',
318                                'criteria',
319                                'period=1')
320
321     @executor.query(httplib.OK, '_query_success', 1)
322     def test_filterErrorStartdate(self):
323         self._create_error_start_date(None)
324         self._create_error_start_date('None')
325         self._create_error_start_date('null')
326         self._create_error_start_date('')
327         return self._set_query('period=5')
328
329     def _query_success(self, body, number):
330         self.assertEqual(number, len(body.results))
331
332     def _query_last_one(self, body, number):
333         self.assertEqual(number, len(body.results))
334         self.assert_res(body.results[0], self.req_10d_later)
335
336     def _query_period_one(self, body, number):
337         self.assertEqual(number, len(body.results))
338         self.assert_res(body.results[0], self.req_d)
339
340     def _create_error_start_date(self, start_date):
341         req = copy.deepcopy(self.req_d)
342         req.start_date = start_date
343         self.create(req)
344         return req
345
346     def _create_changed_date(self, **kwargs):
347         req = copy.deepcopy(self.req_d)
348         req.start_date = datetime.now() + timedelta(**kwargs)
349         req.stop_date = str(req.start_date + timedelta(minutes=10))
350         req.start_date = str(req.start_date)
351         self.create(req)
352         return req
353
354     def _create_public_data(self, **kwargs):
355         req = copy.deepcopy(self.req_d)
356         req.public = 'true'
357         self.create(req)
358         return req
359
360     def _create_private_data(self, **kwargs):
361         req = copy.deepcopy(self.req_d)
362         req.public = 'false'
363         self.create(req)
364         return req
365
366     def _set_query(self, *args):
367         def get_value(arg):
368             return self.__getattribute__(arg) \
369                 if arg != 'trust_indicator' else self.trust_indicator.current
370         uri = ''
371         for arg in args:
372             if arg:
373                 if '=' in arg:
374                     uri += arg + '&'
375                 else:
376                     uri += '{}={}&'.format(arg, get_value(arg))
377         return uri[0: -1]
378
379
380 class TestResultUpdate(TestResultBase):
381     def setUp(self):
382         super(TestResultUpdate, self).setUp()
383         self.req_d_id = self._create_d()
384
385     @executor.update(httplib.OK, '_assert_update_ti')
386     def test_success(self):
387         new_ti = copy.deepcopy(self.trust_indicator)
388         new_ti.current += self.update_step
389         new_ti.histories.append(
390             result_models.TIHistory(self.update_date, self.update_step))
391         new_data = copy.deepcopy(self.req_d)
392         new_data.trust_indicator = new_ti
393         update = result_models.ResultUpdateRequest(trust_indicator=new_ti)
394         self.update_req = new_data
395         return update, self.req_d_id
396
397     def _assert_update_ti(self, request, body):
398         ti = body.trust_indicator
399         self.assertEqual(ti.current, request.trust_indicator.current)
400         if ti.histories:
401             history = ti.histories[0]
402             self.assertEqual(history.date, self.update_date)
403             self.assertEqual(history.step, self.update_step)
404
405
406 if __name__ == '__main__':
407     unittest.main()