test_image_service.py 4.9 KB

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