summaryrefslogtreecommitdiffstats
path: root/utils/test/result_collection_api/tests/unit/test_pod.py
blob: 5a3d485ab8d62ee91b54036f07d8c14eb8b97f74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import unittest
import json

from test_base import TestBase
from resources.pod_models import PodCreateRequest, \
    PodCreateResponse, PodsGetResponse
from common.constants import HTTP_OK, HTTP_BAD_REQUEST, HTTP_FORBIDDEN


class TestPodCreate(TestBase):
    req = PodCreateRequest(name='zte-1', mode='alive', details='zte pod 1')

    def test_withoutBody(self):
        res = self.create('/pods', body=None)
        self.assertEqual(res.code, HTTP_BAD_REQUEST)

    def test_success(self):
        res = self.create('/pods', body=self.req.format())
        self.assertEqual(res.code, HTTP_OK)
        res_body = PodCreateResponse.from_dict(json.loads(res.body))
        self._assertMeta(res_body.meta, True)
        self._assertBody(res_body.pod)

    def test_alreadyExist(self):
        self.create('/pods', body=self.req.format())
        res = self.create('/pods', body=self.req.format())
        self.assertEqual(res.code, HTTP_FORBIDDEN)
        self.assertIn('already exists', res.body)

    def _assertMeta(self, meta, success):
        self.assertEqual(meta.success, success)
        if success:
            self.assertEqual(meta.uri, '/pods/{}'.format(self.req.name))

    def _assertBody(self, res):
        self.assertEqual(res.name, self.req.name)
        self.assertEqual(res.mode, self.req.mode)
        self.assertEqual(res.details, self.req.details)
        self.assertIsNotNone(res.creation_date)
        self.assertIsNotNone(res._id)


class TestPodGet(TestBase):
    def test_notExist(self):
        res = self.get('/pods/notExist')
        body = PodsGetResponse.from_dict(json.loads(res.body))
        self._assertMeta(body.meta, 0)

    def test_getOne(self):
        self.create('/pods', body=TestPodCreate.req.format())
        res = self.get('/pods/{}'.format(TestPodCreate.req.name))
        body = PodsGetResponse.from_dict(json.loads(res.body))
        self._assertMeta(body.meta, 1)
        self._assertBody(TestPodCreate.req, body.pods[0])

    def test_list(self):
        req = PodCreateRequest(name='zte-2', mode='alive', details='zte pod 2')
        self.create('/pods', body=TestPodCreate.req.format())
        self.create('/pods', body=req.format())
        res = self.get('/pods')
        body = PodsGetResponse.from_dict(json.loads(res.body))
        self._assertMeta(body.meta, 2)
        for pod in body.pods:
            if req.name == pod.name:
                self._assertBody(req, pod)
            else:
                self._assertBody(TestPodCreate.req, pod)

    def _assertMeta(self, meta, total):
        def check_success():
            if total is 0:
                return False
            else:
                return True
        self.assertEqual(meta.total, total)
        self.assertEqual(meta.success, check_success())

    def _assertBody(self, req, res):
        self.assertEqual(res.name, req.name)
        self.assertEqual(res.mode, req.mode)
        self.assertEqual(res.details, req.details)
        self.assertIsNotNone(res.creation_date)


if __name__ == '__main__':
    unittest.main()