mirror of
https://github.com/XRPLF/clio.git
synced 2025-12-06 17:27:58 +00:00
test: Fix prometheus tests (#2312)
This commit is contained in:
@@ -214,7 +214,7 @@ struct WithMockPrometheus : virtual ::testing::Test {
|
|||||||
{"prometheus.compress_reply", config::ConfigValue{config::ConfigType::Boolean}.defaultValue(true)},
|
{"prometheus.compress_reply", config::ConfigValue{config::ConfigType::Boolean}.defaultValue(true)},
|
||||||
{"prometheus.enabled", config::ConfigValue{config::ConfigType::Boolean}.defaultValue(true)}
|
{"prometheus.enabled", config::ConfigValue{config::ConfigType::Boolean}.defaultValue(true)}
|
||||||
};
|
};
|
||||||
PrometheusService::init(config);
|
PrometheusService::replaceInstance(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
static MockPrometheusImpl&
|
static MockPrometheusImpl&
|
||||||
@@ -258,20 +258,24 @@ struct WithMockPrometheus : virtual ::testing::Test {
|
|||||||
/**
|
/**
|
||||||
* @note this class should be the first in the inheritance list
|
* @note this class should be the first in the inheritance list
|
||||||
*/
|
*/
|
||||||
struct WithPrometheus : virtual ::testing::Test {
|
template <bool IsEnabled = true>
|
||||||
WithPrometheus()
|
struct WithPrometheusImpl : virtual ::testing::Test {
|
||||||
|
WithPrometheusImpl()
|
||||||
{
|
{
|
||||||
config::ClioConfigDefinition const config{
|
config::ClioConfigDefinition const config{
|
||||||
{"prometheus.compress_reply", config::ConfigValue{config::ConfigType::Boolean}.defaultValue(false)},
|
{"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);
|
PrometheusService::init(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
~WithPrometheus() override
|
~WithPrometheusImpl() override
|
||||||
{
|
{
|
||||||
PrometheusService::replaceInstance(nullptr);
|
PrometheusService::replaceInstance(nullptr);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
using WithPrometheus = WithPrometheusImpl<>;
|
||||||
|
using WithPrometheusDisabled = WithPrometheusImpl<false>;
|
||||||
|
|
||||||
} // namespace util::prometheus
|
} // namespace util::prometheus
|
||||||
|
|||||||
@@ -48,15 +48,11 @@ struct PrometheusCheckRequestTestsParams {
|
|||||||
bool expected;
|
bool expected;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct PrometheusCheckRequestTests : public ::testing::TestWithParam<PrometheusCheckRequestTestsParams> {};
|
struct PrometheusCheckRequestTests : WithPrometheus,
|
||||||
|
::testing::WithParamInterface<PrometheusCheckRequestTestsParams> {};
|
||||||
|
|
||||||
TEST_P(PrometheusCheckRequestTests, isPrometheusRequest)
|
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;
|
boost::beast::http::request<boost::beast::http::string_body> req;
|
||||||
req.method(GetParam().method);
|
req.method(GetParam().method);
|
||||||
req.target(GetParam().target);
|
req.target(GetParam().target);
|
||||||
@@ -111,9 +107,10 @@ INSTANTIATE_TEST_CASE_P(
|
|||||||
tests::util::kNAME_GENERATOR
|
tests::util::kNAME_GENERATOR
|
||||||
);
|
);
|
||||||
|
|
||||||
struct PrometheusHandleRequestTests : util::prometheus::WithPrometheus {
|
struct PrometheusHandleRequestTestsBase {
|
||||||
http::request<http::string_body> const req{http::verb::get, "/metrics", 11};
|
http::request<http::string_body> const req{http::verb::get, "/metrics", 11};
|
||||||
};
|
};
|
||||||
|
struct PrometheusHandleRequestTests : util::prometheus::WithPrometheus, PrometheusHandleRequestTestsBase {};
|
||||||
|
|
||||||
TEST_F(PrometheusHandleRequestTests, emptyResponse)
|
TEST_F(PrometheusHandleRequestTests, emptyResponse)
|
||||||
{
|
{
|
||||||
@@ -124,13 +121,11 @@ TEST_F(PrometheusHandleRequestTests, emptyResponse)
|
|||||||
EXPECT_EQ(response->body(), "");
|
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);
|
auto response = handlePrometheusRequest(req, true);
|
||||||
ASSERT_TRUE(response.has_value());
|
ASSERT_TRUE(response.has_value());
|
||||||
EXPECT_EQ(response->result(), http::status::forbidden);
|
EXPECT_EQ(response->result(), http::status::forbidden);
|
||||||
|
|||||||
@@ -688,7 +688,9 @@ TEST_F(WebServerPrometheusTest, rejectedWithoutAdminPassword)
|
|||||||
EXPECT_EQ(status, boost::beast::http::status::unauthorized);
|
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();
|
uint32_t webServerPort = tests::util::generateFreePort();
|
||||||
std::string const jsonServerConfigWithDisabledPrometheus = fmt::format(
|
std::string const jsonServerConfigWithDisabledPrometheus = fmt::format(
|
||||||
@@ -698,8 +700,7 @@ TEST_F(WebServerPrometheusTest, rejectedIfPrometheusIsDisabled)
|
|||||||
"port": {},
|
"port": {},
|
||||||
"admin_password": "secret",
|
"admin_password": "secret",
|
||||||
"ws_max_sending_queue_size": 1500
|
"ws_max_sending_queue_size": 1500
|
||||||
}},
|
}}
|
||||||
"prometheus": {{ "enabled": false }}
|
|
||||||
}})JSON",
|
}})JSON",
|
||||||
webServerPort
|
webServerPort
|
||||||
);
|
);
|
||||||
@@ -708,7 +709,6 @@ TEST_F(WebServerPrometheusTest, rejectedIfPrometheusIsDisabled)
|
|||||||
ClioConfigDefinition const serverConfig{
|
ClioConfigDefinition const serverConfig{
|
||||||
getParseAdminServerConfig(boost::json::parse(jsonServerConfigWithDisabledPrometheus))
|
getParseAdminServerConfig(boost::json::parse(jsonServerConfigWithDisabledPrometheus))
|
||||||
};
|
};
|
||||||
PrometheusService::init(serverConfig);
|
|
||||||
auto server = makeServerSync(serverConfig, ctx, dosGuard, e);
|
auto server = makeServerSync(serverConfig, ctx, dosGuard, e);
|
||||||
auto const [status, res] = HttpSyncClient::get(
|
auto const [status, res] = HttpSyncClient::get(
|
||||||
"localhost",
|
"localhost",
|
||||||
|
|||||||
@@ -69,7 +69,9 @@ struct MakeServerTestBundle {
|
|||||||
bool expectSuccess;
|
bool expectSuccess;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct MakeServerTest : NoLoggerFixture, testing::WithParamInterface<MakeServerTestBundle> {
|
struct MakeServerTest : util::prometheus::WithPrometheus,
|
||||||
|
NoLoggerFixture,
|
||||||
|
testing::WithParamInterface<MakeServerTestBundle> {
|
||||||
protected:
|
protected:
|
||||||
boost::asio::io_context ioContext_;
|
boost::asio::io_context ioContext_;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user