def test():
    global session_uuid
    session_uuid = acc_ops.login_as_admin()
    l3_1_name = os.environ.get('l3VlanNetworkName1')
    l3_1 = test_lib.lib_get_l3_by_name(l3_1_name)

    #create VRs.
    vrs = test_lib.lib_find_vr_by_l3_uuid(l3_1.uuid)
    if not vrs:
        vm = test_stub.create_vlan_vm(l3_name=l3_1_name)
        vm.destroy()
        vr1 = test_lib.lib_find_vr_by_l3_uuid(l3_1.uuid)[0]
    else:
        vr1 = vrs[0]

    vm_ops.stop_vm(vr1.uuid)
    if test_lib.lib_get_ha_enable() != 'true' or vr1.applianceVmType != "vrouter":
        vm_ops.start_vm(vr1.uuid)

    vr1_mgmt_ip = test_lib.lib_find_vr_mgmt_ip(vr1)
    if not test_lib.lib_wait_target_up(vr1_mgmt_ip, '7272', 120):
        test_util.test_fail('vr: %s is not startup in 120 seconds. Fail to reboot it. ' % vr1.uuid)

    timeout = time.time() + 60
    while time.time() < timeout:
        cond = res_ops.gen_query_conditions('uuid', '=', vr1.uuid) 
        vms = res_ops.query_resource(res_ops.VM_INSTANCE, cond)
        if vms[0].status == 'Connected':
            test_util.test_pass('Test Reconnect VR VM start VRs success')
        time.sleep(1)

    test_util.test_fail('VR VM: %s is not connected within 60 seconds' % vr1.uuid)
예제 #2
0
def test():
    test_util.test_dsc('Create test vm1 and check')
    vm1 = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm1)
    test_util.test_dsc('Create test vm2 and check')
    vm2 = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm2)

    vm1.check()
    vm2.check()

    vrs = test_lib.lib_find_vr_by_vm(vm1.vm)
    if len(vrs) != 1:
        test_util.test_logger(
            'more than 1 VR are found for vm1: %s. Will test the 1st one: %s.'
            % (vm1.vm.uuid, vr.uuid))

    vr = vrs[0]
    vr_mgmt_ip = test_lib.lib_find_vr_mgmt_ip(vr)
    if not test_lib.lib_check_testagent_status(vr_mgmt_ip):
        test_util.test_fail(
            'vr: %s is not reachable, since can not reach its test agent. Give up test and test failure. '
            % vr.uuid)
    test_lib.lib_install_testagent_to_vr_with_vr_vm(vr)
    #Need to put the vr restart into thread. Since vr reboot API is a sync API.
    thread = threading.Thread(target=vm_ops.reboot_vm, args=(vr.uuid, ))
    thread.start()
    #check vr vr service port
    if not test_lib.lib_wait_target_down(vr_mgmt_ip, '7272', 120):
        test_util.test_fail(
            'vr: %s is not shutdown in 120 seconds. Fail to reboot it. ' %
            vr.uuid)

    if not test_lib.lib_wait_target_up(vr_mgmt_ip, '7272', 120):
        test_util.test_fail(
            'vr: %s is not startup in 120 seconds. Fail to reboot it. ' %
            vr.uuid)

    #avoid of possible apt conflicting between install testagent and appliancevm
    #time.sleep(60)
    vm1.check()
    vm2.check()

    test_util.test_dsc('Ping from vm1 to vm2.')
    test_lib.lib_check_ping(vm1.vm, vm2.vm.vmNics[0].ip)
    vm1.destroy()
    vm2.destroy()
    test_util.test_pass(
        'Create vlan VirtualRouter VM (and reboot VR after VM created) Test with snat ping between two VMs Success'
    )
