Skip to content

Commit aa6fc9e

Browse files
committed
Add Create/Delete security group test cases and fix a typo in the e2e test
1 parent e144b99 commit aa6fc9e

File tree

3 files changed

+170
-7
lines changed

3 files changed

+170
-7
lines changed

CloudDriverSecurityGroupE2E.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
#create a new securityGroup through clouddriver
4040
url = clouddriver_host + '/ops'
41-
sg_data = '[ { "upsertSecurityGroup": { "cloudProvider" : "azure", "appName" : "azuresg1", "securityGroupName" : "azuresg1-st1-d1", "stack" : "st1", "detail" : "d1", "credentials" : "' + azure_cred + '", "region" : "westus", "vnet" : "none", "tags" : { "appName" : "testazure4", "stack" : "sg22", "detail" : "d11"}, "securityRules" : [ { "name" : "rule1", "description" : "Allow FE Subnet", "access" : "Allow", "destinationAddressPrefix" : "*", "destinationPortRange" : "433", "direction" : "Inbound", "priority" : 100, "protocol" : "TCP", "sourceAddressPrefix" : "10.0.0.0/24", "sourcePortRange" : "*" } ], "name" : "azuresg1-st1-d1", "user" : "[anonymous]" }} ]'
41+
sg_data = '[ { "upsertSecurityGroup": { "cloudProvider" : "azure", "appName" : "azuresg1", "securityGroupName" : "azuresg1-st1-d1", "stack" : "st1", "detail" : "d1", "credentials" : "' + azure_creds + '", "region" : "westus", "vnet" : "none", "tags" : { "appName" : "testazure4", "stack" : "sg22", "detail" : "d11"}, "securityRules" : [ { "name" : "rule1", "description" : "Allow FE Subnet", "access" : "Allow", "destinationAddressPrefix" : "*", "destinationPortRange" : "433", "direction" : "Inbound", "priority" : 100, "protocol" : "TCP", "sourceAddressPrefix" : "10.0.0.0/24", "sourcePortRange" : "*" } ], "name" : "azuresg1-st1-d1", "user" : "[anonymous]" }} ]'
4242

4343
print ctime(), ' - Post new security group'
4444
sys.stdout.flush()
@@ -90,7 +90,7 @@ def CheckDeployment():
9090
#update the securityGroup
9191
url = clouddriver_host + '/ops'
9292

93-
sg_update = '[ { "upsertSecurityGroup": { "cloudProvider" : "azure", "appName" : "azuresg1", "securityGroupName" : "azuresg1-st1-d1", "stack" : "st1", "detail" : "d1", "credentials" : "' + azure_cred + '", "region" : "westus", "vnet" : "none", "tags" : { "appName" : "testazure4", "stack" : "sg22", "detail" : "d11"}, "securityRules" : [ { "name" : "rule1", "description" : "Allow FE Subnet", "access" : "Allow", "destinationAddressPrefix" : "*", "destinationPortRange" : "433", "direction" : "Inbound", "priority" : 100, "protocol" : "TCP", "sourceAddressPrefix" : "10.0.0.0/24", "sourcePortRange" : "*" }, { "name" : "rule2", "description" : "Block RDP", "access" : "Deny", "destinationAddressPrefix" : "*", "destinationPortRange" : "3389", "direction" : "Inbound", "priority" : 101, "protocol" : "TCP", "sourceAddressPrefix" : "Internet", "sourcePortRange" : "*" } ], "name" : "azuresg1-st1-d1", "user" : "[anonymous]" }} ]'
93+
sg_update = '[ { "upsertSecurityGroup": { "cloudProvider" : "azure", "appName" : "azuresg1", "securityGroupName" : "azuresg1-st1-d1", "stack" : "st1", "detail" : "d1", "credentials" : "' + azure_creds + '", "region" : "westus", "vnet" : "none", "tags" : { "appName" : "testazure4", "stack" : "sg22", "detail" : "d11"}, "securityRules" : [ { "name" : "rule1", "description" : "Allow FE Subnet", "access" : "Allow", "destinationAddressPrefix" : "*", "destinationPortRange" : "433", "direction" : "Inbound", "priority" : 100, "protocol" : "TCP", "sourceAddressPrefix" : "10.0.0.0/24", "sourcePortRange" : "*" }, { "name" : "rule2", "description" : "Block RDP", "access" : "Deny", "destinationAddressPrefix" : "*", "destinationPortRange" : "3389", "direction" : "Inbound", "priority" : 101, "protocol" : "TCP", "sourceAddressPrefix" : "Internet", "sourcePortRange" : "*" } ], "name" : "azuresg1-st1-d1", "user" : "[anonymous]" }} ]'
9494

