def test():
    global test_obj_dict
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('type', '=', 'LocalStorage')
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)
    ps = res_ops.query_resource(res_ops.PRIMARY_STORAGE, cond)
    if len(ps) < 2:
        test_util.test_skip("Requres at least two local ps")

    ps1_res = vol_ops.get_local_storage_capacity(None, ps[0].uuid)[0]
    ps2_res = vol_ops.get_local_storage_capacity(None, ps[1].uuid)[0]

    if ps1_res.availableCapacity > ps2_res.availableCapacity:
        data_volume_size = ps2_res.availableCapacity + (ps1_res.availableCapacity - ps2_res.availableCapacity) / 2
    else:
        data_volume_size = ps1_res.availableCapacity + (ps2_res.availableCapacity - ps1_res.availableCapacity) / 2
    disk_offering_option = test_util.DiskOfferingOption()
    disk_offering_option.set_name('2-local-ps-test')
    disk_offering_option.set_diskSize(data_volume_size)
    data_volume_offering = vol_ops.create_volume_offering(disk_offering_option)
    test_obj_dict.add_disk_offering(data_volume_offering)
    vm = test_stub.create_vlan_vm(disk_offering_uuids=[data_volume_offering.uuid])
    test_obj_dict.add_vm(vm)

    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass('2 Local PS Test Pass')
    return False
def test():
    global test_obj_dict
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('type', '=', 'LocalStorage', cond)
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)
    ps = res_ops.query_resource(res_ops.PRIMARY_STORAGE, cond)
    if len(ps) < 2:
        test_util.test_skip("Requres at least two local ps")

    ps1_res = vol_ops.get_local_storage_capacity(None, ps[0].uuid)[0]
    ps2_res = vol_ops.get_local_storage_capacity(None, ps[1].uuid)[0]

    if ps1_res.availableCapacity > ps2_res.availableCapacity:
        data_volume_size = ps2_res.availableCapacity + (
            ps1_res.availableCapacity - ps2_res.availableCapacity) / 2
    else:
        data_volume_size = ps1_res.availableCapacity + (
            ps2_res.availableCapacity - ps1_res.availableCapacity) / 2
    disk_offering_option = test_util.DiskOfferingOption()
    disk_offering_option.set_name('2-local-ps-test')
    disk_offering_option.set_diskSize(data_volume_size)
    data_volume_offering = vol_ops.create_volume_offering(disk_offering_option)
    test_obj_dict.add_disk_offering(data_volume_offering)
    vm = test_stub.create_vlan_vm(
        disk_offering_uuids=[data_volume_offering.uuid])
    test_obj_dict.add_vm(vm)

    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass('2 Local PS Test Pass')
    return False
def test():
    global delete_policy1
    global delete_policy2
    delete_policy1 = test_lib.lib_set_delete_policy('vm', 'Delay')
    delete_policy2 = test_lib.lib_set_delete_policy('volume', 'Delay')
    test_util.test_dsc('Test storage capacity when using expunge vm')
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)
    host = res_ops.query_resource_with_num(res_ops.HOST, cond, limit = 1)
    if not host:
        test_util.test_skip('No Enabled/Connected host was found, skip test.' )
        return True

    ps = res_ops.query_resource_with_num(res_ops.PRIMARY_STORAGE, cond, limit = 1)
    if not ps:
        test_util.test_skip('No Enabled/Connected primary storage was found, skip test.' )
        return True

    host = host[0]
    ps = ps[0]

    host_res = vol_ops.get_local_storage_capacity(host.uuid, ps.uuid)[0]
    avail_cap = host_res.availableCapacity

    vm = test_stub.create_vm(vm_name = 'basic-test-vm', host_uuid = host.uuid)
    test_obj_dict.add_vm(vm)
    data_volume_size = 1024 * 1024 * 10
    disk_offering_option = test_util.DiskOfferingOption()
    disk_offering_option.set_name('test-expunge-data-volume')
    disk_offering_option.set_diskSize(data_volume_size)
    data_volume_offering = vol_ops.create_volume_offering(disk_offering_option)
    test_obj_dict.add_disk_offering(data_volume_offering)

    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_disk_offering_uuid(data_volume_offering.uuid)
    
    volume_creation_option.set_name('volume-1')
    volume = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume)
    volume.attach(vm)

    time.sleep(1)
    vm.destroy()
    vm.expunge()
    test_obj_dict.rm_vm(vm)
    volume.delete()
    volume.expunge()
    test_obj_dict.rm_volume(volume)
    host_res2 = vol_ops.get_local_storage_capacity(host.uuid, ps.uuid)[0]
    avail_cap2 = host_res.availableCapacity
    if avail_cap != avail_cap2:
        test_util.test_fail('PS capacity is not same after create/expunge vm/volume on host: %s. Capacity before create vm: %s, after expunge vm: %s ' % (host.uuid, avail_cap, avail_cap2))
    test_lib.lib_set_delete_policy('vm', delete_policy1)
    test_lib.lib_set_delete_policy('volume', delete_policy2)
    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass('Expunge VM Test Success')
def test():
    global image
    global test_obj_dict

    #run condition
    hosts = res_ops.query_resource(res_ops.HOST)
    if len(hosts) <= 1:
        test_util.test_skip(
            "skip for host_num is not satisfy condition host_num>1")

    bs_cond = res_ops.gen_query_conditions("status", '=', "Connected")
    bss = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE,
                                        bs_cond,
                                        None,
                                        fields=['uuid'])

    #create disk offering
    data_volume_size = 10737418240
    disk_offering_option = test_util.DiskOfferingOption()
    disk_offering_option.set_name('root-disk-iso')
    disk_offering_option.set_diskSize(data_volume_size)
    data_volume_offering = vol_ops.create_volume_offering(disk_offering_option)
    test_obj_dict.add_disk_offering(data_volume_offering)

    #create instance offering
    cpuNum = 2
    memorySize = 1024 * 1024 * 1024
    name = 'iso-vm-offering'
    new_offering_option = test_util.InstanceOfferingOption()
    new_offering_option.set_cpuNum(cpuNum)
    new_offering_option.set_memorySize(memorySize)
    new_offering_option.set_name(name)
    new_offering = vm_ops.create_instance_offering(new_offering_option)
    test_obj_dict.add_instance_offering(new_offering)

    #add iso
    img_option = test_util.ImageOption()
    img_option.set_name('iso1')
    bs_uuid = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE, [],
                                            None)[0].uuid
    img_option.set_backup_storage_uuid_list([bs_uuid])
    img_option.set_url(os.environ.get('isoForVmUrl'))
    image_inv = img_ops.add_iso_template(img_option)
    image_uuid = image_inv.uuid
    image = test_image.ZstackTestImage()
    image.set_image(image_inv)
    image.set_creation_option(img_option)
    test_obj_dict.add_image(image)

    #create vm by iso
    l3_name = os.environ.get('l3VlanNetworkName1')
    l3_net_uuid = test_lib.lib_get_l3_by_name(l3_name).uuid
    root_disk_uuid = data_volume_offering.uuid
    vm = test_stub.create_vm_with_iso([l3_net_uuid], image_uuid, 'iso-vm',
                                      root_disk_uuid, new_offering.uuid)
    host_ip = test_lib.lib_find_host_by_vm(vm.get_vm()).managementIp
    test_obj_dict.add_vm(vm)

    #check vm
    vm_inv = vm.get_vm()
    vm_ip = vm_inv.vmNics[0].ip

    #cmd ='[ -e /root ]'
    #ssh_timeout = test_lib.SSH_TIMEOUT
    #test_lib.SSH_TIMEOUT = 3600
    test_lib.lib_set_vm_host_l2_ip(vm_inv)
    test_lib.lib_wait_target_up(vm.get_vm().vmNics[0].ip, 22, 1800)
    #if not test_lib.lib_ssh_vm_cmd_by_agent_with_retry(host_ip, vm_ip, 'root', 'password', cmd):
    #    test_lib.SSH_TIMEOUT = ssh_timeout
    #    test_util.test_fail("iso has been failed to installed.")

    #test_lib.SSH_TIMEOUT = ssh_timeout

    #delete iso
    image.delete()
    test_obj_dict.rm_image(image)

    #expunge iso
    image.expunge()

    #detach iso
    img_ops.detach_iso(vm.vm.uuid)

    #vm ops test
    test_stub.vm_ops_test(vm, "VM_TEST_REIMAGE")

    vm.destroy()
    vol_ops.delete_disk_offering(root_disk_uuid)
    vm_ops.delete_instance_offering(new_offering.uuid)
    test_obj_dict.rm_vm(vm)
    test_obj_dict.rm_disk_offering(data_volume_offering)
    test_obj_dict.rm_instance_offering(new_offering)

    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass('Create VM Image in Image Store Success')
