test_security_service.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. """Test cloudbridge.security modules."""
  2. import json
  3. import unittest
  4. import uuid
  5. from test import helpers
  6. from test.helpers import ProviderTestBase
  7. from test.helpers import standard_interface_tests as sit
  8. from cloudbridge.cloud.interfaces import TestMockHelperMixin
  9. class CloudSecurityServiceTestCase(ProviderTestBase):
  10. @helpers.skipIfNoService(['security.key_pairs'])
  11. def test_crud_key_pair_service(self):
  12. name = 'cbtestkeypairA-{0}'.format(uuid.uuid4())
  13. kp = self.provider.security.key_pairs.create(name=name)
  14. with helpers.cleanup_action(
  15. lambda:
  16. self.provider.security.key_pairs.delete(key_pair_id=kp.id)
  17. ):
  18. sit.check_standard_behaviour(
  19. self, self.provider.security.key_pairs, kp)
  20. # Recreating existing keypair should raise an exception
  21. with self.assertRaises(Exception):
  22. self.provider.security.key_pairs.create(name=name)
  23. sit.check_delete(self, self.provider.security.key_pairs, kp)
  24. @helpers.skipIfNoService(['security.key_pairs'])
  25. def test_key_pair_properties(self):
  26. name = 'cbtestkeypairB-{0}'.format(uuid.uuid4())
  27. kp = self.provider.security.key_pairs.create(name=name)
  28. with helpers.cleanup_action(lambda: kp.delete()):
  29. self.assertIsNotNone(
  30. kp.material,
  31. "KeyPair material is empty but it should not be.")
  32. self.assertTrue(
  33. kp == kp,
  34. "The same key pair should be equal to self.")
  35. json_repr = json.dumps(
  36. {"material": kp.material, "id": name, "name": name},
  37. sort_keys=True)
  38. self.assertEqual(
  39. kp.to_json(), json_repr,
  40. "JSON key pair representation {0} does not match expected {1}"
  41. .format(kp.to_json(), json_repr))
  42. def cleanup_sg(self, sg, net):
  43. with helpers.cleanup_action(
  44. lambda: self.provider.network.delete(network_id=net.id)):
  45. self.provider.security.security_groups.delete(group_id=sg.id)
  46. @helpers.skipIfNoService(['security.security_groups'])
  47. def test_crud_security_group_service(self):
  48. name = 'CBTestSecurityGroupA-{0}'.format(uuid.uuid4())
  49. # Declare these variables and late binding will allow
  50. # the cleanup method access to the most current values
  51. net = None
  52. sg = None
  53. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  54. network=net, security_group=sg)):
  55. net, _ = helpers.create_test_network(self.provider, name)
  56. sg = self.provider.security.security_groups.create(
  57. name=name, description=name, network_id=net.id)
  58. self.assertEqual(name, sg.description)
  59. sit.check_standard_behaviour(
  60. self, self.provider.security.security_groups, sg)
  61. sit.check_delete(self, self.provider.security.security_groups, sg)
  62. @helpers.skipIfNoService(['security.security_groups'])
  63. def test_security_group(self):
  64. """Test for proper creation of a security group."""
  65. name = 'CBTestSecurityGroupB-{0}'.format(uuid.uuid4())
  66. # Declare these variables and late binding will allow
  67. # the cleanup method access to the most current values
  68. net = None
  69. sg = None
  70. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  71. network=net, security_group=sg)):
  72. net, _ = helpers.create_test_network(self.provider, name)
  73. sg = self.provider.security.security_groups.create(
  74. name=name, description=name, network_id=net.id)
  75. rule = sg.add_rule(ip_protocol='tcp', from_port=1111, to_port=1111,
  76. cidr_ip='0.0.0.0/0')
  77. found_rule = sg.get_rule(ip_protocol='tcp', from_port=1111,
  78. to_port=1111, cidr_ip='0.0.0.0/0')
  79. self.assertTrue(
  80. rule == found_rule,
  81. "Expected rule {0} not found in security group: {0}".format(
  82. rule, sg.rules))
  83. object_keys = (
  84. sg.rules[0].ip_protocol,
  85. sg.rules[0].from_port,
  86. sg.rules[0].to_port)
  87. self.assertTrue(
  88. all(str(key) in repr(sg.rules[0]) for key in object_keys),
  89. "repr(obj) should contain ip_protocol, form_port, and to_port"
  90. " so that the object can be reconstructed, but does not:"
  91. " {0}; {1}".format(sg.rules[0], object_keys))
  92. self.assertTrue(
  93. sg == sg,
  94. "The same security groups should be equal?")
  95. self.assertFalse(
  96. sg != sg,
  97. "The same security groups should still be equal?")
  98. # json_repr = json.dumps(
  99. # {"description": name, "name": name, "id": sg.id,
  100. # "rules":
  101. # [{"from_port": 1111, "group": "", "cidr_ip": "0.0.0.0/0",
  102. # "parent": sg.id, "to_port": 1111, "ip_protocol": "tcp",
  103. # "id": sg.rules[0].id}]},
  104. # sort_keys=True)
  105. # self.assertTrue(
  106. # sg.to_json() == json_repr,
  107. # "JSON SG representation {0} does not match expected {1}"
  108. # .format(sg.to_json(), json_repr))
  109. sit.check_delete(self, self.provider.security.security_groups, sg)
  110. sgl = self.provider.security.security_groups.list()
  111. found_sg = [g for g in sgl if g.name == name]
  112. self.assertTrue(
  113. len(found_sg) == 0,
  114. "Security group {0} should have been deleted but still exists."
  115. .format(name))
  116. @helpers.skipIfNoService(['security.security_groups'])
  117. def test_security_group_rule_add_twice(self):
  118. """Test whether adding the same rule twice succeeds."""
  119. if isinstance(self.provider, TestMockHelperMixin):
  120. raise unittest.SkipTest(
  121. "Mock provider returns InvalidParameterValue: "
  122. "Value security_group is invalid for parameter.")
  123. name = 'CBTestSecurityGroupC-{0}'.format(uuid.uuid4())
  124. # Declare these variables and late binding will allow
  125. # the cleanup method access to the most current values
  126. net = None
  127. sg = None
  128. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  129. network=net, security_group=sg)):
  130. net, _ = helpers.create_test_network(self.provider, name)
  131. sg = self.provider.security.security_groups.create(
  132. name=name, description=name, network_id=net.id)
  133. rule = sg.add_rule(ip_protocol='tcp', from_port=1111, to_port=1111,
  134. cidr_ip='0.0.0.0/0')
  135. # attempting to add the same rule twice should succeed
  136. same_rule = sg.add_rule(ip_protocol='tcp', from_port=1111,
  137. to_port=1111, cidr_ip='0.0.0.0/0')
  138. self.assertTrue(
  139. rule == same_rule,
  140. "Expected rule {0} not found in security group: {0}".format(
  141. same_rule, sg.rules))
  142. @helpers.skipIfNoService(['security.security_groups'])
  143. def test_security_group_group_rule(self):
  144. """Test for proper creation of a security group rule."""
  145. name = 'CBTestSecurityGroupD-{0}'.format(uuid.uuid4())
  146. # Declare these variables and late binding will allow
  147. # the cleanup method access to the most current values
  148. net = None
  149. sg = None
  150. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  151. network=net, security_group=sg)):
  152. net, _ = helpers.create_test_network(self.provider, name)
  153. sg = self.provider.security.security_groups.create(
  154. name=name, description=name, network_id=net.id)
  155. self.assertTrue(
  156. len(sg.rules) == 0,
  157. "Expected no security group group rule. Got {0}."
  158. .format(sg.rules))
  159. rule = sg.add_rule(src_group=sg, ip_protocol='tcp', from_port=1,
  160. to_port=65535)
  161. self.assertTrue(
  162. rule.group.name == name,
  163. "Expected security group rule name {0}. Got {1}."
  164. .format(name, rule.group.name))
  165. for r in sg.rules:
  166. r.delete()
  167. sg = self.provider.security.security_groups.get(sg.id) # update
  168. self.assertTrue(
  169. len(sg.rules) == 0,
  170. "Deleting SecurityGroupRule should delete it: {0}".format(
  171. sg.rules))
  172. sgl = self.provider.security.security_groups.list()
  173. found_sg = [g for g in sgl if g.name == name]
  174. self.assertTrue(
  175. len(found_sg) == 0,
  176. "Security group {0} should have been deleted but still exists."
  177. .format(name))