def test():
    global new_offering_uuid
    test_util.test_dsc('Test VM disk bandwidth QoS by 20MB')

    #unit is KB
    volume_bandwidth = 5*1024*1024
    new_offering = test_lib.lib_create_instance_offering(volume_bandwidth = volume_bandwidth)

    new_offering_uuid = new_offering.uuid

    vm = test_stub.create_vm(vm_name = 'vm_volume_qos', \
            instance_offering_uuid = new_offering.uuid)
    test_obj_dict.add_vm(vm)

    vm.check()
    vm_inv = vm.get_vm()
    test_stub.make_ssh_no_password(vm_inv)
    test_stub.install_fio(vm_inv)
    test_stub.test_fio_bandwidth(vm_inv, volume_bandwidth)
    if vm_ops.get_vm_disk_qos(test_lib.lib_get_root_volume(vm_inv).uuid).volumeBandwidth != volume_bandwidth:
        test_util.test_fail('Retrieved disk qos not match')
    vm_ops.set_vm_disk_qos(test_lib.lib_get_root_volume(vm_inv).uuid, volume_bandwidth/2)
    test_stub.test_fio_bandwidth(vm_inv, volume_bandwidth/2)
    vm_ops.del_vm_disk_qos(test_lib.lib_get_root_volume(vm_inv).uuid)
    if test_stub.test_fio_bandwidth(vm_inv, volume_bandwidth/2, raise_exception=False):
        test_util.test_fail('disk qos is not expected to have limit after qos setting is deleted')
    vm_ops.delete_instance_offering(new_offering_uuid)
    test_lib.lib_robot_cleanup(test_obj_dict)

    test_util.test_pass('VM Network QoS Test Pass')
def test():
    global vm
    vm_creation_option = test_util.VmOption()
    image_name = os.environ.get('imageName_net')
    l3_name = os.environ.get('l3VlanNetworkName1')
    vm = test_stub.create_vm("test_resize_vm", image_name, l3_name)
    test_obj_dict.add_vm(vm)
    vm.check()
    vm.stop() 
    vm.check()

    vol_size = test_lib.lib_get_root_volume(vm.get_vm()).size
    volume_uuid = test_lib.lib_get_root_volume(vm.get_vm()).uuid
    set_size = 1024*1024*1024*5

    snapshots = test_obj_dict.get_volume_snapshot(volume_uuid)
    snapshots.set_utility_vm(vm)
    snapshots.create_snapshot('create_snapshot1')
    snapshots.check()

    vol_ops.resize_volume(volume_uuid, set_size)
    vm.update()
    vol_size_after = test_lib.lib_get_root_volume(vm.get_vm()).size
    if set_size != vol_size_after:
        test_util.test_fail('Resize Root Volume failed, size = %s' % vol_size_after)

    snapshots.delete()
    test_obj_dict.rm_volume_snapshot(snapshots)
    test_lib.lib_error_cleanup(test_obj_dict)
    test_util.test_pass('Resize VM Snapshot Test Success')
def test():
   test_util.test_dsc('Test Change VM Image In Multihosts Env')
   global vm
   image = test_lib.lib_get_image_by_name("centos")
   vm = test_stub.create_vm(image_uuid=image.uuid)
   last_l3network_uuid = test_lib.lib_get_l3s_uuid_by_vm(vm.get_vm())
   last_primarystorage_uuid = test_lib.lib_get_root_volume(vm.get_vm()).primaryStorageUuid
   last_host_uuid = test_lib.lib_get_vm_last_host(vm.get_vm()).uuid
   image_uuid = test_lib.lib_get_image_by_name("image_for_sg_test").uuid
   vm_uuid = vm.get_vm().uuid
   host_ops.change_host_state(host_uuid = last_host_uuid, state = 'disable')
   vm_ops.stop_vm(vm_uuid)
   ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
   #Disable vm's host.If ps is shared storage,the vm will be started on another host that meets the conditions and the operation of changing vm image will success.
   if ps.type != 'LocalStorage':
      vm_ops.change_vm_image(vm_uuid,image_uuid)
      vm_ops.start_vm(vm_uuid)
      #check whether the network config has changed
      l3network_uuid_after = test_lib.lib_get_l3s_uuid_by_vm(vm.get_vm())
      if l3network_uuid_after != last_l3network_uuid:
         test_util.test_fail('Change VM Image Failed.The Network config has changed.')
      #check whether primarystorage has changed
      primarystorage_uuid_after = test_lib.lib_get_root_volume(vm.get_vm()).primaryStorageUuid
      if primarystorage_uuid_after != last_primarystorage_uuid:
         test_util.test_fail('Change VM Image Failed.Primarystorage has changed.')
      vm.destroy()
      test_util.test_pass('Change Vm Image Test Success In Multihosts Env Success')
   #Disable vm's host.If ps is local storage,the operation of changing vm image will fail.  
   else:
      try:
         vm_ops.change_vm_image(vm_uuid, image_uuid)
      except:
         test_util.test_pass('Change Vm Image Test Success In Multihosts Env Success')
   test_util.test_fail('Test Change VM Image In Multihosts Env Success Failed')
예제 #4
0
def test():
    global vm
    vm_creation_option = test_util.VmOption()
    image_name = os.environ.get('imageName_net')
    l3_name = os.environ.get('l3VlanNetworkName1')
    vm = test_stub.create_vm("test_resize_vm", image_name, l3_name)
    vm.check()
    vm.stop() 
    vm.check()

    vol_size = test_lib.lib_get_root_volume(vm.get_vm()).size
    volume_uuid = test_lib.lib_get_root_volume(vm.get_vm()).uuid
    set_size = 1024*1024*1024*5
    vol_ops.resize_volume(volume_uuid, set_size)
    vm.update()
    vol_size_after = test_lib.lib_get_root_volume(vm.get_vm()).size
    if set_size != vol_size_after:
        test_util.test_fail('Resize Root Volume failed, size = %s' % vol_size_after)

    vm.start()
    set_size = 1024*1024*1024*6   
    vol_ops.resize_volume(volume_uuid, set_size)
    vm.update()
    vol_size_after = test_lib.lib_get_root_volume(vm.get_vm()).size
    if set_size != vol_size_after:
        test_util.test_fail('Resize Root Volume failed, size = %s' % vol_size_after)    

    vm.destroy()
    test_util.test_pass('Resize VM Test Success')
def test():
    global vm
    vm_creation_option = test_util.VmOption()
    image_name = os.environ.get('imageName_net')
    l3_name = os.environ.get('l3VlanNetworkName1')
    vm = test_stub.create_vm("test_resize_vm", image_name, l3_name)
    test_obj_dict.add_vm(vm)
    vm.check()

    vol_size = test_lib.lib_get_root_volume(vm.get_vm()).size
    volume_uuid = test_lib.lib_get_root_volume(vm.get_vm()).uuid
    set_size = 1024*1024*1024*5
    vol_ops.resize_volume(volume_uuid, set_size)
    vm.update()
    vol_size_after = test_lib.lib_get_root_volume(vm.get_vm()).size
    if set_size != vol_size_after:
        test_util.test_fail('Resize Root Volume failed, size = %s' % vol_size_after)

    bs_list = test_lib.lib_get_backup_storage_list_by_vm(vm.vm)
    image_option = test_util.ImageOption()
    image_option.set_root_volume_uuid(volume_uuid)
    image_option.set_name('image_resize_template')
    image_option.set_backup_storage_uuid_list([bs_list[0].uuid])
    image = img_ops.create_root_volume_template(image_option)

    new_vm = test_stub.create_vm("test_vm_after_resize", 'image_resize_template', l3_name)
    test_obj_dict.add_vm(new_vm)
    new_vm.check()
    new_volume_uuid = test_lib.lib_get_root_volume_uuid(new_vm.get_vm())
    vol_size_after = test_lib.lib_get_root_volume(new_vm.get_vm()).size
    if set_size != vol_size_after:
        test_util.test_fail('Resize Root Volume failed, size = %s' % vol_size_after) 
    test_lib.lib_error_cleanup(test_obj_dict)
    test_util.test_pass('Create Template for Resize VM Test Success')
def test():
    global vm
    create_vm_option = test_util.VmOption()
    create_vm_option.set_rootVolume_systemTags(["volumeProvisioningStrategy::ThinProvisioning"])
    create_vm_option.set_name('test_resize_vm_root_volume')
    vm = test_lib.lib_create_vm(create_vm_option)
    vm.check()
    vm.stop() 
    vm.check()

    vol_size = test_lib.lib_get_root_volume(vm.get_vm()).size
    volume_uuid = test_lib.lib_get_root_volume(vm.get_vm()).uuid
    set_size = 1024*1024*1024*5
    vol_ops.resize_volume(volume_uuid, set_size)
    vm.update()
    vol_size_after = test_lib.lib_get_root_volume(vm.get_vm()).size
    if set_size != vol_size_after:
        test_util.test_fail('Resize Root Volume failed, size = %s' % vol_size_after)

    vm.start()
    set_size = 1024*1024*1024*6
    vol_ops.resize_volume(volume_uuid, set_size)
    vm.update()
    vol_size_after = test_lib.lib_get_root_volume(vm.get_vm()).size
    if set_size != vol_size_after:
        test_util.test_fail('Resize Root Volume failed, size = %s' % vol_size_after)

    vm.destroy()
    test_util.test_pass('Resize VM Test Success')
def test():
    if res_ops.query_resource(res_ops.SFTP_BACKUP_STORAGE):
        test_util.test_skip("sftp backupstorage doesn't support for clone test. Skip test")
    global vm
    vm_creation_option = test_util.VmOption()
    image_name = os.environ.get('imageName_net')
    l3_name = os.environ.get('l3VlanNetworkName1')
    vm = test_stub.create_vm("test_resize_vm", image_name, l3_name)
    test_obj_dict.add_vm(vm)
    vm.check()

    vol_size = test_lib.lib_get_root_volume(vm.get_vm()).size
    volume_uuid = test_lib.lib_get_root_volume(vm.get_vm()).uuid
    set_size = 1024*1024*1024*5
    vol_ops.resize_volume(volume_uuid, set_size)
    vm.update()
    vol_size_after = test_lib.lib_get_root_volume(vm.get_vm()).size
    if set_size != vol_size_after:
        test_util.test_fail('Resize Root Volume failed, size = %s' % vol_size_after)

    new_vm = vm.clone(['vm_clone'])[0]
    test_obj_dict.add_vm(new_vm)
    new_volume_uuid = test_lib.lib_get_root_volume_uuid(new_vm.get_vm())
    vol_size_after = test_lib.lib_get_root_volume(new_vm.get_vm()).size
    if set_size != vol_size_after:
        test_util.test_fail('Resize Root Volume failed, size = %s' % vol_size_after) 
    test_lib.lib_error_cleanup(test_obj_dict)
    test_util.test_pass('Resize VM Snapshot Test Success')
def resize_rvol(vm_obj):
    vol_size = test_lib.lib_get_root_volume(vm_obj.get_vm()).size
    volume_uuid = test_lib.lib_get_root_volume(vm_obj.get_vm()).uuid
    set_size = 1024 * 1024 * 1024 + int(vol_size)
    vol_ops.resize_volume(volume_uuid, set_size)
    vm_obj.update()
    vol_size_after = test_lib.lib_get_root_volume(vm_obj.get_vm()).size
    # if set_size != vol_size_after:
    #     test_util.test_fail('Resize Root Volume failed, size = %s' % vol_size_after)
    # vm_obj.check()
    test_lib.lib_wait_target_up(vm_obj.get_vm().vmNics[0].ip, 22, 300)
def test():
    global new_offering_uuid
    test_util.test_dsc('Test VM disk bandwidth QoS by 20MB')

    #unit is KB
    write_bandwidth = 5*1024*1024
    read_bandwidth = 10*1024*1024
    new_offering = test_lib.lib_create_instance_offering(read_bandwidth=read_bandwidth, write_bandwidth=write_bandwidth)

    new_offering_uuid = new_offering.uuid

    vm = test_stub.create_vm(vm_name = 'vm_volume_qos', \
            instance_offering_uuid = new_offering.uuid)
    test_obj_dict.add_vm(vm)

    vm.check()
    vm_inv = vm.get_vm()
    test_stub.make_ssh_no_password(vm_inv)
    test_stub.install_fio(vm_inv)
