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 opnfv_testapi.common.constants import HTTP_OK, HTTP_BAD_REQUEST, \
13 from opnfv_testapi.resources.pod_models import PodCreateRequest
14 from opnfv_testapi.resources.project_models import ProjectCreateRequest
15 from opnfv_testapi.resources.result_models import ResultCreateRequest, \
16 TestResult, TestResults
17 from opnfv_testapi.resources.testcase_models import TestcaseCreateRequest
18 from test_base import TestBase
21 class Details(object):
22 def __init__(self, timestart=None, duration=None, status=None):
23 self.timestart = timestart
24 self.duration = duration
29 "timestart": self.timestart,
30 "duration": self.duration,
35 def from_dict(a_dict):
41 t.timestart = a_dict.get('timestart')
42 t.duration = a_dict.get('duration')
43 t.status = a_dict.get('status')
47 class TestResultBase(TestBase):
50 self.project = 'functest'
52 self.installer = 'fuel'
54 self.build_tag = 'v3.0'
55 self.scenario = 'odl-l2'
56 self.criteria = 'passed'
57 self.trust_indicator = 0.7
58 self.start_date = "2016-05-23 07:16:09.477097"
59 self.stop_date = "2016-05-23 07:16:19.477097"
60 super(TestResultBase, self).setUp()
61 self.details = Details(timestart='0', duration='9s', status='OK')
62 self.req_d = ResultCreateRequest(pod_name=self.pod,
63 project_name=self.project,
65 installer=self.installer,
67 start_date=self.start_date,
68 stop_date=self.stop_date,
69 details=self.details.format(),
70 build_tag=self.build_tag,
71 scenario=self.scenario,
72 criteria=self.criteria,
73 trust_indicator=self.trust_indicator)
74 self.get_res = TestResult
75 self.list_res = TestResults
76 self.basePath = '/api/v1/results'
77 self.req_pod = PodCreateRequest(self.pod, 'metal', 'zte pod 1')
78 self.req_project = ProjectCreateRequest(self.project, 'vping test')
79 self.req_testcase = TestcaseCreateRequest(self.case,
82 self.create_help('/api/v1/pods', self.req_pod)
83 self.create_help('/api/v1/projects', self.req_project)
84 self.create_help('/api/v1/projects/%s/cases',
88 def assert_res(self, code, result, req=None):
89 self.assertEqual(code, HTTP_OK)
92 self.assertEqual(result.pod_name, req.pod_name)
93 self.assertEqual(result.project_name, req.project_name)
94 self.assertEqual(result.case_name, req.case_name)
95 self.assertEqual(result.installer, req.installer)
96 self.assertEqual(result.version, req.version)
97 details_req = Details.from_dict(req.details)
98 details_res = Details.from_dict(result.details)
99 self.assertEqual(details_res.duration, details_req.duration)
100 self.assertEqual(details_res.timestart, details_req.timestart)
101 self.assertEqual(details_res.status, details_req.status)
102 self.assertEqual(result.build_tag, req.build_tag)
103 self.assertEqual(result.scenario, req.scenario)
104 self.assertEqual(result.criteria, req.criteria)
105 self.assertEqual(result.trust_indicator, req.trust_indicator)
106 self.assertEqual(result.start_date, req.start_date)
107 self.assertEqual(result.stop_date, req.stop_date)
108 self.assertIsNotNone(result._id)
111 class TestResultCreate(TestResultBase):
112 def test_nobody(self):
113 (code, body) = self.create(None)
114 self.assertEqual(code, HTTP_BAD_REQUEST)
115 self.assertIn('no body', body)
117 def test_podNotProvided(self):
120 (code, body) = self.create(req)
121 self.assertEqual(code, HTTP_BAD_REQUEST)
122 self.assertIn('pod_name missing', body)
124 def test_projectNotProvided(self):
126 req.project_name = None
127 (code, body) = self.create(req)
128 self.assertEqual(code, HTTP_BAD_REQUEST)
129 self.assertIn('project_name missing', body)
131 def test_testcaseNotProvided(self):
134 (code, body) = self.create(req)
135 self.assertEqual(code, HTTP_BAD_REQUEST)
136 self.assertIn('case_name missing', body)
138 def test_noPod(self):
140 req.pod_name = 'notExistPod'
141 (code, body) = self.create(req)
142 self.assertEqual(code, HTTP_NOT_FOUND)
143 self.assertIn('Could not find pod', body)
145 def test_noProject(self):
147 req.project_name = 'notExistProject'
148 (code, body) = self.create(req)
149 self.assertEqual(code, HTTP_NOT_FOUND)
150 self.assertIn('Could not find project', body)
152 def test_noTestcase(self):
154 req.case_name = 'notExistTestcase'
155 (code, body) = self.create(req)
156 self.assertEqual(code, HTTP_NOT_FOUND)
157 self.assertIn('Could not find testcase', body)
159 def test_success(self):
160 (code, body) = self.create_d()
161 self.assertEqual(code, HTTP_OK)
162 self.assert_href(body)
165 class TestResultGet(TestResultBase):
166 def test_getOne(self):
167 _, res = self.create_d()
168 _id = res.href.split('/')[-1]
169 code, body = self.get(_id)
170 self.assert_res(code, body)
172 def test_queryPod(self):
173 self._query_and_assert(self._set_query('pod'))
175 def test_queryProject(self):
176 self._query_and_assert(self._set_query('project'))
178 def test_queryTestcase(self):
179 self._query_and_assert(self._set_query('case'))
181 def test_queryVersion(self):
182 self._query_and_assert(self._set_query('version'))
184 def test_queryInstaller(self):
185 self._query_and_assert(self._set_query('installer'))
187 def test_queryBuildTag(self):
188 self._query_and_assert(self._set_query('build_tag'))
190 def test_queryScenario(self):
191 self._query_and_assert(self._set_query('scenario'))
193 def test_queryTrustIndicator(self):
194 self._query_and_assert(self._set_query('trust_indicator'))
196 def test_queryCriteria(self):
197 self._query_and_assert(self._set_query('criteria'))
199 def test_queryPeriodFail(self):
200 self._query_and_assert(self._set_query('period=1'),
204 def test_queryPeriodSuccess(self):
205 self._query_and_assert(self._set_query('period=1'),
209 def test_combination(self):
210 self._query_and_assert(self._set_query('pod',
221 def test_notFound(self):
222 self._query_and_assert(self._set_query('pod=notExistPod',
234 def _query_and_assert(self, query, aheadof=False, found=True):
236 from datetime import datetime, timedelta
237 req = copy.deepcopy(self.req_d)
239 req.start_date = datetime.now() - timedelta(days=10)
241 req.start_date = datetime.now()
242 req.stop_date = str(req.start_date + timedelta(minutes=10))
243 req.start_date = str(req.start_date)
244 _, res = self.create(req)
245 code, body = self.query(query)
247 self.assertEqual(code, HTTP_OK)
248 self.assertEqual(0, len(body.results))
250 self.assertEqual(1, len(body.results))
251 for result in body.results:
252 self.assert_res(code, result, req)
254 def _set_query(self, *args):
260 uri += '{}={}&'.format(arg, eval('self.' + arg))
263 if __name__ == '__main__':