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
13 from opnfv_testapi.common import constants
14 from opnfv_testapi.resources import pod_models
15 from opnfv_testapi.resources import project_models
16 from opnfv_testapi.resources import result_models
17 from opnfv_testapi.resources import testcase_models
18 import test_base as base
21 class Details(object):
22 def __init__(self, timestart=None, duration=None, status=None):
23 self.timestart = timestart
24 self.duration = duration
26 self.items = [{'item1': 1}, {'item2': 2}]
30 "timestart": self.timestart,
31 "duration": self.duration,
32 "status": self.status,
33 'items': [{'item1': 1}, {'item2': 2}]
37 def from_dict(a_dict):
43 t.timestart = a_dict.get('timestart')
44 t.duration = a_dict.get('duration')
45 t.status = a_dict.get('status')
46 t.items = a_dict.get('items')
50 class TestResultBase(base.TestBase):
53 self.project = 'functest'
55 self.installer = 'fuel'
57 self.build_tag = 'v3.0'
58 self.scenario = 'odl-l2'
59 self.criteria = 'passed'
60 self.trust_indicator = result_models.TI(0.7)
61 self.start_date = "2016-05-23 07:16:09.477097"
62 self.stop_date = "2016-05-23 07:16:19.477097"
63 self.update_date = "2016-05-24 07:16:19.477097"
64 self.update_step = -0.05
65 super(TestResultBase, self).setUp()
66 self.details = Details(timestart='0', duration='9s', status='OK')
67 self.req_d = result_models.ResultCreateRequest(
69 project_name=self.project,
71 installer=self.installer,
73 start_date=self.start_date,
74 stop_date=self.stop_date,
75 details=self.details.format(),
76 build_tag=self.build_tag,
77 scenario=self.scenario,
78 criteria=self.criteria,
79 trust_indicator=self.trust_indicator)
80 self.get_res = result_models.TestResult
81 self.list_res = result_models.TestResults
82 self.update_res = result_models.TestResult
83 self.basePath = '/api/v1/results'
84 self.req_pod = pod_models.PodCreateRequest(
88 self.req_project = project_models.ProjectCreateRequest(
91 self.req_testcase = testcase_models.TestcaseCreateRequest(
95 self.create_help('/api/v1/pods', self.req_pod)
96 self.create_help('/api/v1/projects', self.req_project)
97 self.create_help('/api/v1/projects/%s/cases',
101 def assert_res(self, code, result, req=None):
102 self.assertEqual(code, constants.HTTP_OK)
105 self.assertEqual(result.pod_name, req.pod_name)
106 self.assertEqual(result.project_name, req.project_name)
107 self.assertEqual(result.case_name, req.case_name)
108 self.assertEqual(result.installer, req.installer)
109 self.assertEqual(result.version, req.version)
110 details_req = Details.from_dict(req.details)
111 details_res = Details.from_dict(result.details)
112 self.assertEqual(details_res.duration, details_req.duration)
113 self.assertEqual(details_res.timestart, details_req.timestart)
114 self.assertEqual(details_res.status, details_req.status)
115 self.assertEqual(details_res.items, details_req.items)
116 self.assertEqual(result.build_tag, req.build_tag)
117 self.assertEqual(result.scenario, req.scenario)
118 self.assertEqual(result.criteria, req.criteria)
119 self.assertEqual(result.start_date, req.start_date)
120 self.assertEqual(result.stop_date, req.stop_date)
121 self.assertIsNotNone(result._id)
122 ti = result.trust_indicator
123 self.assertEqual(ti.current, req.trust_indicator.current)
125 history = ti.histories[0]
126 self.assertEqual(history.date, self.update_date)
127 self.assertEqual(history.step, self.update_step)
130 _, res = self.create_d()
131 return res.href.split('/')[-1]
134 class TestResultCreate(TestResultBase):
135 def test_nobody(self):
136 (code, body) = self.create(None)
137 self.assertEqual(code, constants.HTTP_BAD_REQUEST)
138 self.assertIn('no body', body)
140 def test_podNotProvided(self):
143 (code, body) = self.create(req)
144 self.assertEqual(code, constants.HTTP_BAD_REQUEST)
145 self.assertIn('pod_name missing', body)
147 def test_projectNotProvided(self):
149 req.project_name = None
150 (code, body) = self.create(req)
151 self.assertEqual(code, constants.HTTP_BAD_REQUEST)
152 self.assertIn('project_name missing', body)
154 def test_testcaseNotProvided(self):
157 (code, body) = self.create(req)
158 self.assertEqual(code, constants.HTTP_BAD_REQUEST)
159 self.assertIn('case_name missing', body)
161 def test_noPod(self):
163 req.pod_name = 'notExistPod'
164 (code, body) = self.create(req)
165 self.assertEqual(code, constants.HTTP_NOT_FOUND)
166 self.assertIn('Could not find pod', body)
168 def test_noProject(self):
170 req.project_name = 'notExistProject'
171 (code, body) = self.create(req)
172 self.assertEqual(code, constants.HTTP_NOT_FOUND)
173 self.assertIn('Could not find project', body)
175 def test_noTestcase(self):
177 req.case_name = 'notExistTestcase'
178 (code, body) = self.create(req)
179 self.assertEqual(code, constants.HTTP_NOT_FOUND)
180 self.assertIn('Could not find testcase', body)
182 def test_success(self):
183 (code, body) = self.create_d()
184 self.assertEqual(code, constants.HTTP_OK)
185 self.assert_href(body)
187 def test_key_with_doc(self):
188 req = copy.deepcopy(self.req_d)
189 req.details = {'1.name': 'dot_name'}
190 (code, body) = self.create(req)
191 self.assertEqual(code, constants.HTTP_OK)
192 self.assert_href(body)
194 def test_no_ti(self):
195 req = result_models.ResultCreateRequest(pod_name=self.pod,
196 project_name=self.project,
198 installer=self.installer,
199 version=self.version,
200 start_date=self.start_date,
201 stop_date=self.stop_date,
202 details=self.details.format(),
203 build_tag=self.build_tag,
204 scenario=self.scenario,
205 criteria=self.criteria)
206 (code, res) = self.create(req)
207 _id = res.href.split('/')[-1]
208 self.assertEqual(code, constants.HTTP_OK)
209 code, body = self.get(_id)
210 self.assert_res(code, body, req)
213 class TestResultGet(TestResultBase):
214 def test_getOne(self):
215 _id = self._create_d()
216 code, body = self.get(_id)
217 self.assert_res(code, body)
219 def test_queryPod(self):
220 self._query_and_assert(self._set_query('pod'))
222 def test_queryProject(self):
223 self._query_and_assert(self._set_query('project'))
225 def test_queryTestcase(self):
226 self._query_and_assert(self._set_query('case'))
228 def test_queryVersion(self):
229 self._query_and_assert(self._set_query('version'))
231 def test_queryInstaller(self):
232 self._query_and_assert(self._set_query('installer'))
234 def test_queryBuildTag(self):
235 self._query_and_assert(self._set_query('build_tag'))
237 def test_queryScenario(self):
238 self._query_and_assert(self._set_query('scenario'))
240 def test_queryTrustIndicator(self):
241 self._query_and_assert(self._set_query('trust_indicator'))
243 def test_queryCriteria(self):
244 self._query_and_assert(self._set_query('criteria'))
246 def test_queryPeriodNotInt(self):
247 code, body = self.query(self._set_query('period=a'))
248 self.assertEqual(code, constants.HTTP_BAD_REQUEST)
249 self.assertIn('period must be int', body)
251 def test_queryPeriodFail(self):
252 self._query_and_assert(self._set_query('period=1'),
253 found=False, days=-10)
255 def test_queryPeriodSuccess(self):
256 self._query_and_assert(self._set_query('period=1'),
259 def test_queryLastNotInt(self):
260 code, body = self.query(self._set_query('last=a'))
261 self.assertEqual(code, constants.HTTP_BAD_REQUEST)
262 self.assertIn('last must be int', body)
264 def test_queryLast(self):
265 self._create_changed_date()
266 req = self._create_changed_date(minutes=20)
267 self._create_changed_date(minutes=-20)
268 self._query_and_assert(self._set_query('last=1'), req=req)
270 def test_combination(self):
271 self._query_and_assert(self._set_query('pod',
282 def test_notFound(self):
283 self._query_and_assert(self._set_query('pod=notExistPod',
295 def _query_and_assert(self, query, found=True, req=None, **kwargs):
297 req = self._create_changed_date(**kwargs)
298 code, body = self.query(query)
300 self.assertEqual(code, constants.HTTP_OK)
301 self.assertEqual(0, len(body.results))
303 self.assertEqual(1, len(body.results))
304 for result in body.results:
305 self.assert_res(code, result, req)
307 def _create_changed_date(self, **kwargs):
308 req = copy.deepcopy(self.req_d)
309 req.start_date = datetime.now() + timedelta(**kwargs)
310 req.stop_date = str(req.start_date + timedelta(minutes=10))
311 req.start_date = str(req.start_date)
315 def _set_query(self, *args):
317 return self.__getattribute__(arg) \
318 if arg != 'trust_indicator' else self.trust_indicator.current
324 uri += '{}={}&'.format(arg, get_value(arg))
328 class TestResultUpdate(TestResultBase):
329 def test_success(self):
330 _id = self._create_d()
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 code, body = self.update(update, _id)
340 self.assertEqual(_id, body._id)
341 self.assert_res(code, body, new_data)
343 code, new_body = self.get(_id)
344 self.assertEqual(_id, new_body._id)
345 self.assert_res(code, new_body, new_data)
348 if __name__ == '__main__':