test_security_service.py 9.5 KB

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