Skip to content

Commit

Permalink
avoid get_os_temporary_path in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
battlmonstr committed Mar 21, 2024
1 parent b2191a7 commit ae76c3a
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 139 deletions.
9 changes: 6 additions & 3 deletions silkworm/capi/silkworm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <silkworm/db/snapshots/test_util/common.hpp>
#include <silkworm/db/snapshots/txn_index.hpp>
#include <silkworm/db/snapshots/txn_to_block_index.hpp>
#include <silkworm/infra/common/directories.hpp>
#include <silkworm/infra/test_util/log.hpp>
#include <silkworm/rpc/test/api_test_database.hpp>

Expand All @@ -38,6 +39,8 @@ namespace silkworm {
namespace snapshot_test = snapshots::test_util;

struct CApiTest : public rpc::test::TestDatabaseContext {
TemporaryDirectory tmp_dir;

private:
// TODO(canepat) remove test_util::StreamSwap objects when C API settings include log level
std::stringstream string_cout, string_cerr;
Expand Down Expand Up @@ -639,11 +642,11 @@ TEST_CASE_METHOD(CApiTest, "CAPI silkworm_execute_blocks_perpetual multiple bloc
}

TEST_CASE_METHOD(CApiTest, "CAPI silkworm_add_snapshot", "[silkworm][capi]") {
snapshot_test::SampleHeaderSnapshotFile valid_header_snapshot{};
snapshot_test::SampleHeaderSnapshotFile valid_header_snapshot{tmp_dir.path()};
snapshot_test::SampleHeaderSnapshotPath header_snapshot_path{valid_header_snapshot.path()};
snapshot_test::SampleBodySnapshotFile valid_body_snapshot{};
snapshot_test::SampleBodySnapshotFile valid_body_snapshot{tmp_dir.path()};
snapshot_test::SampleBodySnapshotPath body_snapshot_path{valid_body_snapshot.path()};
snapshot_test::SampleTransactionSnapshotFile valid_tx_snapshot{};
snapshot_test::SampleTransactionSnapshotFile valid_tx_snapshot{tmp_dir.path()};
snapshot_test::SampleTransactionSnapshotPath tx_snapshot_path{valid_tx_snapshot.path()};

auto header_index = snapshots::HeaderIndex::make(header_snapshot_path);
Expand Down
41 changes: 24 additions & 17 deletions silkworm/db/snapshots/index_builder_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <silkworm/db/snapshots/txn_index.hpp>
#include <silkworm/db/snapshots/txn_to_block_index.hpp>
#include <silkworm/infra/common/decoding_exception.hpp>
#include <silkworm/infra/common/directories.hpp>
#include <silkworm/infra/test_util/log.hpp>

namespace silkworm::snapshots {
Expand All @@ -34,28 +35,31 @@ using namespace Catch::Matchers;

TEST_CASE("Index::Index", "[silkworm][snapshot][index]") {
SetLogVerbosityGuard guard{log::Level::kNone};
test::TemporarySnapshotFile tmp_snapshot_file{"v1-014500-015000-headers.seg"};
TemporaryDirectory tmp_dir;
test::TemporarySnapshotFile tmp_snapshot_file{tmp_dir.path(), "v1-014500-015000-headers.seg"};
auto header_index = HeaderIndex::make(*SnapshotPath::parse(tmp_snapshot_file.path().string()));
CHECK_THROWS_AS(header_index.build(), std::logic_error);
}

// This unit test fails on Windows with error: SIGSEGV - Segmentation violation signal
TEST_CASE("BodyIndex::build OK", "[silkworm][snapshot][index]") {
SetLogVerbosityGuard guard{log::Level::kNone};
test::SampleBodySnapshotFile valid_body_snapshot{};
TemporaryDirectory tmp_dir;
test::SampleBodySnapshotFile valid_body_snapshot{tmp_dir.path()};
test::SampleBodySnapshotPath body_snapshot_path{valid_body_snapshot.path()}; // necessary to tweak the block numbers
auto body_index = BodyIndex::make(body_snapshot_path);
CHECK_NOTHROW(body_index.build());
}

TEST_CASE("TransactionIndex::build KO: empty snapshot", "[silkworm][snapshot][index]") {
SetLogVerbosityGuard guard{log::Level::kNone};
TemporaryDirectory tmp_dir;
constexpr const char* kBodiesSnapshotFileName{"v1-014500-015000-bodies.seg"};
constexpr const char* kTransactionsSnapshotFileName{"v1-014500-015000-transactions.seg"};

SECTION("KO: empty body snapshot", "[.]") {
test::TemporarySnapshotFile bodies_snapshot_file{kBodiesSnapshotFileName};
test::TemporarySnapshotFile txs_snapshot_file{kTransactionsSnapshotFileName};
test::TemporarySnapshotFile bodies_snapshot_file{tmp_dir.path(), kBodiesSnapshotFileName};
test::TemporarySnapshotFile txs_snapshot_file{tmp_dir.path(), kTransactionsSnapshotFileName};

auto txs_snapshot_path = *SnapshotPath::parse(txs_snapshot_file.path());
auto bodies_snapshot_path = *SnapshotPath::parse(bodies_snapshot_file.path());
Expand All @@ -67,10 +71,12 @@ TEST_CASE("TransactionIndex::build KO: empty snapshot", "[silkworm][snapshot][in

TEST_CASE("TransactionIndex::build KO: invalid snapshot", "[silkworm][snapshot][index]") {
SetLogVerbosityGuard guard{log::Level::kNone};
TemporaryDirectory tmp_dir;
constexpr const char* kTransactionsSnapshotFileName{"v1-015000-015500-transactions.seg"};

SECTION("KO: invalid zero word length") {
test::TemporarySnapshotFile bodies_snapshot_file{
tmp_dir.path(),
"v1-015000-015500-bodies.seg",
test::SnapshotHeader{
.words_count = 0,
Expand All @@ -79,7 +85,7 @@ TEST_CASE("TransactionIndex::build KO: invalid snapshot", "[silkworm][snapshot][
.positions = {}},
test::SnapshotBody{
*from_hex("0000000000000000")}};
test::TemporarySnapshotFile txs_snapshot_file{kTransactionsSnapshotFileName};
test::TemporarySnapshotFile txs_snapshot_file{tmp_dir.path(), kTransactionsSnapshotFileName};

auto txs_snapshot_path = *SnapshotPath::parse(txs_snapshot_file.path());
auto bodies_snapshot_path = *SnapshotPath::parse(bodies_snapshot_file.path());
Expand All @@ -90,14 +96,14 @@ TEST_CASE("TransactionIndex::build KO: invalid snapshot", "[silkworm][snapshot][

SECTION("KO: invalid position depth") {
test::SampleBodySnapshotFile invalid_bodies_snapshot{
TemporaryDirectory::get_os_temporary_path(),
tmp_dir.path(),
"000000000000000e000000000000000000000000000000000000000000000004"
"c100010801c6837004d980c001c6837004d980c001c6837004d980c001c68370" // {c1, 00} <- c1 instead of 01
"04d980c001c6837004d980c001c6837004d980c001c6837004d980c001c68370"
"04d980c001c6837004d980c001c6837004d980c001c6837004d980c001c68370"
"04d980c001c6837004d980c001c6837004d901c0"};
test::SampleBodySnapshotPath bodies_snapshot_path{invalid_bodies_snapshot.path()};
test::SampleTransactionSnapshotFile valid_txs_snapshot{};
test::SampleTransactionSnapshotFile valid_txs_snapshot{tmp_dir.path()};
test::SampleTransactionSnapshotPath txs_snapshot_path{valid_txs_snapshot.path()}; // necessary to tweak the block numbers

CHECK_THROWS_WITH(TransactionIndex::make(bodies_snapshot_path, txs_snapshot_path).build(), Contains("invalid: position depth"));
Expand All @@ -106,14 +112,14 @@ TEST_CASE("TransactionIndex::build KO: invalid snapshot", "[silkworm][snapshot][

SECTION("KO: invalid position value") {
test::SampleBodySnapshotFile invalid_bodies_snapshot{
TemporaryDirectory::get_os_temporary_path(),
tmp_dir.path(),
"000000000000000e000000000000000000000000000000000000000000000004"
"01ff010801c6837004d980c001c6837004d980c001c6837004d980c001c68370" // {01, ff} <- ff instead of 00
"04d980c001c6837004d980c001c6837004d980c001c6837004d980c001c68370"
"04d980c001c6837004d980c001c6837004d980c001c6837004d980c001c68370"
"04d980c001c6837004d980c001c6837004d901c0"};
test::SampleBodySnapshotPath bodies_snapshot_path{invalid_bodies_snapshot.path()};
test::SampleTransactionSnapshotFile valid_txs_snapshot{};
test::SampleTransactionSnapshotFile valid_txs_snapshot{tmp_dir.path()};
test::SampleTransactionSnapshotPath txs_snapshot_path{valid_txs_snapshot.path()}; // necessary to tweak the block numbers

CHECK_THROWS_WITH(TransactionIndex::make(bodies_snapshot_path, txs_snapshot_path).build(), Contains("invalid: position read"));
Expand All @@ -122,14 +128,14 @@ TEST_CASE("TransactionIndex::build KO: invalid snapshot", "[silkworm][snapshot][

SECTION("KO: invalid positions count") {
test::SampleBodySnapshotFile invalid_bodies_snapshot{
TemporaryDirectory::get_os_temporary_path(),
tmp_dir.path(),
"000000000000000e000000000000000000000000000000000000000000000005" // POSITIONS=5 <- 5 instead of 4
"0100010801c6837004d980c001c6837004d980c001c6837004d980c001c68370"
"04d980c001c6837004d980c001c6837004d980c001c6837004d980c001c68370"
"04d980c001c6837004d980c001c6837004d980c001c6837004d980c001c68370"
"04d980c001c6837004d980c001c6837004d901c0"};
test::SampleBodySnapshotPath bodies_snapshot_path{invalid_bodies_snapshot.path()};
test::SampleTransactionSnapshotFile valid_txs_snapshot{};
test::SampleTransactionSnapshotFile valid_txs_snapshot{tmp_dir.path()};
test::SampleTransactionSnapshotPath txs_snapshot_path{valid_txs_snapshot.path()}; // necessary to tweak the block numbers

CHECK_THROWS_WITH(TransactionIndex::make(bodies_snapshot_path, txs_snapshot_path).build(), Contains("invalid: position read"));
Expand All @@ -138,25 +144,25 @@ TEST_CASE("TransactionIndex::build KO: invalid snapshot", "[silkworm][snapshot][

SECTION("KO: invalid RLP") {
test::SampleBodySnapshotFile invalid_bodies_snapshot{
TemporaryDirectory::get_os_temporary_path(),
tmp_dir.path(),
"000000000000000e000000000000000000000000000000000000000000000004"
"0100010801c6837004d980c001c6837004d980c001c6837004d980c001c68370"
"04d980c001c6837004d980c001c6837004d980c001c6837004d980c001c78370" // {01, c7837004d980c0} <- c7 instead of c6
"04d980c001c6837004d980c001c6837004d980c001c6837004d980c001c68370"
"04d980c001c6837004d980c001c6837004d901c0"};
test::SampleBodySnapshotPath bodies_snapshot_path{invalid_bodies_snapshot.path()};
test::SampleTransactionSnapshotFile valid_txs_snapshot{};
test::SampleTransactionSnapshotFile valid_txs_snapshot{tmp_dir.path()};
test::SampleTransactionSnapshotPath txs_snapshot_path{valid_txs_snapshot.path()}; // necessary to tweak the block numbers

CHECK_THROWS_AS(TransactionIndex::make(bodies_snapshot_path, txs_snapshot_path).build(), DecodingException);
CHECK_THROWS_AS(TransactionToBlockIndex::make(bodies_snapshot_path, txs_snapshot_path).build(), DecodingException);
}

SECTION("KO: unexpected tx amount") {
test::SampleBodySnapshotFile valid_bodies_snapshot{};
test::SampleBodySnapshotFile valid_bodies_snapshot{tmp_dir.path()};
test::SampleBodySnapshotPath bodies_snapshot_path{valid_bodies_snapshot.path()};
test::SampleTransactionSnapshotFile invalid_txs_snapshot{
TemporaryDirectory::get_os_temporary_path(),
tmp_dir.path(),
"000000000000000C" // WC = 12
"0000000000000004" // EWC = 4
"0000000000000000" // PaTS = 0
Expand All @@ -181,9 +187,10 @@ TEST_CASE("TransactionIndex::build KO: invalid snapshot", "[silkworm][snapshot][

TEST_CASE("TransactionIndex::build OK", "[silkworm][snapshot][index]") {
SetLogVerbosityGuard guard{log::Level::kNone};
test::SampleBodySnapshotFile valid_bodies_snapshot{};
TemporaryDirectory tmp_dir;
test::SampleBodySnapshotFile valid_bodies_snapshot{tmp_dir.path()};
test::SampleBodySnapshotPath bodies_snapshot_path{valid_bodies_snapshot.path()};
test::SampleTransactionSnapshotFile valid_txs_snapshot{};
test::SampleTransactionSnapshotFile valid_txs_snapshot{tmp_dir.path()};
test::SampleTransactionSnapshotPath txs_snapshot_path{valid_txs_snapshot.path()}; // necessary to tweak the block numbers

auto tx_index = TransactionIndex::make(bodies_snapshot_path, txs_snapshot_path);
Expand Down
60 changes: 28 additions & 32 deletions silkworm/db/snapshots/repository_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ TEST_CASE("SnapshotRepository::SnapshotRepository", "[silkworm][node][snapshot]"

TEST_CASE("SnapshotRepository::reopen_folder", "[silkworm][node][snapshot]") {
SetLogVerbosityGuard guard{log::Level::kNone};
TemporaryDirectory tmp_dir;

const auto tmp_dir = TemporaryDirectory::get_unique_temporary_path();
std::filesystem::create_directories(tmp_dir);
test::TemporarySnapshotFile tmp_snapshot_1{tmp_dir, "v1-014500-015000-headers.seg"};
test::TemporarySnapshotFile tmp_snapshot_2{tmp_dir, "v1-011500-012000-bodies.seg"};
test::TemporarySnapshotFile tmp_snapshot_3{tmp_dir, "v1-015000-015500-transactions.seg"};
test::TemporarySnapshotFile tmp_snapshot_1{tmp_dir.path(), "v1-014500-015000-headers.seg"};
test::TemporarySnapshotFile tmp_snapshot_2{tmp_dir.path(), "v1-011500-012000-bodies.seg"};
test::TemporarySnapshotFile tmp_snapshot_3{tmp_dir.path(), "v1-015000-015500-transactions.seg"};
SnapshotSettings settings{tmp_snapshot_1.path().parent_path()};
SnapshotRepository repository{settings};
CHECK_THROWS_AS(repository.reopen_folder(), std::logic_error);
Expand All @@ -57,9 +56,9 @@ TEST_CASE("SnapshotRepository::reopen_folder", "[silkworm][node][snapshot]") {

TEST_CASE("SnapshotRepository::view", "[silkworm][node][snapshot]") {
SetLogVerbosityGuard guard{log::Level::kNone};
const auto tmp_dir = TemporaryDirectory::get_unique_temporary_path();
std::filesystem::create_directories(tmp_dir);
SnapshotSettings settings{tmp_dir};
TemporaryDirectory tmp_dir;

SnapshotSettings settings{tmp_dir.path()};
SnapshotRepository repository{settings};
auto failing_walk = [](const auto&) { return false; };
auto successful_walk = [](const auto&) { return true; };
Expand All @@ -81,9 +80,9 @@ TEST_CASE("SnapshotRepository::view", "[silkworm][node][snapshot]") {
}

SECTION("empty snapshots") {
test::TemporarySnapshotFile tmp_snapshot_1{tmp_dir, "v1-014500-015000-headers.seg"};
test::TemporarySnapshotFile tmp_snapshot_2{tmp_dir, "v1-011500-012000-bodies.seg"};
test::TemporarySnapshotFile tmp_snapshot_3{tmp_dir, "v1-015000-015500-transactions.seg"};
test::TemporarySnapshotFile tmp_snapshot_1{tmp_dir.path(), "v1-014500-015000-headers.seg"};
test::TemporarySnapshotFile tmp_snapshot_2{tmp_dir.path(), "v1-011500-012000-bodies.seg"};
test::TemporarySnapshotFile tmp_snapshot_3{tmp_dir.path(), "v1-015000-015500-transactions.seg"};
CHECK_THROWS_AS(repository.reopen_folder(), std::logic_error);

using ViewResult = SnapshotRepository::ViewResult;
Expand All @@ -100,9 +99,9 @@ TEST_CASE("SnapshotRepository::view", "[silkworm][node][snapshot]") {
}

SECTION("non-empty snapshots") {
test::HelloWorldSnapshotFile tmp_snapshot_1{tmp_dir, "v1-014500-015000-headers.seg"};
test::HelloWorldSnapshotFile tmp_snapshot_2{tmp_dir, "v1-011500-012000-bodies.seg"};
test::HelloWorldSnapshotFile tmp_snapshot_3{tmp_dir, "v1-015000-015500-transactions.seg"};
test::HelloWorldSnapshotFile tmp_snapshot_1{tmp_dir.path(), "v1-014500-015000-headers.seg"};
test::HelloWorldSnapshotFile tmp_snapshot_2{tmp_dir.path(), "v1-011500-012000-bodies.seg"};
test::HelloWorldSnapshotFile tmp_snapshot_3{tmp_dir.path(), "v1-015000-015500-transactions.seg"};
repository.reopen_folder();

using ViewResult = SnapshotRepository::ViewResult;
Expand All @@ -128,14 +127,13 @@ TEST_CASE("SnapshotRepository::view", "[silkworm][node][snapshot]") {

TEST_CASE("SnapshotRepository::missing_block_ranges", "[silkworm][node][snapshot]") {
SetLogVerbosityGuard guard{log::Level::kNone};
const auto tmp_dir = TemporaryDirectory::get_unique_temporary_path();
std::filesystem::create_directories(tmp_dir);
SnapshotSettings settings{tmp_dir};
TemporaryDirectory tmp_dir;
SnapshotSettings settings{tmp_dir.path()};
SnapshotRepository repository{settings};

test::HelloWorldSnapshotFile tmp_snapshot_1{tmp_dir, "v1-014500-015000-headers.seg"};
test::HelloWorldSnapshotFile tmp_snapshot_2{tmp_dir, "v1-011500-012000-bodies.seg"};
test::HelloWorldSnapshotFile tmp_snapshot_3{tmp_dir, "v1-015000-015500-transactions.seg"};
test::HelloWorldSnapshotFile tmp_snapshot_1{tmp_dir.path(), "v1-014500-015000-headers.seg"};
test::HelloWorldSnapshotFile tmp_snapshot_2{tmp_dir.path(), "v1-011500-012000-bodies.seg"};
test::HelloWorldSnapshotFile tmp_snapshot_3{tmp_dir.path(), "v1-015000-015500-transactions.seg"};
repository.reopen_folder();
CHECK(repository.missing_block_ranges() == std::vector<BlockNumRange>{
BlockNumRange{0, 11'500'000},
Expand All @@ -144,16 +142,15 @@ TEST_CASE("SnapshotRepository::missing_block_ranges", "[silkworm][node][snapshot

TEST_CASE("SnapshotRepository::find_segment", "[silkworm][node][snapshot]") {
SetLogVerbosityGuard guard{log::Level::kNone};
const auto tmp_dir = TemporaryDirectory::get_unique_temporary_path();
std::filesystem::create_directories(tmp_dir);
SnapshotSettings settings{tmp_dir};
TemporaryDirectory tmp_dir;
SnapshotSettings settings{tmp_dir.path()};
SnapshotRepository repository{settings};

// These sample snapshot files just contain data for block range [1'500'012, 1'500'013], hence current snapshot
// file name format is not sufficient to support them (see checks commented out below)
test::SampleHeaderSnapshotFile header_snapshot{tmp_dir};
test::SampleBodySnapshotFile body_snapshot{tmp_dir};
test::SampleTransactionSnapshotFile txn_snapshot{tmp_dir};
test::SampleHeaderSnapshotFile header_snapshot{tmp_dir.path()};
test::SampleBodySnapshotFile body_snapshot{tmp_dir.path()};
test::SampleTransactionSnapshotFile txn_snapshot{tmp_dir.path()};

SECTION("header w/o index") {
CHECK(repository.find_header_segment(1'500'011) == nullptr);
Expand Down Expand Up @@ -211,16 +208,15 @@ TEST_CASE("SnapshotRepository::find_segment", "[silkworm][node][snapshot]") {

TEST_CASE("SnapshotRepository::find_block_number", "[silkworm][node][snapshot]") {
SetLogVerbosityGuard guard{log::Level::kNone};
const auto tmp_dir = TemporaryDirectory::get_unique_temporary_path();
std::filesystem::create_directories(tmp_dir);
SnapshotSettings settings{tmp_dir};
TemporaryDirectory tmp_dir;
SnapshotSettings settings{tmp_dir.path()};
SnapshotRepository repository{settings};

// These sample snapshot files just contain data for block range [1'500'012, 1'500'013], hence current snapshot
// file name format is not sufficient to support them (see checks commented out below)
test::SampleHeaderSnapshotFile header_snapshot{tmp_dir};
test::SampleBodySnapshotFile body_snapshot{tmp_dir};
test::SampleTransactionSnapshotFile txn_snapshot{tmp_dir};
test::SampleHeaderSnapshotFile header_snapshot{tmp_dir.path()};
test::SampleBodySnapshotFile body_snapshot{tmp_dir.path()};
test::SampleTransactionSnapshotFile txn_snapshot{tmp_dir.path()};

test::SampleHeaderSnapshotPath header_snapshot_path{header_snapshot.path()}; // necessary to tweak the block numbers
auto header_index = HeaderIndex::make(header_snapshot_path);
Expand Down
Loading

0 comments on commit ae76c3a

Please sign in to comment.