refactor: Replace all old instances of Config with New Config (#1627)

Fixes #1184 
Previous PR's found [here](https://github.com/XRPLF/clio/pull/1593) and
[here](https://github.com/XRPLF/clio/pull/1544)
This commit is contained in:
Peter Chen
2024-12-16 15:33:32 -08:00
committed by GitHub
parent b53cfd0ec1
commit 3c4903a339
103 changed files with 1624 additions and 898 deletions

View File

@@ -19,7 +19,9 @@
#include "rpc/common/impl/APIVersionParser.hpp"
#include "util/LoggerFixtures.hpp"
#include "util/config/Config.hpp"
#include "util/newconfig/ConfigDefinition.hpp"
#include "util/newconfig/ConfigValue.hpp"
#include "util/newconfig/Types.hpp"
#include <boost/json/parse.hpp>
#include <fmt/core.h>
@@ -29,6 +31,7 @@ constexpr static auto DEFAULT_API_VERSION = 5u;
constexpr static auto MIN_API_VERSION = 2u;
constexpr static auto MAX_API_VERSION = 10u;
using namespace util::config;
using namespace rpc::impl;
namespace json = boost::json;
@@ -93,18 +96,13 @@ TEST_F(RPCAPIVersionTest, ReturnsParsedVersionIfAllPreconditionsAreMet)
TEST_F(RPCAPIVersionTest, GetsValuesFromConfigCorrectly)
{
util::Config const cfg{json::parse(fmt::format(
R"({{
"min": {},
"max": {},
"default": {}
}})",
MIN_API_VERSION,
MAX_API_VERSION,
DEFAULT_API_VERSION
))};
ClioConfigDefinition cfg{
{"api_version.min", ConfigValue{ConfigType::Integer}.defaultValue(MIN_API_VERSION)},
{"api_version.max", ConfigValue{ConfigType::Integer}.defaultValue(MAX_API_VERSION)},
{"api_version.default", ConfigValue{ConfigType::Integer}.defaultValue(DEFAULT_API_VERSION)}
};
ProductionAPIVersionParser const configuredParser{cfg};
ProductionAPIVersionParser const configuredParser{cfg.getObject("api_version")};
{
auto ver = configuredParser.parse(json::parse(R"({"api_version": 2})").as_object());

View File

@@ -25,7 +25,9 @@
#include "util/MockLoadBalancer.hpp"
#include "util/NameGenerator.hpp"
#include "util/Taggable.hpp"
#include "util/config/Config.hpp"
#include "util/newconfig/ConfigDefinition.hpp"
#include "util/newconfig/ConfigValue.hpp"
#include "util/newconfig/Types.hpp"
#include "web/Context.hpp"
#include <boost/json/object.hpp>
@@ -42,6 +44,7 @@
using namespace rpc;
using namespace testing;
using namespace util::config;
namespace json = boost::json;
constexpr static auto CLIENT_IP = "127.0.0.1";
@@ -52,7 +55,7 @@ protected:
std::shared_ptr<MockHandlerProvider> handlerProvider = std::make_shared<MockHandlerProvider>();
MockCounters counters;
util::Config config;
ClioConfigDefinition const config{{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("none")}};
util::TagDecoratorFactory tagFactory{config};
rpc::impl::ForwardingProxy<MockLoadBalancer, MockCounters, MockHandlerProvider> proxy{

View File

@@ -18,7 +18,6 @@
//==============================================================================
#include "data/BackendInterface.hpp"
#include "data/Types.hpp"
#include "rpc/Errors.hpp"
#include "rpc/FakesAndMocks.hpp"
#include "rpc/RPCEngine.hpp"
@@ -34,8 +33,12 @@
#include "util/MockPrometheus.hpp"
#include "util/NameGenerator.hpp"
#include "util/Taggable.hpp"
#include "util/config/Config.hpp"
#include "web/Context.hpp"
#include "util/newconfig/Array.hpp"
#include "util/newconfig/ConfigConstraints.hpp"
#include "util/newconfig/ConfigDefinition.hpp"
#include "util/newconfig/ConfigFileJson.hpp"
#include "util/newconfig/ConfigValue.hpp"
#include "util/newconfig/Types.hpp"
#include "web/dosguard/DOSGuard.hpp"
#include "web/dosguard/WhitelistHandler.hpp"
@@ -55,6 +58,7 @@ using namespace rpc;
using namespace util;
namespace json = boost::json;
using namespace testing;
using namespace util::config;
namespace {
constexpr auto FORWARD_REPLY = R"JSON({
@@ -66,15 +70,30 @@ constexpr auto FORWARD_REPLY = R"JSON({
})JSON";
} // namespace
inline ClioConfigDefinition
generateDefaultRPCEngineConfig()
{
return ClioConfigDefinition{
{"server.max_queue_size", ConfigValue{ConfigType::Integer}.defaultValue(2)},
{"workers", ConfigValue{ConfigType::Integer}.defaultValue(4).withConstraint(validateUint16)},
{"rpc.cache_timeout", ConfigValue{ConfigType::Double}.defaultValue(0.0).withConstraint(validatePositiveDouble)},
{"log_tag_style", ConfigValue{ConfigType::String}.defaultValue("uint")},
{"dos_guard.whitelist.[]", Array{ConfigValue{ConfigType::String}.optional()}},
{"dos_guard.max_fetches",
ConfigValue{ConfigType::Integer}.defaultValue(1000'000u).withConstraint(validateUint32)},
{"dos_guard.max_connections", ConfigValue{ConfigType::Integer}.defaultValue(20u).withConstraint(validateUint32)
},
{"dos_guard.max_requests", ConfigValue{ConfigType::Integer}.defaultValue(20u).withConstraint(validateUint32)}
};
}
struct RPCEngineTest : util::prometheus::WithPrometheus,
MockBackendTest,
MockCountersTest,
MockLoadBalancerTest,
SyncAsioContextTest {
Config cfg = Config{json::parse(R"JSON({
"server": {"max_queue_size": 2},
"workers": 4
})JSON")};
ClioConfigDefinition cfg = generateDefaultRPCEngineConfig();
util::TagDecoratorFactory tagFactory{cfg};
WorkQueue queue = WorkQueue::make_WorkQueue(cfg);
web::dosguard::WhitelistHandler whitelistHandler{cfg};
@@ -179,7 +198,13 @@ TEST_P(RPCEngineFlowParameterTest, Test)
std::shared_ptr<RPCEngine<MockLoadBalancer, MockCounters>> engine =
RPCEngine<MockLoadBalancer, MockCounters>::make_RPCEngine(
Config{}, backend, mockLoadBalancerPtr, dosGuard, queue, *mockCountersPtr, handlerProvider
generateDefaultRPCEngineConfig(),
backend,
mockLoadBalancerPtr,
dosGuard,
queue,
*mockCountersPtr,
handlerProvider
);
if (testBundle.forwarded) {
@@ -321,7 +346,7 @@ generateCacheTestValuesForParametersTest()
{
return std::vector<RPCEngineCacheTestCaseBundle>{
{.testName = "CacheEnabled",
.config = R"JSON({
.config = R"JSON({
"server": {"max_queue_size": 2},
"workers": 4,
"rpc":
@@ -334,7 +359,7 @@ generateCacheTestValuesForParametersTest()
.config = R"JSON({
"server": {"max_queue_size": 2},
"workers": 4,
"rpc": {}
"rpc": {"cache_timeout": 0}
})JSON",
.method = "server_info",
.isAdmin = false,
@@ -343,7 +368,7 @@ generateCacheTestValuesForParametersTest()
.config = R"JSON({
"server": {"max_queue_size": 2},
"workers": 4,
"rpc": {}
"rpc": {"cache_timeout": 0}
})JSON",
.method = "server_info",
.isAdmin = false,
@@ -388,7 +413,11 @@ INSTANTIATE_TEST_CASE_P(
TEST_P(RPCEngineCacheParameterTest, Test)
{
auto const& testParam = GetParam();
auto const cfgCache = Config{json::parse(testParam.config)};
auto const json = ConfigFileJson{json::parse(testParam.config).as_object()};
auto cfgCache{generateDefaultRPCEngineConfig()};
auto const errors = cfgCache.parse(json);
EXPECT_TRUE(!errors.has_value());
auto const admin = testParam.isAdmin;
auto const method = testParam.method;
@@ -432,11 +461,11 @@ TEST_P(RPCEngineCacheParameterTest, Test)
TEST_F(RPCEngineTest, NotCacheIfErrorHappen)
{
auto const cfgCache = Config{json::parse(R"JSON({
"server": {"max_queue_size": 2},
"workers": 4,
"rpc": {"cache_timeout": 10}
})JSON")};
auto const cfgCache = ClioConfigDefinition{
{"server.max_queue_size", ConfigValue{ConfigType::Integer}.defaultValue(2)},
{"workers", ConfigValue{ConfigType::Integer}.defaultValue(4).withConstraint(validateUint16)},
{"rpc.cache_timeout", ConfigValue{ConfigType::Double}.defaultValue(10.0).withConstraint(validatePositiveDouble)}
};
auto const notAdmin = false;
auto const method = "server_info";

View File

@@ -20,7 +20,9 @@
#include "rpc/WorkQueue.hpp"
#include "util/LoggerFixtures.hpp"
#include "util/MockPrometheus.hpp"
#include "util/config/Config.hpp"
#include "util/newconfig/ConfigDefinition.hpp"
#include "util/newconfig/ConfigValue.hpp"
#include "util/newconfig/Types.hpp"
#include "util/prometheus/Counter.hpp"
#include "util/prometheus/Gauge.hpp"
@@ -34,22 +36,20 @@
#include <semaphore>
using namespace util;
using namespace util::config;
using namespace rpc;
using namespace util::prometheus;
namespace {
constexpr auto JSONConfig = R"JSON({
"server": { "max_queue_size" : 2 },
"workers": 4
})JSON";
} // namespace
struct RPCWorkQueueTestBase : NoLoggerFixture {
ClioConfigDefinition cfg = {
{"server.max_queue_size", ConfigValue{ConfigType::Integer}.defaultValue(2)},
{"workers", ConfigValue{ConfigType::Integer}.defaultValue(4)}
};
struct WorkQueueTestBase : NoLoggerFixture {
Config cfg = Config{boost::json::parse(JSONConfig)};
WorkQueue queue = WorkQueue::make_WorkQueue(cfg);
};
struct WorkQueueTest : WithPrometheus, WorkQueueTestBase {};
struct WorkQueueTest : WithPrometheus, RPCWorkQueueTestBase {};
TEST_F(WorkQueueTest, WhitelistedExecutionCountAddsUp)
{
@@ -158,7 +158,7 @@ TEST_F(WorkQueueStopTest, CallsOnTasksCompleteWhenStoppingOnLastTask)
queue.join();
}
struct WorkQueueMockPrometheusTest : WithMockPrometheus, WorkQueueTestBase {};
struct WorkQueueMockPrometheusTest : WithMockPrometheus, RPCWorkQueueTestBase {};
TEST_F(WorkQueueMockPrometheusTest, postCoroCouhters)
{

View File

@@ -21,7 +21,10 @@
#include "rpc/common/Types.hpp"
#include "rpc/handlers/VersionHandler.hpp"
#include "util/HandlerBaseTestFixture.hpp"
#include "util/config/Config.hpp"
#include "util/log/Logger.hpp"
#include "util/newconfig/ConfigDefinition.hpp"
#include "util/newconfig/ConfigValue.hpp"
#include "util/newconfig/Types.hpp"
#include <boost/json/parse.hpp>
#include <boost/json/value.hpp>
@@ -33,26 +36,32 @@ constexpr static auto MIN_API_VERSION = 2u;
constexpr static auto MAX_API_VERSION = 10u;
using namespace rpc;
namespace json = boost::json;
using namespace util::config;
class RPCVersionHandlerTest : public HandlerBaseTest {};
TEST_F(RPCVersionHandlerTest, Default)
{
util::Config cfg{json::parse(fmt::format(
ClioConfigDefinition cfg{
{"api_version.min", ConfigValue{ConfigType::Integer}.defaultValue(MIN_API_VERSION)},
{"api_version.max", ConfigValue{ConfigType::Integer}.defaultValue(MAX_API_VERSION)},
{"api_version.default", ConfigValue{ConfigType::Integer}.defaultValue(DEFAULT_API_VERSION)}
};
boost::json::value jsonData = boost::json::parse(fmt::format(
R"({{
"min": {},
"max": {},
"default": {}
"api_version.min": {},
"api_version.max": {},
"api_version.default": {}
}})",
MIN_API_VERSION,
MAX_API_VERSION,
DEFAULT_API_VERSION
))};
));
runSpawn([&](auto yield) {
auto const handler = AnyHandler{VersionHandler{cfg}};
auto const output = handler.process(static_cast<json::value>(cfg), Context{yield});
auto const output = handler.process(jsonData, Context{yield});
ASSERT_TRUE(output);
// check all against all the correct values