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 test_base import TestBase
13 from opnfv_testapi.resources.pod_models import PodCreateRequest
14 from opnfv_testapi.resources.project_models import ProjectCreateRequest
15 from opnfv_testapi.resources.testcase_models import TestcaseCreateRequest
16 from opnfv_testapi.resources.result_models import ResultCreateRequest, \
17 TestResult, TestResults
18 from opnfv_testapi.common.constants import HTTP_OK, HTTP_BAD_REQUEST, \
22 class Details(object):
23 def __init__(self, timestart=None, duration=None, status=None):
24 self.timestart = timestart
25 self.duration = duration
30 "timestart": self.timestart,
31 "duration": self.duration,
36 def from_dict(a_dict):
42 t.timestart = a_dict.get('timestart')
43 t.duration = a_dict.get('duration')
44 t.status = a_dict.get('status')
48 class TestResultBase(TestBase):
51 self.project = 'functest'
53 self.installer = 'fuel'
55 self.build_tag = 'v3.0'
56 self.scenario = 'odl-l2'
57 self.criteria = 'passed'
58 self.trust_indicator = 0.7
59 self.start_date = "2016-05-23 07:16:09.477097"
60 self.stop_date = "2016-05-23 07:16:19.477097"
61 super(TestResultBase, self).setUp()
62 self.details = Details(timestart='0', duration='9s', status='OK')
63 self.req_d = ResultCreateRequest(pod_name=self.pod,
64 project_name=self.project,
66 installer=self.installer,
68 start_date=self.start_date,
69 stop_date=self.stop_date,
70 details=self.details.format(),
71 build_tag=self.build_tag,
72 scenario=self.scenario,
73 criteria=self.criteria,
74 trust_indicator=self.trust_indicator)
75 self.get_res = TestResult
76 self.list_res = TestResults
77 self.basePath = '/api/v1/results'
78 self.req_pod = PodCreateRequest(self.pod, 'metal', 'zte pod 1')
79 self.req_project = ProjectCreateRequest(self.project, 'vping test')
80 self.req_testcase = TestcaseCreateRequest(self.case,
83 self.create_help('/api/v1/pods', self.req_pod)
84 self.create_help('/api/v1/projects', self.req_project)
85 self.create_help('/api/v1/projects/%s/cases',
89 def assert_res(self, code, result, req=None):
90 self.assertEqual(code, HTTP_OK)
93 self.assertEqual(result.pod_name, req.pod_name)
94 self.assertEqual(result.project_name, req.project_name)
95 self.assertEqual(result.case_name, req.case_name)
96 self.assertEqual(result.installer, req.installer)
97 self.assertEqual(result.version, req.version)
98 details_req = Details.from_dict(req.details)
99 details_res = Details.from_dict(result.details)
100 self.assertEqual(details_res.duration, details_req.duration)
101 self.assertEqual(details_res.timestart, details_req.timestart)
102 self.assertEqual(details_res.status, details_req.status)
103 self.assertEqual(result.build_tag, req.build_tag)
104 self.assertEqual(result.scenario, req.scenario)
105 self.assertEqual(result.criteria, req.criteria)
106 self.assertEqual(result.trust_indicator, req.trust_indicator)
107 self.assertEqual(result.start_date, req.start_date)
108 self.assertEqual(result.stop_date, req.stop_date)
109 self.assertIsNotNone(result._id)
112 class TestResultCreate(TestResultBase):
113 def test_nobody(self):
114 (code, body) = self.create(None)
115 self.assertEqual(code, HTTP_BAD_REQUEST)
116 self.assertIn('no body', body)
118 def test_podNotProvided(self):
121 (code, body) = self.create(req)
122 self.assertEqual(code, HTTP_BAD_REQUEST)
123 self.assertIn('pod_name missing', body)
125 def test_projectNotProvided(self):
127 req.project_name = None
128 (code, body) = self.create(req)
129 self.assertEqual(code, HTTP_BAD_REQUEST)
130 self.assertIn('project_name missing', body)
132 def test_testcaseNotProvided(self):
135 (code, body) = self.create(req)
136 self.assertEqual(code, HTTP_BAD_REQUEST)
137 self.assertIn('case_name missing', body)
139 def test_noPod(self):
141 req.pod_name = 'notExistPod'
142 (code, body) = self.create(req)
143 self.assertEqual(code, HTTP_NOT_FOUND)
144 self.assertIn('Could not find pod', body)
146 def test_noProject(self):
148 req.project_name = 'notExistProject'
149 (code, body) = self.create(req)
150 self.assertEqual(code, HTTP_NOT_FOUND)
151 self.assertIn('Could not find project', body)
153 def test_noTestcase(self):
155 req.case_name = 'notExistTestcase'
156 (code, body) = self.create(req)
157 self.assertEqual(code, HTTP_NOT_FOUND)
158 self.assertIn('Could not find testcase', body)
160 def test_success(self):
161 (code, body) = self.create_d()
162 self.assertEqual(code, HTTP_OK)
163 self.assert_href(body)
166 class TestResultGet(TestResultBase):
167 def test_getOne(self):
168 _, res = self.create_d()
169 _id = res.href.split('/')[-1]
170 code, body = self.get(_id)
171 self.assert_res(code, body)
173 def test_queryPod(self):
174 self._query_and_assert(self._set_query('pod'))
176 def test_queryProject(self):
177 self._query_and_assert(self._set_query('project'))
179 def test_queryTestcase(self):
180 self._query_and_assert(self._set_query('case'))
182 def test_queryVersion(self):
183 self._query_and_assert(self._set_query('version'))
185 def test_queryInstaller(self):
186 self._query_and_assert(self._set_query('installer'))
188 def test_queryBuildTag(self):
189 self._query_and_assert(self._set_query('build_tag'))
191 def test_queryScenario(self):
192 self._query_and_assert(self._set_query('scenario'))
194 def test_queryTrustIndicator(self):
195 self._query_and_assert(self._set_query('trust_indicator'))
197 def test_queryCriteria(self):
198 self._query_and_assert(self._set_query('criteria'))
200 def test_queryPeriodFail(self):
201 self._query_and_assert(self._set_query('period=1'),
205 def test_queryPeriodSuccess(self):
206 self._query_and_assert(self._set_query('period=1'),
210 def test_combination(self):
211 self._query_and_assert(self._set_query('pod',
222 def test_notFound(self):
223 self._query_and_assert(self._set_query('pod=notExistPod',
235 def _query_and_assert(self, query, aheadof=False, found=True):
237 from datetime import datetime, timedelta
238 req = copy.deepcopy(self.req_d)
240 req.start_date = datetime.now() - timedelta(days=10)
242 req.start_date = datetime.now()
243 req.stop_date = str(req.start_date + timedelta(minutes=10))
244 req.start_date = str(req.start_date)
245 _, res = self.create(req)
246 code, body = self.query(query)
248 self.assertEqual(code, HTTP_OK)
249 self.assertEqual(0, len(body.results))
251 self.assertEqual(1, len(body.results))
252 for result in body.results:
253 self.assert_res(code, result, req)
255 def _set_query(self, *args):
261 uri += '{}={}&'.format(arg, eval('self.' + arg))
264 if __name__ == '__main__':