test_image_service.py 4.7 KB

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