import os
from autotest.client import utils
from autotest.client.shared import error


def run_unittest_kvmctl(test, params, env):
    """
    This is kvm userspace unit test, use kvm test harness kvmctl load binary
    test case file to test various functions of the kvm kernel module.
    The output of all unit tests can be found in the test result dir.

    @param test: QEMU test object.
    @param params: Dictionary with the test parameters.
    @param env: Dictionary with test environment.
    """
    case = params["case"]
    srcdir = params.get("srcdir", test.srcdir)
    unit_dir = os.path.join(srcdir, "kvm_userspace", "kvm", "user")
    if not os.path.isdir(unit_dir):
        os.makedirs(unit_dir)
    os.chdir(unit_dir)

    cmd = "./kvmctl test/x86/bootstrap test/x86/%s.flat" % case
    try:
        results = utils.system_output(cmd)
    except error.CmdError:
        raise error.TestFail("Unit test %s failed" % case)

    result_file = os.path.join(test.resultsdir, case)
    utils.open_write_close(result_file, results)
