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
11 from datetime import timedelta
17 from opnfv_testapi.common import message
18 from opnfv_testapi.models import project_models
19 from opnfv_testapi.models import result_models
20 from opnfv_testapi.models import testcase_models
21 from opnfv_testapi.tests.unit import executor
22 from opnfv_testapi.tests.unit import fake_pymongo
23 from opnfv_testapi.tests.unit.handlers import test_base as base
26 class Details(object):
27 def __init__(self, timestart=None, duration=None, status=None):
28 self.timestart = timestart
29 self.duration = duration
31 self.items = [{'item1': 1}, {'item2': 2}]
35 "timestart": self.timestart,
36 "duration": self.duration,
37 "status": self.status,
38 'items': [{'item1': 1}, {'item2': 2}]
42 def from_dict(a_dict):
48 t.timestart = a_dict.get('timestart')
49 t.duration = a_dict.get('duration')
50 t.status = a_dict.get('status')
51 t.items = a_dict.get('items')
55 class TestResultBase(base.TestBase):
57 super(TestResultBase, self).setUp()
58 self.pod = self.pod_d.name
59 self.project = 'functest'
61 self.installer = 'fuel'
63 self.build_tag = 'v3.0'
64 self.scenario = 'odl-l2'
65 self.criteria = 'PASS'
66 self.trust_indicator = result_models.TI(0.7)
67 self.start_date = str(datetime.now())
68 self.stop_date = str(datetime.now() + timedelta(minutes=1))
69 self.update_date = str(datetime.now() + timedelta(days=1))
70 self.update_step = -0.05
71 self.details = Details(timestart='0', duration='9s', status='OK')
72 self.req_d = result_models.ResultCreateRequest(
74 project_name=self.project,
76 installer=self.installer,
78 start_date=self.start_date,
79 stop_date=self.stop_date,
80 details=self.details.format(),
81 build_tag=self.build_tag,
82 scenario=self.scenario,
83 criteria=self.criteria,
84 trust_indicator=self.trust_indicator)
85 self.get_res = result_models.TestResult
86 self.list_res = result_models.TestResults
87 self.update_res = result_models.TestResult
88 self.basePath = '/api/v1/results'
89 self.req_project = project_models.ProjectCreateRequest(
92 self.req_testcase = testcase_models.TestcaseCreateRequest(
96 fake_pymongo.pods.insert(self.pod_d.format())
97 self.create_help('/api/v1/projects', self.req_project)
98 self.create_help('/api/v1/projects/%s/cases',
102 def assert_res(self, result, req=None):
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]
133 def upload(self, req):
134 if req and not isinstance(req, str) and hasattr(req, 'format'):
136 res = self.fetch(self.basePath + '/upload',
138 body=json.dumps(req),
139 headers=self.headers)
141 return self._get_return(res, self.create_res)
144 class TestResultUpload(TestResultBase):
145 @executor.upload(httplib.BAD_REQUEST, message.key_error('file'))
146 def test_filenotfind(self):
150 class TestResultCreate(TestResultBase):
151 @executor.create(httplib.BAD_REQUEST, message.no_body())
152 def test_nobody(self):
155 @executor.create(httplib.BAD_REQUEST, message.missing('pod_name'))
156 def test_podNotProvided(self):
161 @executor.create(httplib.BAD_REQUEST, message.missing('project_name'))
162 def test_projectNotProvided(self):
164 req.project_name = None
167 @executor.create(httplib.BAD_REQUEST, message.missing('case_name'))
168 def test_testcaseNotProvided(self):
173 @executor.create(httplib.BAD_REQUEST,
174 message.invalid_value('criteria', ['PASS', 'FAIL']))
175 def test_invalid_criteria(self):
177 req.criteria = 'invalid'
180 @executor.create(httplib.FORBIDDEN, message.not_found_base)
181 def test_noPod(self):
183 req.pod_name = 'notExistPod'
186 @executor.create(httplib.FORBIDDEN, message.not_found_base)
187 def test_noProject(self):
189 req.project_name = 'notExistProject'
192 @executor.create(httplib.FORBIDDEN, message.not_found_base)
193 def test_noTestcase(self):
195 req.case_name = 'notExistTestcase'
198 @executor.create(httplib.OK, 'assert_href')
199 def test_success(self):
202 @executor.create(httplib.OK, 'assert_href')
203 def test_key_with_doc(self):
204 req = copy.deepcopy(self.req_d)
205 req.details = {'1.name': 'dot_name'}
208 @executor.create(httplib.OK, '_assert_no_ti')
209 def test_no_ti(self):
210 req = result_models.ResultCreateRequest(pod_name=self.pod,
211 project_name=self.project,
213 installer=self.installer,
214 version=self.version,
215 start_date=self.start_date,
216 stop_date=self.stop_date,
217 details=self.details.format(),
218 build_tag=self.build_tag,
219 scenario=self.scenario,
220 criteria=self.criteria)
221 self.actual_req = req
224 def _assert_no_ti(self, body):
225 _id = body.href.split('/')[-1]
226 code, body = self.get(_id)
227 self.assert_res(body, self.actual_req)
230 class TestResultGet(TestResultBase):
232 super(TestResultGet, self).setUp()
233 self.req_10d_before = self._create_changed_date(days=-10)
234 self.req_d_id = self._create_d()
235 self.req_10d_later = self._create_changed_date(days=10)
237 @executor.get(httplib.OK, 'assert_res')
238 def test_getOne(self):
241 @executor.query(httplib.OK, '_query_success', 3)
242 def test_queryPod(self):
243 return self._set_query('pod')
245 @executor.query(httplib.OK, '_query_success', 3)
246 def test_queryProject(self):
247 return self._set_query('project')
249 @executor.query(httplib.OK, '_query_success', 3)
250 def test_queryTestcase(self):
251 return self._set_query('case')
253 @executor.query(httplib.OK, '_query_success', 3)
254 def test_queryVersion(self):
255 return self._set_query('version')
257 @executor.query(httplib.OK, '_query_success', 3)
258 def test_queryInstaller(self):
259 return self._set_query('installer')
261 @executor.query(httplib.OK, '_query_success', 3)
262 def test_queryBuildTag(self):
263 return self._set_query('build_tag')
265 @executor.query(httplib.OK, '_query_success', 3)
266 def test_queryScenario(self):
267 return self._set_query('scenario')
269 @executor.query(httplib.OK, '_query_success', 3)
270 def test_queryTrustIndicator(self):
271 return self._set_query('trust_indicator')
273 @executor.query(httplib.OK, '_query_success', 3)
274 def test_queryCriteria(self):
275 return self._set_query('criteria')
277 @executor.query(httplib.BAD_REQUEST, message.must_int('period'))
278 def test_queryPeriodNotInt(self):
279 return self._set_query(period='a')
281 @executor.query(httplib.OK, '_query_period_one', 1)
282 def test_queryPeriodSuccess(self):
283 return self._set_query(period=5)
285 @executor.query(httplib.BAD_REQUEST, message.must_int('last'))
286 def test_queryLastNotInt(self):
287 return self._set_query(last='a')
289 @executor.query(httplib.OK, '_query_last_one', 1)
290 def test_queryLast(self):
291 return self._set_query(last=1)
293 @executor.query(httplib.OK, '_query_success', 4)
294 def test_queryPublic(self):
295 self._create_public_data()
296 return self._set_query()
298 @executor.query(httplib.OK, '_query_success', 1)
299 def test_queryPrivate(self):
300 self._create_private_data()
301 return self._set_query(public='false')
303 @executor.query(httplib.OK, '_query_period_one', 1)
304 def test_combination(self):
305 return self._set_query('pod',
316 @executor.query(httplib.OK, '_query_success', 0)
317 def test_notFound(self):
318 return self._set_query('project',
329 @executor.query(httplib.OK, '_query_success', 1)
330 def test_filterErrorStartdate(self):
331 self._create_error_start_date(None)
332 self._create_error_start_date('None')
333 self._create_error_start_date('null')
334 self._create_error_start_date('')
335 return self._set_query(period=5)
337 def _query_success(self, body, number):
338 self.assertEqual(number, len(body.results))
340 def _query_last_one(self, body, number):
341 self.assertEqual(number, len(body.results))
342 self.assert_res(body.results[0], self.req_10d_later)
344 def _query_period_one(self, body, number):
345 self.assertEqual(number, len(body.results))
346 self.assert_res(body.results[0], self.req_d)
348 def _create_error_start_date(self, start_date):
349 req = copy.deepcopy(self.req_d)
350 req.start_date = start_date
354 def _create_changed_date(self, **kwargs):
355 req = copy.deepcopy(self.req_d)
356 req.start_date = datetime.now() + timedelta(**kwargs)
357 req.stop_date = str(req.start_date + timedelta(minutes=10))
358 req.start_date = str(req.start_date)
362 def _create_public_data(self, **kwargs):
363 req = copy.deepcopy(self.req_d)
368 def _create_private_data(self, **kwargs):
369 req = copy.deepcopy(self.req_d)
374 def _set_query(self, *args, **kwargs):
376 return self.__getattribute__(arg) \
377 if arg != 'trust_indicator' else self.trust_indicator.current
380 query.append((arg, get_value(arg)))
381 for k, v in kwargs.iteritems():
383 return urllib.urlencode(query)
386 class TestResultUpdate(TestResultBase):
388 super(TestResultUpdate, self).setUp()
389 self.req_d_id = self._create_d()
391 @executor.update(httplib.OK, '_assert_update_ti')
392 def test_success(self):
393 new_ti = copy.deepcopy(self.trust_indicator)
394 new_ti.current += self.update_step
395 new_ti.histories.append(
396 result_models.TIHistory(self.update_date, self.update_step))
397 new_data = copy.deepcopy(self.req_d)
398 new_data.trust_indicator = new_ti
399 update = result_models.ResultUpdateRequest(trust_indicator=new_ti)
400 self.update_req = new_data
401 return update, self.req_d_id
403 def _assert_update_ti(self, request, body):
404 ti = body.trust_indicator
405 self.assertEqual(ti.current, request.trust_indicator.current)
407 history = ti.histories[0]
408 self.assertEqual(history.date, self.update_date)
409 self.assertEqual(history.step, self.update_step)
412 if __name__ == '__main__':