#     test_stub.test_fio_bandwidth(vm_inv, read_bandwidth)
    vm_ops.set_vm_disk_qos(test_lib.lib_get_root_volume(vm_inv).uuid, read_bandwidth*2)
    if vm_ops.get_vm_disk_qos(test_lib.lib_get_root_volume(vm_inv).uuid).volumeBandwidth != read_bandwidth*2:
        test_util.test_fail('Retrieved disk qos not match')
    if vm_ops.get_vm_disk_qos(test_lib.lib_get_root_volume(vm_inv).uuid).volumeBandwidthRead == read_bandwidth:
        test_util.test_fail('read qos must be cleared after set total qos')
    if vm_ops.get_vm_disk_qos(test_lib.lib_get_root_volume(vm_inv).uuid).volumeBandwidthWrite == write_bandwidth:
        test_util.test_fail('write qos must be cleared after set total qos')

    vm_ops.set_vm_disk_qos(test_lib.lib_get_root_volume(vm_inv).uuid, read_bandwidth*2, 'read')
    if vm_ops.get_vm_disk_qos(test_lib.lib_get_root_volume(vm_inv).uuid).volumeBandwidthRead != read_bandwidth*2:
        test_util.test_fail('Retrieved disk qos not match')

    test_stub.test_fio_bandwidth(vm_inv, read_bandwidth*2, '/dev/vda')
    vm_ops.del_vm_disk_qos(test_lib.lib_get_root_volume(vm_inv).uuid, mode='all')

    if test_stub.test_fio_bandwidth(vm_inv, read_bandwidth, '/dev/vda', raise_exception=False):
        test_util.test_fail('disk qos is not expected to have limit after qos setting is deleted')

    vm_ops.set_vm_disk_qos(test_lib.lib_get_root_volume(vm_inv).uuid, write_bandwidth*2, 'write')
    if vm_ops.get_vm_disk_qos(test_lib.lib_get_root_volume(vm_inv).uuid).volumeBandwidthWrite != write_bandwidth*2:
        test_util.test_fail('Retrieved disk qos not match')

    test_stub.test_fio_bandwidth(vm_inv, write_bandwidth*2)
    vm_ops.del_vm_disk_qos(test_lib.lib_get_root_volume(vm_inv).uuid, mode='all')

    if test_stub.test_fio_bandwidth(vm_inv, write_bandwidth, raise_exception=False):
        test_util.test_fail('disk qos is not expected to have limit after qos setting is deleted')


    vm_ops.delete_instance_offering(new_offering_uuid)
    test_lib.lib_robot_cleanup(test_obj_dict)

    test_util.test_pass('VM Network QoS Test Pass')
def test():
    ps_inv = res_ops.query_resource(res_ops.PRIMARY_STORAGE)
    ceph_ps = [ps for ps in ps_inv if ps.type == 'Ceph']
    if not ceph_ps:
        test_util.test_skip('Skip test as there is not Ceph primary storage')
    
    flavor = case_flavor[os.getenv('CASE_FLAVOR')]
    
    if flavor['shared_vm']:
        multi_ps.create_vm(image_name="ttylinux", ps_type="SharedBlock")
    else:
        multi_ps.create_vm(image_name="ttylinux", ps_type="Ceph")
    multi_ps.create_data_volume(vms=multi_ps.vm, ps_type='SharedBlock')
    multi_ps.create_data_volume(vms= multi_ps.vm, ps_type='Ceph')

    vm = multi_ps.vm[0]
    last_data_volumes_uuids = []
    last_data_volumes = test_lib.lib_get_data_volumes(vm.get_vm())
    for data_volume in last_data_volumes:
        last_data_volumes_uuids.append(data_volume.uuid)
    last_l3network_uuid = test_lib.lib_get_l3s_uuid_by_vm(vm.get_vm())
    last_primarystorage_uuid = test_lib.lib_get_root_volume(vm.get_vm()).primaryStorageUuid
    vm.stop()

    if flavor['imagestore_bs']:
        image_uuid = test_lib.lib_get_image_by_name("image_for_sg_test", bs_type="ImageStoreBackupStorage").uuid
    else:
        image_uuid = test_lib.lib_get_image_by_name("image_for_sg_test", bs_type="Ceph").uuid
    vm_ops.change_vm_image(vm.get_vm().uuid, image_uuid)
    vm.start()
    vm.update()

    #check whether the vm is running successfully
    if not test_lib.lib_wait_target_up(vm.get_vm().vmNics[0].ip, 22, 1200):
        test_util.test_fail('VM:%s is not startup in 1200 seconds.' % vm.get_vm().uuid)
    #check whether data volumes attached to the vm has changed
    now_data_volumes_uuids = []
    now_data_volumes = test_lib.lib_get_data_volumes(vm.get_vm())
    for data_volume in now_data_volumes:
        now_data_volumes_uuids.append(data_volume.uuid)
    if set(last_data_volumes_uuids) != set(now_data_volumes_uuids):
        test_util.test_fail('Change Vm Image Failed.Data volumes changed.')
    #check whether the network config has changed
    now_l3network_uuid = test_lib.lib_get_l3s_uuid_by_vm(vm.get_vm())
    if now_l3network_uuid != last_l3network_uuid:
        test_util.test_fail('Change VM Image Failed.The Network config has changed.')
    #check whether primarystorage has changed
    now_primarystorage_uuid = test_lib.lib_get_root_volume(vm.get_vm()).primaryStorageUuid
    if now_primarystorage_uuid != last_primarystorage_uuid:
        test_util.test_fail('Change VM Image Failed.Primarystorage has changed.')

    test_util.test_pass('Change Vm Image Test Success')
def test():
    vm = test_stub.create_vm(vm_name = vn_prefix)
    test_obj_dict.add_vm(vm)
    backup_storage_list = test_lib.lib_get_backup_storage_list_by_vm(vm.vm)
    for bs in backup_storage_list:
        if bs.type in [inventory.IMAGE_STORE_BACKUP_STORAGE_TYPE, inventory.CEPH_BACKUP_STORAGE_TYPE]:
            break
    else:
        vm.destroy()
        test_util.test_skip('Not find image store or ceph type backup storage.')

    vm1 = test_stub.create_vm(vm_name = vn_prefix)
    test_obj_dict.add_vm(vm1)
    vm_root_volume_inv = test_lib.lib_get_root_volume(vm.get_vm())
    test_util.test_dsc('create snapshot and check')
    snapshots = test_obj_dict.get_volume_snapshot(vm_root_volume_inv.uuid)
    snapshots.set_utility_vm(vm1)
    snapshots.create_snapshot('create_root_snapshot1')
    snapshot1 = snapshots.get_current_snapshot()
    snapshots.create_snapshot('create_root_snapshot2')

    snapshots.delete_snapshot(snapshot1)
    vm.reboot()
    new_vm1 = vm.clone(vm_name1)[0]
    test_obj_dict.add_vm(new_vm1)
    vm.destroy()

    new_vm1.check()

    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass('Clone VM Test with snapshot operations Success')
def test():
    ps_env = test_stub.PSEnvChecker()

    ps, another = ps_env.get_two_ps()
    disk_offering_uuids = [random.choice(res_ops.get_resource(res_ops.DISK_OFFERING)).uuid]

    vm = test_stub.create_iso_vm_with_random_offering(vm_name='iso_vm', l3_name='l3VlanNetworkName1')
    test_obj_dict.add_vm(vm)

    for root_volume_ps_uuid in [None, ps.uuid]:
        for data_vol_ps_uuid in [None, another.uuid]:
            if ps_env.is_local_shared_env or ps_env.is_sb_ceph_env:
                if type(root_volume_ps_uuid) != type(data_vol_ps_uuid):
                    continue
            vm = test_stub.create_iso_vm_with_random_offering(vm_name='test_iso_vm',
                                                              disk_offering_uuids=disk_offering_uuids if data_vol_ps_uuid else None,
                                                              ps_uuid=root_volume_ps_uuid,
                                                              l3_name='l3VlanNetworkName1',
                                                              system_tags=['primaryStorageUuidForDataVolume::{}'.format(data_vol_ps_uuid)] if data_vol_ps_uuid else None)
            if root_volume_ps_uuid:
                root_vol = test_lib.lib_get_root_volume(vm.get_vm())
                assert root_vol.primaryStorageUuid == ps.uuid

            if data_vol_ps_uuid:
                data_vol_list = [vol for vol in vm.get_vm().allVolumes if vol.type != 'Root']
                for data_vol in data_vol_list:
                    assert data_vol.primaryStorageUuid == another.uuid

            test_obj_dict.add_vm(vm)

    test_util.test_pass('Multi PrimaryStorage Test Pass')
def test():
    global vm
    loop = 20
    count = 0
    vm_creation_option = test_util.VmOption()
    image_name = os.environ.get('imageName_net')
    l3_name = os.environ.get('l3VlanNetworkName1')
    vm = test_stub.create_vm("test_libvirt_snapshot_vm", image_name, l3_name)
    test_obj_dict.add_vm(vm)
    vm.check()

    volume_uuid = test_lib.lib_get_root_volume(vm.get_vm()).uuid
     
    snapshots = test_obj_dict.get_volume_snapshot(volume_uuid)
    snapshots.set_utility_vm(vm)
    while count <= loop:
        sp_name = "create_snapshot" + str(count)
        snapshots.create_snapshot(sp_name)
        snapshots.check()
        count += 1

    host = test_lib.lib_find_host_by_vm(vm.get_vm())
    result = test_lib.lib_execute_ssh_cmd(host.managementIp, 'root', 'password', 'service libvirtd restart')

    time.sleep(600)

    vm.check()
    snapshots.check()

    snapshots.delete()
    test_obj_dict.rm_volume_snapshot(snapshots)
    test_lib.lib_error_cleanup(test_obj_dict)
    test_util.test_pass('Libvirt restart with snapshot passed')
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')
예제 #15
0
def compare(ps, vm, backup):
    test_util.test_logger("-----------------compare----------------")
    # find vm_host
    host = test_lib.lib_find_host_by_vm(vm.vm)
    bs = res_ops.query_resource(res_ops.BACKUP_STORAGE)[0]

    root_volume = test_lib.lib_get_root_volume(vm.get_vm())
    vm_path = root_volume.installPath
    if ps.type == "SharedBlock":
        vm_path = "/dev/" + root_volume.installPath.split("/")[2] + "/" + root_volume.installPath.split("/")[3]
    test_util.test_logger(vm_path)

    name = backup.backupStorageRefs[0].installPath.split("/")[2]
    id = backup.backupStorageRefs[0].installPath.split("/")[3]
    # compare vm_root_volume & image
    cmd = "mkdir /root/%s;" \
          "/usr/local/zstack/imagestore/bin/zstcli " \
          "-rootca=/var/lib/zstack/imagestorebackupstorage/package/certs/ca.pem " \
          "-url=%s:8000 " \
          "pull -installpath /root/%s/old.qcow2 %s:%s;" \
          "qemu-img compare %s /root/%s/old.qcow2;" % (id, bs.hostname, id, name, id, vm_path, id)
    # clean image
    result = test_lib.lib_execute_ssh_cmd(host.managementIp, "root", "password", cmd, timeout=300)
    if result != "Images are identical.\n":
        test_util.test_fail("compare vm_root_volume & image created by backup")
def test():
    test_util.test_dsc('Create test vm and check')
    vm = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm)

    vm.stop()
    r_volume = zstack_volume_header.ZstackTestVolume()
    r_volume.set_volume(test_lib.lib_get_root_volume(vm.get_vm()))
    r_volume.set_state(volume_header.ATTACHED)

    test_util.test_dsc('Create volume template and check')
    bs_list = test_lib.lib_get_backup_storage_list_by_vm(vm.get_vm())
    vol_tmpt = r_volume.create_template([bs_list[0].uuid], 'new_data_template')
    test_obj_dict.add_image(vol_tmpt)

    test_util.test_dsc('Try to use data vol_tempt to Create VM. ')
    vm_option = vm.get_creation_option()
    vm_option.set_image_uuid(vol_tmpt.get_image().uuid)
    vm2 = zstack_vm_header.ZstackTestVm()
    vm2.set_creation_option(vm_option)
    try:
        vm2.create()
    except:
        test_util.test_logger('Expected exception, when creating VM by using Data Volume Template as root image')
    else:
        test_util.test_fail('VM can use data volume template to create VM')

    vol_tmpt.delete()
    test_obj_dict.rm_image(vol_tmpt)
    vm.destroy()
    test_util.test_pass('Create VM from Data Volume Template Test Success')
