summaryrefslogtreecommitdiff
path: root/tests/virprocessstattest.c
blob: 8fb82c48ef3333d5085a0e7dd0b1970439343752 (plain)
1
2
3
4
5
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
59
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
#include <config.h>

#include "testutils.h"
#include "virfilewrapper.h"
#include "virprocess.h"


struct testData {
    const char *filename;
    const char *command;
    size_t count;
    bool self;
};


static int
test_virProcessGetStat(const void *opaque)
{
    struct testData *data = (struct testData *) opaque;
    g_autofree char *data_dir = NULL;
    g_auto(GStrv) proc_stat = NULL;
    size_t len = 0;
    id_t id = data->self ? 0 : -1;
    const char *command = NULL;

    data_dir = g_strdup_printf("%s/virprocessstatdata/%s/",
                               abs_srcdir, data->filename);

    /* We are using predictable id of -1 because this case we will clearly see
     * that the test failed in case of virFileWrapper failure */
    if (id)
        virFileWrapperAddPrefix("/proc/-1/task/-1/", data_dir);
    else
        virFileWrapperAddPrefix("/proc/self/", data_dir);

    proc_stat = virProcessGetStat(id, id);

    virFileWrapperClearPrefixes();

    if (!proc_stat) {
        fprintf(stderr, "Could not get process stats\n");
        return -1;
    }

    len = g_strv_length(proc_stat);
    if (data->count != len) {
        fprintf(stderr, "Count incorrect, expected %zu, got %zu\n",
                data->count, len);
        return -1;
    }

    command = proc_stat[VIR_PROCESS_STAT_COMM];
    if (!STREQ_NULLABLE(data->command, command)) {
        fprintf(stderr, "Command incorrect, expected %s, got %s\n",
                data->command, command);
        return -1;
    }

    return 0;
}


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

#define DO_TEST(_filename, _command, _count, _self) \
    do { \
        data = (struct testData){ \
            .filename = _filename, \
            .command = _command, \
            .count = _count, \
            .self = _self, \
        }; \
        if (virTestRun("Reading process stat: " _filename, \
                       test_virProcessGetStat, &data) < 0) \
            ret = -1; \
    } while (0)

    DO_TEST("simple", "command", 5, true);
    DO_TEST("complex", "this) is ( a \t weird )\n)( (command ( ", 100, false);

    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

VIR_TEST_MAIN(mymain)