Ejemplo n.º 1
0
def test():
    global test_obj_dict
    global ps_uuid
    global host_uuid
    global vr_uuid
    test_util.test_dsc('Create test vm and check')

    bs_cond = res_ops.gen_query_conditions("status", '=', "Connected")
    bss = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE, bs_cond, \
            None)
    if not bss:
        test_util.test_skip("not find available backup storage. Skip test")

    if bss[0].type != inventory.CEPH_BACKUP_STORAGE_TYPE:
        test_util.test_skip("not find available ceph backup storage. Skip test")

    test_lib.lib_set_delete_policy('vm', 'Delay')
    test_lib.lib_set_delete_policy('volume', 'Delay')

    l3_1_name = os.environ.get('l3VlanNetworkName1')
    vm = test_stub.create_vlan_vm(l3_name=l3_1_name)
    l3_1 = test_lib.lib_get_l3_by_name(l3_1_name)
    vr = test_lib.lib_find_vr_by_l3_uuid(l3_1.uuid)[0]
    vr_uuid = vr.uuid
    
    host = test_lib.lib_get_vm_host(vm.get_vm())
    host_uuid = host.uuid
    test_obj_dict.add_vm(vm)
    vm.check()

    disk_offering = test_lib.lib_get_disk_offering_by_name(os.environ.get('rootDiskOfferingName'))
    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_disk_offering_uuid(disk_offering.uuid)
    volume_creation_option.set_system_tags(['ephemeral::shareable', 'capability::virtio-scsi'])
    volume = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume)
    volume.check()
    volume.attach(vm)

    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
    ps_uuid = ps.uuid
    ps_ops.change_primary_storage_state(ps_uuid, 'disable')
    if not test_lib.lib_wait_target_up(vm.get_vm().vmNics[0].ip, '22', 90):
        test_util.test_fail('VM is expected to running when PS change to disable state')

    vm.set_state(vm_header.RUNNING)
    vm.check()
    volume.delete()
    #volume.expunge()
    volume.check()

    ps_ops.change_primary_storage_state(ps_uuid, 'enable')
    host_ops.reconnect_host(host_uuid)
    vm_ops.reconnect_vr(vr_uuid)
    vm.destroy()

    test_lib.lib_set_delete_policy('vm', 'Direct')
    test_lib.lib_set_delete_policy('volume', 'Direct')

    test_util.test_pass('Delete volume under PS disable mode Test Success')
def test():
    global test_obj_dict
    global ps_uuid
    global host_uuid
    global vr_uuid
    test_util.test_dsc('Create test vm and check')
    l3_1_name = os.environ.get('l3VlanNetworkName1')
    vm = test_stub.create_vlan_vm(l3_name=l3_1_name)
    #l3_1 = test_lib.lib_get_l3_by_name(l3_1_name)
    #vr = test_lib.lib_find_vr_by_l3_uuid(l3_1.uuid)[0]
    #vr_uuid = vr.uuid
    
    host = test_lib.lib_get_vm_host(vm.get_vm())
    host_uuid = host.uuid
    test_obj_dict.add_vm(vm)
    vm.check()

    test_util.test_dsc('Add ISO Image')
    cond = res_ops.gen_query_conditions("status", '=', "Connected")
    bs_uuid = res_ops.query_resource(res_ops.BACKUP_STORAGE, cond)[0].uuid
    img_option = test_util.ImageOption()
    img_option.set_name('iso')
    img_option.set_backup_storage_uuid_list([bs_uuid])
    os.system("echo fake iso for test only >  %s/apache-tomcat/webapps/zstack/static/test.iso" % (os.environ.get('zstackInstallPath')))
    img_option.set_url('http://%s:8080/zstack/static/test.iso' % (os.environ.get('node1Ip')))
    image_inv = img_ops.add_iso_template(img_option)
    image = test_image.ZstackTestImage()
    image.set_image(image_inv)
    image.set_creation_option(img_option)
    test_obj_dict.add_image(image)


    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
    ps_uuid = ps.uuid
    ps_ops.change_primary_storage_state(ps_uuid, 'maintain')
    if not test_lib.lib_wait_target_down(vm.get_vm().vmNics[0].ip, '22', 90):
        test_util.test_fail('VM is expected to stop when PS change to maintain state')
    vm.set_state(vm_header.STOPPED)
    vm.check()

    test_util.test_dsc('Attach ISO to VM')
    cond = res_ops.gen_query_conditions('name', '=', 'iso')
    iso_uuid = res_ops.query_resource(res_ops.IMAGE, cond)[0].uuid
    img_ops.attach_iso(iso_uuid, vm.vm.uuid)


    ps_ops.change_primary_storage_state(ps_uuid, 'enable')
    host_ops.reconnect_host(host_uuid)
    #vm_ops.reconnect_vr(vr_uuid)
    vrs = test_lib.lib_get_all_vrs()
    for vr in vrs:
        vm_ops.start_vm(vr.uuid)  

    vm.start()
    vm.check()
    vm.destroy()
    vm.check()
    #vm.expunge()
    #vm.check()
    test_util.test_pass('PS maintain mode 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')
def test():
    global session_to
    global session_mc

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

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

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

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

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

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

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

    try:
        test_lib.lib_robot_cleanup(test_obj_dict)
    except:
        test_lib.test_logger('Delete VM may timeout')
    test_util.test_pass('Test create 100 snapshots simultaneously success')
def test():
    global test_obj_dict
    global ps_uuid
    global host_uuid
    global vr_uuid
    test_util.test_dsc('Create test vm and check')
    l3_1_name = os.environ.get('l3VlanNetworkName1')
    vm = test_stub.create_vlan_vm(l3_name=l3_1_name)
    l3_1 = test_lib.lib_get_l3_by_name(l3_1_name)
    vr = test_lib.lib_find_vr_by_l3_uuid(l3_1.uuid)[0]
    vr_uuid = vr.uuid
    
    host = test_lib.lib_get_vm_host(vm.get_vm())
    host_uuid = host.uuid
    test_obj_dict.add_vm(vm)
    vm.check()
    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
    ps_uuid = ps.uuid
    ps_ops.change_primary_storage_state(ps_uuid, 'disable')
    if not test_lib.lib_wait_target_up(vm.get_vm().vmNics[0].ip, '22', 90):
        test_util.test_fail('VM is expected to running when PS change to disable state')

    vm.set_state(vm_header.RUNNING)
    vm.check()
    vm.destroy()
    vm.check()

    ps_ops.change_primary_storage_state(ps_uuid, 'enable')
    host_ops.reconnect_host(host_uuid)
    vm_ops.reconnect_vr(vr_uuid)
    test_util.test_pass('PS disable mode Test Success')
Ejemplo n.º 6
0
def test():
    global test_obj_dict
    global ps_uuid
    global host_uuid
    global vr_uuid
    test_util.test_dsc('Create test vm and check')
    l3_1_name = os.environ.get('l3VlanNetworkName1')
    vm = test_stub.create_vlan_vm(l3_name=l3_1_name)
    #l3_1 = test_lib.lib_get_l3_by_name(l3_1_name)
    #vr = test_lib.lib_find_vr_by_l3_uuid(l3_1.uuid)[0]
    #vr_uuid = vr.uuid
    
    host = test_lib.lib_get_vm_host(vm.get_vm())
    host_uuid = host.uuid
    test_obj_dict.add_vm(vm)
    vm.check()
    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
    ps_uuid = ps.uuid
    ps_ops.change_primary_storage_state(ps_uuid, 'maintain')
    if not test_lib.lib_wait_target_down(vm.get_vm().vmNics[0].ip, '22', 90):
        test_util.test_fail('VM is expected to stop when PS change to maintain state')

    vm.set_state(vm_header.STOPPED)
    vm.check()
    ps_ops.change_primary_storage_state(ps_uuid, 'enable')
    host_ops.reconnect_host(host_uuid)
    #vm_ops.reconnect_vr(vr_uuid)
    vrs = test_lib.lib_get_all_vrs()
    for vr in vrs:
        vm_ops.start_vm(vr.uuid)  

    vm.start()
    vm.check()
    vm.destroy()
    test_util.test_pass('PS maintain mode Test Success')
