from cloudbridge.cloud.interfaces import MachineImageState
from cloudbridge.cloud.interfaces.resources import MachineImage

from test import helpers
from test.helpers import ProviderTestBase
from test.helpers import standard_interface_tests as sit


class CloudImageServiceTestCase(ProviderTestBase):

    _multiprocess_can_split_ = True

    @helpers.skipIfNoService(['compute.images', 'networking.networks',
                              'compute.instances'])
    def test_create_and_list_image(self):
        """
        Create a new image and check whether that image can be listed.
        This covers waiting till the image is ready, checking that the image
        label is the expected one and whether list_images is functional.
        """
        instance_label = "cb-crudimage-{0}".format(helpers.get_uuid())

        # Declare these variables and late binding will allow
        # the cleanup method access to the most current values
        test_instance = None
        net = None
        subnet = None

        def create_img(label):
            return test_instance.create_image(label=label)

        def cleanup_img(img):
            if img:
                img.delete()
                img.wait_for(
                    [MachineImageState.UNKNOWN, MachineImageState.ERROR])

        def extra_tests(img):
            # check image size
            img.refresh()
            self.assertGreater(img.min_disk, 0, "Minimum disk"
                               " size required by image is invalid")

        with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
                test_instance, net)):
            net, subnet = helpers.create_test_network(
                self.provider, instance_label)
            test_instance = helpers.get_test_instance(
                self.provider, instance_label, subnet=subnet)
            sit.check_crud(self, self.provider.compute.images, MachineImage,
                           "cb-listimg", create_img, cleanup_img,
                           extra_test_func=extra_tests)