def test():
    test_util.test_dsc('Create test vm1 and check')
    vm1 = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm1)
    test_util.test_dsc('Create test vm2 and check')
    vm2 = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm2)

    vm1.check()
    vm2.check()

    vrs = test_lib.lib_find_vr_by_vm(vm1.vm)
    if len(vrs) != 1:
        test_util.test_logger('more than 1 VR are found for vm1: %s. Will test the 1st one: %s.' % (vm1.vm.uuid, vr.uuid))

    vr = vrs[0]
    vr_mgmt_ip = test_lib.lib_find_vr_mgmt_ip(vr)
    if not test_lib.lib_check_testagent_status(vr_mgmt_ip):
        test_util.test_fail('vr: %s is not reachable, since can not reach its test agent. Give up test and test failure. ' % vr.uuid)
    test_lib.lib_install_testagent_to_vr_with_vr_vm(vr)
    #Need to put the vr restart into thread. Since vr reboot API is a sync API. 
    thread = threading.Thread(target=vm_ops.reboot_vm, args=(vr.uuid,))
    thread.start()
    #check vr vr service port
    if not test_lib.lib_wait_target_down(vr_mgmt_ip, '7272', 60):
        test_util.test_fail('vr: %s is not shutdown in 60 seconds. Fail to reboot it. ' % vr.uuid)

    if not test_lib.lib_wait_target_up(vr_mgmt_ip, '7272', 120):
        test_util.test_fail('vr: %s is not startup in 120 seconds. Fail to reboot it. ' % vr.uuid)

    #avoid of possible apt conflicting between install testagent and appliancevm
    #time.sleep(60)
    vm1.check()
    vm2.check()

    test_util.test_dsc('Ping from vm1 to vm2.')
    test_lib.lib_check_ping(vm1.vm, vm2.vm.vmNics[0].ip)
    vm1.destroy()
    vm2.destroy()
    test_util.test_pass('Create vlan VirtualRouter VM (and reboot VR after VM created) Test with snat ping between two VMs Success')
