def test():
    global disaster_bs_uuid
    global root_volume_uuid
    disasterBsUrls = os.environ.get('disasterBsUrls')
    name = 'disaster_bs'
    description = 'backup storage for disaster'
    url = '/zstack_bs'
    sshport = 22
    hostname = disasterBsUrls.split('@')[1]
    username = disasterBsUrls.split(':')[0]
    password = disasterBsUrls.split('@')[0].split(':')[1]
    test_util.test_logger('Disaster bs server hostname is %s, username is %s, password is %s' %(hostname, username, password))
    #AddDisasterImageStoreBackupStorage
    disaster_backup_storage = bs_ops.add_disaster_image_store_bs(url, hostname, username, password, sshport, name, description)
    disaster_bs_uuid = disaster_backup_storage.uuid
    #AttachBackupStorageToZone
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    bs_ops.attach_backup_storage(disaster_bs_uuid, zone_uuid)
    #Selete a candidate image to sync
    cond = res_ops.gen_query_conditions('name', '=', os.environ.get('imageStoreBackupStorageName'))
    local_bs_uuid = res_ops.query_resource(res_ops.BACKUP_STORAGE, cond)[0].uuid
    cond = res_ops.gen_query_conditions('backupStorage.uuid', '=', local_bs_uuid)
    cond = res_ops.gen_query_conditions('name', '=', 'other', cond)
    image_uuid_local = res_ops.query_resource(res_ops.IMAGE, cond)[0].uuid
    #SyncImageFromImageStoreBackupStorage 
    disaster_bs_image = img_ops.sync_image_from_image_store_backup_storage(disaster_bs_uuid, local_bs_uuid, image_uuid_local)
    #Check the process status when syncing image
    cond = res_ops.gen_query_conditions('resourceUuid', '=', local_bs_uuid)
    system_tag = res_ops.query_resource(res_ops.SYSTEM_TAG, cond)[0].tag
    status = system_tag.split('::')[7]
    if status not in ['running', 'success']:
        test_util.test_fail('Error status for recovery image, status: %s' %status)
    #Check if the image's media_type correct
    disaster_bs_image_uuid = disaster_bs_image.uuid
    cond = res_ops.gen_query_conditions('uuid', '=', disaster_bs_image_uuid)
    media_type = res_ops.query_resource(res_ops.IMAGE, cond)[0].mediaType
    if media_type != 'RootVolumeTemplate':
        test_util.test_fail('Wrong image type, the expect is "RootVolumeTemplate", the real is "%s"' %media_type) 
    #Check if the system tag of the image in disaster bs is 'remote'
    cond = res_ops.gen_query_conditions('resourceUuid', '=', disaster_bs_image_uuid)
    system_tag = res_ops.query_resource(res_ops.SYSTEM_TAG, cond)[0]
    if system_tag.tag != "remote":
        test_util.test_fail("Here isn't 'remote' system tag for image in data protect bs")
    #Check if GetImagesFromImageStoreBackupStorage works well
    #disaster_bs_image_lst = img_ops.get_images_from_image_store_backup_storage(disaster_bs_uuid)
    #if disaster_bs_image_lst.infos == []:
    #    test_util.test_fail('GetImagesFromImageStoreBackupStorage unable to list the images in disaster bs')
    #Try to sync the same image again, it's negative test
    try:
        image_uuid = img_ops.sync_image_from_image_store_backup_storage(disaster_bs_uuid, local_bs_uuid, image_uuid_local)
    except Exception,e:
        if str(e).find('already contains it') != -1:
            test_util.test_logger('Try to sync the image which had exist in disaster bs get the error info expectly: %s' %str(e))