def test():
    global image
    global test_obj_dict

    #run condition
    hosts = res_ops.query_resource(res_ops.HOST)
    if len(hosts) <= 1:
        test_util.test_skip("skip for host_num is not satisfy condition host_num>1")

    bs_cond = res_ops.gen_query_conditions("status", '=', "Connected")
    bss = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE, bs_cond, None, fields=['uuid'])

    #create disk offering
    data_volume_size = 10737418240
    disk_offering_option = test_util.DiskOfferingOption()
    disk_offering_option.set_name('root-disk-iso')
    disk_offering_option.set_diskSize(data_volume_size)
    data_volume_offering = vol_ops.create_volume_offering(disk_offering_option)
    test_obj_dict.add_disk_offering(data_volume_offering)

    #create instance offering
    cpuNum = 2
    memorySize = 1024*1024*1024
    name = 'iso-vm-offering'
    new_offering_option = test_util.InstanceOfferingOption()
    new_offering_option.set_cpuNum(cpuNum)
    new_offering_option.set_memorySize(memorySize)
    new_offering_option.set_name(name)
    new_offering = vm_ops.create_instance_offering(new_offering_option)
    test_obj_dict.add_instance_offering(new_offering)

    #add iso
    img_option = test_util.ImageOption()
    img_option.set_name('iso1')
    bs_uuid = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE, [], None)[0].uuid
    img_option.set_backup_storage_uuid_list([bs_uuid])
    img_option.set_url(os.environ.get('isoForVmUrl'))
    image_inv = img_ops.add_iso_template(img_option)
    image_uuid = image_inv.uuid
    image = test_image.ZstackTestImage()
    image.set_image(image_inv)
    image.set_creation_option(img_option)
    test_obj_dict.add_image(image)

    #create vm by iso
    l3_name = os.environ.get('l3VlanNetworkName1')
    l3_net_uuid = test_lib.lib_get_l3_by_name(l3_name).uuid
    root_disk_uuid = data_volume_offering.uuid
    vm = test_stub.create_vm_with_iso([l3_net_uuid], image_uuid, 'iso-vm', root_disk_uuid, new_offering.uuid)
    host_ip = test_lib.lib_find_host_by_vm(vm.get_vm()).managementIp
    test_obj_dict.add_vm(vm)
    
    #check vm
    vm_inv = vm.get_vm()
    vm_ip = vm_inv.vmNics[0].ip

    #cmd ='[ -e /root ]'
    #ssh_timeout = test_lib.SSH_TIMEOUT
    #test_lib.SSH_TIMEOUT = 3600
    test_lib.lib_set_vm_host_l2_ip(vm_inv)
    test_lib.lib_wait_target_up(vm.get_vm().vmNics[0].ip, 22, 1800)
    #if not test_lib.lib_ssh_vm_cmd_by_agent_with_retry(host_ip, vm_ip, 'root', 'password', cmd):
    #    test_lib.SSH_TIMEOUT = ssh_timeout
    #    test_util.test_fail("iso has been failed to installed.")

    #test_lib.SSH_TIMEOUT = ssh_timeout

    #delete iso
    image.delete()
    test_obj_dict.rm_image(image)

    #expunge iso
    image.expunge()

    #detach iso
    img_ops.detach_iso(vm.vm.uuid)

    #vm ops test
    test_stub.vm_ops_test(vm, "VM_TEST_STATE")

    vm.destroy()
    vol_ops.delete_disk_offering(root_disk_uuid)
    vm_ops.delete_instance_offering(new_offering.uuid)
    test_obj_dict.rm_vm(vm)
    test_obj_dict.rm_disk_offering(data_volume_offering)
    test_obj_dict.rm_instance_offering(new_offering)

    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass('Create VM Image in Image Store Success')
def test():
    global original_rate
    test_util.test_dsc('Test storage over provision method')
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)
    host = res_ops.query_resource_with_num(res_ops.HOST, cond, limit = 1)
    if not host:
        test_util.test_skip('No Enabled/Connected host was found, skip test.' )
        return True

    ps = res_ops.query_resource_with_num(res_ops.PRIMARY_STORAGE, cond, limit = 1)
    if not ps:
        test_util.test_skip('No Enabled/Connected primary storage was found, skip test.' )
        return True

    host = host[0]
    ps = ps[0]
    over_provision_rate = 2.5
    target_volume_num = 12
    kept_disk_size = 10 * 1024 * 1024

    vm = test_stub.create_vm(vm_name = 'storage_over_prs_vm_1', \
                    host_uuid = host.uuid)
    test_obj_dict.add_vm(vm)

    host_res = vol_ops.get_local_storage_capacity(host.uuid, ps.uuid)[0]
    avail_cap = host_res.availableCapacity
    if avail_cap < kept_disk_size:
        test_util.test_skip('available disk capacity:%d is too small, skip test.' % avail_cap)
        return True

    original_rate = test_lib.lib_set_provision_storage_rate(over_provision_rate)
    data_volume_size = int(over_provision_rate * (avail_cap - kept_disk_size) / target_volume_num)

    #will change the rate back to check if available capacity is same with original one. This was a bug, that only happened when system create 1 vm.
    test_lib.lib_set_provision_storage_rate(original_rate)
    host_res_tmp = vol_ops.get_local_storage_capacity(host.uuid, ps.uuid)[0]
    avail_cap_tmp = host_res_tmp.availableCapacity
    if avail_cap != avail_cap_tmp:
        test_util.test_fail('disk size is not same, between 2 times provision. Before change over rate, 1st cap: %d; 2nd cap: %d' % (avail_cap, avail_cap_tmp))

    test_lib.lib_set_provision_storage_rate(over_provision_rate)
    test_util.test_logger('Will create a serial of volume. Each of them will have %d size.' % data_volume_size)
    disk_offering_option = test_util.DiskOfferingOption()
    disk_offering_option.set_name('storage-over-ps-test')
    disk_offering_option.set_diskSize(data_volume_size)
    data_volume_offering = vol_ops.create_volume_offering(disk_offering_option)
    test_obj_dict.add_disk_offering(data_volume_offering)

    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_disk_offering_uuid(data_volume_offering.uuid)
    
    times = 1
    while (times <= target_volume_num):
        try:
            volume_creation_option.set_name('volume-%d' % times)
            volume = test_stub.create_volume(volume_creation_option)
            test_obj_dict.add_volume(volume)
            res = vol_ops.get_local_storage_capacity(host.uuid, ps.uuid)[0]
            test_util.test_logger('Current available storage size: %d' % res.availableCapacity)
            volume.attach(vm)
        except Exception as e:
            test_util.test_logger("Unexpected volume Creation Failure in storage over provision test. ")
            raise e

        times += 1

    host_res2 = vol_ops.get_local_storage_capacity(host.uuid, ps.uuid)[0]
    avail_cap2 = host_res2.availableCapacity
    if avail_cap2 > data_volume_size:
        test_util.test_fail('Available disk size: %d is still bigger than offering disk size: %d , after creating %d volumes.' % (avail_cap2, data_volume_size, target_volume_num))
    
    try:
        volume_creation_option.set_name('volume-%d' % (times + 1))
        volume = test_stub.create_volume(volume_creation_option)
        test_obj_dict.add_volume(volume)
        volume.attach(vm)
    except:
        test_util.test_logger("Expected Volume Creation Failure in storage over provision test. ")
    else:
        test_util.test_fail("The %dth Volume is still attachable, which is wrong"% (target_volume_num + 1))

    test_lib.lib_set_provision_storage_rate(original_rate)
    test_lib.lib_robot_cleanup(test_obj_dict)

    test_util.test_pass('Memory Over Provision Test Pass')
