7dd07efa87a2217a6fc38715bbcd6735491b9f10
[releng.git] / utils / test / result_collection_api / tests / unit / test_result.py
1 import unittest
2
3 from test_base import TestBase
4 from resources.pod_models import PodCreateRequest
5 from resources.project_models import ProjectCreateRequest
6 from resources.testcase_models import TestcaseCreateRequest
7 from resources.result_models import ResultCreateRequest, \
8     TestResult, TestResults
9 from common.constants import HTTP_OK, HTTP_BAD_REQUEST, HTTP_NOT_FOUND
10
11
12 __author__ = '__serena__'
13
14
15 class Details(object):
16     def __init__(self, timestart=None, duration=None, status=None):
17         self.timestart = timestart
18         self.duration = duration
19         self.status = status
20
21     def format(self):
22         return {
23             "timestart": self.timestart,
24             "duration": self.duration,
25             "status": self.status
26         }
27
28     @staticmethod
29     def from_dict(a_dict):
30
31         if a_dict is None:
32             return None
33
34         t = Details()
35         t.timestart = a_dict.get('timestart')
36         t.duration = a_dict.get('duration')
37         t.status = a_dict.get('status')
38         return t
39
40
41 class TestResultBase(TestBase):
42     def setUp(self):
43         self.pod = 'zte-pod1'
44         self.project = 'functest'
45         self.case = 'vPing'
46         self.installer = 'fuel'
47         self.version = 'C'
48         self.build_tag = 'v3.0'
49         self.scenario = 'odl-l2'
50         self.criteria = 'passed'
51         self.trust_indicator = 0.7
52         self.start_date = "2016-05-23 07:16:09.477097"
53         self.stop_date = "2016-05-23 07:16:19.477097"
54         super(TestResultBase, self).setUp()
55         self.details = Details(timestart='0', duration='9s', status='OK')
56         self.req_d = ResultCreateRequest(pod_name=self.pod,
57                                          project_name=self.project,
58                                          case_name=self.case,
59                                          installer=self.installer,
60                                          version=self.version,
61                                          start_date=self.start_date,
62                                          stop_date=self.stop_date,
63                                          details=self.details.format(),
64                                          build_tag=self.build_tag,
65                                          scenario=self.scenario,
66                                          criteria=self.criteria,
67                                          trust_indicator=self.trust_indicator)
68         self.get_res = TestResult
69         self.list_res = TestResults
70         self.basePath = '/api/v1/results'
71         self.req_pod = PodCreateRequest(self.pod, 'metal', 'zte pod 1')
72         self.req_project = ProjectCreateRequest(self.project, 'vping test')
73         self.req_testcase = TestcaseCreateRequest(self.case,
74                                                   '/cases/vping',
75                                                   'vping-ssh test')
76         self.create_help('/api/v1/pods', self.req_pod)
77         self.create_help('/api/v1/projects', self.req_project)
78         self.create_help('/api/v1/projects/%s/cases',
79                          self.req_testcase,
80                          self.project)
81
82     def assert_res(self, code, result):
83         self.assertEqual(code, HTTP_OK)
84         req = self.req_d
85         self.assertEqual(result.pod_name, req.pod_name)
86         self.assertEqual(result.project_name, req.project_name)
87         self.assertEqual(result.case_name, req.case_name)
88         self.assertEqual(result.installer, req.installer)
89         self.assertEqual(result.version, req.version)
90         details_req = Details.from_dict(req.details)
91         details_res = Details.from_dict(result.details)
92         self.assertEqual(details_res.duration, details_req.duration)
93         self.assertEqual(details_res.timestart, details_req.timestart)
94         self.assertEqual(details_res.status, details_req.status)
95         self.assertEqual(result.build_tag, req.build_tag)
96         self.assertEqual(result.scenario, req.scenario)
97         self.assertEqual(result.criteria, req.criteria)
98         self.assertEqual(result.trust_indicator, req.trust_indicator)
99         self.assertIsNotNone(result.start_date)
100         self.assertIsNotNone(result.stop_date)
101         self.assertIsNotNone(result._id)
102
103
104 class TestResultCreate(TestResultBase):
105     def test_nobody(self):
106         (code, body) = self.create(None)
107         self.assertEqual(code, HTTP_BAD_REQUEST)
108         self.assertIn('no payload', body)
109
110     def test_podNotProvided(self):
111         req = self.req_d
112         req.pod_name = None
113         (code, body) = self.create(req)
114         self.assertEqual(code, HTTP_BAD_REQUEST)
115         self.assertIn('pod is not provided', body)
116
117     def test_projectNotProvided(self):
118         req = self.req_d
119         req.project_name = None
120         (code, body) = self.create(req)
121         self.assertEqual(code, HTTP_BAD_REQUEST)
122         self.assertIn('project is not provided', body)
123
124     def test_testcaseNotProvided(self):
125         req = self.req_d
126         req.case_name = None
127         (code, body) = self.create(req)
128         self.assertEqual(code, HTTP_BAD_REQUEST)
129         self.assertIn('testcase is not provided', body)
130
131     def test_noPod(self):
132         req = self.req_d
133         req.pod_name = 'notExistPod'
134         (code, body) = self.create(req)
135         self.assertEqual(code, HTTP_NOT_FOUND)
136         self.assertIn('Could not find POD', body)
137
138     def test_noProject(self):
139         req = self.req_d
140         req.project_name = 'notExistProject'
141         (code, body) = self.create(req)
142         self.assertEqual(code, HTTP_NOT_FOUND)
143         self.assertIn('Could not find project', body)
144
145     def test_noTestcase(self):
146         req = self.req_d
147         req.case_name = 'notExistTestcase'
148         (code, body) = self.create(req)
149         self.assertEqual(code, HTTP_NOT_FOUND)
150         self.assertIn('Could not find testcase', body)
151
152     def test_success(self):
153         (code, body) = self.create_d()
154         self.assertEqual(code, HTTP_OK)
155         self.assert_href(body)
156
157
158 class TestResultGet(TestResultBase):
159     def test_getOne(self):
160         _, res = self.create_d()
161         _id = res.href.split('/')[-1]
162         code, body = self.get(_id)
163         self.assert_res(code, body)
164
165     def test_queryPod(self):
166         self._query_and_assert(self._set_query('pod'))
167
168     def test_queryProject(self):
169         self._query_and_assert(self._set_query('project'))
170
171     def test_queryTestcase(self):
172         self._query_and_assert(self._set_query('case'))
173
174     def test_queryVersion(self):
175         self._query_and_assert(self._set_query('version'))
176
177     def test_queryInstaller(self):
178         self._query_and_assert(self._set_query('installer'))
179
180     def test_queryBuildTag(self):
181         self._query_and_assert(self._set_query('build_tag'))
182
183     def test_queryScenario(self):
184         self._query_and_assert(self._set_query('scenario'))
185
186     def test_queryTrustIndicator(self):
187         self._query_and_assert(self._set_query('trust_indicator'))
188
189     def test_queryCriteria(self):
190         self._query_and_assert(self._set_query('criteria'))
191
192     def test_queryPeriod(self):
193         self._query_and_assert(self._set_query('period=1'))
194
195     def test_combination(self):
196         self._query_and_assert(self._set_query('pod',
197                                                'project',
198                                                'case',
199                                                'version',
200                                                'installer',
201                                                'build_tag',
202                                                'scenario',
203                                                'trust_indicator',
204                                                'criteria',
205                                                'period=1'))
206
207     def test_notFound(self):
208         self._query_and_assert(self._set_query('pod=notExistPod',
209                                                'project',
210                                                'case',
211                                                'version',
212                                                'installer',
213                                                'build_tag',
214                                                'scenario',
215                                                'trust_indicator',
216                                                'criteria',
217                                                'period=1'),
218                                found=False)
219
220     def _query_and_assert(self, query, found=True):
221         _, res = self.create_d()
222         code, body = self.query(query)
223         if not found:
224             self.assertEqual(code, HTTP_OK)
225             self.assertEqual(0, len(body.results))
226         else:
227             for result in body.results:
228                 self.assert_res(code, result)
229
230     def _set_query(self, *args):
231         uri = ''
232         for arg in args:
233             if '=' in arg:
234                 uri += arg + '&'
235             else:
236                 uri += '{}={}&'.format(arg, eval('self.' + arg))
237         return uri[0: -1]
238
239 if __name__ == '__main__':
240     unittest.main()