def test():
    global test_obj_dict
    global ps_uuid
    global host_uuid
    global vr_uuid
    test_util.test_dsc('Create test vm and check')
    l3_1_name = os.environ.get('l3VlanNetworkName1')
    vm = test_stub.create_vlan_vm(l3_name=l3_1_name)
    l3_1 = test_lib.lib_get_l3_by_name(l3_1_name)
    vr = test_lib.lib_find_vr_by_l3_uuid(l3_1.uuid)[0]
    vr_uuid = vr.uuid

    l3_1 = test_lib.lib_get_l3_by_name(l3_1_name)
    host = test_lib.lib_get_vm_host(vm.get_vm())
    host_uuid = host.uuid
    test_obj_dict.add_vm(vm)
    vm.check()
    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
    ps_uuid = ps.uuid
    ps_ops.change_primary_storage_state(ps_uuid, 'disable')
    if not test_lib.lib_wait_target_up(vm.get_vm().vmNics[0].ip, '22', 90):
        test_util.test_fail(
            'VM is expected to runnning when PS change to disable state')

    vm.set_state(vm_header.RUNNING)
    vm.check()
    vm.suspend()
    vm.check()

    ps_ops.change_primary_storage_state(ps_uuid, 'enable')
    host_ops.reconnect_host(host_uuid)
    vm_ops.reconnect_vr(vr_uuid)
    test_util.test_pass('PS disable mode Test Success')
def test():
    global test_obj_dict
    global ps_uuid
    global host_uuid
    global vr_uuid
    test_util.test_dsc('Create test vm and check')
    l3_1_name = os.environ.get('l3VlanNetworkName1')
    vm = test_stub.create_vlan_vm(l3_name=l3_1_name)
    l3_1 = test_lib.lib_get_l3_by_name(l3_1_name)
    vr = test_lib.lib_find_vr_by_l3_uuid(l3_1.uuid)[0]
    vr_uuid = vr.uuid

    host = test_lib.lib_get_vm_host(vm.get_vm())
    host_uuid = host.uuid
    test_obj_dict.add_vm(vm)
    vm.check()

    test_util.test_dsc('Add ISO Image')
    cond = res_ops.gen_query_conditions("status", '=', "Connected")
    bs_uuid = res_ops.query_resource(res_ops.BACKUP_STORAGE, cond)[0].uuid
    img_option = test_util.ImageOption()
    img_option.set_name('iso')
    img_option.set_backup_storage_uuid_list([bs_uuid])
    os.system(
        "echo fake iso for test only >  %s/apache-tomcat/webapps/zstack/static/test.iso"
        % (os.environ.get('zstackInstallPath')))
    img_option.set_url('http://%s:8080/zstack/static/test.iso' %
                       (os.environ.get('node1Ip')))
    image_inv = img_ops.add_iso_template(img_option)
    image = test_image.ZstackTestImage()
    image.set_image(image_inv)
    image.set_creation_option(img_option)
    test_obj_dict.add_image(image)

    test_util.test_dsc('Attach ISO to VM')
    cond = res_ops.gen_query_conditions('name', '=', 'iso')
    iso_uuid = res_ops.query_resource(res_ops.IMAGE, cond)[0].uuid
    img_ops.attach_iso(iso_uuid, vm.vm.uuid)

    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
    ps_uuid = ps.uuid
    ps_ops.change_primary_storage_state(ps_uuid, 'disable')
    if not test_lib.lib_wait_target_up(vm.get_vm().vmNics[0].ip, '22', 90):
        test_util.test_fail(
            'VM is expected to running when PS change to disable state')
    vm.set_state(vm_header.RUNNING)
    vm.check()

    test_util.test_dsc('Detach ISO to VM')
    img_ops.detach_iso(vm.vm.uuid)

    ps_ops.change_primary_storage_state(ps_uuid, 'enable')
    host_ops.reconnect_host(host_uuid)
    vm_ops.reconnect_vr(vr_uuid)
    vm.destroy()
    vm.check()
    #vm.expunge()
    vm.check()
    test_util.test_pass('PS disable mode Test Success')
Ejemplo n.º 9
0
def test():
    bs_cond = res_ops.gen_query_conditions("status", '=', "Connected")
    bss = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE, bs_cond, \
            None, fields=['uuid'])
    if not bss:
        test_util.test_skip("not find available backup storage. Skip test")

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

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

    new_image.add_root_volume_template()

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

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

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

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

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

    test_util.test_pass('imagecache cleanup Pass.')
Ejemplo n.º 10
0
def test():
    global test_obj_dict
    global ps_uuid
    global host_uuid
    global vr_uuid
    test_util.test_dsc('Create test vm and check')
    test_lib.lib_set_delete_policy('vm', 'Delay')
    test_lib.lib_set_delete_policy('volume', 'Delay')
    l3_1_name = os.environ.get('l3VlanNetworkName1')
    vm = test_stub.create_vlan_vm(l3_name=l3_1_name)
    #l3_1 = test_lib.lib_get_l3_by_name(l3_1_name)
    #vr = test_lib.lib_find_vr_by_l3_uuid(l3_1.uuid)[0]
    #vr_uuid = vr.uuid
    
    #l3_1 = test_lib.lib_get_l3_by_name(l3_1_name)
    host = test_lib.lib_get_vm_host(vm.get_vm())
    host_uuid = host.uuid
    test_obj_dict.add_vm(vm)
    vm.check()

    disk_offering = test_lib.lib_get_disk_offering_by_name(os.environ.get('rootDiskOfferingName'))
    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_disk_offering_uuid(disk_offering.uuid)
    #volume_creation_option.set_system_tags(['ephemeral::shareable', 'capability::virtio-scsi'])
    volume = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume)
    volume.check()
    volume.delete()
    volume.check()

    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
    ps_uuid = ps.uuid
    ps_ops.change_primary_storage_state(ps_uuid, 'maintain')
    if not test_lib.lib_wait_target_down(vm.get_vm().vmNics[0].ip, '22', 90):
        test_util.test_fail('VM is expected to stop when PS change to maintain state')

    vm.set_state(vm_header.STOPPED)
    vm.check()
    volume.recover()
    volume.check()

    ps_ops.change_primary_storage_state(ps_uuid, 'enable')
    host_ops.reconnect_host(host_uuid)
    #vm_ops.reconnect_vr(vr_uuid)
    vrs = test_lib.lib_get_all_vrs()
    for vr in vrs:
        vm_ops.start_vm(vr.uuid)  

    vm.start()
    vm.check()

    volume.delete()
    #volume.expunge()
    volume.check()

    vm.destroy()
    test_lib.lib_set_delete_policy('vm', 'Direct')
    test_lib.lib_set_delete_policy('volume', 'Direct')
    test_util.test_pass('Delete volume under PS maintain mode Test Success')
Ejemplo n.º 11
0
def test():
    global vm
    global test_host
    global ps_uuid
    global host_ip
    global max_attempts
    global storagechecker_timeout

    allow_ps_list = [inventory.LOCAL_STORAGE_TYPE]
    test_lib.skip_test_when_ps_type_not_in_list(allow_ps_list)

    if test_lib.lib_get_ha_enable() != 'true':
        test_util.test_skip("vm ha not enabled. Skip test")

    vm_creation_option = test_util.VmOption()
    image_name = os.environ.get('imageName_net')
    image_uuid = test_lib.lib_get_image_by_name(image_name).uuid
    l3_name = os.environ.get('l3VlanNetworkName1')
    l3_net_uuid = test_lib.lib_get_l3_by_name(l3_name).uuid
    test_lib.clean_up_all_vr()

    conditions = res_ops.gen_query_conditions('type', '=', 'UserVm')
    instance_offering_uuid = res_ops.query_resource(res_ops.INSTANCE_OFFERING,
                                                    conditions)[0].uuid
    vm_creation_option.set_l3_uuids([l3_net_uuid])
    vm_creation_option.set_image_uuid(image_uuid)
    vm_creation_option.set_instance_offering_uuid(instance_offering_uuid)
    vm_creation_option.set_name('ls_vm_ha_self_start')
    vm = test_vm_header.ZstackTestVm()
    vm.set_creation_option(vm_creation_option)
    vm.create()

    ha_ops.set_vm_instance_ha_level(vm.get_vm().uuid, "NeverStop")

    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
    ps_uuid = ps.uuid
    ps_ops.change_primary_storage_state(ps_uuid, 'maintain')
    if not test_lib.lib_wait_target_down(vm.get_vm().vmNics[0].ip, '22', 90):
        test_util.test_fail(
            'VM is expected to stop when PS change to maintain state')

    vm.set_state(vm_header.STOPPED)
    vm.check()

    ps_ops.change_primary_storage_state(ps_uuid, 'enable')

    for i in range(0, 300):
        if res_ops.query_resource(res_ops.VM_INSTANCE,
                                  conditions)[0].state == "Running":
            break
        time.sleep(1)
    else:
        test_util.test_fail(
            "vm has not been changed to running as expected within 300s.")

    vm.destroy()

    test_util.test_pass(
        'Test checking VM ha and none status when force stop vm Success.')