def test():
    global original_rate

    must_ps_list = [inventory.LOCAL_STORAGE_TYPE, inventory.NFS_PRIMARY_STORAGE_TYPE]
    test_lib.skip_test_if_any_ps_not_deployed(must_ps_list)

    test_util.test_dsc('Test change storage over provision rate method')
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)
    host = res_ops.query_resource_with_num(res_ops.HOST, cond, limit = 1)
    if not host:
        test_util.test_skip('No Enabled/Connected host was found, skip test.' )
        return True

    host = host[0]
    over_provision_rate1 = 2.5
    over_provision_rate2 = 1.5
    target_volume_num = 12
    kept_disk_size = 10 * 1024 * 1024

    vm = test_stub.create_vm(vm_name = 'storage_over_prs_vm_1', \
                    host_uuid = host.uuid)
    vm.check()
    test_obj_dict.add_vm(vm)

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)

    ps = res_ops.query_resource_with_num(res_ops.PRIMARY_STORAGE, cond, limit = 2)
    if not ps:
        test_util.test_skip('No Enabled/Connected primary storage was found, skip test.' )
        return True

    ps1 = ps[0].uuid
    ps1_res = test_lib.lib_get_storage_capacity(ps_uuids=[ps1])
    test_util.test_dsc("ps1[uuid:%s]'s available capacity is %s" % (ps1, ps1_res.availableCapacity))
    avail_cap = ps1_res.availableCapacity

    if len(ps) > 1:
        ps2 = ps[1].uuid
        ps2_res = test_lib.lib_get_storage_capacity(ps_uuids=[ps2])
        test_util.test_dsc("ps2[uuid:%s]'s available capacity is %s" % (ps2, ps2_res.availableCapacity))
        avail_cap = ps1_res.availableCapacity + ps2_res.availableCapacity

    if avail_cap < kept_disk_size:
        test_util.test_skip('available disk capacity:%d is too small, skip test.' % avail_cap)
        return True


    original_rate = test_lib.lib_set_provision_storage_rate(over_provision_rate1)
    data_volume_size = int(over_provision_rate1 * (avail_cap - kept_disk_size) / target_volume_num)
    disk_offering_option = test_util.DiskOfferingOption()
    disk_offering_option.set_name('storage-over-ps-test')
    disk_offering_option.set_diskSize(data_volume_size)
    data_volume_offering = vol_ops.create_volume_offering(disk_offering_option)
    test_obj_dict.add_disk_offering(data_volume_offering)

    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_disk_offering_uuid(data_volume_offering.uuid)
    
    volume_creation_option.set_name('volume-1')
    volume1 = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume1)
    #res = test_lib.lib_get_storage_capacity(zone_uuids = [zone_uuid])
    #test_util.test_logger('Current available storage size: %d' % res.availableCapacity)
    volume1.attach(vm)

    test_lib.lib_set_provision_storage_rate(over_provision_rate2)
    volume_creation_option.set_name('volume-2')
    volume2 = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume2)
    #res = test_lib.lib_get_storage_capacity(zone_uuids = [zone_uuid])
    #test_util.test_logger('Current available storage size: %d' % res.availableCapacity)
    volume2.attach(vm)
    volume1.delete()
    volume1.expunge()

    test_lib.lib_set_provision_storage_rate(over_provision_rate1)
    volume2.delete()
    volume2.expunge()

    test_lib.lib_set_provision_storage_rate(original_rate)
    time.sleep(10)

    ps1_res2 = test_lib.lib_get_storage_capacity(ps_uuids=[ps1])
    test_util.test_dsc("ps1[uuid:%s]'s available capacity is %s" % (ps1, ps1_res.availableCapacity))
    avail_cap2 = ps1_res2.availableCapacity

    if len(ps) > 1:
        ps2_res2 = test_lib.lib_get_storage_capacity(ps_uuids=[ps2])
        test_util.test_dsc("ps2[uuid:%s]'s available capacity is %s" % (ps2, ps2_res.availableCapacity))
        avail_cap2 = ps1_res2.availableCapacity + ps2_res2.availableCapacity

    if avail_cap2 != avail_cap:
        test_util.test_fail('Available disk size: %d is different with original size: %d, after creating volume under different over rate.' % (avail_cap2, avail_cap))
    
    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass('Memory Over Provision Test Pass')
def test():
    global ssh_timeout
    data_volume_size = 10737418240
    disk_offering_option = test_util.DiskOfferingOption()
    disk_offering_option.set_name('root-disk-iso')
    disk_offering_option.set_diskSize(data_volume_size)
    data_volume_offering = vol_ops.create_volume_offering(disk_offering_option)
    test_obj_dict.add_disk_offering(data_volume_offering)

    cpuNum = 1
    memorySize = 536870912
    name = 'vm-offering-iso'
    new_offering_option = test_util.InstanceOfferingOption()
    new_offering_option.set_cpuNum(cpuNum)
    new_offering_option.set_memorySize(memorySize)
    new_offering_option.set_name(name)
    new_offering = vm_ops.create_instance_offering(new_offering_option)
    test_obj_dict.add_instance_offering(new_offering)

    cond = res_ops.gen_query_conditions('name', '=', 'image-iso')
    iso_image = res_ops.query_resource(res_ops.IMAGE, cond)
    if not iso_image:
        img_option = test_util.ImageOption()
        img_option.set_name('image-iso')
        bs_uuid = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE, [], None)[0].uuid
        img_option.set_backup_storage_uuid_list([bs_uuid])
        img_option.set_url(os.environ.get('imageServer')+'/iso/iso_for_install_vm_test.iso')
        image_inv = img_ops.add_iso_template(img_option)
        image_uuid = image_inv.uuid
        image = test_image.ZstackTestImage()
        image.set_image(image_inv)
        image.set_creation_option(img_option)
        test_obj_dict.add_image(image)
    else:
        image_uuid = iso_image[0].uuid

    l3_name = os.environ.get('l3VlanNetworkName1')
    l3_net_uuid = test_lib.lib_get_l3_by_name(l3_name).uuid
    root_disk_uuid = data_volume_offering.uuid
    vm = test_stub.create_vm_with_iso([l3_net_uuid], image_uuid, 'vm-iso', root_disk_uuid, new_offering.uuid, system_tags=["cdroms::%s::Empty::Empty" % (image_uuid)])
    host_ip = test_lib.lib_find_host_by_vm(vm.get_vm()).managementIp
    test_obj_dict.add_vm(vm)
    
    test_util.test_dsc('wait for iso installation')    
    vm_inv = vm.get_vm()
    vm_ip = vm_inv.vmNics[0].ip

    #test_lib.lib_wait_target_up(vm_ip, '22', 2400)
    #vm.check()

    #cmd ='[ -e /root ] && echo yes || echo no' 
    cmd ='[ -e /root ]'
    #ssh_num = 0
    #ssh_ok = 0
    #while ssh_num <= 5 and ssh_ok == 0 :
    #    rsp = test_lib.lib_execute_ssh_cmd(vm_ip, 'root', 'password', cmd, 180)
    #    if rsp == False:
    #        time.sleep(30)
    #    else:
    #        ssh_ok = 1
    #        break  
    #    ssh_num = ssh_num + 1

    #if ssh_ok == 0:
    #    test_util.test_fail('fail to ssh to VM')
    ssh_timeout = test_lib.SSH_TIMEOUT
    test_lib.SSH_TIMEOUT = 3600
    test_lib.lib_set_vm_host_l2_ip(vm_inv)
    if not test_lib.lib_ssh_vm_cmd_by_agent_with_retry(host_ip, vm_ip, 'root', 'password', cmd):
        test_lib.SSH_TIMEOUT = ssh_timeout
        test_util.test_fail("iso has not been failed to installed.")

    cmd_cdrom = 'ls /dev/ | grep sr | wc -l'
    cdroms = test_lib.lib_ssh_vm_cmd_by_agent_with_retry(host_ip, vm_ip, 'root', 'password', cmd_cdrom)
    assert str(cdroms).strip() == '3'

    test_lib.SSH_TIMEOUT = ssh_timeout
    vm.destroy()
    test_obj_dict.rm_vm(vm)

#     image.delete()
#     test_obj_dict.rm_image(image)

    vol_ops.delete_disk_offering(root_disk_uuid)
    test_obj_dict.rm_disk_offering(data_volume_offering)

    vm_ops.delete_instance_offering(new_offering.uuid)
    test_obj_dict.rm_instance_offering(new_offering)

    test_util.test_pass('Create VM with ISO Installation Test Success')