def test():
    global disaster_bs_uuid
    global root_volume_uuid
    disasterBsUrls = os.environ.get('disasterBsUrls')
    name = 'disaster_bs'
    description = 'backup storage for disaster'
    url = '/zstack_bs'
    sshport = 22
    hostname = disasterBsUrls.split('@')[1]
    username = disasterBsUrls.split(':')[0]
    password = disasterBsUrls.split('@')[0].split(':')[1]
    test_util.test_logger('Disaster bs server hostname is %s, username is %s, password is %s' %(hostname, username, password))
    #AddDisasterImageStoreBackupStorage
    disaster_backup_storage = bs_ops.add_disaster_image_store_bs(url, hostname, username, password, sshport, name, description)
    disaster_bs_uuid = disaster_backup_storage.uuid
    #AttachBackupStorageToZone
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    bs_ops.attach_backup_storage(disaster_bs_uuid, zone_uuid)
    #Selete a candidate image to sync
    cond = res_ops.gen_query_conditions('name', '=', os.environ.get('imageStoreBackupStorageName'))
    local_bs_uuid = res_ops.query_resource(res_ops.BACKUP_STORAGE, cond)[0].uuid
    cond = res_ops.gen_query_conditions('backupStorage.uuid', '=', local_bs_uuid)
    cond = res_ops.gen_query_conditions('name', '=', 'other', cond)
    image_uuid_local = res_ops.query_resource(res_ops.IMAGE, cond)[0].uuid
    #SyncImageFromImageStoreBackupStorage 
    disaster_bs_image = img_ops.sync_image_from_image_store_backup_storage(disaster_bs_uuid, local_bs_uuid, image_uuid_local)
    #Check the process status when syncing image
    cond = res_ops.gen_query_conditions('resourceUuid', '=', local_bs_uuid)
    system_tag = res_ops.query_resource(res_ops.SYSTEM_TAG, cond)[0].tag
    status = system_tag.split('::')[7]
    if status not in ['running', 'success']:
        test_util.test_fail('Error status for recovery image, status: %s' %status)
    #Check if the image's media_type correct
    disaster_bs_image_uuid = disaster_bs_image.uuid
    cond = res_ops.gen_query_conditions('uuid', '=', disaster_bs_image_uuid)
    media_type = res_ops.query_resource(res_ops.IMAGE, cond)[0].mediaType
    if media_type != 'RootVolumeTemplate':
        test_util.test_fail('Wrong image type, the expect is "RootVolumeTemplate", the real is "%s"' %media_type) 
    #Check if the system tag of the image in disaster bs is 'remote'
    cond = res_ops.gen_query_conditions('resourceUuid', '=', disaster_bs_image_uuid)
    system_tag = res_ops.query_resource(res_ops.SYSTEM_TAG, cond)[0]
    if system_tag.tag != "remote":
        test_util.test_fail("Here isn't 'remote' system tag for image in data protect bs")
    #Check if GetImagesFromImageStoreBackupStorage works well
    #disaster_bs_image_lst = img_ops.get_images_from_image_store_backup_storage(disaster_bs_uuid)
    #if disaster_bs_image_lst.infos == []:
    #    test_util.test_fail('GetImagesFromImageStoreBackupStorage unable to list the images in disaster bs')
    #Try to sync the same image again, it's negative test
    try:
        image_uuid = img_ops.sync_image_from_image_store_backup_storage(disaster_bs_uuid, local_bs_uuid, image_uuid_local)
    except Exception,e:
        if unicode(e).encode("utf-8").find('包含') != -1:
            test_util.test_logger('Try to sync the image which had exist in disaster bs get the error info expectly: %s' %unicode(e).encode("utf-8"))
Exemple #3
0
def test():
    global disaster_bs_uuid
    disasterBsUrls = os.environ.get('disasterBsUrls')
    name = 'disaster_bs'
    description = 'backup storage for disaster'
    url = '/zstack_bs'
    sshport = 22
    hostname = disasterBsUrls.split('@')[1]
    username = disasterBsUrls.split(':')[0]
    password = disasterBsUrls.split('@')[0].split(':')[1]
    test_util.test_logger(
        'Disaster bs server hostname is %s, username is %s, password is %s' %
        (hostname, username, password))
    #AddDisasterImageStoreBackupStorage
    disaster_backup_storage = bs_ops.add_disaster_image_store_bs(
        url, hostname, username, password, sshport, name, description)
    disaster_bs_uuid = disaster_backup_storage.uuid
    #AttachBackupStorageToZone
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    bs_ops.attach_backup_storage(disaster_bs_uuid, zone_uuid)
    #get a candidate image
    cond = res_ops.gen_query_conditions(
        'name', '=', os.environ.get('imageStoreBackupStorageName'))
    local_bs_uuid = res_ops.query_resource(res_ops.BACKUP_STORAGE,
                                           cond)[0].uuid
    cond = res_ops.gen_query_conditions('backupStorageRefs.backupStorageUuid',
                                        '=', local_bs_uuid)
    image = res_ops.query_resource(res_ops.IMAGE, cond)[0]
    #setup image option
    image_option = test_util.ImageOption()
    image_option.set_name('add_image_to_data_protect_bs')
    image_option.set_format(image.format)
    image_option.set_platform(image.platform)
    image_option.set_url(image.url)
    image_option.set_mediaType(image.mediaType)
    image_option.set_backup_storage_uuid_list([disaster_bs_uuid])
    #Try to add image to disaster bs directly
    try:
        new_img = img_ops.add_image(image_option)
    except Exception, e:
        if unicode(e).encode("utf-8").find('禁止') != -1:
            test_util.test_pass(
                'AddImage is forbidden in Disaster BS, Here is the expect error info: %s'
                % unicode(e).encode("utf-8"))
def add_backup_storage():
    bs_option = test_util.ImageStoreBackupStorageOption()
    bs_option.set_name("backup")
    bs_option.set_url("/home/sftpBackupStorage")
    cond = res_ops.gen_query_conditions("type", '=', "ImageStoreBackupStorage")
    bs_ip = res_ops.query_resource(res_ops.BACKUP_STORAGE, cond)[0].hostname
    hosts = res_ops.query_resource(res_ops.HOST)
    for host in hosts:
        if host.managementIp != bs_ip:
            bs_option.set_hostname(host.managementIp)
            zone_uuid = host.zoneUuid
            bs_option.set_sshPort(host.sshPort)
            break;
    bs_option.set_password('password')
    bs_option.set_username("root")
    bs_option.set_import_images('true')
    bs_inv = bs_ops.create_image_store_backup_storage(bs_option)

    bs_ops.attach_backup_storage(bs_inv.uuid, zone_uuid)
    return bs_inv
