test_security_service.py 9.7 KB

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