def test():
    #skip ceph in c74
    cmd = "cat /etc/redhat-release | grep '7.4'"
    mn_ip = res_ops.query_resource(res_ops.MANAGEMENT_NODE)[0].hostName
    rsp = test_lib.lib_execute_ssh_cmd(mn_ip, 'root', 'password', cmd, 180)
    if rsp != False:
        ps = res_ops.query_resource(res_ops.PRIMARY_STORAGE)
        for i in ps:
            if i.type == 'Ceph':
                test_util.test_skip('cannot hotplug iso to the vm in ceph,it is a libvirt bug:https://bugzilla.redhat.com/show_bug.cgi?id=1541702.')

    global image
    global test_obj_dict
    allow_bs_list = [inventory.IMAGE_STORE_BACKUP_STORAGE_TYPE, inventory.CEPH_BACKUP_STORAGE_TYPE]
    test_lib.skip_test_when_bs_type_not_in_list(allow_bs_list)

    allow_ps_list = [inventory.CEPH_PRIMARY_STORAGE_TYPE, inventory.NFS_PRIMARY_STORAGE_TYPE, 'SharedMountPoint']
    test_lib.skip_test_when_ps_type_not_in_list(allow_ps_list)

    #run condition
    hosts = res_ops.query_resource(res_ops.HOST)
    if len(hosts) <= 1:
        test_util.test_skip("skip for host_num is not satisfy condition host_num>1")

    bs_cond = res_ops.gen_query_conditions("status", '=', "Connected")
    bss = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE, bs_cond, None, fields=['uuid'])

    #create disk offering
    data_volume_size = 10737418240
    disk_offering_option = test_util.DiskOfferingOption()
    disk_offering_option.set_name('root-disk-iso')
    disk_offering_option.set_diskSize(data_volume_size)
    data_volume_offering = vol_ops.create_volume_offering(disk_offering_option)
    test_obj_dict.add_disk_offering(data_volume_offering)

    #create instance offering
    cpuNum = 2
    memorySize = 1024*1024*1024
    name = 'iso-vm-offering'
    new_offering_option = test_util.InstanceOfferingOption()
    new_offering_option.set_cpuNum(cpuNum)
    new_offering_option.set_memorySize(memorySize)
    new_offering_option.set_name(name)
    new_offering = vm_ops.create_instance_offering(new_offering_option)
    test_obj_dict.add_instance_offering(new_offering)

    #add iso
    img_option = test_util.ImageOption()
    img_option.set_name('iso1')
    bs_uuid = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE, [], None)[0].uuid
    img_option.set_backup_storage_uuid_list([bs_uuid])
    img_option.set_url(os.environ.get('isoForVmUrl'))
    image_inv = img_ops.add_iso_template(img_option)
    image_uuid = image_inv.uuid
    image = test_image.ZstackTestImage()
    image.set_image(image_inv)
    image.set_creation_option(img_option)
    test_obj_dict.add_image(image)

    #create vm by iso
    l3_name = os.environ.get('l3VlanNetworkName1')
    l3_net_uuid = test_lib.lib_get_l3_by_name(l3_name).uuid
    root_disk_uuid = data_volume_offering.uuid
    vm = test_stub.create_vm_with_iso([l3_net_uuid], image_uuid, 'iso-vm', root_disk_uuid, new_offering.uuid)
    host_ip = test_lib.lib_find_host_by_vm(vm.get_vm()).managementIp
    test_obj_dict.add_vm(vm)
    
    #check vm
    vm_inv = vm.get_vm()
    vm_ip = vm_inv.vmNics[0].ip

    #cmd ='[ -e /root ]'
    #ssh_timeout = test_lib.SSH_TIMEOUT
    #test_lib.SSH_TIMEOUT = 3600
    test_lib.lib_set_vm_host_l2_ip(vm_inv)
    test_lib.lib_wait_target_up(vm.get_vm().vmNics[0].ip, 22, 1800)
    #if not test_lib.lib_ssh_vm_cmd_by_agent_with_retry(host_ip, vm_ip, 'root', 'password', cmd):
    #    test_lib.SSH_TIMEOUT = ssh_timeout
    #    test_util.test_fail("iso has been failed to installed.")

    #test_lib.SSH_TIMEOUT = ssh_timeout

    #clone vm
    cloned_vm_name = ['cloned_vm_name']
    cloned_vm_obj = vm.clone(cloned_vm_name)[0]
    test_obj_dict.add_vm(cloned_vm_obj)

    #delete iso
    image.delete()
    test_obj_dict.rm_image(image)

    #vm ops test
    test_stub.vm_ops_test(cloned_vm_obj, "VM_TEST_ALL")

    #expunge iso
    image.expunge()

    #detach iso
    img_ops.detach_iso(vm.vm.uuid)

    #vm ops test
    test_stub.vm_ops_test(cloned_vm_obj, "VM_TEST_ALL")

    #create image by vm root volume
    cloned_vm_img_name = "cloned_vm_image1"
    img_option2 = test_util.ImageOption()
    img_option2.set_backup_storage_uuid_list([bs_uuid])
    img_option2.set_root_volume_uuid(cloned_vm_obj.vm.rootVolumeUuid)
    img_option2.set_name(cloned_vm_img_name)
    image1 = test_image.ZstackTestImage()
    image1.set_creation_option(img_option2)
    image1.create()
    image1.check()
    test_obj_dict.add_image(image1)

    bs_list = res_ops.query_resource(res_ops.BACKUP_STORAGE)
    if inventory.IMAGE_STORE_BACKUP_STORAGE_TYPE == bs_list[0] and len(bs_list) == 1 :
        #export image
        image1.export()

    #create vm 
    vm2 = test_stub.create_vm('image-vm', cloned_vm_img_name, l3_name)

    #delete image
    image1.delete()
    test_obj_dict.rm_image(image1)

    #vm ops test
    test_stub.vm_ops_test(vm2, "VM_TEST_ALL")

    #expunge image
    image1.expunge()

    #vm ops test
    test_stub.vm_ops_test(vm2, "VM_TEST_ALL")

    vm.destroy()
    vm2.destroy()
    cloned_vm_obj.destroy()

    vol_ops.delete_disk_offering(root_disk_uuid)
    vm_ops.delete_instance_offering(new_offering.uuid)
    test_obj_dict.rm_vm(vm)
    test_obj_dict.rm_disk_offering(data_volume_offering)
    test_obj_dict.rm_instance_offering(new_offering)

    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass('Create VM Image in Image Store Success')
def test():
    global original_rate
    global new_offering_uuid
    global delete_policy
    test_util.test_dsc('Test memory allocation and reclaiming.')
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)
    hosts = res_ops.query_resource_with_num(res_ops.HOST, cond)
    if not hosts:
        test_util.test_skip('No Enabled/Connected host was found, skip test.' )
        return True

    ps = res_ops.query_resource_with_num(res_ops.PRIMARY_STORAGE, cond, limit = 1)
    if not ps:
        test_util.test_skip('No Enabled/Connected primary storage was found, skip test.' )
        return True

    if ps[0].type == inventory.CEPH_PRIMARY_STORAGE_TYPE or ps[0].type == 'SharedMountPoint':
        test_util.test_skip('skip test on ceph and smp.' )
        return True

    host = random.choice(hosts)
    ps = ps[0]
    over_provision_rate = 1
    target_vm_num = 5

    host_res = vol_ops.get_local_storage_capacity(host.uuid, ps.uuid)[0]
    avail_cap = host_res.availableCapacity

    image_name = os.environ.get('imageName_net')
    image = test_lib.lib_get_image_by_name(image_name)
    image_size = image.size

    original_rate = test_lib.lib_set_provision_storage_rate(over_provision_rate)
    data_volume_size = int(avail_cap / target_vm_num * over_provision_rate - image_size)
    if data_volume_size < 0:
        test_util.test_skip('Do not have enough disk space to do test')
        return True

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

    host_res = vol_ops.get_local_storage_capacity(host.uuid, ps.uuid)[0]
    avail_cap = host_res.availableCapacity

    disk_offering_option = test_util.DiskOfferingOption()
    disk_offering_option.set_name('vm-parallel-creation-test')
    disk_offering_option.set_diskSize(data_volume_size)
    data_volume_offering = vol_ops.create_volume_offering(disk_offering_option)
    test_obj_dict.add_disk_offering(data_volume_offering)

    rounds = 1
    while (rounds <= 3):
        times = 1
        test_util.test_logger('test round: %s' % rounds)
        while (times <= (target_vm_num)):
            thread = threading.Thread(target = parallelly_create_vm, \
                    args = ('parallel_vm_creating_%d' % times, \
                        image_name, \
                        host.uuid, \
                        data_volume_offering.uuid, ))
            thread.start()

            times += 1

        times = 1
        print 'Running VM: %s ' % len(test_obj_dict.get_vm_list())
        while threading.active_count() > 1:
            check_thread_exception()
            time.sleep(1)
            if times > 30:
                test_util.test_fail('creating vm time exceed 30s')
            times += 1

        check_thread_exception()
        try:
            vm = test_stub.create_vm(vm_name = 'unexpected vm', \
                    image_name = image_name, \
                    host_uuid = host.uuid)
            test_obj_dict.add_vm(vm)
        except:
            test_util.test_logger('expect vm creation failure')
        else:
            test_util.test_fail('The extra vm is unexpected to be created up')

        for vm in test_obj_dict.get_all_vm_list():
            try:
                test_lib.lib_destroy_vm_and_data_volumes_objs_update_test_dict(vm, test_obj_dict)
            except Exception as e:
                test_util.test_logger("VM Destroying Failure in vm parallel creation test. :%s " % e)
                raise e

        rounds += 1
    
    test_lib.lib_set_provision_storage_rate(original_rate)
    test_lib.lib_robot_cleanup(test_obj_dict)
    test_lib.lib_set_delete_policy('vm', delete_policy)
    test_lib.lib_set_delete_policy('volume', delete_policy)

    test_util.test_pass('Parallel vm creation Test Pass')