def back_up(vm_obj):
     global backup
     bs = res_ops.query_resource(res_ops.BACKUP_STORAGE)[0]
     backup_option = test_util.BackupOption()
     backup_option.set_name("test_compare")
     backup_option.set_volume_uuid(test_lib.lib_get_root_volume(vm_obj.get_vm()).uuid)
     backup_option.set_backupStorage_uuid(bs.uuid)
     backup = vol_ops.create_backup(backup_option)
def create_snapshot(vm_obj):
    vol_obj = zstack_volume_header.ZstackTestVolume()
    vol_obj.set_volume(test_lib.lib_get_root_volume(vm_obj.get_vm()))
    snapshots_root = zstack_sp_header.ZstackVolumeSnapshot()
    snapshots_root.set_utility_vm(vm_obj)
    snapshots_root.set_target_volume(vol_obj)
    snapshots_root.create_snapshot('create_data_snapshot1')
    snapshots_root.check()
    sp1 = snapshots_root.get_current_snapshot()
def delete_snapshot(vm_obj):
    vol_obj = zstack_volume_header.ZstackTestVolume()
    vol_obj.set_volume(test_lib.lib_get_root_volume(vm_obj.get_vm()))
    snapshots_root = zstack_sp_header.ZstackVolumeSnapshot()
    snapshots_root.set_utility_vm(vm_obj)
    snapshots_root.set_target_volume(vol_obj)
    sp_list = snapshots_root.get_snapshot_list()
    if sp_list:
        snapshots_root.delete_snapshot(random.choice(sp_list))
def test():
    ps_env = test_stub.PSEnvChecker()

    local_nfs_env = ps_env.is_local_nfs_env
    local_smp_env = ps_env.is_local_smp_env

    local_ps, another_ps = ps_env.get_two_ps()

    vm = test_stub.create_multi_vms(name_prefix='test-', count=1)[0]
    test_obj_dict.add_vm(vm)

    volume_in_local = []
    if not local_nfs_env:
        volume_in_local = test_stub.create_multi_volumes(count=VOLUME_NUMBER, ps=local_ps,
                                                         host_uuid=test_lib.lib_get_vm_host(vm.get_vm()).uuid)

    volume_in_another = test_stub.create_multi_volumes(count=VOLUME_NUMBER, ps=another_ps,
                                                       host_uuid=None if local_nfs_env else test_lib.lib_get_vm_host(vm.get_vm()).uuid)

    for volume in volume_in_local + volume_in_another:
        test_obj_dict.add_volume(volume)

    for volume in volume_in_local + volume_in_another:
        volume.attach(vm)
        volume.check()
    vm.check()

    for volume in volume_in_local + volume_in_another:
        volume.detach()
        volume.check()
    vm.check()

    target_host = test_lib.lib_find_random_host(vm.get_vm())

    vm.stop()
    vm.check()
    vol_ops.migrate_volume(test_lib.lib_get_root_volume(vm.get_vm()).uuid, target_host.uuid)

    for volume in volume_in_local:
        vol_ops.migrate_volume(volume.get_volume().uuid, target_host.uuid)

    if not (local_nfs_env or local_smp_env):
        for volume in volume_in_another:
            vol_ops.migrate_volume(volume.get_volume().uuid, target_host.uuid)

    for volume in volume_in_local + volume_in_another:
        volume.attach(vm)
        volume.check()

    vm.start()
    vm.check()

    for volume in volume_in_local + volume_in_another:
        assert volume.get_volume().vmInstanceUuid == vm.get_vm().uuid

    test_util.test_pass('Multi PrimaryStorage Test Pass')
def test():
    test_util.test_dsc('Create original vm')
    vm = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm)
    vm1 = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm1)

    vm_root_volume_inv = test_lib.lib_get_root_volume(vm.get_vm())
    root_volume_uuid = vm_root_volume_inv.uuid 
    test_util.test_dsc('Stop vm before create snapshot.')
    vm.stop()

    test_util.test_dsc('create snapshot and check')
    snapshots = test_obj_dict.get_volume_snapshot(root_volume_uuid)
    snapshots.set_utility_vm(vm1)

    vm1.check()

    #create snapshot1 from VM's root volume
    snapshot1 = snapshots.create_snapshot('create_root_snapshot1')

    #create new data volume from last root volume's snapshot1
    volume2 = snapshot1.create_data_volume('data_volume_for_root')
    test_obj_dict.add_volume(volume2)

    ##backup snapshot1
    #snapshots.backup_snapshot(snapshot1)

    ##delete backuped snapshot1
    #snapshots.delete_backuped_snapshot(snapshot1)

    snapshots2 = test_obj_dict.get_volume_snapshot(volume2.get_volume().uuid)
    snapshots2.set_utility_vm(vm1)

    #create snapshot2 from new created data volume
    snapshot2 = snapshots2.create_snapshot('create_data_snapshot2')
    snapshots.use_snapshot(snapshot1)

    #create 2nd data volume3 from previous root volume snapshot!
    volume3 = snapshot1.create_data_volume('data_volume_for_root2')

    #Delete the snapshot2
    snapshots2.delete_snapshot(snapshot2)

    #Create snaphot5 from new created data volume
    #In the bug, this operation will fail, when doing volume attach.
    snapshots2.create_snapshot('create_root_snapshot5')

    #snapshots2.backup_snapshot(snapshots2.get_current_snapshot())
    #snapshots2.delete_backuped_snapshot(snapshots2.get_current_snapshot())

    snapshots2.check()
    snapshots.check()

    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass('Do snapshot ops on VM root volume with VM ops successfully')
def back_up(vm_obj):
     global backup
     cond = res_ops.gen_query_conditions("type", '=', "ImageStoreBackupStorage")
     bs = res_ops.query_resource(res_ops.BACKUP_STORAGE, cond)[0]
     backup_option = test_util.BackupOption()
     backup_option.set_name("test_compare")
     backup_option.set_volume_uuid(test_lib.lib_get_root_volume(vm_obj.get_vm()).uuid)
     backup_option.set_backupStorage_uuid(bs.uuid)
     backup = vol_ops.create_vm_backup(backup_option)
     backup_list.append(backup)
def test():
    global vm, vm2, image_uuid
    test_util.test_dsc('create VM with setting password')

    for root_password in root_password_list:
        test_util.test_dsc("root_password: \"%s\"" %(root_password))
        vm = test_stub.create_vm(vm_name = 'c7-vm-no-sys-tag', image_name = "imageName_i_c7_no_tag")

        backup_storage_list = test_lib.lib_get_backup_storage_list_by_vm(vm.vm)
        for bs in backup_storage_list:
            if bs.type == inventory.IMAGE_STORE_BACKUP_STORAGE_TYPE:
                break
            if bs.type == inventory.SFTP_BACKUP_STORAGE_TYPE:
                break
            if bs.type == inventory.CEPH_BACKUP_STORAGE_TYPE:
                break
        else:
            vm.destroy()
            test_util.test_skip('Not find image store type backup storage.')

        vm.check()

        #add tag to vm
        tag_ops.create_system_tag('VmInstanceVO', vm.get_vm().uuid, "qemuga")

        vm_ops.change_vm_password(vm.get_vm().uuid, "root", root_password)

        #create image by the vm with tag
        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('add_tag_vm_to_image')
        image_option1.set_format('qcow2')
        image_option1.set_backup_storage_uuid_list([bs.uuid])

        vm.stop()
        image = img_ops.create_root_volume_template(image_option1)

        #create vm by new image
        vm2 = test_stub.create_vm(vm_name = 'c7-vm-add-tag-from-previous-vm', image_name = "add_tag_vm_to_image")
        if not test_lib.lib_check_login_in_vm(vm2.get_vm(), "root", root_password):
            test_util.test_fail("create vm with user:%s password: %s failed", "root", root_password)

        vm_ops.change_vm_password(vm2.get_vm().uuid, "root", root_password)

        image_uuid = image.uuid
        if not image_uuid:
            img_ops.delete_image(image_uuid)
            img_ops.expunge_image(image_uuid)
            

    test_util.test_pass('add system tag on a no system tag image test passed')
def test():
    global new_offering_uuid
    test_util.test_dsc('Test VM disk bandwidth QoS by 20MB')

    #unit is KB
    write_bandwidth = 15*1024*1024
    new_offering = test_lib.lib_create_instance_offering(write_bandwidth=write_bandwidth)
    new_offering_uuid = new_offering.uuid

    vm = test_stub.create_vm(vm_name = 'vm_volume_qos', \
            instance_offering_uuid = new_offering.uuid)
    test_obj_dict.add_vm(vm)

    vm.check()

    volume_creation_option = test_util.VolumeOption()
    disk_offering = test_lib.lib_get_disk_offering_by_name(os.environ.get('largeDiskOfferingName'))
    volume_creation_option.set_disk_offering_uuid(disk_offering.uuid)
    
    volume_creation_option.set_name('volume-1')
    volume = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume)
    vm_inv = vm.get_vm()
#     test_lib.lib_mkfs_for_volume(volume.get_volume().uuid, vm_inv)
#     mount_point = '/tmp/zstack/test'
#     test_stub.attach_mount_volume(volume, vm, mount_point)

    test_stub.make_ssh_no_password(vm_inv)
    test_stub.install_fio(vm_inv)

    if vm_ops.get_vm_disk_qos(test_lib.lib_get_root_volume(vm_inv).uuid).volumeBandwidthRead != -1 and \
    vm_ops.get_vm_disk_qos(test_lib.lib_get_root_volume(vm_inv).uuid).volumeBandwidthWrite != write_bandwidth:
        test_util.test_fail('Retrieved disk qos not match')
    test_stub.test_fio_bandwidth(vm_inv, write_bandwidth)

    if test_stub.test_fio_bandwidth(vm_inv, write_bandwidth/2, '/dev/vda', raise_exception=False):
        test_util.test_fail('disk write read is not expected to have limit as only write qos was set')
    vm_ops.delete_instance_offering(new_offering_uuid)
    test_lib.lib_robot_cleanup(test_obj_dict)

    test_util.test_pass('VM Disk wirte QoS Test Pass')
def change_os(vm_obj):
    vm_uuid = vm_obj.get_vm().uuid
    last_l3network_uuid = test_lib.lib_get_l3s_uuid_by_vm(vm_obj.get_vm())
    last_ps_uuid = test_lib.lib_get_root_volume(vm_obj.get_vm()).primaryStorageUuid
    cond = res_ops.gen_query_conditions("system", '=', "false")
    cond = res_ops.gen_query_conditions("mediaType", '=', "RootVolumeTemplate", cond)
    cond = res_ops.gen_query_conditions("platform", '=', "Linux", cond)
    image_uuid = random.choice(res_ops.query_resource(res_ops.IMAGE, cond)).uuid
    vm_ops.change_vm_image(vm_uuid, image_uuid)
    vm_obj.start()
    vm_obj.update()
    # check whether the vm is running successfully
    test_lib.lib_wait_target_up(vm_obj.get_vm().vmNics[0].ip, 22, 300)
    # check whether the network config has changed
    l3network_uuid_after = test_lib.lib_get_l3s_uuid_by_vm(vm_obj.get_vm())
    if l3network_uuid_after != last_l3network_uuid:
        test_util.test_fail('Change VM Image Failed.The Network config has changed.')
    # check whether primarystorage has changed
    ps_uuid_after = test_lib.lib_get_root_volume(vm_obj.get_vm()).primaryStorageUuid
    if ps_uuid_after != last_ps_uuid:
        test_util.test_fail('Change VM Image Failed.Primarystorage has changed.')
def test():
    test_util.test_dsc('Create test vm as utility vm')
    vm1 = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm1)
    #this test will rely on live snapshot capability supporting
    host_inv = test_lib.lib_find_host_by_vm(vm1.get_vm())

    if not test_lib.lib_check_live_snapshot_cap(host_inv):
        vm1.destroy()
        test_obj_dict.rm_vm(vm1)
        test_util.test_skip('Skip test, since [host:] %s does not support live snapshot.' % host_inv.uuid)

    live_snapshot = test_lib.lib_check_live_snapshot_cap(host_inv)
    if not live_snapshot:
        vm1.destroy()
        test_obj_dict.rm_vm(vm1)
        test_util.test_skip("Skip test, since [host:] %s doesn't support live snapshot " % host_inv.uuid)

    vm = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm)

    test_util.test_dsc('Create volume for snapshot testing')
    disk_offering = test_lib.lib_get_disk_offering_by_name(os.environ.get('smallDiskOfferingName'))
    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_name('volume for snapshot testing')
    volume_creation_option.set_disk_offering_uuid(disk_offering.uuid)
    volume1 = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume1)
    volume2 = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume2)
    #make sure utility vm is starting and running
    vm.check()

    volume1.attach(vm1)
    volume2.attach(vm1)

    test_util.test_dsc('create snapshot for root')
    vm_root_volume_inv = test_lib.lib_get_root_volume(vm1.get_vm())
    snapshots = test_obj_dict.get_volume_snapshot(vm_root_volume_inv.uuid)
    snapshots.set_utility_vm(vm)
    snapshots.create_snapshot('create_root_volume_snapshot1')
    volume1.check()
    volume2.check()

    snapshots2 = test_obj_dict.get_volume_snapshot(volume1.get_volume().uuid)
    snapshots2.set_utility_vm(vm)
    snapshots2.create_snapshot('create_data_volume_snapshot1')
    snapshots.check()
    volume1.check()
    volume2.check()

    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass('Create root Snapshot and test data volume status test Success')
