nodeinfotest.c 3.1 KB
Newer Older
1
#include <config.h>
2

3 4 5
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
6
#include <unistd.h>
7 8 9 10

#include "testutils.h"
#include "internal.h"
#include "nodeinfo.h"
J
Jim Meyering 已提交
11
#include "util.h"
E
Eric Blake 已提交
12
#include "virfile.h"
13

14 15 16
#if ! (defined __linux__  &&  (defined(__x86_64__) || \
                               defined(__amd64__)  || \
                               defined(__i386__)))
17

18 19
int
main(void)
20
{
21
    return EXIT_AM_SKIP;
22 23 24 25
}

#else

26 27
extern int linuxNodeInfoCPUPopulate(FILE *cpuinfo, virNodeInfoPtr nodeinfo,
                                    bool need_hyperthreads);
28

29 30 31 32 33 34
static int
linuxTestCompareFiles(const char *cpuinfofile, const char *outputfile)
{
    int ret = -1;
    char *actualData = NULL;
    char *expectData = NULL;
35
    virNodeInfo nodeinfo;
36
    FILE *cpuinfo;
37

38 39
    if (virtTestLoadFile(outputfile, &expectData) < 0)
        goto fail;
40 41 42

    cpuinfo = fopen(cpuinfofile, "r");
    if (!cpuinfo)
43
        goto fail;
44 45

    memset(&nodeinfo, 0, sizeof(nodeinfo));
46
    if (linuxNodeInfoCPUPopulate(cpuinfo, &nodeinfo, false) < 0) {
47 48 49 50 51 52
        if (virTestGetDebug()) {
            virErrorPtr error = virSaveLastError();
            if (error && error->code != VIR_ERR_OK)
                fprintf(stderr, "\n%s\n", error->message);
            virFreeError(error);
        }
53
        VIR_FORCE_FCLOSE(cpuinfo);
54
        goto fail;
55
    }
56
    VIR_FORCE_FCLOSE(cpuinfo);
57

58 59 60 61 62
    /* 'nodes' is filled using libnuma.so from current machine
     * topology, which makes it unsuitable for the test suite
     * so blank it to a predictable value */
    nodeinfo.nodes = 1;

63 64 65 66
    if (virAsprintf(&actualData, "CPUs: %u, MHz: %u, Nodes: %u, Cores: %u\n",
                    nodeinfo.cpus, nodeinfo.mhz, nodeinfo.nodes,
                    nodeinfo.cores) < 0)
        goto fail;
67 68 69 70 71 72

    if (STRNEQ(actualData, expectData)) {
        if (getenv("DEBUG_TESTS")) {
            printf("Expect %d '%s'\n", (int)strlen(expectData), expectData);
            printf("Actual %d '%s'\n", (int)strlen(actualData), actualData);
        }
73
        goto fail;
74 75
    }

76 77 78 79 80 81
    ret = 0;

fail:
    free(expectData);
    free(actualData);
    return ret;
82 83 84
}


85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
static int
linuxTestNodeInfo(const void *data)
{
    int result = -1;
    char *cpuinfo = NULL;
    char *output = NULL;

    if (virAsprintf(&cpuinfo, "%s/nodeinfodata/linux-%s.cpuinfo",
                    abs_srcdir, (const char*)data) < 0 ||
        virAsprintf(&output, "%s/nodeinfodata/linux-%s.txt",
                    abs_srcdir, (const char*)data) < 0) {
        goto cleanup;
    }

    result = linuxTestCompareFiles(cpuinfo, output);

cleanup:
    free(cpuinfo);
    free(output);

    return result;
106 107 108
}


109
static int
E
Eric Blake 已提交
110
mymain(void)
111 112 113 114 115 116 117 118 119 120 121 122
{
    int ret = 0;
    int i;
    const char *nodeData[] = {
        "nodeinfo-1",
        "nodeinfo-2",
        "nodeinfo-3",
        "nodeinfo-4",
        "nodeinfo-5",
        "nodeinfo-6",
    };

123 124
    if (virInitialize() < 0)
        return EXIT_FAILURE;
125

J
Jim Meyering 已提交
126
    for (i = 0 ; i < ARRAY_CARDINALITY(nodeData); i++)
127
      if (virtTestRun(nodeData[i], 1, linuxTestNodeInfo, nodeData[i]) != 0)
128
        ret = -1;
129

130
    return(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
131
}
132 133

VIRT_TEST_MAIN(mymain)
134 135

#endif /* __linux__ */