test_security_service.py 7.2 KB

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