Пример #1
0
def test():
    bs_cond = res_ops.gen_query_conditions("status", '=', "Connected")
    bss = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE, bs_cond, \
            None, fields=['uuid'])
    if not bss:
        test_util.test_skip("not find available backup storage. Skip test")

    image_option = test_util.ImageOption()
    image_option.set_name('test_image_cache_cleanup')
    image_option.set_format('qcow2')
    image_option.set_mediaType('RootVolumeTemplate')
    image_option.set_url(os.environ.get('imageUrl_s'))
    image_option.set_backup_storage_uuid_list([bss[0].uuid])

    new_image = zstack_image_header.ZstackTestImage()
    new_image.set_creation_option(image_option)

    new_image.add_root_volume_template()

    l3_name = os.environ.get('l3VlanNetworkName1')
    l3_net_uuid = test_lib.lib_get_l3_by_name(l3_name).uuid

    vm = test_stub.create_vm([l3_net_uuid], new_image.image.uuid, 'imagecache_vm', \
            default_l3_uuid = l3_net_uuid)
    test_obj_dict.add_vm(vm)
    vm.check()
    host = test_lib.lib_find_host_by_vm(vm.get_vm())
    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())

    vm.destroy()
    if test_lib.lib_get_vm_delete_policy() != 'Direct':
        vm.expunge()

    new_image.delete()
    if test_lib.lib_get_image_delete_policy() != 'Direct':
        new_image.expunge()
    if ps.type == 'SharedMountPoint':
        test_util.test_skip(
            'CleanUpImageCacheOnPrimaryStorage not supported on SMP storage, skip test.'
        )
    elif ps.type == inventory.CEPH_PRIMARY_STORAGE_TYPE:
        test_util.test_skip(
            'ceph is not directly using image cache, skip test.')

    ps_ops.cleanup_imagecache_on_primary_storage(ps.uuid)
    if ps.type == inventory.LOCAL_STORAGE_TYPE:
        image_cache_path = "%s/imagecache/template/%s" % (ps.mountPath,
                                                          new_image.image.uuid)
        if test_lib.lib_check_file_exist(host, image_cache_path):
            test_util.test_fail('image cache is expected to be deleted')
    elif ps.type == inventory.NFS_PRIMARY_STORAGE_TYPE:
        image_cache_path = "%s/imagecache/template/%s" % (ps.mountPath,
                                                          new_image.image.uuid)
        if test_lib.lib_check_file_exist(host, image_cache_path):
            test_util.test_fail('image cache is expected to be deleted')
#    elif ps.type == inventory.CEPH_PRIMARY_STORAGE_TYPE:
#    elif ps.type == 'SharedMountPoint':

    test_util.test_pass('imagecache cleanup Pass.')
def test():
    bs_cond = res_ops.gen_query_conditions("status", '=', "Connected")
    bss = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE, bs_cond, \
            None, fields=['uuid'])
    if not bss:
        test_util.test_skip("not find available backup storage. Skip test")

    image_option = test_util.ImageOption()
    image_option.set_name('test_image_cache_cleanup')
    image_option.set_format('qcow2')
    image_option.set_mediaType('RootVolumeTemplate')
    image_option.set_url(os.environ.get('imageUrl_s'))
    image_option.set_backup_storage_uuid_list([bss[0].uuid])

    new_image = zstack_image_header.ZstackTestImage()
    new_image.set_creation_option(image_option)

    new_image.add_root_volume_template()

    l3_name = os.environ.get('l3VlanNetworkName1')
    l3_net_uuid = test_lib.lib_get_l3_by_name(l3_name).uuid

    vm = test_stub.create_vm([l3_net_uuid], new_image.image.uuid, 'imagecache_vm', \
            default_l3_uuid = l3_net_uuid)
    test_obj_dict.add_vm(vm)
    vm.check()
    host = test_lib.lib_find_host_by_vm(vm.get_vm())
    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())

    vm.destroy()
    if test_lib.lib_get_vm_delete_policy() != 'Direct':
        vm.expunge()

    new_image.delete()
    if test_lib.lib_get_image_delete_policy() != 'Direct':
        new_image.expunge()
    if ps.type == 'SharedMountPoint':
        test_util.test_skip('CleanUpImageCacheOnPrimaryStorage not supported on SMP storage, skip test.')
    elif ps.type == inventory.CEPH_PRIMARY_STORAGE_TYPE:
        test_util.test_skip('ceph is not directly using image cache, skip test.')

    ps_ops.cleanup_imagecache_on_primary_storage(ps.uuid)
    if ps.type == inventory.LOCAL_STORAGE_TYPE:
        image_cache_path = "%s/imagecache/template/%s/%s.qcow2" % (ps.mountPath, new_image.image.uuid, new_image.image.uuid)
        if test_lib.lib_check_file_exist(host, image_cache_path):
            test_util.test_fail('image cache is expected to be deleted')
    elif ps.type == inventory.NFS_PRIMARY_STORAGE_TYPE:
        image_cache_path = "%s/imagecache/template/%s/%s.qcow2" % (ps.mountPath, new_image.image.uuid, new_image.image.uuid)
        if test_lib.lib_check_file_exist(host, image_cache_path):
            test_util.test_fail('image cache is expected to be deleted')
#    elif ps.type == inventory.CEPH_PRIMARY_STORAGE_TYPE:
#    elif ps.type == 'SharedMountPoint':

    test_util.test_pass('imagecache cleanup Pass.')
    def check(self):
        super(zstack_kvm_image_file_checker, self).check()
        image = self.test_obj.image
        backupStorages = image.backupStorageRefs
        bs_one = backupStorages[0]
        bs = test_lib.lib_get_backup_storage_by_uuid(bs_one.backupStorageUuid)
        if bs.type == inventory.SFTP_BACKUP_STORAGE_TYPE:
            host = test_lib.lib_get_backup_storage_host(bs_one.backupStorageUuid)
            image_url = backupStorages[0].installPath
            self.judge(test_lib.lib_check_file_exist(host, image_url))
        elif bs.type == inventory.CEPH_BACKUP_STORAGE_TYPE:
            if self.test_obj.state == image_header.DELETED:
                #https://github.com/zstackorg/zstack/issues/93#issuecomment-130935998
                test_util.test_logger("skip ceph image delete check, since the image won't be deleted until no vms refer to it.")
                return self.judge(self.exp_result)

            ceph_host, username, password = test_lib.lib_get_ceph_info(os.environ.get('cephBackupStorageMonUrls'))
            image_installPath = bs_one.installPath.split('ceph://')[1]

            command = 'rbd info %s' % image_installPath
            if test_lib.lib_execute_ssh_cmd(ceph_host, username, password, command, 10):
                test_util.test_logger('Check result: [image:] %s [file:] %s exist on ceph [host name:] %s .' % (image.uuid, image_installPath, ceph_host))
                return self.judge(True)
            else:
                test_util.test_logger('Check result: [image:] %s [file:] %s does not exist on ceph [host name:] %s .' % (image.uuid, image_installPath, ceph_host))
                return self.judge(False)
