test_image_service.py 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from cloudbridge.cloud.interfaces import MachineImageState
  2. from cloudbridge.cloud.interfaces.resources import Instance, MachineImage
  3. from test import helpers
  4. from test.helpers import ProviderTestBase
  5. from test.helpers import standard_interface_tests as sit
  6. class CloudImageServiceTestCase(ProviderTestBase):
  7. _multiprocess_can_split_ = True
  8. @helpers.skipIfNoService(['compute.images', 'networking.networks',
  9. 'compute.instances'])
  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. label is the expected one and whether list_images is functional.
  15. """
  16. instance_label = "cb-crudimage-{0}".format(helpers.get_uuid())
  17. img_inst_label = "cb-crudimage-{0}".format(helpers.get_uuid())
  18. # Declare these variables and late binding will allow
  19. # the cleanup method access to the most current values
  20. test_instance = None
  21. subnet = None
  22. def create_img(label):
  23. return test_instance.create_image(label=label)
  24. def cleanup_img(img):
  25. if img:
  26. img.delete()
  27. img.wait_for(
  28. [MachineImageState.UNKNOWN, MachineImageState.ERROR])
  29. img.refresh()
  30. self.assertTrue(
  31. img.state == MachineImageState.UNKNOWN,
  32. "MachineImage.state must be unknown when refreshing after "
  33. "a delete but got %s"
  34. % img.state)
  35. def extra_tests(img):
  36. # check image size
  37. img.refresh()
  38. self.assertGreater(img.min_disk, 0, "Minimum disk"
  39. " size required by image is invalid")
  40. create_instance_from_image(img)
  41. def create_instance_from_image(img):
  42. img_instance = None
  43. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  44. img_instance)):
  45. img_instance = self.provider.compute.instances.create(
  46. img_inst_label, img,
  47. helpers.get_provider_test_data(self.provider, 'vm_type'),
  48. subnet=subnet,
  49. zone=helpers.get_provider_test_data(
  50. self.provider, 'placement'))
  51. img_instance.wait_till_ready()
  52. self.assertIsInstance(img_instance, Instance)
  53. self.assertEqual(
  54. img_instance.label, img_inst_label,
  55. "Instance label {0} is not equal to the expected label"
  56. " {1}".format(img_instance.label, img_inst_label))
  57. image_id = img.id
  58. self.assertEqual(img_instance.image_id, image_id,
  59. "Image id {0} is not equal to the expected id"
  60. " {1}".format(img_instance.image_id,
  61. image_id))
  62. self.assertIsInstance(img_instance.public_ips, list)
  63. if img_instance.public_ips:
  64. self.assertTrue(
  65. img_instance.public_ips[0],
  66. "public ip should contain a"
  67. " valid value if a list of public_ips exist")
  68. self.assertIsInstance(img_instance.private_ips, list)
  69. self.assertTrue(img_instance.private_ips[0],
  70. "private ip should"
  71. " contain a valid value")
  72. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  73. test_instance)):
  74. subnet = helpers.get_or_create_default_subnet(
  75. self.provider)
  76. test_instance = helpers.get_test_instance(
  77. self.provider, instance_label, subnet=subnet)
  78. sit.check_crud(self, self.provider.compute.images, MachineImage,
  79. "cb-listimg", create_img, cleanup_img,
  80. extra_test_func=extra_tests)