Exemple #5
0
def test():
    global disaster_bs_uuid
    disaster_bs_dict = bs_ops.get_disaster_backup_storage_info(
        test_lib.deploy_config)
    name = disaster_bs_dict['name']
    description = disaster_bs_dict['description']
    hostname = disaster_bs_dict['hostname']
    url = disaster_bs_dict['url']
    username = disaster_bs_dict['username']
    password = disaster_bs_dict['password']
    sshport = disaster_bs_dict['port']
    #AddDisasterImageStoreBackupStorage
    disaster_backup_storage = bs_ops.add_disaster_image_store_bs(
        url, hostname, username, password, sshport, name, description)
    disaster_bs_uuid = disaster_backup_storage.uuid
    #AttachBackupStorageToZone
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    bs_ops.attach_backup_storage(disaster_bs_uuid, zone_uuid)
    #get a candidate image
    cond = res_ops.gen_query_conditions('name', '=', 'sftp')
    local_bs_uuid = res_ops.query_resource(res_ops.BACKUP_STORAGE,
                                           cond)[0].uuid
    cond = res_ops.gen_query_conditions('backupStorageRefs.backupStorageUuid',
                                        '=', local_bs_uuid)
    image = res_ops.query_resource(res_ops.IMAGE, cond)[0]
    #setup image option
    image_option = test_util.ImageOption()
    image_option.set_name('add_image_to_data_protect_bs')
    image_option.set_format(image.format)
    image_option.set_platform(image.platform)
    image_option.set_url(image.url)
    image_option.set_mediaType(image.mediaType)
    image_option.set_backup_storage_uuid_list([disaster_bs_uuid])
    #Try to add image to disaster bs directly
    try:
        new_img = img_ops.add_image(image_option)
    except Exception, e:
        if str(e).find('forbidden') != -1:
            test_util.test_pass(
                'AddImage is forbidden in Disaster BS, Here is the expect error info: %s'
                % str(e))
Exemple #6
0
def test():
    global disaster_bs_uuid
    disaster_bs_dict = bs_ops.get_disaster_backup_storage_info(test_lib.deploy_config)
    name = disaster_bs_dict['name']
    description = disaster_bs_dict['description']
    hostname = disaster_bs_dict['hostname']
    url = disaster_bs_dict['url']
    username = disaster_bs_dict['username']
    password = disaster_bs_dict['password']
    sshport = disaster_bs_dict['port']
    #AddDisasterImageStoreBackupStorage 
    disaster_backup_storage = bs_ops.add_disaster_image_store_bs(url, hostname, username, password, sshport, name, description)
    disaster_bs_uuid = disaster_backup_storage.uuid
    #AttachBackupStorageToZone
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    bs_ops.attach_backup_storage(disaster_bs_uuid, zone_uuid)
    #Check if the bs has 'remote' system tag
    cond = res_ops.gen_query_conditions('resourceUuid', '=', disaster_bs_uuid)
    system_tag =  res_ops.query_resource(res_ops.SYSTEM_TAG, cond)[0]
    if system_tag.tag != "remote":
        test_util.test_fail("Here isn't 'remote' system tag for disaster bs")
     
    test_util.test_pass('Setup data protect bs image store success, the uuid is %s' %disaster_bs_uuid)
def test():
    bs_list = res_ops.query_resource(res_ops.IMAGE_STORE_BACKUP_STORAGE)
    images = res_ops.query_resource(res_ops.IMAGE)
    image_name_list = [img.name for img in images]
    for _bs in bs_list:
        bs_ops.delete_backup_storage(_bs.uuid)

    time.sleep(10)

    for bs in bs_list:
        bs_option = test_util.ImageStoreBackupStorageOption()
        bs_option.set_name(bs.name)
        bs_option.set_url(bs.url)
        bs_option.set_hostname(bs.hostname)
        bs_option.set_password('password')
        bs_option.set_sshPort(bs.sshPort)
        bs_option.set_username(bs.username)
        bs_option.set_import_images('true')
        bs_inv = bs_ops.create_image_store_backup_storage(bs_option)

        for zone_uuid in bs.attachedZoneUuids:
            bs_ops.attach_backup_storage(bs_inv.uuid, zone_uuid)

    time.sleep(10)

    _bs_list = res_ops.query_resource(res_ops.IMAGE_STORE_BACKUP_STORAGE)
    _images = res_ops.query_resource(res_ops.IMAGE)
    _image_name_list = [_img.name for _img in _images]

    if set([bs.name for bs in bs_list]) != set([_bs.name for _bs in _bs_list]):
        test_util.test_fail("Add ImageStore backupstorage failed")

    if set(image_name_list) != set(_image_name_list):
        test_util.test_fail("Import image failed when add ImageStore backupstorage")

    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass('Add ImageStore backupstorage with image importing test Pass')
