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.resources import project_models
19 from opnfv_testapi.resources import result_models
20 from opnfv_testapi.resources 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.resources 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 = 'passed'
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.FORBIDDEN, message.not_found_base)
174 def test_noPod(self):
176 req.pod_name = 'notExistPod'
179 @executor.create(httplib.FORBIDDEN, message.not_found_base)
180 def test_noProject(self):
182 req.project_name = 'notExistProject'
185 @executor.create(httplib.FORBIDDEN, message.not_found_base)
186 def test_noTestcase(self):
188 req.case_name = 'notExistTestcase'
191 @executor.create(httplib.OK, 'assert_href')
192 def test_success(self):
195 @executor.create(httplib.OK, 'assert_href')
196 def test_key_with_doc(self):
197 req = copy.deepcopy(self.req_d)
198 req.details = {'1.name': 'dot_name'}
201 @executor.create(httplib.OK, '_assert_no_ti')
202 def test_no_ti(self):
203 req = result_models.ResultCreateRequest(pod_name=self.pod,
204 project_name=self.project,
206 installer=self.installer,
207 version=self.version,
208 start_date=self.start_date,
209 stop_date=self.stop_date,
210 details=self.details.format(),
211 build_tag=self.build_tag,
212 scenario=self.scenario,
213 criteria=self.criteria)
214 self.actual_req = req
217 def _assert_no_ti(self, body):
218 _id = body.href.split('/')[-1]
219 code, body = self.get(_id)
220 self.assert_res(body, self.actual_req)
223 class TestResultGet(TestResultBase):
225 super(TestResultGet, self).setUp()
226 self.req_10d_before = self._create_changed_date(days=-10)
227 self.req_d_id = self._create_d()
228 self.req_10d_later = self._create_changed_date(days=10)
230 @executor.get(httplib.OK, 'assert_res')
231 def test_getOne(self):
234 @executor.query(httplib.OK, '_query_success', 3)
235 def test_queryPod(self):
236 return self._set_query('pod')
238 @executor.query(httplib.OK, '_query_success', 3)
239 def test_queryProject(self):
240 return self._set_query('project')
242 @executor.query(httplib.OK, '_query_success', 3)
243 def test_queryTestcase(self):
244 return self._set_query('case')
246 @executor.query(httplib.OK, '_query_success', 3)
247 def test_queryVersion(self):
248 return self._set_query('version')
250 @executor.query(httplib.OK, '_query_success', 3)
251 def test_queryInstaller(self):
252 return self._set_query('installer')
254 @executor.query(httplib.OK, '_query_success', 3)
255 def test_queryBuildTag(self):
256 return self._set_query('build_tag')
258 @executor.query(httplib.OK, '_query_success', 3)
259 def test_queryScenario(self):
260 return self._set_query('scenario')
262 @executor.query(httplib.OK, '_query_success', 3)
263 def test_queryTrustIndicator(self):
264 return self._set_query('trust_indicator')
266 @executor.query(httplib.OK, '_query_success', 3)
267 def test_queryCriteria(self):
268 return self._set_query('criteria')
270 @executor.query(httplib.BAD_REQUEST, message.must_int('period'))
271 def test_queryPeriodNotInt(self):
272 return self._set_query(period='a')
274 @executor.query(httplib.OK, '_query_period_one', 1)
275 def test_queryPeriodSuccess(self):
276 return self._set_query(period=5)
278 @executor.query(httplib.BAD_REQUEST, message.must_int('last'))
279 def test_queryLastNotInt(self):
280 return self._set_query(last='a')
282 @executor.query(httplib.OK, '_query_last_one', 1)
283 def test_queryLast(self):
284 return self._set_query(last=1)
286 @executor.query(httplib.OK, '_query_success', 4)
287 def test_queryPublic(self):
288 self._create_public_data()
289 return self._set_query()
291 @executor.query(httplib.OK, '_query_success', 1)
292 def test_queryPrivate(self):
293 self._create_private_data()
294 return self._set_query(public='false')
296 @executor.query(httplib.OK, '_query_period_one', 1)
297 def test_combination(self):
298 return self._set_query('pod',
309 @executor.query(httplib.OK, '_query_success', 0)
310 def test_notFound(self):
311 return self._set_query('project',
322 @executor.query(httplib.OK, '_query_success', 1)
323 def test_filterErrorStartdate(self):
324 self._create_error_start_date(None)
325 self._create_error_start_date('None')
326 self._create_error_start_date('null')
327 self._create_error_start_date('')
328 return self._set_query(period=5)
330 def _query_success(self, body, number):
331 self.assertEqual(number, len(body.results))
333 def _query_last_one(self, body, number):
334 self.assertEqual(number, len(body.results))
335 self.assert_res(body.results[0], self.req_10d_later)
337 def _query_period_one(self, body, number):
338 self.assertEqual(number, len(body.results))
339 self.assert_res(body.results[0], self.req_d)
341 def _create_error_start_date(self, start_date):
342 req = copy.deepcopy(self.req_d)
343 req.start_date = start_date
347 def _create_changed_date(self, **kwargs):
348 req = copy.deepcopy(self.req_d)
349 req.start_date = datetime.now() + timedelta(**kwargs)
350 req.stop_date = str(req.start_date + timedelta(minutes=10))
351 req.start_date = str(req.start_date)
355 def _create_public_data(self, **kwargs):
356 req = copy.deepcopy(self.req_d)
361 def _create_private_data(self, **kwargs):
362 req = copy.deepcopy(self.req_d)
367 def _set_query(self, *args, **kwargs):
369 return self.__getattribute__(arg) \
370 if arg != 'trust_indicator' else self.trust_indicator.current
373 query.append((arg, get_value(arg)))
374 for k, v in kwargs.iteritems():
376 return urllib.urlencode(query)
379 class TestResultUpdate(TestResultBase):
381 super(TestResultUpdate, self).setUp()
382 self.req_d_id = self._create_d()
384 @executor.update(httplib.OK, '_assert_update_ti')
385 def test_success(self):
386 new_ti = copy.deepcopy(self.trust_indicator)
387 new_ti.current += self.update_step
388 new_ti.histories.append(
389 result_models.TIHistory(self.update_date, self.update_step))
390 new_data = copy.deepcopy(self.req_d)
391 new_data.trust_indicator = new_ti
392 update = result_models.ResultUpdateRequest(trust_indicator=new_ti)
393 self.update_req = new_data
394 return update, self.req_d_id
396 def _assert_update_ti(self, request, body):
397 ti = body.trust_indicator
398 self.assertEqual(ti.current, request.trust_indicator.current)
400 history = ti.histories[0]
401 self.assertEqual(history.date, self.update_date)
402 self.assertEqual(history.step, self.update_step)
405 if __name__ == '__main__':