-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathtest_security_groups.py
More file actions
160 lines (143 loc) · 8.03 KB
/
test_security_groups.py
File metadata and controls
160 lines (143 loc) · 8.03 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import unittest
from http import HTTPStatus
from abstract_test_case import AbstractTestCase
from cloudfoundry_client.v3.entities import Entity, ToManyRelationship, ToOneRelationship
from cloudfoundry_client.v3.security_groups import Rule, RuleProtocol, GloballyEnabled
class TestSecurityGroups(unittest.TestCase, AbstractTestCase):
@classmethod
def setUpClass(cls):
cls.mock_client_class()
def setUp(self):
self.build_client()
def test_create(self):
self.client.post.return_value = self.mock_response(
"/v3/security_groups", HTTPStatus.CREATED, None, "v3", "security_groups", "POST_response.json"
)
group_name = "my-group0"
result = self.client.v3.security_groups.create(group_name, rules=[
Rule(protocol=RuleProtocol.TCP, destination="10.10.10.0/24", ports="443,80,8080"),
Rule(protocol=RuleProtocol.ICMP, destination="10.10.10.0/24", type=8, code=0,
description="Allow ping requests to private services")])
self.client.post.assert_called_with(
self.client.post.return_value.url,
json={
"name": group_name,
"rules": [
{"protocol": "tcp", "destination": "10.10.10.0/24", "ports": "443,80,8080"},
{"protocol": "icmp", "destination": "10.10.10.0/24", "type": 8, "code": 0,
"description": "Allow ping requests to private services"}
]
},
files=None
)
self.assertIsNotNone(result)
self.assertIsInstance(result, Entity)
def test_list(self):
self.client.get.return_value = self.mock_response(
"/v3/security_groups", HTTPStatus.OK, None, "v3", "security_groups", "GET_response.json"
)
all_security_groups = [security_group for security_group in self.client.v3.security_groups.list()]
self.client.get.assert_called_with(self.client.get.return_value.url)
self.assertEqual(2, len(all_security_groups))
self.assertEqual(all_security_groups[0]["name"], "my-group0")
self.assertIsInstance(all_security_groups[0], Entity)
def test_get(self):
self.client.get.return_value = self.mock_response(
"/v3/security_groups/security_group_guid_123", HTTPStatus.OK, None, "v3", "security_groups",
"GET_{id}_response.json"
)
security_group = self.client.v3.security_groups.get("security_group_guid_123")
self.client.get.assert_called_with(self.client.get.return_value.url)
self.assertEqual("my-group0", security_group["name"])
self.assertIsInstance(security_group, Entity)
def test_update(self):
self.client.patch.return_value = self.mock_response(
"/v3/security_groups/security_group_guid_123", HTTPStatus.OK, None, "v3", "security_groups",
"PATCH_{id}_response.json"
)
group_name = "my-group0"
result = self.client.v3.security_groups.update("security_group_guid_123",
group_name,
rules=[
Rule(protocol=RuleProtocol.TCP, destination="10.10.10.0/24",
ports="443,80,8080"),
Rule(protocol=RuleProtocol.ICMP, destination="10.10.10.0/24",
type=8, code=0,
description="Allow ping requests to private services")
],
globally_enabled=GloballyEnabled(running=True))
self.client.patch.assert_called_with(
self.client.patch.return_value.url,
json={
"name": group_name,
"rules": [
{"protocol": "tcp", "destination": "10.10.10.0/24", "ports": "443,80,8080"},
{"protocol": "icmp", "destination": "10.10.10.0/24", "type": 8, "code": 0,
"description": "Allow ping requests to private services"}
],
"globally_enabled": {
"running": True
}
}
)
self.assertIsNotNone(result)
self.assertIsInstance(result, Entity)
def test_remove(self):
self.client.delete.return_value = self.mock_response("/v3/security_groups/security_group_guid",
HTTPStatus.NO_CONTENT, None)
self.client.v3.security_groups.remove("security_group_guid")
self.client.delete.assert_called_with(self.client.delete.return_value.url)
def test_bind_running_spaces(self):
self.client.post.return_value = self.mock_response(
"/v3/security_groups/security_group_guid/relationships/running_spaces",
HTTPStatus.OK, None, "v3", "security_groups", "POST_{id}_relationships_running_spaces_response.json"
)
result = self.client.v3.security_groups.bind_running_security_group_to_spaces("security_group_guid",
ToManyRelationship("space-guid1",
"space-guid2"))
self.client.post.assert_called_with(
self.client.post.return_value.url,
json={
"data": [
{"guid": "space-guid1"},
{"guid": "space-guid2"}
]
},
files=None
)
self.assertIsInstance(result, ToManyRelationship)
self.assertListEqual(["space-guid1", "space-guid2", "previous-space-guid"], result.guids)
def test_bind_staging_spaces(self):
self.client.post.return_value = self.mock_response(
"/v3/security_groups/security_group_guid/relationships/staging_spaces",
HTTPStatus.OK, None, "v3", "security_groups", "POST_{id}_relationships_staging_spaces_response.json"
)
result = self.client.v3.security_groups.bind_staging_security_group_to_spaces("security_group_guid",
ToManyRelationship("space-guid1",
"space-guid2"))
self.client.post.assert_called_with(
self.client.post.return_value.url,
json={
"data": [
{"guid": "space-guid1"},
{"guid": "space-guid2"}
]
},
files=None
)
self.assertIsInstance(result, ToManyRelationship)
self.assertListEqual(["space-guid1", "space-guid2", "previous-space-guid"], result.guids)
def test_unbind_running_from_space(self):
self.client.delete.return_value = self.mock_response(
"/v3/security_groups/security_group_guid/relationships/running_spaces/space-guid",
HTTPStatus.NO_CONTENT, None)
self.client.v3.security_groups.unbind_running_security_group_from_space("security_group_guid",
ToOneRelationship("space-guid"))
self.client.delete.assert_called_with(self.client.delete.return_value.url)
def test_unbind_staging_from_space(self):
self.client.delete.return_value = self.mock_response(
"/v3/security_groups/security_group_guid/relationships/staging_spaces/space-guid",
HTTPStatus.NO_CONTENT, None)
self.client.v3.security_groups.unbind_staging_security_group_from_space("security_group_guid",
ToOneRelationship("space-guid"))
self.client.delete.assert_called_with(self.client.delete.return_value.url)