9595
print ctime(), ' - Update security group'
9696
sys.stdout.flush()
@@ -124,7 +124,7 @@ def CheckDeployment():
124124
#delete a securityGroup through clouddriver
125125
url = clouddriver_host + '/ops'
126126

127-
sg_delete = '[ { "deleteSecurityGroup": { "cloudProvider" : "azure", "appName" : "azuresg1", "securityGroupName" : "azuresg1-st1-d1", "regions": [{"westus"}], "credentials": "' + azure_cred + '" }} ]'
127+
sg_delete = '[ { "deleteSecurityGroup": { "cloudProvider" : "azure", "appName" : "azuresg1", "securityGroupName" : "azuresg1-st1-d1", "regions": ["westus"], "credentials": "' + azure_creds + '" }} ]'
128128

129129
print ctime(), ' - Delete security group'
130130
sys.stdout.flush()
@@ -133,17 +133,19 @@ def CheckDeployment():
133133
sys.stdout.flush()
134134

135135
#validate delete
136+
sleep(10)
136137
print ctime(), ' - Validate Delete'
137138
sys.stdout.flush()
138139
r = requests.get(security_group_endpoint, headers=headers)
139140

140-
if (r.json()['error']):
141-
print ctime(), ' - securityGroup Deleted'
142-
sys.stdout.flush()
143-
else:
141+
if (not r.json()['error']):
144142
print ctime(), ' - Deletion Failed: ', r.text
145143
test_passed = False
144+
else:
145+
sys.stdout.flush()
146+
print ctime(), ' - securityGroup Deleted'
146147
sys.stdout.flush()
148+
147149
#end delete validation
148150
#
149151
# DELETE

CreateSecurityGroup.py

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env python
2+
import adal
3+
import requests
4+
import os
5+
import json
6+
from time import sleep, ctime
7+
import sys
8+
9+
test_passed = True
10+
11+
authority = os.environ['AZURE_E2E_AUTHORITY']
12+
client_id = os.environ['AZURE_E2E_CLIENT_ID']
13+
client_secret = os.environ['AZURE_E2E_CLIENT_SECRET']
14+
subscription_id = os.environ['AZURE_SUBSCRIPTION_ID']
15+
clouddriver_host = 'http://localhost:7002'
16+
azure_creds = 'azure-cred1'
17+
18+
token_response = adal.acquire_token_with_client_credentials(
19+
authority,
20+
client_id,
21+
client_secret
22+
)
23+
24+
access_token = token_response.get('accessToken')
25+
headers = {"Authorization": 'Bearer ' + access_token}
26+
27+
security_group_endpoint = 'https://management.azure.com/subscriptions/' + subscription_id + '/resourceGroups/azuresg1-westus/providers/Microsoft.Network/networkSecurityGroups/azuresg1-st1-d1?api-version=2015-05-01-preview'
28+
deployment_endpoint = 'https://management.azure.com/subscriptions/' + subscription_id + '/resourceGroups/azuresg1-westus/providers/microsoft.resources/deployments/azuresg1-st1-d1-deployment?api-version=2015-11-01'
29+
30+
print ctime(), ' - Check for existing security group'
31+
sys.stdout.flush()
32+
r = requests.get(security_group_endpoint, headers=headers)
33+
34+
#the next line will fail if there is not 'error' as the first element.
35+
#this should pass if the security group has not been created yet
36+
if (not r.json()['error']):
37+
test_passed = False
38+
39+
#create a new securityGroup through clouddriver
40+
url = clouddriver_host + '/ops'
41+
sg_data = '[ { "upsertSecurityGroup": { "cloudProvider" : "azure", "appName" : "azuresg1", "securityGroupName" : "azuresg1-st1-d1", "stack" : "st1", "detail" : "d1", "credentials" : "' + azure_creds + '", "region" : "westus", "vnet" : "none", "tags" : { "appName" : "testazure4", "stack" : "sg22", "detail" : "d11"}, "securityRules" : [ { "name" : "rule1", "description" : "Allow FE Subnet", "access" : "Allow", "destinationAddressPrefix" : "*", "destinationPortRange" : "433", "direction" : "Inbound", "priority" : 100, "protocol" : "TCP", "sourceAddressPrefix" : "10.0.0.0/24", "sourcePortRange" : "*" } ], "name" : "azuresg1-st1-d1", "user" : "[anonymous]" }} ]'
42+
43+
print ctime(), ' - Post new security group'
44+
sys.stdout.flush()
45+
r = requests.post(url, data = sg_data, headers={'Content-Type': 'application/json'})
46+
print ctime(), ' - result: ', (r.text)
47+
sys.stdout.flush()
48+
49+
#continuously check the deployment until it is complete
50+
def CheckDeployment():
51+
print ctime(), ' - Waiting for deployment...'
52+
sys.stdout.flush()
53+
r = requests.get(deployment_endpoint, headers=headers)
54+
while (r.text.find('error') != -1):
55+
sleep(10)
56+
r = requests.get(deployment_endpoint, headers=headers)
57+
58+
provisioningState = 'none'
59+
60+
print ctime(), ' - Checking deployment state'
61+
sys.stdout.flush()
62+
63+
while (provisioningState != 'Succeeded'):
64+
sleep(10)
65+
r = requests.get(deployment_endpoint, headers=headers)
66+
provisioningState = r.json()['properties']['provisioningState']
67+
print ctime(), ' - provisioningState: ', provisioningState
68+
sys.stdout.flush()
69+
70+
print ctime(), ' - Deployment complete'
71+
sys.stdout.flush()
72+
#deployment complete
73+
74+
CheckDeployment()
75+
76+
#validate creation
77+
print ctime(), ' - Validate Create'
78+
sys.stdout.flush()
79+
r = requests.get(security_group_endpoint, headers=headers)
80+
81+
if (r.json()['name'] == 'azuresg1-st1-d1'):
82+
print ctime(), ' - securityGroup Created'
83+
sys.stdout.flush()
84+
else:
85+
print ctime(), ' - Create Failed'
86+
sys.stdout.flush()
87+
test_passed = False
88+
#end creation validation
89+
90+
if (test_passed):
91+
print('SUCCESS!!')
92+
else:
93+
print('FAILED')
94+

