8000 Add main test for list/get operations · codeplay/cf-python-client@e34f385 · GitHub
[go: up one dir, main page]

Skip to content

Commit e34f385

Browse files
author
Ben Einaudi
committed
Add main test for list/get operations
1 parent 587bfa6 commit e34f385

14 files changed

+337
-15
lines changed
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
21
"""
32
This module provides a client library for cloudfoundry_client v2.
43
"""
54

65
__version__ = "0.0.13"
7-
8-
from main import main, build_client_from_configuration
9-
from client import CloudFoundryClient
10-
from entities import InvalidStatusCode
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"metadata": {
3+
"guid": "6e72c33b-dff0-4020-8603-bcd8a4eb05e4",
4+
"url": "/v2/buildpacks/6e72c33b-dff0-4020-8603-bcd8a4eb05e4",
5+
"created_at": "2015-11-30T23:38:26Z",
6+
"updated_at": null
7+
},
8+
"entity": {
9+
"name": "name_1",
10+
"position": 1,
11+
"enabled": true,
12+
"locked": false,
13+
"filename": "name-28"
14+
}
15+
}

test/python/abstract_test_case.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
1+
import json
12
import types
23

34
import mock
4-
import json
55

6-
from cloudfoundry_client.client import CloudFoundryClient, CredentialManager
6+
from cloudfoundry_client.client import CloudFoundryClient
77
from fake_requests import TARGET_ENDPOINT, mock_response
88

99

10+
def mock_class(clazz):
11+
if not getattr(clazz, 'CLASS_MOCKED', False):
12+
true_mother_class = clazz.__bases__
13+
14+
class MockClass(object):
15+
def __init__(self, *args, **kwargs):
16+
for mother_class in true_mother_class:
17+
for attribute in dir(mother_class):
18+
if isinstance(getattr(mother_class, attribute), types.MethodType):
19+
setattr(self, attribute, mock.MagicMock())
20+
21+
clazz.__bases__ = (MockClass,)
22+
setattr(clazz, 'CLASS_MOCKED', True)
23+
24+
1025
class AbstractTestCase(object):
26+
@classmethod
27+
def mock_client_class(cls):
28+
mock_class(CloudFoundryClient)
1129

1230
def build_client(self):
1331
with mock.patch('cloudfoundry_client.client.requests') as fake_requests:
14-
class MockCredentail(object):
15-
def __init__(self, *args, **kwargs):
16-
for attribute in dir(CredentialManager):
17-
if isinstance(getattr(CredentialManager, attribute), types.MethodType):
18-
setattr(self, attribute, mock.MagicMock())
1932
fake_info_response = mock_response('/v2/info', 200, None)
2033
fake_info_response.text = json.dumps(dict(api_version='2.X',
2134
authorization_endpoint=TARGET_ENDPOINT,
2235
logging_endpoint=TARGET_ENDPOINT))
2336
fake_requests.get.return_value = fake_info_response
24-
25-
CloudFoundryClient.__bases__ = (MockCredentail,)
2637
self.client = CloudFoundryClient(TARGET_ENDPOINT)

test/python/test_apps.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import httplib
2+
import sys
23
import unittest
34

45
import mock
56

7+
import cloudfoundry_client.main as main
68
from abstract_test_case import AbstractTestCase
79
from fake_requests import mock_response
810

911

1012
class TestApps(unittest.TestCase, AbstractTestCase):
13+
@classmethod
14+
def setUpClass(cls):
15+
cls.mock_client_class()
16+
1117
def setUp(self):
1218
self.build_client()
1319

