test: Fix prometheus tests (#2312)

This commit is contained in:
Sergey Kuznetsov
2025-07-23 15:26:01 +01:00
committed by GitHub
parent 3f0f20a542
commit bcaa5f3392
4 changed files with 24 additions and 23 deletions

View File

@@ -214,7 +214,7 @@ struct WithMockPrometheus : virtual ::testing::Test {
{"prometheus.compress_reply", config::ConfigValue{config::ConfigType::Boolean}.defaultValue(true)},
{"prometheus.enabled", config::ConfigValue{config::ConfigType::Boolean}.defaultValue(true)}
};
PrometheusService::init(config);
PrometheusService::replaceInstance(nullptr);
}
static MockPrometheusImpl&
@@ -258,20 +258,24 @@ struct WithMockPrometheus : virtual ::testing::Test {
/**
* @note this class should be the first in the inheritance list
*/
struct WithPrometheus : virtual ::testing::Test {
WithPrometheus()
template <bool IsEnabled = true>
struct WithPrometheusImpl : virtual ::testing::Test {
WithPrometheusImpl()
{
config::ClioConfigDefinition const config{
{"prometheus.compress_reply", config::ConfigValue{config::ConfigType::Boolean}.defaultValue(false)},
{"prometheus.enabled", config::ConfigValue{config::ConfigType::Boolean}.defaultValue(true)}
{"prometheus.enabled", config::ConfigValue{config::ConfigType::Boolean}.defaultValue(IsEnabled)}
};
PrometheusService::init(config);
}
~WithPrometheus() override
~WithPrometheusImpl() override
{
PrometheusService::replaceInstance(nullptr);
}
};
using WithPrometheus = WithPrometheusImpl<>;
using WithPrometheusDisabled = WithPrometheusImpl<false>;
} // namespace util::prometheus

View File

@@ -48,15 +48,11 @@ struct PrometheusCheckRequestTestsParams {
bool expected;
};
struct PrometheusCheckRequestTests : public ::testing::TestWithParam<PrometheusCheckRequestTestsParams> {};
struct PrometheusCheckRequestTests : WithPrometheus,
::testing::WithParamInterface<PrometheusCheckRequestTestsParams> {};
TEST_P(PrometheusCheckRequestTests, isPrometheusRequest)
{
ClioConfigDefinition const config{
{"prometheus.enabled", ConfigValue{ConfigType::Boolean}.defaultValue(GetParam().prometheusEnabled)},
{"prometheus.compress_reply", ConfigValue{ConfigType::Boolean}.defaultValue(true)}
};
PrometheusService::init(config);
boost::beast::http::request<boost::beast::http::string_body> req;
req.method(GetParam().method);
req.target(GetParam().target);
@@ -111,9 +107,10 @@ INSTANTIATE_TEST_CASE_P(
tests::util::kNAME_GENERATOR
);
struct PrometheusHandleRequestTests : util::prometheus::WithPrometheus {
struct PrometheusHandleRequestTestsBase {
http::request<http::string_body> const req{http::verb::get, "/metrics", 11};
};
struct PrometheusHandleRequestTests : util::prometheus::WithPrometheus, PrometheusHandleRequestTestsBase {};
TEST_F(PrometheusHandleRequestTests, emptyResponse)
{
@@ -124,13 +121,11 @@ TEST_F(PrometheusHandleRequestTests, emptyResponse)
EXPECT_EQ(response->body(), "");
}
TEST_F(PrometheusHandleRequestTests, prometheusDisabled)
struct PrometheusDisabledHandleRequestTests : util::prometheus::WithPrometheusDisabled,
PrometheusHandleRequestTestsBase {};
TEST_F(PrometheusDisabledHandleRequestTests, prometheusDisabled)
{
ClioConfigDefinition const config{
{"prometheus.enabled", ConfigValue{ConfigType::Boolean}.defaultValue(false)},
{"prometheus.compress_reply", ConfigValue{ConfigType::Boolean}.defaultValue(true)}
};
PrometheusService::init(config);
auto response = handlePrometheusRequest(req, true);
ASSERT_TRUE(response.has_value());
EXPECT_EQ(response->result(), http::status::forbidden);

View File

@@ -688,7 +688,9 @@ TEST_F(WebServerPrometheusTest, rejectedWithoutAdminPassword)
EXPECT_EQ(status, boost::beast::http::status::unauthorized);
}
TEST_F(WebServerPrometheusTest, rejectedIfPrometheusIsDisabled)
struct WebServerPrometheusDisabledTest : util::prometheus::WithPrometheusDisabled, WebServerTest {};
TEST_F(WebServerPrometheusDisabledTest, rejectedIfPrometheusIsDisabled)
{
uint32_t webServerPort = tests::util::generateFreePort();
std::string const jsonServerConfigWithDisabledPrometheus = fmt::format(
@@ -698,8 +700,7 @@ TEST_F(WebServerPrometheusTest, rejectedIfPrometheusIsDisabled)
"port": {},
"admin_password": "secret",
"ws_max_sending_queue_size": 1500
}},
"prometheus": {{ "enabled": false }}
}}
}})JSON",
webServerPort
);
@@ -708,7 +709,6 @@ TEST_F(WebServerPrometheusTest, rejectedIfPrometheusIsDisabled)
ClioConfigDefinition const serverConfig{
getParseAdminServerConfig(boost::json::parse(jsonServerConfigWithDisabledPrometheus))
};
PrometheusService::init(serverConfig);
auto server = makeServerSync(serverConfig, ctx, dosGuard, e);
auto const [status, res] = HttpSyncClient::get(
"localhost",

View File

@@ -69,7 +69,9 @@ struct MakeServerTestBundle {
bool expectSuccess;
};
struct MakeServerTest : NoLoggerFixture, testing::WithParamInterface<MakeServerTestBundle> {
struct MakeServerTest : util::prometheus::WithPrometheus,
NoLoggerFixture,
testing::WithParamInterface<MakeServerTestBundle> {
protected:
boost::asio::io_context ioContext_;
};