def setUp(self):
        self.img_kernel = GlanceSyncImage('kernel1', '000A', 'Valladolid',
                                          'own0', True, '00', 10000, 'active',
                                          {})
        self.img_ramdisk = GlanceSyncImage('ramdisk1', '000B', 'Valladolid',
                                           'own0', True, '00', 10000, 'active',
                                           {})
        self.reg_image = GlanceSyncImage('img1', '000C', 'Burgos', 'own0',
                                         True, '00', 10000, 'active', {
                                             'kernel_id': '001',
                                             'ramdisk_id': '002',
                                             'type': 'base'
                                         })
        self.master_image = GlanceSyncImage('img1', '0003', 'Valladolid',
                                            'own0', True, '00', 10000,
                                            'active', {
                                                'kernel_id': 'kernel1',
                                                'ramdisk_id': 'ramdisk1',
                                                'type': 'base'
                                            })

        self.dict_reg = {
            'kernel1': self.img_kernel,
            'ramdisk1': self.img_ramdisk,
            'img1': self.reg_image
        }
        self.logbuffer = StringIO.StringIO()
        handler = logging.StreamHandler(self.logbuffer)
        logging.getLogger('GlanceSync-Client').addHandler(handler)
 def setUp(self):
     self.name = name = "image1"
     self.id1 = id = "0001"
     self.region = region = "Valladolid"
     self.owner = owner = "00000001"
     self.is_public = is_public = True
     self.checksum = checksum = "cheksum00001"
     self.size = size = 1500000
     self.status = status = "active"
     self.props = props = {"p1": "v1", "p2": "v2", "p3": "v3"}
     self.image1 = GlanceSyncImage(name, id, region, owner, is_public, checksum, size, status, props)
     self.name2 = name = "image2"
     self.id2 = id = "0002"
     self.checksum2 = checksum = "cheksum00002"
     self.props2 = props = {"p1": "v1", "p2": "v2bis"}
     self.image2 = GlanceSyncImage(name, id, region, owner, is_public, checksum, size, status, props)
     self.id3 = id = "0003"
     self.size3 = size = 100000
     self.props3 = props = {}
     self.checksum3 = checksum = "cheksum00003"
     self.image3 = GlanceSyncImage(name, id, region, owner, is_public, checksum, size, status, props)
     self.id4 = id = "0004"
     self.props4 = props = {}
     self.checksum4 = checksum = "checksum0004"
     self.size4 = size = 100000
     self.image4 = GlanceSyncImage(name, id, region, owner, False, checksum, size, status, props)
    def setUp(self):
        """create self.facade, create also a GlanceSyncImage object and a
        temporal file. Use a mock to replace osclients"""
        target = dict()
        target['target_name'] = 'master'
        target['user'] = '******'
        target['password'] = '******'
        target['keystone_url'] = 'http://127.0.0.1/'
        target['tenant'] = 'faketenant'
        target['use_keystone_v3'] = False
        self.target = target

        self.region = 'fakeregion'
        targets = dict()
        targets['master'] = target
        self.region_obj = GlanceSyncRegion(self.region, targets)

        mock_osclients.reset_mock()
        self.facade = ServersFacade(self.target)
        self.facade.images_dir = None

        image = GlanceSyncImage('imagetest', '01', self.region, None, False)
        image.raw = dict()
        image.raw['disk_format'] = 'qcow2'
        image.raw['is_public'] = False
        image.raw['protected'] = False
        image.raw['container_format'] = 'bare'
        image.raw['min_ram'] = '0'
        image.raw['min_disk'] = '0'

        self.image = image
    def _create_images(self, name):
        """Create a pair or images, one on each region"""
        image1 = GlanceSyncImage(
            name, 'id_VA_' + name, 'Valladolid', 'VA_tenant1', True,
            'checksum', 1000, 'active', dict())
        image1.is_public = False

        image2 = GlanceSyncImage(
            name, 'id_BU_' + name, 'Burgos', 'BU_tenant1', True,
            'checksum', 1000, 'active', dict())
        image2.is_public = False

        return image1, image2
 def setUp(self):
     self.name = name = 'image1'
     self.id1 = id = '0001'
     self.region = region = 'Valladolid'
     self.owner = owner = '00000001'
     self.is_public = is_public = True
     self.checksum = checksum = 'cheksum00001'
     self.size = size = 1500000
     self.status = status = 'active'
     self.props = props = {'p1': 'v1', 'p2': 'v2', 'p3': 'v3'}
     self.image1 = GlanceSyncImage(name, id, region, owner, is_public,
                                   checksum, size, status, props)
     self.name2 = name = 'image2'
     self.id2 = id = '0002'
     self.checksum2 = checksum = 'cheksum00002'
     self.props2 = props = {'p1': 'v1', 'p2': 'v2bis'}
     self.image2 = GlanceSyncImage(name, id, region, owner, is_public,
                                   checksum, size, status, props)
     self.id3 = id = '0003'
     self.size3 = size = 100000
     self.props3 = props = {}
     self.checksum3 = checksum = 'cheksum00003'
     self.image3 = GlanceSyncImage(name, id, region, owner, is_public,
                                   checksum, size, status, props)
     self.id4 = id = '0004'
     self.props4 = props = {}
     self.checksum4 = checksum = 'checksum0004'
     self.size4 = size = 100000
     self.image4 = GlanceSyncImage(name, id, region, owner, False, checksum,
                                   size, status, props)
    def setUp(self):
        """create self.facade, create also a GlanceSyncImage object and a
        temporal file. Use a mock to replace osclients"""
        target = dict()
        target['target_name'] = 'master'
        target['user'] = '******'
        target['password'] = '******'
        target['keystone_url'] = 'http://127.0.0.1/'
        target['tenant'] = 'faketenant'
        target['use_keystone_v3'] = False
        self.target = target

        self.region = 'fakeregion'
        targets = dict()
        targets['master'] = target
        self.region_obj = GlanceSyncRegion(self.region, targets)

        mock_osclients.reset_mock()
        self.facade = ServersFacade(self.target)
        self.facade.images_dir = None

        image = GlanceSyncImage('imagetest', '01', self.region, None, False)
        image.raw = dict()
        image.raw['disk_format'] = 'qcow2'
        image.raw['is_public'] = False
        image.raw['protected'] = False
        image.raw['container_format'] = 'bare'
        image.raw['min_ram'] = '0'
        image.raw['min_disk'] = '0'

        self.image = image
 def setUp(self):
     self.name = name = 'image1'
     self.owner = owner = 'owner1'
     size = 300000
     checksum = 'fac084184af34b'
     self.id1 = id = '0001'
     self.props1 = p = {'nid': 5043, 'type': 6043, 'localprop': 8888}
     self.master_images = dict()
     self.master_images[name] = GlanceSyncImage(name, id, 'Valladolid',
                                                owner, True, checksum, size,
                                                'active', p)
     self.owner2 = owner = 'owner2'
     self.id2 = id = '000A'
     self.props1 = p = {'nid': 5043, 'type': 6043, 'localprop': 8889}
     self.image = GlanceSyncImage(name, id, 'Burgos', owner, True, checksum,
                                  size, 'active', p)
    def create_image(self):
        """function to create_image, used by several tests; check that UUID
         is returned"""

        image = GlanceSyncImage('imagetest', '01', self.region, None, False)
        image.raw = dict()
        image.raw['disk_format'] = 'qcow2'
        image.raw['is_public'] = False
        image.raw['protected'] = False
        image.raw['container_format'] = 'bare'
        image.raw['min_ram'] = '0'
        image.raw['min_disk'] = '0'
        image.user_properties['key'] = 'original_value'

        self.created = self.facade.upload_image(self.region_obj, image)
        self.assertIsNotNone(self.created)
    def create_image(self):
        """function to create_image, used by several tests; check that UUID
         is returned"""

        image = GlanceSyncImage('imagetest', '01', self.region, None, False)
        image.raw = dict()
        image.raw['disk_format'] = 'qcow2'
        image.raw['is_public'] = False
        image.raw['protected'] = False
        image.raw['container_format'] = 'bare'
        image.raw['min_ram'] = '0'
        image.raw['min_disk'] = '0'
        image.user_properties['key'] = 'original_value'

        self.created = self.facade.upload_image(self.region_obj, image)
        self.assertIsNotNone(self.created)
