test_security_service.py 8.4 KB

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