test_security_service.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. # Recreating existing keypair should raise an exception
  43. with self.assertRaises(Exception):
  44. self.provider.security.key_pairs.create(name=name)
  45. kpl = self.provider.security.key_pairs.list()
  46. found_kp = [k for k in kpl if k.name == name]
  47. self.assertTrue(
  48. len(found_kp) == 0,
  49. "Key pair {0} should have been deleted but still exists."
  50. .format(name))
  51. no_kp = self.provider.security.key_pairs.find(name='bogus_kp')
  52. self.assertFalse(
  53. no_kp,
  54. "Found a key pair {0} that should not exist?".format(no_kp))
  55. def test_key_pair(self):
  56. name = 'cbtestkeypairB-{0}'.format(uuid.uuid4())
  57. kp = self.provider.security.key_pairs.create(name=name)
  58. with helpers.cleanup_action(lambda: kp.delete()):
  59. kpl = self.provider.security.key_pairs.list()
  60. found_kp = [k for k in kpl if k.name == name]
  61. self.assertTrue(
  62. len(found_kp) == 1,
  63. "List key pairs did not return the expected key {0}."
  64. .format(name))
  65. self.assertTrue(
  66. kp.id in repr(kp),
  67. "repr(obj) should contain the object id so that the object"
  68. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  69. self.assertIsNotNone(
  70. kp.material,
  71. "KeyPair material is empty but it should not be.")
  72. self.assertTrue(
  73. kp == kp,
  74. "The same key pair should be equal to self.")
  75. json_repr = json.dumps(
  76. {"material": kp.material, "id": name, "name": name},
  77. sort_keys=True)
  78. self.assertEqual(
  79. kp.to_json(), json_repr,
  80. "JSON key pair representation {0} does not match expected {1}"
  81. .format(kp.to_json(), json_repr))
  82. kpl = self.provider.security.key_pairs.list()
  83. found_kp = [k for k in kpl if k.name == name]
  84. self.assertTrue(
  85. len(found_kp) == 0,
  86. "Key pair {0} should have been deleted but still exists."
  87. .format(name))
  88. def test_crud_security_group_service(self):
  89. name = 'cbtestsecuritygroupA-{0}'.format(uuid.uuid4())
  90. sg = self.provider.security.security_groups.create(
  91. name=name, description=name)
  92. with helpers.cleanup_action(
  93. lambda:
  94. self.provider.security.security_groups.delete(group_id=sg.id)
  95. ):
  96. self.assertEqual(name, sg.description)
  97. # test list method
  98. sgl = self.provider.security.security_groups.list()
  99. found_sgl = [i for i in sgl if i.name == name]
  100. self.assertTrue(
  101. len(found_sgl) == 1,
  102. "List security groups does not return the expected group %s" %
  103. name)
  104. # check iteration
  105. found_sgl = [i for i in self.provider.security.security_groups
  106. if i.name == name]
  107. self.assertTrue(
  108. len(found_sgl) == 1,
  109. "Iter security groups does not return the expected group %s" %
  110. name)
  111. # check find
  112. find_sg = self.provider.security.security_groups.find(name=sg.name)
  113. self.assertTrue(
  114. len(find_sg) == 1,
  115. "List security groups returned {0} when expected was: {1}."
  116. .format(find_sg, sg.name))
  117. # check get
  118. get_sg = self.provider.security.security_groups.get(sg.id)
  119. self.assertTrue(
  120. get_sg == sg,
  121. "Get SecurityGroup did not return the expected key {0}."
  122. .format(name))
  123. self.assertTrue(
  124. sg.id in repr(sg),
  125. "repr(obj) should contain the object id so that the object"
  126. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  127. sgl = self.provider.security.security_groups.list()
  128. found_sg = [g for g in sgl if g.name == name]
  129. self.assertTrue(
  130. len(found_sg) == 0,
  131. "Security group {0} should have been deleted but still exists."
  132. .format(name))
  133. no_sg = self.provider.security.security_groups.find(name='bogus_sg')
  134. self.assertTrue(
  135. len(no_sg) == 0,
  136. "Found a bogus security group?!?".format(no_sg))
  137. def test_security_group(self):
  138. """Test for proper creation of a security group."""
  139. name = 'cbtestsecuritygroupB-{0}'.format(uuid.uuid4())
  140. sg = self.provider.security.security_groups.create(
  141. name=name, description=name)
  142. with helpers.cleanup_action(lambda: sg.delete()):
  143. rule = sg.add_rule(ip_protocol='tcp', from_port=1111, to_port=1111,
  144. cidr_ip='0.0.0.0/0')
  145. found_rule = sg.get_rule(ip_protocol='tcp', from_port=1111,
  146. to_port=1111, cidr_ip='0.0.0.0/0')
  147. self.assertTrue(
  148. rule == found_rule,
  149. "Expected rule {0} not found in security group: {0}".format(
  150. rule, sg.rules))
  151. object_keys = (
  152. sg.rules[0].ip_protocol,
  153. sg.rules[0].from_port,
  154. sg.rules[0].to_port)
  155. self.assertTrue(
  156. all(str(key) in repr(sg.rules[0]) for key in object_keys),
  157. "repr(obj) should contain ip_protocol, form_port, and to_port"
  158. " so that the object can be reconstructed, but does not:"
  159. " {0}; {1}".format(sg.rules[0], object_keys))
  160. self.assertTrue(
  161. sg == sg,
  162. "The same security groups should be equal?")
  163. self.assertFalse(
  164. sg != sg,
  165. "The same security groups should still be equal?")
  166. json_repr = json.dumps(
  167. {"description": name, "name": name, "id": sg.id, "rules":
  168. [{"from_port": 1111, "group": "", "cidr_ip": "0.0.0.0/0",
  169. "parent": sg.id, "to_port": 1111, "ip_protocol": "tcp",
  170. "id": sg.rules[0].id}]},
  171. sort_keys=True)
  172. self.assertTrue(
  173. sg.to_json() == json_repr,
  174. "JSON sec group representation {0} does not match expected {1}"
  175. .format(sg.to_json(), json_repr))
  176. sgl = self.provider.security.security_groups.list()
  177. found_sg = [g for g in sgl if g.name == name]
  178. self.assertTrue(
  179. len(found_sg) == 0,
  180. "Security group {0} should have been deleted but still exists."
  181. .format(name))
  182. def test_security_group_rule_add_twice(self):
  183. """Test whether adding the same rule twice succeeds."""
  184. name = 'cbtestsecuritygroupB-{0}'.format(uuid.uuid4())
  185. sg = self.provider.security.security_groups.create(
  186. name=name, description=name)
  187. with helpers.cleanup_action(lambda: sg.delete()):
  188. rule = sg.add_rule(ip_protocol='tcp', from_port=1111, to_port=1111,
  189. cidr_ip='0.0.0.0/0')
  190. # attempting to add the same rule twice should succeed
  191. same_rule = sg.add_rule(ip_protocol='tcp', from_port=1111,
  192. to_port=1111, cidr_ip='0.0.0.0/0')
  193. self.assertTrue(
  194. rule == same_rule,
  195. "Expected rule {0} not found in security group: {0}".format(
  196. same_rule, sg.rules))
  197. def test_security_group_group_rule(self):
  198. """Test for proper creation of a security group rule."""
  199. name = 'cbtestsecuritygroupC-{0}'.format(uuid.uuid4())
  200. sg = self.provider.security.security_groups.create(
  201. name=name, description=name)
  202. with helpers.cleanup_action(lambda: sg.delete()):
  203. self.assertTrue(
  204. len(sg.rules) == 0,
  205. "Expected no security group group rule. Got {0}."
  206. .format(sg.rules))
  207. rule = sg.add_rule(src_group=sg)
  208. self.assertTrue(
  209. rule.group.name == name,
  210. "Expected security group rule name {0}. Got {1}."
  211. .format(name, rule.group.name))
  212. for r in sg.rules:
  213. r.delete()
  214. sg = self.provider.security.security_groups.get(sg.id) # update
  215. self.assertTrue(
  216. len(sg.rules) == 0,
  217. "Deleting SecurityGroupRule should delete it: {0}".format(
  218. sg.rules))
  219. sgl = self.provider.security.security_groups.list()
  220. found_sg = [g for g in sgl if g.name == name]
  221. self.assertTrue(
  222. len(found_sg) == 0,
  223. "Security group {0} should have been deleted but still exists."
  224. .format(name))