test_provider_image_service.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 ProviderImageServiceTestCase(ProviderTestBase):
  7. def __init__(self, methodName, provider):
  8. super(ProviderImageServiceTestCase, 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],
  27. terminal_states=[MachineImageState.ERROR],
  28. interval=self.get_test_wait_interval())
  29. with helpers.cleanup_action(lambda: cleanup_img(test_image)):
  30. test_image.wait_till_ready(
  31. interval=self.get_test_wait_interval())
  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. images = self.provider.compute.images.list()
  41. found_images = [image for image in images
  42. if image.name == name]
  43. self.assertTrue(
  44. len(found_images) == 1,
  45. "List images does not return the expected image %s" %
  46. name)
  47. get_img = self.provider.compute.images.get(
  48. test_image.id)
  49. self.assertTrue(
  50. found_images[0].id == get_img.id == test_image.id,
  51. "Ids returned by list: {0} and get: {1} are not as "
  52. " expected: {2}" .format(found_images[0].id,
  53. get_img.id,
  54. test_image.id))
  55. self.assertTrue(
  56. found_images[0].name ==
  57. get_img.name == test_image.name,
  58. "Names returned by list: {0} and get: {1} are not as "
  59. " expected: {2}" .format(found_images[0].name,
  60. get_img.name,
  61. test_image.name))
  62. # TODO: Images take a long time to deregister on EC2. Needs
  63. # investigation
  64. images = self.provider.compute.images.list()
  65. found_images = [image for image in images
  66. if image.name == name]
  67. self.assertTrue(
  68. len(found_images) == 0,
  69. "Image %s should have been deleted but still exists." %
  70. name)