@@ -182,3 +188,25 @@ def test_entity(self):
182188
self.assertEqual(cpt, 1)
183189
self.client.get.assert_has_calls([mock.call(side_effect.url) for side_effect in self.client.get.side_effect],
184190
any_order=False)
191+
192+
@mock.patch.object(sys, 'argv', ['main', 'list_apps'])
193+
def test_main_list_apps(self):
194+
with mock.patch('cloudfoundry_client.main.build_client_from_configuration',
195+
new=lambda: self.client):
196+
self.client.get.return_value = mock_response('/v2/apps',
197+
httplib.OK,
198+
None,
199+
'v2', 'apps', 'GET_response.json')
200+
main.main()
201+
self.client.get.assert_called_with(self.client.get.return_value.url)
202+
203+
@mock.patch.object(sys, 'argv', ['main', 'get_app', '906775ea-622e-4bc7-af5d-9aab3b652f81'])
204+
def test_main_get_app(self):
205+
with mock.patch('cloudfoundry_client.main.build_client_from_configuration',
206+
new=lambda: self.client):
207+
self.client.get.return_value = mock_response('/v2/apps/906775ea-622e-4bc7-af5d-9aab3b652f81',
208+
httplib.OK,
209+
None,
210+
'v2', 'apps', 'GET_{id}_response.json')
211+
main.main()
212+
self.client.get.assert_called_with(self.client.get.return_value.url)

test/python/test_buildpacks.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import httplib
22
import unittest
33

4+
import mock
5+
import sys
6+
7+
import cloudfoundry_client.main as main
48
from abstract_test_case import AbstractTestCase
59
from fake_requests import mock_response
610

711

812
class TestBuildpacks(unittest.TestCase, AbstractTestCase):
13+
@classmethod
14+
def setUpClass(cls):
15+
cls.mock_client_class()
16+
917
def setUp(self):
1018
self.build_client()
1119

@@ -18,6 +26,16 @@ def test_list(self):
1826
self.client.get.assert_called_with(self.client.get.return_value.url)
1927
self.assertEqual(cpt, 3)
2028

