test_integration_azure_security_group.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import json
  2. import unittest
  3. import uuid
  4. from cloudbridge.cloud.interfaces import TestMockHelperMixin
  5. from test.helpers import ProviderTestBase
  6. import test.helpers as helpers
  7. class AzureIntegrationSecurityServiceTestCase(ProviderTestBase):
  8. def __init__(self, methodName, provider):
  9. super(AzureIntegrationSecurityServiceTestCase, self).__init__(
  10. methodName=methodName, provider=provider)
  11. @helpers.skipIfNoService(['security.security_groups'])
  12. def test_azure_security_group(self):
  13. sg_name = '{0}'.format(uuid.uuid4())
  14. print(sg_name)
  15. listBeforeCreate = self.provider.security.security_groups.list()
  16. print("Before create - " + str(len(listBeforeCreate)))
  17. sg = self.provider.security.security_groups.create(name=sg_name, description="testCreateSecGroup", network_id="")
  18. self.assertEqual(sg_name, sg.name)
  19. listAfterCreate = self.provider.security.security_groups.list()
  20. print("After create - "+ str(len(listAfterCreate)))
  21. self.assertEqual( len(listAfterCreate) , len(listBeforeCreate)+1)
  22. get = self.provider.security.security_groups.get("/subscriptions/7904d702-e01c-4826-8519-f5a25c866a96/resourceGroups/cloudbridge-azure/providers/Microsoft.Network/networkSecurityGroups/"+ sg_name)
  23. self.assertEqual( get.name , sg_name)
  24. get_notfound = self.provider.security.security_groups.get("/subscriptions/7904d702-e01c-4826-8519-f5a25c866a96/resourceGroups/cloudbridge-azure/providers/Microsoft.Network/networkSecurityGroups/testCreateSecGroup1")
  25. self.assertEqual(get_notfound , None)
  26. cb = listAfterCreate.data[0]
  27. lenBeforeCreateRule = len(cb.rules)
  28. cb.add_rule('*', '25', '100', '*')
  29. lenAfterCreateRule = len(cb.rules)
  30. self.assertEqual(lenAfterCreateRule, lenBeforeCreateRule+1)
  31. get_rule = cb.get_rule('*', '*', '*', '*')
  32. self.assertEqual(str(get_rule), "<CBSecurityGroupRule: IP: *; from: *; to: *; grp: None>")
  33. get_rule_notfound = cb.get_rule('*', '25', '1', '1')
  34. self.assertEqual(str(get_rule_notfound), 'None')
  35. rule_json = cb.rules[1].to_json()
  36. self.assertEqual(rule_json[2:9], "cidr_ip")
  37. sg_json = cb.to_json()
  38. self.assertEqual(sg_json[2:4], "id")
  39. with self.assertRaises(Exception):
  40. cb.rules[1].delete()
  41. listBeforeDeleteFound = self.provider.security.security_groups.list()
  42. sg_del = self.provider.security.security_groups.delete("/subscriptions/7904d702-e01c-4826-8519-f5a25c866a96/resourceGroups/cloudbridge-azure/providers/Microsoft.Network/networkSecurityGroups/"+sg_name)
  43. listAfterDeleteFound = self.provider.security.security_groups.list()
  44. print(str(len(listBeforeDeleteFound))+"---"+str(len(listAfterDeleteFound)))
  45. self.assertEqual(len(listAfterDeleteFound), len(listBeforeDeleteFound)-1)
  46. sg_del_notfound = self.provider.security.security_groups.delete("/subscriptions/7904d702-e01c-4826-8519-f5a25c866a96/resourceGroups/cloudbridge-azure/providers/Microsoft.Network/networkSecurityGroups/sg5")
  47. listAfterDeleteNotFound = self.provider.security.security_groups.list()
  48. self.assertEqual(len(listAfterDeleteNotFound), len(listAfterDeleteFound))