Merge "Remove compass4nfv weekly danube job"
[releng.git] / utils / test / testapi / opnfv_testapi / tests / unit / handlers / test_result.py
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 ##############################################################################
9 import copy
10 from datetime import datetime
11 from datetime import timedelta
12 import httplib
13 import json
14 import urllib
15 import unittest
16
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
24
25
26 class Details(object):
27     def __init__(self, timestart=None, duration=None, status=None):
28         self.timestart = timestart
29         self.duration = duration
30         self.status = status
31         self.items = [{'item1': 1}, {'item2': 2}]
32
33     def format(self):
34         return {
35             "timestart": self.timestart,
36             "duration": self.duration,
37             "status": self.status,
38             'items': [{'item1': 1}, {'item2': 2}]
39         }
40
41     @staticmethod
42     def from_dict(a_dict):
43
44         if a_dict is None:
45             return None
46
47         t = Details()
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')
52         return t
53
54
55 class TestResultBase(base.TestBase):
56     def setUp(self):
57         super(TestResultBase, self).setUp()
58         self.pod = self.pod_d.name
59         self.project = 'functest'
60         self.case = 'vPing'
61         self.installer = 'fuel'
62         self.version = 'C'
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(
73             pod_name=self.pod,
74             project_name=self.project,
75             case_name=self.case,
76             installer=self.installer,
77             version=self.version,
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(
90             self.project,
91             'vping test')
92         self.req_testcase = testcase_models.TestcaseCreateRequest(
93             self.case,
94             '/cases/vping',
95             'vping-ssh test')
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',
99                          self.req_testcase,
100                          self.project)
101
102     def assert_res(self, result, req=None):
103         if req is None:
104             req = self.req_d
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)
124         if ti.histories:
125             history = ti.histories[0]
126             self.assertEqual(history.date, self.update_date)
127             self.assertEqual(history.step, self.update_step)
128
129     def _create_d(self):
130         _, res = self.create_d()
131         return res.href.split('/')[-1]
132
133     def upload(self, req):
134         if req and not isinstance(req, str) and hasattr(req, 'format'):
135             req = req.format()
136         res = self.fetch(self.basePath + '/upload',
137                          method='POST',
138                          body=json.dumps(req),
139                          headers=self.headers)
140
141         return self._get_return(res, self.create_res)
142
143
144 class TestResultUpload(TestResultBase):
145     @executor.upload(httplib.BAD_REQUEST, message.key_error('file'))
146     def test_filenotfind(self):
147         return None
148
149
150 class TestResultCreate(TestResultBase):
151     @executor.create(httplib.BAD_REQUEST, message.no_body())
152     def test_nobody(self):
153         return None
154
155     @executor.create(httplib.BAD_REQUEST, message.missing('pod_name'))
156     def test_podNotProvided(self):
157         req = self.req_d
158         req.pod_name = None
159         return req
160
161     @executor.create(httplib.BAD_REQUEST, message.missing('project_name'))
162     def test_projectNotProvided(self):
163         req = self.req_d
164         req.project_name = None
165         return req
166
167     @executor.create(httplib.BAD_REQUEST, message.missing('case_name'))
168     def test_testcaseNotProvided(self):
169         req = self.req_d
170         req.case_name = None
171         return req
172
173     @executor.create(httplib.BAD_REQUEST,
174                      message.invalid_value('criteria', ['PASS', 'FAIL']))
175     def test_invalid_criteria(self):
176         req = self.req_d
177         req.criteria = 'invalid'
178         return req
179
180     @executor.create(httplib.FORBIDDEN, message.not_found_base)
181     def test_noPod(self):
182         req = self.req_d
183         req.pod_name = 'notExistPod'
184         return req
185
186     @executor.create(httplib.FORBIDDEN, message.not_found_base)
187     def test_noProject(self):
188         req = self.req_d
189         req.project_name = 'notExistProject'
190         return req
191
192     @executor.create(httplib.FORBIDDEN, message.not_found_base)
193     def test_noTestcase(self):
194         req = self.req_d
195         req.case_name = 'notExistTestcase'
196         return req
197
198     @executor.create(httplib.OK, 'assert_href')
199     def test_success(self):
200         return self.req_d
201
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'}
206         return req
207
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,
212                                                 case_name=self.case,
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
222         return req
223
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)
228
229
230 class TestResultGet(TestResultBase):
231     def setUp(self):
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)
236
237     @executor.get(httplib.OK, 'assert_res')
238     def test_getOne(self):
239         return self.req_d_id
240
241     @executor.query(httplib.OK, '_query_success', 3)
242     def test_queryPod(self):
243         return self._set_query('pod')
244
245     @executor.query(httplib.OK, '_query_success', 3)
246     def test_queryProject(self):
247         return self._set_query('project')
248
249     @executor.query(httplib.OK, '_query_success', 3)
250     def test_queryTestcase(self):
251         return self._set_query('case')
252
253     @executor.query(httplib.OK, '_query_success', 3)
254     def test_queryVersion(self):
255         return self._set_query('version')
256
257     @executor.query(httplib.OK, '_query_success', 3)
258     def test_queryInstaller(self):
259         return self._set_query('installer')
260
261     @executor.query(httplib.OK, '_query_success', 3)
262     def test_queryBuildTag(self):
263         return self._set_query('build_tag')
264
265     @executor.query(httplib.OK, '_query_success', 3)
266     def test_queryScenario(self):
267         return self._set_query('scenario')
268
269     @executor.query(httplib.OK, '_query_success', 3)
270     def test_queryTrustIndicator(self):
271         return self._set_query('trust_indicator')
272
273     @executor.query(httplib.OK, '_query_success', 3)
274     def test_queryCriteria(self):
275         return self._set_query('criteria')
276
277     @executor.query(httplib.BAD_REQUEST, message.must_int('period'))
278     def test_queryPeriodNotInt(self):
279         return self._set_query(period='a')
280
281     @executor.query(httplib.OK, '_query_period_one', 1)
282     def test_queryPeriodSuccess(self):
283         return self._set_query(period=5)
284
285     @executor.query(httplib.BAD_REQUEST, message.must_int('last'))
286     def test_queryLastNotInt(self):
287         return self._set_query(last='a')
288
289     @executor.query(httplib.OK, '_query_last_one', 1)
290     def test_queryLast(self):
291         return self._set_query(last=1)
292
293     @executor.query(httplib.OK, '_query_success', 4)
294     def test_queryPublic(self):
295         self._create_public_data()
296         return self._set_query()
297
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')
302
303     @executor.query(httplib.OK, '_query_period_one', 1)
304     def test_combination(self):
305         return self._set_query('pod',
306                                'project',
307                                'case',
308                                'version',
309                                'installer',
310                                'build_tag',
311                                'scenario',
312                                'trust_indicator',
313                                'criteria',
314                                period=5)
315
316     @executor.query(httplib.OK, '_query_success', 0)
317     def test_notFound(self):
318         return self._set_query('project',
319                                'case',
320                                'version',
321                                'installer',
322                                'build_tag',
323                                'scenario',
324                                'trust_indicator',
325                                'criteria',
326                                pod='notExistPod',
327                                period=1)
328
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)
336
337     def _query_success(self, body, number):
338         self.assertEqual(number, len(body.results))
339
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)
343
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)
347
348     def _create_error_start_date(self, start_date):
349         req = copy.deepcopy(self.req_d)
350         req.start_date = start_date
351         self.create(req)
352         return req
353
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)
359         self.create(req)
360         return req
361
362     def _create_public_data(self, **kwargs):
363         req = copy.deepcopy(self.req_d)
364         req.public = 'true'
365         self.create(req)
366         return req
367
368     def _create_private_data(self, **kwargs):
369         req = copy.deepcopy(self.req_d)
370         req.public = 'false'
371         self.create(req)
372         return req
373
374     def _set_query(self, *args, **kwargs):
375         def get_value(arg):
376             return self.__getattribute__(arg) \
377                 if arg != 'trust_indicator' else self.trust_indicator.current
378         query = []
379         for arg in args:
380             query.append((arg, get_value(arg)))
381         for k, v in kwargs.iteritems():
382             query.append((k, v))
383         return urllib.urlencode(query)
384
385
386 class TestResultUpdate(TestResultBase):
387     def setUp(self):
388         super(TestResultUpdate, self).setUp()
389         self.req_d_id = self._create_d()
390
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
402
403     def _assert_update_ti(self, request, body):
404         ti = body.trust_indicator
405         self.assertEqual(ti.current, request.trust_indicator.current)
406         if ti.histories:
407             history = ti.histories[0]
408             self.assertEqual(history.date, self.update_date)
409             self.assertEqual(history.step, self.update_step)
410
411
412 if __name__ == '__main__':
413     unittest.main()