def test():
    global session_uuid
    session_uuid = acc_ops.login_as_admin()
    l3_1_name = os.environ.get('l3VlanNetworkName1')
    l3_1 = test_lib.lib_get_l3_by_name(l3_1_name)

    #create VRs.
    vrs = test_lib.lib_find_vr_by_l3_uuid(l3_1.uuid)
    if not vrs:
        vm = test_stub.create_vlan_vm(l3_name=l3_1_name)
        vm.destroy()
        vr1 = test_lib.lib_find_vr_by_l3_uuid(l3_1.uuid)[0]
    else:
        vr1 = vrs[0]

    vm_ops.stop_vm(vr1.uuid)
    if test_lib.lib_get_ha_enable(
    ) != 'true' or vr1.applianceVmType != "vrouter":
        vm_ops.start_vm(vr1.uuid)

    vr1_mgmt_ip = test_lib.lib_find_vr_mgmt_ip(vr1)
    if not test_lib.lib_wait_target_up(vr1_mgmt_ip, '7272', 120):
        test_util.test_fail(
            'vr: %s is not startup in 120 seconds. Fail to reboot it. ' %
            vr1.uuid)

    timeout = time.time() + 60
    while time.time() < timeout:
        cond = res_ops.gen_query_conditions('uuid', '=', vr1.uuid)
        vms = res_ops.query_resource(res_ops.VM_INSTANCE, cond)
        if vms[0].status == 'Connected':
            test_util.test_pass('Test Reconnect VR VM start VRs success')
        time.sleep(1)

    test_util.test_fail('VR VM: %s is not connected within 60 seconds' %
                        vr1.uuid)
    def check(self):
        super(zstack_kvm_vm_dhcp_checker, self).check()
        vm = self.test_obj.vm
        test_lib.lib_install_testagent_to_vr(vm)
        host = test_lib.lib_get_vm_host(vm)
        test_lib.lib_install_testagent_to_host(host)
        test_lib.lib_set_vm_host_l2_ip(vm)

        vm_cmd_result = None
        vr_vms = test_lib.lib_find_vr_by_vm(vm)
        print('find %d vr vms.' % len(vr_vms))
        for vr_vm in vr_vms:
            test_util.test_logger(
                "Begin to check [vm:] %s DHCP binding setting in [VR:] %s" %
                (vm.uuid, vr_vm.uuid))
            nic = test_lib.lib_get_vm_nic_by_vr(vm, vr_vm)
            if not 'DHCP' in \
                    test_lib.lib_get_l3_service_type(nic.l3NetworkUuid):
                test_util.test_logger(
                    "Skip [VR:] %s, since it doesn't provide DHCP service" %
                    vr_vm.uuid)
                continue

            guest_ip = nic.ip
            guest_mac = nic.mac
            vr_ip = test_lib.lib_find_vr_mgmt_ip(vr_vm)
            if vr_vm.hasattr(
                    'applianceVmType') and vr_vm.applianceVmType == 'vrouter':
                command = '/bin/cli-shell-api showCfg'
            else:
                command = 'cat /etc/hosts.dhcp'
            vr_cmd_result = test_lib.lib_execute_sh_cmd_by_agent_with_retry(
                vr_ip, command, self.exp_result)
            if not vr_cmd_result:
                test_util.test_logger(
                    'Checker result: FAIL to execute shell commaond in [vm:] %s'
                    % vr_vm.uuid)
                return self.judge(False)

            if vr_cmd_result == True:
                test_util.test_logger(
                    'Checker result: FAIL to get ssh result in [vm:] %s' %
                    vr_vm.uuid)
                return self.judge(False)

            if not guest_mac in vr_cmd_result or not guest_ip in vr_cmd_result:
                test_util.test_logger(
                    'Checker result: [vm:] %s [mac:] %s is not found in [vr:] %s. VR ip/mac result is %s.'
                    % (vm.uuid, guest_mac, vr_vm.uuid, vr_cmd_result))
                return self.judge(False)
            else:
                test_util.test_logger(
                    'Checker result: [vm:] %s [mac:] %s is found in VR %s .' %
                    (vm.uuid, guest_mac, vr_vm.uuid))

            test_util.test_logger("Begin to check VM DHCP in VM: %s" % vm.uuid)
            if not vm_cmd_result:
                vm_command = '/sbin/ip a'
                vm_cmd_result = test_lib.lib_ssh_vm_cmd_by_agent_with_retry(
                    host.managementIp, nic.ip,
                    test_lib.lib_get_vm_username(vm),
                    test_lib.lib_get_vm_password(vm), vm_command,
                    self.exp_result)
                if not vm_cmd_result:
                    test_util.test_logger(
                        'Checker result: FAIL to execute test ssh command in test [vm:] %s throught [host:] %s.'
                        % (vm.uuid, host.name))
                    return self.judge(False)

            if isinstance(vm_cmd_result, str) and guest_ip in vm_cmd_result:
                test_util.test_logger(
                    'Checker result: guest [ip:] %s is SET in guest [vm:] %s.'
                    % (guest_ip, vm.uuid))
            else:
                test_util.test_logger(
                    'Checker result: guest [ip:] %s is NOT found in guest [vm:] %s. \n Will Try again. It might be because the ifconfig is not reflect the ip address yet. \n The current ifconfig result is: %s'
                    % (guest_ip, vm.uuid, vm_cmd_result))
                vm_cmd_result = test_lib.lib_ssh_vm_cmd_by_agent_with_retry(
                    host.managementIp, nic.ip,
                    test_lib.lib_get_vm_username(vm),
                    test_lib.lib_get_vm_password(vm), vm_command,
                    self.exp_result)
                if not vm_cmd_result:
                    test_util.test_logger(
                        'Checker result: FAIL to execute test ssh command in test [vm:] %s throught [host:] %s.'
                        % (vm.uuid, host.name))
                    return self.judge(False)
                if isinstance(vm_cmd_result,
                              str) and guest_ip in vm_cmd_result:
                    test_util.test_logger(
                        'Checker result: guest [ip:] %s is SET in guest [vm:] %s.'
                        % (guest_ip, vm.uuid))
                else:
                    if not guest_ip in vm_cmd_result:
                        test_util.test_logger(
                            'Checker result: guest [ip:] %s is NOT found in guest [vm:] %s. The current ifconfig result is: %s'
                            % (guest_ip, vm.uuid, vm_cmd_result))
                    else:
                        test_util.test_logger(
                            'vm_cmd_result: %s is not string type. It is: %s .'
                            % (vm_cmd_result, type(vm_cmd_result)))
                    return self.judge(False)

        return self.judge(True)