29+
def test_get(self):
30+
self.client.get.return_value = mock_response(
31+
'/v2/buildpacks/buildpack_id',
32+
httplib.OK,
33+
None,
34+
'v2', 'buildpacks', 'GET_{id}_response.json')
35+
result = self.client.buildpacks.get('buildpack_id')
36+
self.client.get.assert_called_with(self.client.get.return_value.url)
37+
self.assertIsNotNone(result)
38+
2139
def test_update(self):
2240
self.client.put.return_value = mock_response(
2341
'/v2/buildpacks/build_pack_id',
@@ -28,3 +46,25 @@ def test_update(self):
2846
self.client.put.assert_called_with(self.client.put.return_value.url,
2947
json=dict(enabled=False))
3048
self.assertIsNotNone(result)
49+
50+
@mock.patch.object(sys, 'argv', ['main', 'list_buildpacks'])
51+
def test_main_list_buildpacks(self):
52+
with mock.patch('cloudfoundry_client.main.build_client_from_configuration',
53+
new=lambda: self.client):
54+
self.client.get.return_value = mock_response('/v2/buildpacks',
55+
httplib.OK,
56+
None,
57+
'v2', 'buildpacks', 'GET_response.json')
58+
main.main()
59+
self.client.get.assert_called_with(self.client.get.return_value.url)
60+
61+
@mock.patch.object(sys, 'argv', ['main', 'get_buildpack', '6e72c33b-dff0-4020-8603-bcd8a4eb05e4'])
62+
def test_main_get_buildpack(self):
63+
with mock.patch('cloudfoundry_client.main.build_client_from_configuration',
64+
new=lambda: self.client):
65+
self.client.get.return_value = mock_response('/v2/buildpacks/6e72c33b-dff0-4020-8603-bcd8a4eb05e4',
66+
httplib.OK,
67+
None,
68+
'v2', 'buildpacks', 'GET_{id}_response.json')
69+
main.main()
70+
self.client.get.assert_called_with(self.client.get.return_value.url)

test/python/test_loggregator.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77

88
class TestLoggregator(unittest.TestCase, AbstractTestCase):
9+
@classmethod
10+
def setUpClass(cls):
11+
cls.mock_client_class()
12+
913
def setUp(self):
1014
self.build_client()
1115

test/python/test_organizations.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22
import unittest
33

44
import mock
5+
import sys
6+
import cloudfoundry_client.main as main
57

68
from abstract_test_case import AbstractTestCase
79
from fake_requests import mock_response
810

911

1012
class TestOrganizations(unittest.TestCase, AbstractTestCase):
13+
@classmethod
14+
def setUpClass(cls):
15+
cls.mock_client_class()
16+
1117
def setUp(self):
1218
self.build_client()
1319

@@ -48,3 +54,25 @@ def test_entity(self):
4854
self.assertEqual(cpt, 1)
4955
self.client.get.assert_has_calls([mock.call(side_effect.url) for side_effect in self.client.get.side_effect],
5056
any_order=False)
57+
58+
@mock.patch.object(sys, 'argv', ['main', 'list_organizations'])
59+
def test_main_list_organizations(self):
60+
with mock.patch('cloudfoundry_client.main.build_client_from_configuration',
61+
new=lambda: self.client):
62+
self.client.get.return_value = mock_response('/v2/organizations',
63+
httplib.OK,
64+
None,
65+
'v2', 'organizations', 'GET_response.json')
66+
main.main()
67+
self.client.get.assert_called_with(self.client.get.return_value.url)
68+
69+
@mock.patch.object(sys, 'argv', ['main', 'get_organization', 'fe79371b-39b8-4f0d-8331-cff423a06aca'])
70+
def test_main_get_organization(self):
71+
with mock.patch('cloudfoundry_client.main.build_client_from_configuration',
72+
new=lambda: self.client):
73+
self.client.get.return_value = mock_response('/v2/organizations/fe79371b-39b8-4f0d-8331-cff423a06aca',
74+
httplib.OK,
75+
None,
76+
'v2', 'organizations', 'GET_{id}_response.json')
77+
main.main()
78+
self.client.get.assert_called_with(self.client.get.return_value.url)

test/python/test_routes.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import httplib
2+
import sys
23
import unittest
34

45
import mock
56

7+
import cloudfoundry_client.main as main
68
from abstract_test_case import AbstractTestCase
79
from fake_requests import mock_response
810

911

1012
class TestRoutes(unittest.TestCase, AbstractTestCase):
13+
@classmethod
14+
def setUpClass(cls):
15+
cls.mock_client_class()
16+
1117
def setUp(self):
1218
self.build_client()
1319

@@ -61,3 +67,26 @@ def test_entity(self):
6167
self.assertEqual(cpt, 3)
6268
self.client.get.assert_has_calls([mock.call(side_effect.url) for side_effect in self.client.get.side_effect],
6369
any_order=False)
70+
71+
72+
@mock.patch.object(sys, 'argv', ['main', 'list_routes'])
73+
def test_main_list_routes(self):
74+
with mock.patch('cloudfoundry_client.main.build_client_from_configuration',
75+
new=lambda: self.client):
76+
self.client.get.return_value = mock_response('/v2/routes',
77+
httplib.OK,
78+
None,
79+
'v2', 'routes', 'GET_response.json')
80+
main.main()
81+
self.client.get.assert_called_with(self.client.get.return_value.url)
82+
83+
@mock.patch.object(sys, 'argv', ['main', 'get_route', '75c16cfe-9b8a-4faf-bb65-02c713c7956f'])
84+
def test_main_get_route(self):
85+
with mock.patch('cloudfoundry_client.main.build_client_from_configuration',
86+
new=lambda: self.client):
87+
self.client.get.return_value = mock_response('/v2/routes/75c16cfe-9b8a-4faf-bb65-02c713c7956f',
88+
httplib.OK,
89+
None,
90+
'v2', 'routes', 'GET_{id}_response.json')
91+
main.main()
92+
self.client.get.assert_called_with(self.client.get.return_value.url)

test/python/test_service_bindings.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import httplib
2+
import sys
23
import unittest
34

45
import mock
56

7+
import cloudfoundry_client.main as main
68
from abstract_test_case import AbstractTestCase
79
from fake_requests import mock_response
810

911

1012
class TestServiceBindings(unittest.TestCase, AbstractTestCase):
13+
@classmethod
14+
def setUpClass(cls):
15+
cls.mock_client_class()
16+
1117
def setUp(self):
1218
self.build_client()
1319

@@ -78,3 +84,25 @@ def test_entity(self):
7884
self.assertIsNotNone(service_binding.app())
7985
self.client.get.assert_has_calls([mock.call(side_effect.url) for side_effect in self.client.get.side_effect],
8086
any_order=False)
87+
88+
@mock.patch.object(sys, 'argv', ['main', 'list_service_bindings'])
89+
def test_main_list_service_bindings(self):
90+
with mock.patch('cloudfoundry_client.main.build_client_from_configuration',
91+
new=lambda: self.client):
92+
self.client.get.return_value = mock_response('/v2/service_bindings',
93+
httplib.OK,
94+
None,
95+
'v2', 'service_bindings', 'GET_response.json')
96+
main.main()
97+
self.client.get.assert_called_with(self.client.get.return_value.url)
98+
99+
@mock.patch.object(sys, 'argv', ['main', 'get_service_binding', 'eaabd042-8f5c-44a2-9580-1e114c36bdcb'])
100+
def test_main_get_service_binding(self):
101+
with mock.patch('cloudfoundry_client.main.build_client_from_configuration',
102+
new=lambda: self.client):
103+
self.client.get.return_value = mock_response('/v2/service_bindings/eaabd042-8f5c-44a2-9580-1e114c36bdcb',
104+
httplib.OK,
105+
None,
106+
'v2', 'service_bindings', 'GET_{id}_response.json')
107+
main.main()
108+
self.client.get.assert_called_with(self.client.get.return_value.url)

test/python/test_service_brokers.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import httplib
2+
import sys
23
import unittest
34

5+
import mock
6+
7+
import cloudfoundry_client.main as main
48
from abstract_test_case import AbstractTestCase
59
from fake_requests import mock_response
610

711

812
class TestServiceBrokers(unittest.TestCase, AbstractTestCase):
13+
@classmethod
14+
def setUpClass(cls):
15+
cls.mock_client_class()
16+
917
def setUp(self):
1018
self.build_client()
1119

@@ -67,3 +75,26 @@ def test_delete(self):
6775
None)
6876
self.client.service_brokers.remove('broker_id')
6977
self.client.delete.assert_called_with(self.client.delete.return_value.url)
78+
79+
@mock.patch.object(sys, 'argv', ['main', 'list_service_brokers'])
80+
def test_main_list_service_brokers(self):
81+
with mock.patch('cloudfoundry_client.main.build_client_from_configuration',
82+
new=lambda: self.client):
83+
self.client.get.return_value = mock_response('/v2/service_brokers',
84+
httplib.OK,
85+
None,
86+
'v2', 'service_brokers', 'GET_response.json')
87+
main.main()
88+
self.client.get.assert_called_with(self.client.get.return_value.url)
89+
90+
@mock.patch.object(sys, 'argv', ['main', 'get_service_broker', 'ade9730c-4ee5-4290-ad37-0b15cecd2ca6'])
91+
def test_main_get_service_broker(self):
92+
with mock.patch('cloudfoundry_client.main.build_client_from_configuration',
93+
new=lambda: self.client):
94+
self.client.get.return_value = mock_response('/v2/service_brokers/ade9730c-4ee5-4290-ad37-0b15cecd2ca6',
95+
httplib.OK,
96+
None,
97+
'v2', 'service_brokers', 'GET_{id}_response.json')
98+
main.main()
99+
self.client.get.assert_called_with(self.client.get.return_value.url)
100+

0 commit comments

Comments
 (0)
0