def test():
    global session_to
    global session_mc

    session_to = con_ops.change_global_config('identity', 'session.timeout', '720000')
    session_mc = con_ops.change_global_config('identity', 'session.maxConcurrent', '10000')
    test_util.test_dsc('Create test vm as utility vm')
    vm = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm)
    #use root volume to skip add_checking_point
    test_util.test_dsc('Use root volume for snapshot testing')
    root_volume_inv = test_lib.lib_get_root_volume(vm.get_vm())
    root_volume = zstack_volume_header.ZstackTestVolume()
    root_volume.set_volume(root_volume_inv)
    root_volume.set_state(volume_header.ATTACHED)
    root_volume.set_target_vm(vm)
    test_obj_dict.add_volume(root_volume)
    vm.check()

    snapshots = test_obj_dict.get_volume_snapshot(root_volume.get_volume().uuid)
    snapshots.set_utility_vm(vm)

    ori_num = 100
    index = 1
    while index < 101:
        thread = threading.Thread(target=create_snapshot, args=(snapshots, index,))
        thread.start()
        index += 1

    while threading.activeCount() > 1:
        time.sleep(0.1)

    #snapshot.check() doesn't work for root volume
    #snapshots.check()
    #check if snapshot exists in install_path
    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
    if ps.type == inventory.NFS_PRIMARY_STORAGE_TYPE or ps.type == inventory.LOCAL_STORAGE_TYPE:
        host = test_lib.lib_get_vm_host(vm.get_vm())
        for snapshot in snapshots.get_snapshot_list():
            snapshot_inv = snapshot.get_snapshot()
            sp_ps_install_path = snapshot_inv.primaryStorageInstallPath
            if test_lib.lib_check_file_exist(host, sp_ps_install_path):
                test_util.test_logger('Check result: snapshot %s is found in host %s in path %s' % (snapshot_inv.name, host.managementIp, sp_ps_install_path))
            else:
                test_lib.lib_robot_cleanup(test_obj_dict)
                test_util.test_fail('Check result: snapshot %s is not found in host %s in path %s' % (snapshot_inv.name, host.managementIp, sp_ps_install_path))
    else:
        test_util.test_logger('Skip check file install path for %s primary storage' % (ps.type))

    cond = res_ops.gen_query_conditions('volumeUuid', '=', root_volume.get_volume().uuid)
    sps_num = res_ops.query_resource_count(res_ops.VOLUME_SNAPSHOT, cond)

    if sps_num != ori_num:
        test_util.test_fail('Create %d snapshots, but only %d snapshots were successfully created' % (ori_num, sps_num))

    try:
        test_lib.lib_robot_cleanup(test_obj_dict)
    except:
        test_lib.test_logger('Delete VM may timeout')
    test_util.test_pass('Test create 100 snapshots simultaneously success')
    def check(self):
        '''
        will check if backuped snapshot existed.
        '''
        super(zstack_kvm_backuped_snapshot_checker, self).check()
        backuped_snapshots = self.test_obj.get_backuped_snapshots()
        for sp in backuped_snapshots:
            sp_bs_refs = sp.get_snapshot().backupStorageRefs
            for sp_bs_ref in sp_bs_refs:
                bs_uuid = sp_bs_ref.backupStorageUuid
                sp_path = sp_bs_ref.installPath
                sp_uuid = sp_bs_ref.volumeSnapshotUuid
                bs_host = test_lib.lib_get_backup_storage_host(bs_uuid)
                bs = test_lib.lib_get_backup_storage_by_uuid(sp_bs_ref.backupStorageUuid)
                if hasattr(inventory, 'IMAGE_STORE_BACKUP_STORAGE_TYPE') and bs.type == inventory.IMAGE_STORE_BACKUP_STORAGE_TYPE:
                    sp_info = sp_path.split('://')[1].split('/')
                    sp_path = '%s/registry/v1/repos/public/%s/manifests/revisions/%s' % (bs.url, sp_info[0], sp_info[1])
                if test_lib.lib_check_file_exist(bs_host, sp_path):
                    test_util.test_logger('Checker result: backuped snapshot:%s is found in backup storage:%s in path: %s' % (sp_uuid, bs_uuid, sp_path))
                    if self.exp_result == False:
                        return self.judge(True)
                else:
                    test_util.test_logger('Checker result: backuped snapshot:%s is NOT found in backup storage:%s in path: %s' % (sp_uuid, bs_uuid, sp_path))
                    if self.exp_result == True:
                        return self.judge(False)

        test_util.test_logger('Checker result: Finish backuped snapshot checking')
        return self.judge(self.exp_result)
Пример #6
0
 def check(self):
     super(zstack_kvm_image_file_checker, self).check()
     image = self.test_obj.image
     backupStorages = image.backupStorageRefs
     image_url = backupStorages[0].installPath
     host = test_lib.lib_get_backup_storage_host(image.backupStorageUuid)
     self.judge(test_lib.lib_check_file_exist(host, image_url))
    def check(self):
        '''
        will check if backuped snapshot existed.
        '''
        super(zstack_kvm_backuped_snapshot_checker, self).check()
        backuped_snapshots = self.test_obj.get_backuped_snapshots()
        for sp in backuped_snapshots:
            sp_bs_refs = sp.get_snapshot().backupStorageRefs
            for sp_bs_ref in sp_bs_refs:
                bs_uuid = sp_bs_ref.backupStorageUuid
                sp_path = sp_bs_ref.installPath
                sp_uuid = sp_bs_ref.volumeSnapshotUuid
                bs_host = test_lib.lib_get_backup_storage_host(bs_uuid)
                if test_lib.lib_check_file_exist(bs_host, sp_path):
                    test_util.test_logger(
                        'Checker result: backuped snapshot:%s is found in backup storage:%s in path: %s'
                        % (sp_uuid, bs_uuid, sp_path))
                    if self.exp_result == False:
                        return self.judge(True)
                else:
                    test_util.test_logger(
                        'Checker result: backuped snapshot:%s is NOT found in backup storage:%s in path: %s'
                        % (sp_uuid, bs_uuid, sp_path))
                    if self.exp_result == True:
                        return self.judge(False)

        test_util.test_logger(
            'Checker result: Finish backuped snapshot checking')
        return self.judge(self.exp_result)
    def check(self):
        '''
        will check if backuped snapshot existed.
        '''
        super(zstack_vcenter_backuped_snapshot_checker, self).check()
        backuped_snapshots = self.test_obj.get_backuped_snapshots()
        for sp in backuped_snapshots:
            sp_bs_refs = sp.get_snapshot().backupStorageRefs
            for sp_bs_ref in sp_bs_refs:
                bs_uuid = sp_bs_ref.backupStorageUuid
                sp_path = sp_bs_ref.installPath
                sp_uuid = sp_bs_ref.volumeSnapshotUuid
                bs_host = test_lib.lib_get_backup_storage_host(bs_uuid)
                bs = test_lib.lib_get_backup_storage_by_uuid(
                    sp_bs_ref.backupStorageUuid)
                if hasattr(
                        inventory, 'IMAGE_STORE_BACKUP_STORAGE_TYPE'
                ) and bs.type == inventory.IMAGE_STORE_BACKUP_STORAGE_TYPE:
                    sp_info = sp_path.split('://')[1].split('/')
                    sp_path = '%s/registry/v1/repos/public/%s/manifests/revisions/%s' % (
                        bs.url, sp_info[0], sp_info[1])
                if test_lib.lib_check_file_exist(bs_host, sp_path):
                    test_util.test_logger(
                        'Checker result: backuped snapshot:%s is found in backup storage:%s in path: %s'
                        % (sp_uuid, bs_uuid, sp_path))
                    if self.exp_result == False:
                        return self.judge(True)
                else:
                    test_util.test_logger(
                        'Checker result: backuped snapshot:%s is NOT found in backup storage:%s in path: %s'
                        % (sp_uuid, bs_uuid, sp_path))
                    if self.exp_result == True:
                        return self.judge(False)

        test_util.test_logger(
            'Checker result: Finish backuped snapshot checking')
        return self.judge(self.exp_result)
    def check(self):
        '''
        will check if backuped snapshot existed.
        '''
        super(zstack_kvm_backuped_snapshot_checker, self).check()
        backuped_snapshots = self.test_obj.get_backuped_snapshots()
        for sp in backuped_snapshots:
            sp_bs_refs = sp.get_snapshot().backupStorageRefs
            for sp_bs_ref in sp_bs_refs:
                bs_uuid = sp_bs_ref.backupStorageUuid
                sp_path = sp_bs_ref.installPath
                sp_uuid = sp_bs_ref.volumeSnapshotUuid
                bs_host = test_lib.lib_get_backup_storage_host(bs_uuid)
                if test_lib.lib_check_file_exist(bs_host, sp_path):
                    test_util.test_logger('Checker result: backuped snapshot:%s is found in backup storage:%s in path: %s' % (sp_uuid, bs_uuid, sp_path))
                    if self.exp_result == False:
                        return self.judge(True)
                else:
                    test_util.test_logger('Checker result: backuped snapshot:%s is NOT found in backup storage:%s in path: %s' % (sp_uuid, bs_uuid, sp_path))
                    if self.exp_result == True:
                        return self.judge(False)

        test_util.test_logger('Checker result: Finish backuped snapshot checking')
        return self.judge(self.exp_result)