def test():
    flavor = case_flavor[os.environ.get('CASE_FLAVOR')]
    ps_env = test_stub.PSEnvChecker()
    local_ps, shared_ps = ps_env.get_two_ps()

    if flavor['local_enable'] == False:
        ps_ops.change_primary_storage_state(local_ps.uuid, state='disable')
    if flavor['shared_enable'] == False:
        ps_ops.change_primary_storage_state(shared_ps.uuid, state='disable')

    test_util.test_dsc("Try to Create VM without specified ps")
    if flavor['local_enable']:
        vm_list = test_stub.create_multi_vms(name_prefix='test-vm', count=2)
        for vm in vm_list:
            test_obj_dict.add_vm(vm)
            assert test_lib.lib_get_root_volume(vm.get_vm()).primaryStorageUuid == local_ps.uuid
    else:
        with test_lib.expected_failure('Create vm when no ps enabled', Exception):
            test_stub.create_multi_vms(name_prefix='test-vm', count=2)

    test_util.test_dsc("Create VM with Volume without specified ps")
    if flavor['local_enable'] and flavor['shared_enable']:
        vm_list = test_stub.create_multi_vms(name_prefix='test-vm', count=2, data_volume_number=1)
        for vm in vm_list:
            test_obj_dict.add_vm(vm)
            assert test_lib.lib_get_root_volume(vm.get_vm()).primaryStorageUuid == local_ps.uuid

            for data_vol in [volume for volume in vm.get_vm().allVolumes if volume.type != 'Root']:
                assert data_vol.primaryStorageUuid == shared_ps.uuid

    else:
        with test_lib.expected_failure('Create vm with volume when no ps enabled', Exception):
            test_stub.create_multi_vms(name_prefix='test-vm', count=2, data_volume_number=1)

    if flavor['local_enable'] == False:
        ps_ops.change_primary_storage_state(local_ps.uuid, state='enable')
    if flavor['shared_enable'] == False:
        ps_ops.change_primary_storage_state(shared_ps.uuid, state='enable')

    test_lib.lib_error_cleanup(test_obj_dict)
def test():
    ps_inv = res_ops.query_resource(res_ops.PRIMARY_STORAGE)
    ceph_ps = [ps for ps in ps_inv if ps.type == 'Ceph']
    if not ceph_ps:
        test_util.test_skip('Skip test as there is not Ceph primary storage')
    
    flavor = case_flavor[os.getenv('CASE_FLAVOR')]
    
    if flavor['shared_vm']:
        multi_ps.create_vm(ps_type="SharedBlock")
    else:
        multi_ps.create_vm(ps_type="Ceph")
    multi_ps.create_data_volume(vms=multi_ps.vm, ps_type='SharedBlock')
    multi_ps.create_data_volume(vms= multi_ps.vm, ps_type='Ceph')

    vm = multi_ps.vm[0]
    vm.stop()

    shared_ps = multi_ps.get_ps(ps_type='SharedBlock')
    ceph_ps = multi_ps.get_ps(ps_type='Ceph')
    if flavor['to_shared_vm']:
        if not flavor['to_shared_volume']:
            ps_uuid_for_root_volume = shared_ps.uuid
            ps_uuid_for_data_volume = ceph_ps.uuid
    else:
        if flavor['to_shared_volume']:
            ps_uuid_for_root_volume = ceph_ps.uuid
            ps_uuid_for_data_volume = shared_ps.uuid
    root_volume_systag = []
    data_volume_systag = ["volumeProvisioningStrategy::ThinProvisioning"]
    cloned_vm = vm.clone(['test_stop_vm_full_clone'], full=True, ps_uuid_for_root_volume=ps_uuid_for_root_volume, ps_uuid_for_data_volume=ps_uuid_for_data_volume, root_volume_systag=root_volume_systag, data_volume_systag=data_volume_systag)[0]
    multi_ps.vm.append(cloned_vm.vm)

    volumes_list = test_lib.lib_get_all_volumes(cloned_vm.vm)
    volumes_number = len(volumes_list)
    if volumes_number != 3:
        test_util.test_fail('Did not just find 3 volumes for [vm:] %s. But we assigned 2 data volume to the vm. We only catch %s volumes' % (cloned_vm.vm.uuid, volumes_number))
    else:
        test_util.test_logger('Find 3 volumes for [vm:] %s.' % cloned_vm.vm.uuid)
        ps = test_lib.lib_get_primary_storage_by_uuid(test_lib.lib_get_root_volume(cloned_vm.vm).primaryStorageUuid)
        data_volume_ps1 = test_lib.lib_get_primary_storage_by_uuid(test_lib.lib_get_data_volumes(cloned_vm.vm)[0].primaryStorageUuid)
        data_volume_ps2 = test_lib.lib_get_primary_storage_by_uuid(test_lib.lib_get_data_volumes(cloned_vm.vm)[1].primaryStorageUuid)
        if flavor['to_shared_vm']:
            if not flavor['to_shared_volume']:
                test_util.test_logger(ps.type + data_volume_ps1.type + data_volume_ps2.type)
                assert ps.type == 'SharedBlock' and data_volume_ps1.type == 'Ceph' and data_volume_ps2.type == 'Ceph'
        else:
            if flavor['to_shared_volume']:
                test_util.test_logger(ps.type + data_volume_ps1.type + data_volume_ps2.type)
                assert ps.type == 'Ceph' and data_volume_ps1.type == 'SharedBlock' and data_volume_ps2.type == 'SharedBlock'
    
    test_util.test_pass('Full Clone Stopped VM Test Success')
def create_image(vm_obj):
    volume_uuid = test_lib.lib_get_root_volume(vm_obj.get_vm()).uuid
    bs_list = test_lib.lib_get_backup_storage_list_by_vm(vm_obj.vm)
    image_option = test_util.ImageOption()
    image_option.set_root_volume_uuid(volume_uuid)
    image_option.set_name('image_resize_template')
    image_option.set_backup_storage_uuid_list([bs_list[0].uuid])
    image = img_ops.create_root_volume_template(image_option)
    new_image = zstack_image_header.ZstackTestImage()
    new_image.set_creation_option(image_option)
    new_image.set_image(image)
    new_image.check()
    new_image.clean()
def compare(ps, vm, backup):
    test_util.test_logger("-----------------compare----------------")
    # find vm_host
    host = test_lib.lib_find_host_by_vm(vm.vm)
    cond = res_ops.gen_query_conditions("type", '=', "ImageStoreBackupStorage")
    bs = res_ops.query_resource(res_ops.BACKUP_STORAGE, cond)[0]

    root_volume = test_lib.lib_get_root_volume(vm.get_vm())
    for i in test_lib.lib_get_data_volumes(vm.get_vm()):
        if i.name == "DATA-for-test_vm":
            data_volume = i

    vm_path = root_volume.installPath
    data_path = data_volume.installPath
    if ps.type == "SharedBlock":
        vm_path = "/dev/" + root_volume.installPath.split("/")[2] + "/" + root_volume.installPath.split("/")[3]
        data_path = "/dev/" + data_volume.installPath.split("/")[2] + "/" + data_volume.installPath.split("/")[3]
    test_util.test_logger(vm_path)
    test_util.test_logger(data_path)

    for i in backup:
        if i.type == "Root":
            name = i.backupStorageRefs[0].installPath.split("/")[2]
            id = i.backupStorageRefs[0].installPath.split("/")[3]
        if i.type == "Data" and "DATA-for-test_vm" in i.metadata:
            name1 = i.backupStorageRefs[0].installPath.split("/")[2]
            id1 = i.backupStorageRefs[0].installPath.split("/")[3]

    # compare vm_root_volume & image
    cmd = "mkdir /root/%s;" \
          "/usr/local/zstack/imagestore/bin/zstcli " \
          "-rootca=/var/lib/zstack/imagestorebackupstorage/package/certs/ca.pem " \
          "-url=%s:8000 " \
          "pull -installpath /root/%s/old.qcow2 %s:%s;" \
          "qemu-img compare %s /root/%s/old.qcow2;" % (id, bs.hostname, id, name, id, vm_path, id)
    cmd1 = "mkdir /root/%s;" \
          "/usr/local/zstack/imagestore/bin/zstcli " \
          "-rootca=/var/lib/zstack/imagestorebackupstorage/package/certs/ca.pem " \
          "-url=%s:8000 " \
          "pull -installpath /root/%s/old.qcow2 %s:%s;" \
          "qemu-img compare %s /root/%s/old.qcow2;" % (id1, bs.hostname, id1, name1, id1, data_path, id1)
    # clean image
    result = test_lib.lib_execute_ssh_cmd(host.managementIp, "root", "password", cmd, timeout=300)
    if result != "Images are identical.\n":
        test_util.test_fail("compare vm_root_volume & image created by backup")
    result = test_lib.lib_execute_ssh_cmd(host.managementIp, "root", "password", cmd1, timeout=300)
    if result != "Images are identical.\n":
        test_util.test_fail("compare vm_data_volume & image created by backup")
def test():
    ps_env = test_stub.PSEnvChecker()

    local_nfs_env = ps_env.is_local_nfs_env
    local_smp_env = ps_env.is_local_smp_env

    local_ps, another_ps = ps_env.get_two_ps()

    vm = test_stub.create_multi_vms(name_prefix='test-', count=1)[0]
    test_obj_dict.add_vm(vm)
    vm.check()

    volume_in_local = test_stub.create_multi_volumes(
        count=VOLUME_NUMBER,
        ps=local_ps,
        host_uuid=test_lib.lib_get_vm_host(vm.get_vm()).uuid)

    volume_in_another = test_stub.create_multi_volumes(
        count=VOLUME_NUMBER,
        ps=another_ps,
        host_uuid=None
        if local_nfs_env else test_lib.lib_get_vm_host(vm.get_vm()).uuid)

    for volume in volume_in_local + volume_in_another:
        test_obj_dict.add_volume(volume)

    for volume in volume_in_local + volume_in_another:
        volume.attach(vm)
        volume.check()
    vm.check()
    vm.update()

    snapshots_list = []
    for volume in vm.get_vm().allVolumes:
        snapshots = test_obj_dict.get_volume_snapshot(volume.uuid)
        snapshots.set_utility_vm(vm)
        snapshots.create_snapshot('create_volume_snapshot')
        snapshots_list.append(snapshots)

    for volume in volume_in_local + volume_in_another:
        volume.detach()
        volume.check()
    vm.check()

    target_host = test_lib.lib_find_random_host(vm.get_vm())

    vm.stop()
    vm.check()
    vol_ops.migrate_volume(
        test_lib.lib_get_root_volume(vm.get_vm()).uuid, target_host.uuid)

    for volume in volume_in_local:
        vol_ops.migrate_volume(volume.get_volume().uuid, target_host.uuid)

    if not (local_nfs_env or local_smp_env):
        for volume in volume_in_another:
            vol_ops.migrate_volume(volume.get_volume().uuid, target_host.uuid)

    for volume in volume_in_local + volume_in_another:
        volume.attach(vm)
        volume.check()

    for snapshots in snapshots_list:
        snapshot = snapshots.get_current_snapshot()
        snapshots.use_snapshot(snapshot)

    vm.start()
    vm.check()

    for volume in volume_in_local + volume_in_another:
        assert volume.get_volume().vmInstanceUuid == vm.get_vm().uuid

    test_util.test_pass('Multi PrimaryStorage Test Pass')
