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