Exemple #10
0
    def setUp(self):
        """
        Configure the execution of each test in the class. Get the app for testing and create the testing DB.

        :return: Nothing.
        """
        self.app = app.app.test_client()
        self.app.testing = True
        app.app.config[
            'SQLALCHEMY_DATABASE_URI'] = TEST_SQLALCHEMY_DATABASE_URI

        db.create_all()

        image1 = GlanceSyncImage(
            region='Valladolid',
            name='image10',
            id='010',
            status='active',
            size=1073741914,
            checksum='b1d5781111d84f7b3fe45a0852e59758cd7a87e5',
            owner='tenant1',
            is_public=True,
            user_properties={'type': 'baseimage'})

        image2 = GlanceSyncImage(
            region='Valladolid',
            name='image20',
            id='020',
            status='active',
            size=1073741916,
            checksum='d885781111d84f7b3fe45a0852e59758cd7a87e5',
            owner='tenant1',
            is_public=True,
            user_properties={'type': 'baseimage'})

        images_region = [image1, image2]

        self.config = {
            'return_value.get_images_region.return_value': images_region
        }
 def setUp(self):
     self.name = name = "image1"
     self.owner = owner = "owner1"
     size = 300000
     checksum = "fac084184af34b"
     self.id1 = id = "0001"
     self.props1 = p = {"nid": 5043, "type": 6043, "localprop": 8888}
     self.master_images = dict()
     self.master_images[name] = GlanceSyncImage(name, id, "Valladolid", owner, True, checksum, size, "active", p)
     self.owner2 = owner = "owner2"
     self.id2 = id = "000A"
     self.props1 = p = {"nid": 5043, "type": 6043, "localprop": 8889}
     self.image = GlanceSyncImage(name, id, "Burgos", owner, True, checksum, size, "active", p)
 def setUp(self):
     self.img_kernel = GlanceSyncImage('kernel1', '0001', 'Valladolid',
                                       'own0', True, '00', 10000, 'active',
                                       {})
     self.img_ramdisk = GlanceSyncImage('ramdisk1', '0002', 'Valladolid',
                                        'own0', True, '00', 10000, 'active',
                                        {})
     self.img1 = GlanceSyncImage('img1', '0003', 'Valladolid', 'own0', True,
                                 '00', 10000, 'active', {
                                     'kernel_id': '0001',
                                     'ramdisk_id': '0002',
                                     'type': 'base'
                                 })
     self.list = [self.img_kernel, self.img_ramdisk, self.img1]
     self.img_mod = copy.deepcopy(self.img1)
     self.img_mod.user_properties['kernel_id'] = 'kernel1'
     self.img_mod.user_properties['ramdisk_id'] = 'ramdisk1'
     self.dict_master = {
         'kernel1': self.img_kernel,
         'ramdisk1': self.img_ramdisk,
         'img1': self.img_mod
     }
    def _create_images(self, name):
        """Create a pair or images, one on each region"""
        image1 = GlanceSyncImage(name, 'id_VA_' + name, 'Valladolid',
                                 'VA_tenant1', True, 'checksum', 1000,
                                 'active', dict())
        image1.is_public = False

        image2 = GlanceSyncImage(name, 'id_BU_' + name, 'Burgos', 'BU_tenant1',
                                 True, 'checksum', 1000, 'active', dict())
        image2.is_public = False

        return image1, image2
