Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/tests/tslibmeminfo_procparser.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2017 Davide Madrisan <[email protected]>
5 *
6 * Unit test for lib/meminfo.c
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23 #include "testutils.h"
24
25 #if defined (PROC_MEMINFO) && !defined (HAVE_LIBPROCPS)
26
27 # define NPL_TESTING
28 # include "../lib/meminfo.c"
29 # undef NPL_TESTING
30
31 proc_sysmem_t *sysmem = NULL;
32
33 static int
34 test_memory_init ()
35 {
36 int ret = 0;
37 const char *env_variable = "NPL_TEST_PATH_PROCMEMINFO";
38
39 ret = proc_sysmem_new (&sysmem);
40 if (ret < 0)
41 return EXIT_AM_HARDFAIL;
42
43 ret = setenv (env_variable, NPL_TEST_PATH_PROCMEMINFO, 1);
44 if (ret < 0)
45 return EXIT_AM_HARDFAIL;
46
47 proc_sysmem_read (sysmem);
48 unsetenv (env_variable);
49
50 return 0;
51 }
52
53 static void
54 test_memory_release ()
55 {
56 proc_sysmem_unref (sysmem);
57 }
58
59 typedef struct test_data
60 {
61 unsigned long value;
62 unsigned long expect_value;
63 } test_data;
64
65 static int
66 test_procmeminfo (const void *tdata)
67 {
68 const struct test_data *data = tdata;
69 int ret = 0;
70
71 TEST_ASSERT_EQUAL_NUMERIC (data->value, data->expect_value);
72 return ret;
73 }
74
75 static int
76 mymain (void)
77 {
78 int err, ret = 0;
79
80 if ((err = test_memory_init ()) != 0)
81 return err;
82
83 proc_sysmem_data_t *procdata = sysmem->data;
84
85 # define DO_TEST(MEMTYPE, VALUE, EXPECT_VALUE) \
86 do \
87 { \
88 test_data data = { \
89 .value = VALUE, \
90 .expect_value = EXPECT_VALUE, \
91 }; \
92 if (test_run("check " PROC_MEMINFO " parser for " MEMTYPE " memory", \
93 test_procmeminfo, (&data)) < 0) \
94 ret = -1; \
95 } \
96 while (0)
97
98 /* test the proc parser */
99
100 DO_TEST ("Active", procdata->kb_active, 3090692UL);
101 DO_TEST ("Active(file)", procdata->kb_active_file, 2021912UL);
102 DO_TEST ("AnonPages", procdata->kb_anon_pages, 1008780UL);
103 DO_TEST ("Buffers", procdata->kb_main_buffers, 384876UL);
104 DO_TEST ("Cached", procdata->kb_page_cache, 2741476UL);
105 DO_TEST ("Committed_AS", procdata->kb_committed_as, 3678828UL);
106 DO_TEST ("Dirty", procdata->kb_dirty, 92UL);
107 DO_TEST ("HighTotal", procdata->kb_high_total, 0UL);
108 DO_TEST ("Inact_clean", procdata->kb_inact_clean, 0UL);
109 DO_TEST ("Inact_dirty", procdata->kb_inact_dirty, 0UL);
110 DO_TEST ("Inact_laundry", procdata->kb_inact_laundry, 0UL);
111 DO_TEST ("Inactive", procdata->kb_inactive, 1044404UL);
112 DO_TEST ("Inactive(file)", procdata->kb_inactive_file, 716976UL);
113 DO_TEST ("LowFree", procdata->kb_low_free, 0UL);
114 DO_TEST ("LowTotal", procdata->kb_low_total, MEMINFO_UNSET);
115 DO_TEST ("MemAvailable", procdata->kb_main_available, 14564424UL);
116 DO_TEST ("MemFree", procdata->kb_main_free, 11918208UL);
117 DO_TEST ("MemTotal", procdata->kb_main_total, 16384256UL);
118 DO_TEST ("SReclaimable", procdata->kb_slab_reclaimable, 152528UL);
119 DO_TEST ("Shmem", procdata->kb_main_shared, 387476UL);
120 DO_TEST ("Slab", procdata->kb_slab, 202816UL);
121 DO_TEST ("SwapCached", procdata->kb_swap_cached, 1024UL);
122 DO_TEST ("SwapFree", procdata->kb_swap_free, 8387580UL);
123 DO_TEST ("SwapTotal", procdata->kb_swap_total, 8388604UL);
124
125 test_memory_release ();
126
127 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
128 }
129
130 TEST_MAIN (mymain)
131
132 #else
133
134 int
135 main (void)
136 {
137 return EXIT_AM_SKIP;
138 }
139
140 #endif