test_security_service.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import json
  2. import uuid
  3. from test.helpers import ProviderTestBase
  4. import test.helpers as helpers
  5. class CloudSecurityServiceTestCase(ProviderTestBase):
  6. def __init__(self, methodName, provider):
  7. super(CloudSecurityServiceTestCase, self).__init__(
  8. methodName=methodName, provider=provider)
  9. def test_crud_key_pair_service(self):
  10. name = 'cbtestkeypairA-{0}'.format(uuid.uuid4())
  11. kp = self.provider.security.key_pairs.create(name=name)
  12. with helpers.cleanup_action(
  13. lambda:
  14. self.provider.security.key_pairs.delete(key_pair_id=kp.id)
  15. ):
  16. # test list method
  17. kpl = self.provider.security.key_pairs.list()
  18. list_kpl = [i for i in kpl if i.name == name]
  19. self.assertTrue(
  20. len(list_kpl) == 1,
  21. "List key pairs does not return the expected key pair %s" %
  22. name)
  23. # check iteration
  24. iter_kpl = [i for i in self.provider.security.key_pairs
  25. if i.name == name]
  26. self.assertTrue(
  27. len(iter_kpl) == 1,
  28. "Iter key pairs does not return the expected key pair %s" %
  29. name)
  30. # check find
  31. find_kp = self.provider.security.key_pairs.find(name=name)[0]
  32. self.assertTrue(
  33. find_kp == kp,
  34. "Find key pair did not return the expected key {0}."
  35. .format(name))
  36. # check get
  37. get_kp = self.provider.security.key_pairs.get(name)
  38. self.assertTrue(
  39. get_kp == kp,
  40. "Get key pair did not return the expected key {0}."
  41. .format(name))
  42. recreated_kp = self.provider.security.key_pairs.create(name=name)
  43. self.assertTrue(
  44. recreated_kp == kp,
  45. "Recreating key pair did not return the expected key {0}."
  46. .format(name))
  47. kpl = self.provider.security.key_pairs.list()
  48. found_kp = [k for k in kpl if k.name == name]
  49. self.assertTrue(
  50. len(found_kp) == 0,
  51. "Key pair {0} should have been deleted but still exists."
  52. .format(name))
  53. no_kp = self.provider.security.key_pairs.find(name='bogus_kp')
  54. self.assertFalse(
  55. no_kp,
  56. "Found a key pair {0} that should not exist?".format(no_kp))
  57. def test_key_pair(self):
  58. name = 'cbtestkeypairB-{0}'.format(uuid.uuid4())
  59. kp = self.provider.security.key_pairs.create(name=name)
  60. with helpers.cleanup_action(lambda: kp.delete()):
  61. kpl = self.provider.security.key_pairs.list()
  62. found_kp = [k for k in kpl if k.name == name]
  63. self.assertTrue(
  64. len(found_kp) == 1,
  65. "List key pairs did not return the expected key {0}."
  66. .format(name))
  67. self.assertTrue(
  68. kp.id in repr(kp),
  69. "repr(obj) should contain the object id so that the object"
  70. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  71. self.assertIsNotNone(
  72. kp.material,
  73. "KeyPair material is empty but it should not be.")
  74. self.assertTrue(
  75. kp == kp,
  76. "The same key pair should be equal to self.")
  77. json_repr = json.dumps(
  78. {"material": kp.material, "id": name, "name": name},
  79. sort_keys=True)
  80. self.assertEqual(
  81. kp.to_json(), json_repr,
  82. "JSON key pair representation {0} does not match expected {1}"
  83. .format(kp.to_json(), json_repr))
  84. kpl = self.provider.security.key_pairs.list()
  85. found_kp = [k for k in kpl if k.name == name]
  86. self.assertTrue(
  87. len(found_kp) == 0,
  88. "Key pair {0} should have been deleted but still exists."
  89. .format(name))
  90. def test_crud_security_group_service(self):
  91. name = 'cbtestsecuritygroupA-{0}'.format(uuid.uuid4())
  92. sg = self.provider.security.security_groups.create(
  93. name=name, description=name)
  94. with helpers.cleanup_action(
  95. lambda:
  96. self.provider.security.security_groups.delete(group_id=sg.id)
  97. ):
  98. self.assertEqual(name, sg.description)
  99. # test list method
  100. sgl = self.provider.security.security_groups.list()
  101. found_sgl = [i for i in sgl if i.name == name]
  102. self.assertTrue(
  103. len(found_sgl) == 1,
  104. "List security groups does not return the expected group %s" %
  105. name)
  106. # check iteration
  107. found_sgl = [i for i in self.provider.security.security_groups
  108. if i.name == name]
  109. self.assertTrue(
  110. len(found_sgl) == 1,
  111. "Iter security groups does not return the expected group %s" %
  112. name)
  113. # check find
  114. find_sg = self.provider.security.security_groups.find(name=sg.name)
  115. self.assertTrue(
  116. len(find_sg) == 1,
  117. "List security groups returned {0} when expected was: {1}."
  118. .format(find_sg, sg.name))
  119. # check get
  120. get_sg = self.provider.security.security_groups.get(sg.id)
  121. self.assertTrue(
  122. get_sg == sg,
  123. "Get SecurityGroup did not return the expected key {0}."
  124. .format(name))
  125. self.assertTrue(
  126. sg.id in repr(sg),
  127. "repr(obj) should contain the object id so that the object"
  128. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  129. sgl = self.provider.security.security_groups.list()
  130. found_sg = [g for g in sgl if g.name == name]
  131. self.assertTrue(
  132. len(found_sg) == 0,
  133. "Security group {0} should have been deleted but still exists."
  134. .format(name))
  135. no_sg = self.provider.security.security_groups.find(name='bogus_sg')
  136. self.assertTrue(
  137. len(no_sg) == 0,
  138. "Found a bogus security group?!?".format(no_sg))
  139. def test_security_group(self):
  140. """Test for proper creation of a security group."""
  141. name = 'cbtestsecuritygroupB-{0}'.format(uuid.uuid4())
  142. sg = self.provider.security.security_groups.create(
  143. name=name, description=name)
  144. with helpers.cleanup_action(lambda: sg.delete()):
  145. sg.add_rule(ip_protocol='tcp', from_port=1111, to_port=1111,
  146. cidr_ip='0.0.0.0/0')
  147. rule = sg.get_rule(ip_protocol='tcp', from_port=1111, to_port=1111,
  148. cidr_ip='0.0.0.0/0')
  149. self.assertTrue(
  150. (rule.ip_protocol == 'tcp' and
  151. rule.from_port == 1111 and
  152. rule.to_port == 1111 and
  153. rule.cidr_ip == '0.0.0.0/0'),
  154. "Expected rule {0} not found in security group: {0}".format(
  155. rule, sg.rules))
  156. object_keys = (
  157. sg.rules[0].ip_protocol,
  158. sg.rules[0].from_port,
  159. sg.rules[0].to_port)
  160. self.assertTrue(
  161. all(str(key) in repr(sg.rules[0]) for key in object_keys),
  162. "repr(obj) should contain ip_protocol, form_port, and to_port"
  163. " so that the object can be reconstructed, but does not:"
  164. " {0}; {1}".format(sg.rules[0], object_keys))
  165. self.assertTrue(
  166. sg == sg,
  167. "The same security groups should be equal?")
  168. self.assertFalse(
  169. sg != sg,
  170. "The same security groups should still be equal?")
  171. json_repr = json.dumps(
  172. {"description": name, "name": name, "id": sg.id, "rules":
  173. [{"from_port": "1111", "group": "", "cidr_ip": "0.0.0.0/0",
  174. "parent": sg.id, "to_port": "1111", "ip_protocol": "tcp",
  175. "id": sg.rules[0].id}]},
  176. sort_keys=True)
  177. self.assertTrue(
  178. sg.to_json() == json_repr,
  179. "JSON sec group representation {0} does not match expected {1}"
  180. .format(sg.to_json(), json_repr))
  181. sgl = self.provider.security.security_groups.list()
  182. found_sg = [g for g in sgl if g.name == name]
  183. self.assertTrue(
  184. len(found_sg) == 0,
  185. "Security group {0} should have been deleted but still exists."
  186. .format(name))
  187. def test_security_group_group_role(self):
  188. """Test for proper creation of a security group rule."""
  189. name = 'cbtestsecuritygroupC-{0}'.format(uuid.uuid4())
  190. sg = self.provider.security.security_groups.create(
  191. name=name, description=name)
  192. with helpers.cleanup_action(lambda: sg.delete()):
  193. self.assertTrue(
  194. len(sg.rules) == 0,
  195. "Expected no security group group rule. Got {0}."
  196. .format(sg.rules))
  197. rule = sg.add_rule(src_group=sg)
  198. self.assertTrue(
  199. rule.group.name == name,
  200. "Expected security group rule name {0}. Got {1}."
  201. .format(name, rule.group.name))
  202. rule.delete()
  203. self.assertTrue(
  204. len(sg.rules) == 0,
  205. "Deleting SecurityGroupRule should delete it: {0}".format(
  206. sg.rules))
  207. sgl = self.provider.security.security_groups.list()
  208. found_sg = [g for g in sgl if g.name == name]
  209. self.assertTrue(
  210. len(found_sg) == 0,
  211. "Security group {0} should have been deleted but still exists."
  212. .format(name))