test_security_service.py 7.7 KB

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