test_security_service.py 8.4 KB

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