1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "fd-util.h"
5 #include "fileio.h"
6 #include "fs-util.h"
7 #include "parse-util.h"
8 #include "psi-util.h"
9 #include "tests.h"
10 #include "tmpfile-util.h"
11
TEST(read_mem_pressure)12 TEST(read_mem_pressure) {
13 _cleanup_(unlink_tempfilep) char path[] = "/tmp/pressurereadtestXXXXXX";
14 _cleanup_close_ int fd = -1;
15 ResourcePressure rp;
16
17 if (geteuid() != 0)
18 return (void) log_tests_skipped("not root");
19
20 assert_se((fd = mkostemp_safe(path)) >= 0);
21
22 assert_se(read_resource_pressure("/verylikelynonexistentpath", PRESSURE_TYPE_SOME, &rp) < 0);
23 assert_se(read_resource_pressure(path, PRESSURE_TYPE_SOME, &rp) < 0);
24
25 assert_se(write_string_file(path, "herpdederp\n", WRITE_STRING_FILE_CREATE) == 0);
26 assert_se(read_resource_pressure(path, PRESSURE_TYPE_SOME, &rp) < 0);
27
28 /* Pressure file with some invalid values */
29 assert_se(write_string_file(path, "some avg10=0.22=55 avg60=0.17=8 avg300=1.11=00 total=58761459\n"
30 "full avg10=0.23=55 avg60=0.16=8 avg300=1.08=00 total=58464525", WRITE_STRING_FILE_CREATE) == 0);
31 assert_se(read_resource_pressure(path, PRESSURE_TYPE_SOME, &rp) < 0);
32
33 /* Same pressure valid values as below but with duplicate avg60 field */
34 assert_se(write_string_file(path, "some avg10=0.22 avg60=0.17 avg60=0.18 avg300=1.11 total=58761459\n"
35 "full avg10=0.23 avg60=0.16 avg300=1.08 total=58464525", WRITE_STRING_FILE_CREATE) == 0);
36 assert_se(read_resource_pressure(path, PRESSURE_TYPE_SOME, &rp) < 0);
37
38 assert_se(write_string_file(path, "some avg10=0.22 avg60=0.17 avg300=1.11 total=58761459\n"
39 "full avg10=0.23 avg60=0.16 avg300=1.08 total=58464525", WRITE_STRING_FILE_CREATE) == 0);
40 assert_se(read_resource_pressure(path, PRESSURE_TYPE_SOME, &rp) == 0);
41 assert_se(LOADAVG_INT_SIDE(rp.avg10) == 0);
42 assert_se(LOADAVG_DECIMAL_SIDE(rp.avg10) == 22);
43 assert_se(LOADAVG_INT_SIDE(rp.avg60) == 0);
44 assert_se(LOADAVG_DECIMAL_SIDE(rp.avg60) == 17);
45 assert_se(LOADAVG_INT_SIDE(rp.avg300) == 1);
46 assert_se(LOADAVG_DECIMAL_SIDE(rp.avg300) == 11);
47 assert_se(rp.total == 58761459);
48 assert_se(read_resource_pressure(path, PRESSURE_TYPE_FULL, &rp) == 0);
49 assert_se(LOADAVG_INT_SIDE(rp.avg10) == 0);
50 assert_se(LOADAVG_DECIMAL_SIDE(rp.avg10) == 23);
51 assert_se(LOADAVG_INT_SIDE(rp.avg60) == 0);
52 assert_se(LOADAVG_DECIMAL_SIDE(rp.avg60) == 16);
53 assert_se(LOADAVG_INT_SIDE(rp.avg300) == 1);
54 assert_se(LOADAVG_DECIMAL_SIDE(rp.avg300) == 8);
55 assert_se(rp.total == 58464525);
56
57 /* Pressure file with extra unsupported fields */
58 assert_se(write_string_file(path, "some avg5=0.55 avg10=0.22 avg60=0.17 avg300=1.11 total=58761459\n"
59 "full avg10=0.23 avg60=0.16 avg300=1.08 avg600=2.00 total=58464525", WRITE_STRING_FILE_CREATE) == 0);
60 assert_se(read_resource_pressure(path, PRESSURE_TYPE_SOME, &rp) == 0);
61 assert_se(LOADAVG_INT_SIDE(rp.avg10) == 0);
62 assert_se(LOADAVG_DECIMAL_SIDE(rp.avg10) == 22);
63 assert_se(LOADAVG_INT_SIDE(rp.avg60) == 0);
64 assert_se(LOADAVG_DECIMAL_SIDE(rp.avg60) == 17);
65 assert_se(LOADAVG_INT_SIDE(rp.avg300) == 1);
66 assert_se(LOADAVG_DECIMAL_SIDE(rp.avg300) == 11);
67 assert_se(rp.total == 58761459);
68 assert_se(read_resource_pressure(path, PRESSURE_TYPE_FULL, &rp) == 0);
69 assert_se(LOADAVG_INT_SIDE(rp.avg10) == 0);
70 assert_se(LOADAVG_DECIMAL_SIDE(rp.avg10) == 23);
71 assert_se(LOADAVG_INT_SIDE(rp.avg60) == 0);
72 assert_se(LOADAVG_DECIMAL_SIDE(rp.avg60) == 16);
73 assert_se(LOADAVG_INT_SIDE(rp.avg300) == 1);
74 assert_se(LOADAVG_DECIMAL_SIDE(rp.avg300) == 8);
75 assert_se(rp.total == 58464525);
76 }
77
78 DEFINE_TEST_MAIN(LOG_DEBUG);
79