test_image_service.py 5.1 KB

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