def test():
    test_util.test_dsc('Test Change VM Image Function')
    #set overProvisioning.primaryStorage's value as 10
    con_ops.change_global_config('mevoco', 'overProvisioning.primaryStorage',
                                 10)
    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")
    test_lib.lib_create_disk_offering(diskSize=1099511627776, name="1T")
    disk_offering_uuids = [
        test_lib.lib_get_disk_offering_by_name("smallDiskOffering").uuid,
        test_lib.lib_get_disk_offering_by_name("root-disk").uuid,
        test_lib.lib_get_disk_offering_by_name("1T").uuid
    ]
    #create vm with 3 data volumes
    vm = test_stub.create_vm(image_name="ttylinux",
                             vm_name="test-vm",
                             disk_offering_uuids=disk_offering_uuids)
    test_obj_dict.add_vm(vm)
    vm.check()

    vm_uuid = vm.get_vm().uuid
    last_data_volumes_uuids = []
    last_data_volumes = test_lib.lib_get_data_volumes(vm.get_vm())
    for data_volume in last_data_volumes:
        last_data_volumes_uuids.append(data_volume.uuid)
    last_l3network_uuid = test_lib.lib_get_l3s_uuid_by_vm(vm.get_vm())
    last_primarystorage_uuid = test_lib.lib_get_root_volume(
        vm.get_vm()).primaryStorageUuid
    vm_ops.stop_vm(vm_uuid)
    image_option = test_util.ImageOption()
    image_option.set_name('windows_for_test')
    image_option.set_format('qcow2')
    image_option.set_mediaType('RootVolumeTemplate')
    image_option.set_platform('windows')
    image_option.set_url(
        'http://172.20.1.16:7480/diskimages/windows-telnet.qcow2')
    image_option.set_backup_storage_uuid_list([bss[0].uuid])
    image_option.set_timeout(1800 * 1000)
    new_image = zstack_image_header.ZstackTestImage()
    new_image.set_creation_option(image_option)
    new_image.add_root_volume_template()

    image_windows_uuid = test_lib.lib_get_image_by_name(
        "windows_for_test").uuid
    vm_ops.change_vm_image(vm_uuid, image_windows_uuid)
    vm_ops.start_vm(vm_uuid)
    vm.update()
    vm_ip = vm.get_vm().vmNics[0].ip
    #check whether the windows vm is running successfully
    if not test_lib.lib_wait_target_up(vm_ip, '23', 1200):
        test_util.test_fail(
            'vm:%s is not startup in 1200 seconds.Fail to reboot it.' %
            vm_uuid)
    #check whether data volumes attached to the vm has changed
    data_volumes_after_uuids = []
    data_volumes_after = test_lib.lib_get_data_volumes(vm.get_vm())
    for data_volume in data_volumes_after:
        data_volumes_after_uuids.append(data_volume.uuid)
    if set(last_data_volumes_uuids) != set(data_volumes_after_uuids):
        test_util.test_fail('Change Vm Image Failed.Data volumes changed.')
    #check whether the network config has changed
    l3network_uuid_after = test_lib.lib_get_l3s_uuid_by_vm(vm.get_vm())
    if l3network_uuid_after != last_l3network_uuid:
        test_util.test_fail(
            'Change VM Image Failed.The Network config has changed.')
    #check whether primarystorage has changed
    primarystorage_uuid_after = test_lib.lib_get_root_volume(
        vm.get_vm()).primaryStorageUuid
    if primarystorage_uuid_after != last_primarystorage_uuid:
        test_util.test_fail(
            'Change VM Image Failed.Primarystorage has changed.')
    #check whether the linux vm is running successfully
    image_linux_uuid = test_lib.lib_get_image_by_name("image_for_sg_test").uuid
    vm_ops.stop_vm(vm_uuid)
    vm_ops.change_vm_image(vm_uuid, image_linux_uuid)
    vm_ops.start_vm(vm_uuid)
    if not test_lib.lib_wait_target_up(vm_ip, '22', 120):
        test_util.test_fail(
            'vm:%s is not startup in 120 seconds.Fail to reboot it.' % vm_uuid)
    #check whether data volumes attached to the vm has changed
    data_volumes_after_uuids_linux = []
    data_volumes_after_linux = test_lib.lib_get_data_volumes(vm.get_vm())
    for data_volume in data_volumes_after_linux:
        data_volumes_after_uuids_linux.append(data_volume.uuid)
    if set(data_volumes_after_uuids) != set(data_volumes_after_uuids_linux):
        test_util.test_fail('Change Vm Image Failed.Data volumes changed.')
    #check whether the network config has changed
    l3network_uuid_after_linux = test_lib.lib_get_l3s_uuid_by_vm(vm.get_vm())
    if l3network_uuid_after != l3network_uuid_after_linux:
        test_util.test_fail(
            'Change VM Image Failed.The Network config has changed.')
    #check whether primarystorage has changed
    primarystorage_uuid_after_linux = test_lib.lib_get_root_volume(
        vm.get_vm()).primaryStorageUuid
    if primarystorage_uuid_after != primarystorage_uuid_after_linux:
        test_util.test_fail(
            'Change VM Image Failed.Primarystorage has changed.')

    test_lib.lib_destroy_vm_and_data_volumes(vm.get_vm())
    vm.expunge()
    img_ops.delete_image(image_windows_uuid)
    img_ops.expunge_image(image_windows_uuid)
    test_util.test_pass('Change Vm Image Test Success')
예제 #33
0
def test():
    test_util.test_dsc('Create original vm')
    vm = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm)
    vm.stop()
    vm1 = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm1)
    
    vm1.check()
    test_util.test_dsc('create snapshot for root volume')
    vm_root_volume_inv = test_lib.lib_get_root_volume(vm.get_vm())
    snapshots_root = test_obj_dict.get_volume_snapshot(vm_root_volume_inv.uuid)
    snapshots_root.set_utility_vm(vm1)
    test_obj_dict.add_volume_snapshot(snapshots_root)
    snapshots_root.create_snapshot('create_root_snapshot1')

    test_util.test_dsc('create image template from root volume')
    root_volume_uuid = vm_root_volume_inv.uuid
    root_image_uuid = vm_root_volume_inv.rootImageUuid
    vm_img_inv = test_lib.lib_get_image_by_uuid(root_image_uuid)
    image_option = test_util.ImageOption()
    image_option.set_name('creating_image_from_root_volume')
    image_option.set_guest_os_type(vm_img_inv.guestOsType)
    image_option.set_bits(vm_img_inv.bits)
    image_option.set_root_volume_uuid(root_volume_uuid)
    backup_storage_list = test_lib.lib_get_backup_storage_list_by_vm(vm.get_vm())
    bs_uuid_list = []
    for bs in backup_storage_list:
        bs_uuid_list.append(bs.uuid)
    image_option.set_backup_storage_uuid_list(bs_uuid_list)
    image = zstack_img_header.ZstackTestImage()
    image.set_creation_option(image_option)
    image.create()
    if test_lib.lib_get_delete_policy('image') != zstack_header.DELETE_DIRECT:
        test_obj_dict.add_image(image)
    image.delete()

    test_util.test_dsc('Construct volume obj.')
    r_volume = zstack_volume_header.ZstackTestVolume()
    r_volume.set_volume(test_lib.lib_get_root_volume(vm.get_vm()))
    r_volume.set_state(vol_header.ATTACHED)

    test_util.test_dsc('Create volume template')
    bs_list = test_lib.lib_get_backup_storage_list_by_vm(vm.get_vm())
    vol_tmpt = r_volume.create_template([bs_list[0].uuid], 'new_data_template_by_root_volume')
    if test_lib.lib_get_delete_policy('image') != zstack_header.DELETE_DIRECT:
        test_obj_dict.add_image(vol_tmpt)

    #destroy vm
    host_uuid = test_lib.lib_get_vm_host(vm.get_vm()).uuid
    vm.destroy()

    test_util.test_dsc('Create volume from template')
    ps_uuid = vm.get_vm().allVolumes[0].primaryStorageUuid
    ps = test_lib.lib_get_primary_storage_by_uuid(ps_uuid)

    if ps.type == inventory.LOCAL_STORAGE_TYPE:
        volume = vol_tmpt.create_data_volume(ps_uuid, 'new_data_volume_from_template1', host_uuid)
    else:
        volume = vol_tmpt.create_data_volume(ps_uuid, 'new_data_volume_from_template1')

    test_obj_dict.add_volume(volume)
    vol_tmpt.delete()

    test_util.test_dsc('create snapshot')
    snapshots = zstack_sp_header.ZstackVolumeSnapshot()
    snapshots.set_target_volume(volume)
    snapshots.set_utility_vm(vm1)
    test_obj_dict.add_volume_snapshot(snapshots)
    snapshots.create_snapshot('create_snapshot1')

    snapshot1 = snapshots.get_current_snapshot()
    snapshots.create_snapshot('create_snapshot2')
    snapshot2 = snapshots.get_current_snapshot()
    snapshots.create_snapshot('create_snapshot3')
    snapshot3 = snapshots.get_current_snapshot()

    test_util.test_dsc('delete snapshot3')
    snapshots.delete_snapshot(snapshot3)

    snapshots.check()
    test_obj_dict.rm_volume_snapshot(snapshots)

    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass('Create snapshot from a volume, which is created from data volume template, which is create from a root volume Success')
예제 #34
0
def test():
    test_util.test_dsc('Create original vm')
    vm = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm)
    vm1 = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm1)

    test_util.test_dsc('Construct volume obj.')
    vm_root_volume_inv = test_lib.lib_get_root_volume(vm.get_vm())
    #root_volume = zstack_vol_header.ZstackTestVolume()
    #root_volume.set_volume(vm_root_volume_inv)
    #root_volume.set_target_vm(vm)
    #root_volume.set_state(vol_header.ATTACHED)
    root_volume_uuid = vm_root_volume_inv.uuid
    root_image_uuid = vm_root_volume_inv.rootImageUuid
    vm_img_inv = test_lib.lib_get_image_by_uuid(root_image_uuid)

    test_util.test_dsc('Stop vm before create snapshot.')
    vm.stop()

    test_util.test_dsc('create snapshot')
    #snapshots = zstack_sp_header.ZstackVolumeSnapshot()
    #snapshots.set_target_volume(root_volume)
    #test_obj_dict.add_volume_snapshot(snapshots)
    snapshots = test_obj_dict.get_volume_snapshot(vm_root_volume_inv.uuid)
    snapshots.set_utility_vm(vm1)
    snapshots.create_snapshot('create_root_snapshot1')

    snapshot1 = snapshots.get_current_snapshot()
    snapshots.create_snapshot('create_snapshot2')
    snapshot2 = snapshots.get_current_snapshot()
    snapshots.create_snapshot('create_snapshot3')
    snapshot3 = snapshots.get_current_snapshot()

    test_util.test_dsc('delete snapshot3 and create image tempalte from root')
    snapshots.delete_snapshot(snapshot3)

    image_option = test_util.ImageOption()
    image_option.set_name('creating_image_from_root_volume_after_creating_sp')
    image_option.set_guest_os_type(vm_img_inv.guestOsType)
    image_option.set_bits(vm_img_inv.bits)
    image_option.set_root_volume_uuid(root_volume_uuid)
    backup_storage_list = test_lib.lib_get_backup_storage_list_by_vm(
        vm.get_vm())

    bs_uuid_list = []
    for bs in backup_storage_list:
        bs_uuid_list.append(bs.uuid)
    image_option.set_backup_storage_uuid_list(bs_uuid_list)

    test_util.test_dsc('create image template from root volume')
    image2 = zstack_img_header.ZstackTestImage()
    image2.set_creation_option(image_option)
    image2.create()
    test_obj_dict.add_image(image2)
    image2.check()
    image2_uuid = image2.get_image().uuid

    test_util.test_dsc('create vm2 with new created template and check')
    vm_creation_option = vm.get_creation_option()
    vm_creation_option.set_image_uuid(image2_uuid)
    vm2 = test_lib.lib_create_vm(vm_creation_option)
    test_obj_dict.add_vm(vm2)
    vm2.check()

    vm2.destroy()
    test_obj_dict.rm_vm(vm2)

    test_util.test_dsc('create snapshot4 and finally delete all snapshots')
    snapshots.create_snapshot('create_snapshot4')
    snapshot4 = snapshots.get_current_snapshot()
    snapshot4.backup()
    snapshots.check()
    snapshots.delete()
    test_obj_dict.rm_volume_snapshot(snapshots)

    test_util.test_dsc('create image template2 from root volume')
    image3 = zstack_img_header.ZstackTestImage()
    image3.set_creation_option(image_option)
    image3.create()
    test_obj_dict.add_image(image3)
    image3.check()
    image3_uuid = image3.get_image().uuid

    test_util.test_dsc('create vm3 with new created template and check')
    vm_creation_option.set_image_uuid(image3_uuid)
    vm3 = test_lib.lib_create_vm(vm_creation_option)
    test_obj_dict.add_vm(vm3)
    vm3.check()

    vm3.destroy()
    test_obj_dict.rm_vm(vm3)

    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass(
        'Create image from root volume with creating/destroying Snapshot Success'
    )
