def test():
    vm1 = test_stub.create_vr_vm('maintain_host_vm1', 'imageName_s',
                                 'l3VlanNetwork2')
    test_obj_dict.add_vm(vm1)

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

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

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

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

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

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

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

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

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

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

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

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

    vm1.check()
    vm2.check()

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

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

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

    ps = test_lib.lib_get_primary_storage_by_uuid(
        vm1.get_vm().allVolumes[0].primaryStorageUuid)
    if ps.type == inventory.LOCAL_STORAGE_TYPE:
        test_util.test_skip(
            'skip migrate vm with data volume if localstorate is used')

    ps = test_lib.lib_get_primary_storage_by_uuid(
        vm2.get_vm().allVolumes[0].primaryStorageUuid)
    if ps.type == inventory.LOCAL_STORAGE_TYPE:
        test_util.test_skip(
            'skip migrate vm with data volume if localstorate is used')

    test_util.test_dsc('Create volume and check')
    disk_offering = test_lib.lib_get_disk_offering_by_name(
        os.environ.get('smallDiskOfferingName'))
    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_disk_offering_uuid(disk_offering.uuid)

    volume = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume)

    test_util.test_dsc('Attach volume and check')
    volume.attach(vm1)
    volume.check()

    current_host1 = test_lib.lib_get_vm_host(vm1.vm)
    conditions = res_ops.gen_query_conditions('clusterUuid', '=',
                                              vm1.vm.clusterUuid)
    conditions = res_ops.gen_query_conditions('state', '=',
                                              host_header.ENABLED, conditions)
    conditions = res_ops.gen_query_conditions('status', '=',
                                              host_header.CONNECTED,
                                              conditions)
    all_hosts = res_ops.query_resource(res_ops.HOST, conditions)
    if len(all_hosts) <= 1:
        test_util.test_fail(
            'Not available host to do maintenance, since there is only %s host'
            % len(all_hosts))

    target_host = random.choice(all_hosts)
    if current_host1.uuid != target_host.uuid:
        vm1.migrate(target_host.uuid)

    current_host2 = test_lib.lib_get_vm_host(vm2.vm)
    if current_host2.uuid != target_host.uuid:
        vm2.migrate(target_host.uuid)

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

    volume.check()

    host = test_kvm_host.ZstackTestKvmHost()
    host.set_host(target_host)

    host.maintain()

    #need to update vm's inventory, since they will be changed by maintenace mode
    vm1.update()
    vm2.update()

    vm1.check()
    vm2.check()
    volume.check()

    host.change_state(test_kvm_host.ENABLE_EVENT)
    if not linux.wait_callback_success(is_host_connected,
                                       host.get_host().uuid, 120):
        test_util.test_fail(
            'host status is not changed to connected, after changing its state to Enable'
        )

    vm1.migrate(target_host.uuid)
    vm2.migrate(target_host.uuid)

    vm1.check()
    vm2.check()
    volume.check()

    vm1.destroy()
    test_obj_dict.rm_vm(vm1)
    vm2.destroy()
    test_obj_dict.rm_vm(vm2)
    volume.delete()
    test_obj_dict.rm_volume(volume)
    test_util.test_pass('Maintain Host Test Success')
