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 ##############################################################################
10 from datetime import datetime, timedelta
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
23 class Details(object):
24 def __init__(self, timestart=None, duration=None, status=None):
25 self.timestart = timestart
26 self.duration = duration
28 self.items = [{'item1': 1}, {'item2': 2}]
32 "timestart": self.timestart,
33 "duration": self.duration,
34 "status": self.status,
35 'items': [{'item1': 1}, {'item2': 2}]
39 def from_dict(a_dict):
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')
52 class TestResultBase(base.TestBase):
55 self.project = 'functest'
57 self.installer = 'fuel'
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(
71 project_name=self.project,
73 installer=self.installer,
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(
90 self.req_project = project_models.ProjectCreateRequest(
93 self.req_testcase = testcase_models.TestcaseCreateRequest(
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',
103 def assert_res(self, result, req=None):
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)
126 history = ti.histories[0]
127 self.assertEqual(history.date, self.update_date)
128 self.assertEqual(history.step, self.update_step)
131 _, res = self.create_d()
132 return res.href.split('/')[-1]
135 class TestResultCreate(TestResultBase):
136 @executor.create(httplib.BAD_REQUEST, message.no_body())
137 def test_nobody(self):
140 @executor.create(httplib.BAD_REQUEST, message.missing('pod_name'))
141 def test_podNotProvided(self):
146 @executor.create(httplib.BAD_REQUEST, message.missing('project_name'))
147 def test_projectNotProvided(self):
149 req.project_name = None
152 @executor.create(httplib.BAD_REQUEST, message.missing('case_name'))
153 def test_testcaseNotProvided(self):
158 @executor.create(httplib.FORBIDDEN, message.not_found_base)
159 def test_noPod(self):
161 req.pod_name = 'notExistPod'
164 @executor.create(httplib.FORBIDDEN, message.not_found_base)
165 def test_noProject(self):
167 req.project_name = 'notExistProject'
170 @executor.create(httplib.FORBIDDEN, message.not_found_base)
171 def test_noTestcase(self):
173 req.case_name = 'notExistTestcase'
176 @executor.create(httplib.OK, 'assert_href')
177 def test_success(self):
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'}
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,
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
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)
208 class TestResultGet(TestResultBase):
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)
215 @executor.get(httplib.OK, 'assert_res')
216 def test_getOne(self):
219 @executor.query(httplib.OK, '_query_success', 3)
220 def test_queryPod(self):
221 return self._set_query('pod')
223 @executor.query(httplib.OK, '_query_success', 3)
224 def test_queryProject(self):
225 return self._set_query('project')
227 @executor.query(httplib.OK, '_query_success', 3)
228 def test_queryTestcase(self):
229 return self._set_query('case')
231 @executor.query(httplib.OK, '_query_success', 3)
232 def test_queryVersion(self):
233 return self._set_query('version')
235 @executor.query(httplib.OK, '_query_success', 3)
236 def test_queryInstaller(self):
237 return self._set_query('installer')
239 @executor.query(httplib.OK, '_query_success', 3)
240 def test_queryBuildTag(self):
241 return self._set_query('build_tag')
243 @executor.query(httplib.OK, '_query_success', 3)
244 def test_queryScenario(self):
245 return self._set_query('scenario')
247 @executor.query(httplib.OK, '_query_success', 3)
248 def test_queryTrustIndicator(self):
249 return self._set_query('trust_indicator')
251 @executor.query(httplib.OK, '_query_success', 3)
252 def test_queryCriteria(self):
253 return self._set_query('criteria')
255 @executor.query(httplib.BAD_REQUEST, message.must_int('period'))
256 def test_queryPeriodNotInt(self):
257 return self._set_query('period=a')
259 @executor.query(httplib.OK, '_query_last_one', 1)
260 def test_queryPeriodSuccess(self):
261 return self._set_query('period=1')
263 @executor.query(httplib.BAD_REQUEST, message.must_int('last'))
264 def test_queryLastNotInt(self):
265 return self._set_query('last=a')
267 @executor.query(httplib.OK, '_query_last_one', 1)
268 def test_queryLast(self):
269 return self._set_query('last=1')
271 @executor.query(httplib.OK, '_query_last_one', 1)
272 def test_combination(self):
273 return self._set_query('pod',
284 @executor.query(httplib.OK, '_query_success', 0)
285 def test_notFound(self):
286 return self._set_query('pod=notExistPod',
297 def _query_success(self, body, number):
298 self.assertEqual(number, len(body.results))
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)
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)
312 def _set_query(self, *args):
314 return self.__getattribute__(arg) \
315 if arg != 'trust_indicator' else self.trust_indicator.current
321 uri += '{}={}&'.format(arg, get_value(arg))
325 class TestResultUpdate(TestResultBase):
327 super(TestResultUpdate, self).setUp()
328 self.req_d_id = self._create_d()
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
342 def _assert_update_ti(self, request, body):
343 ti = body.trust_indicator
344 self.assertEqual(ti.current, request.trust_indicator.current)
346 history = ti.histories[0]
347 self.assertEqual(history.date, self.update_date)
348 self.assertEqual(history.step, self.update_step)
351 if __name__ == '__main__':