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 ##############################################################################
12 from datetime import datetime, timedelta
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
24 class Details(object):
25 def __init__(self, timestart=None, duration=None, status=None):
26 self.timestart = timestart
27 self.duration = duration
29 self.items = [{'item1': 1}, {'item2': 2}]
33 "timestart": self.timestart,
34 "duration": self.duration,
35 "status": self.status,
36 'items': [{'item1': 1}, {'item2': 2}]
40 def from_dict(a_dict):
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')
53 class TestResultBase(base.TestBase):
56 self.project = 'functest'
58 self.installer = 'fuel'
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(
72 project_name=self.project,
74 installer=self.installer,
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(
91 self.req_project = project_models.ProjectCreateRequest(
94 self.req_testcase = testcase_models.TestcaseCreateRequest(
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',
104 def assert_res(self, result, req=None):
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)
127 history = ti.histories[0]
128 self.assertEqual(history.date, self.update_date)
129 self.assertEqual(history.step, self.update_step)
132 _, res = self.create_d()
133 return res.href.split('/')[-1]
135 def upload(self, req):
136 if req and not isinstance(req, str) and hasattr(req, 'format'):
138 res = self.fetch(self.basePath + '/upload',
140 body=json.dumps(req),
141 headers=self.headers)
143 return self._get_return(res, self.create_res)
146 class TestResultUpload(TestResultBase):
147 @executor.upload(httplib.BAD_REQUEST, message.key_error('file'))
148 def test_filenotfind(self):
152 class TestResultCreate(TestResultBase):
153 @executor.create(httplib.BAD_REQUEST, message.no_body())
154 def test_nobody(self):
157 @executor.create(httplib.BAD_REQUEST, message.missing('pod_name'))
158 def test_podNotProvided(self):
163 @executor.create(httplib.BAD_REQUEST, message.missing('project_name'))
164 def test_projectNotProvided(self):
166 req.project_name = None
169 @executor.create(httplib.BAD_REQUEST, message.missing('case_name'))
170 def test_testcaseNotProvided(self):
175 @executor.create(httplib.FORBIDDEN, message.not_found_base)
176 def test_noPod(self):
178 req.pod_name = 'notExistPod'
181 @executor.create(httplib.FORBIDDEN, message.not_found_base)
182 def test_noProject(self):
184 req.project_name = 'notExistProject'
187 @executor.create(httplib.FORBIDDEN, message.not_found_base)
188 def test_noTestcase(self):
190 req.case_name = 'notExistTestcase'
193 @executor.create(httplib.OK, 'assert_href')
194 def test_success(self):
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'}
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,
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
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)
225 class TestResultGet(TestResultBase):
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)
232 @executor.get(httplib.OK, 'assert_res')
233 def test_getOne(self):
236 @executor.query(httplib.OK, '_query_success', 3)
237 def test_queryPod(self):
238 return self._set_query('pod')
240 @executor.query(httplib.OK, '_query_success', 3)
241 def test_queryProject(self):
242 return self._set_query('project')
244 @executor.query(httplib.OK, '_query_success', 3)
245 def test_queryTestcase(self):
246 return self._set_query('case')
248 @executor.query(httplib.OK, '_query_success', 3)
249 def test_queryVersion(self):
250 return self._set_query('version')
252 @executor.query(httplib.OK, '_query_success', 3)
253 def test_queryInstaller(self):
254 return self._set_query('installer')
256 @executor.query(httplib.OK, '_query_success', 3)
257 def test_queryBuildTag(self):
258 return self._set_query('build_tag')
260 @executor.query(httplib.OK, '_query_success', 3)
261 def test_queryScenario(self):
262 return self._set_query('scenario')
264 @executor.query(httplib.OK, '_query_success', 3)
265 def test_queryTrustIndicator(self):
266 return self._set_query('trust_indicator')
268 @executor.query(httplib.OK, '_query_success', 3)
269 def test_queryCriteria(self):
270 return self._set_query('criteria')
272 @executor.query(httplib.BAD_REQUEST, message.must_int('period'))
273 def test_queryPeriodNotInt(self):
274 return self._set_query('period=a')
276 @executor.query(httplib.OK, '_query_period_one', 1)
277 def test_queryPeriodSuccess(self):
278 return self._set_query('period=5')
280 @executor.query(httplib.BAD_REQUEST, message.must_int('last'))
281 def test_queryLastNotInt(self):
282 return self._set_query('last=a')
284 @executor.query(httplib.OK, '_query_last_one', 1)
285 def test_queryLast(self):
286 return self._set_query('last=1')
288 @executor.query(httplib.OK, '_query_success', 4)
289 def test_queryPublic(self):
290 self._create_public_data()
291 return self._set_query('')
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')
298 @executor.query(httplib.OK, '_query_period_one', 1)
299 def test_combination(self):
300 return self._set_query('pod',
311 @executor.query(httplib.OK, '_query_success', 0)
312 def test_notFound(self):
313 return self._set_query('pod=notExistPod',
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')
332 def _query_success(self, body, number):
333 self.assertEqual(number, len(body.results))
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)
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)
343 def _create_error_start_date(self, start_date):
344 req = copy.deepcopy(self.req_d)
345 req.start_date = start_date
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)
357 def _create_public_data(self, **kwargs):
358 req = copy.deepcopy(self.req_d)
363 def _create_private_data(self, **kwargs):
364 req = copy.deepcopy(self.req_d)
369 def _set_query(self, *args):
371 return self.__getattribute__(arg) \
372 if arg != 'trust_indicator' else self.trust_indicator.current
379 uri += '{}={}&'.format(arg, get_value(arg))
383 class TestResultUpdate(TestResultBase):
385 super(TestResultUpdate, self).setUp()
386 self.req_d_id = self._create_d()
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
400 def _assert_update_ti(self, request, body):
401 ti = body.trust_indicator
402 self.assertEqual(ti.current, request.trust_indicator.current)
404 history = ti.histories[0]
405 self.assertEqual(history.date, self.update_date)
406 self.assertEqual(history.step, self.update_step)
409 if __name__ == '__main__':