diff --git a/tests/common/util/MockPrometheus.hpp b/tests/common/util/MockPrometheus.hpp index 0b88efa3..8b0b23dc 100644 --- a/tests/common/util/MockPrometheus.hpp +++ b/tests/common/util/MockPrometheus.hpp @@ -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 +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; + } // namespace util::prometheus diff --git a/tests/unit/util/prometheus/HttpTests.cpp b/tests/unit/util/prometheus/HttpTests.cpp index 8624974b..b86d4dfc 100644 --- a/tests/unit/util/prometheus/HttpTests.cpp +++ b/tests/unit/util/prometheus/HttpTests.cpp @@ -48,15 +48,11 @@ struct PrometheusCheckRequestTestsParams { bool expected; }; -struct PrometheusCheckRequestTests : public ::testing::TestWithParam {}; +struct PrometheusCheckRequestTests : WithPrometheus, + ::testing::WithParamInterface {}; 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 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 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); diff --git a/tests/unit/web/ServerTests.cpp b/tests/unit/web/ServerTests.cpp index 790f81d2..62ddcdb4 100644 --- a/tests/unit/web/ServerTests.cpp +++ b/tests/unit/web/ServerTests.cpp @@ -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", diff --git a/tests/unit/web/ng/ServerTests.cpp b/tests/unit/web/ng/ServerTests.cpp index 4e8dc9de..9103f0ca 100644 --- a/tests/unit/web/ng/ServerTests.cpp +++ b/tests/unit/web/ng/ServerTests.cpp @@ -69,7 +69,9 @@ struct MakeServerTestBundle { bool expectSuccess; }; -struct MakeServerTest : NoLoggerFixture, testing::WithParamInterface { +struct MakeServerTest : util::prometheus::WithPrometheus, + NoLoggerFixture, + testing::WithParamInterface { protected: boost::asio::io_context ioContext_; };