test_security_service.py 8.4 KB

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