test_image_service.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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, subnet = helpers.create_test_network(self.provider, instance_name)
  24. test_instance = helpers.get_test_instance(self.provider, instance_name,
  25. subnet=subnet)
  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. # These checks won't work when >50 images are available
  45. # images = self.provider.compute.images.list()
  46. # list_images = [image for image in images
  47. # if image.name == name]
  48. # self.assertTrue(
  49. # len(list_images) == 1,
  50. # "List images does not return the expected image %s" %
  51. # name)
  52. # # check iteration
  53. # iter_images = [image for image in self.provider.compute.images
  54. # if image.name == name]
  55. # self.assertTrue(
  56. # len(iter_images) == 1,
  57. # "Iter images does not return the expected image %s" %
  58. # name)
  59. # find image
  60. found_images = self.provider.compute.images.find(name=name)
  61. self.assertTrue(
  62. len(found_images) == 1,
  63. "Find images error: expected image %s but found: %s" %
  64. (name, found_images))
  65. # check non-existent find
  66. ne_images = self.provider.compute.images.find(
  67. name="non_existent")
  68. self.assertTrue(
  69. len(ne_images) == 0,
  70. "Find() for a non-existent image returned %s" %
  71. ne_images)
  72. get_img = self.provider.compute.images.get(
  73. test_image.id)
  74. self.assertTrue(
  75. found_images[0] == get_img == test_image,
  76. "Objects returned by list: {0} and get: {1} are not as "
  77. " expected: {2}" .format(found_images[0].id,
  78. get_img.id,
  79. test_image.id))
  80. self.assertTrue(
  81. found_images[0].name == get_img.name == test_image.name,
  82. "Names returned by find: {0} and get: {1} are"
  83. " not as expected: {2}" .format(found_images[0].name,
  84. get_img.name,
  85. test_image.name))
  86. # TODO: Images take a long time to deregister on EC2. Needs
  87. # investigation
  88. images = self.provider.compute.images.list()
  89. found_images = [image for image in images
  90. if image.name == name]
  91. self.assertTrue(
  92. len(found_images) == 0,
  93. "Image %s should have been deleted but still exists." %
  94. name)