From: Michael Tremer Date: Sat, 4 Oct 2025 14:58:18 +0000 (+0000) Subject: sources: Add some tests X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=57233aca863d7e9fe6d06b0cc95df6ecdacfe823;p=telemetry.git sources: Add some tests 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 --- diff --git a/Makefile.am b/Makefile.am index cde8540..56204ac 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 diff --git a/src/daemon/sources.c b/src/daemon/sources.c index caef0e1..25d91db 100644 --- a/src/daemon/sources.c +++ b/src/daemon/sources.c @@ -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 index 0000000..bd6b864 --- /dev/null +++ b/src/daemon/sources/test-error.c @@ -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 . # +# # +#############################################################################*/ + +#include + +#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 index 0000000..ec2b178 --- /dev/null +++ b/src/daemon/sources/test-error.h @@ -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 . # +# # +#############################################################################*/ + +#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 index 0000000..860e563 --- /dev/null +++ b/src/daemon/sources/test-flapping.c @@ -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 . # +# # +#############################################################################*/ + +#include +#include + +#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 index 0000000..f73f4c9 --- /dev/null +++ b/src/daemon/sources/test-flapping.h @@ -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 . # +# # +#############################################################################*/ + +#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 */