def test():
    bs_cond = res_ops.gen_query_conditions("status", '=', "Connected")
    bss = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE, bs_cond, \
            None, fields=['uuid'])
    if not bss:
        test_util.test_skip("not find available backup storage. Skip test")

    image_option = test_util.ImageOption()
    image_option.set_name('test_image_cache_cleanup')
    image_option.set_format('qcow2')
    image_option.set_mediaType('RootVolumeTemplate')
    image_option.set_url(os.environ.get('imageUrl_s'))
    image_option.set_backup_storage_uuid_list([bss[0].uuid])

    new_image = zstack_image_header.ZstackTestImage()
    new_image.set_creation_option(image_option)

    new_image.add_root_volume_template()

    l3_name = os.environ.get('l3VlanNetworkName1')
    l3_net_uuid = test_lib.lib_get_l3_by_name(l3_name).uuid

    conditions = res_ops.gen_query_conditions('type', '=', 'UserVm')
    instance_offering_uuid = res_ops.query_resource(res_ops.INSTANCE_OFFERING,
                                                    conditions)[0].uuid
    conditions = res_ops.gen_query_conditions('state', '=', 'Enabled')
    conditions = res_ops.gen_query_conditions('status', '=', 'Connected',
                                              conditions)
    host_uuid = res_ops.query_resource(res_ops.HOST, conditions)[0].uuid
    vm_creation_option = test_util.VmOption()
    vm_creation_option.set_host_uuid(host_uuid)
    vm_creation_option.set_l3_uuids([l3_net_uuid])
    vm_creation_option.set_image_uuid(new_image.image.uuid)
    vm_creation_option.set_instance_offering_uuid(instance_offering_uuid)
    vm_creation_option.set_name('test_image_cache_cleanup_vm1')
    vm = test_vm_header.ZstackTestVm()
    vm.set_creation_option(vm_creation_option)
    vm.create()

    test_obj_dict.add_vm(vm)
    vm.check()
    host = test_lib.lib_find_host_by_vm(vm.get_vm())
    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())

    vm.destroy()
    vm.expunge()

    conditions = res_ops.gen_query_conditions('state', '=', 'Enabled')
    conditions = res_ops.gen_query_conditions('status', '=', 'Connected',
                                              conditions)
    conditions = res_ops.gen_query_conditions('uuid', '!=', host.uuid,
                                              conditions)
    host_uuid = res_ops.query_resource(res_ops.HOST, conditions)[0].uuid
    vm_creation_option.set_host_uuid(host_uuid)
    vm_creation_option.set_name('test_image_cache_cleanup_vm2')
    vm2 = test_vm_header.ZstackTestVm()
    vm2.set_creation_option(vm_creation_option)
    vm2.create()
    host2 = test_lib.lib_find_host_by_vm(vm2.get_vm())
    test_obj_dict.add_vm(vm2)
    disk_offering = test_lib.lib_get_disk_offering_by_name(
        os.environ.get('smallDiskOfferingName'))
    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_disk_offering_uuid(disk_offering.uuid)
    volume1 = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume1)
    volume1.attach(vm2)

    if ps.type == inventory.CEPH_PRIMARY_STORAGE_TYPE:
        test_util.test_skip(
            'ceph is not directly using image cache, skip test.')

    if ps.type == "SharedBlock":
        path = "/dev/" + ps.uuid + '/' + new_image.image.uuid
        if not test_lib.lib_check_sharedblock_file_exist(host, path):
            test_util.test_fail('image cache is expected to exist')
    else:
        if ps.type == "AliyunNAS":
            image_cache_path = "%s/datas/imagecache/template/%s" % (
                ps.mountPath, new_image.image.uuid)
        else:
            image_cache_path = "%s/imagecache/template/%s" % (
                ps.mountPath, new_image.image.uuid)
        if not test_lib.lib_check_file_exist(host, image_cache_path):
            test_util.test_fail('image cache is expected to exist')
        if bss[0].type == inventory.IMAGE_STORE_BACKUP_STORAGE_TYPE:
            image_cache_path = "%s/zstore-cache/%s" % (ps.mountPath,
                                                       new_image.image.uuid)
            if not test_lib.lib_check_file_exist(host, image_cache_path):
                test_util.test_fail('image cache is expected to exist')

    new_image.delete()
    new_image.expunge()

    ps_ops.cleanup_imagecache_on_primary_storage(ps.uuid)
    if ps.type == inventory.LOCAL_STORAGE_TYPE:
        count = 0
        while True:
            image_cache_path = "%s/imagecache/template/%s/%s.qcow2" % (
                ps.mountPath, new_image.image.uuid, new_image.image.uuid)
            if not test_lib.lib_check_file_exist(host, image_cache_path):
                break
            elif count > 5:
                test_util.test_fail('image cache is expected to be deleted')
            test_util.test_logger('check %s times: image cache still exist' %
                                  (count))
            time.sleep(5)
            count += 1

    vm2.destroy()
    vm2.expunge()
    ps_ops.cleanup_imagecache_on_primary_storage(ps.uuid)

    if ps.type == "SharedBlock":
        image_cache_path = "/dev/" + ps.uuid + '/' + new_image.image.uuid
        count = 0
        while True:
            if not test_lib.lib_check_sharedblock_file_exist(
                    host, image_cache_path):
                break
            elif count > 5:
                test_util.test_fail('image cache is expected to be deleted')
            test_util.test_logger('check %s times: image cache still exist' %
                                  (count))
            time.sleep(10)
            count += 1
        test_util.test_pass('imagecache cleanup Pass.')

    count = 0
    while True:
        image_cache_path = "%s/imagecache/template/%s" % (ps.mountPath,
                                                          new_image.image.uuid)
        if not test_lib.lib_check_file_exist(host, image_cache_path):
            break
        elif count > 5:
            test_util.test_fail('image cache is expected to be deleted')
        test_util.test_logger('check %s times: image cache still exist' %
                              (count))
        time.sleep(5)
        count += 1

    count = 0
    while True:
        image_cache_path = "%s/zstore-cache/%s" % (ps.mountPath,
                                                   new_image.image.uuid)
        if not test_lib.lib_check_file_exist(host, image_cache_path):
            break
        elif count > 5:
            test_util.test_fail('image cache is expected to be deleted')
        test_util.test_logger('check %s times: image cache still exist' %
                              (count))
        time.sleep(5)
        count += 1

    test_util.test_pass('imagecache cleanup Pass.')