def test():
    global host
    if test_lib.lib_get_active_host_number() < 2:
        test_util.test_fail(
            'Not available host to do maintenance, since there are not 2 hosts'
        )

    vm1 = test_stub.create_vm(vm_name='maintain_host_vm1')
    test_obj_dict.add_vm(vm1)

    vm2 = test_stub.create_vm(vm_name='maintain_host_vm2')
    test_obj_dict.add_vm(vm2)

    #vm1.check()
    #vm2.check()

    test_util.test_dsc('Create volume and check')
    disk_offering = test_lib.lib_get_disk_offering_by_name(
        os.environ.get('smallDiskOfferingName'))
    volume_creation_option = test_util.VolumeOption()
    volume_creation_option.set_disk_offering_uuid(disk_offering.uuid)

    volume = test_stub.create_volume(volume_creation_option)
    test_obj_dict.add_volume(volume)

    test_util.test_dsc('Attach volume and check')
    volume.attach(vm1)
    volume.check()

    current_host1 = test_lib.lib_get_vm_host(vm1.vm)
    conditions = res_ops.gen_query_conditions('state', '=',
                                              host_header.ENABLED)
    conditions = res_ops.gen_query_conditions('status', '=',
                                              host_header.CONNECTED,
                                              conditions)
    all_hosts = res_ops.query_resource(res_ops.HOST, conditions)

    target_host = random.choice(all_hosts)
    if current_host1.uuid != target_host.uuid:
        vm1.migrate(target_host.uuid)

    current_host2 = test_lib.lib_get_vm_host(vm2.vm)
    if current_host2.uuid != target_host.uuid:
        vm2.migrate(target_host.uuid)

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

    volume.check()

    host = test_kvm_host.ZstackTestKvmHost()
    host.set_host(target_host)

    host.maintain()

    #need to update vm's inventory, since they will be changed by maintenace mode
    vm1.update()
    vm2.update()
    vm1.set_state(vm_header.STOPPED)
    vm2.set_state(vm_header.STOPPED)

    vm1.check()
    vm2.check()
    volume.check()

    host.change_state(test_kvm_host.ENABLE_EVENT)
    if not linux.wait_callback_success(is_host_connected,
                                       host.get_host().uuid, 120):
        test_util.test_fail(
            'host status is not changed to connected, after changing its state to Enable'
        )

    volume.detach()

    vm1_root_volume = test_lib.lib_get_root_volume(vm1.get_vm())
    vm2_root_volume = test_lib.lib_get_root_volume(vm2.get_vm())

    conditions = res_ops.gen_query_conditions('uuid', '!=', target_host.uuid,
                                              conditions)
    rest_hosts = res_ops.query_resource(res_ops.HOST, conditions)
    new_target_host = random.choice(rest_hosts)

    vol_ops.migrate_volume(vm1_root_volume.uuid, new_target_host.uuid)
    vol_ops.migrate_volume(vm2_root_volume.uuid, new_target_host.uuid)
    vol_ops.migrate_volume(volume.get_volume().uuid, new_target_host.uuid)

    volume.attach(vm1)

    vm1.start()
    vm2.start()

    vm1.check()
    vm2.check()
    volume.check()

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

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

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

    current_host1 = test_lib.lib_get_vm_host(vm1.vm)
    conditions = res_ops.gen_query_conditions('clusterUuid', '=',
                                              vm1.vm.clusterUuid)
    conditions = res_ops.gen_query_conditions('state', '=',
                                              host_header.ENABLED, conditions)
    conditions = res_ops.gen_query_conditions('status', '=',
                                              host_header.CONNECTED,
                                              conditions)
    all_hosts = res_ops.query_resource(res_ops.HOST, conditions)
    if len(all_hosts) <= 1:
        test_util.test_fail(
            'Not available host to do maintenance, since there is only %s host'
            % len(all_hosts))

    target_host = random.choice(all_hosts)
    if current_host1.uuid != target_host.uuid:
        vm1.migrate(target_host.uuid)

    current_host2 = test_lib.lib_get_vm_host(vm2.vm)
    if current_host2.uuid != target_host.uuid:
        vm2.migrate(target_host.uuid)

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

    host = test_kvm_host.ZstackTestKvmHost()
    host.set_host(target_host)

    host.maintain()

    #need to update vm's inventory, since they will be changed by maintenace mode
    vm1.update()
    vm2.update()

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

    host.change_state(test_kvm_host.ENABLE_EVENT)
    if not linux.wait_callback_success(is_host_connected,
                                       host.get_host().uuid, 120):
        test_util.test_fail(
            'host status is not changed to connected, after changing its state to Enable'
        )

    vm1.migrate(target_host.uuid)
    vm2.migrate(target_host.uuid)

    vm1.check()
    vm2.check()

    vm1.destroy()
    test_obj_dict.rm_vm(vm1)
    vm2.destroy()
    test_obj_dict.rm_vm(vm2)
    test_util.test_pass('Maintain Host Test Success')