def test():
    global original_rate
    test_util.test_dsc('Test change storage over provision rate method')
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)
    host = res_ops.query_resource_with_num(res_ops.HOST, cond, limit = 1)
    if not host:
        test_util.test_skip('No Enabled/Connected host was found, skip test.' )
        return True

    host = host[0]
    over_provision_rate1 = 2.5
    over_provision_rate2 = 1.5
    target_volume_num = 12
    kept_disk_size = 10 * 1024 * 1024

    vm = test_stub.create_vm(vm_name = 'storage_over_prs_vm_1', \
                    host_uuid = host.uuid)
    test_obj_dict.add_vm(vm)

    host_res = test_lib.lib_get_cpu_memory_capacity(host_uuids = [host.uuid])
    ps_res = test_lib.lib_get_storage_capacity(zone_uuids = [zone_uuid])
    avail_cap = ps_res.availableCapacity
    if avail_cap < kept_disk_size:
        test_util.test_skip('available disk capacity:%d is too small, skip test.' % avail_cap)
        return True

    original_rate = test_lib.lib_set_provision_storage_rate(over_provision_rate1)
    data_volume_size = int(over_provision_rate1 * (avail_cap - kept_disk_size) / target_volume_num)
    disk_offering_option = test_util.DiskOfferingOption()
    disk_offering_option.set_name('storage-over-ps-test')
    disk_offering_option.set_diskSize(data_volume_size)
    data_volume_offering = vol_ops.create_volume_offering(disk_offering_option)
    test_obj_dict.add_disk_offering(data_volume_offering)

    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_disk_offering_uuid(data_volume_offering.uuid)
    
    volume_creation_option.set_name('volume-1')
    volume1 = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume1)
    #res = test_lib.lib_get_storage_capacity(zone_uuids = [zone_uuid])
    #test_util.test_logger('Current available storage size: %d' % res.availableCapacity)
    volume1.attach(vm)

    test_lib.lib_set_provision_storage_rate(over_provision_rate2)
    volume_creation_option.set_name('volume-2')
    volume2 = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume2)
    #res = test_lib.lib_get_storage_capacity(zone_uuids = [zone_uuid])
    #test_util.test_logger('Current available storage size: %d' % res.availableCapacity)
    volume2.attach(vm)
    volume1.delete()

    test_lib.lib_set_provision_storage_rate(over_provision_rate1)
    volume2.delete()

    test_lib.lib_set_provision_storage_rate(original_rate)
    ps_res2 = test_lib.lib_get_storage_capacity(zone_uuids = [zone_uuid])
    avail_cap2 = ps_res2.availableCapacity
    if avail_cap2 != avail_cap:
        test_util.test_fail('Available disk size: %d is different with original size: %d, after creating volume under different over rate.' % (avail_cap2, avail_cap))
    
    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass('Memory Over Provision Test Pass')
示例#12
0
def test():
    global ssh_timeout
    data_volume_size = 10737418240
    disk_offering_option = test_util.DiskOfferingOption()
    disk_offering_option.set_name('root-disk-iso')
    disk_offering_option.set_diskSize(data_volume_size)
    data_volume_offering = vol_ops.create_volume_offering(disk_offering_option)
    test_obj_dict.add_disk_offering(data_volume_offering)

    cpuNum = 1
    memorySize = 536870912
    name = 'vm-offering-iso'
    new_offering_option = test_util.InstanceOfferingOption()
    new_offering_option.set_cpuNum(cpuNum)
    new_offering_option.set_memorySize(memorySize)
    new_offering_option.set_name(name)
    new_offering = vm_ops.create_instance_offering(new_offering_option)
    test_obj_dict.add_instance_offering(new_offering)

    img_option = test_util.ImageOption()
    img_option.set_name('image-iso')
    bs_uuid = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE, [], None)[0].uuid
    img_option.set_backup_storage_uuid_list([bs_uuid])
    img_option.set_url('http://172.20.1.15:7480/iso/iso_for_install_vm_test.iso')
    image_inv = img_ops.add_iso_template(img_option)
    image_uuid = image_inv.uuid
    image = test_image.ZstackTestImage()
    image.set_image(image_inv)
    image.set_creation_option(img_option)
    test_obj_dict.add_image(image)

    l3_name = os.environ.get('l3VlanNetworkName1')
    l3_net_uuid = test_lib.lib_get_l3_by_name(l3_name).uuid
    root_disk_uuid = data_volume_offering.uuid
    vm = test_stub.create_vm_with_iso([l3_net_uuid], image_uuid, 'vm-iso', root_disk_uuid, new_offering.uuid)
    host_ip = test_lib.lib_find_host_by_vm(vm.get_vm()).managementIp
    test_obj_dict.add_vm(vm)
    
    test_util.test_dsc('wait for iso installation')    
    vm_inv = vm.get_vm()
    vm_ip = vm_inv.vmNics[0].ip

    #test_lib.lib_wait_target_up(vm_ip, '22', 2400)
    #vm.check()

    #cmd ='[ -e /root ] && echo yes || echo no' 
    cmd ='[ -e /root ]'
    #ssh_num = 0
    #ssh_ok = 0
    #while ssh_num <= 5 and ssh_ok == 0 :
    #    rsp = test_lib.lib_execute_ssh_cmd(vm_ip, 'root', 'password', cmd, 180)
    #    if rsp == False:
    #        time.sleep(30)
    #    else:
    #        ssh_ok = 1
    #        break  
    #    ssh_num = ssh_num + 1

    #if ssh_ok == 0:
    #    test_util.test_fail('fail to ssh to VM')
    ssh_timeout = test_lib.SSH_TIMEOUT
    test_lib.SSH_TIMEOUT = 3600
    test_lib.lib_set_vm_host_l2_ip(vm_inv)
    if not test_lib.lib_ssh_vm_cmd_by_agent_with_retry(host_ip, vm_ip, 'root', 'password', cmd):
        test_lib.SSH_TIMEOUT = ssh_timeout
        test_util.test_fail("iso has not been failed to installed.")

    test_lib.SSH_TIMEOUT = ssh_timeout
    vm.destroy()
    test_obj_dict.rm_vm(vm)

    image.delete()
    test_obj_dict.rm_image(image)

    vol_ops.delete_disk_offering(root_disk_uuid)
    test_obj_dict.rm_disk_offering(data_volume_offering)

    vm_ops.delete_instance_offering(new_offering.uuid)
    test_obj_dict.rm_instance_offering(new_offering)

    test_util.test_pass('Create VM with ISO Installation Test Success')