Exemple #14
0
def create_images(region, count, prefix, tenant):
    """Helper function for creating a sequence or regions. The images are
    also added to mock."""
    images = list()
    nid = 1
    seq = 0
    size = 1073741824
    for i in range(1, count + 1):
        image_id = str(i).zfill(2)
        user_properties = dict()
        public = True
        if seq == 0:
            user_properties['type'] = 'ngimages'
            user_properties['nid'] = nid
            nid += 1
            seq += 1
        elif seq == 1:
            user_properties['type'] = 'baseimage'
            seq += 1
        elif seq == 2:
            user_properties['type'] = 'ngimages'
            user_properties['nid'] = nid
            nid += 1
            seq += 1
            public = False
        else:
            seq = 0
        image = (GlanceSyncImage('image' + image_id, prefix + image_id, region,
                                 tenant, public,
                                 hashlib.sha1(image_id).hexdigest(), size,
                                 'active', user_properties))
        # order of uploading is ascending size.
        size += 10
        images.append(image)
        ServersFacade.add_image_to_mock(image)
    return images
class TestGlanceSyncImageRegion(unittest.TestCase):
    """Class to test all methods but compare_with_masterregion, that has
    its own testing class"""

    def setUp(self):
        self.name = name = "image1"
        self.id1 = id = "0001"
        self.region = region = "Valladolid"
        self.owner = owner = "00000001"
        self.is_public = is_public = True
        self.checksum = checksum = "cheksum00001"
        self.size = size = 1500000
        self.status = status = "active"
        self.props = props = {"p1": "v1", "p2": "v2", "p3": "v3"}
        self.image1 = GlanceSyncImage(name, id, region, owner, is_public, checksum, size, status, props)
        self.name2 = name = "image2"
        self.id2 = id = "0002"
        self.checksum2 = checksum = "cheksum00002"
        self.props2 = props = {"p1": "v1", "p2": "v2bis"}
        self.image2 = GlanceSyncImage(name, id, region, owner, is_public, checksum, size, status, props)
        self.id3 = id = "0003"
        self.size3 = size = 100000
        self.props3 = props = {}
        self.checksum3 = checksum = "cheksum00003"
        self.image3 = GlanceSyncImage(name, id, region, owner, is_public, checksum, size, status, props)
        self.id4 = id = "0004"
        self.props4 = props = {}
        self.checksum4 = checksum = "checksum0004"
        self.size4 = size = 100000
        self.image4 = GlanceSyncImage(name, id, region, owner, False, checksum, size, status, props)

    def test_contstructor(self):
        """Only check that each value is in the correct position"""
        self.assertEquals(self.image1.name, self.name)
        self.assertEquals(self.image1.id, self.id1)
        self.assertEquals(self.image1.is_public, self.is_public)
        self.assertEquals(self.image1.checksum, self.checksum)
        self.assertEquals(self.image1.size, self.size)
        self.assertEquals(self.image1.status, self.status)
        self.assertEquals(self.image1.user_properties, self.props)

    def test_user_properties_independent(self):
        """Verify that stored user properties are independent from the ones
         passed in the constructor (modify this last and check that object
         content has not changed)"""
        original = copy.copy(self.props)
        del (self.props["p1"])
        self.assertNotEquals(self.props, self.image1.user_properties)
        self.assertEquals(original, self.image1.user_properties)

    def test_eq(self):
        """Test == and != operators"""
        self.assertNotEquals(self.image1, self.image2)
        self.assertEquals(self.image1, copy.deepcopy(self.image1))

    def test_to_field_list(self):
        """Test method to_field_list, without filter"""
        result = [
            self.region,
            self.name,
            self.id1,
            self.status,
            self.size,
            self.checksum,
            self.owner,
            self.is_public,
            str(self.props),
        ]

        self.assertEquals(self.image1.to_field_list(None), result)

    def test_to_field_list_filtered(self):
        """test method to_field_lst, with filter"""
        props = ["p1", "p2"]
        result = [
            self.region,
            self.name,
            self.id1,
            self.status,
            self.size,
            self.checksum,
            self.owner,
            self.is_public,
            self.props["p1"],
            self.props["p2"],
        ]
        self.assertEquals(self.image1.to_field_list(props), result)

    def test_from_field_list(self):
        """test method from_field_list"""
        l = self.image1.to_field_list()
        image = GlanceSyncImage.from_field_list(l)
        self.assertEquals(image, self.image1)

    def test_csv_userproperties(self):
        """test method csv_userproperties"""
        props = ("p1", "missing", "p2")
        result = self.name + "," + self.props["p1"] + ",," + self.props["p2"]
        self.assertEquals(self.image1.csv_userproperties(props), result)

    def test_is_synchronisable_nometada_nofunction(self):
        """Test is_synchronisable method, without medata_set nor filter
        function:
         *All public images are synchronisable.
         *A private image is syncrhonisable only if forcesync.
        """
        force_sync = list()
        metadata_s = set()
        self.assertTrue(self.image1.is_synchronisable(metadata_s, force_sync))
        self.assertTrue(self.image2.is_synchronisable(metadata_s, force_sync))
        self.assertTrue(self.image3.is_synchronisable(metadata_s, force_sync))
        self.assertFalse(self.image4.is_synchronisable(metadata_s, force_sync))
        force_sync = [self.id4]
        self.assertTrue(self.image4.is_synchronisable(metadata_s, force_sync))

    def test_is_synchronisable_metada_nofunction(self):
        """Test is_synchronisable method, with metadata_set but without filter
        function
         *Images without p3 property are not synchronisable, unless they are
         included in forcesync
         *A private image is syncrhonisable only if forcesync.
         """
        force_sync = set()
        metadata_s = set(["p3"])
        self.assertTrue(self.image1.is_synchronisable(metadata_s, force_sync))
        self.assertFalse(self.image2.is_synchronisable(metadata_s, force_sync))
        self.assertFalse(self.image3.is_synchronisable(metadata_s, force_sync))
        self.assertFalse(self.image4.is_synchronisable(metadata_s, force_sync))
        force_sync = set([self.id2, self.id4])
        self.assertTrue(self.image1.is_synchronisable(metadata_s, force_sync))
        self.assertTrue(self.image2.is_synchronisable(metadata_s, force_sync))
        self.assertFalse(self.image3.is_synchronisable(metadata_s, force_sync))
        self.assertTrue(self.image4.is_synchronisable(metadata_s, force_sync))

        # Images without p3 nor p2 properties are not synchronisable, unless
        # they are included in forcesync
        force_sync = set()
        metadata_s = set(["p3", "p2"])
        self.assertTrue(self.image1.is_synchronisable(metadata_s, force_sync))
        self.assertTrue(self.image2.is_synchronisable(metadata_s, force_sync))
        self.assertFalse(self.image3.is_synchronisable(metadata_s, force_sync))
        self.assertFalse(self.image4.is_synchronisable(metadata_s, force_sync))
        force_sync = set([self.id3])
        self.assertTrue(self.image1.is_synchronisable(metadata_s, force_sync))
        self.assertTrue(self.image2.is_synchronisable(metadata_s, force_sync))
        self.assertTrue(self.image3.is_synchronisable(metadata_s, force_sync))
        self.assertFalse(self.image4.is_synchronisable(metadata_s, force_sync))

    def test_is_synchronisable_nometadata_function(self):
        """Test is_synchronisable method, using a filter function.
        When using a function, private images may also match."""
        func = "'p2' in image.user_properties and " "image.user_properties['p2']=='v2bis' or image.size <= 100000"
        m = set()
        force = set()
        self.assertFalse(self.image1.is_synchronisable(m, force, func))
        self.assertTrue(self.image2.is_synchronisable(m, force, func))
        self.assertTrue(self.image3.is_synchronisable(m, force, func))
        self.assertTrue(self.image4.is_synchronisable(m, force, func))
        force = set([self.id1])
        self.assertTrue(self.image1.is_synchronisable(m, force, func))

    def test_is_synchronisable_metadata_function(self):
        """Test is_sycnhronisable method, with a function that also checks
        metadata_set"""
        m = set(["p3"])
        force = set()
        func = "image.is_public and metadata_set.intersection(" "image.user_properties.keys())"
        self.assertTrue(self.image1.is_synchronisable(m, force, func))
        self.assertFalse(self.image2.is_synchronisable(m, force, func))
        self.assertFalse(self.image3.is_synchronisable(m, force, func))
        self.assertFalse(self.image4.is_synchronisable(m, force, func))

    def test_is_synchronisable_obsolete(self):
        """if image name ends with '_obsolete' it is not synchronisable"""
        force_sync = list([self.image1.id])
        self.assertTrue(self.image1.is_synchronisable(set(), force_sync))
        self.image1.name += "_obsolete"
        self.assertFalse(self.image1.is_synchronisable(set(), force_sync))
