test_image_service.py 5.0 KB

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