test_provider_security_service.py 6.6 KB

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