test_image_service.py 3.9 KB

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