test_security_service.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. # 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.id == kp.id]
  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 = 'cbtestkeypair-b'
  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.id == kp.id]
  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. # check json deserialization
  77. self.assertTrue(json.loads(kp.to_json()),
  78. "to_json must yield a valid json string: {0}"
  79. .format(kp.to_json()))
  80. kpl = self.provider.security.key_pairs.list()
  81. found_kp = [k for k in kpl if k.id == kp.id]
  82. self.assertTrue(
  83. len(found_kp) == 0,
  84. "Key pair {0} should have been deleted but still exists."
  85. .format(name))
  86. def test_crud_security_group_service(self):
  87. name = 'cbtestsecuritygroup-a'
  88. sg = self.provider.security.security_groups.create(
  89. name=name, description=name)
  90. #Empty security groups don't exist in GCE. Let's add a dummy rule.
  91. sg.add_rule(ip_protocol='tcp', cidr_ip='0.0.0.0/0')
  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 = 'cbtestsecuritygroup-b'
  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: {1}".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: {1}".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 = 'cbtestsecuritygroup-c'
  200. sg = self.provider.security.security_groups.create(
  201. name=name, description=name)
  202. with helpers.cleanup_action(
  203. lambda: None if sg is None else sg.delete()):
  204. self.assertTrue(
  205. len(sg.rules) == 0,
  206. "Expected no security group group rule. Got {0}."
  207. .format(sg.rules))
  208. rule = sg.add_rule(ip_protocol='tcp', src_group=sg)
  209. self.assertTrue(
  210. rule.group.name == name,
  211. "Expected security group rule name {0}. Got {1}."
  212. .format(name, rule.group.name))
  213. for r in sg.rules:
  214. r.delete()
  215. sg = self.provider.security.security_groups.get(sg.id) # update
  216. self.assertTrue(
  217. sg is None or len(sg.rules) == 0,
  218. "Deleting SecurityGroupRule should delete it: {0}".format(
  219. [] if sg is None else sg.rules))
  220. sgl = self.provider.security.security_groups.list()
  221. found_sg = [g for g in sgl if g.name == name]
  222. self.assertTrue(
  223. len(found_sg) == 0,
  224. "Security group {0} should have been deleted but still exists."
  225. .format(name))