class TestGlanceSyncImageCompare(unittest.TestCase):
    """Class to test compare_with_masterregion under different conditions"""

    def setUp(self):
        self.name = name = "image1"
        self.owner = owner = "owner1"
        size = 300000
        checksum = "fac084184af34b"
        self.id1 = id = "0001"
        self.props1 = p = {"nid": 5043, "type": 6043, "localprop": 8888}
        self.master_images = dict()
        self.master_images[name] = GlanceSyncImage(name, id, "Valladolid", owner, True, checksum, size, "active", p)
        self.owner2 = owner = "owner2"
        self.id2 = id = "000A"
        self.props1 = p = {"nid": 5043, "type": 6043, "localprop": 8889}
        self.image = GlanceSyncImage(name, id, "Burgos", owner, True, checksum, size, "active", p)

    def test_compare_with_props(self):
        """check that images are synchronised, although some properties differ,
        the ones included in the set are matching"""
        rslt = self.image.compare_with_masterregion(self.master_images, None)
        self.assertEquals(rslt, "#")
        prop_eval = set(["nid", "type"])
        r = self.image.compare_with_masterregion(self.master_images, prop_eval)
        self.assertEquals(r, "")

    def test_compare_with_ami(self):
        """checks with kernel_id and ramdisk_id"""
        prop_eval = set(["nid", "type"])
        self.master_images[self.name].user_properties["kernel_id"] = 350
        self.master_images[self.name].user_properties["ramdisk_id"] = 351
        r = self.image.compare_with_masterregion(self.master_images, prop_eval)
        self.assertEquals(r, "#")
        # Different values of kernel_id and ramdisk_id is OK
        self.image.user_properties["kernel_id"] = 450
        self.image.user_properties["ramdisk_id"] = 451
        r = self.image.compare_with_masterregion(self.master_images, prop_eval)
        self.assertEquals(r, "")

    def test_compare_missing(self):
        """test when the image is missing"""
        self.image.name = "image2"
        r = self.image.compare_with_masterregion(self.master_images, None)
        self.assertEquals(r, "+")

    def test_compare_private_region(self):
        """test when image exists but it is private in the region"""
        self.image.is_public = False
        r = self.image.compare_with_masterregion(self.master_images, None)
        self.assertEquals(r, "_")

    def test_compare_private_master(self):
        """test when image exists and is private in the master, public in
        region"""
        self.master_images[self.name].is_public = False
        r = self.image.compare_with_masterregion(self.master_images, None)
        self.assertEquals(r, "-")

    def test_compare_checksum(self):
        """test when the checksum differs"""
        self.image.checksum = "000000000000000"
        r = self.image.compare_with_masterregion(self.master_images, None)
        self.assertEquals(r, "!")

    def test_compare_status(self):
        """test when the image is in a wrong status"""
        self.image.status = "pending"
        r = self.image.compare_with_masterregion(self.master_images, None)
        self.assertEquals(r, "$")
 def test_from_field_list(self):
     """test method from_field_list"""
     l = self.image1.to_field_list()
     image = GlanceSyncImage.from_field_list(l)
     self.assertEquals(image, self.image1)
 def create_image(self, region, count, prefix):
     """Helper function for creating a sequence or regions"""
     count = str(count).zfill(2)
     return GlanceSyncImage('image' + count, prefix + count, region,
                            'tenantid', True, 'checksum', 1000, 'active',
                            dict())
 def test_from_field_list(self):
     """test method from_field_list"""
     l = self.image1.to_field_list()
     image = GlanceSyncImage.from_field_list(l)
     self.assertEquals(image, self.image1)
