test_security_service.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import itertools
  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(name=kp.name)
  15. ):
  16. found_kp = self.provider.security.key_pairs.find(name=name)
  17. self.assertTrue(
  18. found_kp == kp,
  19. "Find key pair did not return the expected key {0}."
  20. .format(name))
  21. recreated_kp = self.provider.security.key_pairs.create(name=name)
  22. self.assertTrue(
  23. recreated_kp == kp,
  24. "Recreating key pair did not return the expected key {0}."
  25. .format(name))
  26. kpl = self.provider.security.key_pairs.list()
  27. # check iteration
  28. iter_key_pairs = list(itertools.islice(
  29. self.provider.security.key_pairs,
  30. len(kpl)))
  31. self.assertListEqual(iter_key_pairs, kpl)
  32. found_kp = [k for k in kpl if k.name == name]
  33. self.assertTrue(
  34. len(found_kp) == 0,
  35. "Key pair {0} should have been deleted but still exists."
  36. .format(name))
  37. no_kp = self.provider.security.key_pairs.find(name='bogus_kp')
  38. self.assertTrue(
  39. no_kp is None,
  40. "Found a key pair {0} that should not exist?".format(no_kp))
  41. def test_key_pair(self):
  42. name = 'cbtestkeypairB-{0}'.format(uuid.uuid4())
  43. kp = self.provider.security.key_pairs.create(name=name)
  44. with helpers.cleanup_action(lambda: kp.delete()):
  45. kpl = self.provider.security.key_pairs.list()
  46. found_kp = [k for k in kpl if k.name == name]
  47. self.assertTrue(
  48. len(found_kp) == 1,
  49. "List key pairs did not return the expected key {0}."
  50. .format(name))
  51. self.assertTrue(
  52. kp.name in repr(kp),
  53. "repr(obj) should contain the object id so that the object"
  54. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  55. self.assertIsNotNone(
  56. kp.material,
  57. "KeyPair material is empty but it should not be.")
  58. self.assertTrue(
  59. kp == kp,
  60. "The same key pair should be equal to self.")
  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) == 0,
  65. "Key pair {0} should have been deleted but still exists."
  66. .format(name))
  67. def test_crud_security_group_service(self):
  68. name = 'cbtestsecuritygroupA-{0}'.format(uuid.uuid4())
  69. sg = self.provider.security.security_groups.create(
  70. name=name, description=name)
  71. with helpers.cleanup_action(
  72. lambda:
  73. self.provider.security.security_groups.delete(group_id=sg.id)
  74. ):
  75. self.assertEqual(name, sg.description)
  76. sgl = self.provider.security.security_groups.get(
  77. group_names=[
  78. sg.name])
  79. found_sg = [g for g in sgl if g.name == name]
  80. self.assertTrue(
  81. len(found_sg) == 1,
  82. "List security groups did not return the expected group {0}."
  83. .format(name))
  84. sgl = self.provider.security.security_groups.list()
  85. # check iteration
  86. iter_security_groups = list(itertools.islice(
  87. self.provider.security.security_groups,
  88. len(sgl)))
  89. self.assertListEqual(iter_security_groups, sgl)
  90. found_sg = [g for g in sgl if g.name == name]
  91. self.assertTrue(
  92. len(found_sg) == 0,
  93. "Security group {0} should have been deleted but still exists."
  94. .format(name))
  95. no_sg = self.provider.security.security_groups.get(
  96. group_ids=['bogus_sg'])
  97. self.assertTrue(
  98. len(no_sg) == 0,
  99. "Found a bogus security group?!?".format(no_sg))
  100. def test_security_group(self):
  101. """Test for proper creation of a security group."""
  102. name = 'cbtestsecuritygroupB-{0}'.format(uuid.uuid4())
  103. sg = self.provider.security.security_groups.create(
  104. name=name, description=name)
  105. with helpers.cleanup_action(lambda: sg.delete()):
  106. sg.add_rule(ip_protocol='tcp', from_port=1111, to_port=1111,
  107. cidr_ip='0.0.0.0/0')
  108. found_rules = [rule for rule in sg.rules if
  109. rule.cidr_ip == '0.0.0.0/0' and
  110. rule.ip_protocol == 'tcp' and
  111. rule.from_port == 1111 and
  112. rule.to_port == 1111]
  113. self.assertTrue(
  114. len(found_rules) == 1,
  115. "Expected rule not found in security group: {0}".format(name))
  116. object_keys = (
  117. sg.rules[0].ip_protocol,
  118. sg.rules[0].from_port,
  119. sg.rules[0].to_port)
  120. self.assertTrue(
  121. all(str(key) in repr(sg.rules[0]) for key in object_keys),
  122. "repr(obj) should contain ip_protocol, form_port and to_port"
  123. " so that the object can be reconstructed, but does not."
  124. " eval(repr(obj)) == obj")
  125. self.assertTrue(
  126. sg == sg,
  127. "The same security groups should be equal?")
  128. self.assertFalse(
  129. sg != sg,
  130. "The same security groups should still be equal?")
  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. def test_security_group_group_role(self):
  138. """Test for proper creation of a security group rule."""
  139. name = 'cbtestsecuritygroupC-{0}'.format(uuid.uuid4())
  140. sg = self.provider.security.security_groups.create(
  141. name=name, description=name)
  142. with helpers.cleanup_action(lambda: sg.delete()):
  143. self.assertTrue(
  144. len(sg.rules) == 0,
  145. "Expected no security group group rule. Got {0}."
  146. .format(sg.rules))
  147. sg.add_rule(src_group=sg)
  148. self.assertTrue(
  149. sg.rules[0].group.name == name,
  150. "Expected security group rule name {0}. Got {1}."
  151. .format(name, sg.rules[0].group.name))
  152. sgl = self.provider.security.security_groups.list()
  153. found_sg = [g for g in sgl if g.name == name]
  154. self.assertTrue(
  155. len(found_sg) == 0,
  156. "Security group {0} should have been deleted but still exists."
  157. .format(name))