test_security_service.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. """Test cloudbridge.security modules."""
  2. import json
  3. import uuid
  4. from test.helpers import ProviderTestBase
  5. import test.helpers as helpers
  6. class CloudSecurityServiceTestCase(ProviderTestBase):
  7. def __init__(self, methodName, provider):
  8. super(CloudSecurityServiceTestCase, self).__init__(
  9. methodName=methodName, provider=provider)
  10. def test_crud_key_pair_service(self):
  11. name = 'cbtestkeypairA-{0}'.format(uuid.uuid4())
  12. kp = self.provider.security.key_pairs.create(name=name)
  13. with helpers.cleanup_action(
  14. lambda:
  15. self.provider.security.key_pairs.delete(key_pair_id=kp.id)
  16. ):
  17. # test list method
  18. kpl = self.provider.security.key_pairs.list()
  19. list_kpl = [i for i in kpl if i.name == name]
  20. self.assertTrue(
  21. len(list_kpl) == 1,
  22. "List key pairs does not return the expected key pair %s" %
  23. name)
  24. # check iteration
  25. iter_kpl = [i for i in self.provider.security.key_pairs
  26. if i.name == name]
  27. self.assertTrue(
  28. len(iter_kpl) == 1,
  29. "Iter key pairs does not return the expected key pair %s" %
  30. name)
  31. # check find
  32. find_kp = self.provider.security.key_pairs.find(name=name)[0]
  33. self.assertTrue(
  34. find_kp == kp,
  35. "Find key pair did not return the expected key {0}."
  36. .format(name))
  37. # check get
  38. get_kp = self.provider.security.key_pairs.get(name)
  39. self.assertTrue(
  40. get_kp == kp,
  41. "Get key pair did not return the expected key {0}."
  42. .format(name))
  43. # Recreating existing keypair should raise an exception
  44. with self.assertRaises(Exception):
  45. self.provider.security.key_pairs.create(name=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. json_repr = json.dumps(
  77. {"material": kp.material, "id": name, "name": name},
  78. sort_keys=True)
  79. self.assertEqual(
  80. kp.to_json(), json_repr,
  81. "JSON key pair representation {0} does not match expected {1}"
  82. .format(kp.to_json(), json_repr))
  83. kpl = self.provider.security.key_pairs.list()
  84. found_kp = [k for k in kpl if k.name == name]
  85. self.assertTrue(
  86. len(found_kp) == 0,
  87. "Key pair {0} should have been deleted but still exists."
  88. .format(name))
  89. def cleanup_sg(self, sg, net):
  90. self.provider.security.security_groups.delete(group_id=sg.id)
  91. self.provider.network.delete(network_id=net.id)
  92. def test_crud_security_group_service(self):
  93. name = 'cbtestsecuritygroupA-{0}'.format(uuid.uuid4())
  94. net = self.provider.network.create(name=name)
  95. sg = self.provider.security.security_groups.create(
  96. name=name, description=name, network_id=net.id)
  97. with helpers.cleanup_action(lambda: self.cleanup_sg(sg, net)):
  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. net = self.provider.network.create(name=name)
  143. sg = self.provider.security.security_groups.create(
  144. name=name, description=name, network_id=net.id)
  145. with helpers.cleanup_action(lambda: self.cleanup_sg(sg, net)):
  146. rule = sg.add_rule(ip_protocol='tcp', from_port=1111, to_port=1111,
  147. cidr_ip='0.0.0.0/0')
  148. found_rule = sg.get_rule(ip_protocol='tcp', from_port=1111,
  149. to_port=1111, cidr_ip='0.0.0.0/0')
  150. self.assertTrue(
  151. rule == found_rule,
  152. "Expected rule {0} not found in security group: {0}".format(
  153. rule, sg.rules))
  154. object_keys = (
  155. sg.rules[0].ip_protocol,
  156. sg.rules[0].from_port,
  157. sg.rules[0].to_port)
  158. self.assertTrue(
  159. all(str(key) in repr(sg.rules[0]) for key in object_keys),
  160. "repr(obj) should contain ip_protocol, form_port, and to_port"
  161. " so that the object can be reconstructed, but does not:"
  162. " {0}; {1}".format(sg.rules[0], object_keys))
  163. self.assertTrue(
  164. sg == sg,
  165. "The same security groups should be equal?")
  166. self.assertFalse(
  167. sg != sg,
  168. "The same security groups should still be equal?")
  169. json_repr = json.dumps(
  170. {"description": name, "name": name, "id": sg.id, "rules":
  171. [{"from_port": 1111, "group": "", "cidr_ip": "0.0.0.0/0",
  172. "parent": sg.id, "to_port": 1111, "ip_protocol": "tcp",
  173. "id": sg.rules[0].id}]},
  174. sort_keys=True)
  175. self.assertTrue(
  176. sg.to_json() == json_repr,
  177. "JSON sec group representation {0} does not match expected {1}"
  178. .format(sg.to_json(), json_repr))
  179. sgl = self.provider.security.security_groups.list()
  180. found_sg = [g for g in sgl if g.name == name]
  181. self.assertTrue(
  182. len(found_sg) == 0,
  183. "Security group {0} should have been deleted but still exists."
  184. .format(name))
  185. def test_security_group_rule_add_twice(self):
  186. """Test whether adding the same rule twice succeeds."""
  187. name = 'cbtestsecuritygroupB-{0}'.format(uuid.uuid4())
  188. net = self.provider.network.create(name=name)
  189. sg = self.provider.security.security_groups.create(
  190. name=name, description=name, network_id=net.id)
  191. with helpers.cleanup_action(lambda: self.cleanup_sg(sg, net)):
  192. rule = sg.add_rule(ip_protocol='tcp', from_port=1111, to_port=1111,
  193. cidr_ip='0.0.0.0/0')
  194. # attempting to add the same rule twice should succeed
  195. same_rule = sg.add_rule(ip_protocol='tcp', from_port=1111,
  196. to_port=1111, cidr_ip='0.0.0.0/0')
  197. self.assertTrue(
  198. rule == same_rule,
  199. "Expected rule {0} not found in security group: {0}".format(
  200. same_rule, sg.rules))
  201. def test_security_group_group_rule(self):
  202. """Test for proper creation of a security group rule."""
  203. name = 'cbtestsecuritygroupC-{0}'.format(uuid.uuid4())
  204. net = self.provider.network.create(name=name)
  205. sg = self.provider.security.security_groups.create(
  206. name=name, description=name, network_id=net.id)
  207. with helpers.cleanup_action(lambda: self.cleanup_sg(sg, net)):
  208. self.assertTrue(
  209. len(sg.rules) == 0,
  210. "Expected no security group group rule. Got {0}."
  211. .format(sg.rules))
  212. rule = sg.add_rule(src_group=sg, ip_protocol='tcp', from_port=0,
  213. to_port=65535)
  214. self.assertTrue(
  215. rule.group.name == name,
  216. "Expected security group rule name {0}. Got {1}."
  217. .format(name, rule.group.name))
  218. for r in sg.rules:
  219. r.delete()
  220. sg = self.provider.security.security_groups.get(sg.id) # update
  221. self.assertTrue(
  222. len(sg.rules) == 0,
  223. "Deleting SecurityGroupRule should delete it: {0}".format(
  224. sg.rules))
  225. sgl = self.provider.security.security_groups.list()
  226. found_sg = [g for g in sgl if g.name == name]
  227. self.assertTrue(
  228. len(found_sg) == 0,
  229. "Security group {0} should have been deleted but still exists."
  230. .format(name))