def test():
    global test_obj_dict
    global ps_uuid
    global host_uuid
    global vr_uuid
    test_util.test_dsc('Create test vm and check')
    test_lib.lib_set_delete_policy('vm', 'Delay')
    test_lib.lib_set_delete_policy('volume', 'Delay')
    l3_1_name = os.environ.get('l3VlanNetworkName1')
    vm = test_stub.create_vlan_vm(l3_name=l3_1_name)
    #l3_1 = test_lib.lib_get_l3_by_name(l3_1_name)
    #vr = test_lib.lib_find_vr_by_l3_uuid(l3_1.uuid)[0]
    #vr_uuid = vr.uuid
    
    host = test_lib.lib_get_vm_host(vm.get_vm())
    host_uuid = host.uuid
    test_obj_dict.add_vm(vm)
    vm.check()

    disk_offering = test_lib.lib_get_disk_offering_by_name(os.environ.get('rootDiskOfferingName'))
    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_disk_offering_uuid(disk_offering.uuid)
    volume_creation_option.set_system_tags(['ephemeral::shareable', 'capability::virtio-scsi'])
    volume = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume)
    volume.check()
    volume.delete()
    volume.check()

    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
    ps_uuid = ps.uuid
    ps_ops.change_primary_storage_state(ps_uuid, 'maintain')
    if not test_lib.lib_wait_target_down(vm.get_vm().vmNics[0].ip, '22', 90):
        test_util.test_fail('VM is expected to stop when PS change to maintain state')

    vm.set_state(vm_header.STOPPED)
    vm.check()
    volume.recover()
    volume.check()

    ps_ops.change_primary_storage_state(ps_uuid, 'enable')
    host_ops.reconnect_host(host_uuid)
    #vm_ops.reconnect_vr(vr_uuid)
    vrs = test_lib.lib_get_all_vrs()
    for vr in vrs:
        vm_ops.start_vm(vr.uuid)  

    vm.start()
    vm.check()

    volume.delete()
    #volume.expunge()
    volume.check()

    vm.destroy()
    test_lib.lib_set_delete_policy('vm', 'Direct')
    test_lib.lib_set_delete_policy('volume', 'Direct')
    test_util.test_pass('Delete volume under PS maintain mode Test Success')
def test():
    global vm
    global test_host
    global ps_uuid
    global host_ip
    global max_attempts
    global storagechecker_timeout


    allow_ps_list = [inventory.LOCAL_STORAGE_TYPE]
    test_lib.skip_test_when_ps_type_not_in_list(allow_ps_list)

    if test_lib.lib_get_ha_enable() != 'true':
        test_util.test_skip("vm ha not enabled. Skip test")

    vm_creation_option = test_util.VmOption()
    image_name = os.environ.get('imageName_net')
    image_uuid = test_lib.lib_get_image_by_name(image_name).uuid
    l3_name = os.environ.get('l3VlanNetworkName1')
    l3_net_uuid = test_lib.lib_get_l3_by_name(l3_name).uuid
    test_lib.clean_up_all_vr()

    conditions = res_ops.gen_query_conditions('type', '=', 'UserVm')
    instance_offering_uuid = res_ops.query_resource(res_ops.INSTANCE_OFFERING, conditions)[0].uuid
    vm_creation_option.set_l3_uuids([l3_net_uuid])
    vm_creation_option.set_image_uuid(image_uuid)
    vm_creation_option.set_instance_offering_uuid(instance_offering_uuid)
    vm_creation_option.set_name('ls_vm_ha_self_start')
    vm = test_vm_header.ZstackTestVm()
    vm.set_creation_option(vm_creation_option)
    vm.create()


    ha_ops.set_vm_instance_ha_level(vm.get_vm().uuid, "NeverStop")

    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
    ps_uuid = ps.uuid
    ps_ops.change_primary_storage_state(ps_uuid, 'maintain')
    if not test_lib.lib_wait_target_down(vm.get_vm().vmNics[0].ip, '22', 90):
        test_util.test_fail('VM is expected to stop when PS change to maintain state')

    vm.set_state(vm_header.STOPPED)
    vm.check()
	
    ps_ops.change_primary_storage_state(ps_uuid, 'enable')

    for i in range(0, 300):
        if res_ops.query_resource(res_ops.VM_INSTANCE, conditions)[0].state == "Running":
            break
        time.sleep(1)
    else:
        test_util.test_fail("vm has not been changed to running as expected within 300s.")

    vm.destroy()

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

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

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

    new_image.add_root_volume_template()

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

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

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

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

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

    test_util.test_pass('imagecache cleanup Pass.')
def test():
    vm1 = test_stub.create_vr_vm('vm_imagecache', 'imageName_net',
                                 'l3VlanNetwork3')
    test_obj_dict.add_vm(vm1)
    vm1.check()
    vm1.stop()
    image_creation_option = test_util.ImageOption()
    backup_storage_list = test_lib.lib_get_backup_storage_list_by_vm(
        vm1.get_vm())
    image_creation_option.set_backup_storage_uuid_list(
        [backup_storage_list[0].uuid])
    image_creation_option.set_root_volume_uuid(vm1.get_vm().rootVolumeUuid)
    image_creation_option.set_name('test_create_image_template_imagecache')

    image = test_image.ZstackTestImage()
    image.set_creation_option(image_creation_option)
    image.create()
    test_obj_dict.add_image(image)
    image.check()
    vm2 = test_stub.create_vm('vm_imagecache2',
                              'test_create_image_template_imagecache',
                              os.environ.get('l3VlanNetwork3'))
    test_obj_dict.add_vm(vm2)

    host = test_lib.lib_find_host_by_vm(vm2.get_vm())
    ps = test_lib.lib_get_primary_storage_by_vm(vm2.get_vm())
    image = test_lib.lib_get_image_by_name(
        'test_create_image_template_imagecache')
    img_ops.sync_image_size(image.uuid)
    image = test_lib.lib_get_image_by_name(
        'test_create_image_template_imagecache')
    img_ops.delete_image(image.uuid)

    if ps.type == inventory.LOCAL_STORAGE_TYPE or ps.type == inventory.NFS_PRIMARY_STORAGE_TYPE or ps.type == 'SharedMountPoint':
        image_cache_path = "%s/imagecache/template/%s/" % (ps.mountPath,
                                                           image.uuid)
        imagecache_file_size = int(
            test_lib.lib_get_file_size(host, image_cache_path))
        image_actual_size = int(image.actualSize)
        if imagecache_file_size < image.actualSize * 0.99 or imagecache_file_size > image.actualSize * 1.01:
            test_util.test_fail(
                'image cache size (%s) not match image actual size(%s)' %
                (imagecache_file_size, image_actual_size))
    else:
        test_util.test_skip(
            "Skip test when primary storage is not local or NFS")
#    elif ps.type == inventory.CEPH_PRIMARY_STORAGE_TYPE:

    test_lib.lib_error_cleanup(test_obj_dict)
    test_util.test_pass('imagecache cleanup Pass.')
def migrate(vm_obj):
    ps = test_lib.lib_get_primary_storage_by_vm(vm_obj.get_vm())
    if vm_obj.vm.state == "Running" and ps.type in [inventory.CEPH_PRIMARY_STORAGE_TYPE, 'SharedMountPoint', inventory.NFS_PRIMARY_STORAGE_TYPE,
                   'SharedBlock', inventory.LOCAL_STORAGE_TYPE]:
        target_host = test_lib.lib_find_random_host(vm_obj.vm)
        vm_obj.migrate(target_host.uuid)
    elif ps.type in [inventory.LOCAL_STORAGE_TYPE]:
        vm_obj.check()
        target_host = test_lib.lib_find_random_host(vm_obj.vm)
        vol_ops.migrate_volume(vm_obj.get_vm().allVolumes[0].uuid, target_host.uuid)
        vm_obj.start()
        test_lib.lib_wait_target_up(vm_obj.get_vm().vmNics[0].ip, 22, 300)
    else:
        test_util.test_fail("FOUND NEW STORAGTE TYPE. FAILED")
Ejemplo n.º 17
0
def test():
    global test_obj_dict
    global ps_uuid
    global host_uuid
    global vr_uuid
    test_util.test_dsc('Create test vm and check')
    l3_1_name = os.environ.get('l3VlanNetworkName1')
    vm = test_stub.create_vlan_vm(l3_name=l3_1_name)
    l3_1 = test_lib.lib_get_l3_by_name(l3_1_name)
    vr = test_lib.lib_find_vr_by_l3_uuid(l3_1.uuid)[0]
    vr_uuid = vr.uuid

    #l3_1 = test_lib.lib_get_l3_by_name(l3_1_name)
    host = test_lib.lib_get_vm_host(vm.get_vm())
    host_uuid = host.uuid
    test_obj_dict.add_vm(vm)
    vm.check()

    disk_offering = test_lib.lib_get_disk_offering_by_name(
        os.environ.get('rootDiskOfferingName'))
    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_disk_offering_uuid(disk_offering.uuid)
    #volume_creation_option.set_system_tags(['ephemeral::shareable', 'capability::virtio-scsi'])
    volume = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume)
    volume.check()
    volume.attach(vm)

    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
    ps_uuid = ps.uuid
    ps_ops.change_primary_storage_state(ps_uuid, 'disable')
    if not test_lib.lib_wait_target_up(vm.get_vm().vmNics[0].ip, '22', 90):
        test_util.test_fail(
            'VM is expected to running when PS change to disable state')

    vm.set_state(vm_header.RUNNING)
    vm.check()
    volume.detach(vm.get_vm().uuid)

    ps_ops.change_primary_storage_state(ps_uuid, 'enable')
    host_ops.reconnect_host(host_uuid)
    vm_ops.reconnect_vr(vr_uuid)

    volume.delete()
    #volume.expunge()
    volume.check()
    vm.destroy()

    test_util.test_pass('Delete volume under PS disable mode Test Success')
