test_provider_security_service.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.exception_action(
  12. lambda:
  13. self.provider.security.key_pairs.delete(name=kp.name)
  14. ):
  15. kpl = self.provider.security.key_pairs.list()
  16. found_kp = [k for k in kpl if k.name == name]
  17. self.assertTrue(
  18. len(found_kp) == 1,
  19. "List key pairs did not return the expected key {0}."
  20. .format(name))
  21. self.provider.security.key_pairs.delete(name=kp.name)
  22. kpl = self.provider.security.key_pairs.list()
  23. found_kp = [k for k in kpl if k.name == name]
  24. self.assertTrue(
  25. len(found_kp) == 0,
  26. "Key pair {0} should have been deleted but still exists."
  27. .format(name))
  28. no_kp = self.provider.security.key_pairs.delete(name='bogus_kp')
  29. self.assertTrue(
  30. no_kp,
  31. "Found a key pair {0} that should not exist?".format(no_kp))
  32. def test_key_pair(self):
  33. name = 'cbtestkeypairB-{0}'.format(uuid.uuid4())
  34. kp = self.provider.security.key_pairs.create(name=name)
  35. with helpers.exception_action(
  36. lambda:
  37. self.provider.security.key_pairs.delete(name=kp.name)
  38. ):
  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. repr(kp) == "<CBKeyPair: {0}>".format(name),
  47. "KeyPair repr {0} not matching expected format.".format(kp))
  48. self.assertIsNotNone(
  49. kp.material,
  50. "KeyPair material is empty but it should not be.")
  51. self.assertTrue(
  52. kp == kp,
  53. "The same key pair should be equal to self.")
  54. kp.delete()
  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.exception_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. self.provider.security.security_groups.delete(group_id=sg.id)
  78. sgl = self.provider.security.security_groups.list()
  79. found_sg = [g for g in sgl if g.name == name]
  80. self.assertTrue(
  81. len(found_sg) == 0,
  82. "Security group {0} should have been deleted but still exists."
  83. .format(name))
  84. def test_security_group(self):
  85. """Test for proper creation of a security group."""
  86. name = 'cbtestsecuritygroupB-{0}'.format(uuid.uuid4())
  87. sg = self.provider.security.security_groups.create(
  88. name=name, description=name)
  89. with helpers.exception_action(
  90. lambda:
  91. self.provider.security.security_groups.delete(group_id=sg.id)
  92. ):
  93. sg.add_rule(ip_protocol='tcp', from_port=1111, to_port=1111,
  94. cidr_ip='0.0.0.0/0')
  95. found_rules = [rule for rule in sg.rules if
  96. rule.cidr_ip == '0.0.0.0/0' and
  97. rule.ip_protocol == 'tcp' and
  98. rule.from_port == 1111 and
  99. rule.to_port == 1111]
  100. self.assertTrue(
  101. len(found_rules) == 1,
  102. "Expected rule not found in security group: {0}".format(name))
  103. self.assertTrue(
  104. repr(sg.rules[0]) == ("<CBSecurityGroupRule: IP: {0}; from: "
  105. "{1}; to: {2}>"
  106. .format(sg.rules[0].ip_protocol,
  107. sg.rules[0].from_port,
  108. sg.rules[0].to_port)),
  109. ("Security group rule repr {0} not matching expected format."
  110. .format(sg.rules[0])))
  111. self.assertTrue(
  112. sg == sg,
  113. "The same security groups should be equal?")
  114. self.assertFalse(
  115. sg != sg,
  116. "The same security groups should still be equal?")
  117. sg.delete()
  118. sgl = self.provider.security.security_groups.list()
  119. found_sg = [g for g in sgl if g.name == name]
  120. self.assertTrue(
  121. len(found_sg) == 0,
  122. "Security group {0} should have been deleted but still exists."
  123. .format(name))
  124. def test_security_group_group_role(self):
  125. """Test for proper creation of a security group rule."""
  126. name = 'cbtestsecuritygroupC-{0}'.format(uuid.uuid4())
  127. sg = self.provider.security.security_groups.create(
  128. name=name, description=name)
  129. with helpers.exception_action(
  130. lambda:
  131. self.provider.security.security_groups.delete(group_id=sg.id)
  132. ):
  133. self.assertTrue(
  134. len(sg.rules) == 0,
  135. "Expected no security group group rule. Got {0}."
  136. .format(sg.rules))
  137. sg.add_rule(src_group=sg)
  138. self.assertTrue(
  139. sg.rules[0].group.name == name,
  140. "Expected security group rule name {0}. Got {1}."
  141. .format(name, sg.rules[0].group.name))
  142. sg.delete()
  143. sgl = self.provider.security.security_groups.list()
  144. found_sg = [g for g in sgl if g.name == name]
  145. self.assertTrue(
  146. len(found_sg) == 0,
  147. "Security group {0} should have been deleted but still exists."
  148. .format(name))