test_security_service.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. recreated_kp = self.provider.security.key_pairs.create(name=name)
  43. self.assertTrue(
  44. recreated_kp == kp,
  45. "Recreating key pair did not return the expected key {0}."
  46. .format(name))
  47. kpl = self.provider.security.key_pairs.list()
  48. found_kp = [k for k in kpl if k.name == name]
  49. self.assertTrue(
  50. len(found_kp) == 0,
  51. "Key pair {0} should have been deleted but still exists."
  52. .format(name))
  53. no_kp = self.provider.security.key_pairs.find(name='bogus_kp')
  54. self.assertFalse(
  55. no_kp,
  56. "Found a key pair {0} that should not exist?".format(no_kp))
  57. def test_key_pair(self):
  58. name = 'cbtestkeypairB-{0}'.format(uuid.uuid4())
  59. kp = self.provider.security.key_pairs.create(name=name)
  60. with helpers.cleanup_action(lambda: kp.delete()):
  61. kpl = self.provider.security.key_pairs.list()
  62. found_kp = [k for k in kpl if k.name == name]
  63. self.assertTrue(
  64. len(found_kp) == 1,
  65. "List key pairs did not return the expected key {0}."
  66. .format(name))
  67. self.assertTrue(
  68. kp.id in repr(kp),
  69. "repr(obj) should contain the object id so that the object"
  70. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  71. self.assertIsNotNone(
  72. kp.material,
  73. "KeyPair material is empty but it should not be.")
  74. self.assertTrue(
  75. kp == kp,
  76. "The same key pair should be equal to self.")
  77. json_repr = json.dumps(
  78. {"material": kp.material, "id": name, "name": name},
  79. sort_keys=True)
  80. self.assertEqual(
  81. kp.to_json(), json_repr,
  82. "JSON key pair representation {0} does not match expected {1}"
  83. .format(kp.to_json(), json_repr))
  84. kpl = self.provider.security.key_pairs.list()
  85. found_kp = [k for k in kpl if k.name == name]
  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 = 'cbtestsecuritygroupA-{0}'.format(uuid.uuid4())
  92. sg = self.provider.security.security_groups.create(
  93. name=name, description=name)
  94. with helpers.cleanup_action(
  95. lambda:
  96. self.provider.security.security_groups.delete(group_id=sg.id)
  97. ):
  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. sg = self.provider.security.security_groups.create(
  143. name=name, description=name)
  144. with helpers.cleanup_action(lambda: sg.delete()):
  145. rule = sg.add_rule(ip_protocol='tcp', from_port=1111, to_port=1111,
  146. cidr_ip='0.0.0.0/0')
  147. found_rule = sg.get_rule(ip_protocol='tcp', from_port=1111,
  148. to_port=1111, cidr_ip='0.0.0.0/0')
  149. self.assertTrue(
  150. rule == found_rule,
  151. "Expected rule {0} not found in security group: {0}".format(
  152. rule, sg.rules))
  153. object_keys = (
  154. sg.rules[0].ip_protocol,
  155. sg.rules[0].from_port,
  156. sg.rules[0].to_port)
  157. self.assertTrue(
  158. all(str(key) in repr(sg.rules[0]) for key in object_keys),
  159. "repr(obj) should contain ip_protocol, form_port, and to_port"
  160. " so that the object can be reconstructed, but does not:"
  161. " {0}; {1}".format(sg.rules[0], object_keys))
  162. self.assertTrue(
  163. sg == sg,
  164. "The same security groups should be equal?")
  165. self.assertFalse(
  166. sg != sg,
  167. "The same security groups should still be equal?")
  168. json_repr = json.dumps(
  169. {"description": name, "name": name, "id": sg.id, "rules":
  170. [{"from_port": 1111, "group": "", "cidr_ip": "0.0.0.0/0",
  171. "parent": sg.id, "to_port": 1111, "ip_protocol": "tcp",
  172. "id": sg.rules[0].id}]},
  173. sort_keys=True)
  174. self.assertTrue(
  175. sg.to_json() == json_repr,
  176. "JSON sec group representation {0} does not match expected {1}"
  177. .format(sg.to_json(), json_repr))
  178. sgl = self.provider.security.security_groups.list()
  179. found_sg = [g for g in sgl if g.name == name]
  180. self.assertTrue(
  181. len(found_sg) == 0,
  182. "Security group {0} should have been deleted but still exists."
  183. .format(name))
  184. def test_security_group_group_role(self):
  185. """Test for proper creation of a security group rule."""
  186. name = 'cbtestsecuritygroupC-{0}'.format(uuid.uuid4())
  187. sg = self.provider.security.security_groups.create(
  188. name=name, description=name)
  189. with helpers.cleanup_action(lambda: sg.delete()):
  190. self.assertTrue(
  191. len(sg.rules) == 0,
  192. "Expected no security group group rule. Got {0}."
  193. .format(sg.rules))
  194. rule = sg.add_rule(src_group=sg)
  195. self.assertTrue(
  196. rule.group.name == name,
  197. "Expected security group rule name {0}. Got {1}."
  198. .format(name, rule.group.name))
  199. for r in sg.rules:
  200. r.delete()
  201. sg = self.provider.security.security_groups.get(sg.id) # update
  202. self.assertTrue(
  203. len(sg.rules) == 0,
  204. "Deleting SecurityGroupRule should delete it: {0}".format(
  205. sg.rules))
  206. sgl = self.provider.security.security_groups.list()
  207. found_sg = [g for g in sgl if g.name == name]
  208. self.assertTrue(
  209. len(found_sg) == 0,
  210. "Security group {0} should have been deleted but still exists."
  211. .format(name))