nodeinfotest.c 3.6 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
#if ! (defined __linux__  &&  (defined(__x86_64__) || \
                               defined(__amd64__)  || \
16 17 18
                               defined(__i386__)  || \
                               defined(__powerpc__)  || \
                               defined(__powerpc64__)))
19

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

#else

28
extern int linuxNodeInfoCPUPopulate(FILE *cpuinfo,
29
                                    char *sysfs_dir,
30
                                    virNodeInfoPtr nodeinfo);
31

32
static int
33
linuxTestCompareFiles(const char *cpuinfofile,
34
                      char *sysfs_dir,
35
                      const char *outputfile)
36 37 38 39
{
    int ret = -1;
    char *actualData = NULL;
    char *expectData = NULL;
40
    virNodeInfo nodeinfo;
41
    FILE *cpuinfo;
42

43 44
    if (virtTestLoadFile(outputfile, &expectData) < 0)
        goto fail;
45 46 47

    cpuinfo = fopen(cpuinfofile, "r");
    if (!cpuinfo)
48
        goto fail;
49 50

    memset(&nodeinfo, 0, sizeof(nodeinfo));
51
    if (linuxNodeInfoCPUPopulate(cpuinfo, sysfs_dir, &nodeinfo) < 0) {
52 53 54 55 56 57
        if (virTestGetDebug()) {
            virErrorPtr error = virSaveLastError();
            if (error && error->code != VIR_ERR_OK)
                fprintf(stderr, "\n%s\n", error->message);
            virFreeError(error);
        }
58
        VIR_FORCE_FCLOSE(cpuinfo);
59
        goto fail;
60
    }
61
    VIR_FORCE_FCLOSE(cpuinfo);
62

E
Eric Blake 已提交
63 64 65 66 67 68
    if (virAsprintf(&actualData,
                    "CPUs: %u/%u, MHz: %u, Nodes: %u, Sockets: %u, "
                    "Cores: %u, Threads: %u\n",
                    nodeinfo.cpus, VIR_NODEINFO_MAXCPUS(nodeinfo),
                    nodeinfo.mhz, nodeinfo.nodes, nodeinfo.sockets,
                    nodeinfo.cores, nodeinfo.threads) < 0)
69
        goto fail;
70 71

    if (STRNEQ(actualData, expectData)) {
72
        virtTestDifference(stderr, expectData, actualData);
73
        goto fail;
74 75
    }

76 77 78
    ret = 0;

fail:
79 80
    VIR_FREE(expectData);
    VIR_FREE(actualData);
81
    return ret;
82 83 84
}


85 86 87 88 89
static int
linuxTestNodeInfo(const void *data)
{
    int result = -1;
    char *cpuinfo = NULL;
90
    char *sysfs_dir = NULL;
91
    char *output = NULL;
92 93
    const char *test = data;
    const char *arch = "x86";
94

95 96
# if defined(__powerpc__) || \
     defined(__powerpc64__)
97
    arch = "ppc";
98
# endif
99 100 101 102 103 104 105

    if (virAsprintf(&sysfs_dir, "%s/nodeinfodata/linux-%s",
                    abs_srcdir, test) < 0 ||
        virAsprintf(&cpuinfo, "%s/nodeinfodata/linux-%s-%s.cpuinfo",
                    abs_srcdir, test, arch) < 0 ||
        virAsprintf(&output, "%s/nodeinfodata/linux-%s-cpu-%s-output.txt",
                    abs_srcdir, test, arch) < 0) {
106 107 108
        goto cleanup;
    }

109
    result = linuxTestCompareFiles(cpuinfo, sysfs_dir, output);
110 111

cleanup:
112 113
    VIR_FREE(cpuinfo);
    VIR_FREE(output);
114
    VIR_FREE(sysfs_dir);
115 116

    return result;
117 118 119
}


120
static int
E
Eric Blake 已提交
121
mymain(void)
122 123 124 125
{
    int ret = 0;
    int i;
    const char *nodeData[] = {
126
        "nodeinfo-sysfs-test-1",
E
Eric Blake 已提交
127 128 129 130
# if !(defined(__powerpc__) ||                  \
       defined(__powerpc64__))
        "nodeinfo-sysfs-test-2",
        "nodeinfo-sysfs-test-3",
131
        "nodeinfo-sysfs-test-4",
132
        "nodeinfo-sysfs-test-5",
E
Eric Blake 已提交
133
# endif
134 135
    };

136 137
    if (virInitialize() < 0)
        return EXIT_FAILURE;
138

J
Jim Meyering 已提交
139
    for (i = 0 ; i < ARRAY_CARDINALITY(nodeData); i++)
140
      if (virtTestRun(nodeData[i], 1, linuxTestNodeInfo, nodeData[i]) != 0)
141
        ret = -1;
142

143
    return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
144
}
145 146

VIRT_TEST_MAIN(mymain)
147 148

#endif /* __linux__ */