Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/include/testutils.h

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /* testutils.h -- a simple framework for unit testing
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
17 #ifndef _TESTUTILS_H
18 #define _TESTUTILS_H
19
20 #include <stddef.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "getenv.h"
27 #include "string-macros.h"
28
29 #define EXIT_AM_SKIP 77 /* tell Automake we're skipping a test */
30 #define EXIT_AM_HARDFAIL 99 /* tell Automake that the framework is broken */
31
32 #define TEST_KERNEL_VERSION_MAJOR 2
33 #define TEST_KERNEL_VERSION_MINOR 6
34 #define TEST_KERNEL_VERSION_PATCH 27
35 #define TEST_KERNEL_VERSION "2.6.27"
36
37 /* environment variables to allow testing via static proc and sysfs files;
38 note that 'abs_srcdir' seems the only way to make 'make distcheck' happy
39 (we cannot use 'abs_builddir' because the *.data files are not copied to
40 <pckrootdir>/nagios-plugins-linux-<version>/_build/sub/tests/) */
41 #define NPL_TEST_PATH_PROCSTAT abs_srcdir "/ts_procstat.data"
42 #define NPL_TEST_PATH_PROCMEMINFO abs_srcdir "/ts_procmeminfo.data"
43 #define NPL_TEST_PATH_PROCVMSTAT abs_srcdir "/ts_procvmstat.data"
44 #define NPL_TEST_PATH_PROCPRESSURE_CPU abs_srcdir "/ts_procpressurecpu.data"
45 #define NPL_TEST_PATH_PROCPRESSURE_IO abs_srcdir "/ts_procpressureio.data"
46
47 /* simulate the test of a query to the docker rest API */
48 #define NPL_TEST_PATH_CONTAINER_JSON abs_srcdir "/ts_container_docker.data"
49
50 /* simulate the test of a query to varlink */
51 #define NPL_TEST_PATH_PODMAN_GETCONTAINERSBYSTATUS_JSON \
52 abs_srcdir "/ts_container_podman_GetContainersByStatus.data"
53 #define NPL_TEST_PATH_PODMAN_GETCONTAINERSTATS_JSON \
54 abs_srcdir "/ts_container_podman_GetContainerStats.data"
55 #define NPL_TEST_PATH_PODMAN_LISTCONTAINERS_JSON \
56 abs_srcdir "/ts_container_podman_ListContainers.data"
57
58 #define TEST_ASSERT_EQUAL_NUMERIC(A, B) \
59 do { if ((A) != (B)) ret = -1; } while (0)
60
61 #define TEST_ASSERT_EQUAL_STRING(A, B) \
62 do { if (STRNEQ (A, B)) ret = -1; } while (0)
63
64 #ifdef __cplusplus
65 extern "C"
66 {
67 #endif
68
69 char * test_fstringify (const char * filename);
70 int test_main (int argc, char **argv, int (*func) (void), ...);
71
72 # define TEST_MAIN(func) \
73 int main (int argc, char **argv) \
74 { \
75 return test_main (argc, argv, func, NULL); \
76 }
77
78 # define TEST_PRELOAD(lib) \
79 do \
80 { \
81 const char *preload = secure_getenv ("LD_PRELOAD"); \
82 if (preload == NULL || strstr (preload, lib) == NULL) \
83 { \
84 char *newenv; \
85 if (!test_file_is_executable (lib)) \
86 { \
87 perror (lib); \
88 return EXIT_FAILURE; \
89 } \
90 if (!preload) \
91 { \
92 newenv = (char *) lib; \
93 } \
94 else if (asprintf (&newenv, "%s:%s", lib, preload) < 0) \
95 { \
96 perror ("asprintf"); \
97 return EXIT_FAILURE; \
98 } \
99 if (setenv ("LD_PRELOAD", newenv, 1) < 0) \
100 { \
101 perror ("setenv"); \
102 return EXIT_FAILURE; \
103 } \
104 char *argv0 = realpath (argv[0], NULL); \
105 if (!argv0) \
106 { \
107 perror ("realpath"); \
108 return EXIT_FAILURE; \
109 } \
110 execv (argv0, argv); \
111 free (argv0); \
112 } \
113 } \
114 while (0)
115
116 # define TEST_MAIN_PRELOAD(func, ...) \
117 int main (int argc, char **argv) \
118 { \
119 return test_main (argc, argv, func, __VA_ARGS__, NULL); \
120 }
121
122 /* Runs test.
123 returns: -1 = error, 0 = success */
124 int test_run (const char *title,
125 int (*body) (const void *data), const void *data);
126
127 #ifdef __cplusplus
128 }
129 #endif
130
131 #endif /* _TESTUTILS_H */