예제 #35
0
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=snapshots.create_snapshot,
                                  args=('create_snapshot%s' % str(index), ))
        thread.start()
        index += 1

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

    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))

    test_num = 100
    snapshot_list = snapshots.get_snapshot_list()
    for index in range(test_num):
        thread_1 = threading.Thread(target=snapshots.delete_snapshot,
                                    args=(random.choice(snapshot_list), ))
        thread_2 = threading.Thread(target=snapshots.use_snapshot,
                                    args=(random.choice(snapshot_list), ))
        thread_1.start()
        thread_2.start()

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

    #snapshot.check() doesn't work for root volume
    #snapshots.check()
    #check if snapshot exists in ps (host) 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:
        cond = res_ops.gen_query_conditions('volumeUuid', '=',
                                            root_volume.get_volume().uuid)
        sps_in_database = res_ops.query_resource(res_ops.VOLUME_SNAPSHOT, cond)
        host = test_lib.lib_get_vm_host(vm.get_vm())
        for snapshot_inv in sps_in_database:
            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))

    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass(
        'Test delete and revert 100 snapshots simultaneously success')
예제 #36
0
def test():
    test_util.test_dsc('Create original vm')
    vm = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm)
    vm1 = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm1)

    test_util.test_dsc('Create Data Volume obj.')
    disk_offering = test_lib.lib_get_disk_offering_by_name(
        os.environ.get('smallDiskOfferingName'))
    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_name(
        'volume for create both root and data volume snapshot')
    volume_creation_option.set_disk_offering_uuid(disk_offering.uuid)
    volume = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume)

    volume.attach(vm)
    test_util.test_dsc('Construct root volume obj.')
    vm_root_volume_inv = test_lib.lib_get_root_volume(vm.get_vm())
    #root_volume = zstack_vol_header.ZstackTestVolume()
    #root_volume.set_volume(vm_root_volume_inv)
    #root_volume.set_target_vm(vm)
    #root_volume.set_state(vol_header.ATTACHED)
    root_volume_uuid = vm_root_volume_inv.uuid
    root_image_uuid = vm_root_volume_inv.rootImageUuid
    vm_img_inv = test_lib.lib_get_image_by_uuid(root_image_uuid)

    test_util.test_dsc('Stop vm before create snapshot.')
    vm.stop()

    test_util.test_dsc('create snapshot')
    snapshots_data = test_obj_dict.get_volume_snapshot(
        volume.get_volume().uuid)
    snapshots_data.set_utility_vm(vm1)
    snapshots_data.create_snapshot('create_data_snapshot1')
    snapshot1 = snapshots_data.get_current_snapshot()
    snapshots_data.create_snapshot('create_data_snapshot2')
    snapshots_data.create_snapshot('create_data_snapshot3')

    #snapshots_root = zstack_sp_header.ZstackVolumeSnapshot()
    #snapshots_root.set_target_volume(root_volume)
    #test_obj_dict.add_volume_snapshot(snapshots_root)
    snapshots_root = test_obj_dict.get_volume_snapshot(vm_root_volume_inv.uuid)
    snapshots_root.set_utility_vm(vm1)
    snapshots_root.create_snapshot('create_root_snapshot1')

    snapshots_root.create_snapshot('create_root_snapshot2')
    snapshot2 = snapshots_root.get_current_snapshot()
    snapshots_root.create_snapshot('create_root_snapshot3')
    snapshot3 = snapshots_root.get_current_snapshot()

    test_util.test_dsc('delete snapshot3 and create image tempalte from root')
    snapshots_root.delete_snapshot(snapshot3)

    image_option = test_util.ImageOption()
    image_option.set_name('creating_image_from_root_volume_after_creating_sp')
    image_option.set_guest_os_type(vm_img_inv.guestOsType)
    image_option.set_bits(vm_img_inv.bits)
    image_option.set_root_volume_uuid(root_volume_uuid)
    backup_storage_list = test_lib.lib_get_backup_storage_list_by_vm(
        vm.get_vm())
    bs_uuid_list = []
    for bs in backup_storage_list:
        bs_uuid_list.append(bs.uuid)
    image_option.set_backup_storage_uuid_list(bs_uuid_list)

    test_util.test_dsc('create image template from root volume')
    image2 = zstack_img_header.ZstackTestImage()
    image2.set_creation_option(image_option)
    image2.create()
    test_obj_dict.add_image(image2)
    image2.check()
    image2_uuid = image2.get_image().uuid

    test_util.test_dsc('create vm2 with new created template and check')
    vm_creation_option = vm.get_creation_option()
    vm_creation_option.set_image_uuid(image2_uuid)
    vm2 = test_lib.lib_create_vm(vm_creation_option)
    test_obj_dict.add_vm(vm2)
    vm2.check()

    vm2.destroy()
    test_obj_dict.rm_vm(vm2)

    #check data snapshots
    snapshots_data.use_snapshot(snapshot1)
    snapshots_data.create_snapshot('create_snapshot1.1.1')
    snapshots_data.create_snapshot('create_snapshot1.1.2')

    test_util.test_dsc(
        'create snapshot4 and finally delete all snapshots_root')
    snapshots_root.create_snapshot('create_snapshot4')
    snapshot4 = snapshots_root.get_current_snapshot()
    snapshots_root.backup_snapshot(snapshot4)
    snapshots_root.check()
    vm.destroy()
    test_obj_dict.rm_vm(vm)

    test_util.test_dsc('create image template2 from root snapshot')
    image_option.set_root_volume_uuid(snapshot4.get_snapshot().uuid)
    snapshot4.set_image_creation_option(image_option)
    image3 = snapshot4.create_image_template()
    test_obj_dict.add_image(image3)
    image3.check()
    image3_uuid = image3.get_image().uuid

    test_util.test_dsc('create vm3 with new created template and check')
    vm_creation_option.set_image_uuid(image3_uuid)
    vm3 = test_lib.lib_create_vm(vm_creation_option)
    test_obj_dict.add_vm(vm3)
    vm3.check()

    vm3.destroy()
    test_obj_dict.rm_vm(vm3)

    #check data snapshots
    snapshots_data.use_snapshot(snapshot1)
    snapshots_data.create_snapshot('create_snapshot1.2.1')
    snapshots_data.check()

    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass(
        'Create image from root volume with creating/destroying Snapshot Success'
    )
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))
예제 #38
0
def test():
    test_util.test_dsc('Test Change VM Image Function')
    #set overProvisioning.primaryStorage's value as 10
    con_ops.change_global_config('mevoco', 'overProvisioning.primaryStorage',
                                 10)
    global vm
    test_lib.lib_create_disk_offering(diskSize=1099511627776, name="1T")
    disk_offering_uuids = [
        test_lib.lib_get_disk_offering_by_name("smallDiskOffering").uuid,
        test_lib.lib_get_disk_offering_by_name("root-disk").uuid,
        test_lib.lib_get_disk_offering_by_name("1T").uuid
    ]
    vm = test_stub.create_vm(image_name="ttylinux",
                             vm_name="test-vm",
                             disk_offering_uuids=disk_offering_uuids)
    test_obj_dict.add_vm(vm)
    vm.check()

    vm_uuid = vm.get_vm().uuid
    last_data_volumes_uuids = []
    last_data_volumes = test_lib.lib_get_data_volumes(vm.get_vm())
    for data_volume in last_data_volumes:
        last_data_volumes_uuids.append(data_volume.uuid)
    last_l3network_uuid = test_lib.lib_get_l3s_uuid_by_vm(vm.get_vm())
    last_primarystorage_uuid = test_lib.lib_get_root_volume(
        vm.get_vm()).primaryStorageUuid

    ps = test_lib.lib_get_primary_storage_by_uuid(last_primarystorage_uuid)
    avail_cap = ps.availableCapacity
    total_cap = ps.totalCapacity

    vm_ops.stop_vm(vm_uuid)
    image_uuid = test_lib.lib_get_image_by_name("image_for_sg_test").uuid
    vm_ops.change_vm_image(vm_uuid, image_uuid)
    vm_ops.start_vm(vm_uuid)
    vm.update()
    #check whether the vm is running successfully
    if not test_lib.lib_wait_target_up(vm.get_vm().vmNics[0].ip, '22', 120):
        test_util.test_fail(
            'vm:%s is not startup in 120 seconds.Fail to reboot it.' % vm_uuid)
    #check whether data volumes attached to the vm has changed
    data_volumes_after_uuids = []
    data_volumes_after = test_lib.lib_get_data_volumes(vm.get_vm())
    for data_volume in data_volumes_after:
        data_volumes_after_uuids.append(data_volume.uuid)
    if set(last_data_volumes_uuids) != set(data_volumes_after_uuids):
        test_util.test_fail('Change Vm Image Failed.Data volumes changed.')
    #check whether the network config has changed
    l3network_uuid_after = test_lib.lib_get_l3s_uuid_by_vm(vm.get_vm())
    if l3network_uuid_after != last_l3network_uuid:
        test_util.test_fail(
            'Change VM Image Failed.The Network config has changed.')
    #check whether primarystorage has changed
    primarystorage_uuid_after = test_lib.lib_get_root_volume(
        vm.get_vm()).primaryStorageUuid
    if primarystorage_uuid_after != last_primarystorage_uuid:
        test_util.test_fail(
            'Change VM Image Failed.Primarystorage has changed.')

    ps = test_lib.lib_get_primary_storage_by_uuid(primarystorage_uuid_after)
    avail_cap1 = ps.availableCapacity
    total_cap1 = ps.totalCapacity

    if total_cap != total_cap1:
        test_util.test_fail(
            'Primary Storage total capacity is not same,after changing vm image:%s.The previous value:%s, the current value:%s'
            % (image_uuid, total_cap, total_cap1))
    if avail_cap <= avail_cap1:
        test_util.test_fail(
            'Primary Storage available capacity is not correct,after changing larger image:%s.The previous value:%s, the current value:%s'
            % (image_uuid, avail_cap, avail_cap1))

    vm_ops.stop_vm(vm_uuid)
    image_tiny_uuid = test_lib.lib_get_image_by_name("ttylinux").uuid
    vm_ops.change_vm_image(vm_uuid, image_tiny_uuid)
    vm_ops.start_vm(vm_uuid)
    vm.update()
    #check whether the vm is running successfully
    if not test_lib.lib_wait_target_up(vm.get_vm().vmNics[0].ip, '22', 120):
        test_util.test_fail(
            'vm:%s is not startup in 120 seconds.Fail to reboot it.' % vm_uuid)
    #check whether data volumes attached to the vm has changed
    data_volumes_after_uuids_tiny = []
    data_volumes_after_tiny = test_lib.lib_get_data_volumes(vm.get_vm())
    for data_volume in data_volumes_after_tiny:
        data_volumes_after_uuids_tiny.append(data_volume.uuid)
    if set(data_volumes_after_uuids_tiny) != set(data_volumes_after_uuids):
        test_util.test_fail('Change Vm Image Failed.Data volumes changed.')
    #check whether the network config has changed
    l3network_uuid_after_tiny = test_lib.lib_get_l3s_uuid_by_vm(vm.get_vm())
    if l3network_uuid_after_tiny != l3network_uuid_after:
        test_util.test_fail(
            'Change VM Image Failed.The Network config has changed.')
    #check whether primarystorage has changed
    primarystorage_uuid_after_tiny = test_lib.lib_get_root_volume(
        vm.get_vm()).primaryStorageUuid
    if primarystorage_uuid_after_tiny != primarystorage_uuid_after:
        test_util.test_fail(
            'Change VM Image Failed.Primarystorage has changed.')

    ps = test_lib.lib_get_primary_storage_by_uuid(
        primarystorage_uuid_after_tiny)
    avail_cap2 = ps.availableCapacity
    total_cap2 = ps.totalCapacity

    if total_cap2 != total_cap1:
        test_util.test_fail(
            'Primary Storage total capacity is not same,after changing vm image:%s.The previous value:%s, the current value:%s'
            % (image_uuid, total_cap1, total_cap2))
    if avail_cap2 <= avail_cap1:
        test_util.test_fail(
            'Primary Storage available capacity is not correct,after changing smaller image:%s.The previous value:%s, the current value:%s'
            % (image_uuid, avail_cap1, avail_cap2))

    test_lib.lib_destroy_vm_and_data_volumes(vm.get_vm())
    vm.expunge()
    test_util.test_pass('Change Vm Image Test Success')
