virresctrltest.c 2.5 KB
Newer Older
M
Martin Kletzander 已提交
1 2 3 4
#include <config.h>

#include "testutils.h"
#include "virfilewrapper.h"
5
#define LIBVIRT_VIRRESCTRLPRIV_H_ALLOW
M
Martin Kletzander 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include "virresctrlpriv.h"


#define VIR_FROM_THIS VIR_FROM_NONE

struct virResctrlData {
    const char *filename;
    bool fail;
};


static int
test_virResctrlGetUnused(const void *opaque)
{
    struct virResctrlData *data = (struct virResctrlData *) opaque;
    char *system_dir = NULL;
    char *resctrl_dir = NULL;
    int ret = -1;
    virResctrlAllocPtr alloc = NULL;
    char *schemata_str = NULL;
    char *schemata_file;
    virCapsPtr caps = NULL;

    if (virAsprintf(&system_dir, "%s/vircaps2xmldata/linux-%s/system",
                    abs_srcdir, data->filename) < 0)
        goto cleanup;

    if (virAsprintf(&resctrl_dir, "%s/vircaps2xmldata/linux-%s/resctrl",
                    abs_srcdir, data->filename) < 0)
        goto cleanup;

    if (virAsprintf(&schemata_file, "%s/virresctrldata/%s.schemata",
                    abs_srcdir, data->filename) < 0)
        goto cleanup;

    virFileWrapperAddPrefix("/sys/devices/system", system_dir);
    virFileWrapperAddPrefix("/sys/fs/resctrl", resctrl_dir);

    caps = virCapabilitiesNew(VIR_ARCH_X86_64, false, false);
    if (!caps || virCapabilitiesInitCaches(caps) < 0) {
        fprintf(stderr, "Could not initialize capabilities");
        goto cleanup;
    }

    alloc = virResctrlAllocGetUnused(caps->host.resctrl);

    virFileWrapperClearPrefixes();

    if (!alloc) {
        if (data->fail)
            ret = 0;
        goto cleanup;
    } else if (data->fail) {
59
        VIR_TEST_DEBUG("Error expected but there wasn't any.");
M
Martin Kletzander 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
        ret = -1;
        goto cleanup;
    }

    schemata_str = virResctrlAllocFormat(alloc);

    if (virTestCompareToFile(schemata_str, schemata_file) < 0)
        goto cleanup;

    ret = 0;
 cleanup:
    virObjectUnref(caps);
    virObjectUnref(alloc);
    VIR_FREE(system_dir);
    VIR_FREE(resctrl_dir);
    VIR_FREE(schemata_str);
    VIR_FREE(schemata_file);
    return ret;
}


static int
mymain(void)
{
    struct virResctrlData data = {0};
    int ret = 0;

#define DO_TEST_UNUSED(_filename) \
    do { \
        data = (struct virResctrlData) { .filename = _filename }; \
        if (virTestRun("Free: " _filename, test_virResctrlGetUnused, &data) < 0) \
            ret = -1; \
    } while (0)

    DO_TEST_UNUSED("resctrl");
    DO_TEST_UNUSED("resctrl-cdp");
    DO_TEST_UNUSED("resctrl-skx");
    DO_TEST_UNUSED("resctrl-skx-twocaches");

    return ret;
}

VIR_TEST_MAIN(mymain)