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

    vm_cpu = 1
    vm_memory = 1073741824 #1G
    cond = res_ops.gen_query_conditions('name', '=', 'ttylinux')
    image_uuid = res_ops.query_resource(res_ops.IMAGE, cond)[0].uuid
    l3_network_uuid = res_ops.query_resource(res_ops.L3_NETWORK)[0].uuid
    vm = test_stub.create_mini_vm([l3_network_uuid], image_uuid, cpu_num = vm_cpu, memory_size = vm_memory)
    test_obj_dict.add_vm(vm)

    host_uuid = test_lib.lib_get_vm_host(vm.vm).uuid
    host_ops.change_host_state(host_uuid, 'maintain')

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

    vm.check()

    host_ops.change_host_state(host_uuid, 'enable')
    if not linux.wait_callback_success(is_host_connected, host_uuid, 120):
        test_util.test_fail('host status is not changed to connected, after changing its state to Enable')

    vm.start()
    vm.check()
    vm.destroy()
    test_obj_dict.rm_vm(vm)
    test_util.test_pass('Maintain Host Test Success')
def test():
    if test_lib.lib_get_active_host_number() < 2:
        test_util.test_skip('There is not 2 or more hosts to do migration test')

    vm = test_stub.create_vm(vm_name = 'test-vm-migration')
    test_obj_dict.add_vm(vm)
    vm.check()
    test_stub.migrate_vm_to_random_host(vm)
    vm.check()
    vm.destroy()
    test_util.test_pass('Migrate VM Test Success')
def test():
    if test_lib.lib_get_active_host_number() < 2:
        test_util.test_skip(
            'There is not 2 or more hosts to do migration test')

    vm = test_stub.create_vm(vm_name='test-vm-migration')
    test_obj_dict.add_vm(vm)
    vm.check()
    test_stub.migrate_vm_to_random_host(vm)
    vm.check()
    vm.destroy()
    test_util.test_pass('Migrate VM Test Success')
def test():
    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')

    vm = test_stub.create_vm(vm_name = 'migrate_stopped_vm_with_snapshot')
    host_uuid = vm.get_vm().hostUuid
    root_volume_uuid = test_lib.lib_get_root_volume_uuid(vm.get_vm())
    test_obj_dict.add_vm(vm)

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

    volume.attach(vm)
    volume.detach()

    test_util.test_dsc('create snapshot and check')
    snapshots = test_obj_dict.get_volume_snapshot(volume.get_volume().uuid)
    snapshots.set_utility_vm(vm)
    snapshots.create_snapshot('create_snapshot1')
    snapshots.check()

    vm.stop()
    conditions = res_ops.gen_query_conditions('uuid', '!=', host_uuid)
    conditions = res_ops.gen_query_conditions('state', '=', host_header.ENABLED, conditions)
    conditions = res_ops.gen_query_conditions('status', '=', host_header.CONNECTED, conditions)
    rest_hosts = res_ops.query_resource(res_ops.HOST, conditions)
    target_host = random.choice(rest_hosts)

    test_util.test_dsc('migrate vm and volumes')
    vol_ops.migrate_volume(root_volume_uuid, target_host.uuid)
    vol_ops.migrate_volume(volume.get_volume().uuid, target_host.uuid)
    
    vm.start()
    vm.check()
    snapshots.check()

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

    snapshots.use_snapshot(snapshot1)
    snapshots.create_snapshot('create_snapshot1.1.1')
    snapshots.check()

    snapshots.use_snapshot(snapshot1)
    snapshots.create_snapshot('create_snapshot1.2.1')
    snapshots.check()

    snapshots.delete()
    test_obj_dict.rm_volume_snapshot(snapshots)
    volume.check()
    volume.delete()

    test_obj_dict.rm_volume(volume)
    vm.destroy()
    test_util.test_pass('Create Snapshot with VM migration 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():
    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')