]> git.ipfire.org Git - collecty.git/commitdiff
sources: Add some tests
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 4 Oct 2025 14:58:18 +0000 (14:58 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 4 Oct 2025 15:08:15 +0000 (15:08 +0000)
Oone constantly returns an error, the other one returns an error with a
probability of 50%. They are to test the internal error handling.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/daemon/sources.c
src/daemon/sources/test-error.c [new file with mode: 0644]
src/daemon/sources/test-error.h [new file with mode: 0644]
src/daemon/sources/test-flapping.c [new file with mode: 0644]
src/daemon/sources/test-flapping.h [new file with mode: 0644]

index cde8540a98f610818473296c510deeb67ae17a2b..56204ac76294e76a76ab7d3e3a9b885430af8a37 100644 (file)
@@ -142,6 +142,10 @@ dist_collectyd_SOURCES = \
        src/daemon/sources/pressure-memory.h \
        src/daemon/sources/processor.c \
        src/daemon/sources/processor.h \
+       src/daemon/sources/test-error.c \
+       src/daemon/sources/test-error.h \
+       src/daemon/sources/test-flapping.c \
+       src/daemon/sources/test-flapping.h \
        src/daemon/util.c \
        src/daemon/util.h
 
index caef0e1e626b0a018e73e519b2334472ba327e1e..25d91db51f85cc4ba01f2b6ddb364172014ba0ed 100644 (file)
@@ -36,6 +36,8 @@
 #include "sources/pressure-io.h"
 #include "sources/pressure-memory.h"
 #include "sources/processor.h"
+#include "sources/test-error.h"
+#include "sources/test-flapping.h"
 
 // Register all sources
 static const collecty_source_impl* source_impls[] = {
@@ -47,6 +49,11 @@ static const collecty_source_impl* source_impls[] = {
        &pressure_io_source,
        &pressure_memory_source,
        &processor_source,
+
+       // Tests
+       &test_error_source,
+       &test_flapping_source,
+
        NULL,
 };
 
diff --git a/src/daemon/sources/test-error.c b/src/daemon/sources/test-error.c
new file mode 100644 (file)
index 0000000..bd6b864
--- /dev/null
@@ -0,0 +1,40 @@
+/*#############################################################################
+#                                                                             #
+# collecty - A system statistics collection daemon for IPFire                 #
+# Copyright (C) 2025 IPFire Development Team                                  #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+
+#include "../ctx.h"
+#include "../source.h"
+#include "error.h"
+
+/*
+       This is a test source which always fails.
+*/
+
+static int test_error_collect(collecty_ctx* ctx, collecty_source* source) {
+       return -ENOSPC;
+}
+
+const collecty_source_impl test_error_source = {
+       .name    = "test-error",
+
+       // Methods
+       .collect = test_error_collect,
+};
diff --git a/src/daemon/sources/test-error.h b/src/daemon/sources/test-error.h
new file mode 100644 (file)
index 0000000..ec2b178
--- /dev/null
@@ -0,0 +1,28 @@
+/*#############################################################################
+#                                                                             #
+# collecty - A system statistics collection daemon for IPFire                 #
+# Copyright (C) 2025 IPFire Development Team                                  #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef COLLECTY_SOURCE_TEST_ERROR_H
+#define COLLECTY_SOURCE_TEST_ERROR_H
+
+#include "../source.h"
+
+extern const collecty_source_impl test_error_source;
+
+#endif /* COLLECTY_SOURCE_TEST_ERROR_H */
diff --git a/src/daemon/sources/test-flapping.c b/src/daemon/sources/test-flapping.c
new file mode 100644 (file)
index 0000000..860e563
--- /dev/null
@@ -0,0 +1,57 @@
+/*#############################################################################
+#                                                                             #
+# collecty - A system statistics collection daemon for IPFire                 #
+# Copyright (C) 2025 IPFire Development Team                                  #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+#include <stdlib.h>
+
+#include "../ctx.h"
+#include "../source.h"
+#include "error.h"
+
+/*
+       This is a test source which fails with a certain probability.
+*/
+
+static int test_flapping_collect(collecty_ctx* ctx, collecty_source* source) {
+       int r;
+
+       // Fetch some random value
+       r = rand();
+
+       // With a chance of 50%, return an error
+       if (r < (RAND_MAX / 2))
+               return -ENOSPC;
+
+       // Otherwise return the random number
+       return collecty_source_submit(source, NULL, "%d", r);
+}
+
+const collecty_source_impl test_flapping_source = {
+       .name    = "test-flapping",
+
+       // RRD Data Sources
+       .rrd_dss = {
+               { "random", "GAUGE", 0, RAND_MAX, },
+               { NULL },
+       },
+
+       // Methods
+       .collect = test_flapping_collect,
+};
diff --git a/src/daemon/sources/test-flapping.h b/src/daemon/sources/test-flapping.h
new file mode 100644 (file)
index 0000000..f73f4c9
--- /dev/null
@@ -0,0 +1,28 @@
+/*#############################################################################
+#                                                                             #
+# collecty - A system statistics collection daemon for IPFire                 #
+# Copyright (C) 2025 IPFire Development Team                                  #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef COLLECTY_SOURCE_TEST_FLAPPING_H
+#define COLLECTY_SOURCE_TEST_FLAPPING_H
+
+#include "../source.h"
+
+extern const collecty_source_impl test_flapping_source;
+
+#endif /* COLLECTY_SOURCE_TEST_FLAPPING_H */