def test():
    test_util.test_dsc('Test Change VM Image Function')
    primary_storage_list = res_ops.query_resource(res_ops.PRIMARY_STORAGE)
    for ps in primary_storage_list:
        if ps.type == "SharedBlock":
            test_util.test_skip(
                'SharedBlock primary storage does not support overProvision')
    global vm
    #set overProvisioning.primaryStorage's value as 10
    con_ops.change_global_config('mevoco', 'overProvisioning.primaryStorage',
                                 10)
    test_lib.lib_create_disk_offering(diskSize=1099511627776, name="1T")
    l3_uuid = test_lib.lib_get_l3_by_name("l3VlanNetwork1").uuid
    image_uuid = test_lib.lib_get_image_by_name("ttylinux").uuid
    disk_offering_uuids = [
        test_lib.lib_get_disk_offering_by_name("smallDiskOffering").uuid,
        test_lib.lib_get_disk_offering_by_name("root-disk").uuid,
        test_lib.lib_get_disk_offering_by_name("1T").uuid
    ]
    vm = test_stub.create_vm(l3_uuid_list=[l3_uuid],
                             image_uuid=image_uuid,
                             vm_name="test-vm",
                             disk_offering_uuids=disk_offering_uuids)
    test_obj_dict.add_vm(vm)
    vm.check()

    vm_uuid = vm.get_vm().uuid
    last_data_volumes_uuids = []
    last_data_volumes = test_lib.lib_get_data_volumes(vm.get_vm())
    for data_volume in last_data_volumes:
        last_data_volumes_uuids.append(data_volume.uuid)
    last_l3network_uuid = test_lib.lib_get_l3s_uuid_by_vm(vm.get_vm())
    last_primarystorage_uuid = test_lib.lib_get_root_volume(
        vm.get_vm()).primaryStorageUuid
    vm_ops.stop_vm(vm_uuid)
    vr = test_lib.lib_find_vr_by_vm(vm.get_vm())[0]
    vr_mgmt_ip = test_lib.lib_find_vr_mgmt_ip(vr)
    #stop vm's vr
    vm_ops.stop_vm(vr.uuid)
    #change vm image
    image_uuid = test_lib.lib_get_image_by_name("windows").uuid
    vm_ops.change_vm_image(vm_uuid, image_uuid)
    #check whether vr's status is running
    if vr.applianceVmType == 'vrouter':
        if not test_lib.lib_wait_target_up(vr_mgmt_ip, '7272', 240):
            test_util.test_fail(
                'vm:%s is not startup in 240 seconds.Fail to reboot it.' %
                vr.uuid)
        time.sleep(20)
    else:
        vm_ops.start_vm(vr.uuid)
        vm_ops.reconnect_vr(vr.uuid)
    vm_ops.start_vm(vm_uuid)
    vm.update()
    #check whether the windows vm is running successfully
    vm_ip = vm.get_vm().vmNics[0].ip
    if not test_lib.lib_wait_target_up(vm_ip, '23', 1200):
        test_util.test_fail(
            'vm:%s is not startup in 1200 seconds.Fail to reboot it.' %
            vm_uuid)
    #check whether data volumes attached to the vm has changed
    data_volumes_after_uuids = []
    data_volumes_after = test_lib.lib_get_data_volumes(vm.get_vm())
    for data_volume in data_volumes_after:
        data_volumes_after_uuids.append(data_volume.uuid)
    if set(last_data_volumes_uuids) != set(data_volumes_after_uuids):
        test_util.test_fail('Change Vm Image Failed.Data volumes changed.')
    #check whether the network config has changed
    l3network_uuid_after = test_lib.lib_get_l3s_uuid_by_vm(vm.get_vm())
    if l3network_uuid_after != last_l3network_uuid:
        test_util.test_fail(
            'Change VM Image Failed.The Network config has changed.')
    #check whether primarystorage has changed
    primarystorage_uuid_after = test_lib.lib_get_root_volume(
        vm.get_vm()).primaryStorageUuid
    if primarystorage_uuid_after != last_primarystorage_uuid:
        test_util.test_fail(
            'Change VM Image Failed.Primarystorage has changed.')

    #check whether the linux vm is running successfully
    vm_ops.stop_vm(vm_uuid)
    #stop vm's vr
    vm_ops.stop_vm(vr.uuid)
    image_uuid = test_lib.lib_get_image_by_name("image_for_sg_test").uuid
    vm_ops.change_vm_image(vm_uuid, image_uuid)
    #check whether vr's status is running
    if vr.applianceVmType == 'vrouter':
        if not test_lib.lib_wait_target_up(vr_mgmt_ip, '7272', 240):
            test_util.test_fail(
                'vm:%s is not startup in 240 seconds.Fail to reboot it.' %
                vr.uuid)
        time.sleep(20)
    else:
        vm_ops.start_vm(vr.uuid)
        vm_ops.reconnect_vr(vr.uuid)
    vm_ops.start_vm(vm_uuid)

    if not test_lib.lib_wait_target_up(vm_ip, '22', 120):
        test_util.test_fail(
            'vm:%s is not startup in 120 seconds.Fail to reboot it.' % vm_uuid)
    #check whether data volumes attached to the vm has changed
    data_volumes_after_uuids_linux = []
    data_volumes_after_linux = test_lib.lib_get_data_volumes(vm.get_vm())
    for data_volume in data_volumes_after_linux:
        data_volumes_after_uuids_linux.append(data_volume.uuid)
    if set(data_volumes_after_uuids) != set(data_volumes_after_uuids_linux):
        test_util.test_fail('Change Vm Image Failed.Data volumes changed.')
    #check whether the network config has changed
    l3network_uuid_after_linux = test_lib.lib_get_l3s_uuid_by_vm(vm.get_vm())
    if l3network_uuid_after != l3network_uuid_after_linux:
        test_util.test_fail(
            'Change VM Image Failed.The Network config has changed.')
    #check whether primarystorage has changed
    primarystorage_uuid_after_linux = test_lib.lib_get_root_volume(
        vm.get_vm()).primaryStorageUuid
    if primarystorage_uuid_after_linux != primarystorage_uuid_after:
        test_util.test_fail(
            'Change VM Image Failed.Primarystorage has changed.')

    test_lib.lib_destroy_vm_and_data_volumes(vm.get_vm())
    test_util.test_pass('Change Vm Image Test Success')
예제 #40
0
def test():
    vm = test_stub.create_vm(vm_name='basic-test-vm',
                             image_name='image_for_sg_test')
    test_obj_dict.add_vm(vm)
    vm1 = test_stub.create_vm(vm_name='basic-test-vm1',
                              image_name='image_for_sg_test')
    test_obj_dict.add_vm(vm1)

    image_creation_option = test_util.ImageOption()
    backup_storage_list = test_lib.lib_get_backup_storage_list_by_vm(vm1.vm)
    for bs in backup_storage_list:
        if bs.type == inventory.IMAGE_STORE_BACKUP_STORAGE_TYPE:
            image_creation_option.set_backup_storage_uuid_list(
                [backup_storage_list[0].uuid])
            break
    else:
        test_util.test_skip('Not find image store type backup storage.')

    #vm1.check()
    vm_root_volume_inv = test_lib.lib_get_root_volume(vm1.get_vm())
    root_volume_uuid = vm_root_volume_inv.uuid
    test_util.test_dsc('create snapshot and check')
    snapshots = test_obj_dict.get_volume_snapshot(root_volume_uuid)
    snapshots.set_utility_vm(vm)
    times = 1
    #create 20 snapshots.
    while (times < 21):
        snapshots.create_snapshot('create_root_snapshot_%s' % times)
        times += 1

    image_creation_option.set_root_volume_uuid(root_volume_uuid)
    image_creation_option.set_name('test_create_vm_images_vm1')
    image_creation_option.set_format('qcow2')
    #image_creation_option.set_platform('Linux')
    bs_type = backup_storage_list[0].type

    image1 = test_image.ZstackTestImage()
    image1.set_creation_option(image_creation_option)
    image1.create()
    test_obj_dict.add_image(image1)
    image1_url = image1.export()

    image_creation_option.set_url(image1_url)
    image_creation_option.set_name(exported_image_name1)
    image2 = test_image.ZstackTestImage()
    image2.set_creation_option(image_creation_option)
    image2.add_root_volume_template()
    test_obj_dict.add_image(image2)
    image2_url = image2.export()

    vm2 = test_stub.create_vm(image_name=exported_image_name1)
    test_obj_dict.add_vm(vm2)
    image_creation_option.set_url(image2_url)
    image_creation_option.set_name(exported_image_name2)
    image2 = test_image.ZstackTestImage()
    image2.set_creation_option(image_creation_option)
    image2.add_root_volume_template()

    vm3 = test_stub.create_vm(image_name=exported_image_name2)
    test_obj_dict.add_vm(vm3)

    #do vm check before snapshot check
    vm2.check()
    vm3.check()
    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass('Expose Image Test Success')
