attach version number to url in testAPI
[releng.git] / utils / test / result_collection_api / tests / unit / test_pod.py
1 import unittest
2
3 from test_base import TestBase
4 from resources.pod_models import PodCreateRequest, Pod, Pods
5 from common.constants import HTTP_OK, HTTP_BAD_REQUEST, \
6     HTTP_FORBIDDEN, HTTP_NOT_FOUND
7
8
9 class TestPodBase(TestBase):
10     def setUp(self):
11         super(TestPodBase, self).setUp()
12         self.req_d = PodCreateRequest('zte-1', 'virtual',
13                                       'zte pod 1', 'ci-pod')
14         self.req_e = PodCreateRequest('zte-2', 'metal', 'zte pod 2')
15         self.get_res = Pod
16         self.list_res = Pods
17         self.basePath = '/api/v1/pods'
18
19     def assert_get_body(self, pod, req=None):
20         if not req:
21             req = self.req_d
22         self.assertEqual(pod.name, req.name)
23         self.assertEqual(pod.mode, req.mode)
24         self.assertEqual(pod.details, req.details)
25         self.assertEqual(pod.role, req.role)
26         self.assertIsNotNone(pod.creation_date)
27         self.assertIsNotNone(pod._id)
28
29
30 class TestPodCreate(TestPodBase):
31     def test_withoutBody(self):
32         (code, body) = self.create()
33         self.assertEqual(code, HTTP_BAD_REQUEST)
34
35     def test_success(self):
36         code, body = self.create_d()
37         self.assertEqual(code, HTTP_OK)
38         self.assert_create_body(body)
39
40     def test_alreadyExist(self):
41         self.create_d()
42         code, body = self.create_d()
43         self.assertEqual(code, HTTP_FORBIDDEN)
44         self.assertIn('already exists', body)
45
46
47 class TestPodGet(TestPodBase):
48     def test_notExist(self):
49         code, body = self.get('notExist')
50         self.assertEqual(code, HTTP_NOT_FOUND)
51
52     def test_getOne(self):
53         self.create_d()
54         code, body = self.get(self.req_d.name)
55         self.assert_get_body(body)
56
57     def test_list(self):
58         self.create_d()
59         self.create_e()
60         code, body = self.get()
61         self.assertEqual(len(body.pods), 2)
62         for pod in body.pods:
63             if self.req_d.name == pod.name:
64                 self.assert_get_body(pod)
65             else:
66                 self.assert_get_body(pod, self.req_e)
67
68 if __name__ == '__main__':
69     unittest.main()