def test():
    global test_obj_dict
    test_util.test_dsc('Create test vm on each ps and check')
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)
    pss = res_ops.query_resource_with_num(res_ops.PRIMARY_STORAGE, cond)
    for ps in pss:
        vm = test_stub.create_specified_ps_vm(ps_uuid = ps.uuid)
        test_obj_dict.add_vm(vm)
        vm.check()
        if test_lib.lib_get_primary_storage_by_vm(vm.get_vm()).uuid != ps.uuid:
            test_util.test_pass('[vm:] %s is expected to create on [ps:] %s' % (vm.get_vm().uuid, ps.uuid))
        vm.destroy()

    test_util.test_pass('Create VirtualRouter VM on each PS Test Success')
def test():
    global test_obj_dict
    test_util.test_dsc('Create test vm on each ps and check')
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)
    pss = res_ops.query_resource_with_num(res_ops.PRIMARY_STORAGE, cond)
    for ps in pss:
        vm = test_stub.create_specified_ps_vm(ps_uuid=ps.uuid)
        test_obj_dict.add_vm(vm)
        vm.check()
        if test_lib.lib_get_primary_storage_by_vm(vm.get_vm()).uuid != ps.uuid:
            test_util.test_pass('[vm:] %s is expected to create on [ps:] %s' %
                                (vm.get_vm().uuid, ps.uuid))
        vm.destroy()

    test_util.test_pass('Create VirtualRouter VM on each PS Test Success')
def test():
    global test_obj_dict
    global ps_uuid
    global host_uuid
    global vr_uuid
    test_util.test_dsc('Create test vm and check')
    l3_1_name = os.environ.get('l3VlanNetworkName1')
    vm = test_stub.create_vlan_vm(l3_name=l3_1_name)
    l3_1 = test_lib.lib_get_l3_by_name(l3_1_name)
    vr = test_lib.lib_find_vr_by_l3_uuid(l3_1.uuid)[0]
    vr_uuid = vr.uuid
    
    #l3_1 = test_lib.lib_get_l3_by_name(l3_1_name)
    host = test_lib.lib_get_vm_host(vm.get_vm())
    host_uuid = host.uuid
    test_obj_dict.add_vm(vm)
    vm.check()

    disk_offering = test_lib.lib_get_disk_offering_by_name(os.environ.get('rootDiskOfferingName'))
    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_disk_offering_uuid(disk_offering.uuid)
    #volume_creation_option.set_system_tags(['ephemeral::shareable', 'capability::virtio-scsi'])
    volume = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume)
    volume.check()
    volume.attach(vm)

    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
    ps_uuid = ps.uuid
    ps_ops.change_primary_storage_state(ps_uuid, 'disable')
    if not test_lib.lib_wait_target_up(vm.get_vm().vmNics[0].ip, '22', 90):
        test_util.test_fail('VM is expected to running when PS change to disable state')

    vm.set_state(vm_header.RUNNING)
    vm.check()
    volume.detach(vm.get_vm().uuid)

    ps_ops.change_primary_storage_state(ps_uuid, 'enable')
    host_ops.reconnect_host(host_uuid)
    vm_ops.reconnect_vr(vr_uuid)

    volume.delete()
    #volume.expunge()
    volume.check()
    vm.destroy()

    test_util.test_pass('Delete volume under PS disable mode Test Success')
Ejemplo n.º 21
0
def migrate(vm_obj):
    ps = test_lib.lib_get_primary_storage_by_vm(vm_obj.get_vm())
    if vm_obj.vm.state == "Running" and ps.type in [
            inventory.CEPH_PRIMARY_STORAGE_TYPE, 'SharedMountPoint',
            inventory.NFS_PRIMARY_STORAGE_TYPE, 'SharedBlock',
            inventory.LOCAL_STORAGE_TYPE
    ]:
        target_host = test_lib.lib_find_random_host(vm_obj.vm)
        vm_obj.migrate(target_host.uuid)
    elif ps.type in [inventory.LOCAL_STORAGE_TYPE]:
        vm_obj.check()
        target_host = test_lib.lib_find_random_host(vm_obj.vm)
        vol_ops.migrate_volume(vm_obj.get_vm().allVolumes[0].uuid,
                               target_host.uuid)
        vm_obj.start()
        test_lib.lib_wait_target_up(vm_obj.get_vm().vmNics[0].ip, 22, 300)
    else:
        test_util.test_fail("FOUND NEW STORAGTE TYPE. FAILED")
def test():
    global test_obj_dict
    global ps_uuid
    global host_uuid
    global vr_uuid

    test_util.test_dsc('Create test vm and check')
    vm = test_stub.create_vr_vm('vm1', 'imageName_net', 'l3VlanNetwork3')
    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 == inventory.CEPH_BACKUP_STORAGE_TYPE:
            break
    else:
        vm.destroy()
        test_util.test_skip('Not find ceph type backup storage.')

    l3_1_name = os.environ.get('l3VlanNetwork3')
    l3_1 = test_lib.lib_get_l3_by_name(l3_1_name)
    vr = test_lib.lib_find_vr_by_l3_uuid(l3_1.uuid)[0]
    vr_uuid = vr.uuid

    host = test_lib.lib_get_vm_host(vm.get_vm())
    host_uuid = host.uuid
    test_obj_dict.add_vm(vm)
    vm.check()
    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
    ps_uuid = ps.uuid
    ps_ops.change_primary_storage_state(ps_uuid, 'disable')
    if not test_lib.lib_wait_target_down(vm.get_vm().vmNics[0].ip, '22', 90):
        test_util.test_fail(
            'VM is expected to stop when PS change to disable state')

    vm.set_state(vm_header.STOPPED)
    vm.check()
    test_stub.migrate_vm_to_random_host(vm)
    vm.check()
    volume.check()

    ps_ops.change_primary_storage_state(ps_uuid, 'Enabled')
    host_ops.reconnect_host(host_uuid)
    vm_ops.reconnect_vr(vr_uuid)
    test_util.test_pass('PS disable mode Test Success')
def test():
    global test_obj_dict
    global ps_uuid
    global host_uuid
    global vr_uuid

    test_util.test_dsc('Create test vm and check')
    vm = test_stub.create_vr_vm('vm1', 'imageName_net', 'l3VlanNetwork3')
    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 == inventory.CEPH_BACKUP_STORAGE_TYPE:
            break
    else:
        vm.destroy()
        test_util.test_skip('Not find ceph type backup storage.')

    l3_1_name = os.environ.get('l3VlanNetwork3')
    l3_1 = test_lib.lib_get_l3_by_name(l3_1_name)
    vr = test_lib.lib_find_vr_by_l3_uuid(l3_1.uuid)[0]
    vr_uuid = vr.uuid
    
    host = test_lib.lib_get_vm_host(vm.get_vm())
    host_uuid = host.uuid
    test_obj_dict.add_vm(vm)
    vm.check()
    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
    ps_uuid = ps.uuid
    ps_ops.change_primary_storage_state(ps_uuid, 'disable')
    if not test_lib.lib_wait_target_down(vm.get_vm().vmNics[0].ip, '22', 90):
        test_util.test_fail('VM is expected to stop when PS change to disable state')

    vm.set_state(vm_header.STOPPED)
    vm.check()
    test_stub.migrate_vm_to_random_host(vm)
    vm.check()
    volume.check()

    ps_ops.change_primary_storage_state(ps_uuid, 'Enabled')
    host_ops.reconnect_host(host_uuid)
    vm_ops.reconnect_vr(vr_uuid)
    test_util.test_pass('PS disable mode Test Success')
