icinga2/test/perfdata-influxdbwriter.cpp
Johannes Schmidt c0bf592ec9 Add unit-tests for PerfdataWriterConnection
There's a set of two tests for each perfdatawriter, just
to make sure they can connect and send data that looks reasonably
correct, and to make sure pausing actually works while the connection
is stuck.

Then there's a more in-depth suite of tests for PerfdataWriterConnection
itself, to verify that connection handling works well in all types
of scenarios.
2026-02-03 09:31:49 +01:00

50 lines
1.4 KiB
C++

/* Icinga 2 | (c) 2025 Icinga GmbH | GPLv2+ */
#include <BoostTestTargetConfig.h>
#include "perfdata/influxdb2writer.hpp"
#include "test/base-testloggerfixture.hpp"
#include "test/perfdata-perfdatawriterfixture.hpp"
using namespace icinga;
BOOST_FIXTURE_TEST_SUITE(perfdata_influxdbwriter, PerfdataWriterFixture<Influxdb2Writer>,
*boost::unit_test::label("perfdata"))
BOOST_AUTO_TEST_CASE(connect)
{
ResumeWriter();
ReceiveCheckResults(1, ServiceState::ServiceCritical);
Accept();
auto req = GetSplitRequestBody(',');
SendResponse(boost::beast::http::status::no_content);
// Just some basic sanity tests. It's not important to check if everything is entirely
// correct here.
BOOST_REQUIRE_EQUAL(req.size(), 3);
BOOST_CHECK_EQUAL(req[0], "dummy");
BOOST_CHECK_EQUAL(req[1], "hostname=h1");
std::string_view perfData = "metric=thing value=42";
BOOST_CHECK_EQUAL(req[2].substr(0, perfData.length()), perfData);
PauseWriter();
}
BOOST_AUTO_TEST_CASE(pause_with_pending_work)
{
ResumeWriter();
// Make Influxdb2Writer fill up the connection's buffer with a huge check-result.
ReceiveCheckResults(1, ServiceState::ServiceCritical);
// Accept the connection, but only read far enough so we know the writer is now stuck.
Accept();
// Now try to pause.
PauseWriter();
REQUIRE_LOG_MESSAGE("Operation cancelled\\.", 1s);
REQUIRE_LOG_MESSAGE("'Influxdb2Writer' paused\\.", 1s);
}
BOOST_AUTO_TEST_SUITE_END()