def test():
    bs_cond = res_ops.gen_query_conditions("status", '=', "Connected")
    bss = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE, bs_cond, \
            None, fields=['uuid'])
    if not bss:
        test_util.test_skip("not find available backup storage. Skip test")

    image_option = test_util.ImageOption()
    image_option.set_name('test_image_cache_cleanup')
    image_option.set_format('qcow2')
    image_option.set_mediaType('RootVolumeTemplate')
    image_option.set_url(os.environ.get('imageUrl_s'))
    image_option.set_backup_storage_uuid_list([bss[0].uuid])

    new_image = zstack_image_header.ZstackTestImage()
    new_image.set_creation_option(image_option)

    new_image.add_root_volume_template()

    l3_name = os.environ.get('l3VlanNetworkName1')
    l3_net_uuid = test_lib.lib_get_l3_by_name(l3_name).uuid

    conditions = res_ops.gen_query_conditions('type', '=', 'UserVm')
    instance_offering_uuid = res_ops.query_resource(res_ops.INSTANCE_OFFERING, conditions)[0].uuid
    conditions = res_ops.gen_query_conditions('state', '=', 'Enabled')
    conditions = res_ops.gen_query_conditions('status', '=', 'Connected', conditions)
    host_uuid = res_ops.query_resource(res_ops.HOST, conditions)[0].uuid
    vm_creation_option = test_util.VmOption()
    vm_creation_option.set_host_uuid(host_uuid)
    vm_creation_option.set_l3_uuids([l3_net_uuid])
    vm_creation_option.set_image_uuid(new_image.image.uuid)
    vm_creation_option.set_instance_offering_uuid(instance_offering_uuid)
    vm_creation_option.set_name('test_image_cache_cleanup_vm1')
    vm = test_vm_header.ZstackTestVm()
    vm.set_creation_option(vm_creation_option)
    vm.create()

    test_obj_dict.add_vm(vm)
    vm.check()
    host = test_lib.lib_find_host_by_vm(vm.get_vm())
    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())

    vm.destroy()
    vm.expunge()

    conditions = res_ops.gen_query_conditions('state', '=', 'Enabled')
    conditions = res_ops.gen_query_conditions('status', '=', 'Connected', conditions)
    conditions = res_ops.gen_query_conditions('uuid', '!=', host.uuid, conditions)
    host_uuid = res_ops.query_resource(res_ops.HOST, conditions)[0].uuid
    vm_creation_option.set_host_uuid(host_uuid)
    vm_creation_option.set_name('test_image_cache_cleanup_vm2')
    vm2 = test_vm_header.ZstackTestVm()
    vm2.set_creation_option(vm_creation_option)
    vm2.create()
    host2 = test_lib.lib_find_host_by_vm(vm2.get_vm())
    test_obj_dict.add_vm(vm2)
    disk_offering = test_lib.lib_get_disk_offering_by_name(os.environ.get('smallDiskOfferingName'))
    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_disk_offering_uuid(disk_offering.uuid)
    volume1 = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume1)
    volume1.attach(vm2)

    if ps.type == inventory.CEPH_PRIMARY_STORAGE_TYPE:
        test_util.test_skip('ceph is not directly using image cache, skip test.')

    if ps.type == "SharedBlock":
        path = "/dev/" + ps.uuid + '/' + new_image.image.uuid
        if not test_lib.lib_check_sharedblock_file_exist(host, path):
            test_util.test_fail('image cache is expected to exist')
    else:
        if ps.type == "AliyunNAS":
            image_cache_path = "%s/datas/imagecache/template/%s" % (ps.mountPath, new_image.image.uuid)
        else:
            image_cache_path = "%s/imagecache/template/%s" % (ps.mountPath, new_image.image.uuid)
        if not test_lib.lib_check_file_exist(host, image_cache_path):
            test_util.test_fail('image cache is expected to exist')
        if bss[0].type == inventory.IMAGE_STORE_BACKUP_STORAGE_TYPE:
            image_cache_path = "%s/zstore-cache/%s" % (ps.mountPath, new_image.image.uuid)
            if not test_lib.lib_check_file_exist(host, image_cache_path):
                test_util.test_fail('image cache is expected to exist') 

    new_image.delete()
    new_image.expunge()

    ps_ops.cleanup_imagecache_on_primary_storage(ps.uuid)
    if ps.type == inventory.LOCAL_STORAGE_TYPE:
        count = 0
        while True:
            image_cache_path = "%s/imagecache/template/%s/%s.qcow2" % (ps.mountPath, new_image.image.uuid, new_image.image.uuid)
            if not test_lib.lib_check_file_exist(host, image_cache_path):
                break
            elif count > 5:
                test_util.test_fail('image cache is expected to be deleted')
            test_util.test_logger('check %s times: image cache still exist' % (count))
            time.sleep(5)
            count += 1

    vm2.destroy()
    vm2.expunge()
    ps_ops.cleanup_imagecache_on_primary_storage(ps.uuid)    

    if ps.type == "SharedBlock":
        image_cache_path = "/dev/" + ps.uuid + '/' + new_image.image.uuid
        count = 0
        while True:
            if not test_lib.lib_check_sharedblock_file_exist(host, image_cache_path):
                break
            elif count > 5:
                test_util.test_fail('image cache is expected to be deleted')
            test_util.test_logger('check %s times: image cache still exist' % (count))
            time.sleep(10)
            count += 1
        test_util.test_pass('imagecache cleanup Pass.')

    if ps.type == "AliyunNAS":
        count = 0
        while True:
            image_cache_path = "%s/datas/imagecache/template/%s/%s.qcow2" % (ps.mountPath, new_image.image.uuid, new_image.image.uuid)
            if not test_lib.lib_check_file_exist(host, image_cache_path):
                break
            elif count > 5:
                test_util.test_fail('image cache is expected to be deleted')
            test_util.test_logger('check %s times: image cache still exist' % (count))
            time.sleep(5)
            count += 1
        test_util.test_pass('imagecache cleanup Pass.')

    count = 0
    while True:
        image_cache_path = "%s/imagecache/template/%s" % (ps.mountPath, new_image.image.uuid)
        if not test_lib.lib_check_file_exist(host, image_cache_path):
            break
        elif count > 5:
            test_util.test_fail('image cache is expected to be deleted')
        test_util.test_logger('check %s times: image cache still exist' % (count))
        time.sleep(5)
        count += 1
    
    count = 0
    while True:
        image_cache_path = "%s/zstore-cache/%s" % (ps.mountPath, new_image.image.uuid)
        if not test_lib.lib_check_file_exist(host, image_cache_path):
            break
        elif count > 5:
            test_util.test_fail('image cache is expected to be deleted')
        test_util.test_logger('check %s times: image cache still exist' % (count))
        time.sleep(5)
        count += 1

    test_util.test_pass('imagecache cleanup Pass.')