def test():
    global disaster_bs_uuid
    disasterBsUrls = os.environ.get('disasterBsUrls')
    name = 'disaster_bs'
    description = 'backup storage for disaster'
    url = '/zstack_bs'
    sshport = 22
    hostname = disasterBsUrls.split('@')[1]
    username = disasterBsUrls.split(':')[0]
    password = disasterBsUrls.split('@')[0].split(':')[1]
    test_util.test_logger('Disaster bs server hostname is %s, username is %s, password is %s' %(hostname, username, password))
    #AddDisasterImageStoreBackupStorage
    disaster_backup_storage = bs_ops.add_disaster_image_store_bs(url, hostname, username, password, sshport, name, description)
    disaster_bs_uuid = disaster_backup_storage.uuid
    #AttachBackupStorageToZone
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    bs_ops.attach_backup_storage(disaster_bs_uuid, zone_uuid)
    #get a candidate image
    cond = res_ops.gen_query_conditions('name', '=', os.environ.get('imageStoreBackupStorageName'))
    local_bs_uuid = res_ops.query_resource(res_ops.BACKUP_STORAGE, cond)[0].uuid
    cond = res_ops.gen_query_conditions('backupStorageRefs.backupStorageUuid', '=', local_bs_uuid)
    image = res_ops.query_resource(res_ops.IMAGE, cond)[0]
    #setup image option
    image_option = test_util.ImageOption()
    image_option.set_name('add_image_to_data_protect_bs')
    image_option.set_format(image.format)
    image_option.set_platform(image.platform)
    image_option.set_url(image.url)
    image_option.set_mediaType(image.mediaType)
    image_option.set_backup_storage_uuid_list([disaster_bs_uuid])
    #Try to add image to disaster bs directly
    try:
        new_img = img_ops.add_image(image_option)
    except Exception,e:
        if unicode(e).encode("utf-8").find('禁止') != -1:
            test_util.test_pass('AddImage is forbidden in Disaster BS, Here is the expect error info: %s' %unicode(e).encode("utf-8"))
Exemple #9
0
def test():
    global disaster_bs_uuid
    disasterBsUrls = os.environ.get('disasterBsUrls')
    name = 'disaster_bs'
    description = 'backup storage for disaster'
    url = '/zstack_bs'
    sshport = 22
    hostname = disasterBsUrls.split('@')[1]
    username = disasterBsUrls.split(':')[0]
    password = disasterBsUrls.split('@')[0].split(':')[1]
    test_util.test_logger('Disaster bs server hostname is %s, username is %s, password is %s' %(hostname, username, password)) 
    #AddDisasterImageStoreBackupStorage 
    disaster_backup_storage = bs_ops.add_disaster_image_store_bs(url, hostname, username, password, sshport, name, description)
    disaster_bs_uuid = disaster_backup_storage.uuid
    #AttachBackupStorageToZone
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    bs_ops.attach_backup_storage(disaster_bs_uuid, zone_uuid)
    #Check if the bs has 'remote' system tag
    cond = res_ops.gen_query_conditions('resourceUuid', '=', disaster_bs_uuid)
    system_tag =  res_ops.query_resource(res_ops.SYSTEM_TAG, cond)[0]
    if system_tag.tag != "remote":
        test_util.test_fail("Here isn't 'remote' system tag for disaster bs")
     
    test_util.test_pass('Setup data protect bs image store success, the uuid is %s' %disaster_bs_uuid)
Exemple #10
0
def test():
    global disaster_bs_uuid
    global data_volume_uuid
    global image_uuid
    disasterBsUrls = os.environ.get('disasterBsUrls')
    name = 'disaster_bs'
    description = 'backup storage for disaster'
    url = '/zstack_bs'
    sshport = 22
    hostname = disasterBsUrls.split('@')[1]
    username = disasterBsUrls.split(':')[0]
    password = disasterBsUrls.split('@')[0].split(':')[1]
    test_util.test_logger(
        'Disaster bs server hostname is %s, username is %s, password is %s' %
        (hostname, username, password))
    #AddDisasterImageStoreBackupStorage
    disaster_backup_storage = bs_ops.add_disaster_image_store_bs(
        url, hostname, username, password, sshport, name, description)
    disaster_bs_uuid = disaster_backup_storage.uuid
    #AttachBackupStorageToZone
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    bs_ops.attach_backup_storage(disaster_bs_uuid, zone_uuid)
    #Create data volume
    cond = res_ops.gen_query_conditions(
        'name', '=', os.environ.get('nfsPrimaryStorageName'))
    primary_storage_uuid = res_ops.query_resource(res_ops.PRIMARY_STORAGE,
                                                  cond)[0].uuid
    disk_offering_uuid = res_ops.query_resource(res_ops.DISK_OFFERING)[0].uuid
    cond = res_ops.gen_query_conditions(
        'name', '=', os.environ.get('imageStoreBackupStorageName'))
    local_bs_uuid = res_ops.query_resource(res_ops.BACKUP_STORAGE,
                                           cond)[0].uuid
    volume_option = test_util.VolumeOption()
    volume_option.set_disk_offering_uuid(disk_offering_uuid)
    volume_option.set_name('data_volume_for_data_protect_test')
    volume_option.set_primary_storage_uuid(primary_storage_uuid)
    data_volume = vol_ops.create_volume_from_offering(volume_option)
    #CreateDataVolumeTemplateFromVolume
    data_volume_uuid = data_volume.uuid
    image_option = test_util.ImageOption()
    image_option.set_data_volume_uuid(data_volume_uuid)
    image_option.set_name('create_data_iso_to_image_store')
    image_option.set_backup_storage_uuid_list([disaster_bs_uuid])
    image = img_ops.create_data_volume_template(image_option)
    disaster_bs_image_uuid = image.uuid
    #Check if the image's media_type correct
    cond = res_ops.gen_query_conditions('uuid', '=', disaster_bs_image_uuid)
    media_type = res_ops.query_resource(res_ops.IMAGE, cond)[0].mediaType
    if media_type != 'DataVolumeTemplate':
        test_util.test_fail(
            'Wrong image media type, the expect is "DataVolumeTemplate", the real is "%s"'
            % media_type)
    #Check if create data volume with volume template success
    cond = res_ops.gen_query_conditions(
        'name', '=', os.environ.get('nfsPrimaryStorageName'))
    ps_uuid = res_ops.query_resource(res_ops.PRIMARY_STORAGE, cond)[0].uuid
    new_volume = vol_ops.create_volume_from_template(disaster_bs_image_uuid,
                                                     ps_uuid)
    vol_ops.delete_volume(new_volume.uuid)
    #Check if the system tag of the image in disaster bs is 'remote'
    cond = res_ops.gen_query_conditions('resourceUuid', '=',
                                        disaster_bs_image_uuid)
    system_tag = res_ops.query_resource(res_ops.SYSTEM_TAG, cond)[0]
    if system_tag.tag != "remote":
        test_util.test_fail(
            "Here isn't 'remote' system tag for image in data protect bs")
    #Check recovery data volume
    recovery_image = img_ops.recovery_image_from_image_store_backup_storage(
        local_bs_uuid, disaster_bs_uuid, disaster_bs_image_uuid)
    #Check the process status when recoverying image
    cond = res_ops.gen_query_conditions('resourceUuid', '=', local_bs_uuid)
    system_tag = res_ops.query_resource(res_ops.SYSTEM_TAG, cond)[0].tag
    status = system_tag.split('::')[7]
    if status not in ['running', 'success']:
        test_util.test_fail('Error status for recovery image, status: %s' %
                            status)
    #Check if recovery data volume success
    if recovery_image.backupStorageRefs[0].backupStorageUuid != local_bs_uuid:
        test_util.test_fail('Recovery image failed, image uuid is %s' %
                            recovery_image.uuid)
    if recovery_image.mediaType != 'DataVolumeTemplate':
        test_util.test_fail(
            'Wrong image media type after recovery, the expect is "DataVolumeTemplate", the real is "%s"'
            % media_type)
    image_uuid = recovery_image.uuid

    try:
        #Try to recovery the same image again, it's negative test
        recovery_image = img_ops.recovery_image_from_image_store_backup_storage(
            local_bs_uuid, disaster_bs_uuid, disaster_bs_image_uuid)
    except Exception, e:
        if unicode(e).encode("utf-8").find('包含') != -1:
            test_util.test_pass(
                'Try to recovery the same image again and get the error info expectly: %s'
                % unicode(e).encode("utf-8"))