def test():
    vm = test_stub.create_vr_vm('vm_imagecache', 'imageName_net', 'l3VlanNetwork3')
    test_obj_dict.add_vm(vm)
    vm.check()
    host = test_lib.lib_find_host_by_vm(vm.get_vm())
    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
    image = test_lib.lib_get_image_by_name(os.environ.get('imageName_net'))

    if ps.type == inventory.LOCAL_STORAGE_TYPE or ps.type == inventory.NFS_PRIMARY_STORAGE_TYPE or ps.type == 'SharedMountPoint':
        image_cache_path = "%s/imagecache/template/%s/" % (ps.mountPath, image.uuid)
        imagecache_file_size = int(test_lib.lib_get_file_size(host, image_cache_path))
        image_actual_size = int(image.actualSize)
        if imagecache_file_size < image.actualSize*0.99 or imagecache_file_size > image.actualSize*1.01:
            test_util.test_fail('image cache size (%s) not match image actual size(%s)' % (imagecache_file_size, image_actual_size))
    else:
        test_util.test_skip("Skip test when primary storage is not local or NFS")
#    elif ps.type == inventory.CEPH_PRIMARY_STORAGE_TYPE:

    test_lib.lib_error_cleanup(test_obj_dict)
    test_util.test_pass('imagecache cleanup Pass.')
Ejemplo n.º 25
0
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')
def test():
    vm1 = test_stub.create_vr_vm('vm_imagecache', 'imageName_net', 'l3VlanNetwork3')
    test_obj_dict.add_vm(vm1)
    vm1.check()
    vm1.stop()
    image_creation_option = test_util.ImageOption()
    backup_storage_list = test_lib.lib_get_backup_storage_list_by_vm(vm1.get_vm())
    image_creation_option.set_backup_storage_uuid_list([backup_storage_list[0].uuid])
    image_creation_option.set_root_volume_uuid(vm1.get_vm().rootVolumeUuid)
    image_creation_option.set_name('test_create_image_template_imagecache')

    image = test_image.ZstackTestImage()
    image.set_creation_option(image_creation_option)
    image.create()
    test_obj_dict.add_image(image)
    image.check()
    vm2 = test_stub.create_vm('vm_imagecache2', 'test_create_image_template_imagecache', os.environ.get('l3VlanNetwork3'))
    test_obj_dict.add_vm(vm2)

    host = test_lib.lib_find_host_by_vm(vm2.get_vm())
    ps = test_lib.lib_get_primary_storage_by_vm(vm2.get_vm())
    image = test_lib.lib_get_image_by_name('test_create_image_template_imagecache')
    img_ops.sync_image_size(image.uuid)
    image = test_lib.lib_get_image_by_name('test_create_image_template_imagecache')
    img_ops.delete_image(image.uuid)

    if ps.type == inventory.LOCAL_STORAGE_TYPE or ps.type == inventory.NFS_PRIMARY_STORAGE_TYPE or ps.type == 'SharedMountPoint':
        image_cache_path = "%s/imagecache/template/%s/" % (ps.mountPath, image.uuid)
        imagecache_file_size = int(test_lib.lib_get_file_size(host, image_cache_path))
        image_actual_size = int(image.actualSize)
        if imagecache_file_size < image.actualSize*0.99 or imagecache_file_size > image.actualSize*1.01:
            test_util.test_fail('image cache size (%s) not match image actual size(%s)' % (imagecache_file_size, image_actual_size))
    else:
        test_util.test_skip("Skip test when primary storage is not local or NFS")
#    elif ps.type == inventory.CEPH_PRIMARY_STORAGE_TYPE:

    test_lib.lib_error_cleanup(test_obj_dict)
    test_util.test_pass('imagecache cleanup Pass.')
def test():
    global vm
    global host_uuid
    global test_host
    global host_ip
    global max_attempts
    global storagechecker_timeout

    allow_ps_list = [inventory.LOCAL_STORAGE_TYPE]
    test_lib.skip_test_when_ps_type_not_in_list(allow_ps_list)

    if test_lib.lib_get_ha_enable() != 'true':
        test_util.test_skip("vm ha not enabled. Skip test")
    
    test_lib.lib_skip_if_ps_num_is_not_eq_number(2)

    vm_creation_option = test_util.VmOption()
    image_name = os.environ.get('imageName_net')
    image_uuid = test_lib.lib_get_image_by_name(image_name).uuid
    l3_name = os.environ.get('l3VlanNetworkName1')
    l3_net_uuid = test_lib.lib_get_l3_by_name(l3_name).uuid
    test_lib.clean_up_all_vr()

    mn_ip = res_ops.query_resource(res_ops.MANAGEMENT_NODE)[0].hostName
    conditions = res_ops.gen_query_conditions('type', '=', 'UserVm')
    instance_offering_uuid = res_ops.query_resource(res_ops.INSTANCE_OFFERING, conditions)[0].uuid
    conditions = res_ops.gen_query_conditions('state', '=', 'Enabled')
    conditions = res_ops.gen_query_conditions('status', '=', 'Connected', conditions)
    conditions = res_ops.gen_query_conditions('managementIp', '!=', mn_ip, conditions)
    host_uuid = res_ops.query_resource(res_ops.HOST, conditions)[0].uuid
    vm_creation_option.set_host_uuid(host_uuid)
    vm_creation_option.set_l3_uuids([l3_net_uuid])
    vm_creation_option.set_image_uuid(image_uuid)
    vm_creation_option.set_instance_offering_uuid(instance_offering_uuid)
    vm_creation_option.set_name('ls_vm_ha_self_start')
    vm = test_vm_header.ZstackTestVm()
    vm.set_creation_option(vm_creation_option)
    vm.create()

    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
    if ps.type == inventory.LOCAL_STORAGE_TYPE:
        test_util.test_skip('unable to live migrate vm with volumes on local storage, skip test.')
    test_stub.ensure_all_vrs_on_host(host_uuid)

    #vm.check()
    host_ip = test_lib.lib_find_host_by_vm(vm.get_vm()).managementIp
    test_util.test_logger("host %s is disconnecting" %(host_ip))

    ha_ops.set_vm_instance_ha_level(vm.get_vm().uuid, "NeverStop")

    host_list = test_stub.get_sce_hosts(test_lib.all_scenario_config, test_lib.scenario_file)
    for host in host_list:
        if host.ip_ == host_ip:
            test_host = host
            break
    if not test_host:
        test_util.test_fail('there is no host with ip %s in scenario file.' %(host_ip))

    test_stub.stop_host(test_host, test_lib.all_scenario_config, 'cold')

    vm_stop_time = None
    cond = res_ops.gen_query_conditions('name', '=', 'ls_vm_ha_self_start')
    cond = res_ops.gen_query_conditions('uuid', '=', vm.vm.uuid, cond)
    for i in range(0, 300):
        vm_stop_time = i
        if res_ops.query_resource(res_ops.VM_INSTANCE, cond)[0].state == "Stopped":
            test_stub.start_host(test_host, test_lib.all_scenario_config)
            test_stub.recover_host_vlan(test_host, test_lib.all_scenario_config, test_lib.deploy_config)
            break
        time.sleep(1)
    if vm_stop_time is None:
        vm_stop_time = 300    
    for i in range(vm_stop_time, 300):
        if res_ops.query_resource(res_ops.VM_INSTANCE, cond)[0].state == "Starting":
            break
        time.sleep(1)
    else:
        test_util.test_fail("vm has not been changed to running as expected within 300s.")


    vm.destroy()

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

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

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

    new_image.add_root_volume_template()

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

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

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

    vm.destroy()
    vm.expunge()

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

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

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

    new_image.delete()
    new_image.expunge()

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

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

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

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

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

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

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

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

    new_image.add_root_volume_template()

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

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

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

    vm.destroy()
    vm.expunge()

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

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

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

    new_image.delete()
    new_image.expunge()

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

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

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

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

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

    test_util.test_pass('imagecache cleanup Pass.')
