test: Fix integration tests (#1788)

Fixes #1784
This commit is contained in:
Sergey Kuznetsov
2024-12-17 15:35:17 +00:00
committed by GitHub
parent 8dc7f16ef1
commit fc3e60f17f
3 changed files with 116 additions and 75 deletions

View File

@@ -21,50 +21,71 @@
#include "data/cassandra/Handle.hpp"
#include "util/AsioContextTestFixture.hpp"
#include "util/MockPrometheus.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 <TestGlobals.hpp>
#include <boost/json/parse.hpp>
#include <fmt/core.h>
#include <gtest/gtest.h>
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <stdexcept>
#include <string>
#include <thread>
using namespace util::config;
namespace {
constexpr auto keyspace = "factory_test";
} // namespace
struct BackendCassandraFactoryTest : SyncAsioContextTest, util::prometheus::WithPrometheus {
constexpr static auto keyspace = "factory_test";
class BackendCassandraFactoryTest : public SyncAsioContextTest, public util::prometheus::WithPrometheus {
protected:
void
SetUp() override
{
SyncAsioContextTest::SetUp();
}
ClioConfigDefinition cfg_{
{"database.type", ConfigValue{ConfigType::String}.defaultValue("cassandra")},
{"database.cassandra.contact_points",
ConfigValue{ConfigType::String}.defaultValue(TestGlobals::instance().backendHost)},
{"database.cassandra.secure_connect_bundle", ConfigValue{ConfigType::String}.optional()},
{"database.cassandra.port", ConfigValue{ConfigType::Integer}.optional()},
{"database.cassandra.keyspace", ConfigValue{ConfigType::String}.defaultValue(keyspace)},
{"database.cassandra.replication_factor", ConfigValue{ConfigType::Integer}.defaultValue(1)},
{"database.cassandra.table_prefix", ConfigValue{ConfigType::String}.optional()},
{"database.cassandra.max_write_requests_outstanding", ConfigValue{ConfigType::Integer}.defaultValue(10'000)},
{"database.cassandra.max_read_requests_outstanding", ConfigValue{ConfigType::Integer}.defaultValue(100'000)},
{"database.cassandra.threads",
ConfigValue{ConfigType::Integer}.defaultValue(static_cast<uint32_t>(std::thread::hardware_concurrency()))},
{"database.cassandra.core_connections_per_host", ConfigValue{ConfigType::Integer}.defaultValue(1)},
{"database.cassandra.queue_size_io", ConfigValue{ConfigType::Integer}.optional()},
{"database.cassandra.write_batch_size", ConfigValue{ConfigType::Integer}.defaultValue(20)},
{"database.cassandra.connect_timeout", ConfigValue{ConfigType::Integer}.defaultValue(1).optional()},
{"database.cassandra.request_timeout", ConfigValue{ConfigType::Integer}.defaultValue(1).optional()},
{"database.cassandra.username", ConfigValue{ConfigType::String}.optional()},
{"database.cassandra.password", ConfigValue{ConfigType::String}.optional()},
{"database.cassandra.certfile", ConfigValue{ConfigType::String}.optional()},
{"read_only", ConfigValue{ConfigType::Boolean}.defaultValue(false)}
};
void
TearDown() override
useConfig(std::string config)
{
SyncAsioContextTest::TearDown();
auto jsonConfig = boost::json::parse(config).as_object();
auto const parseErrors = cfg_.parse(ConfigFileJson{jsonConfig});
if (parseErrors) {
std::ranges::for_each(*parseErrors, [](auto const& error) { std::cout << error.error << std::endl; });
FAIL() << "Failed to parse config";
}
}
};
class BackendCassandraFactoryTestWithDB : public BackendCassandraFactoryTest {
protected:
void
SetUp() override
{
BackendCassandraFactoryTest::SetUp();
}
void
TearDown() override
{
BackendCassandraFactoryTest::TearDown();
// drop the keyspace for next test
data::cassandra::Handle const handle{TestGlobals::instance().backendHost};
EXPECT_TRUE(handle.connect());
@@ -74,34 +95,28 @@ protected:
TEST_F(BackendCassandraFactoryTest, NoSuchBackend)
{
ClioConfigDefinition const cfg{{"database.type", ConfigValue{ConfigType::String}.defaultValue("unknown")}};
EXPECT_THROW(data::make_Backend(cfg), std::runtime_error);
useConfig(R"json( {"database": {"type": "unknown"}} )json");
EXPECT_THROW(data::make_Backend(cfg_), std::runtime_error);
}
TEST_F(BackendCassandraFactoryTest, CreateCassandraBackendDBDisconnect)
{
ClioConfigDefinition const cfg{
{"database.type", ConfigValue{ConfigType::String}.defaultValue("cassandra")},
{"database.cassandra.contact_points", ConfigValue{ConfigType::String}.defaultValue("127.0.0.2")},
{"database.cassandra.keyspace", ConfigValue{ConfigType::String}.defaultValue(keyspace)},
{"database.cassandra.replication_factor", ConfigValue{ConfigType::Integer}.defaultValue(1)},
{"database.cassandra.connect_timeout", ConfigValue{ConfigType::Integer}.defaultValue(2)}
};
EXPECT_THROW(data::make_Backend(cfg), std::runtime_error);
useConfig(R"json(
{"database": {
"type": "cassandra",
"cassandra": {
"contact_points": "127.0.0.2"
}
}}
)json");
EXPECT_THROW(data::make_Backend(cfg_), std::runtime_error);
}
TEST_F(BackendCassandraFactoryTestWithDB, CreateCassandraBackend)
{
ClioConfigDefinition const cfg{
{"database.type", ConfigValue{ConfigType::String}.defaultValue("cassandra")},
{"database.cassandra.contact_points",
ConfigValue{ConfigType::String}.defaultValue(TestGlobals::instance().backendHost)},
{"database.cassandra.keyspace", ConfigValue{ConfigType::String}.defaultValue(keyspace)},
{"database.cassandra.replication_factor", ConfigValue{ConfigType::Integer}.defaultValue(1)}
};
{
auto backend = data::make_Backend(cfg);
auto backend = data::make_Backend(cfg_);
EXPECT_TRUE(backend);
// empty db does not have ledger range
@@ -115,7 +130,7 @@ TEST_F(BackendCassandraFactoryTestWithDB, CreateCassandraBackend)
}
{
auto backend = data::make_Backend(cfg);
auto backend = data::make_Backend(cfg_);
EXPECT_TRUE(backend);
auto const range = backend->fetchLedgerRange();
@@ -126,38 +141,15 @@ TEST_F(BackendCassandraFactoryTestWithDB, CreateCassandraBackend)
TEST_F(BackendCassandraFactoryTestWithDB, CreateCassandraBackendReadOnlyWithEmptyDB)
{
ClioConfigDefinition const cfg{
{"read_only", ConfigValue{ConfigType::Boolean}.defaultValue(true)},
{"database.type", ConfigValue{ConfigType::String}.defaultValue("cassandra")},
{"database.cassandra.contact_points",
ConfigValue{ConfigType::String}.defaultValue(TestGlobals::instance().backendHost)},
{"database.cassandra.keyspace", ConfigValue{ConfigType::String}.defaultValue(keyspace)},
{"database.cassandra.replication_factor", ConfigValue{ConfigType::Integer}.defaultValue(1)}
};
EXPECT_THROW(data::make_Backend(cfg), std::runtime_error);
useConfig(R"json( {"read_only": true} )json");
EXPECT_THROW(data::make_Backend(cfg_), std::runtime_error);
}
TEST_F(BackendCassandraFactoryTestWithDB, CreateCassandraBackendReadOnlyWithDBReady)
{
ClioConfigDefinition const cfgReadOnly{
{"read_only", ConfigValue{ConfigType::Boolean}.defaultValue(true)},
{"database.type", ConfigValue{ConfigType::String}.defaultValue("cassandra")},
{"database.cassandra.contact_points",
ConfigValue{ConfigType::String}.defaultValue(TestGlobals::instance().backendHost)},
{"database.cassandra.keyspace", ConfigValue{ConfigType::String}.defaultValue(keyspace)},
{"database.cassandra.replication_factor", ConfigValue{ConfigType::Integer}.defaultValue(1)}
};
auto cfgReadOnly = cfg_;
ASSERT_FALSE(cfgReadOnly.parse(ConfigFileJson{boost::json::parse(R"json( {"read_only": true} )json").as_object()}));
ClioConfigDefinition const cfgWrite{
{"read_only", ConfigValue{ConfigType::Boolean}.defaultValue(false)},
{"database.type", ConfigValue{ConfigType::String}.defaultValue("cassandra")},
{"database.cassandra.contact_points",
ConfigValue{ConfigType::String}.defaultValue(TestGlobals::instance().backendHost)},
{"database.cassandra.keyspace", ConfigValue{ConfigType::String}.defaultValue(keyspace)},
{"database.cassandra.replication_factor", ConfigValue{ConfigType::Integer}.defaultValue(1)}
};
EXPECT_TRUE(data::make_Backend(cfgWrite));
EXPECT_TRUE(data::make_Backend(cfg_));
EXPECT_TRUE(data::make_Backend(cfgReadOnly));
}