test_image_service.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from unittest import skip
  2. import uuid
  3. import six
  4. from cloudbridge.cloud.interfaces import MachineImageState
  5. from test.helpers import ProviderTestBase
  6. import test.helpers as helpers
  7. class CloudImageServiceTestCase(ProviderTestBase):
  8. def __init__(self, methodName, provider):
  9. super(CloudImageServiceTestCase, self).__init__(
  10. methodName=methodName, provider=provider)
  11. @skip("Until Moto supports 'state' for DescribeSubnets filter")
  12. def test_create_and_list_image(self):
  13. """
  14. Create a new image and check whether that image can be listed.
  15. This covers waiting till the image is ready, checking that the image
  16. name is the expected one and whether list_images is functional.
  17. """
  18. instance_name = "CBImageTest-{0}-{1}".format(
  19. self.provider.name,
  20. uuid.uuid4())
  21. net, _ = helpers.create_test_network(self.provider, instance_name)
  22. test_instance = helpers.get_test_instance(self.provider, instance_name,
  23. network=net)
  24. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  25. test_instance, net)):
  26. name = "CBUnitTestListImg-{0}".format(uuid.uuid4())
  27. test_image = test_instance.create_image(name)
  28. def cleanup_img(img):
  29. img.delete()
  30. img.wait_for(
  31. [MachineImageState.UNKNOWN, MachineImageState.ERROR])
  32. with helpers.cleanup_action(lambda: cleanup_img(test_image)):
  33. test_image.wait_till_ready()
  34. self.assertTrue(
  35. test_instance.id in repr(test_instance),
  36. "repr(obj) should contain the object id so that the object"
  37. " can be reconstructed, but does not.")
  38. self.assertTrue(
  39. test_image.description is None or isinstance(
  40. test_image.description, six.string_types),
  41. "Image description must be None or a string")
  42. images = self.provider.compute.images.list()
  43. list_images = [image for image in images
  44. if image.name == name]
  45. self.assertTrue(
  46. len(list_images) == 1,
  47. "List images does not return the expected image %s" %
  48. name)
  49. # check iteration
  50. iter_images = [image for image in self.provider.compute.images
  51. if image.name == name]
  52. self.assertTrue(
  53. len(iter_images) == 1,
  54. "Iter images does not return the expected image %s" %
  55. name)
  56. # find image
  57. found_images = self.provider.compute.images.find(name=name)
  58. self.assertTrue(
  59. len(found_images) == 1,
  60. "Find images error: expected image %s but found: %s" %
  61. (name, found_images))
  62. # check non-existent find
  63. ne_images = self.provider.compute.images.find(
  64. name="non_existent")
  65. self.assertTrue(
  66. len(ne_images) == 0,
  67. "Find() for a non-existent image returned %s" %
  68. ne_images)
  69. get_img = self.provider.compute.images.get(
  70. test_image.id)
  71. self.assertTrue(
  72. found_images[0] == iter_images[0] == get_img == test_image,
  73. "Objects returned by list: {0} and get: {1} are not as "
  74. " expected: {2}" .format(found_images[0].id,
  75. get_img.id,
  76. test_image.id))
  77. self.assertTrue(
  78. list_images[0].name == found_images[0].name ==
  79. get_img.name == test_image.name,
  80. "Names returned by list: {0}, find: {1} and get: {2} are"
  81. " not as expected: {3}" .format(list_images[0].name,
  82. found_images[0].name,
  83. get_img.name,
  84. test_image.name))
  85. # TODO: Images take a long time to deregister on EC2. Needs
  86. # investigation
  87. images = self.provider.compute.images.list()
  88. found_images = [image for image in images
  89. if image.name == name]
  90. self.assertTrue(
  91. len(found_images) == 0,
  92. "Image %s should have been deleted but still exists." %
  93. name)