def test():
   test_util.test_dsc('Test Change VM Image Function')
   global vm
   #set overProvisioning.primaryStorage's value as 10
   con_ops.change_global_config('mevoco','overProvisioning.primaryStorage',10)
   test_lib.lib_create_disk_offering(diskSize=1099511627776,name="1T")
   l3_uuid = test_lib.lib_get_l3_by_name("l3VlanNetwork1").uuid
   image_uuid = test_lib.lib_get_image_by_name("ttylinux").uuid
   disk_offering_uuids = [test_lib.lib_get_disk_offering_by_name("smallDiskOffering").uuid,test_lib.lib_get_disk_offering_by_name("root-disk").uuid,test_lib.lib_get_disk_offering_by_name("1T").uuid]
   vm = test_stub.create_vm(l3_uuid_list = [l3_uuid],image_uuid = image_uuid,vm_name="test-vm",disk_offering_uuids = disk_offering_uuids)
   test_obj_dict.add_vm(vm)
   vm.check()

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

   test_lib.lib_destroy_vm_and_data_volumes(vm.get_vm())
   test_util.test_pass('Change Vm Image Test Success')
    def check(self):
        super(zstack_kvm_vm_dhcp_checker, self).check()
        vm = self.test_obj.vm
        test_lib.lib_install_testagent_to_vr(vm)
        host = test_lib.lib_get_vm_host(vm)
        test_lib.lib_install_testagent_to_host(host)
        test_lib.lib_set_vm_host_l2_ip(vm)

        vm_cmd_result = None
        vr_vms = test_lib.lib_find_vr_by_vm(vm)
        print('find %d vr vms.' % len(vr_vms))
        for vr_vm in vr_vms:
            test_util.test_logger("Begin to check [vm:] %s DHCP binding setting in [VR:] %s" % (vm.uuid, vr_vm.uuid))
            nic = test_lib.lib_get_vm_nic_by_vr(vm, vr_vm)
            if not 'DHCP' in \
                    test_lib.lib_get_l3_service_type(nic.l3NetworkUuid):
                test_util.test_logger("Skip [VR:] %s, since it doesn't provide DHCP service" % vr_vm.uuid)
                continue

            for i in range(300):
                cond = res_ops.gen_query_conditions('uuid', '=', vr_vm.uuid)
                vr = res_ops.query_resource_fields(res_ops.VM_INSTANCE, cond, None)[0]
                if "connected" in vr.status.lower():
                    test_util.test_logger("vr.uuid=%s vr.status=%s" %(vr_vm.uuid, vr.status.lower()))
                    break
                time.sleep(1)
            else:
                test_util.test_fail("vr.uuid=%s is not changed to changed within max waiting time." %(vr_vm.uuid))
          
            guest_ip = nic.ip
            guest_mac = nic.mac
            vr_ip = test_lib.lib_find_vr_mgmt_ip(vr_vm)
            if vr_vm.hasattr('applianceVmType') and vr_vm.applianceVmType == 'vrouter':
                command = '/bin/cli-shell-api showCfg'
            else:
                command = 'cat /etc/hosts.dhcp'
            vr_cmd_result = test_lib.lib_execute_sh_cmd_by_agent_with_retry(vr_ip, command, self.exp_result)
            if not vr_cmd_result:
                test_util.test_logger('Checker result: FAIL to execute shell commaond in [vm:] %s' % vr_vm.uuid)
                return self.judge(False)
            
            if vr_cmd_result == True:
                test_util.test_logger('Checker result: FAIL to get ssh result in [vm:] %s' % vr_vm.uuid)
                return self.judge(False)

            if not guest_mac in vr_cmd_result or not guest_ip in vr_cmd_result:
                test_util.test_logger('Checker result: [vm:] %s [mac:] %s is not found in [vr:] %s. VR ip/mac result is %s.' % (vm.uuid, guest_mac, vr_vm.uuid, vr_cmd_result))
                return self.judge(False)
            else:
                test_util.test_logger('Checker result: [vm:] %s [mac:] %s is found in VR %s .' % (vm.uuid, guest_mac, vr_vm.uuid))

            test_util.test_logger("Begin to check VM DHCP in VM: %s" % vm.uuid)
            if not vm_cmd_result:
                vm_command = '/sbin/ip a'
                vm_cmd_result = test_lib.lib_ssh_vm_cmd_by_agent_with_retry(host.managementIp, nic.ip, test_lib.lib_get_vm_username(vm), test_lib.lib_get_vm_password(vm), vm_command, self.exp_result)
                if not vm_cmd_result:
                    test_util.test_logger('Checker result: FAIL to execute test ssh command in test [vm:] %s throught [host:] %s.' % (vm.uuid, host.name))
                    return self.judge(False)

            if isinstance(vm_cmd_result, str) and guest_ip in vm_cmd_result:
                test_util.test_logger('Checker result: guest [ip:] %s is SET in guest [vm:] %s.' % (guest_ip, vm.uuid))
            else:
                test_util.test_logger('Checker result: guest [ip:] %s is NOT found in guest [vm:] %s. \n Will Try again. It might be because the ifconfig is not reflect the ip address yet. \n The current ifconfig result is: %s' % (guest_ip, vm.uuid, vm_cmd_result))
                vm_cmd_result = test_lib.lib_ssh_vm_cmd_by_agent_with_retry(host.managementIp, nic.ip, test_lib.lib_get_vm_username(vm), test_lib.lib_get_vm_password(vm), vm_command, self.exp_result)
                if not vm_cmd_result:
                    test_util.test_logger('Checker result: FAIL to execute test ssh command in test [vm:] %s throught [host:] %s.' % (vm.uuid, host.name))
                    return self.judge(False)
                if isinstance(vm_cmd_result, str) and guest_ip in vm_cmd_result:
                    test_util.test_logger('Checker result: guest [ip:] %s is SET in guest [vm:] %s.' % (guest_ip, vm.uuid))
                else:
                    if not guest_ip in vm_cmd_result:
                        test_util.test_logger('Checker result: guest [ip:] %s is NOT found in guest [vm:] %s. The current ifconfig result is: %s' % (guest_ip, vm.uuid, vm_cmd_result))
                    else:
                        test_util.test_logger('vm_cmd_result: %s is not string type. It is: %s .' % (vm_cmd_result, type(vm_cmd_result)))
                    return self.judge(False)

        return self.judge(True)
    def check(self):
        super(zstack_kvm_vm_dhcp_checker, self).check()
        vm = self.test_obj.vm
        test_lib.lib_install_testagent_to_vr(vm)
        host = test_lib.lib_get_vm_host(vm)
        test_lib.lib_install_testagent_to_host(host)
        test_lib.lib_set_vm_host_l2_ip(vm)

        vm_cmd_result = None
        vr_vms = test_lib.lib_find_vr_by_vm(vm)
        print('find %d vr vms.' % len(vr_vms))
        for vr_vm in vr_vms:
            test_util.test_logger("Begin to check [vm:] %s DHCP binding setting in [VR:] %s" % (vm.uuid, vr_vm.uuid))
            nic = test_lib.lib_get_vm_nic_by_vr(vm, vr_vm)
            if not 'DHCP' in \
                    test_lib.lib_get_l3_service_type(nic.l3NetworkUuid):
                test_util.test_logger("Skip [VR:] %s, since it doesn't provide DHCP service" % vr_vm.uuid)
                continue

            guest_ip = nic.ip
            guest_mac = nic.mac
            vr_ip = test_lib.lib_find_vr_mgmt_ip(vr_vm)
            command = 'cat /etc/hosts.dhcp'
            vr_cmd_result = test_lib.lib_execute_sh_cmd_by_agent_with_retry(vr_ip, command, self.exp_result)
            if not vr_cmd_result:
                test_util.test_logger('Checker result: FAIL to execute shell commaond in [vm:] %s' % vr_vm.uuid)
                return self.judge(False)
            
            if vr_cmd_result == True:
                test_util.test_logger('Checker result: FAIL to get ssh result in [vm:] %s' % vr_vm.uuid)
                return self.judge(False)

            if not guest_mac in vr_cmd_result or not guest_ip in vr_cmd_result:
                test_util.test_logger('Checker result: [vm:] %s [mac:] %s is not found in [vr:] %s. VR ip/mac result is %s.' % (vm.uuid, guest_mac, vr_vm.uuid, vr_cmd_result))
                return self.judge(False)
            else:
                test_util.test_logger('Checker result: [vm:] %s [mac:] %s is found in VR %s .' % (vm.uuid, guest_mac, vr_vm.uuid))

            test_util.test_logger("Begin to check VM DHCP in VM: %s" % vm.uuid)
            if not vm_cmd_result:
                vm_command = '/sbin/ip a'
                vm_cmd_result = test_lib.lib_ssh_vm_cmd_by_agent_with_retry(host.managementIp, nic.ip, test_lib.lib_get_vm_username(vm), test_lib.lib_get_vm_password(vm), vm_command, self.exp_result)
                if not vm_cmd_result:
                    test_util.test_logger('Checker result: FAIL to execute test ssh command in test [vm:] %s throught [host:] %s.' % (vm.uuid, host.name))
                    return self.judge(False)

            if isinstance(vm_cmd_result, str) and guest_ip in vm_cmd_result:
                test_util.test_logger('Checker result: guest [ip:] %s is SET in guest [vm:] %s.' % (guest_ip, vm.uuid))
            else:
                test_util.test_logger('Checker result: guest [ip:] %s is NOT found in guest [vm:] %s. \n Will Try again. It might be because the ifconfig is not reflect the ip address yet. \n The current ifconfig result is: %s' % (guest_ip, vm.uuid, vm_cmd_result))
                vm_cmd_result = test_lib.lib_ssh_vm_cmd_by_agent_with_retry(host.managementIp, nic.ip, test_lib.lib_get_vm_username(vm), test_lib.lib_get_vm_password(vm), vm_command, self.exp_result)
                if not vm_cmd_result:
                    test_util.test_logger('Checker result: FAIL to execute test ssh command in test [vm:] %s throught [host:] %s.' % (vm.uuid, host.name))
                    return self.judge(False)
                if isinstance(vm_cmd_result, str) and guest_ip in vm_cmd_result:
                    test_util.test_logger('Checker result: guest [ip:] %s is SET in guest [vm:] %s.' % (guest_ip, vm.uuid))
                else:
                    if not guest_ip in vm_cmd_result:
                        test_util.test_logger('Checker result: guest [ip:] %s is NOT found in guest [vm:] %s. The current ifconfig result is: %s' % (guest_ip, vm.uuid, vm_cmd_result))
                    else:
                        test_util.test_logger('vm_cmd_result: %s is not string type. It is: %s .' % (vm_cmd_result, type(vm_cmd_result)))
                    return self.judge(False)

        return self.judge(True)
