-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathtest_vmc_security_rules.py
More file actions
219 lines (192 loc) · 6.76 KB
/
Copy pathtest_vmc_security_rules.py
File metadata and controls
219 lines (192 loc) · 6.76 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"""
Integration Tests for vmc_security_rules execution module
"""
import pytest
import requests
from saltext.vmware.utils import vmc_request
@pytest.fixture(scope="module")
def security_rule_test_data():
domain_id = "cgw"
rule_id = "vCenter_Inbound_Rule_2"
return domain_id, rule_id
@pytest.fixture
def get_security_rules(vmc_nsx_connect, security_rule_test_data):
hostname, refresh_key, authorization_host, org_id, sddc_id, verify_ssl, cert = vmc_nsx_connect
domain_id, rule_id = security_rule_test_data
url = (
"https://{hostname}/vmc/reverse-proxy/api/orgs/{org_id}/sddcs/{sddc_id}/"
"policy/api/v1/infra/domains/{domain_id}/gateway-policies/default/rules"
)
api_url = url.format(hostname=hostname, org_id=org_id, sddc_id=sddc_id, domain_id=domain_id)
session = requests.Session()
response = session.get(
url=api_url,
verify=cert if verify_ssl else False,
headers=vmc_request.get_headers(refresh_key, authorization_host),
)
response.raise_for_status()
return response.json()
@pytest.fixture
def delete_security_rule(get_security_rules, vmc_nsx_connect, security_rule_test_data):
"""
Sets up test requirements:
Queries vmc api for security rules
Deletes security rule if exists
"""
hostname, refresh_key, authorization_host, org_id, sddc_id, verify_ssl, cert = vmc_nsx_connect
domain_id, rule_id = security_rule_test_data
security_rules_dict = get_security_rules
if security_rules_dict["result_count"] != 0:
results = security_rules_dict["results"]
for result in results:
if result["id"] == rule_id:
url = (
"https://{hostname}/vmc/reverse-proxy/api/orgs/{org_id}/sddcs/{sddc_id}/"
"policy/api/v1/infra/domains/{domain_id}/gateway-policies/default/rules/{rule_id}"
)
api_url = url.format(
hostname=hostname,
org_id=org_id,
sddc_id=sddc_id,
domain_id=domain_id,
rule_id=rule_id,
)
session = requests.Session()
response = session.delete(
url=api_url,
verify=cert if verify_ssl else False,
headers=vmc_request.get_headers(refresh_key, authorization_host),
)
# raise error if any
response.raise_for_status()
@pytest.fixture
def create_security_rule(get_security_rules, vmc_nsx_connect, security_rule_test_data):
hostname, refresh_key, authorization_host, org_id, sddc_id, verify_ssl, cert = vmc_nsx_connect
domain_id, rule_id = security_rule_test_data
security_rules_dict = get_security_rules
if security_rules_dict["result_count"] != 0:
results = security_rules_dict["results"]
for result in results:
if result["id"] == rule_id:
return
url = (
"https://{hostname}/vmc/reverse-proxy/api/orgs/{org_id}/sddcs/{sddc_id}/"
"policy/api/v1/infra/domains/{domain_id}/gateway-policies/default/rules/{rule_id}"
)
api_url = url.format(
hostname=hostname, org_id=org_id, sddc_id=sddc_id, domain_id=domain_id, rule_id=rule_id
)
data = {
"sequence_number": 200,
"source_groups": ["ANY"],
"services": ["ANY"],
"logged": False,
"destination_groups": ["ANY"],
"scope": ["/infra/tier-1s/cgw"],
"action": "ALLOW",
"_revision": 1,
}
session = requests.Session()
response = session.patch(
url=api_url,
json=data,
verify=cert if verify_ssl else False,
headers=vmc_request.get_headers(refresh_key, authorization_host),
)
# raise error if any
response.raise_for_status()
def test_create_security_rule(
salt_call_cli,
delete_security_rule,
vmc_nsx_connect,
security_rule_test_data,
patch_salt_globals_vmc_security_rule,
):
hostname, refresh_key, authorization_host, org_id, sddc_id, verify_ssl, cert = vmc_nsx_connect
domain_id, rule_id = security_rule_test_data
ret = salt_call_cli.run(
"vmc_security_rules.create",
hostname=hostname,
refresh_key=refresh_key,
authorization_host=authorization_host,
org_id=org_id,
sddc_id=sddc_id,
domain_id=domain_id,
rule_id=rule_id,
verify_ssl=verify_ssl,
cert=cert,
)
assert ret is not None
result_as_json = ret.json
assert result_as_json["id"] == result_as_json["display_name"] == rule_id
def test_get_security_rules(
salt_call_cli,
get_security_rules,
vmc_nsx_connect,
security_rule_test_data,
patch_salt_globals_vmc_security_rule,
):
hostname, refresh_key, authorization_host, org_id, sddc_id, verify_ssl, cert = vmc_nsx_connect
domain_id, rule_id = security_rule_test_data
ret = salt_call_cli.run(
"vmc_security_rules.get",
hostname=hostname,
refresh_key=refresh_key,
authorization_host=authorization_host,
org_id=org_id,
sddc_id=sddc_id,
domain_id=domain_id,
verify_ssl=verify_ssl,
cert=cert,
)
assert ret is not None
result_as_json = ret.json
assert result_as_json == get_security_rules
def test_delete_security_rule(
salt_call_cli,
create_security_rule,
vmc_nsx_connect,
security_rule_test_data,
patch_salt_globals_vmc_security_rule,
):
hostname, refresh_key, authorization_host, org_id, sddc_id, verify_ssl, cert = vmc_nsx_connect
domain_id, rule_id = security_rule_test_data
ret = salt_call_cli.run(
"vmc_security_rules.delete",
hostname=hostname,
refresh_key=refresh_key,
authorization_host=authorization_host,
org_id=org_id,
sddc_id=sddc_id,
domain_id=domain_id,
rule_id=rule_id,
verify_ssl=verify_ssl,
cert=cert,
)
assert ret is not None
result_as_json = ret.json
assert result_as_json["result"] == "success"
def test_update_security_rule(
salt_call_cli,
create_security_rule,
vmc_nsx_connect,
security_rule_test_data,
patch_salt_globals_vmc_security_rule,
):
hostname, refresh_key, authorization_host, org_id, sddc_id, verify_ssl, cert = vmc_nsx_connect
domain_id, rule_id = security_rule_test_data
ret = salt_call_cli.run(
"vmc_security_rules.update",
hostname=hostname,
refresh_key=refresh_key,
authorization_host=authorization_host,
org_id=org_id,
sddc_id=sddc_id,
domain_id=domain_id,
rule_id=rule_id,
verify_ssl=verify_ssl,
cert=cert,
)
assert ret is not None
result_as_json = ret.json
assert result_as_json["result"] == "success"