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