test_image_service.py 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. "Iter images does not return the expected image %s" %
  56. name)
  57. get_img = self.provider.compute.images.get(
  58. test_image.id)
  59. self.assertTrue(
  60. found_images[0] == iter_images[0] == get_img == test_image,
  61. "Objects returned by list: {0} and get: {1} are not as "
  62. " expected: {2}" .format(found_images[0].id,
  63. get_img.id,
  64. test_image.id))
  65. self.assertTrue(
  66. list_images[0].name == found_images[0].name ==
  67. get_img.name == test_image.name,
  68. "Names returned by list: {0}, find: {1} and get: {2} are"
  69. " not as expected: {3}" .format(list_images[0].name,
  70. found_images[0].name,
  71. get_img.name,
  72. test_image.name))
  73. # TODO: Images take a long time to deregister on EC2. Needs
  74. # investigation
  75. images = self.provider.compute.images.list()
  76. found_images = [image for image in images
  77. if image.name == name]
  78. self.assertTrue(
  79. len(found_images) == 0,
  80. "Image %s should have been deleted but still exists." %
  81. name)