def test():
    global test_obj_dict
    global ps_uuid
    global host_uuid
    global vr_uuid

    test_util.test_dsc('Create test vm and check')
    image_name = os.environ.get('imageName_net')
    image_uuid = test_lib.lib_get_image_by_name(image_name).uuid
    l3_name = os.environ.get('l3VlanDNATNetworkName')
    l3_net_uuid = test_lib.lib_get_l3_by_name(l3_name).uuid
    l3_net_list = [l3_net_uuid]
    l3_name2 = os.environ.get('l3VlanNetworkName1')
    l3_net_uuid2 = test_lib.lib_get_l3_by_name(l3_name2).uuid
    

    vm = test_stub.create_vm(l3_net_list, image_uuid, 'attach_nic_vm', \
            default_l3_uuid = l3_net_uuid)
    test_obj_dict.add_vm(vm)
    vm.check()
    l3_1 = test_lib.lib_get_l3_by_name(l3_name)
    vr = test_lib.lib_find_vr_by_l3_uuid(l3_1.uuid)[0]
    vr_uuid = vr.uuid
    
    host = test_lib.lib_get_vm_host(vm.get_vm())
    host_uuid = host.uuid
    test_obj_dict.add_vm(vm)
    vm.check()
    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
    ps_uuid = ps.uuid


    vm.add_nic(l3_net_uuid2)
    attached_nic = test_lib.lib_get_vm_last_nic(vm.get_vm())
    if l3_net_uuid2 != attached_nic.l3NetworkUuid:
        test_util.test_fail("After attach a nic, VM:%s last nic is not belong l3: %s" % (vm.get_vm().uuid, l3_net_uuid2))

    test_lib.lib_restart_vm_network(vm.get_vm())
    vm.check()

    ps_ops.change_primary_storage_state(ps_uuid, 'disable')
    if not test_lib.lib_wait_target_up(vm.get_vm().vmNics[0].ip, '22', 90):
        test_util.test_fail('VM is expected to running when PS change to disable state')
    vm.set_state(vm_header.RUNNING)

    vm.remove_nic(attached_nic.uuid)
    attached_nic = test_lib.lib_get_vm_last_nic(vm.get_vm())
    if l3_net_uuid != attached_nic.l3NetworkUuid:
        test_util.test_fail("After detached NIC, VM:%s only nic is not belong l3: %s" % (vm.get_vm().uuid, l3_net_uuid2))

    vm.add_nic(l3_net_uuid2)
    attached_nic = test_lib.lib_get_vm_last_nic(vm.get_vm())
    if l3_net_uuid2 != attached_nic.l3NetworkUuid:
        test_util.test_fail("After attach a nic, VM:%s last nic is not belong l3: %s" % (vm.get_vm().uuid, l3_net_uuid2))

    test_lib.lib_restart_vm_network(vm.get_vm())
    vm.check()

    ps_ops.change_primary_storage_state(ps_uuid, 'enable')
    host_ops.reconnect_host(host_uuid)
    vm_ops.reconnect_vr(vr_uuid)
    vm.check()
    vm.destroy()
    vm.check()
    #vm.expunge()
    test_util.test_pass('PS disable mode Test Success')
def test():
    vm1 = test_stub.create_vr_vm('maintain_host_vm1', 'imageName_s', 'l3VlanNetwork2')
    test_obj_dict.add_vm(vm1)

    vm2 = test_stub.create_vr_vm('maintain_host_vm2', 'imageName_s', 'l3VlanNetwork2')
    test_obj_dict.add_vm(vm2)

    vm1.check()
    vm2.check()
    if not test_lib.lib_check_vm_live_migration_cap(vm1.vm) or not test_lib.lib_check_vm_live_migration_cap(vm2.vm):
        test_util.test_skip('skip migrate if live migrate not supported')

    current_host1 = test_lib.lib_get_vm_host(vm1.vm)
    current_host2 = test_lib.lib_get_vm_host(vm2.vm)
    conditions = res_ops.gen_query_conditions('clusterUuid', '=', vm1.vm.clusterUuid)
    conditions = res_ops.gen_query_conditions('state', '=', host_header.ENABLED, conditions)
    conditions = res_ops.gen_query_conditions('status', '=', host_header.CONNECTED, conditions)
    all_hosts = res_ops.query_resource(res_ops.HOST, conditions)
    if len(all_hosts) <= 1:
        test_util.test_fail('Not available host to do maintenance, since there is only %s host' % len(all_hosts))
    vr = test_lib.lib_get_all_vrs()
    if len(vr) == 0:
        test_util.test_skip('Skip test if not using vr')
    vr_uuid = vr[0].uuid
    vr_host_uuid = test_lib.lib_get_vm_host(vr[0]).uuid

    for host_n in all_hosts:
        print'host_n%s'% (host_n.uuid)
        if host_n.uuid != current_host1.uuid:
            if host_n.uuid != current_host2.uuid:
                if host_n.uuid != vr_host_uuid:
                    target_host = host_n
                    print'target_host_uuid%s'%(target_host.uuid)
                    vm1.migrate(target_host.uuid)
                    vm2.migrate(target_host.uuid)
                    break   
    else:
        test_util.test_skip('can not find a host to migrate two host')
    new_host = test_lib.lib_get_vm_host(vm1.vm)
    if new_host.uuid != target_host.uuid:
        test_util.test_fail('VM did not migrate to target [host:] %s, but to [host:] %s' % (target_host.uuid, new_host.uuid))

    new_host1 = test_lib.lib_get_vm_host(vm2.vm)
    if new_host1.uuid != target_host.uuid:
        test_util.test_fail('VM did not migrate to target [host:] %s, but to [host:] %s' % (target_host.uuid, new_host1.uuid))

    host = test_kvm_host.ZstackTestKvmHost()
    host.set_host(target_host)
    host.maintain()
    #need to update vm's inventory, since they will be changed by maintenace mode
    vm1.update()
    vm2.update()

    ps = test_lib.lib_get_primary_storage_by_vm(vm1.get_vm())
    if ps.type == inventory.LOCAL_STORAGE_TYPE:
        vm1.set_state(vm_header.STOPPED)
        vm2.set_state(vm_header.STOPPED)
    vm1.check()
    vm2.check()
    host.change_state(test_kvm_host.ENABLE_EVENT)
    if not linux.wait_callback_success(is_host_connected, host.get_host().uuid, 120):
        test_util.test_fail('host status is not changed to connected or host state is not changed to Enabled within 120s')

    if ps.type == inventory.LOCAL_STORAGE_TYPE:
        vm1.start()
        vm2.start()

    vm1.set_state(vm_header.RUNNING)
    vm2.set_state(vm_header.RUNNING)
    vm1.check()
    vm2.check()
    
    post_host1 = test_lib.lib_get_vm_host(vm1.vm)
    post_host2 = test_lib.lib_get_vm_host(vm2.vm)

    if post_host1.uuid != current_host1.uuid:
        vm1.migrate(current_host1.uuid)

    if post_host2.uuid != current_host2.uuid:
        vm2.migrate(current_host2.uuid)

    vm1.check()
    vm2.check()

    vm1.destroy()
    test_obj_dict.rm_vm(vm1)
    vm2.destroy()
    test_obj_dict.rm_vm(vm2)
    test_util.test_pass('Maintain Host Test Success')
def test():
    vm1 = test_stub.create_vr_vm('maintain_host_vm1', 'imageName_s',
                                 'l3VlanNetwork2')
    test_obj_dict.add_vm(vm1)

    vm2 = test_stub.create_vr_vm('maintain_host_vm2', 'imageName_s',
                                 'l3VlanNetwork2')
    test_obj_dict.add_vm(vm2)

    vm1.check()
    vm2.check()
    if not test_lib.lib_check_vm_live_migration_cap(
            vm1.vm) or not test_lib.lib_check_vm_live_migration_cap(vm2.vm):
        test_util.test_skip('skip migrate if live migrate not supported')

    current_host1 = test_lib.lib_get_vm_host(vm1.vm)
    current_host2 = test_lib.lib_get_vm_host(vm2.vm)
    conditions = res_ops.gen_query_conditions('clusterUuid', '=',
                                              vm1.vm.clusterUuid)
    conditions = res_ops.gen_query_conditions('state', '=',
                                              host_header.ENABLED, conditions)
    conditions = res_ops.gen_query_conditions('status', '=',
                                              host_header.CONNECTED,
                                              conditions)
    all_hosts = res_ops.query_resource(res_ops.HOST, conditions)
    if len(all_hosts) <= 1:
        test_util.test_fail(
            'Not available host to do maintenance, since there is only %s host'
            % len(all_hosts))
    vr = test_lib.lib_get_all_vrs()
    if len(vr) == 0:
        test_util.test_skip('Skip test if not using vr')
    vr_uuid = vr[0].uuid
    vr_host_uuid = test_lib.lib_get_vm_host(vr[0]).uuid

    for host_n in all_hosts:
        print 'host_n%s' % (host_n.uuid)
        if host_n.uuid != current_host1.uuid:
            if host_n.uuid != current_host2.uuid:
                if host_n.uuid != vr_host_uuid:
                    target_host = host_n
                    print 'target_host_uuid%s' % (target_host.uuid)
                    vm1.migrate(target_host.uuid)
                    vm2.migrate(target_host.uuid)
                    break
    else:
        test_util.test_skip('can not find a host to migrate two host')
    new_host = test_lib.lib_get_vm_host(vm1.vm)
    if new_host.uuid != target_host.uuid:
        test_util.test_fail(
            'VM did not migrate to target [host:] %s, but to [host:] %s' %
            (target_host.uuid, new_host.uuid))

    new_host1 = test_lib.lib_get_vm_host(vm2.vm)
    if new_host1.uuid != target_host.uuid:
        test_util.test_fail(
            'VM did not migrate to target [host:] %s, but to [host:] %s' %
            (target_host.uuid, new_host1.uuid))

    host = test_kvm_host.ZstackTestKvmHost()
    host.set_host(target_host)
    host.maintain()
    #need to update vm's inventory, since they will be changed by maintenace mode
    vm1.update()
    vm2.update()

    ps = test_lib.lib_get_primary_storage_by_vm(vm1.get_vm())
    if ps.type == inventory.LOCAL_STORAGE_TYPE:
        vm1.set_state(vm_header.STOPPED)
        vm2.set_state(vm_header.STOPPED)
    vm1.check()
    vm2.check()
    host.change_state(test_kvm_host.ENABLE_EVENT)
    if not linux.wait_callback_success(is_host_connected,
                                       host.get_host().uuid, 120):
        test_util.test_fail(
            'host status is not changed to connected or host state is not changed to Enabled within 120s'
        )

    if ps.type == inventory.LOCAL_STORAGE_TYPE:
        vm1.start()
        vm2.start()

    vm1.set_state(vm_header.RUNNING)
    vm2.set_state(vm_header.RUNNING)
    vm1.check()
    vm2.check()

    post_host1 = test_lib.lib_get_vm_host(vm1.vm)
    post_host2 = test_lib.lib_get_vm_host(vm2.vm)

    if post_host1.uuid != current_host1.uuid:
        vm1.migrate(current_host1.uuid)

    if post_host2.uuid != current_host2.uuid:
        vm2.migrate(current_host2.uuid)

    vm1.check()
    vm2.check()

    vm1.destroy()
    test_obj_dict.rm_vm(vm1)
    vm2.destroy()
    test_obj_dict.rm_vm(vm2)
    test_util.test_pass('Maintain Host Test Success')