class TestGlanceSyncImageCompare(unittest.TestCase):
    """Class to test compare_with_masterregion under different conditions"""
    def setUp(self):
        self.name = name = 'image1'
        self.owner = owner = 'owner1'
        size = 300000
        checksum = 'fac084184af34b'
        self.id1 = id = '0001'
        self.props1 = p = {'nid': 5043, 'type': 6043, 'localprop': 8888}
        self.master_images = dict()
        self.master_images[name] = GlanceSyncImage(name, id, 'Valladolid',
                                                   owner, True, checksum, size,
                                                   'active', p)
        self.owner2 = owner = 'owner2'
        self.id2 = id = '000A'
        self.props1 = p = {'nid': 5043, 'type': 6043, 'localprop': 8889}
        self.image = GlanceSyncImage(name, id, 'Burgos', owner, True, checksum,
                                     size, 'active', p)

    def test_compare_with_props(self):
        """check that images are synchronised, although some properties differ,
        the ones included in the set are matching"""
        rslt = self.image.compare_with_masterregion(self.master_images, None)
        self.assertEquals(rslt, '#')
        prop_eval = set(['nid', 'type'])
        r = self.image.compare_with_masterregion(self.master_images, prop_eval)
        self.assertEquals(r, '')

    def test_compare_with_ami(self):
        """checks with kernel_id and ramdisk_id"""
        prop_eval = set(['nid', 'type'])
        self.master_images[self.name].user_properties['kernel_id'] = 350
        self.master_images[self.name].user_properties['ramdisk_id'] = 351
        r = self.image.compare_with_masterregion(self.master_images, prop_eval)
        self.assertEquals(r, '#')
        # Different values of kernel_id and ramdisk_id is OK
        self.image.user_properties['kernel_id'] = 450
        self.image.user_properties['ramdisk_id'] = 451
        r = self.image.compare_with_masterregion(self.master_images, prop_eval)
        self.assertEquals(r, '')

    def test_compare_missing(self):
        """test when the image is missing"""
        self.image.name = 'image2'
        r = self.image.compare_with_masterregion(self.master_images, None)
        self.assertEquals(r, '+')

    def test_compare_private_region(self):
        """test when image exists but it is private in the region"""
        self.image.is_public = False
        r = self.image.compare_with_masterregion(self.master_images, None)
        self.assertEquals(r, '_')

    def test_compare_private_master(self):
        """test when image exists and is private in the master, public in
        region"""
        self.master_images[self.name].is_public = False
        r = self.image.compare_with_masterregion(self.master_images, None)
        self.assertEquals(r, '-')

    def test_compare_checksum(self):
        """test when the checksum differs"""
        self.image.checksum = '000000000000000'
        r = self.image.compare_with_masterregion(self.master_images, None)
        self.assertEquals(r, '!')

    def test_compare_status(self):
        """test when the image is in a wrong status"""
        self.image.status = 'pending'
        r = self.image.compare_with_masterregion(self.master_images, None)
        self.assertEquals(r, '$')
