summaryrefslogtreecommitdiffstats
path: root/src/test/test-psi-util.c
blob: 4ce0811a8d9cdd73a163a53f549264436a6d1b89 (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
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
#include "parse-util.h"
#include "psi-util.h"
#include "tests.h"
#include "tmpfile-util.h"

TEST(read_mem_pressure) {
        _cleanup_(unlink_tempfilep) char path[] = "/tmp/pressurereadtestXXXXXX";
        _cleanup_close_ int fd = -EBADF;
        ResourcePressure rp;

        if (geteuid() != 0)
                return (void) log_tests_skipped("not root");

        assert_se((fd = mkostemp_safe(path)) >= 0);

        assert_se(read_resource_pressure("/verylikelynonexistentpath", PRESSURE_TYPE_SOME, &rp) < 0);
        assert_se(read_resource_pressure(path, PRESSURE_TYPE_SOME, &rp) < 0);

        assert_se(write_string_file(path, "herpdederp\n", WRITE_STRING_FILE_CREATE) == 0);
        assert_se(read_resource_pressure(path, PRESSURE_TYPE_SOME, &rp) < 0);

        /* Pressure file with some invalid values */
        assert_se(write_string_file(path, "some avg10=0.22=55 avg60=0.17=8 avg300=1.11=00 total=58761459\n"
                                          "full avg10=0.23=55 avg60=0.16=8 avg300=1.08=00 total=58464525", WRITE_STRING_FILE_CREATE) == 0);
        assert_se(read_resource_pressure(path, PRESSURE_TYPE_SOME, &rp) < 0);

        /* Same pressure valid values as below but with duplicate avg60 field */
        assert_se(write_string_file(path, "some avg10=0.22 avg60=0.17 avg60=0.18 avg300=1.11 total=58761459\n"
                                          "full avg10=0.23 avg60=0.16 avg300=1.08 total=58464525", WRITE_STRING_FILE_CREATE) == 0);
        assert_se(read_resource_pressure(path, PRESSURE_TYPE_SOME, &rp) < 0);

        assert_se(write_string_file(path, "some avg10=0.22 avg60=0.17 avg300=1.11 total=58761459\n"
                                          "full avg10=0.23 avg60=0.16 avg300=1.08 total=58464525", WRITE_STRING_FILE_CREATE) == 0);
        assert_se(read_resource_pressure(path, PRESSURE_TYPE_SOME, &rp) == 0);
        assert_se(LOADAVG_INT_SIDE(rp.avg10) == 0);
        assert_se(LOADAVG_DECIMAL_SIDE(rp.avg10) == 22);
        assert_se(LOADAVG_INT_SIDE(rp.avg60) == 0);
        assert_se(LOADAVG_DECIMAL_SIDE(rp.avg60) == 17);
        assert_se(LOADAVG_INT_SIDE(rp.avg300) == 1);
        assert_se(LOADAVG_DECIMAL_SIDE(rp.avg300) == 11);
        assert_se(rp.total == 58761459);
        assert_se(read_resource_pressure(path, PRESSURE_TYPE_FULL, &rp) == 0);
        assert_se(LOADAVG_INT_SIDE(rp.avg10) == 0);
        assert_se(LOADAVG_DECIMAL_SIDE(rp.avg10) == 23);
        assert_se(LOADAVG_INT_SIDE(rp.avg60) == 0);
        assert_se(LOADAVG_DECIMAL_SIDE(rp.avg60) == 16);
        assert_se(LOADAVG_INT_SIDE(rp.avg300) == 1);
        assert_se(LOADAVG_DECIMAL_SIDE(rp.avg300) == 8);
        assert_se(rp.total == 58464525);

        /* Pressure file with extra unsupported fields */
        assert_se(write_string_file(path, "some avg5=0.55 avg10=0.22 avg60=0.17 avg300=1.11 total=58761459\n"
                                          "full avg10=0.23 avg60=0.16 avg300=1.08 avg600=2.00 total=58464525", WRITE_STRING_FILE_CREATE) == 0);
        assert_se(read_resource_pressure(path, PRESSURE_TYPE_SOME, &rp) == 0);
        assert_se(LOADAVG_INT_SIDE(rp.avg10) == 0);
        assert_se(LOADAVG_DECIMAL_SIDE(rp.avg10) == 22);
        assert_se(LOADAVG_INT_SIDE(rp.avg60) == 0);
        assert_se(LOADAVG_DECIMAL_SIDE(rp.avg60) == 17);
        assert_se(LOADAVG_INT_SIDE(rp.avg300) == 1);
        assert_se(LOADAVG_DECIMAL_SIDE(rp.avg300) == 11);
        assert_se(rp.total == 58761459);
        assert_se(read_resource_pressure(path, PRESSURE_TYPE_FULL, &rp) == 0);
        assert_se(LOADAVG_INT_SIDE(rp.avg10) == 0);
        assert_se(LOADAVG_DECIMAL_SIDE(rp.avg10) == 23);
        assert_se(LOADAVG_INT_SIDE(rp.avg60) == 0);
        assert_se(LOADAVG_DECIMAL_SIDE(rp.avg60) == 16);
        assert_se(LOADAVG_INT_SIDE(rp.avg300) == 1);
        assert_se(LOADAVG_DECIMAL_SIDE(rp.avg300) == 8);
        assert_se(rp.total == 58464525);
}

DEFINE_TEST_MAIN(LOG_DEBUG);