def test():
    global vm
    bs_cond = res_ops.gen_query_conditions("status", '=', "Connected")
    bss = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE, bs_cond, \
            None, fields=['uuid'])
    if not bss:
        test_util.test_skip("not find available backup storage. Skip test")

    image_option = test_util.ImageOption()
    image_option.set_name('test_image_cache_cleanup')
    image_option.set_format('qcow2')
    image_option.set_mediaType('RootVolumeTemplate')
    image_option.set_url(os.environ.get('imageUrl_s'))
    image_option.set_backup_storage_uuid_list([bss[0].uuid])

    new_image = zstack_image_header.ZstackTestImage()
    new_image.set_creation_option(image_option)

    new_image.add_root_volume_template()

    l3_name = os.environ.get('l3VlanNetworkName1')
    l3_net_uuid = test_lib.lib_get_l3_by_name(l3_name).uuid

    conditions = res_ops.gen_query_conditions('type', '=', 'UserVm')
    instance_offering_uuid = res_ops.query_resource(res_ops.INSTANCE_OFFERING, conditions)[0].uuid
    conditions = res_ops.gen_query_conditions('state', '=', 'Enabled')
    conditions = res_ops.gen_query_conditions('status', '=', 'Connected', conditions)
    host_uuid = res_ops.query_resource(res_ops.HOST, conditions)[0].uuid
    vm_creation_option = test_util.VmOption()
    vm_creation_option.set_host_uuid(host_uuid)
    vm_creation_option.set_l3_uuids([l3_net_uuid])
    vm_creation_option.set_image_uuid(new_image.image.uuid)
    vm_creation_option.set_instance_offering_uuid(instance_offering_uuid)
    vm_creation_option.set_name('test_image_cache_cleanup_vm1')
    vm = test_vm_header.ZstackTestVm()
    vm.set_creation_option(vm_creation_option)
    vm.create()
    ps = test_lib.lib_get_primary_storage_by_uuid(vm.get_vm().allVolumes[0].primaryStorageUuid)
    if ps.type != inventory.LOCAL_STORAGE_TYPE:
        test_util.test_skip('Skip test on non-localstorage')

    test_obj_dict.add_vm(vm)
    vm.check()
    host = test_lib.lib_find_host_by_vm(vm.get_vm())
    target_host = test_lib.lib_find_random_host(vm.vm)
    vm.stop()
    vol_ops.migrate_volume(vm.get_vm().allVolumes[0].uuid, target_host.uuid)
    vm.check()
    vm.start()
    vm.check()

    new_image.delete()
    new_image.expunge()
    ps_ops.cleanup_imagecache_on_primary_storage(ps.uuid)
    if ps.type == inventory.LOCAL_STORAGE_TYPE:
        image_cache_path = "%s/imagecache/template/%s" % (ps.mountPath, new_image.image.uuid)
        if test_lib.lib_check_file_exist(host, image_cache_path):
            test_util.test_fail('image cache is expected to be deleted')
#    elif ps.type == inventory.NFS_PRIMARY_STORAGE_TYPE:
#    elif ps.type == inventory.CEPH_PRIMARY_STORAGE_TYPE:
#    elif ps.type == 'SharedMountPoint':

    vm.destroy()
    test_util.test_pass('Migrate VM Test Success')
def test():
    bs_cond = res_ops.gen_query_conditions("status", '=', "Connected")
    bss = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE, bs_cond, \
            None, fields=['uuid'])
    if not bss:
        test_util.test_skip("not find available backup storage. Skip test")

    image_option = test_util.ImageOption()
    image_option.set_name('test_image_cache_cleanup')
    image_option.set_format('qcow2')
    image_option.set_mediaType('RootVolumeTemplate')
    image_option.set_url(os.environ.get('imageUrl_s'))
    image_option.set_backup_storage_uuid_list([bss[0].uuid])

    new_image = zstack_image_header.ZstackTestImage()
    new_image.set_creation_option(image_option)

    new_image.add_root_volume_template()

    l3_name = os.environ.get('l3VlanNetworkName1')
    l3_net_uuid = test_lib.lib_get_l3_by_name(l3_name).uuid

    conditions = res_ops.gen_query_conditions('type', '=', 'UserVm')
    instance_offering_uuid = res_ops.query_resource(res_ops.INSTANCE_OFFERING,
                                                    conditions)[0].uuid
    conditions = res_ops.gen_query_conditions('state', '=', 'Enabled')
    conditions = res_ops.gen_query_conditions('status', '=', 'Connected',
                                              conditions)
    host_uuid = res_ops.query_resource(res_ops.HOST, conditions)[0].uuid
    vm_creation_option = test_util.VmOption()
    vm_creation_option.set_host_uuid(host_uuid)
    vm_creation_option.set_l3_uuids([l3_net_uuid])
    vm_creation_option.set_image_uuid(new_image.image.uuid)
    vm_creation_option.set_instance_offering_uuid(instance_offering_uuid)
    vm_creation_option.set_name('test_image_cache_cleanup_vm1')
    vm = test_vm_header.ZstackTestVm()
    vm.set_creation_option(vm_creation_option)
    vm.create()

    test_obj_dict.add_vm(vm)
    vm.check()
    host = test_lib.lib_find_host_by_vm(vm.get_vm())
    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())

    vm.destroy()
    vm.expunge()

    conditions = res_ops.gen_query_conditions('state', '=', 'Enabled')
    conditions = res_ops.gen_query_conditions('status', '=', 'Connected',
                                              conditions)
    conditions = res_ops.gen_query_conditions('uuid', '!=', host.uuid,
                                              conditions)
    host_uuid = res_ops.query_resource(res_ops.HOST, conditions)[0].uuid
    vm_creation_option.set_host_uuid(host_uuid)
    vm_creation_option.set_name('test_image_cache_cleanup_vm2')
    vm2 = test_vm_header.ZstackTestVm()
    vm2.set_creation_option(vm_creation_option)
    vm2.create()
    host2 = test_lib.lib_find_host_by_vm(vm2.get_vm())
    test_obj_dict.add_vm(vm2)
    disk_offering = test_lib.lib_get_disk_offering_by_name(
        os.environ.get('smallDiskOfferingName'))
    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_disk_offering_uuid(disk_offering.uuid)
    volume1 = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume1)
    volume1.attach(vm2)

    new_image.delete()
    new_image.expunge()

    if ps.type == 'SharedMountPoint':
        test_util.test_skip(
            'CleanUpImageCacheOnPrimaryStorage not supported on SMP storage, skip test.'
        )
    elif ps.type == inventory.CEPH_PRIMARY_STORAGE_TYPE:
        test_util.test_skip(
            'ceph is not directly using image cache, skip test.')

    ps_ops.cleanup_imagecache_on_primary_storage(ps.uuid)
    if ps.type == inventory.LOCAL_STORAGE_TYPE:
        image_cache_path = "%s/imagecache/template/%s/%s.qcow2" % (
            ps.mountPath, new_image.image.uuid, new_image.image.uuid)
        if test_lib.lib_check_file_exist(host, image_cache_path):
            test_util.test_fail('image cache is expected to be deleted')

    vm2.destroy()
    vm2.expunge()
    ps_ops.cleanup_imagecache_on_primary_storage(ps.uuid)
    if ps.type == inventory.LOCAL_STORAGE_TYPE:
        image_cache_path = "%s/imagecache/template/%s" % (ps.mountPath,
                                                          new_image.image.uuid)
        if test_lib.lib_check_file_exist(host2, image_cache_path):
            test_util.test_fail('image cache is expected to be deleted')
    elif ps.type == inventory.NFS_PRIMARY_STORAGE_TYPE:
        image_cache_path = "%s/imagecache/template/%s" % (ps.mountPath,
                                                          new_image.image.uuid)
        if test_lib.lib_check_file_exist(host2, image_cache_path):
            test_util.test_fail('image cache is expected to be deleted')
