test_security_service.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import json
  2. from test.helpers import ProviderTestBase
  3. import time
  4. import uuid
  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 = 'cbtestkeypair-a'
  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.id == kp.id]
  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.id == kp.id]
  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=kp.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(kp.id)
  39. self.assertTrue(
  40. get_kp == kp,
  41. "Get key pair did not return the expected key {0}."
  42. .format(name))
  43. # FIXME: This test doesn't work if the server generates the id
  44. # and does not care about name uniqueness (e.g. azure)
  45. # recreated_kp = self.provider.security.key_pairs.create(name=name)
  46. # self.assertTrue(
  47. # recreated_kp == kp,
  48. # "Recreating key pair did not return the expected key {0}."
  49. # .format(name))
  50. kpl = self.provider.security.key_pairs.list()
  51. found_kp = [k for k in kpl if k.id == kp.id]
  52. self.assertTrue(
  53. len(found_kp) == 0,
  54. "Key pair {0} should have been deleted but still exists."
  55. .format(name))
  56. no_kp = self.provider.security.key_pairs.find(name='bogus_kp')
  57. self.assertFalse(
  58. no_kp,
  59. "Found a key pair {0} that should not exist?".format(no_kp))
  60. def test_key_pair(self):
  61. name = 'cbtestkeypair-b'
  62. kp = self.provider.security.key_pairs.create(name=name)
  63. with helpers.cleanup_action(lambda: kp.delete()):
  64. kpl = self.provider.security.key_pairs.list()
  65. found_kp = [k for k in kpl if k.id == kp.id]
  66. self.assertTrue(
  67. len(found_kp) == 1,
  68. "List key pairs did not return the expected key {0}."
  69. .format(name))
  70. self.assertTrue(
  71. kp.id in repr(kp),
  72. "repr(obj) should contain the object id so that the object"
  73. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  74. self.assertIsNotNone(
  75. kp.material,
  76. "KeyPair material is empty but it should not be.")
  77. self.assertTrue(
  78. kp == kp,
  79. "The same key pair should be equal to self.")
  80. # check json deserialization
  81. self.assertTrue(json.loads(kp.to_json()),
  82. "to_json must yield a valid json string: {0}"
  83. .format(kp.to_json()))
  84. kpl = self.provider.security.key_pairs.list()
  85. found_kp = [k for k in kpl if k.id == kp.id]
  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 = 'cbtestsecuritygroup-a'
  92. sg = self.provider.security.security_groups.create(
  93. name=name, description=name)
  94. #Empty security groups don't exist in GCE. Let's add a dummy rule.
  95. sg.add_rule(ip_protocol='tcp')
  96. with helpers.cleanup_action(
  97. lambda:
  98. self.provider.security.security_groups.delete(group_id=sg.id)
  99. ):
  100. self.assertEqual(name, sg.description)
  101. # test list method
  102. sgl = self.provider.security.security_groups.list()
  103. found_sgl = [i for i in sgl if i.name == name]
  104. self.assertTrue(
  105. len(found_sgl) == 1,
  106. "List security groups does not return the expected group %s" %
  107. name)
  108. # check iteration
  109. found_sgl = [i for i in self.provider.security.security_groups
  110. if i.name == name]
  111. self.assertTrue(
  112. len(found_sgl) == 1,
  113. "Iter security groups does not return the expected group %s" %
  114. name)
  115. # check find
  116. find_sg = self.provider.security.security_groups.find(name=sg.name)
  117. self.assertTrue(
  118. len(find_sg) == 1,
  119. "List security groups returned {0} when expected was: {1}."
  120. .format(find_sg, sg.name))
  121. # check get
  122. get_sg = self.provider.security.security_groups.get(sg.id)
  123. self.assertTrue(
  124. get_sg == sg,
  125. "Get SecurityGroup did not return the expected key {0}."
  126. .format(name))
  127. self.assertTrue(
  128. sg.id in repr(sg),
  129. "repr(obj) should contain the object id so that the object"
  130. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  131. sgl = self.provider.security.security_groups.list()
  132. found_sg = [g for g in sgl if g.name == name]
  133. self.assertTrue(
  134. len(found_sg) == 0,
  135. "Security group {0} should have been deleted but still exists."
  136. .format(name))
  137. no_sg = self.provider.security.security_groups.find(name='bogus_sg')
  138. self.assertTrue(
  139. len(no_sg) == 0,
  140. "Found a bogus security group?!?".format(no_sg))
  141. def test_security_group(self):
  142. """Test for proper creation of a security group."""
  143. name = 'cbtestsecuritygroup-b'
  144. sg = self.provider.security.security_groups.create(
  145. name=name, description=name)
  146. with helpers.cleanup_action(lambda: sg.delete()):
  147. rule = sg.add_rule(ip_protocol='tcp', from_port=1111, to_port=1111,
  148. cidr_ip='0.0.0.0/0')
  149. found_rule = sg.get_rule(ip_protocol='tcp', from_port=1111,
  150. to_port=1111, cidr_ip='0.0.0.0/0')
  151. self.assertTrue(
  152. rule == found_rule,
  153. "Expected rule {0} not found in security group: {0}".format(
  154. rule, sg.rules))
  155. object_keys = (
  156. sg.rules[0].ip_protocol,
  157. sg.rules[0].from_port,
  158. sg.rules[0].to_port)
  159. self.assertTrue(
  160. all(str(key) in repr(sg.rules[0]) for key in object_keys),
  161. "repr(obj) should contain ip_protocol, form_port, and to_port"
  162. " so that the object can be reconstructed, but does not:"
  163. " {0}; {1}".format(sg.rules[0], object_keys))
  164. self.assertTrue(
  165. sg == sg,
  166. "The same security groups should be equal?")
  167. self.assertFalse(
  168. sg != sg,
  169. "The same security groups should still be equal?")
  170. json_repr = json.dumps(
  171. {"description": name, "name": name, "id": sg.id, "rules":
  172. [{"from_port": 1111, "group": "", "cidr_ip": "0.0.0.0/0",
  173. "parent": sg.id, "to_port": 1111, "ip_protocol": "tcp",
  174. "id": sg.rules[0].id}]},
  175. sort_keys=True)
  176. self.assertTrue(
  177. sg.to_json() == json_repr,
  178. "JSON sec group representation {0} does not match expected {1}"
  179. .format(sg.to_json(), json_repr))
  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))
  186. def test_security_group_group_role(self):
  187. """Test for proper creation of a security group rule."""
  188. name = 'cbtestsecuritygroup-c'
  189. sg = self.provider.security.security_groups.create(
  190. name=name, description=name)
  191. with helpers.cleanup_action(
  192. lambda: None if sg is None else 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(ip_protocol='tcp', 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. for r in sg.rules:
  203. r.delete()
  204. sg = self.provider.security.security_groups.get(sg.id) # update
  205. self.assertTrue(
  206. sg is None or len(sg.rules) == 0,
  207. "Deleting SecurityGroupRule should delete it: {0}".format(
  208. [] if sg is None else sg.rules))
  209. sgl = self.provider.security.security_groups.list()
  210. found_sg = [g for g in sgl if g.name == name]
  211. self.assertTrue(
  212. len(found_sg) == 0,
  213. "Security group {0} should have been deleted but still exists."
  214. .format(name))