class TestGlanceSyncImageRegion(unittest.TestCase):
    """Class to test all methods but compare_with_masterregion, that has
    its own testing class"""
    def setUp(self):
        self.name = name = 'image1'
        self.id1 = id = '0001'
        self.region = region = 'Valladolid'
        self.owner = owner = '00000001'
        self.is_public = is_public = True
        self.checksum = checksum = 'cheksum00001'
        self.size = size = 1500000
        self.status = status = 'active'
        self.props = props = {'p1': 'v1', 'p2': 'v2', 'p3': 'v3'}
        self.image1 = GlanceSyncImage(name, id, region, owner, is_public,
                                      checksum, size, status, props)
        self.name2 = name = 'image2'
        self.id2 = id = '0002'
        self.checksum2 = checksum = 'cheksum00002'
        self.props2 = props = {'p1': 'v1', 'p2': 'v2bis'}
        self.image2 = GlanceSyncImage(name, id, region, owner, is_public,
                                      checksum, size, status, props)
        self.id3 = id = '0003'
        self.size3 = size = 100000
        self.props3 = props = {}
        self.checksum3 = checksum = 'cheksum00003'
        self.image3 = GlanceSyncImage(name, id, region, owner, is_public,
                                      checksum, size, status, props)
        self.id4 = id = '0004'
        self.props4 = props = {}
        self.checksum4 = checksum = 'checksum0004'
        self.size4 = size = 100000
        self.image4 = GlanceSyncImage(name, id, region, owner, False, checksum,
                                      size, status, props)

    def test_contstructor(self):
        """Only check that each value is in the correct position"""
        self.assertEquals(self.image1.name, self.name)
        self.assertEquals(self.image1.id, self.id1)
        self.assertEquals(self.image1.is_public, self.is_public)
        self.assertEquals(self.image1.checksum, self.checksum)
        self.assertEquals(self.image1.size, self.size)
        self.assertEquals(self.image1.status, self.status)
        self.assertEquals(self.image1.user_properties, self.props)

    def test_user_properties_independent(self):
        """Verify that stored user properties are independent from the ones
         passed in the constructor (modify this last and check that object
         content has not changed)"""
        original = copy.copy(self.props)
        del (self.props['p1'])
        self.assertNotEquals(self.props, self.image1.user_properties)
        self.assertEquals(original, self.image1.user_properties)

    def test_eq(self):
        """Test == and != operators"""
        self.assertNotEquals(self.image1, self.image2)
        self.assertEquals(self.image1, copy.deepcopy(self.image1))

    def test_to_field_list(self):
        """Test method to_field_list, without filter"""
        result = [
            self.region, self.name, self.id1, self.status, self.size,
            self.checksum, self.owner, self.is_public,
            str(self.props)
        ]

        self.assertEquals(self.image1.to_field_list(None), result)

    def test_to_field_list_filtered(self):
        """test method to_field_lst, with filter"""
        props = ['p1', 'p2']
        result = [
            self.region, self.name, self.id1, self.status, self.size,
            self.checksum, self.owner, self.is_public, self.props['p1'],
            self.props['p2']
        ]
        self.assertEquals(self.image1.to_field_list(props), result)

    def test_from_field_list(self):
        """test method from_field_list"""
        l = self.image1.to_field_list()
        image = GlanceSyncImage.from_field_list(l)
        self.assertEquals(image, self.image1)

    def test_csv_userproperties(self):
        """test method csv_userproperties"""
        props = ('p1', 'missing', 'p2')
        result = self.name + ',' + self.props['p1'] + ',,' + self.props['p2']
        self.assertEquals(self.image1.csv_userproperties(props), result)

    def test_is_synchronisable_nometada_nofunction(self):
        """Test is_synchronisable method, without medata_set nor filter
        function:
         *All public images are synchronisable.
         *A private image is syncrhonisable only if forcesync.
        """
        force_sync = list()
        metadata_s = set()
        self.assertTrue(self.image1.is_synchronisable(metadata_s, force_sync))
        self.assertTrue(self.image2.is_synchronisable(metadata_s, force_sync))
        self.assertTrue(self.image3.is_synchronisable(metadata_s, force_sync))
        self.assertFalse(self.image4.is_synchronisable(metadata_s, force_sync))
        force_sync = [self.id4]
        self.assertTrue(self.image4.is_synchronisable(metadata_s, force_sync))

    def test_is_synchronisable_metada_nofunction(self):
        """Test is_synchronisable method, with metadata_set but without filter
        function
         *Images without p3 property are not synchronisable, unless they are
         included in forcesync
         *A private image is syncrhonisable only if forcesync.
         """
        force_sync = set()
        metadata_s = set(['p3'])
        self.assertTrue(self.image1.is_synchronisable(metadata_s, force_sync))
        self.assertFalse(self.image2.is_synchronisable(metadata_s, force_sync))
        self.assertFalse(self.image3.is_synchronisable(metadata_s, force_sync))
        self.assertFalse(self.image4.is_synchronisable(metadata_s, force_sync))
        force_sync = set([self.id2, self.id4])
        self.assertTrue(self.image1.is_synchronisable(metadata_s, force_sync))
        self.assertTrue(self.image2.is_synchronisable(metadata_s, force_sync))
        self.assertFalse(self.image3.is_synchronisable(metadata_s, force_sync))
        self.assertTrue(self.image4.is_synchronisable(metadata_s, force_sync))

        # Images without p3 nor p2 properties are not synchronisable, unless
        # they are included in forcesync
        force_sync = set()
        metadata_s = set(['p3', 'p2'])
        self.assertTrue(self.image1.is_synchronisable(metadata_s, force_sync))
        self.assertTrue(self.image2.is_synchronisable(metadata_s, force_sync))
        self.assertFalse(self.image3.is_synchronisable(metadata_s, force_sync))
        self.assertFalse(self.image4.is_synchronisable(metadata_s, force_sync))
        force_sync = set([self.id3])
        self.assertTrue(self.image1.is_synchronisable(metadata_s, force_sync))
        self.assertTrue(self.image2.is_synchronisable(metadata_s, force_sync))
        self.assertTrue(self.image3.is_synchronisable(metadata_s, force_sync))
        self.assertFalse(self.image4.is_synchronisable(metadata_s, force_sync))

    def test_is_synchronisable_nometadata_function(self):
        """Test is_synchronisable method, using a filter function.
        When using a function, private images may also match."""
        func = "'p2' in image.user_properties and "\
               "image.user_properties['p2']=='v2bis' or image.size <= 100000"
        m = set()
        force = set()
        self.assertFalse(self.image1.is_synchronisable(m, force, func))
        self.assertTrue(self.image2.is_synchronisable(m, force, func))
        self.assertTrue(self.image3.is_synchronisable(m, force, func))
        self.assertTrue(self.image4.is_synchronisable(m, force, func))
        force = set([self.id1])
        self.assertTrue(self.image1.is_synchronisable(m, force, func))

    def test_is_synchronisable_metadata_function(self):
        """Test is_sycnhronisable method, with a function that also checks
        metadata_set"""
        m = set(['p3'])
        force = set()
        func = "image.is_public and metadata_set.intersection("\
               "image.user_properties.keys())"
        self.assertTrue(self.image1.is_synchronisable(m, force, func))
        self.assertFalse(self.image2.is_synchronisable(m, force, func))
        self.assertFalse(self.image3.is_synchronisable(m, force, func))
        self.assertFalse(self.image4.is_synchronisable(m, force, func))

    def test_is_synchronisable_obsolete(self):
        """if image name ends with '_obsolete' it is not synchronisable"""
        force_sync = list([self.image1.id])
        self.assertTrue(self.image1.is_synchronisable(set(), force_sync))
        self.image1.name += '_obsolete'
        self.assertFalse(self.image1.is_synchronisable(set(), force_sync))