def test():
    global disaster_bs_uuid
    global image_uuid
    global vm
    disasterBsUrls = os.environ.get('disasterBsUrls')
    name = 'disaster_bs'
    description = 'backup storage for disaster'
    url = '/zstack_bs'
    sshport = 22
    hostname = disasterBsUrls.split('@')[1]
    username = disasterBsUrls.split(':')[0]
    password = disasterBsUrls.split('@')[0].split(':')[1]
    test_util.test_logger('Disaster bs server hostname is %s, username is %s, password is %s' %(hostname, username, password))
    #AddDisasterImageStoreBackupStorage
    disaster_backup_storage = bs_ops.add_disaster_image_store_bs(url, hostname, username, password, sshport, name, description)
    disaster_bs_uuid = disaster_backup_storage.uuid
    #AttachBackupStorageToZone
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    bs_ops.attach_backup_storage(disaster_bs_uuid, zone_uuid)
    #Create vm
    primary_storage_uuid = res_ops.query_resource(res_ops.PRIMARY_STORAGE)[0].uuid
    disk_offering_uuid = res_ops.query_resource(res_ops.DISK_OFFERING)[0].uuid
    host_ip = res_ops.query_resource(res_ops.HOST)[0].managementIp
    cond = res_ops.gen_query_conditions('name', '=', os.environ.get('imageStoreBackupStorageName'))
    local_bs_uuid = res_ops.query_resource(res_ops.BACKUP_STORAGE, cond)[0].uuid

    cond = res_ops.gen_query_conditions('system', '=', False)
    l3network_uuid = res_ops.query_resource(res_ops.L3_NETWORK, cond)[0].uuid
    cond = res_ops.gen_query_conditions('type', '=', 'UserVm')
    instance_offering_uuid = res_ops.query_resource(res_ops.INSTANCE_OFFERING, cond)[0].uuid
    cond = res_ops.gen_query_conditions('backupStorage.uuid', '=', local_bs_uuid)
    cond = res_ops.gen_query_conditions('name', '=', 'other', cond)
    image_uuid = res_ops.query_resource(res_ops.IMAGE, cond)[0].uuid
    vm_option = test_util.VmOption()
    vm_option.set_instance_offering_uuid(instance_offering_uuid)
    vm_option.set_name('vm_for_data_protect_test')
    vm_option.set_l3_uuids([l3network_uuid])
    vm_option.set_image_uuid(image_uuid)
    vm = vm_ops.create_vm(vm_option)
    #CreateRootVolumeTemplateFromRootVolume
    root_volume_uuid = vm.rootVolumeUuid 
    image_option = test_util.ImageOption()
    image_option.set_root_volume_uuid(root_volume_uuid)
    image_option.set_name('create_root_image_to_image_store')
    image_option.set_backup_storage_uuid_list([disaster_bs_uuid])
    image = img_ops.create_root_volume_template(image_option)
    disaster_bs_image_uuid = image.uuid
    #Check if the system tag of the image in disaster bs is 'remote'
    cond = res_ops.gen_query_conditions('resourceUuid', '=', disaster_bs_image_uuid)
    system_tag = res_ops.query_resource(res_ops.SYSTEM_TAG, cond)[0]
    if system_tag.tag != "remote":
        test_util.test_fail("Here isn't 'remote' system tag for image in data protect bs")
    #Check if the image's media_type correct
    cond = res_ops.gen_query_conditions('uuid', '=', disaster_bs_image_uuid)
    media_type = res_ops.query_resource(res_ops.IMAGE, cond)[0].mediaType
    if media_type != 'RootVolumeTemplate':
        test_util.test_fail('Wrong image type, the expect is "RootVolumeTemplate", the real is "%s"' %media_type) 
    #Check recovery root volume
    recovery_image = img_ops.recovery_image_from_image_store_backup_storage(local_bs_uuid, disaster_bs_uuid, disaster_bs_image_uuid) 
    #Check the process status when recoverying image
    cond = res_ops.gen_query_conditions('resourceUuid', '=', local_bs_uuid)
    system_tag = res_ops.query_resource(res_ops.SYSTEM_TAG, cond)[0].tag
    status = system_tag.split('::')[7]
    if status not in ['running', 'success']:
        test_util.test_fail('Error status for recovery image, status: %s' %status)
    #Check if recovery root volume success
    if recovery_image.backupStorageRefs[0].backupStorageUuid != local_bs_uuid:
        test_util.test_fail('Recovery image failed, image uuid is %s' %recovery_image.uuid)
    if recovery_image.mediaType != 'RootVolumeTemplate':
        test_util.test_fail('Wrong image type after recovery, the expect is "RootVolumeTemplate", the real is "%s"' %recovery_image.mediaType)
    image_uuid = recovery_image.uuid

    try:
        #Try to recovery the same image again, it's negative test
        recovery_image = img_ops.recovery_image_from_image_store_backup_storage(local_bs_uuid, disaster_bs_uuid, disaster_bs_image_uuid)
        img_ops.delete_image(recovery_image.uuid)
    except Exception,e:
        if unicode(e).encode("utf-8").find('包含') != -1:
            test_util.test_pass('Try to recovery the same image again and get the error info expectly: %s' %unicode(e).encode("utf-8"))