def test():
    data_volume_size = 10737418240
    disk_offering_option = test_util.DiskOfferingOption()
    disk_offering_option.set_name('root-disk-iso')
    disk_offering_option.set_diskSize(data_volume_size)
    data_volume_offering = vol_ops.create_volume_offering(disk_offering_option)
    test_obj_dict.add_disk_offering(data_volume_offering)

    cpuNum = 1
    cpuSpeed = 16
    memorySize = 536870912
    name = 'vm-offering-iso'
    new_offering_option = test_util.InstanceOfferingOption()
    new_offering_option.set_cpuNum(cpuNum)
    new_offering_option.set_cpuSpeed(cpuSpeed)
    new_offering_option.set_memorySize(memorySize)
    new_offering_option.set_name(name)
    new_offering = vm_ops.create_instance_offering(new_offering_option)
    test_obj_dict.add_instance_offering(new_offering)

    img_option = test_util.ImageOption()
    img_option.set_name('image-iso')
    bs_uuid = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE, [], None)[0].uuid
    img_option.set_backup_storage_uuid_list([bs_uuid])
    img_option.set_url('http://172.20.1.15:7480/iso/iso_for_install_vm_test.iso')
    image_inv = img_ops.add_iso_template(img_option)
    image_uuid = image_inv.uuid
    image = test_image.ZstackTestImage()
    image.set_image(image_inv)
    image.set_creation_option(img_option)
    test_obj_dict.add_image(image)

    l3_name = os.environ.get('l3PublicNetworkName')
    l3_net_uuid = test_lib.lib_get_l3_by_name(l3_name).uuid
    root_disk_uuid = data_volume_offering.uuid
    vm = test_stub.create_vm_with_iso([l3_net_uuid], image_uuid, 'vm-iso', root_disk_uuid, new_offering.uuid)
    test_obj_dict.add_vm(vm)
    
    test_util.test_dsc('wait for iso installation')    
    vm_inv = vm.get_vm()
    vm_ip = vm_inv.vmNics[0].ip

    test_lib.lib_wait_target_up(vm_ip, '22', 2400)

    cmd ='[ -e /root ] && echo yes || echo no' 
    ssh_num = 0
    ssh_ok = 0
    while ssh_num <= 5 and ssh_ok == 0 :
        rsp = test_lib.lib_execute_ssh_cmd(vm_ip, 'root', 'password', cmd, 180)
        if rsp == False:
            time.sleep(30)
        else:
            ssh_ok = 1
            break  
        ssh_num = ssh_num + 1

    if ssh_ok == 0:
        test_util.test_fail('fail to ssh to VM')

    vm.check()

    vm.destroy()
    test_obj_dict.rm_vm(vm)

    image.delete()
    test_obj_dict.rm_image(image)

    vol_ops.delete_disk_offering(root_disk_uuid)
    test_obj_dict.rm_disk_offering(data_volume_offering)

    vm_ops.delete_instance_offering(new_offering.uuid)
    test_obj_dict.rm_instance_offering(new_offering)

    test_util.test_pass('Create VM with ISO Installation Test Success')
def test():
    data_volume_size = 10737418240
    disk_offering_option = test_util.DiskOfferingOption()
    disk_offering_option.set_name('root-disk-iso')
    disk_offering_option.set_diskSize(data_volume_size)
    data_volume_offering = vol_ops.create_volume_offering(disk_offering_option)
    test_obj_dict.add_disk_offering(data_volume_offering)

    cpuNum = 1
    cpuSpeed = 16
    memorySize = 536870912
    name = 'vm-offering-iso'
    new_offering_option = test_util.InstanceOfferingOption()
    new_offering_option.set_cpuNum(cpuNum)
    new_offering_option.set_cpuSpeed(cpuSpeed)
    new_offering_option.set_memorySize(memorySize)
    new_offering_option.set_name(name)
    new_offering = vm_ops.create_instance_offering(new_offering_option)
    test_obj_dict.add_instance_offering(new_offering)

    img_option = test_util.ImageOption()
    img_option.set_name('image-iso')
    bs_uuid = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE, [],
                                            None)[0].uuid
    img_option.set_backup_storage_uuid_list([bs_uuid])
    img_option.set_url(
        'http://172.20.1.15:7480/iso/iso_for_install_vm_test.iso')
    image_inv = img_ops.add_iso_template(img_option)
    image_uuid = image_inv.uuid
    image = test_image.ZstackTestImage()
    image.set_image(image_inv)
    image.set_creation_option(img_option)
    test_obj_dict.add_image(image)

    l3_name = os.environ.get('l3PublicNetworkName')
    l3_net_uuid = test_lib.lib_get_l3_by_name(l3_name).uuid
    root_disk_uuid = data_volume_offering.uuid
    vm = test_stub.create_vm_with_iso([l3_net_uuid], image_uuid, 'vm-iso',
                                      root_disk_uuid, new_offering.uuid)
    test_obj_dict.add_vm(vm)

    test_util.test_dsc('wait for iso installation')
    vm_inv = vm.get_vm()
    vm_ip = vm_inv.vmNics[0].ip

    test_lib.lib_wait_target_up(vm_ip, '22', 2400)

    cmd = '[ -e /root ] && echo yes || echo no'
    ssh_num = 0
    ssh_ok = 0
    while ssh_num <= 5 and ssh_ok == 0:
        rsp = test_lib.lib_execute_ssh_cmd(vm_ip, 'root', 'password', cmd, 180)
        if rsp == False:
            time.sleep(30)
        else:
            ssh_ok = 1
            break
        ssh_num = ssh_num + 1

    if ssh_ok == 0:
        test_util.test_fail('fail to ssh to VM')

    vm.check()

    vm.destroy()
    test_obj_dict.rm_vm(vm)

    image.delete()
    test_obj_dict.rm_image(image)

    vol_ops.delete_disk_offering(root_disk_uuid)
    test_obj_dict.rm_disk_offering(data_volume_offering)

    vm_ops.delete_instance_offering(new_offering.uuid)
    test_obj_dict.rm_instance_offering(new_offering)

    test_util.test_pass('Create VM with ISO Installation Test Success')
示例#15
0
def test():
    global original_rate
    test_util.test_dsc('Test storage over provision method')
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)
    host = res_ops.query_resource_with_num(res_ops.HOST, cond, limit=1)
    if not host:
        test_util.test_skip('No Enabled/Connected host was found, skip test.')
        return True

    ps = res_ops.query_resource_with_num(res_ops.PRIMARY_STORAGE,
                                         cond,
                                         limit=1)
    if not ps:
        test_util.test_skip(
            'No Enabled/Connected primary storage was found, skip test.')
        return True

    host = host[0]
    ps = ps[0]
    ps_type = ps.type
    #TODO: Fix ceph testing
    if ps_type == 'Ceph' or ps_type == 'SharedMountPoint':
        test_util.test_skip('skip test for ceph and smp.')

    over_provision_rate = 2.5
    target_volume_num = 12
    kept_disk_size = 10 * 1024 * 1024

    vm = test_stub.create_vm(vm_name = 'storage_over_prs_vm_1', \
                    host_uuid = host.uuid)
    test_obj_dict.add_vm(vm)
    vm.check()

    avail_cap = get_storage_capacity(ps_type, host.uuid, ps.uuid)
    if avail_cap < kept_disk_size:
        test_util.test_skip(
            'available disk capacity:%d is too small, skip test.' % avail_cap)
        return True

    original_rate = test_lib.lib_set_provision_storage_rate(
        over_provision_rate)
    data_volume_size = int(over_provision_rate * (avail_cap - kept_disk_size) /
                           target_volume_num)

    #will change the rate back to check if available capacity is same with original one. This was a bug, that only happened when system create 1 vm.
    test_lib.lib_set_provision_storage_rate(original_rate)
    avail_cap_tmp = get_storage_capacity(ps_type, host.uuid, ps.uuid)
    if avail_cap != avail_cap_tmp:
        test_util.test_fail(
            'disk size is not same, between 2 times provision. Before change over rate, 1st cap: %d; 2nd cap: %d'
            % (avail_cap, avail_cap_tmp))

    test_lib.lib_set_provision_storage_rate(over_provision_rate)
    test_util.test_logger(
        'Will create a serial of volume. Each of them will have %d size.' %
        data_volume_size)
    disk_offering_option = test_util.DiskOfferingOption()
    disk_offering_option.set_name('storage-over-ps-test')
    disk_offering_option.set_diskSize(data_volume_size)
    data_volume_offering = vol_ops.create_volume_offering(disk_offering_option)
    test_obj_dict.add_disk_offering(data_volume_offering)

    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_disk_offering_uuid(data_volume_offering.uuid)

    times = 1
    while (times <= target_volume_num):
        try:
            volume_creation_option.set_name('volume-%d' % times)
            volume = test_stub.create_volume(volume_creation_option)
            test_obj_dict.add_volume(volume)
            test_util.test_logger(
                'Current available storage size: %d' %
                get_storage_capacity(ps_type, host.uuid, ps.uuid))
            volume.attach(vm)
        except Exception as e:
            test_util.test_logger(
                "Unexpected volume Creation Failure in storage over provision test. "
            )
            raise e

        times += 1

    time.sleep(2)
    avail_cap2 = get_storage_capacity(ps_type, host.uuid, ps.uuid)
    if avail_cap2 > data_volume_size:
        test_util.test_fail(
            'Available disk size: %d is still bigger than offering disk size: %d , after creating %d volumes.'
            % (avail_cap2, data_volume_size, target_volume_num))

    try:
        volume_creation_option.set_name('volume-%d' % (times + 1))
        volume = test_stub.create_volume(volume_creation_option)
        test_obj_dict.add_volume(volume)
        volume.attach(vm)
    except:
        test_util.test_logger(
            "Expected Volume Creation Failure in storage over provision test. "
        )
    else:
        test_util.test_fail(
            "The %dth Volume is still attachable, which is wrong" %
            (target_volume_num + 1))

    test_lib.lib_set_provision_storage_rate(original_rate)
    test_lib.lib_robot_cleanup(test_obj_dict)

    test_util.test_pass('Memory Over Provision Test Pass')
