test_security_service.py 8.4 KB

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