test_security_service.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. sgl = self.provider.security.security_groups.get(
  76. group_names=[
  77. sg.name])
  78. found_sg = [g for g in sgl if g.name == name]
  79. self.assertTrue(
  80. len(found_sg) == 1,
  81. "List security groups did not return the expected group {0}."
  82. .format(name))
  83. sgl = self.provider.security.security_groups.list()
  84. # check iteration
  85. iter_security_groups = list(itertools.islice(
  86. self.provider.security.security_groups,
  87. len(sgl)))
  88. self.assertListEqual(iter_security_groups, sgl)
  89. found_sg = [g for g in sgl if g.name == name]
  90. self.assertTrue(
  91. len(found_sg) == 0,
  92. "Security group {0} should have been deleted but still exists."
  93. .format(name))
  94. no_sg = self.provider.security.security_groups.get(
  95. group_ids=['bogus_sg'])
  96. self.assertTrue(
  97. len(no_sg) == 0,
  98. "Found a bogus security group?!?".format(no_sg))
  99. def test_security_group(self):
  100. """Test for proper creation of a security group."""
  101. name = 'cbtestsecuritygroupB-{0}'.format(uuid.uuid4())
  102. sg = self.provider.security.security_groups.create(
  103. name=name, description=name)
  104. with helpers.cleanup_action(lambda: sg.delete()):
  105. sg.add_rule(ip_protocol='tcp', from_port=1111, to_port=1111,
  106. cidr_ip='0.0.0.0/0')
  107. found_rules = [rule for rule in sg.rules if
  108. rule.cidr_ip == '0.0.0.0/0' and
  109. rule.ip_protocol == 'tcp' and
  110. rule.from_port == 1111 and
  111. rule.to_port == 1111]
  112. self.assertTrue(
  113. len(found_rules) == 1,
  114. "Expected rule not found in security group: {0}".format(name))
  115. object_keys = (
  116. sg.rules[0].ip_protocol,
  117. sg.rules[0].from_port,
  118. sg.rules[0].to_port)
  119. self.assertTrue(
  120. all(str(key) in repr(sg.rules[0]) for key in object_keys),
  121. "repr(obj) should contain ip_protocol, form_port and to_port"
  122. " so that the object can be reconstructed, but does not."
  123. " eval(repr(obj)) == obj")
  124. self.assertTrue(
  125. sg == sg,
  126. "The same security groups should be equal?")
  127. self.assertFalse(
  128. sg != sg,
  129. "The same security groups should still be equal?")
  130. sgl = self.provider.security.security_groups.list()
  131. found_sg = [g for g in sgl if g.name == name]
  132. self.assertTrue(
  133. len(found_sg) == 0,
  134. "Security group {0} should have been deleted but still exists."
  135. .format(name))
  136. def test_security_group_group_role(self):
  137. """Test for proper creation of a security group rule."""
  138. name = 'cbtestsecuritygroupC-{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. self.assertTrue(
  143. len(sg.rules) == 0,
  144. "Expected no security group group rule. Got {0}."
  145. .format(sg.rules))
  146. sg.add_rule(src_group=sg)
  147. self.assertTrue(
  148. sg.rules[0].group.name == name,
  149. "Expected security group rule name {0}. Got {1}."
  150. .format(name, sg.rules[0].group.name))
  151. sgl = self.provider.security.security_groups.list()
  152. found_sg = [g for g in sgl if g.name == name]
  153. self.assertTrue(
  154. len(found_sg) == 0,
  155. "Security group {0} should have been deleted but still exists."
  156. .format(name))