Skip to content

Commit

Permalink
Merge bitcoin#13145: Use common getPath method to create temp directo…
Browse files Browse the repository at this point in the history
…ry in tests.

075429a Use common SetDataDir method to create temp directory in tests. (winder)

Pull request description:

  Took a stab at bitcoin#12574

  Created a `getPath` method which can be used with the `TestingSetup` fixture to create a temp directory. Updated tests using temp directories to use this method.

  I tried setting up a `BOOST_GLOBAL_FIXTURE` to create a truly global path for all tests but was getting linker errors when including `boost/test/unit_test.hpp` in `test_bitcoin.cpp`. Even if I had gotten the linking to work, it looks like `make check` invokes the test binary a bunch of times, so it may not have worked anyway.

Tree-SHA512: b51d0f5fada5d652ccc9362596cf98a742aa47f5daf94f189b5f034d8c035c85d095377befdcff7fb4247154d5160e8c500d70f554a2158e2c185a9d24f694f1
  • Loading branch information
MarcoFalke authored and PastaPastaPasta committed Dec 16, 2020
1 parent 300572e commit 6372283
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 36 deletions.
14 changes: 7 additions & 7 deletions src/test/dbwrapper_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
{
// Perform tests both obfuscated and non-obfuscated.
for (bool obfuscate : {false, true}) {
fs::path ph = fs::temp_directory_path() / fs::unique_path();
fs::path ph = SetDataDir(std::string("dbwrapper").append(obfuscate ? "_true" : "_false"));
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
char key = 'k';
uint256 in = InsecureRand256();
Expand All @@ -47,7 +47,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_batch)
{
// Perform tests both obfuscated and non-obfuscated.
for (bool obfuscate : {false, true}) {
fs::path ph = fs::temp_directory_path() / fs::unique_path();
fs::path ph = SetDataDir(std::string("dbwrapper_batch").append(obfuscate ? "_true" : "_false"));
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);

char key = 'i';
Expand Down Expand Up @@ -83,7 +83,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
{
// Perform tests both obfuscated and non-obfuscated.
for (bool obfuscate : {false, true}) {
fs::path ph = fs::temp_directory_path() / fs::unique_path();
fs::path ph = SetDataDir(std::string("dbwrapper_iterator").append(obfuscate ? "_true" : "_false"));
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);

// The two keys are intentionally chosen for ordering
Expand Down Expand Up @@ -123,7 +123,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
{
// We're going to share this fs::path between two wrappers
fs::path ph = fs::temp_directory_path() / fs::unique_path();
fs::path ph = SetDataDir("existing_data_no_obfuscate");
create_directories(ph);

// Set up a non-obfuscated wrapper to write some initial data.
Expand Down Expand Up @@ -164,7 +164,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
BOOST_AUTO_TEST_CASE(existing_data_reindex)
{
// We're going to share this fs::path between two wrappers
fs::path ph = fs::temp_directory_path() / fs::unique_path();
fs::path ph = SetDataDir("existing_data_reindex");
create_directories(ph);

// Set up a non-obfuscated wrapper to write some initial data.
Expand Down Expand Up @@ -199,7 +199,7 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex)

BOOST_AUTO_TEST_CASE(iterator_ordering)
{
fs::path ph = fs::temp_directory_path() / fs::unique_path();
fs::path ph = SetDataDir("iterator_ordering");
CDBWrapper dbw(ph, (1 << 20), true, false, false);
for (int x=0x00; x<256; ++x) {
uint8_t key = x;
Expand Down Expand Up @@ -277,7 +277,7 @@ BOOST_AUTO_TEST_CASE(iterator_string_ordering)
{
char buf[10];

fs::path ph = fs::temp_directory_path() / fs::unique_path();
fs::path ph = SetDataDir("iterator_string_ordering");
CDBWrapper dbw(ph, (1 << 20), true, false, false);
for (int x=0x00; x<10; ++x) {
for (int y = 0; y < 10; y++) {
Expand Down
51 changes: 29 additions & 22 deletions src/test/test_dash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,42 +53,50 @@ extern bool fPrintToConsole;
extern void noui_connect();

BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
: m_path_root(fs::temp_directory_path() / "test_bitcoin" / strprintf("%lu_%i", (unsigned long)GetTime(), (int)(InsecureRandRange(1 << 30))))
{
SHA256AutoDetect();
RandomInit();
ECC_Start();
BLSInit();
SetupEnvironment();
SetupNetworking();
InitSignatureCache();
InitScriptExecutionCache();
CPrivateSend::InitStandardDenominations();
fPrintToDebugLog = false; // don't want to write to debug.log file
fCheckBlockIndex = true;
SelectParams(chainName);
evoDb.reset(new CEvoDB(1 << 20, true, true));
deterministicMNManager.reset(new CDeterministicMNManager(*evoDb));
noui_connect();
SHA256AutoDetect();
RandomInit();
ECC_Start();
BLSInit();
SetupEnvironment();
SetupNetworking();
InitSignatureCache();
InitScriptExecutionCache();
CPrivateSend::InitStandardDenominations();
fPrintToDebugLog = false; // don't want to write to debug.log file
fCheckBlockIndex = true;
SelectParams(chainName);
evoDb.reset(new CEvoDB(1 << 20, true, true));
deterministicMNManager.reset(new CDeterministicMNManager(*evoDb));
noui_connect();
}

BasicTestingSetup::~BasicTestingSetup()
{
deterministicMNManager.reset();
evoDb.reset();
deterministicMNManager.reset();
evoDb.reset();

ECC_Stop();
fs::remove_all(m_path_root);
ECC_Stop();
}

fs::path BasicTestingSetup::SetDataDir(const std::string& name)
{
fs::path ret = m_path_root / name;
fs::create_directories(ret);
gArgs.ForceSetArg("-datadir", ret.string());
return ret;
}

TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(chainName)
{
SetDataDir("tempdir");
const CChainParams& chainparams = Params();
// Ideally we'd move all the RPC tests to the functional testing framework
// instead of unit tests, but for now we need these here.
RegisterAllCoreRPCCommands(tableRPC);
ClearDatadirCache();
pathTemp = fs::temp_directory_path() / strprintf("test_dash_%lu_%i", (unsigned long)GetTime(), (int)(InsecureRandRange(100000)));
fs::create_directories(pathTemp);
gArgs.ForceSetArg("-datadir", pathTemp.string());

// We have to run a scheduler thread to prevent ActivateBestChain
// from blocking due to queue overrun.
Expand Down Expand Up @@ -131,7 +139,6 @@ TestingSetup::~TestingSetup()
llmq::DestroyLLMQSystem();
pcoinsdbview.reset();
pblocktree.reset();
fs::remove_all(pathTemp);
}

TestChainSetup::TestChainSetup(int blockCount) : TestingSetup(CBaseChainParams::REGTEST)
Expand Down
6 changes: 5 additions & 1 deletion src/test/test_dash.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ struct BasicTestingSetup {

explicit BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
~BasicTestingSetup();

fs::path SetDataDir(const std::string& name);

private:
const fs::path m_path_root;
};

/** Testing setup that configures a complete environment.
Expand All @@ -65,7 +70,6 @@ struct CConnmanTest {

class PeerLogicValidation;
struct TestingSetup: public BasicTestingSetup {
fs::path pathTemp;
boost::thread_group threadGroup;
CConnman* connman;
CScheduler scheduler;
Expand Down
8 changes: 4 additions & 4 deletions src/test/util_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,7 @@ static void TestOtherProcess(fs::path dirname, std::string lockname, int fd)

BOOST_AUTO_TEST_CASE(test_LockDirectory)
{
fs::path dirname = fs::temp_directory_path() / fs::unique_path();
fs::path dirname = SetDataDir("test_LockDirectory") / fs::unique_path();
const std::string lockname = ".lock";
#ifndef WIN32
// Revert SIGCHLD to default, otherwise boost.test will catch and fail on
Expand Down Expand Up @@ -1213,12 +1213,12 @@ BOOST_AUTO_TEST_CASE(test_LockDirectory)

BOOST_AUTO_TEST_CASE(test_DirIsWritable)
{
// Should be able to write to the system tmp dir.
fs::path tmpdirname = fs::temp_directory_path();
// Should be able to write to the data dir.
fs::path tmpdirname = SetDataDir("test_DirIsWritable");
BOOST_CHECK_EQUAL(DirIsWritable(tmpdirname), true);

// Should not be able to write to a non-existent dir.
tmpdirname = fs::temp_directory_path() / fs::unique_path();
tmpdirname = tmpdirname / fs::unique_path();
BOOST_CHECK_EQUAL(DirIsWritable(tmpdirname), false);

fs::create_directory(tmpdirname);
Expand Down
6 changes: 4 additions & 2 deletions src/wallet/test/wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,8 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)

LOCK(cs_main);

std::string backup_file = (SetDataDir("importwallet_rescan") / "wallet.backup").string();

// Import key into wallet and call dumpwallet to create backup file.
{
CWallet wallet("dummy", WalletDatabase::CreateDummy());
Expand All @@ -478,7 +480,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)

JSONRPCRequest request;
request.params.setArray();
request.params.push_back((pathTemp / "wallet.backup").string());
request.params.push_back(backup_file);
AddWallet(&wallet);
::dumpwallet(request);
RemoveWallet(&wallet);
Expand All @@ -491,7 +493,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)

JSONRPCRequest request;
request.params.setArray();
request.params.push_back((pathTemp / "wallet.backup").string());
request.params.push_back(backup_file);
AddWallet(&wallet);
::importwallet(request);
RemoveWallet(&wallet);
Expand Down

0 comments on commit 6372283

Please sign in to comment.