#    elif ps.type == inventory.CEPH_PRIMARY_STORAGE_TYPE:
#    elif ps.type == 'SharedMountPoint':

    test_util.test_pass('imagecache cleanup Pass.')
def test():
    global vm
    bs_cond = res_ops.gen_query_conditions("status", '=', "Connected")
    bss = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE, bs_cond, \
            None, fields=['uuid'])
    if not bss:
        test_util.test_skip("not find available backup storage. Skip test")

    image_option = test_util.ImageOption()
    image_option.set_name('test_image_cache_cleanup')
    image_option.set_format('qcow2')
    image_option.set_mediaType('RootVolumeTemplate')
    image_option.set_url(os.environ.get('imageUrl_s'))
    image_option.set_backup_storage_uuid_list([bss[0].uuid])

    new_image = zstack_image_header.ZstackTestImage()
    new_image.set_creation_option(image_option)

    new_image.add_root_volume_template()

    l3_name = os.environ.get('l3VlanNetworkName1')
    l3_net_uuid = test_lib.lib_get_l3_by_name(l3_name).uuid

    conditions = res_ops.gen_query_conditions('type', '=', 'UserVm')
    instance_offering_uuid = res_ops.query_resource(res_ops.INSTANCE_OFFERING, conditions)[0].uuid
    conditions = res_ops.gen_query_conditions('state', '=', 'Enabled')
    conditions = res_ops.gen_query_conditions('status', '=', 'Connected', conditions)
    host_uuid = res_ops.query_resource(res_ops.HOST, conditions)[0].uuid
    vm_creation_option = test_util.VmOption()
    vm_creation_option.set_host_uuid(host_uuid)
    vm_creation_option.set_l3_uuids([l3_net_uuid])
    vm_creation_option.set_image_uuid(new_image.image.uuid)
    vm_creation_option.set_instance_offering_uuid(instance_offering_uuid)
    vm_creation_option.set_name('test_image_cache_cleanup_migration_vm')
    vm = test_vm_header.ZstackTestVm()
    vm.set_creation_option(vm_creation_option)
    vm.create()
    if not test_lib.lib_check_vm_live_migration_cap(vm.vm):
        test_util.test_skip('skip migrate if live migrate not supported')

    ps = test_lib.lib_get_primary_storage_by_uuid(vm.get_vm().allVolumes[0].primaryStorageUuid)
    test_obj_dict.add_vm(vm)
    vm.check()
    if ps.type == inventory.CEPH_PRIMARY_STORAGE_TYPE:
        test_util.test_skip('ceph is not directly using image cache, skip test.')

    host = test_lib.lib_find_host_by_vm(vm.get_vm())
    test_stub.migrate_vm_to_random_host(vm)
    vm.check()

    test_stub.migrate_vm_to_random_host(vm)
    vm.check()

    new_image.delete()
    new_image.expunge()

    vm.destroy()
    vm.expunge()
    ps_ops.cleanup_imagecache_on_primary_storage(ps.uuid)

    count = 0
    while True:
        image_cache_path = "%s/imagecache/template/%s" % (ps.mountPath, new_image.image.uuid)
        if not test_lib.lib_check_file_exist(host, image_cache_path):
            break
        elif count > 5:
            test_util.test_fail('image cache is expected to be deleted')
        test_util.test_logger('check %s times: image cache still exist' % (count))
        time.sleep(5)
        count += 1
    
    count = 0
    while True:
        image_cache_path = "%s/zstore-cache/%s" % (ps.mountPath, new_image.image.uuid)
        if not test_lib.lib_check_file_exist(host, image_cache_path):
            break
        elif count > 5:
            test_util.test_fail('image cache is expected to be deleted')
        test_util.test_logger('check %s times: image cache still exist' % (count))
        time.sleep(5)
        count += 1


    test_util.test_pass('Migrate VM Test Success')
