Debian Codegraph @

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

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2018,2024 Davide Madrisan <[email protected]>
5 *
6 * Unit test for lib/container_count.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 <stdlib.h>
24
25 #include "container.h"
26 #include "testutils.h"
27
28 #define NPL_TESTING
29
30 static int docker_get (chunk_t * chunk, const int query, const char *id);
31 static void docker_close (chunk_t * chunk);
32
33 #include "../lib/container.c"
34
35 static int
36 docker_get (chunk_t *chunk, const int query, const char *id)
37 {
38 char *filename = NULL;
39
40 switch (query)
41 {
42 default:
43 return EXIT_AM_HARDFAIL;
44 break;
45 case DOCKER_CONTAINERS_JSON:
46 filename = NPL_TEST_PATH_CONTAINER_JSON;
47 break;
48 }
49
50 chunk->memory = test_fstringify (filename);
51 if (NULL == chunk->memory)
52 return EXIT_AM_HARDFAIL;
53
54 chunk->size = strlen (chunk->memory);
55
56 return 0;
57 }
58
59 static void
60 docker_close (chunk_t *chunk)
61 {
62 free (chunk->memory);
63 }
64
65 #undef NPL_TESTING
66
67 typedef struct test_data
68 {
69 char *image;
70 char *perfdata;
71 unsigned int expect_value;
72 } test_data;
73
74 static int
75 test_docker_running_containers (const void *tdata)
76 {
77 const struct test_data *data = tdata;
78 int err, ret = 0;
79 char *perfdata = NULL;
80 unsigned int containers;
81
82 err =
83 docker_running_containers (NULL, &containers, data->image, &perfdata,
84 false);
85 if (err != 0)
86 {
87 free (perfdata);
88 return err;
89 }
90
91 TEST_ASSERT_EQUAL_NUMERIC (containers, data->expect_value);
92 TEST_ASSERT_EQUAL_STRING (perfdata, data->perfdata);
93
94 free (perfdata);
95 return ret;
96 }
97
98 static int
99 mymain (void)
100 {
101 int ret = 0;
102
103 #define DO_TEST(MSG, IMAGE, PERFDATA, EXPECT_VALUE) \
104 do \
105 { \
106 test_data data = { \
107 .image = IMAGE, \
108 .perfdata = PERFDATA, \
109 .expect_value = EXPECT_VALUE \
110 }; \
111 if (test_run(MSG, test_docker_running_containers, &data) < 0) \
112 ret = -1; \
113 } \
114 while (0)
115
116 DO_TEST ("check running containers with image set", "nginx",
117 "containers_nginx=3", 3);
118 DO_TEST ("check running containers with image set", "redis",
119 "containers_redis=1", 1);
120 DO_TEST ("check running containers", NULL,
121 "containers_redis=1 containers_nginx=3 containers_total=4", 4);
122
123 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
124 }
125
126 TEST_MAIN (mymain);