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 ##############################################################################
11 from datetime import datetime, timedelta
13 from opnfv_testapi.common.constants import HTTP_OK, HTTP_BAD_REQUEST, \
15 from opnfv_testapi.resources.pod_models import PodCreateRequest
16 from opnfv_testapi.resources.project_models import ProjectCreateRequest
17 from opnfv_testapi.resources.result_models import ResultCreateRequest, \
18 TestResult, TestResults, ResultUpdateRequest, TI, TIHistory
19 from opnfv_testapi.resources.testcase_models import TestcaseCreateRequest
20 from test_base import TestBase
23 class Details(object):
24 def __init__(self, timestart=None, duration=None, status=None):
25 self.timestart = timestart
26 self.duration = duration
31 "timestart": self.timestart,
32 "duration": self.duration,
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')
49 class TestResultBase(TestBase):
52 self.project = 'functest'
54 self.installer = 'fuel'
56 self.build_tag = 'v3.0'
57 self.scenario = 'odl-l2'
58 self.criteria = 'passed'
59 self.trust_indicator = TI(0.7)
60 self.start_date = "2016-05-23 07:16:09.477097"
61 self.stop_date = "2016-05-23 07:16:19.477097"
62 self.update_date = "2016-05-24 07:16:19.477097"
63 self.update_step = -0.05
64 super(TestResultBase, self).setUp()
65 self.details = Details(timestart='0', duration='9s', status='OK')
66 self.req_d = ResultCreateRequest(pod_name=self.pod,
67 project_name=self.project,
69 installer=self.installer,
71 start_date=self.start_date,
72 stop_date=self.stop_date,
73 details=self.details.format(),
74 build_tag=self.build_tag,
75 scenario=self.scenario,
76 criteria=self.criteria,
77 trust_indicator=self.trust_indicator)
78 self.get_res = TestResult
79 self.list_res = TestResults
80 self.update_res = TestResult
81 self.basePath = '/api/v1/results'
82 self.req_pod = PodCreateRequest(self.pod, 'metal', 'zte pod 1')
83 self.req_project = ProjectCreateRequest(self.project, 'vping test')
84 self.req_testcase = TestcaseCreateRequest(self.case,
87 self.create_help('/api/v1/pods', self.req_pod)
88 self.create_help('/api/v1/projects', self.req_project)
89 self.create_help('/api/v1/projects/%s/cases',
93 def assert_res(self, code, result, req=None):
94 self.assertEqual(code, HTTP_OK)
97 self.assertEqual(result.pod_name, req.pod_name)
98 self.assertEqual(result.project_name, req.project_name)
99 self.assertEqual(result.case_name, req.case_name)
100 self.assertEqual(result.installer, req.installer)
101 self.assertEqual(result.version, req.version)
102 details_req = Details.from_dict(req.details)
103 details_res = Details.from_dict(result.details)
104 self.assertEqual(details_res.duration, details_req.duration)
105 self.assertEqual(details_res.timestart, details_req.timestart)
106 self.assertEqual(details_res.status, details_req.status)
107 self.assertEqual(result.build_tag, req.build_tag)
108 self.assertEqual(result.scenario, req.scenario)
109 self.assertEqual(result.criteria, req.criteria)
110 self.assertEqual(result.start_date, req.start_date)
111 self.assertEqual(result.stop_date, req.stop_date)
112 self.assertIsNotNone(result._id)
113 ti = result.trust_indicator
114 self.assertEqual(ti.current, req.trust_indicator.current)
116 history = ti.histories[0]
117 self.assertEqual(history.date, self.update_date)
118 self.assertEqual(history.step, self.update_step)
121 _, res = self.create_d()
122 return res.href.split('/')[-1]
125 class TestResultCreate(TestResultBase):
126 def test_nobody(self):
127 (code, body) = self.create(None)
128 self.assertEqual(code, HTTP_BAD_REQUEST)
129 self.assertIn('no body', body)
131 def test_podNotProvided(self):
134 (code, body) = self.create(req)
135 self.assertEqual(code, HTTP_BAD_REQUEST)
136 self.assertIn('pod_name missing', body)
138 def test_projectNotProvided(self):
140 req.project_name = None
141 (code, body) = self.create(req)
142 self.assertEqual(code, HTTP_BAD_REQUEST)
143 self.assertIn('project_name missing', body)
145 def test_testcaseNotProvided(self):
148 (code, body) = self.create(req)
149 self.assertEqual(code, HTTP_BAD_REQUEST)
150 self.assertIn('case_name missing', body)
152 def test_noPod(self):
154 req.pod_name = 'notExistPod'
155 (code, body) = self.create(req)
156 self.assertEqual(code, HTTP_NOT_FOUND)
157 self.assertIn('Could not find pod', body)
159 def test_noProject(self):
161 req.project_name = 'notExistProject'
162 (code, body) = self.create(req)
163 self.assertEqual(code, HTTP_NOT_FOUND)
164 self.assertIn('Could not find project', body)
166 def test_noTestcase(self):
168 req.case_name = 'notExistTestcase'
169 (code, body) = self.create(req)
170 self.assertEqual(code, HTTP_NOT_FOUND)
171 self.assertIn('Could not find testcase', body)
173 def test_success(self):
174 (code, body) = self.create_d()
175 self.assertEqual(code, HTTP_OK)
176 self.assert_href(body)
178 def test_key_with_doc(self):
179 req = copy.deepcopy(self.req_d)
180 req.details = {'1.name': 'dot_name'}
181 (code, body) = self.create(req)
182 self.assertEqual(code, HTTP_OK)
183 self.assert_href(body)
186 class TestResultGet(TestResultBase):
187 def test_getOne(self):
188 _id = self._create_d()
189 code, body = self.get(_id)
190 self.assert_res(code, body)
192 def test_queryPod(self):
193 self._query_and_assert(self._set_query('pod'))
195 def test_queryProject(self):
196 self._query_and_assert(self._set_query('project'))
198 def test_queryTestcase(self):
199 self._query_and_assert(self._set_query('case'))
201 def test_queryVersion(self):
202 self._query_and_assert(self._set_query('version'))
204 def test_queryInstaller(self):
205 self._query_and_assert(self._set_query('installer'))
207 def test_queryBuildTag(self):
208 self._query_and_assert(self._set_query('build_tag'))
210 def test_queryScenario(self):
211 self._query_and_assert(self._set_query('scenario'))
213 def test_queryTrustIndicator(self):
214 self._query_and_assert(self._set_query('trust_indicator'))
216 def test_queryCriteria(self):
217 self._query_and_assert(self._set_query('criteria'))
219 def test_queryPeriodNotInt(self):
220 code, body = self.query(self._set_query('period=a'))
221 self.assertEqual(code, HTTP_BAD_REQUEST)
222 self.assertIn('period must be int', body)
224 def test_queryPeriodFail(self):
225 self._query_and_assert(self._set_query('period=1'),
226 found=False, days=-10)
228 def test_queryPeriodSuccess(self):
229 self._query_and_assert(self._set_query('period=1'),
232 def test_queryLastNotInt(self):
233 code, body = self.query(self._set_query('last=a'))
234 self.assertEqual(code, HTTP_BAD_REQUEST)
235 self.assertIn('last must be int', body)
237 def test_queryLast(self):
238 self._create_changed_date()
239 req = self._create_changed_date(minutes=20)
240 self._create_changed_date(minutes=-20)
241 self._query_and_assert(self._set_query('last=1'), req=req)
243 def test_combination(self):
244 self._query_and_assert(self._set_query('pod',
255 def test_notFound(self):
256 self._query_and_assert(self._set_query('pod=notExistPod',
268 def _query_and_assert(self, query, found=True, req=None, **kwargs):
270 req = self._create_changed_date(**kwargs)
271 code, body = self.query(query)
273 self.assertEqual(code, HTTP_OK)
274 self.assertEqual(0, len(body.results))
276 self.assertEqual(1, len(body.results))
277 for result in body.results:
278 self.assert_res(code, result, req)
280 def _create_changed_date(self, **kwargs):
281 req = copy.deepcopy(self.req_d)
282 req.start_date = datetime.now() + timedelta(**kwargs)
283 req.stop_date = str(req.start_date + timedelta(minutes=10))
284 req.start_date = str(req.start_date)
288 def _set_query(self, *args):
290 return eval('self.' + arg) \
291 if arg != 'trust_indicator' else self.trust_indicator.current
297 uri += '{}={}&'.format(arg, get_value(arg))
301 class TestResultUpdate(TestResultBase):
302 def test_success(self):
303 _id = self._create_d()
305 new_ti = copy.deepcopy(self.trust_indicator)
306 new_ti.current += self.update_step
307 new_ti.histories.append(TIHistory(self.update_date, self.update_step))
308 new_data = copy.deepcopy(self.req_d)
309 new_data.trust_indicator = new_ti
310 update = ResultUpdateRequest(trust_indicator=new_ti)
311 code, body = self.update(update, _id)
312 self.assertEqual(_id, body._id)
313 self.assert_res(code, body, new_data)
315 code, new_body = self.get(_id)
316 self.assertEqual(_id, new_body._id)
317 self.assert_res(code, new_body, new_data)
320 if __name__ == '__main__':