def test():
    bs_cond = res_ops.gen_query_conditions("status", '=', "Connected")
    bss = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE, bs_cond, \
            None, fields=['uuid'])
    if not bss:
        test_util.test_skip("not find available backup storage. Skip test")

    image_option = test_util.ImageOption()
    image_option.set_name('test_image_cache_cleanup')
    image_option.set_format('qcow2')
    image_option.set_mediaType('RootVolumeTemplate')
    image_option.set_url(os.environ.get('imageUrl_s'))
    image_option.set_backup_storage_uuid_list([bss[0].uuid])

    new_image = zstack_image_header.ZstackTestImage()
    new_image.set_creation_option(image_option)

    new_image.add_root_volume_template()

    l3_name = os.environ.get('l3VlanNetworkName1')
    l3_net_uuid = test_lib.lib_get_l3_by_name(l3_name).uuid

    vm = test_stub.create_vm([l3_net_uuid], new_image.image.uuid, 'imagecache_vm', \
            default_l3_uuid = l3_net_uuid)
    test_obj_dict.add_vm(vm)
    vm.check()
    host = test_lib.lib_find_host_by_vm(vm.get_vm())
    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
    if ps.type == inventory.CEPH_PRIMARY_STORAGE_TYPE:
        test_util.test_skip(
            'ceph is not directly using image cache, skip test.')

    if ps.type == "SharedBlock":
        path = "/dev/" + ps.uuid + '/' + new_image.image.uuid
        if not test_lib.lib_check_sharedblock_file_exist(host, path):
            test_util.test_fail('image cache is expected to exist')
    else:
        if ps.type == "AliyunNAS":
            image_cache_path = "%s/datas/imagecache/template/%s" % (
                ps.mountPath, new_image.image.uuid)
        else:
            image_cache_path = "%s/imagecache/template/%s" % (
                ps.mountPath, new_image.image.uuid)
        if not test_lib.lib_check_file_exist(host, image_cache_path):
            test_util.test_fail('image cache is expected to exist')

        if bss[0].type == inventory.IMAGE_STORE_BACKUP_STORAGE_TYPE:
            image_cache_path = "%s/zstore-cache/%s" % (ps.mountPath,
                                                       new_image.image.uuid)
            if not test_lib.lib_check_file_exist(host, image_cache_path):
                test_util.test_fail('image cache is expected to exist')

    vm.destroy()
    if test_lib.lib_get_vm_delete_policy() != 'Direct':
        vm.expunge()

    new_image.delete()
    if test_lib.lib_get_image_delete_policy() != 'Direct':
        new_image.expunge()

    ps_ops.cleanup_imagecache_on_primary_storage(ps.uuid)

    if ps.type == "SharedBlock":
        image_cache_path = "/dev/" + ps.uuid + '/' + new_image.image.uuid
        count = 0
        while True:
            if not test_lib.lib_check_sharedblock_file_exist(
                    host, image_cache_path):
                break
            elif count > 5:
                test_util.test_fail('image cache is expected to be deleted')
            test_util.test_logger('check %s times: image cache still exist' %
                                  (count))
            time.sleep(5)
            count += 1
        test_util.test_pass('imagecache cleanup Pass.')

    count = 0
    while True:
        image_cache_path = "%s/imagecache/template/%s" % (ps.mountPath,
                                                          new_image.image.uuid)
        if not test_lib.lib_check_file_exist(host, image_cache_path):
            break
        elif count > 5:
            test_util.test_fail('image cache is expected to be deleted')
        test_util.test_logger('check %s times: image cache still exist' %
                              (count))
        time.sleep(5)
        count += 1

    count = 0
    while True:
        image_cache_path = "%s/zstore-cache/%s" % (ps.mountPath,
                                                   new_image.image.uuid)
        if not test_lib.lib_check_file_exist(host, image_cache_path):
            break
        elif count > 5:
            test_util.test_fail('image cache is expected to be deleted')
        test_util.test_logger('check %s times: image cache still exist' %
                              (count))
        time.sleep(5)
        count += 1

    test_util.test_pass('imagecache cleanup Pass.')
def test():
    global session_to
    global session_mc

    session_to = con_ops.change_global_config('identity', 'session.timeout',
                                              '720000')
    session_mc = con_ops.change_global_config('identity',
                                              'session.maxConcurrent', '10000')
    test_util.test_dsc('Create test vm as utility vm')
    vm = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm)
    #use root volume to skip add_checking_point
    test_util.test_dsc('Use root volume for snapshot testing')
    root_volume_inv = test_lib.lib_get_root_volume(vm.get_vm())
    root_volume = zstack_volume_header.ZstackTestVolume()
    root_volume.set_volume(root_volume_inv)
    root_volume.set_state(volume_header.ATTACHED)
    root_volume.set_target_vm(vm)
    test_obj_dict.add_volume(root_volume)
    vm.check()

    snapshots = test_obj_dict.get_volume_snapshot(
        root_volume.get_volume().uuid)
    snapshots.set_utility_vm(vm)

    ori_num = 100
    index = 1
    while index < 101:
        thread = threading.Thread(target=create_snapshot,
                                  args=(
                                      snapshots,
                                      index,
                                  ))
        thread.start()
        index += 1

    while threading.activeCount() > 1:
        time.sleep(0.1)

    #snapshot.check() doesn't work for root volume
    #snapshots.check()
    #check if snapshot exists in install_path
    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
    if ps.type == inventory.NFS_PRIMARY_STORAGE_TYPE or ps.type == inventory.LOCAL_STORAGE_TYPE:
        host = test_lib.lib_get_vm_host(vm.get_vm())
        for snapshot in snapshots.get_snapshot_list():
            snapshot_inv = snapshot.get_snapshot()
            sp_ps_install_path = snapshot_inv.primaryStorageInstallPath
            if test_lib.lib_check_file_exist(host, sp_ps_install_path):
                test_util.test_logger(
                    'Check result: snapshot %s is found in host %s in path %s'
                    %
                    (snapshot_inv.name, host.managementIp, sp_ps_install_path))
            else:
                test_lib.lib_robot_cleanup(test_obj_dict)
                test_util.test_fail(
                    'Check result: snapshot %s is not found in host %s in path %s'
                    %
                    (snapshot_inv.name, host.managementIp, sp_ps_install_path))
    else:
        test_util.test_logger(
            'Skip check file install path for %s primary storage' % (ps.type))

    cond = res_ops.gen_query_conditions('volumeUuid', '=',
                                        root_volume.get_volume().uuid)
    sps_num = res_ops.query_resource_count(res_ops.VOLUME_SNAPSHOT, cond)

    if sps_num != ori_num:
        test_util.test_fail(
            'Create %d snapshots, but only %d snapshots were successfully created'
            % (ori_num, sps_num))

    try:
        test_lib.lib_robot_cleanup(test_obj_dict)
    except:
        test_lib.test_logger('Delete VM may timeout')
    test_util.test_pass('Test create 100 snapshots simultaneously success')
