test_image_service.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. from unittest import skip
  2. import uuid
  3. import six
  4. from cloudbridge.cloud.interfaces import MachineImageState
  5. from test.helpers import ProviderTestBase
  6. import test.helpers as helpers
  7. class CloudImageServiceTestCase(ProviderTestBase):
  8. def __init__(self, methodName, provider):
  9. super(CloudImageServiceTestCase, self).__init__(
  10. methodName=methodName, provider=provider)
  11. @helpers.skipIfNoService(['compute.images', 'network',
  12. 'compute.instances'])
  13. @skip("Until Moto supports 'state' for DescribeSubnets filter")
  14. def test_create_and_list_image(self):
  15. """
  16. Create a new image and check whether that image can be listed.
  17. This covers waiting till the image is ready, checking that the image
  18. name is the expected one and whether list_images is functional.
  19. """
  20. instance_name = "CBImageTest-{0}-{1}".format(
  21. self.provider.name,
  22. uuid.uuid4())
  23. net, _ = helpers.create_test_network(self.provider, instance_name)
  24. test_instance = helpers.get_test_instance(self.provider, instance_name,
  25. network=net)
  26. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  27. test_instance, net)):
  28. name = "CBUnitTestListImg-{0}".format(uuid.uuid4())
  29. test_image = test_instance.create_image(name)
  30. def cleanup_img(img):
  31. img.delete()
  32. img.wait_for(
  33. [MachineImageState.UNKNOWN, MachineImageState.ERROR])
  34. with helpers.cleanup_action(lambda: cleanup_img(test_image)):
  35. test_image.wait_till_ready()
  36. self.assertTrue(
  37. test_instance.id in repr(test_instance),
  38. "repr(obj) should contain the object id so that the object"
  39. " can be reconstructed, but does not.")
  40. self.assertTrue(
  41. test_image.description is None or isinstance(
  42. test_image.description, six.string_types),
  43. "Image description must be None or a string")
  44. images = self.provider.compute.images.list()
  45. list_images = [image for image in images
  46. if image.name == name]
  47. self.assertTrue(
  48. len(list_images) == 1,
  49. "List images does not return the expected image %s" %
  50. name)
  51. # check iteration
  52. iter_images = [image for image in self.provider.compute.images
  53. if image.name == name]
  54. self.assertTrue(
  55. len(iter_images) == 1,
  56. "Iter images does not return the expected image %s" %
  57. name)
  58. # find image
  59. found_images = self.provider.compute.images.find(name=name)
  60. self.assertTrue(
  61. len(found_images) == 1,
  62. "Find images error: expected image %s but found: %s" %
  63. (name, found_images))
  64. # check non-existent find
  65. ne_images = self.provider.compute.images.find(
  66. name="non_existent")
  67. self.assertTrue(
  68. len(ne_images) == 0,
  69. "Find() for a non-existent image returned %s" %
  70. ne_images)
  71. get_img = self.provider.compute.images.get(
  72. test_image.id)
  73. self.assertTrue(
  74. found_images[0] == iter_images[0] == get_img == test_image,
  75. "Objects returned by list: {0} and get: {1} are not as "
  76. " expected: {2}" .format(found_images[0].id,
  77. get_img.id,
  78. test_image.id))
  79. self.assertTrue(
  80. list_images[0].name == found_images[0].name ==
  81. get_img.name == test_image.name,
  82. "Names returned by list: {0}, find: {1} and get: {2} are"
  83. " not as expected: {3}" .format(list_images[0].name,
  84. found_images[0].name,
  85. get_img.name,
  86. test_image.name))
  87. # TODO: Images take a long time to deregister on EC2. Needs
  88. # investigation
  89. images = self.provider.compute.images.list()
  90. found_images = [image for image in images
  91. if image.name == name]
  92. self.assertTrue(
  93. len(found_images) == 0,
  94. "Image %s should have been deleted but still exists." %
  95. name)