nodeinfotest.c 3.3 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

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

    if (STRNEQ(actualData, expectData)) {
69
        virtTestDifference(stderr, expectData, actualData);
70
        goto fail;
71 72
    }

73 74 75
    ret = 0;

fail:
76 77
    VIR_FREE(expectData);
    VIR_FREE(actualData);
78
    return ret;
79 80 81
}


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

92 93
# if defined(__powerpc__) || \
     defined(__powerpc64__)
94
    arch = "ppc";
95
# endif
96 97 98 99 100 101 102

    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) {
103 104 105
        goto cleanup;
    }

106
    result = linuxTestCompareFiles(cpuinfo, sysfs_dir, output);
107 108

cleanup:
109 110
    VIR_FREE(cpuinfo);
    VIR_FREE(output);
111
    VIR_FREE(sysfs_dir);
112 113

    return result;
114 115 116
}


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

131 132
    if (virInitialize() < 0)
        return EXIT_FAILURE;
133

J
Jim Meyering 已提交
134
    for (i = 0 ; i < ARRAY_CARDINALITY(nodeData); i++)
135
      if (virtTestRun(nodeData[i], 1, linuxTestNodeInfo, nodeData[i]) != 0)
136
        ret = -1;
137

138
    return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
139
}
140 141

VIRT_TEST_MAIN(mymain)
142 143

#endif /* __linux__ */