def running_vm_operations(vm,bss):
      
   numa = config_ops.get_global_config_value('vm', 'numa')
   live_migration = config_ops.get_global_config_value('localStoragePrimaryStorage','liveMigrationWithStorage.allow')
   ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
   vm_uuid = vm.get_vm().uuid
   #change vm's instanceoffering
   if numa == 'false':
      try:
         vm_ops.update_vm(vm_uuic,2,2048*1024*1024)
         test_util.test_fail('Test Fail.Cannot change instanceoffering of running vm when NUMA is false')
      except:
         config_ops.change_global_config('vm','numa','true')
   
   vm_ops.reboot_vm(vm_uuid)
   vm_ops.update_vm(vm_uuid,2,2048*1024*1024)

   #Change vm's status;set ha level/stop/del ha level/reboot/pause/resume/force stop
   ha_ops.set_vm_instance_ha_level(vm_uuid,'NeverStop')
   vm_ops.stop_vm(vm_uuid)
   ha_ops.del_vm_instance_ha_level(vm_uuid)
   vm_ops.stop_vm(vm_uuid)
   vm_ops.start_vm(vm_uuid)
   vm_ops.reboot_vm(vm_uuid)
   vm_ops.suspend_vm(vm_uuid)
   vm_ops.resume_vm(vm_uuid)
   vm_ops.stop_vm(vm_uuid,'cold')
   vm_ops.start_vm(vm_uuid)

   #clone vms
   vm_ops.clone_vm(vm_uuid,['vm-1','vm-2','vm-3'],'InstantStart')

   #migrate
   candidate_hosts = vm_ops.get_vm_migration_candidate_hosts(vm_uuid)
   migrate_host_uuids = []
   if candidate_hosts == None:
      pass
   else:
      for host in candidate_hosts.inventories:
         migrate_host_uuids.append(host.uuid)
      if ps.type == 'LocalStorage':
         if live_migration == 'false':
       	   try:
       	      vm_ops.migrate_vm(vm_uuid,migrate_host_uuids[0])
       	      test_util.test_fail('Test Fail.Cannot migrate localstorage vm when liveMigrationWithStorage is false.' )
       	   except:
       	      config_ops.change_global_config('localStoragePrimaryStorage','liveMigrationWithStorage.allow','true')
       	 else:
       	    vm_ops.migrate_vm(vm_uuid,migrate_host_uuids[0])
       	    test_util.test_logger('migrate vm success')
      else:
         vm_ops.migrate_vm(vm_uuid,migrate_host_uuids[0])
         test_util.test_logger('migrate vm success')

   #change vm's password(qga)
   try:
      vm_ops.change_vm_password(vm_uuid,'root','testpassword')
      test_util.test_fail('Test Fail.Cannot change vm password when qga is disabled.')
   except:
      vm_ops.set_vm_qga_enable(vm_uuid)
     
   vm_ops.change_vm_password(vm_uuid,'root','testpassword')
   vm_ops.set_vm_qga_disable(vm_uuid)

   #snapshot operations
   sp_option = test_util.SnapshotOption()
   vm_root_volume_inv = test_lib.lib_get_root_volume(vm.get_vm())
   root_volume_uuid = vm_root_volume_inv.uuid
   test_util.test_logger('rootvolumerunning:%s' % root_volume_uuid)
   sp_option.set_volume_uuid(root_volume_uuid)
   sp = vol_ops.create_snapshot(sp_option)
   vm_ops.stop_vm(vm_uuid)
   vol_ops.use_snapshot(sp.uuid)
   vm_ops.start_vm(vm_uuid)
   vol_ops.delete_snapshot(sp.uuid)
   
   common_operations(vm,bss,'running')

   vm_ops.destroy_vm(vm_uuid)
   vm_ops.recover_vm(vm_uuid)
   vm_ops.start_vm(vm_uuid)
   vm.destroy()
   vm.expunge()
def stopped_vm_operations(vm,bss):
   
   vm_uuid = vm.get_vm().uuid
   ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
   vm_ops.stop_vm(vm_uuid)
   #Change vm's instanceoffering
   vm_ops.update_vm(vm_uuid,2,2048*1024*1024)
   
   #Change vm's status
   ha_ops.set_vm_instance_ha_level(vm_uuid,'neverstop')
   vm_ops.stop_vm(vm_uuid)
   ha_ops.del_vm_instance_ha_level(vm_uuid)
   vm_ops.stop_vm(vm_uuid)
   if ps.type != 'LocalStorage':
      target_host_uuid = test_lib.lib_find_random_host(vm.get_vm()).uuid
      if target_host_uuid != None:
         vm_ops.start_vm_with_target_host(vm_uuid,target_host_uuid)
   #vm_ops.stop_vm(vm_uuid)
   vm_ops.destroy_vm(vm_uuid)
   vm_ops.recover_vm(vm_uuid)
   vm_ops.reinit_vm(vm_uuid)

   #clone
   vm_ops.clone_vm(vm_uuid,['vm-1','vm-2','vm-3'],'InstantStart')

   #migrate
   vm_ops.start_vm(vm_uuid)
   candidate_hosts = vm_ops.get_vm_migration_candidate_hosts(vm_uuid)
   vm_ops.stop_vm(vm_uuid)
   migrate_host_uuids = []
   if candidate_hosts != None:
      for host in candidate_hosts.inventories:
         migrate_host_uuids.append(host.uuid)
      if ps.type == 'LocalStorage':
         vol_uuid = test_lib.lib_get_root_volume(vm.get_vm()).uuid
         #vm_ops.migrate_vm(vm_uuid,migrate_host_uuids[0])		
         vol_ops.migrate_volume(vol_uuid, migrate_host_uuids[0])
         test_util.test_logger('migrate vm success')
   
   #resize root volume
   vol_size = test_lib.lib_get_root_volume(vm.get_vm()).size
   vol_uuid = test_lib.lib_get_root_volume(vm.get_vm()).uuid
   set_size = 1024*1024*1024*5
   vol_ops.resize_volume(vol_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)
   test_util.test_logger('resize vm success')
   
   #storage migrate
         
   #change vm image
   image_list = vm_ops.get_image_candidates_for_vm_to_change(vm_uuid)
   image_uuids = []
   for image in image_list.inventories:
      image_uuids.append(image.uuid)
   vm_ops.change_vm_image(vm_uuid,image_uuids[0])
   vm.update()
   
   #update vm nic mac
   nic_uuid = vm.get_vm().vmNics[0].uuid
   mac = 'fa:4c:ee:9a:76:00'
   vm_ops.update_vm_nic_mac(nic_uuid,mac)

   #set/del static ip
   l3network_uuid = test_lib.lib_get_l3_uuid_by_nic(nic_uuid)
   vm_ops.change_vm_static_ip(vm_uuid, l3network_uuid, vm.get_vm().vmNics[0].ip)
   vm_ops.delete_vm_static_ip(vm_uuid, l3network_uuid)
  
   #snapshot operations
   sp_option = test_util.SnapshotOption()
   vm_root_volume_inv = test_lib.lib_get_root_volume(vm.get_vm())
   root_volume_uuid = vm_root_volume_inv.uuid
   sp_option.set_volume_uuid(root_volume_uuid)
   sp = vol_ops.create_snapshot(sp_option)
   vol_ops.use_snapshot(sp.uuid)
   vol_ops.delete_snapshot(sp.uuid)

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

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

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

    new_image.add_root_volume_template()

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

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

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

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

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

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

    ps_ops.cleanup_imagecache_on_primary_storage(ps.uuid)

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

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

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

    test_util.test_pass('imagecache cleanup Pass.')
