test_image_service.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. 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. net, _ = helpers.create_test_network(self.provider, instance_name)
  20. test_instance = helpers.get_test_instance(self.provider, instance_name,
  21. network=net)
  22. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  23. test_instance, net)):
  24. name = "CBUnitTestListImg-{0}".format(uuid.uuid4())
  25. test_image = test_instance.create_image(name)
  26. def cleanup_img(img):
  27. img.delete()
  28. img.wait_for(
  29. [MachineImageState.UNKNOWN, MachineImageState.ERROR])
  30. with helpers.cleanup_action(lambda: cleanup_img(test_image)):
  31. test_image.wait_till_ready()
  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. # check iteration
  41. # iter_images = [image for image in self.provider.compute.images
  42. # if image.name == name]
  43. # self.assertTrue(
  44. # len(iter_images) == 1,
  45. # "Iter images does not return the expected image %s" %
  46. # name)
  47. # find image
  48. found_images = self.provider.compute.images.find(name)
  49. self.assertTrue(
  50. len(found_images) == 1,
  51. "Find images error: expected image %s but found: %s" %
  52. (name, found_images))
  53. # check non-existent find
  54. ne_images = self.provider.compute.images.find(
  55. name="non_existent")
  56. self.assertTrue(
  57. len(ne_images) == 0,
  58. "Find() for a non-existent image returned %s" %
  59. ne_images)
  60. get_img = self.provider.compute.images.get(
  61. test_image.id)
  62. self.assertTrue(
  63. found_images[0] == get_img == test_image,
  64. "Objects returned by list: {0} and get: {1} are not as "
  65. " expected: {2}" .format(found_images[0].id,
  66. get_img.id,
  67. test_image.id))
  68. # It's currently not possible to "delete" EC2 images, only
  69. # to deallocate them. Images remain in the list but attempting
  70. # to access properties (name, description, state) will results
  71. # in an AttributeError which gets passed back as "None" to us.
  72. # found_images = self.provider.compute.images.find(name)
  73. # self.assertTrue(
  74. # len(found_images) == 0 or found_images[0].name is None,
  75. # "Image %s should have been deleted but still exists." %
  76. # name)