test_image_service.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. interval=self.get_test_wait_interval())
  28. with helpers.cleanup_action(lambda: cleanup_img(test_image)):
  29. test_image.wait_till_ready(
  30. interval=self.get_test_wait_interval())
  31. self.assertTrue(
  32. test_instance.id in repr(test_instance),
  33. "repr(obj) should contain the object id so that the object"
  34. " can be reconstructed, but does not.")
  35. self.assertTrue(
  36. test_image.description is None or isinstance(
  37. test_image.description, six.string_types),
  38. "Image description must be None or a string")
  39. images = self.provider.compute.images.list()
  40. found_images = [image for image in images
  41. if image.name == name]
  42. self.assertTrue(
  43. len(found_images) == 1,
  44. "List images does not return the expected image %s" %
  45. name)
  46. # check iteration
  47. found_images = [image for image in self.provider.compute.images
  48. if image.name == name]
  49. self.assertTrue(
  50. len(found_images) == 1,
  51. "Iter images does not return the expected image %s" %
  52. name)
  53. get_img = self.provider.compute.images.get(
  54. test_image.id)
  55. self.assertTrue(
  56. found_images[0] == get_img == test_image,
  57. "Objects returned by list: {0} and get: {1} are not as "
  58. " expected: {2}" .format(found_images[0].id,
  59. get_img.id,
  60. test_image.id))
  61. self.assertTrue(
  62. found_images[0].name ==
  63. get_img.name == test_image.name,
  64. "Names returned by list: {0} and get: {1} are not as "
  65. " expected: {2}" .format(found_images[0].name,
  66. get_img.name,
  67. test_image.name))
  68. # TODO: Images take a long time to deregister on EC2. Needs
  69. # investigation
  70. images = self.provider.compute.images.list()
  71. found_images = [image for image in images
  72. if image.name == name]
  73. self.assertTrue(
  74. len(found_images) == 0,
  75. "Image %s should have been deleted but still exists." %
  76. name)