示例#16
0
def test():
    global original_rate
    test_util.test_dsc('Test change storage over provision rate method')
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)
    host = res_ops.query_resource_with_num(res_ops.HOST, cond, limit=1)
    if not host:
        test_util.test_skip('No Enabled/Connected host was found, skip test.')
        return True

    host = host[0]
    over_provision_rate1 = 2.5
    over_provision_rate2 = 1.5
    target_volume_num = 12
    kept_disk_size = 10 * 1024 * 1024

    vm = test_stub.create_vm(vm_name = 'storage_over_prs_vm_1', \
                    host_uuid = host.uuid)
    test_obj_dict.add_vm(vm)

    host_res = test_lib.lib_get_cpu_memory_capacity(host_uuids=[host.uuid])
    ps_res = test_lib.lib_get_storage_capacity(zone_uuids=[zone_uuid])
    avail_cap = ps_res.availableCapacity
    if avail_cap < kept_disk_size:
        test_util.test_skip(
            'available disk capacity:%d is too small, skip test.' % avail_cap)
        return True

    original_rate = test_lib.lib_set_provision_storage_rate(
        over_provision_rate1)
    data_volume_size = int(over_provision_rate1 *
                           (avail_cap - kept_disk_size) / target_volume_num)
    disk_offering_option = test_util.DiskOfferingOption()
    disk_offering_option.set_name('storage-over-ps-test')
    disk_offering_option.set_diskSize(data_volume_size)
    data_volume_offering = vol_ops.create_volume_offering(disk_offering_option)
    test_obj_dict.add_disk_offering(data_volume_offering)

    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_disk_offering_uuid(data_volume_offering.uuid)

    volume_creation_option.set_name('volume-1')
    volume1 = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume1)
    #res = test_lib.lib_get_storage_capacity(zone_uuids = [zone_uuid])
    #test_util.test_logger('Current available storage size: %d' % res.availableCapacity)
    volume1.attach(vm)

    test_lib.lib_set_provision_storage_rate(over_provision_rate2)
    volume_creation_option.set_name('volume-2')
    volume2 = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume2)
    #res = test_lib.lib_get_storage_capacity(zone_uuids = [zone_uuid])
    #test_util.test_logger('Current available storage size: %d' % res.availableCapacity)
    volume2.attach(vm)
    volume1.delete()

    test_lib.lib_set_provision_storage_rate(over_provision_rate1)
    volume2.delete()

    test_lib.lib_set_provision_storage_rate(original_rate)
    ps_res2 = test_lib.lib_get_storage_capacity(zone_uuids=[zone_uuid])
    avail_cap2 = ps_res2.availableCapacity
    if avail_cap2 != avail_cap:
        test_util.test_fail(
            'Available disk size: %d is different with original size: %d, after creating volume under different over rate.'
            % (avail_cap2, avail_cap))

    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass('Memory Over Provision Test Pass')
def test():
    global original_rate
    global new_offering_uuid
    global delete_policy
    test_util.test_dsc('Test memory allocation and reclaiming.')
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)
    hosts = res_ops.query_resource_with_num(res_ops.HOST, cond)
    if not hosts:
        test_util.test_skip('No Enabled/Connected host was found, skip test.' )
        return True

    ps = res_ops.query_resource_with_num(res_ops.PRIMARY_STORAGE, cond, limit = 1)
    if not ps:
        test_util.test_skip('No Enabled/Connected primary storage was found, skip test.' )
        return True

    host = random.choice(hosts)
    ps = ps[0]
    over_provision_rate = 1
    target_vm_num = 5

    host_res = vol_ops.get_local_storage_capacity(host.uuid, ps.uuid)[0]
    avail_cap = host_res.availableCapacity

    image_name = os.environ.get('imageName_net')
    image = test_lib.lib_get_image_by_name(image_name)
    image_size = image.size

    original_rate = test_lib.lib_set_provision_storage_rate(over_provision_rate)
    data_volume_size = int(over_provision_rate * (avail_cap - image_size * target_vm_num) / target_vm_num)
    if data_volume_size < 0:
        test_util.test_skip('Do not have enough disk space to do test')
        return True

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

    host_res = vol_ops.get_local_storage_capacity(host.uuid, ps.uuid)[0]
    avail_cap = host_res.availableCapacity

    disk_offering_option = test_util.DiskOfferingOption()
    disk_offering_option.set_name('vm-parallel-creation-test')
    disk_offering_option.set_diskSize(data_volume_size)
    data_volume_offering = vol_ops.create_volume_offering(disk_offering_option)
    test_obj_dict.add_disk_offering(data_volume_offering)

    rounds = 1
    while (rounds <= 3):
        times = 1
        test_util.test_logger('test round: %s' % rounds)
        while (times <= (target_vm_num)):
            thread = threading.Thread(target = parallelly_create_vm, \
                    args = ('parallel_vm_creating_%d' % times, \
                        image_name, \
                        host.uuid, \
                        data_volume_offering.uuid, ))
            thread.start()

            times += 1

        times = 1
        print 'Running VM: %s ' % len(test_obj_dict.get_vm_list())
        while threading.active_count() > 1:
            check_thread_exception()
            time.sleep(1)
            if times > 10:
                test_util.test_fail('creating vm time exceed 10s')
            times += 1

        check_thread_exception()
        try:
            vm = test_stub.create_vm(vm_name = 'unexpected vm', \
                    image_name = image_name, \
                    host_uuid = host.uuid)
            test_obj_dict.add_vm(vm)
        except:
            test_util.test_logger('expect vm creation failure')
        else:
            test_util.test_fail('The extra vm is unexpected to be created up')

        for vm in test_obj_dict.get_all_vm_list():
            try:
                test_lib.lib_destroy_vm_and_data_volumes_objs_update_test_dict(vm, test_obj_dict)
            except Exception as e:
                test_util.test_logger("VM Destroying Failure in vm parallel creation test. :%s " % e)
                raise e

        rounds += 1
    
    test_lib.lib_set_provision_storage_rate(original_rate)
    test_lib.lib_robot_cleanup(test_obj_dict)
    test_lib.lib_set_delete_policy('vm', delete_policy)
    test_lib.lib_set_delete_policy('volume', delete_policy)

    test_util.test_pass('Parallel vm creation Test Pass')
