@@ -804,6 +804,10 @@ tests += [
|
||||
[['src/test/test-local-addresses.c'],
|
||||
[],
|
||||
[]],
|
||||
|
||||
[['src/test/test-psi-util.c'],
|
||||
[],
|
||||
[]],
|
||||
]
|
||||
|
||||
############################################################
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
|
||||
#include <errno.h>
|
||||
#include <linux/loadavg.h>
|
||||
#include <locale.h>
|
||||
#include <math.h>
|
||||
#include <sys/socket.h>
|
||||
@@ -929,6 +930,42 @@ static void test_parse_mtu(void) {
|
||||
assert_se(parse_mtu(AF_UNSPEC, "", &mtu) == -EINVAL);
|
||||
}
|
||||
|
||||
static void test_parse_loadavg_fixed_point(void) {
|
||||
loadavg_t fp;
|
||||
|
||||
assert_se(parse_loadavg_fixed_point("1.23", &fp) == 0);
|
||||
assert_se(LOAD_INT(fp) == 1);
|
||||
assert_se(LOAD_FRAC(fp) == 23);
|
||||
|
||||
assert_se(parse_loadavg_fixed_point("1.80", &fp) == 0);
|
||||
assert_se(LOAD_INT(fp) == 1);
|
||||
assert_se(LOAD_FRAC(fp) == 80);
|
||||
|
||||
assert_se(parse_loadavg_fixed_point("0.07", &fp) == 0);
|
||||
assert_se(LOAD_INT(fp) == 0);
|
||||
assert_se(LOAD_FRAC(fp) == 7);
|
||||
|
||||
assert_se(parse_loadavg_fixed_point("0.00", &fp) == 0);
|
||||
assert_se(LOAD_INT(fp) == 0);
|
||||
assert_se(LOAD_FRAC(fp) == 0);
|
||||
|
||||
assert_se(parse_loadavg_fixed_point("4096.57", &fp) == 0);
|
||||
assert_se(LOAD_INT(fp) == 4096);
|
||||
assert_se(LOAD_FRAC(fp) == 57);
|
||||
|
||||
/* Caps out at 2 digit fracs */
|
||||
assert_se(parse_loadavg_fixed_point("1.100", &fp) == -ERANGE);
|
||||
|
||||
assert_se(parse_loadavg_fixed_point("4096.4096", &fp) == -ERANGE);
|
||||
assert_se(parse_loadavg_fixed_point("-4000.5", &fp) == -ERANGE);
|
||||
assert_se(parse_loadavg_fixed_point("18446744073709551615.5", &fp) == -ERANGE);
|
||||
assert_se(parse_loadavg_fixed_point("foobar", &fp) == -EINVAL);
|
||||
assert_se(parse_loadavg_fixed_point("3333", &fp) == -EINVAL);
|
||||
assert_se(parse_loadavg_fixed_point("1.2.3", &fp) == -EINVAL);
|
||||
assert_se(parse_loadavg_fixed_point(".", &fp) == -EINVAL);
|
||||
assert_se(parse_loadavg_fixed_point("", &fp) == -EINVAL);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
log_parse_environment();
|
||||
log_open();
|
||||
@@ -955,6 +992,7 @@ int main(int argc, char *argv[]) {
|
||||
test_parse_errno();
|
||||
test_parse_syscall_and_errno();
|
||||
test_parse_mtu();
|
||||
test_parse_loadavg_fixed_point();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
|
||||
#include <linux/loadavg.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "fileio.h"
|
||||
#include "fs-util.h"
|
||||
#include "psi-util.h"
|
||||
#include "tests.h"
|
||||
|
||||
static void test_read_mem_pressure(void) {
|
||||
_cleanup_(unlink_tempfilep) char path[] = "/tmp/pressurereadtestXXXXXX";
|
||||
ResourcePressure rp;
|
||||
|
||||
if (geteuid() != 0)
|
||||
return (void) log_tests_skipped("not root");
|
||||
|
||||
assert_se(mkstemp(path));
|
||||
|
||||
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(LOAD_INT(rp.avg10) == 0);
|
||||
assert_se(LOAD_FRAC(rp.avg10) == 22);
|
||||
assert_se(LOAD_INT(rp.avg60) == 0);
|
||||
assert_se(LOAD_FRAC(rp.avg60) == 17);
|
||||
assert_se(LOAD_INT(rp.avg300) == 1);
|
||||
assert_se(LOAD_FRAC(rp.avg300) == 11);
|
||||
assert_se(rp.total == 58761459);
|
||||
assert(read_resource_pressure(path, PRESSURE_TYPE_FULL, &rp) == 0);
|
||||
assert_se(LOAD_INT(rp.avg10) == 0);
|
||||
assert_se(LOAD_FRAC(rp.avg10) == 23);
|
||||
assert_se(LOAD_INT(rp.avg60) == 0);
|
||||
assert_se(LOAD_FRAC(rp.avg60) == 16);
|
||||
assert_se(LOAD_INT(rp.avg300) == 1);
|
||||
assert_se(LOAD_FRAC(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(LOAD_INT(rp.avg10) == 0);
|
||||
assert_se(LOAD_FRAC(rp.avg10) == 22);
|
||||
assert_se(LOAD_INT(rp.avg60) == 0);
|
||||
assert_se(LOAD_FRAC(rp.avg60) == 17);
|
||||
assert_se(LOAD_INT(rp.avg300) == 1);
|
||||
assert_se(LOAD_FRAC(rp.avg300) == 11);
|
||||
assert_se(rp.total == 58761459);
|
||||
assert(read_resource_pressure(path, PRESSURE_TYPE_FULL, &rp) == 0);
|
||||
assert_se(LOAD_INT(rp.avg10) == 0);
|
||||
assert_se(LOAD_FRAC(rp.avg10) == 23);
|
||||
assert_se(LOAD_INT(rp.avg60) == 0);
|
||||
assert_se(LOAD_FRAC(rp.avg60) == 16);
|
||||
assert_se(LOAD_INT(rp.avg300) == 1);
|
||||
assert_se(LOAD_FRAC(rp.avg300) == 8);
|
||||
assert_se(rp.total == 58464525);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
test_setup_logging(LOG_DEBUG);
|
||||
test_read_mem_pressure();
|
||||
return 0;
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "architecture.h"
|
||||
#include "automount.h"
|
||||
#include "cgroup.h"
|
||||
#include "cgroup-util.h"
|
||||
#include "compress.h"
|
||||
#include "condition.h"
|
||||
#include "device-private.h"
|
||||
@@ -71,6 +72,7 @@ int main(int argc, char **argv) {
|
||||
test_table(locale_variable, VARIABLE_LC);
|
||||
test_table(log_target, LOG_TARGET);
|
||||
test_table(mac_address_policy, MAC_ADDRESS_POLICY);
|
||||
test_table(managed_oom_mode, MANAGED_OOM_MODE);
|
||||
test_table(manager_state, MANAGER_STATE);
|
||||
test_table(manager_timestamp, MANAGER_TIMESTAMP);
|
||||
test_table(mount_exec_command, MOUNT_EXEC_COMMAND);
|
||||
|
||||
Reference in New Issue
Block a user