def test():
    global vm
    bs_cond = res_ops.gen_query_conditions("status", '=', "Connected")
    bss = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE, bs_cond, \
            None, fields=['uuid'])
    if not bss:
        test_util.test_skip("not find available backup storage. Skip test")

    image_option = test_util.ImageOption()
    image_option.set_name('test_image_cache_cleanup')
    image_option.set_format('qcow2')
    image_option.set_mediaType('RootVolumeTemplate')
    image_option.set_url(os.environ.get('imageUrl_s'))
    image_option.set_backup_storage_uuid_list([bss[0].uuid])

    new_image = zstack_image_header.ZstackTestImage()
    new_image.set_creation_option(image_option)

    new_image.add_root_volume_template()

    l3_name = os.environ.get('l3VlanNetworkName1')
    l3_net_uuid = test_lib.lib_get_l3_by_name(l3_name).uuid

    conditions = res_ops.gen_query_conditions('type', '=', 'UserVm')
    instance_offering_uuid = res_ops.query_resource(res_ops.INSTANCE_OFFERING, conditions)[0].uuid
    conditions = res_ops.gen_query_conditions('state', '=', 'Enabled')
    conditions = res_ops.gen_query_conditions('status', '=', 'Connected', conditions)
    host_uuid = res_ops.query_resource(res_ops.HOST, conditions)[0].uuid
    vm_creation_option = test_util.VmOption()
    vm_creation_option.set_host_uuid(host_uuid)
    vm_creation_option.set_l3_uuids([l3_net_uuid])
    vm_creation_option.set_image_uuid(new_image.image.uuid)
    vm_creation_option.set_instance_offering_uuid(instance_offering_uuid)
    vm_creation_option.set_name('test_image_cache_cleanup_vm1')
    vm = test_vm_header.ZstackTestVm()
    vm.set_creation_option(vm_creation_option)
    vm.create()
    ps = test_lib.lib_get_primary_storage_by_uuid(vm.get_vm().allVolumes[0].primaryStorageUuid)
    if ps.type != inventory.LOCAL_STORAGE_TYPE:
        test_util.test_skip('Skip test on non-localstorage')

    test_obj_dict.add_vm(vm)
    vm.check()
    host = test_lib.lib_find_host_by_vm(vm.get_vm())
    target_host = test_lib.lib_find_random_host(vm.vm)
    vm.stop()
    vol_ops.migrate_volume(vm.get_vm().allVolumes[0].uuid, target_host.uuid)
    vm.check()
    vm.start()
    vm.check()

    new_image.delete()
    new_image.expunge()
    ps_ops.cleanup_imagecache_on_primary_storage(ps.uuid)
    count = 0
    while True:
        image_cache_path = "%s/imagecache/template/%s" % (ps.mountPath, new_image.image.uuid)
        if not test_lib.lib_check_file_exist(host, image_cache_path):
            break
        elif count > 5:
            test_util.test_fail('image cache is expected to be deleted')
        test_util.test_logger('check %s times: image cache still exist' % (count))
        time.sleep(5)
        count += 1
    
    count = 0
    while True:
        image_cache_path = "%s/zstore-cache/%s" % (ps.mountPath, new_image.image.uuid)
        if not test_lib.lib_check_file_exist(host, image_cache_path):
            break
        elif count > 5:
            test_util.test_fail('image cache is expected to be deleted')
        test_util.test_logger('check %s times: image cache still exist' % (count))
        time.sleep(5)
        count += 1


    vm.destroy()
    test_util.test_pass('Migrate VM Test Success')
def test():
    bs_cond = res_ops.gen_query_conditions("status", '=', "Connected")
    bss = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE, bs_cond, \
            None, fields=['uuid'])
    if not bss:
        test_util.test_skip("not find available backup storage. Skip test")

    bs = res_ops.query_resource(res_ops.BACKUP_STORAGE)
    for i in bs:
        if i.type == 'AliyunEBS':
            test_util.test_skip('Skip test on AliyunEBS backup storage')

    image_option = test_util.ImageOption()
    image_option.set_name('test_image_cache_cleanup')
    image_option.set_format('qcow2')
    image_option.set_mediaType('RootVolumeTemplate')
    image_option.set_url(os.environ.get('imageUrl_s'))
    image_option.set_backup_storage_uuid_list([bss[0].uuid])

    new_image = zstack_image_header.ZstackTestImage()
    new_image.set_creation_option(image_option)

    new_image.add_root_volume_template()

    l3_name = os.environ.get('l3VlanNetworkName1')
    l3_net_uuid = test_lib.lib_get_l3_by_name(l3_name).uuid

    vm = test_stub.create_vm([l3_net_uuid], new_image.image.uuid, 'imagecache_vm', \
            default_l3_uuid = l3_net_uuid)
    test_obj_dict.add_vm(vm)
    vm.check()
    host = test_lib.lib_find_host_by_vm(vm.get_vm())
    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
    if ps.type == inventory.CEPH_PRIMARY_STORAGE_TYPE:
        test_util.test_skip('ceph is not directly using image cache, skip test.')

    if ps.type == "SharedBlock":
        path = "/dev/" + ps.uuid + '/' + new_image.image.uuid
        if not test_lib.lib_check_sharedblock_file_exist(host, path):
            test_util.test_fail('image cache is expected to exist')
    else:
        if ps.type == "AliyunNAS":
            image_cache_path = "%s/datas/imagecache/template/%s" % (ps.mountPath, new_image.image.uuid)
        else:
            image_cache_path = "%s/imagecache/template/%s" % (ps.mountPath, new_image.image.uuid)
        if not test_lib.lib_check_file_exist(host, image_cache_path):
            test_util.test_fail('image cache is expected to exist')

        if bss[0].type == inventory.IMAGE_STORE_BACKUP_STORAGE_TYPE:
            image_cache_path = "%s/zstore-cache/%s" % (ps.mountPath, new_image.image.uuid)
            if not test_lib.lib_check_file_exist(host, image_cache_path):
                test_util.test_fail('image cache is expected to exist')

    vm.destroy()
    if test_lib.lib_get_vm_delete_policy() != 'Direct':
        vm.expunge()

    new_image.delete()
    if test_lib.lib_get_image_delete_policy() != 'Direct':
        new_image.expunge()

    ps_ops.cleanup_imagecache_on_primary_storage(ps.uuid)

    if ps.type == "SharedBlock":
        image_cache_path = "/dev/" + ps.uuid + '/' + new_image.image.uuid
        count = 0
        while True:
            if not test_lib.lib_check_sharedblock_file_exist(host, image_cache_path):
                break
            elif count > 5:
                test_util.test_fail('image cache is expected to be deleted')
            test_util.test_logger('check %s times: image cache still exist' % (count))
            time.sleep(5)
            count += 1
        test_util.test_pass('imagecache cleanup Pass.')

    if ps.type == "AliyunNAS":
        count = 0
        while True:
            image_cache_path = "%s/datas/imagecache/template/%s/%s.qcow2" % (ps.mountPath, new_image.image.uuid, new_image.image.uuid)
            if not test_lib.lib_check_file_exist(host, image_cache_path):
                break
            elif count > 5:
                test_util.test_fail('image cache is expected to be deleted')
            test_util.test_logger('check %s times: image cache still exist' % (count))
            time.sleep(5)
            count += 1
        test_util.test_pass('imagecache cleanup Pass.')

    count = 0
    while True:
        image_cache_path = "%s/imagecache/template/%s" % (ps.mountPath, new_image.image.uuid)
        if not test_lib.lib_check_file_exist(host, image_cache_path):
            break
        elif count > 5:
            test_util.test_fail('image cache is expected to be deleted')
        test_util.test_logger('check %s times: image cache still exist' % (count))
        time.sleep(5)
        count += 1
    
    count = 0
    while True:
        image_cache_path = "%s/zstore-cache/%s" % (ps.mountPath, new_image.image.uuid)
        if not test_lib.lib_check_file_exist(host, image_cache_path):
            break
        elif count > 5:
            test_util.test_fail('image cache is expected to be deleted')
        test_util.test_logger('check %s times: image cache still exist' % (count))
        time.sleep(5)
        count += 1

    test_util.test_pass('imagecache cleanup Pass.')