test_image_service.py 4.5 KB

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