def test():
    """
    """
    global image_uuid, backup_storage_inventory_uuid
    

    test_util.test_dsc('create image check timeout test')


    #modify default timeout value
    set_global_timeout_value()


    #create sftp server vm
    sftp_vm_offering = test_lib.lib_create_instance_offering(cpuNum = 1, \
            memorySize = 536870912, name = 'sftp_vm_instance_name', \
            volume_iops = None, volume_bandwidth = None, \
            net_outbound_bandwidth = vm_outbound_bandwidth, net_inbound_bandwidth = None)
    test_obj_dict.add_instance_offering(sftp_vm_offering)

    sftp_server_vm = test_stub.create_vm(vm_name = 'sftp-vm', image_name = 'img5h', \
            instance_offering_uuid = sftp_vm_offering.uuid)
    test_obj_dict.add_vm(sftp_server_vm)

    sftp_server_vm.check()


    #create backup storage on previous vm
    sftp_backup_storage_option = test_util.SftpBackupStorageOption()
    sftp_backup_storage_option.name = "bs_from_sftp_server"
    sftp_backup_storage_option.username = "******"
    sftp_backup_storage_option.hostname = sftp_server_vm.get_vm().vmNics[0].ip
    sftp_backup_storage_option.password = "******"
    sftp_backup_storage_option.sshPort = 22
    sftp_backup_storage_option.url = "/zstack_bs"

    backup_storage_inventory = bs_ops.create_backup_storage(sftp_backup_storage_option)
    if not backup_storage_inventory.uuid:
        backup_storage_inventory_uuid = backup_storage_inventory.uuid


    #create a new vm for creating image step
    vm_offering = test_lib.lib_create_instance_offering(cpuNum = 1, \
            memorySize = 536870912, name = 'image_create_test_vm', \
            volume_iops = None, volume_bandwidth = None, \
            net_outbound_bandwidth = None, net_inbound_bandwidth = None)
    test_obj_dict.add_instance_offering(vm_offering)

    vm = test_stub.create_vm(vm_name = 'timeout-test-vm', image_name = 'img5h', \
            instance_offering_uuid = vm_offering.uuid)
    test_obj_dict.add_vm(vm)

    vm.check()


    #populate vm with big files, the max http execute time is 30 mins.
    #the max disk throughput is 50MB/s, use dd to generate 10GB big file will cost ~2400s.
    #the total disk size after dd is 1.0+5=6GB
    #import tempfile
    #script_file = tempfile.NamedTemporaryFile(delete=False)
    #script_file.write("dd if=/dev/random of=test1 bs=1M count=5120")
    #script_file.close()
    #if not test_lib.lib_execute_shell_script_in_vm(vm.get_vm(), script_file.name, timeout=3600):    
    #    test_util.test_fail("generate 5GB big file failed")
    cmd = "dd if=/dev/random of=test1 bs=1M count=5120"
    ret, output, stderr = ssh.execute(cmd, vm.get_vm().vmNics[0].ip, "root", "password", False, 22)
    if ret != 0:
        test_util.test_fail("generate 5GB big file failed")
    

    #attach backup storage to zone
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    zone = res_ops.query_resource_with_num(res_ops.ZONE, cond, limit = 1)

    bs_ops.attach_backup_storage(backup_storage_inventory.uuid, zone[0].uuid)


    #invoke API with timeout
    vm_root_volume_inv = test_lib.lib_get_root_volume(vm.get_vm())
    root_volume_uuid = vm_root_volume_inv.uuid

    image_option1 = test_util.ImageOption()
    image_option1.set_root_volume_uuid(root_volume_uuid)
    image_option1.set_name('big_image_for_upload')
    image_option1.set_format('qcow2')
    image_option1.set_backup_storage_uuid_list([backup_storage_inventory.uuid])
    #image_option1.set_platform('Linux')
    #bs_type = backup_storage_list[0].type


    #this API can only be invoke when vm is stopped
    vm.stop()
    vm.check()


    time1 = time.time()
    image = img_ops.create_root_volume_template(image_option1)
    time2 = time.time()

    image_uuid = image.uuid

    cost_time = time2 - time1
    test_util.test_logger("start time: %s"  % (time1))
    test_util.test_logger("end time: %s"  % (time2))
    test_util.test_logger("total time: %s"  % (cost_time))


    if cost_time > int(timeout_value):
        test_util.test_fail("The test create image cost time is greater than %s hours: \
%s, which does not meet the test criterial." % (str(timeout_value), cost_time))
def test():
    test_util.test_dsc('Create Scheduler Trigger and Scheduler Job')
    cond = res_ops.gen_query_conditions('type', '=', 'ImageStoreBackupStorage')
    bs = res_ops.query_resource(res_ops.BACKUP_STORAGE, cond)
    if not bs :
        test_util.test_skip('Not find image store type backup storage.')
    image_name = os.environ.get('imageName_net')
    l3_name = os.environ.get('l3PublicNetworkName')
    remote_bs_vm = test_stub.create_vm('remote_bs_vm', image_name, l3_name)
    test_obj_dict.add_vm(remote_bs_vm)
    image_name = os.environ.get('imageName_s')
    l3_name = os.environ.get('l3VlanNetworkName1')
    test_vm = test_stub.create_vm('test-vm', image_name, l3_name)
    test_obj_dict.add_vm(test_vm)
    add_local_bs_tag = tag_ops.create_system_tag('ImageStoreBackupStorageVO', bs[0].uuid,'allowbackup')
    #wait for vm start up 
    test_lib.lib_wait_target_up(remote_bs_vm.vm.vmNics[0].ip, '22', 90)
    remote_bs = test_stub.create_image_store_backup_storage('remote_bs', remote_bs_vm.vm.vmNics[0].ip, 'root', 'password', '/zstack_bs', '22')
    add_remote_bs = tag_ops.create_system_tag('ImageStoreBackupStorageVO', remote_bs.uuid,'remotebackup')
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    bs_ops.attach_backup_storage(remote_bs.uuid, zone_uuid)    
    parameters= {"retentionType":"Count","retentionValue":"1","backupStorageUuids":bs[0].uuid,"remoteBackupStorageUuid":""}
    schd_job = schd_ops.create_scheduler_job('backup_database_scheduler', 'backup_database_scheduler', '7ae6456c0b01324dae6d4bef358a5772', 'databaseBackup',parameters=parameters)
    schd_trigger = schd_ops.create_scheduler_trigger('backup_database_schedule',type='cron', cron='0 * * ? * *')
    schd_ops.add_scheduler_job_to_trigger(schd_trigger.uuid, schd_job.uuid)
    #wait for 60s *2
    time.sleep(120)
 
    db_backup1 = schd_ops.query_db_backup()
    if len(db_backup1) != 1:
        test_util.test_fail('there sholuld be 1 db backup,but now there are %s' % len(db_backup1))   
    db_backup2 = schd_ops.get_db_backup_from_imagestore(url = 'ssh://*****:*****@%s:22/zstack_bs' % bs[0].hostname)
    if len(db_backup2.backups) != 1:
        test_util.test_fail('there sholuld be 1 db backup,but now there are %s' % len(db_backup2.backups))

    db_url = schd_ops.export_db_backup_from_bs(bs[0].uuid, db_backup1[0].uuid) 
    test_util.test_dsc('export database backup successfully,url is %s' % db_url.databaseBackupUrl)
    schd_ops.sync_db_from_imagestore_bs(remote_bs.uuid, bs[0].uuid, db_backup1[0].uuid)
    test_vm.destroy()

    test_util.test_dsc('Recover db From BackupStorage')
    backupStorageUrl = 'ssh://*****:*****@%s:22/zstack_bs' % remote_bs_vm.vm.vmNics[0].ip
    recover_db = schd_ops.recover_db_from_backup(backupStorageUrl = backupStorageUrl, backupInstallPath = db_backup2.backups[0].installPath, mysqlRootPassword='******')
    #wait for db recover
    time.sleep(60)
    cond = res_ops.gen_query_conditions('name', '=', 'test-vm')
    vm = res_ops.query_resource(res_ops.VM_INSTANCE, cond)
    if not vm:
        test_util.test_fail('there sholuld be a vm after recovering db from remote backup bs')
    
    schd_ops.change_scheduler_state(schd_job.uuid, 'disable')
    db_backup3 = schd_ops.query_db_backup()
    zone = res_ops.query_resource(res_ops.ZONE)[0]
    zone_ops.delete_zone(zone.uuid)
    recover_db = schd_ops.recover_db_from_backup(uuid=db_backup3[0].uuid, mysqlRootPassword='******')
    #wait for db recover
    time.sleep(60)
    vm = res_ops.query_resource(res_ops.VM_INSTANCE, cond)
    if not vm:
        test_util.test_fail('there sholuld be a vm after recovering db from local backup bs')

    test_util.test_dsc('Clear env')
    schd_ops.del_scheduler_job(schd_job.uuid)
    schd_ops.del_scheduler_trigger(schd_trigger.uuid)
    tag_ops.delete_tag(add_local_bs_tag.uuid)
    bs_ops.delete_backup_storage(remote_bs.uuid)    
    bs_ops.reclaim_space_from_bs(bs[0].uuid)
    remote_bs_vm.destroy()
    test_vm.destroy()
