test_security_service.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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(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. kpl = self.provider.security.key_pairs.list()
  30. found_kp = [k for k in kpl if k.name == name]
  31. self.assertTrue(
  32. len(found_kp) == 1,
  33. "List key pairs did not return the expected key {0}."
  34. .format(name))
  35. self.assertTrue(
  36. kp.id in repr(kp),
  37. "repr(obj) should contain the object id so that the object"
  38. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  39. self.assertIsNotNone(
  40. kp.material,
  41. "KeyPair material is empty but it should not be.")
  42. self.assertTrue(
  43. kp == kp,
  44. "The same key pair should be equal to self.")
  45. json_repr = json.dumps(
  46. {"material": kp.material, "id": name, "name": name},
  47. sort_keys=True)
  48. self.assertEqual(
  49. kp.to_json(), json_repr,
  50. "JSON key pair representation {0} does not match expected {1}"
  51. .format(kp.to_json(), json_repr))
  52. kpl = self.provider.security.key_pairs.list()
  53. found_kp = [k for k in kpl if k.name == name]
  54. self.assertTrue(
  55. len(found_kp) == 0,
  56. "Key pair {0} should have been deleted but still exists."
  57. .format(name))
  58. def cleanup_sg(self, sg, net):
  59. with helpers.cleanup_action(
  60. lambda: self.provider.network.delete(network_id=net.id)):
  61. self.provider.security.security_groups.delete(group_id=sg.id)
  62. @helpers.skipIfNoService(['security.security_groups'])
  63. def test_crud_security_group_service(self):
  64. name = 'CBTestSecurityGroupA-{0}'.format(uuid.uuid4())
  65. # Declare these variables and late binding will allow
  66. # the cleanup method access to the most current values
  67. net = None
  68. sg = None
  69. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  70. network=net, security_group=sg)):
  71. net, _ = helpers.create_test_network(self.provider, name)
  72. sg = self.provider.security.security_groups.create(
  73. name=name, description=name, network_id=net.id)
  74. self.assertEqual(name, sg.description)
  75. sit.check_standard_behaviour(
  76. self, self.provider.security.security_groups, sg)
  77. # sit.check_delete(self, self.provider.security.security_groups, sg)
  78. sgl = self.provider.security.security_groups.list()
  79. found_sg = [g for g in sgl if g.name == name]
  80. self.assertTrue(
  81. len(found_sg) == 0,
  82. "Security group {0} should have been deleted but still exists."
  83. .format(name))
  84. @helpers.skipIfNoService(['security.security_groups'])
  85. def test_security_group(self):
  86. """Test for proper creation of a security group."""
  87. name = 'CBTestSecurityGroupB-{0}'.format(uuid.uuid4())
  88. # Declare these variables and late binding will allow
  89. # the cleanup method access to the most current values
  90. net = None
  91. sg = None
  92. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  93. network=net, security_group=sg)):
  94. net, _ = helpers.create_test_network(self.provider, name)
  95. sg = self.provider.security.security_groups.create(
  96. name=name, description=name, network_id=net.id)
  97. rule = sg.add_rule(ip_protocol='tcp', from_port=1111, to_port=1111,
  98. cidr_ip='0.0.0.0/0')
  99. found_rule = sg.get_rule(ip_protocol='tcp', from_port=1111,
  100. to_port=1111, cidr_ip='0.0.0.0/0')
  101. self.assertTrue(
  102. rule == found_rule,
  103. "Expected rule {0} not found in security group: {0}".format(
  104. rule, sg.rules))
  105. object_keys = (
  106. sg.rules[0].ip_protocol,
  107. sg.rules[0].from_port,
  108. sg.rules[0].to_port)
  109. self.assertTrue(
  110. all(str(key) in repr(sg.rules[0]) for key in object_keys),
  111. "repr(obj) should contain ip_protocol, form_port, and to_port"
  112. " so that the object can be reconstructed, but does not:"
  113. " {0}; {1}".format(sg.rules[0], object_keys))
  114. self.assertTrue(
  115. sg == sg,
  116. "The same security groups should be equal?")
  117. self.assertFalse(
  118. sg != sg,
  119. "The same security groups should still be equal?")
  120. # json_repr = json.dumps(
  121. # {"description": name, "name": name, "id": sg.id,
  122. # "rules":
  123. # [{"from_port": 1111, "group": "", "cidr_ip": "0.0.0.0/0",
  124. # "parent": sg.id, "to_port": 1111, "ip_protocol": "tcp",
  125. # "id": sg.rules[0].id}]},
  126. # sort_keys=True)
  127. # self.assertTrue(
  128. # sg.to_json() == json_repr,
  129. # "JSON SG representation {0} does not match expected {1}"
  130. # .format(sg.to_json(), json_repr))
  131. # sit.check_delete(self, self.provider.security.security_groups, sg)
  132. sgl = self.provider.security.security_groups.list()
  133. found_sg = [g for g in sgl if g.name == name]
  134. self.assertTrue(
  135. len(found_sg) == 0,
  136. "Security group {0} should have been deleted but still exists."
  137. .format(name))
  138. @helpers.skipIfNoService(['security.security_groups'])
  139. def test_security_group_rule_add_twice(self):
  140. """Test whether adding the same rule twice succeeds."""
  141. if isinstance(self.provider, TestMockHelperMixin):
  142. raise unittest.SkipTest(
  143. "Mock provider returns InvalidParameterValue: "
  144. "Value security_group is invalid for parameter.")
  145. name = 'CBTestSecurityGroupC-{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. rule = sg.add_rule(ip_protocol='tcp', from_port=1111, to_port=1111,
  156. cidr_ip='0.0.0.0/0')
  157. # attempting to add the same rule twice should succeed
  158. same_rule = sg.add_rule(ip_protocol='tcp', from_port=1111,
  159. to_port=1111, cidr_ip='0.0.0.0/0')
  160. self.assertTrue(
  161. rule == same_rule,
  162. "Expected rule {0} not found in security group: {0}".format(
  163. same_rule, sg.rules))
  164. @helpers.skipIfNoService(['security.security_groups'])
  165. def test_security_group_group_rule(self):
  166. """Test for proper creation of a security group rule."""
  167. name = 'CBTestSecurityGroupD-{0}'.format(uuid.uuid4())
  168. # Declare these variables and late binding will allow
  169. # the cleanup method access to the most current values
  170. net = None
  171. sg = None
  172. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  173. network=net, security_group=sg)):
  174. net, _ = helpers.create_test_network(self.provider, name)
  175. sg = self.provider.security.security_groups.create(
  176. name=name, description=name, network_id=net.id)
  177. self.assertTrue(
  178. len(sg.rules) == 0,
  179. "Expected no security group group rule. Got {0}."
  180. .format(sg.rules))
  181. rule = sg.add_rule(src_group=sg, ip_protocol='tcp', from_port=1,
  182. to_port=65535)
  183. self.assertTrue(
  184. rule.group.name == name,
  185. "Expected security group rule name {0}. Got {1}."
  186. .format(name, rule.group.name))
  187. for r in sg.rules:
  188. r.delete()
  189. sg = self.provider.security.security_groups.get(sg.id) # update
  190. self.assertTrue(
  191. len(sg.rules) == 0,
  192. "Deleting SecurityGroupRule should delete it: {0}".format(
  193. sg.rules))
  194. sgl = self.provider.security.security_groups.list()
  195. found_sg = [g for g in sgl if g.name == name]
  196. self.assertTrue(
  197. len(found_sg) == 0,
  198. "Security group {0} should have been deleted but still exists."
  199. .format(name))