test_provider_security_service.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import uuid
  2. from test.helpers import ProviderTestBase
  3. import test.helpers as helpers
  4. class ProviderSecurityServiceTestCase(ProviderTestBase):
  5. def __init__(self, methodName, provider):
  6. super(ProviderSecurityServiceTestCase, self).__init__(
  7. methodName=methodName, provider=provider)
  8. def test_crud_key_pair_service(self):
  9. name = 'cbtestkeypairA-{0}'.format(uuid.uuid4())
  10. kp = self.provider.security.key_pairs.create(name=name)
  11. with helpers.cleanup_action(
  12. lambda:
  13. self.provider.security.key_pairs.delete(name=kp.name)
  14. ):
  15. found_kp = self.provider.security.key_pairs.find(name=name)
  16. self.assertTrue(
  17. found_kp == kp,
  18. "Find key pair did not return the expected key {0}."
  19. .format(name))
  20. kpl = self.provider.security.key_pairs.list()
  21. found_kp = [k for k in kpl if k.name == name]
  22. self.assertTrue(
  23. len(found_kp) == 0,
  24. "Key pair {0} should have been deleted but still exists."
  25. .format(name))
  26. no_kp = self.provider.security.key_pairs.find(name='bogus_kp')
  27. self.assertTrue(
  28. no_kp is None,
  29. "Found a key pair {0} that should not exist?".format(no_kp))
  30. def test_key_pair(self):
  31. name = 'cbtestkeypairB-{0}'.format(uuid.uuid4())
  32. kp = self.provider.security.key_pairs.create(name=name)
  33. with helpers.cleanup_action(lambda: kp.delete()):
  34. kpl = self.provider.security.key_pairs.list()
  35. found_kp = [k for k in kpl if k.name == name]
  36. self.assertTrue(
  37. len(found_kp) == 1,
  38. "List key pairs did not return the expected key {0}."
  39. .format(name))
  40. self.assertTrue(
  41. kp.name in repr(kp),
  42. "repr(obj) should contain the object id so that the object"
  43. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  44. self.assertIsNotNone(
  45. kp.material,
  46. "KeyPair material is empty but it should not be.")
  47. self.assertTrue(
  48. kp == kp,
  49. "The same key pair should be equal to self.")
  50. kpl = self.provider.security.key_pairs.list()
  51. found_kp = [k for k in kpl if k.name == name]
  52. self.assertTrue(
  53. len(found_kp) == 0,
  54. "Key pair {0} should have been deleted but still exists."
  55. .format(name))
  56. def test_crud_security_group_service(self):
  57. name = 'cbtestsecuritygroupA-{0}'.format(uuid.uuid4())
  58. sg = self.provider.security.security_groups.create(
  59. name=name, description=name)
  60. with helpers.cleanup_action(
  61. lambda:
  62. self.provider.security.security_groups.delete(group_id=sg.id)
  63. ):
  64. sgl = self.provider.security.security_groups.get(
  65. group_names=[
  66. sg.name])
  67. found_sg = [g for g in sgl if g.name == name]
  68. self.assertTrue(
  69. len(found_sg) == 1,
  70. "List security groups did not return the expected group {0}."
  71. .format(name))
  72. sgl = self.provider.security.security_groups.list()
  73. found_sg = [g for g in sgl if g.name == name]
  74. self.assertTrue(
  75. len(found_sg) == 0,
  76. "Security group {0} should have been deleted but still exists."
  77. .format(name))
  78. def test_security_group(self):
  79. """Test for proper creation of a security group."""
  80. name = 'cbtestsecuritygroupB-{0}'.format(uuid.uuid4())
  81. sg = self.provider.security.security_groups.create(
  82. name=name, description=name)
  83. with helpers.cleanup_action(lambda: sg.delete()):
  84. sg.add_rule(ip_protocol='tcp', from_port=1111, to_port=1111,
  85. cidr_ip='0.0.0.0/0')
  86. found_rules = [rule for rule in sg.rules if
  87. rule.cidr_ip == '0.0.0.0/0' and
  88. rule.ip_protocol == 'tcp' and
  89. rule.from_port == 1111 and
  90. rule.to_port == 1111]
  91. self.assertTrue(
  92. len(found_rules) == 1,
  93. "Expected rule not found in security group: {0}".format(name))
  94. object_keys = (
  95. sg.rules[0].ip_protocol,
  96. sg.rules[0].from_port,
  97. sg.rules[0].to_port)
  98. self.assertTrue(
  99. all(str(key) in repr(sg.rules[0]) for key in object_keys),
  100. "repr(obj) should contain ip_protocol, form_port and to_port"
  101. " so that the object can be reconstructed, but does not."
  102. " eval(repr(obj)) == obj")
  103. self.assertTrue(
  104. sg == sg,
  105. "The same security groups should be equal?")
  106. self.assertFalse(
  107. sg != sg,
  108. "The same security groups should still be equal?")
  109. sgl = self.provider.security.security_groups.list()
  110. found_sg = [g for g in sgl if g.name == name]
  111. self.assertTrue(
  112. len(found_sg) == 0,
  113. "Security group {0} should have been deleted but still exists."
  114. .format(name))
  115. def test_security_group_group_role(self):
  116. """Test for proper creation of a security group rule."""
  117. name = 'cbtestsecuritygroupC-{0}'.format(uuid.uuid4())
  118. sg = self.provider.security.security_groups.create(
  119. name=name, description=name)
  120. with helpers.cleanup_action(lambda: sg.delete()):
  121. self.assertTrue(
  122. len(sg.rules) == 0,
  123. "Expected no security group group rule. Got {0}."
  124. .format(sg.rules))
  125. sg.add_rule(src_group=sg)
  126. self.assertTrue(
  127. sg.rules[0].group.name == name,
  128. "Expected security group rule name {0}. Got {1}."
  129. .format(name, sg.rules[0].group.name))
  130. sgl = self.provider.security.security_groups.list()
  131. found_sg = [g for g in sgl if g.name == name]
  132. self.assertTrue(
  133. len(found_sg) == 0,
  134. "Security group {0} should have been deleted but still exists."
  135. .format(name))