def test():
    global vr_type
    test_util.test_dsc('Create test vm1 and check')
    vm1 = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm1)
    test_util.test_dsc('Create test vm2 and check')
    vm2 = test_stub.create_vlan_vm()
    test_obj_dict.add_vm(vm2)

    vm1.check()
    vm2.check()

    vr_type = res_ops.query_resource(
        res_ops.VIRTUALROUTER_VM)[0].applianceVmType
    test_util.test_logger("vr type is %s" % (vr_type))

    #vrs = test_lib.lib_find_vr_by_vm(vm1.vm)
    vrs = test_lib.lib_find_flat_dhcp_vr_by_vm(vm1.vm)
    vr = vrs[0]
    if len(vrs) != 1:
        test_util.test_logger(
            'more than 1 VR are found for vm1: %s. Will test the 1st one: %s.'
            % (vm1.vm.uuid, vr.uuid))

    vr_mgmt_ip = test_lib.lib_find_vr_mgmt_ip(vr)

    vr_reboot_record_org = check_vr_reboot_record_line(vr_mgmt_ip)

    test_lib.lib_install_testagent_to_vr_with_vr_vm(vr)
    if not test_lib.lib_check_testagent_status(vr_mgmt_ip):
        test_util.test_fail(
            'vr: %s is not reachable, since can not reach its test agent. Give up test and test failure. '
            % vr.uuid)

    #Need to put the vr restart into thread. Since vr reboot API is a sync API.
    thread = threading.Thread(target=vm_ops.reboot_vm, args=(vr.uuid, ))
    thread.start()

    #check vr vr service port
    cond = res_ops.gen_query_conditions('resourceUuid', '=', vr.uuid)
    cond = res_ops.gen_query_conditions('tag', '=', "ha::NeverStop", cond)
    if res_ops.query_resource(res_ops.SYSTEM_TAG,
                              cond) and res_ops.query_resource(
                                  res_ops.SYSTEM_TAG, cond)[0]:
        time.sleep(30)
    elif not test_lib.lib_wait_target_down(vr_mgmt_ip, '7272', 120):
        test_util.test_fail(
            'vr: %s is not shutdown in 120 seconds. Fail to reboot it. ' %
            vr.uuid)

    if not test_lib.lib_wait_target_up(vr_mgmt_ip, '7272', 120):
        test_util.test_fail(
            'vr: %s is not startup in 120 seconds. Fail to reboot it. ' %
            vr.uuid)

    vr_reboot_record_new = check_vr_reboot_record_line(vr_mgmt_ip)

    if vr_reboot_record_new == vr_reboot_record_org:
        test_util.test_fail("not find vr reboot record increased.")

    #avoid of possible apt conflicting between install testagent and appliancevm
    #time.sleep(60)
    vm1.check()
    vm2.check()

    test_util.test_dsc('Ping from vm1 to vm2.')
    test_lib.lib_check_ping(vm1.vm, vm2.vm.vmNics[0].ip)
    vm1.destroy()
    vm2.destroy()
    test_util.test_pass(
        'Create vlan VirtualRouter VM (and reboot VR after VM created) Test with snat ping between two VMs Success'
    )