test_security_service.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import uuid
  2. from test.helpers import ProviderTestBase
  3. import test.helpers as helpers
  4. class CloudSecurityServiceTestCase(ProviderTestBase):
  5. def __init__(self, methodName, provider):
  6. super(CloudSecurityServiceTestCase, 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. # test list method
  16. kpl = self.provider.security.key_pairs.list()
  17. found_kpl = [i for i in kpl if i.name == name]
  18. self.assertTrue(
  19. len(found_kpl) == 1,
  20. "List keypairs does not return the expected keypair %s" %
  21. name)
  22. # check iteration
  23. found_kpl = [i for i in self.provider.security.key_pairs
  24. if i.name == name]
  25. self.assertTrue(
  26. len(found_kpl) == 1,
  27. "Iter keypairs does not return the expected keypair %s" %
  28. name)
  29. found_kp = self.provider.security.key_pairs.find(name=name)
  30. self.assertTrue(
  31. found_kp == kp,
  32. "Find key pair did not return the expected key {0}."
  33. .format(name))
  34. recreated_kp = self.provider.security.key_pairs.create(name=name)
  35. self.assertTrue(
  36. recreated_kp == kp,
  37. "Recreating key pair did not return the expected key {0}."
  38. .format(name))
  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) == 0,
  43. "Key pair {0} should have been deleted but still exists."
  44. .format(name))
  45. no_kp = self.provider.security.key_pairs.find(name='bogus_kp')
  46. self.assertTrue(
  47. no_kp is None,
  48. "Found a key pair {0} that should not exist?".format(no_kp))
  49. def test_key_pair(self):
  50. name = 'cbtestkeypairB-{0}'.format(uuid.uuid4())
  51. kp = self.provider.security.key_pairs.create(name=name)
  52. with helpers.cleanup_action(lambda: kp.delete()):
  53. kpl = self.provider.security.key_pairs.list()
  54. found_kp = [k for k in kpl if k.name == name]
  55. self.assertTrue(
  56. len(found_kp) == 1,
  57. "List key pairs did not return the expected key {0}."
  58. .format(name))
  59. self.assertTrue(
  60. kp.id in repr(kp),
  61. "repr(obj) should contain the object id so that the object"
  62. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  63. self.assertIsNotNone(
  64. kp.material,
  65. "KeyPair material is empty but it should not be.")
  66. self.assertTrue(
  67. kp == kp,
  68. "The same key pair should be equal to self.")
  69. kpl = self.provider.security.key_pairs.list()
  70. found_kp = [k for k in kpl if k.name == name]
  71. self.assertTrue(
  72. len(found_kp) == 0,
  73. "Key pair {0} should have been deleted but still exists."
  74. .format(name))
  75. def test_crud_security_group_service(self):
  76. name = 'cbtestsecuritygroupA-{0}'.format(uuid.uuid4())
  77. sg = self.provider.security.security_groups.create(
  78. name=name, description=name)
  79. with helpers.cleanup_action(
  80. lambda:
  81. self.provider.security.security_groups.delete(group_id=sg.id)
  82. ):
  83. self.assertEqual(name, sg.description)
  84. # test list method
  85. sgl = self.provider.security.security_groups.list()
  86. found_sgl = [i for i in sgl if i.name == name]
  87. self.assertTrue(
  88. len(found_sgl) == 1,
  89. "List security groups does not return the expected group %s" %
  90. name)
  91. # check iteration
  92. found_sgl = [i for i in self.provider.security.security_groups
  93. if i.name == name]
  94. self.assertTrue(
  95. len(found_sgl) == 1,
  96. "Iter security groups does not return the expected group %s" %
  97. name)
  98. sgl = self.provider.security.security_groups.get(
  99. group_names=[
  100. sg.name])
  101. found_sg = [g for g in sgl if g.name == name]
  102. self.assertTrue(
  103. len(found_sg) == 1,
  104. "List security groups did not return the expected group {0}."
  105. .format(name))
  106. self.assertTrue(
  107. sg.id in repr(sg),
  108. "repr(obj) should contain the object id so that the object"
  109. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  110. sgl = self.provider.security.security_groups.list()
  111. found_sg = [g for g in sgl if g.name == name]
  112. self.assertTrue(
  113. len(found_sg) == 0,
  114. "Security group {0} should have been deleted but still exists."
  115. .format(name))
  116. no_sg = self.provider.security.security_groups.get(
  117. group_ids=['bogus_sg'])
  118. self.assertTrue(
  119. len(no_sg) == 0,
  120. "Found a bogus security group?!?".format(no_sg))
  121. def test_security_group(self):
  122. """Test for proper creation of a security group."""
  123. name = 'cbtestsecuritygroupB-{0}'.format(uuid.uuid4())
  124. sg = self.provider.security.security_groups.create(
  125. name=name, description=name)
  126. with helpers.cleanup_action(lambda: sg.delete()):
  127. sg.add_rule(ip_protocol='tcp', from_port=1111, to_port=1111,
  128. cidr_ip='0.0.0.0/0')
  129. found_rules = [rule for rule in sg.rules if
  130. rule.cidr_ip == '0.0.0.0/0' and
  131. rule.ip_protocol == 'tcp' and
  132. rule.from_port == 1111 and
  133. rule.to_port == 1111]
  134. self.assertTrue(
  135. len(found_rules) == 1,
  136. "Expected rule not found in security group: {0}".format(name))
  137. object_keys = (
  138. sg.rules[0].ip_protocol,
  139. sg.rules[0].from_port,
  140. sg.rules[0].to_port)
  141. self.assertTrue(
  142. all(str(key) in repr(sg.rules[0]) for key in object_keys),
  143. "repr(obj) should contain ip_protocol, form_port and to_port"
  144. " so that the object can be reconstructed, but does not."
  145. " eval(repr(obj)) == obj")
  146. self.assertTrue(
  147. sg == sg,
  148. "The same security groups should be equal?")
  149. self.assertFalse(
  150. sg != sg,
  151. "The same security groups should still be equal?")
  152. sgl = self.provider.security.security_groups.list()
  153. found_sg = [g for g in sgl if g.name == name]
  154. self.assertTrue(
  155. len(found_sg) == 0,
  156. "Security group {0} should have been deleted but still exists."
  157. .format(name))
  158. def test_security_group_group_role(self):
  159. """Test for proper creation of a security group rule."""
  160. name = 'cbtestsecuritygroupC-{0}'.format(uuid.uuid4())
  161. sg = self.provider.security.security_groups.create(
  162. name=name, description=name)
  163. with helpers.cleanup_action(lambda: sg.delete()):
  164. self.assertTrue(
  165. len(sg.rules) == 0,
  166. "Expected no security group group rule. Got {0}."
  167. .format(sg.rules))
  168. sg.add_rule(src_group=sg)
  169. self.assertTrue(
  170. sg.rules[0].group.name == name,
  171. "Expected security group rule name {0}. Got {1}."
  172. .format(name, sg.rules[0].group.name))
  173. sgl = self.provider.security.security_groups.list()
  174. found_sg = [g for g in sgl if g.name == name]
  175. self.assertTrue(
  176. len(found_sg) == 0,
  177. "Security group {0} should have been deleted but still exists."
  178. .format(name))