Debian Codegraph @

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

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2019 Davide Madrisan <[email protected]>
5 *
6 * Unit test for lib/perfdata.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 "common.h"
24 #include "perfdata.h"
25 #include "system.h"
26 #include "testutils.h"
27 #include "thresholds.h"
28 #include "units.h"
29
30 typedef struct test_data
31 {
32 char *warn_string;
33 char *critical_string;
34 unsigned long base;
35 int shift;
36 unsigned long long limit;
37 bool percent;
38 unsigned long long expect_value[2];
39 } test_data;
40
41 static int
42 test_get_perfdata_limit_converted (const void *tdata)
43 {
44 const struct test_data *data = tdata;
45 thresholds *my_threshold = NULL;
46 unsigned long long limit_warning, limit_critical;
47 int ret = 0, retw, retc, status;
48
49 status = set_thresholds (&my_threshold, data->warn_string,
50 data->critical_string);
51 if (status == NP_RANGE_UNPARSEABLE)
52 return -1;
53
54 retw =
55 get_perfdata_limit_converted (my_threshold->warning, data->base,
56 data->shift, &limit_warning, data->percent);
57 retc =
58 get_perfdata_limit_converted (my_threshold->critical, data->base,
59 data->shift, &limit_critical,
60 data->percent);
61 if (retw != 0 || retc != 0)
62 return -1;
63
64 TEST_ASSERT_EQUAL_NUMERIC (limit_warning, data->expect_value[0]);
65 retw = ret;
66 TEST_ASSERT_EQUAL_NUMERIC (limit_critical, data->expect_value[1]);
67 retc = ret;
68 if (retw != 0 || retc != 0)
69 ret = -1;
70
71 free (my_threshold);
72 return ret;
73 }
74
75 static int
76 mymain (void)
77 {
78 int ret = 0;
79
80 #define DO_TEST(WARN_STR, CRITICAL_STR, BASE, SHIFT, PERCENT,\
81 EXPECT_VALUE_W, EXPECT_VALUE_C) \
82 do \
83 { \
84 test_data data = { \
85 .warn_string = WARN_STR, \
86 .critical_string = CRITICAL_STR, \
87 .base = BASE, \
88 .shift = SHIFT, \
89 .percent = PERCENT, \
90 .expect_value[0] = EXPECT_VALUE_W, \
91 .expect_value[1] = EXPECT_VALUE_C, \
92 }; \
93 if (test_run( \
94 "check get_perfdata_limit_converted with -w " \
95 WARN_STR " -c " CRITICAL_STR " and " #SHIFT, \
96 test_get_perfdata_limit_converted, (&data)) < 0) \
97 ret = -1; \
98 } \
99 while (0)
100
101 DO_TEST ("20%:", "10%:", 16302692L, k_shift, true, 3260538L, 1630269L);
102 DO_TEST ("20%:", "10%:", 16302692L, m_shift, true, 3184L, 1592L);
103 DO_TEST ("20%:", "10%:", 16302692L, g_shift, true, 3L, 1L);
104
105 DO_TEST ("80%", "90%", 16302692L, k_shift, true, 13042153L, 14672422L);
106 DO_TEST ("80%", "90%", 16302692L, m_shift, true, 12736L, 14328L);
107 DO_TEST ("80%", "90%", 16302692L, g_shift, true, 12L, 13L);
108
109 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
110 }
111
112 TEST_MAIN (mymain);