test_image_service.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import uuid
  2. import six
  3. from cloudbridge.cloud.interfaces import MachineImageState
  4. from test.helpers import ProviderTestBase
  5. import test.helpers as helpers
  6. class CloudImageServiceTestCase(ProviderTestBase):
  7. def __init__(self, methodName, provider):
  8. super(CloudImageServiceTestCase, self).__init__(
  9. methodName=methodName, provider=provider)
  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 = helpers.get_test_instance(self.provider, instance_name)
  20. with helpers.cleanup_action(lambda: test_instance.terminate()):
  21. name = "CBUnitTestListImg-{0}".format(uuid.uuid4())
  22. test_image = test_instance.create_image(name)
  23. def cleanup_img(img):
  24. img.delete()
  25. img.wait_for(
  26. [MachineImageState.UNKNOWN, MachineImageState.ERROR])
  27. with helpers.cleanup_action(lambda: cleanup_img(test_image)):
  28. test_image.wait_till_ready()
  29. self.assertTrue(
  30. test_instance.id in repr(test_instance),
  31. "repr(obj) should contain the object id so that the object"
  32. " can be reconstructed, but does not.")
  33. self.assertTrue(
  34. test_image.description is None or isinstance(
  35. test_image.description, six.string_types),
  36. "Image description must be None or a string")
  37. images = self.provider.compute.images.list()
  38. list_images = [image for image in images
  39. if image.name == name]
  40. self.assertTrue(
  41. len(list_images) == 1,
  42. "List images does not return the expected image %s" %
  43. name)
  44. # check iteration
  45. iter_images = [image for image in self.provider.compute.images
  46. if image.name == name]
  47. self.assertTrue(
  48. len(iter_images) == 1,
  49. "Iter images does not return the expected image %s" %
  50. name)
  51. # find image
  52. found_images = self.provider.compute.images.find(name=name)
  53. self.assertTrue(
  54. len(found_images) == 1,
  55. "Find images error: expected image %s but found: %s" %
  56. (name, found_images))
  57. # check non-existent find
  58. ne_images = self.provider.compute.images.find(
  59. name="non_existent")
  60. self.assertTrue(
  61. len(ne_images) == 0,
  62. "Find() for a non-existent image returned %s" %
  63. ne_images)
  64. get_img = self.provider.compute.images.get(
  65. test_image.id)
  66. self.assertTrue(
  67. found_images[0] == iter_images[0] == get_img == test_image,
  68. "Objects returned by list: {0} and get: {1} are not as "
  69. " expected: {2}" .format(found_images[0].id,
  70. get_img.id,
  71. test_image.id))
  72. self.assertTrue(
  73. list_images[0].name == found_images[0].name ==
  74. get_img.name == test_image.name,
  75. "Names returned by list: {0}, find: {1} and get: {2} are"
  76. " not as expected: {3}" .format(list_images[0].name,
  77. found_images[0].name,
  78. get_img.name,
  79. test_image.name))
  80. # TODO: Images take a long time to deregister on EC2. Needs
  81. # investigation
  82. images = self.provider.compute.images.list()
  83. found_images = [image for image in images
  84. if image.name == name]
  85. self.assertTrue(
  86. len(found_images) == 0,
  87. "Image %s should have been deleted but still exists." %
  88. name)