DeleteSecurityGroup.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python
2+
import adal
3+
import requests
4+
import os
5+
import json
6+
from time import sleep, ctime
7+
import sys
8+
9+
test_passed = True
10+
11+
authority = os.environ['AZURE_E2E_AUTHORITY']
12+
client_id = os.environ['AZURE_E2E_CLIENT_ID']
13+
client_secret = os.environ['AZURE_E2E_CLIENT_SECRET']
14+
subscription_id = os.environ['AZURE_SUBSCRIPTION_ID']
15+
clouddriver_host = 'http://localhost:7002'
16+
azure_creds = 'azure-cred1'
17+
18+
token_response = adal.acquire_token_with_client_credentials(
19+
authority,
20+
client_id,
21+
client_secret
22+
)
23+
24+
access_token = token_response.get('accessToken')
25+
headers = {"Authorization": 'Bearer ' + access_token}
26+
27+
security_group_endpoint = 'https://management.azure.com/subscriptions/' + subscription_id + '/resourceGroups/azuresg1-westus/providers/Microsoft.Network/networkSecurityGroups/azuresg1-st1-d1?api-version=2015-05-01-preview'
28+
deployment_endpoint = 'https://management.azure.com/subscriptions/' + subscription_id + '/resourceGroups/azuresg1-westus/providers/microsoft.resources/deployments/azuresg1-st1-d1-deployment?api-version=2015-11-01'
29+
30+
#
31+
# DELETE
32+
#
33+
#delete a securityGroup through clouddriver
34+
url = clouddriver_host + '/ops'
35+
36+
sg_delete = '[ { "deleteSecurityGroup": { "cloudProvider" : "azure", "appName" : "azuresg1", "securityGroupName" : "azuresg1-st1-d1", "regions": ["westus"], "credentials": "' + azure_creds + '" }} ]'
37+
38+
print ctime(), ' - Delete security group'
39+
sys.stdout.flush()
40+
r = requests.post(url, data = sg_delete, headers={'Content-Type': 'application/json'})
41+
print ctime(), ' - result: ', (r.text)
42+
sys.stdout.flush()
43+
44+
#validate delete
45+
sleep(10)
46+
print ctime(), ' - Validate Delete'
47+
sys.stdout.flush()
48+
r = requests.get(security_group_endpoint, headers=headers)
49+
50+
if (not r.json()['error']):
51+
print ctime(), ' - Deletion Failed: ', r.text
52+
test_passed = False
53+
else:
54+
sys.stdout.flush()
55+
print ctime(), ' - securityGroup Deleted'
56+
sys.stdout.flush()
57+
58+
#end delete validation
59+
#
60+
# DELETE
61+
#
62+
63+
if (test_passed):
64+
print('SUCCESS!!')
65+
else:
66+
print('FAILED')
67+

0 commit comments

Comments
 (0)