示例#18
0
def test():
    global image
    global test_obj_dict
    allow_bs_list = [
        inventory.IMAGE_STORE_BACKUP_STORAGE_TYPE,
        inventory.CEPH_BACKUP_STORAGE_TYPE, inventory.SFTP_BACKUP_STORAGE_TYPE
    ]
    test_lib.skip_test_when_bs_type_not_in_list(allow_bs_list)

    allow_ps_list = [
        inventory.CEPH_PRIMARY_STORAGE_TYPE,
        inventory.NFS_PRIMARY_STORAGE_TYPE, 'SharedMountPoint'
    ]
    test_lib.skip_test_when_ps_type_not_in_list(allow_ps_list)

    #run condition
    hosts = res_ops.query_resource(res_ops.HOST)
    if len(hosts) <= 1:
        test_util.test_skip(
            "skip for host_num is not satisfy condition host_num>1")

    bs_cond = res_ops.gen_query_conditions("status", '=', "Connected")
    bss = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE,
                                        bs_cond,
                                        None,
                                        fields=['uuid'])

    #create disk offering
    data_volume_size = 10737418240
    disk_offering_option = test_util.DiskOfferingOption()
    disk_offering_option.set_name('root-disk-iso')
    disk_offering_option.set_diskSize(data_volume_size)
    data_volume_offering = vol_ops.create_volume_offering(disk_offering_option)
    test_obj_dict.add_disk_offering(data_volume_offering)

    #create instance offering
    cpuNum = 2
    memorySize = 2147483648
    name = 'iso-vm-offering'
    new_offering_option = test_util.InstanceOfferingOption()
    new_offering_option.set_cpuNum(cpuNum)
    new_offering_option.set_memorySize(memorySize)
    new_offering_option.set_name(name)
    new_offering = vm_ops.create_instance_offering(new_offering_option)
    test_obj_dict.add_instance_offering(new_offering)

    #add iso
    img_option = test_util.ImageOption()
    img_option.set_name('iso1')
    bs_uuid = res_ops.query_resource_fields(res_ops.BACKUP_STORAGE, [],
                                            None)[0].uuid
    img_option.set_backup_storage_uuid_list([bs_uuid])
    img_option.set_url(
        'http://172.20.1.15:7480/iso/CentOS-x86_64-7.2-Minimal.iso')
    image_inv = img_ops.add_iso_template(img_option)
    image_uuid = image_inv.uuid
    image = test_image.ZstackTestImage()
    image.set_image(image_inv)
    image.set_creation_option(img_option)
    test_obj_dict.add_image(image)

    #create vm by iso
    l3_name = os.environ.get('l3VlanNetworkName1')
    l3_net_uuid = test_lib.lib_get_l3_by_name(l3_name).uuid
    root_disk_uuid = data_volume_offering.uuid
    vm = test_stub.create_vm_with_iso([l3_net_uuid], image_uuid, 'iso-vm',
                                      root_disk_uuid, new_offering.uuid)
    host_ip = test_lib.lib_find_host_by_vm(vm.get_vm()).managementIp
    test_obj_dict.add_vm(vm)

    #check vm
    vm_inv = vm.get_vm()
    vm_ip = vm_inv.vmNics[0].ip

    #cmd ='[ -e /root ]'
    #ssh_timeout = test_lib.SSH_TIMEOUT
    #test_lib.SSH_TIMEOUT = 3600
    test_lib.lib_set_vm_host_l2_ip(vm_inv)
    test_lib.lib_wait_target_up(vm.get_vm().vmNics[0].ip, 22, 1800)
    #if not test_lib.lib_ssh_vm_cmd_by_agent_with_retry(host_ip, vm_ip, 'root', 'password', cmd):
    #    test_lib.SSH_TIMEOUT = ssh_timeout
    #    test_util.test_fail("iso has been failed to installed.")

    #test_lib.SSH_TIMEOUT = ssh_timeout

    #clone vm
    cloned_vm_name = ['cloned_vm_name']
    cloned_vm_obj = vm.clone(cloned_vm_name)[0]
    test_obj_dict.add_vm(cloned_vm_obj)

    #delete iso
    image.delete()
    test_obj_dict.rm_image(image)

    #vm ops test
    test_stub.vm_ops_test(cloned_vm_obj, "VM_TEST_ALL")

    #expunge iso
    image.expunge()

    #detach iso
    img_ops.detach_iso(vm.vm.uuid)

    #vm ops test
    test_stub.vm_ops_test(cloned_vm_obj, "VM_TEST_ALL")

    #create image by vm root volume
    cloned_vm_img_name = "cloned_vm_image1"
    img_option2 = test_util.ImageOption()
    img_option2.set_backup_storage_uuid_list([bs_uuid])
    img_option2.set_root_volume_uuid(cloned_vm_obj.vm.rootVolumeUuid)
    img_option2.set_name(cloned_vm_img_name)
    image1 = test_image.ZstackTestImage()
    image1.set_creation_option(img_option2)
    image1.create()
    image1.check()
    test_obj_dict.add_image(image1)

    #export image
    image1.export()

    #create vm
    vm2 = test_stub.create_vm('image-vm', cloned_vm_img_name, l3_name)

    #delete image
    image1.delete()
    test_obj_dict.rm_image(image1)

    #vm ops test
    test_stub.vm_ops_test(vm2, "VM_TEST_ALL")

    #expunge image
    image1.expunge()

    #vm ops test
    test_stub.vm_ops_test(vm2, "VM_TEST_ALL")

    vm.destroy()
    vm2.destroy()
    cloned_vm_obj.destroy()

    vol_ops.delete_disk_offering(root_disk_uuid)
    vm_ops.delete_instance_offering(new_offering.uuid)
    test_obj_dict.rm_vm(vm)
    test_obj_dict.rm_disk_offering(data_volume_offering)
    test_obj_dict.rm_instance_offering(new_offering)

    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass('Create VM Image in Image Store Success')
def test():
    global original_rate

    must_ps_list = [
        inventory.LOCAL_STORAGE_TYPE, inventory.NFS_PRIMARY_STORAGE_TYPE
    ]
    test_lib.skip_test_if_any_ps_not_deployed(must_ps_list)

    test_util.test_dsc('Test change storage over provision rate method')
    zone_uuid = res_ops.query_resource(res_ops.ZONE)[0].uuid
    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)
    host = res_ops.query_resource_with_num(res_ops.HOST, cond, limit=1)
    if not host:
        test_util.test_skip('No Enabled/Connected host was found, skip test.')
        return True

    host = host[0]
    over_provision_rate1 = 2.5
    over_provision_rate2 = 1.5
    target_volume_num = 12
    kept_disk_size = 10 * 1024 * 1024

    vm = test_stub.create_vm(vm_name = 'storage_over_prs_vm_1', \
                    host_uuid = host.uuid)
    vm.check()
    test_obj_dict.add_vm(vm)

    cond = res_ops.gen_query_conditions('state', '=', 'Enabled')
    cond = res_ops.gen_query_conditions('status', '=', 'Connected', cond)

    ps = res_ops.query_resource_with_num(res_ops.PRIMARY_STORAGE,
                                         cond,
                                         limit=2)
    if not ps:
        test_util.test_skip(
            'No Enabled/Connected primary storage was found, skip test.')
        return True

    ps1 = ps[0].uuid
    ps1_res = test_lib.lib_get_storage_capacity(ps_uuids=[ps1])
    test_util.test_dsc("ps1[uuid:%s]'s available capacity is %s" %
                       (ps1, ps1_res.availableCapacity))
    avail_cap = ps1_res.availableCapacity

    if len(ps) > 1:
        ps2 = ps[1].uuid
        ps2_res = test_lib.lib_get_storage_capacity(ps_uuids=[ps2])
        test_util.test_dsc("ps2[uuid:%s]'s available capacity is %s" %
                           (ps2, ps2_res.availableCapacity))
        avail_cap = ps1_res.availableCapacity + ps2_res.availableCapacity

    if avail_cap < kept_disk_size:
        test_util.test_skip(
            'available disk capacity:%d is too small, skip test.' % avail_cap)
        return True

    original_rate = test_lib.lib_set_provision_storage_rate(
        over_provision_rate1)
    data_volume_size = int(over_provision_rate1 *
                           (avail_cap - kept_disk_size) / target_volume_num)
    disk_offering_option = test_util.DiskOfferingOption()
    disk_offering_option.set_name('storage-over-ps-test')
    disk_offering_option.set_diskSize(data_volume_size)
    data_volume_offering = vol_ops.create_volume_offering(disk_offering_option)
    test_obj_dict.add_disk_offering(data_volume_offering)

    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_disk_offering_uuid(data_volume_offering.uuid)

    volume_creation_option.set_name('volume-1')
    volume1 = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume1)
    #res = test_lib.lib_get_storage_capacity(zone_uuids = [zone_uuid])
    #test_util.test_logger('Current available storage size: %d' % res.availableCapacity)
    volume1.attach(vm)

    test_lib.lib_set_provision_storage_rate(over_provision_rate2)
    volume_creation_option.set_name('volume-2')
    volume2 = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume2)
    #res = test_lib.lib_get_storage_capacity(zone_uuids = [zone_uuid])
    #test_util.test_logger('Current available storage size: %d' % res.availableCapacity)
    volume2.attach(vm)
    volume1.delete()
    volume1.expunge()

    test_lib.lib_set_provision_storage_rate(over_provision_rate1)
    volume2.delete()
    volume2.expunge()

    test_lib.lib_set_provision_storage_rate(original_rate)
    time.sleep(10)

    ps1_res2 = test_lib.lib_get_storage_capacity(ps_uuids=[ps1])
    test_util.test_dsc("ps1[uuid:%s]'s available capacity is %s" %
                       (ps1, ps1_res.availableCapacity))
    avail_cap2 = ps1_res2.availableCapacity

    if len(ps) > 1:
        ps2_res2 = test_lib.lib_get_storage_capacity(ps_uuids=[ps2])
        test_util.test_dsc("ps2[uuid:%s]'s available capacity is %s" %
                           (ps2, ps2_res.availableCapacity))
        avail_cap2 = ps1_res2.availableCapacity + ps2_res2.availableCapacity

    if avail_cap2 != avail_cap:
        test_util.test_fail(
            'Available disk size: %d is different with original size: %d, after creating volume under different over rate.'
            % (avail_cap2, avail_cap))

    test_lib.lib_robot_cleanup(test_obj_dict)
    test_util.test_pass('Memory Over Provision Test Pass')