예제 #41
0
def test():
   test_util.test_dsc('Test Change VM Image Function')
   #set overProvisioning.primaryStorage's value as 10
   primary_storage_list = res_ops.query_resource(res_ops.PRIMARY_STORAGE)
   for ps in primary_storage_list:
       if ps.type == "SharedBlock":
           test_util.test_skip('SharedBlock primary storage does not support overProvision')
   con_ops.change_global_config('mevoco','overProvisioning.primaryStorage',10)
   global vm
   test_lib.lib_create_disk_offering(diskSize=1099511627776,name="1T")
   l3_uuid = test_lib.lib_get_l3_by_name("l3VlanNetwork3").uuid
   image_uuid = test_lib.lib_get_image_by_name("ttylinux").uuid
   disk_offering_uuids = [test_lib.lib_get_disk_offering_by_name("smallDiskOffering").uuid,test_lib.lib_get_disk_offering_by_name("root-disk").uuid,test_lib.lib_get_disk_offering_by_name("1T").uuid]
   vm = test_stub.create_vm(l3_uuid_list = [l3_uuid],image_uuid = image_uuid,vm_name="test-nxs",disk_offering_uuids = disk_offering_uuids)
   test_obj_dict.add_vm(vm)
   vm.check()

   vm_uuid = vm.get_vm().uuid
   last_data_volumes_uuids = []
   last_data_volumes = test_lib.lib_get_data_volumes(vm.get_vm())
   for data_volume in last_data_volumes:
        last_data_volumes_uuids.append(data_volume.uuid)
   last_l3network_uuid = test_lib.lib_get_l3s_uuid_by_vm(vm.get_vm())
   last_primarystorage_uuid = test_lib.lib_get_root_volume(vm.get_vm()).primaryStorageUuid

   ps = test_lib.lib_get_primary_storage_by_uuid(last_primarystorage_uuid)
   avail_cap = ps.availableCapacity
   total_cap = ps.totalCapacity

   vm_ops.stop_vm(vm_uuid)
   vr = test_lib.lib_find_vr_by_vm(vm.get_vm())[0]
   vr_mgmt_ip = test_lib.lib_find_vr_mgmt_ip(vr)
   #stop vm's vr
   vm_ops.stop_vm(vr.uuid)
   image_uuid = test_lib.lib_get_image_by_name("image_for_sg_test").uuid
   vm_ops.change_vm_image(vm_uuid,image_uuid)
   #check whether vr's status is running
   if vr.applianceVmType == 'vrouter':
      if not test_lib.lib_wait_target_up(vr_mgmt_ip,'7272',240):
         test_util.test_fail('vm:%s is not startup in 240 seconds.Fail to reboot it.' % vr.uuid)
      time.sleep(20)
   #if vr.state == 'Stopped':
   else:
      vm_ops.start_vm(vr.uuid)
      vm_ops.reconnect_vr(vr.uuid)
   vm_ops.start_vm(vm_uuid)
   vm.update()
   #check whether the vm is running successfully
   if not test_lib.lib_wait_target_up(vm.get_vm().vmNics[0].ip,'22',120):
      test_util.test_fail('vm:%s is not startup in 120 seconds.Fail to reboot it.' % vm_uuid)
   #check whether data volumes attached to the vm has changed
   data_volumes_after_uuids = []
   data_volumes_after = test_lib.lib_get_data_volumes(vm.get_vm())
   for data_volume in data_volumes_after:
      data_volumes_after_uuids.append(data_volume.uuid)
   if set(last_data_volumes_uuids) != set(data_volumes_after_uuids):
      test_util.test_fail('Change Vm Image Failed.Data volumes changed.')
   #check whether the network config has changed
   l3network_uuid_after = test_lib.lib_get_l3s_uuid_by_vm(vm.get_vm())
   if set(l3network_uuid_after) != set(last_l3network_uuid):
      test_util.test_fail('Change VM Image Failed.The Network config has changed.')
   #check whether primarystorage has changed
   primarystorage_uuid_after = test_lib.lib_get_root_volume(vm.get_vm()).primaryStorageUuid
   if primarystorage_uuid_after != last_primarystorage_uuid:
      test_util.test_fail('Change VM Image Failed.Primarystorage has changed.')

   ps = test_lib.lib_get_primary_storage_by_uuid(primarystorage_uuid_after)
   avail_cap1 = ps.availableCapacity
   total_cap1 = ps.totalCapacity

   if total_cap != total_cap1:
      test_util.test_fail('Primary Storage total capacity is not same,after changing vm image:%s.The previous value:%s, the current value:%s' % (image_uuid,total_cap,total_cap1))
   if avail_cap <= avail_cap1:
      test_util.test_fail('Primary Storage available capacity is not correct,after changing larger image:%s.The previous value:%s, the current value:%s' % (image_uuid,avail_cap,avail_cap1))

   vm_ops.stop_vm(vm_uuid)
   #stop vm's vr
   vm_ops.stop_vm(vr.uuid)
   image_tiny_uuid = test_lib.lib_get_image_by_name("ttylinux").uuid
   vm_ops.change_vm_image(vm_uuid,image_tiny_uuid)
   #check whether vr's status is running
   if vr.applianceVmType == 'vrouter':
      if not test_lib.lib_wait_target_up(vr_mgmt_ip,'7272',240):
         test_util.test_fail('vm:%s is not startup in 240 seconds.Fail to reboot it.' % vr.uuid)
      time.sleep(20)
   #if vr.state == 'Stopped':
   else:
      vm_ops.start_vm(vr.uuid)
      vm_ops.reconnect_vr(vr.uuid)
   vm_ops.start_vm(vm_uuid)
   vm.update()
   #check whether the vm is running successfully
   if not test_lib.lib_wait_target_up(vm.get_vm().vmNics[0].ip,'22',120):
      test_util.test_fail('vm:%s is not startup in 120 seconds.Fail to reboot it.' % vm_uuid)
   #check whether data volumes attached to the vm has changed
   data_volumes_after_uuids_tiny = []
   data_volumes_after_tiny = test_lib.lib_get_data_volumes(vm.get_vm())
   for data_volume in data_volumes_after_tiny:
      data_volumes_after_uuids_tiny.append(data_volume.uuid)
   if set(data_volumes_after_uuids_tiny) != set(data_volumes_after_uuids):
      test_util.test_fail('Change Vm Image Failed.Data volumes changed.')
   #check whether the network config has changed
   l3network_uuid_after_tiny = test_lib.lib_get_l3s_uuid_by_vm(vm.get_vm())
   if set(l3network_uuid_after_tiny) != set(l3network_uuid_after_tiny):
      test_util.test_fail('Change VM Image Failed.The Network config has changed.')
   #check whether primarystorage has changed
   primarystorage_uuid_after_tiny = test_lib.lib_get_root_volume(vm.get_vm()).primaryStorageUuid
   if primarystorage_uuid_after != primarystorage_uuid_after_tiny:
      test_util.test_fail('Change VM Image Failed.Primarystorage has changed.')

   ps = test_lib.lib_get_primary_storage_by_uuid(primarystorage_uuid_after_tiny)
   avail_cap2 = ps.availableCapacity
   total_cap2 = ps.totalCapacity

   if total_cap2 != total_cap1:
      test_util.test_fail('Primary Storage total capacity is not same,after changing vm image:%s.The previous value:%s, the current value:%s' % (image_uuid,total_cap1,total_cap2))
   if avail_cap2 <= avail_cap1:
      test_util.test_fail('Primary Storage available capacity is not correct,after changing smaller image:%s.The previous value:%s, the current value:%s' % (image_uuid,avail_cap1,avail_cap2))


   test_lib.lib_destroy_vm_and_data_volumes(vm.get_vm())
   test_util.test_pass('Change Vm Image Test Success')
def test():
    primary_storage_list = res_ops.query_resource(res_ops.PRIMARY_STORAGE)
    for ps in primary_storage_list:
        if ps.type == "AliyunNAS":
            test_util.test_skip('The test is not supported by AliyunNAS primary storage.')

    test_util.test_dsc('Create original vm')
    vm = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm)
    vm1 = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm1)

    test_util.test_dsc('Construct volume obj.')
    vm_root_volume_inv = test_lib.lib_get_root_volume(vm.get_vm())
    #root_volume = zstack_vol_header.ZstackTestVolume()
    #root_volume.set_volume(vm_root_volume_inv)
    #root_volume.set_state(vol_header.ATTACHED)
    root_image_uuid = vm_root_volume_inv.rootImageUuid
    vm_img_inv = test_lib.lib_get_image_by_uuid(root_image_uuid)

    test_util.test_dsc('Stop vm before create snapshot.')
    vm.stop()

    test_util.test_dsc('create snapshot and check')
    #snapshots = zstack_sp_header.ZstackVolumeSnapshot()
    #snapshots.set_target_volume(root_volume)
    #test_obj_dict.add_volume_snapshot(snapshots)
    snapshots = test_obj_dict.get_volume_snapshot(vm_root_volume_inv.uuid)
    snapshots.set_utility_vm(vm1)
    snapshots.create_snapshot('create_root_snapshot1')
    snapshots.check()

    snapshot1 = snapshots.get_current_snapshot()
    snapshots.create_snapshot('create_snapshot2')
    snapshots.create_snapshot('create_snapshot3')
    snapshots.check()
    snapshot3 = snapshots.get_current_snapshot()

    test_util.test_dsc('create image template from sp3 and check')
    
    image_option = test_util.ImageOption()
    image_option.set_name('test_snapshot_creating_image')
    image_option.set_guest_os_type(vm_img_inv.guestOsType)
    image_option.set_bits(vm_img_inv.bits)
    image_option.set_root_volume_uuid(snapshot3.get_snapshot().uuid)
    snapshot3.set_image_creation_option(image_option)
    image3 = snapshot3.create_image_template()
    test_obj_dict.add_image(image3)
    image3_uuid = image3.get_image().uuid
    image3.check()

    vm_creation_option = vm.get_creation_option()
    vm_creation_option.set_image_uuid(image3_uuid)
    vm3 = test_lib.lib_create_vm(vm_creation_option)
    test_obj_dict.add_vm(vm3)
    vm3.check()

    test_util.test_dsc('create image template from sp1 and check')
    snapshot1.set_image_creation_option(image_option)
    image1 = snapshot3.create_image_template()
    test_obj_dict.add_image(image1)
    image1_uuid = image1.get_image().uuid
    image1.check()

    vm_creation_option.set_image_uuid(image1_uuid)
    vm2 = test_lib.lib_create_vm(vm_creation_option)
    test_obj_dict.add_vm(vm2)
    vm2.check()
    
    snapshots.delete()
    test_obj_dict.rm_volume_snapshot(snapshots)
    vm2.check()
    vm3.check()

    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass('Create image from Snapshot test Success')
def test():
    vm = test_stub.create_vm(vm_name='basic-test-vm',
                             image_name='image_for_sg_test')
    test_obj_dict.add_vm(vm)
    vm1 = test_stub.create_vm(vm_name='basic-test-vm1',
                              image_name='image_for_sg_test')
    test_obj_dict.add_vm(vm1)

    image_creation_option = test_util.ImageOption()
    backup_storage_list = test_lib.lib_get_backup_storage_list_by_vm(vm1.vm)
    for bs in backup_storage_list:
        if bs.type == inventory.IMAGE_STORE_BACKUP_STORAGE_TYPE:
            image_creation_option.set_backup_storage_uuid_list(
                [backup_storage_list[0].uuid])
            break
    else:
        vm.destroy()
        vm1.destroy()
        test_util.test_skip('Not find image store type backup storage.')

    #vm1.check()
    vm_root_volume_inv = test_lib.lib_get_root_volume(vm1.get_vm())
    root_image_uuid = vm_root_volume_inv.rootImageUuid
    vm_img_inv = test_lib.lib_get_image_by_uuid(root_image_uuid)
    test_util.test_dsc('create snapshot and check')
    snapshots = test_obj_dict.get_volume_snapshot(vm_root_volume_inv.uuid)
    snapshots.set_utility_vm(vm)
    snapshots.create_snapshot('create_root_snapshot1')
    snapshot1 = snapshots.get_current_snapshot()
    snapshots.create_snapshot('create_root_snapshot2')

    image_creation_option.set_root_volume_uuid(vm_root_volume_inv.uuid)
    image_creation_option.set_name('test_create_vm_images_vm1')
    #image_creation_option.set_platform('Linux')
    bs_type = backup_storage_list[0].type
    if bs_type == 'Ceph':
        origin_interval = conf_ops.change_global_config(
            'ceph', 'imageCache.cleanup.interval', '1')

    image1 = test_image.ZstackTestImage()
    image1.set_creation_option(image_creation_option)
    image1.create()
    test_obj_dict.add_image(image1)
    image1.check()
    vm2 = test_stub.create_vm(image_name='test_create_vm_images_vm1')
    test_obj_dict.add_vm(vm2)

    #do vm check before snapshot check
    vm.check()
    vm1.stop()
    snapshots.check()
    snapshots.use_snapshot(snapshot1)
    vm1.start()
    snapshots.create_snapshot('create_root_snapshot1.1')

    image_creation_option.set_name('test_create_vm_images_vm2')
    image2 = test_image.ZstackTestImage()
    image2.set_creation_option(image_creation_option)
    image2.create()
    test_obj_dict.add_image(image2)
    image2.check()
    vm3 = test_stub.create_vm(image_name='test_create_vm_images_vm2')
    test_obj_dict.add_vm(vm3)
    snapshots.check()
    vm2.check()
    vm3.check()
    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass('Create VM Image in Image Store Success')
def test():
    ps_env = test_stub.PSEnvChecker()

    local_nfs_env = ps_env.is_local_nfs_env

    local_ps, another_ps = ps_env.get_two_ps()

    vm = test_stub.create_multi_vms(name_prefix='test-', count=1)[0]
    test_obj_dict.add_vm(vm)

    volume_in_local = []
    if not local_nfs_env:
        volume_in_local = test_stub.create_multi_volumes(
            count=VOLUME_NUMBER,
            ps=local_ps,
            host_uuid=test_lib.lib_get_vm_host(vm.get_vm()).uuid)

    volume_in_another = test_stub.create_multi_volumes(
        count=VOLUME_NUMBER,
        ps=another_ps,
        host_uuid=None
        if local_nfs_env else test_lib.lib_get_vm_host(vm.get_vm()).uuid)

    for volume in volume_in_local + volume_in_another:
        test_obj_dict.add_volume(volume)

    for volume in volume_in_local + volume_in_another:
        volume.attach(vm)
        volume.check()
    vm.check()

    for volume in volume_in_local + volume_in_another:
        volume.detach()
        volume.check()
    vm.check()

    target_host = test_lib.lib_find_random_host(vm.get_vm())

    vm.stop()
    vm.check()
    vol_ops.migrate_volume(
        test_lib.lib_get_root_volume(vm.get_vm()).uuid, target_host.uuid)

    for volume in volume_in_local:
        vol_ops.migrate_volume(volume.get_volume().uuid, target_host.uuid)

    if not local_nfs_env:
        for volume in volume_in_another:
            vol_ops.migrate_volume(volume.get_volume().uuid, target_host.uuid)

    for volume in volume_in_local + volume_in_another:
        volume.attach(vm)
        volume.check()

    vm.start()
    vm.check()

    for volume in volume_in_local + volume_in_another:
        assert volume.get_volume().vmInstanceUuid == vm.get_vm().uuid

    test_util.test_pass('Multi PrimaryStorage Test Pass')