def running_vm_operations(vm, bss):

    numa = config_ops.get_global_config_value('vm', 'numa')
    live_migration = config_ops.get_global_config_value(
        'localStoragePrimaryStorage', 'liveMigrationWithStorage.allow')
    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
    vm_uuid = vm.get_vm().uuid
    #change vm's instanceoffering
    if numa == 'false':
        try:
            vm_ops.update_vm(vm_uuic, 2, 2048 * 1024 * 1024)
            test_util.test_fail(
                'Test Fail.Cannot change instanceoffering of running vm when NUMA is false'
            )
        except:
            config_ops.change_global_config('vm', 'numa', 'true')

    vm_ops.reboot_vm(vm_uuid)
    vm_ops.update_vm(vm_uuid, 2, 2048 * 1024 * 1024)

    #Change vm's status;set ha level/stop/del ha level/reboot/pause/resume/force stop
    ha_ops.set_vm_instance_ha_level(vm_uuid, 'neverstop')
    vm_ops.stop_vm(vm_uuid)
    ha_ops.del_vm_instance_ha_level(vm_uuid)
    vm_ops.stop_vm(vm_uuid)
    vm_ops.start_vm(vm_uuid)
    vm_ops.reboot_vm(vm_uuid)
    vm_ops.suspend_vm(vm_uuid)
    vm_ops.resume_vm(vm_uuid)
    vm_ops.stop_vm(vm_uuid, 'cold')
    vm_ops.start_vm(vm_uuid)

    #clone vms
    vm_ops.clone_vm(vm_uuid, ['vm-1', 'vm-2', 'vm-3'], 'InstantStart')

    #migrate
    candidate_hosts = vm_ops.get_vm_migration_candidate_hosts(vm_uuid)
    migrate_host_uuids = []
    if candidate_hosts == None:
        pass
    else:
        for host in candidate_hosts.inventories:
            migrate_host_uuids.append(host.uuid)
        if ps.type == 'LocalStorage':
            if live_migration == 'false':
                try:
                    vm_ops.migrate_vm(vm_uuid, migrate_host_uuids[0])
                    test_util.test_fail(
                        'Test Fail.Cannot migrate localstorage vm when liveMigrationWithStorage is false.'
                    )
                except:
                    config_ops.change_global_config(
                        'localStoragePrimaryStorage',
                        'liveMigrationWithStorage.allow', 'true')
            else:
                vm_ops.migrate_vm(vm_uuid, migrate_host_uuids[0])
                test_util.test_logger('migrate vm success')
        else:
            vm_ops.migrate_vm(vm_uuid, migrate_host_uuids[0])
            test_util.test_logger('migrate vm success')

    #change vm's password(qga)
    try:
        vm_ops.change_vm_password(vm_uuid, 'root', 'testpassword')
        test_util.test_fail(
            'Test Fail.Cannot change vm password when qga is disabled.')
    except:
        vm_ops.set_vm_qga_enable(vm_uuid)

    vm_ops.change_vm_password(vm_uuid, 'root', 'testpassword')
    vm_ops.set_vm_qga_disable(vm_uuid)

    #snapshot operations
    sp_option = test_util.SnapshotOption()
    vm_root_volume_inv = test_lib.lib_get_root_volume(vm.get_vm())
    root_volume_uuid = vm_root_volume_inv.uuid
    test_util.test_logger('rootvolumerunning:%s' % root_volume_uuid)
    sp_option.set_volume_uuid(root_volume_uuid)
    sp = vol_ops.create_snapshot(sp_option)
    vm_ops.stop_vm(vm_uuid)
    vol_ops.use_snapshot(sp.uuid)
    vm_ops.start_vm(vm_uuid)
    vol_ops.delete_snapshot(sp.uuid)

    common_operations(vm, bss, 'running')

    vm_ops.destroy_vm(vm_uuid)
    vm_ops.recover_vm(vm_uuid)
    vm_ops.start_vm(vm_uuid)
    vm.destroy()
    vm.expunge()
def test():
    global session_to
    global session_mc

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

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

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

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

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

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

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

    try:
        test_lib.lib_robot_cleanup(test_obj_dict)
    except:
        test_lib.test_logger('Delete VM may timeout')
    test_util.test_pass('Test create 100 snapshots simultaneously success')
def stopped_vm_operations(vm, bss):

    vm_uuid = vm.get_vm().uuid
    ps = test_lib.lib_get_primary_storage_by_vm(vm.get_vm())
    vm_ops.stop_vm(vm_uuid)
    #Change vm's instanceoffering
    vm_ops.update_vm(vm_uuid, 2, 2048 * 1024 * 1024)

    #Change vm's status
    ha_ops.set_vm_instance_ha_level(vm_uuid, 'neverstop')
    vm_ops.stop_vm(vm_uuid)
    ha_ops.del_vm_instance_ha_level(vm_uuid)
    vm_ops.stop_vm(vm_uuid)
    if ps.type != 'LocalStorage':
        target_host_uuid = test_lib.lib_find_random_host(vm.get_vm()).uuid
        if target_host_uuid != None:
            vm_ops.start_vm_with_target_host(vm_uuid, target_host_uuid)
    #vm_ops.stop_vm(vm_uuid)
    vm_ops.destroy_vm(vm_uuid)
    vm_ops.recover_vm(vm_uuid)
    vm_ops.reinit_vm(vm_uuid)

    #clone
    vm_ops.clone_vm(vm_uuid, ['vm-1', 'vm-2', 'vm-3'], 'InstantStart')

    #migrate
    vm_ops.start_vm(vm_uuid)
    candidate_hosts = vm_ops.get_vm_migration_candidate_hosts(vm_uuid)
    vm_ops.stop_vm(vm_uuid)
    migrate_host_uuids = []
    if candidate_hosts != None:
        for host in candidate_hosts.inventories:
            migrate_host_uuids.append(host.uuid)
        if ps.type == 'LocalStorage':
            vol_uuid = test_lib.lib_get_root_volume(vm.get_vm()).uuid
            #vm_ops.migrate_vm(vm_uuid,migrate_host_uuids[0])
            vol_ops.migrate_volume(vol_uuid, migrate_host_uuids[0])
            test_util.test_logger('migrate vm success')

    #resize root volume
    vol_size = test_lib.lib_get_root_volume(vm.get_vm()).size
    vol_uuid = test_lib.lib_get_root_volume(vm.get_vm()).uuid
    set_size = 1024 * 1024 * 1024 * 5
    vol_ops.resize_volume(vol_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)
    test_util.test_logger('resize vm success')

    #storage migrate

    #change vm image
    image_list = vm_ops.get_image_candidates_for_vm_to_change(vm_uuid)
    image_uuids = []
    for image in image_list.inventories:
        image_uuids.append(image.uuid)
    vm_ops.change_vm_image(vm_uuid, image_uuids[0])
    vm.update()

    #update vm nic mac
    nic_uuid = vm.get_vm().vmNics[0].uuid
    mac = 'fa:4c:ee:9a:76:00'
    vm_ops.update_vm_nic_mac(nic_uuid, mac)

    #set/del static ip
    l3network_uuid = test_lib.lib_get_l3_uuid_by_nic(nic_uuid)
    vm_ops.change_vm_static_ip(vm_uuid, l3network_uuid,
                               vm.get_vm().vmNics[0].ip)
    vm_ops.delete_vm_static_ip(vm_uuid, l3network_uuid)

    #snapshot operations
    sp_option = test_util.SnapshotOption()
    vm_root_volume_inv = test_lib.lib_get_root_volume(vm.get_vm())
    root_volume_uuid = vm_root_volume_inv.uuid
    sp_option.set_volume_uuid(root_volume_uuid)
    sp = vol_ops.create_snapshot(sp_option)
    vol_ops.use_snapshot(sp.uuid)
    vol_ops.delete_snapshot(sp.uuid)

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

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

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

    new_image.add_root_volume_template()

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

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

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

    vm.destroy()
    vm.expunge()

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

    new_image.delete()
    new_image.expunge()

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

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

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

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

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

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

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

    new_image.add_root_volume_template()

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

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

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

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

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

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

    ps_ops.cleanup_imagecache_on_primary_storage(ps.uuid)

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

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

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

    test_util.test_pass('imagecache cleanup Pass.')