def test():
    """
    """
    global image_uuid, backup_storage_inventory_uuid

    test_util.test_dsc('create image check timeout test')

    #modify default timeout value
    set_global_timeout_value()

    #create sftp server vm
    sftp_vm_offering = test_lib.lib_create_instance_offering(cpuNum = 1, \
            cpuSpeed = 16, memorySize = 536870912, name = 'sftp_vm_instance_name', \
            volume_iops = None, volume_bandwidth = None, \
            net_outbound_bandwidth = vm_outbound_bandwidth, net_inbound_bandwidth = None)
    test_obj_dict.add_instance_offering(sftp_vm_offering)

    sftp_server_vm = test_stub.create_vm(vm_name = 'sftp-vm', image_name = 'img5h', \
            instance_offering_uuid = sftp_vm_offering.uuid)
    test_obj_dict.add_vm(sftp_server_vm)

    sftp_server_vm.check()

    #create backup storage on previous vm
    sftp_backup_storage_option = test_util.SftpBackupStorageOption()
    sftp_backup_storage_option.name = "bs_from_sftp_server"
    sftp_backup_storage_option.username = "******"
    sftp_backup_storage_option.hostname = sftp_server_vm.get_vm().vmNics[0].ip
    sftp_backup_storage_option.password = "******"
    sftp_backup_storage_option.sshPort = 22
    sftp_backup_storage_option.url = "/zstack_bs"

    backup_storage_inventory = bs_ops.create_backup_storage(
        sftp_backup_storage_option)
    if not backup_storage_inventory.uuid:
        backup_storage_inventory_uuid = backup_storage_inventory.uuid

    #create a new vm for creating image step
    vm_offering = test_lib.lib_create_instance_offering(cpuNum = 1, \
            cpuSpeed = 16, memorySize = 536870912, name = 'image_create_test_vm', \
            volume_iops = None, volume_bandwidth = None, \
            net_outbound_bandwidth = None, net_inbound_bandwidth = None)
    test_obj_dict.add_instance_offering(vm_offering)

    vm = test_stub.create_vm(vm_name = 'timeout-test-vm', image_name = 'img5h', \
            instance_offering_uuid = vm_offering.uuid)
    test_obj_dict.add_vm(vm)

    vm.check()

    #populate vm with big files, the max http execute time is 30 mins.
    #the max disk throughput is 50MB/s, use dd to generate 10GB big file will cost ~2400s.
    #the total disk size after dd is 1.0+5=6GB
    #import tempfile
    #script_file = tempfile.NamedTemporaryFile(delete=False)
    #script_file.write("dd if=/dev/random of=test1 bs=1M count=5120")
    #script_file.close()
    #if not test_lib.lib_execute_shell_script_in_vm(vm.get_vm(), script_file.name, timeout=3600):
    #    test_util.test_fail("generate 5GB big file failed")
    cmd = "dd if=/dev/random of=test1 bs=1M count=5120"
    ret, output, stderr = ssh.execute(cmd,
                                      vm.get_vm().vmNics[0].ip, "root",
                                      "password", False, 22)
    if ret != 0:
        test_util.test_fail("generate 5GB big file failed")

    #attach backup storage to zone
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    zone = res_ops.query_resource_with_num(res_ops.ZONE, cond, limit=1)

    bs_ops.attach_backup_storage(backup_storage_inventory.uuid, zone[0].uuid)

    #invoke API with timeout
    vm_root_volume_inv = test_lib.lib_get_root_volume(vm.get_vm())
    root_volume_uuid = vm_root_volume_inv.uuid

    image_option1 = test_util.ImageOption()
    image_option1.set_root_volume_uuid(root_volume_uuid)
    image_option1.set_name('big_image_for_upload')
    image_option1.set_format('qcow2')
    image_option1.set_backup_storage_uuid_list([backup_storage_inventory.uuid])
    #image_option1.set_platform('Linux')
    #bs_type = backup_storage_list[0].type

    #this API can only be invoke when vm is stopped
    vm.stop()
    vm.check()

    time1 = time.time()
    image = img_ops.create_root_volume_template(image_option1)
    time2 = time.time()

    image_uuid = image.uuid

    cost_time = time2 - time1
    test_util.test_logger("start time: %s" % (time1))
    test_util.test_logger("end time: %s" % (time2))
    test_util.test_logger("total time: %s" % (cost_time))

    if cost_time > int(timeout_value):
        test_util.test_fail(
            "The test create image cost time is greater than %s hours: \
%s, which does not meet the test criterial." % (str(timeout_value), cost_time))