Skip to content

Commit c0cbe26

Browse files
committed
Merge bitcoin/bitcoin#30748: test: Pin and document TEST_DIR_PATH_ELEMENT, SeedRand::FIXED_SEED
fa84f9d test: Pin and document TEST_DIR_PATH_ELEMENT (MarcoFalke) 2222f7a test: Rename SeedRand::SEED to FIXED_SEED for clarity (MarcoFalke) Pull request description: Two small test changes: * A refactor to update the name and documentation around `SeedRand::FIXED_SEED`. * A change to extract and document `TEST_DIR_PATH_ELEMENT`, and to change its value to better match the `TMPDIR_PREFIX` in functional tests. The value previously included `PACKAGE_NAME`, which is cute, but doesn't explain why it was used (to include a space). So just use `test_common bitcoin` to achieve the same with less effort. ACKs for top commit: hodlinator: ACK fa84f9d ryanofsky: Code review ACK fa84f9d Tree-SHA512: eb35d6598bb08f9b996e3a4762d8f26b2441c0ca00780798e473015af735dfc9997120895a922b94d4b6ada45adadba4a686e9cf9c285ddf688848e764c64840
2 parents c3af4b1 + fa84f9d commit c0cbe26

File tree

5 files changed

+21
-14
lines changed

5 files changed

+21
-14
lines changed

src/test/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ build/src/test/test_bitcoin --run_test=getarg_tests/doubledash
6363
```
6464

6565
`test_bitcoin` creates a temporary working (data) directory with a randomly
66-
generated pathname within `test_common_Bitcoin Core/`, which in turn is within
66+
generated pathname within `test_common bitcoin/`, which in turn is within
6767
the system's temporary directory (see
6868
[`temp_directory_path`](https://en.cppreference.com/w/cpp/filesystem/temp_directory_path)).
6969
This data directory looks like a simplified form of the standard `bitcoind` data
@@ -73,7 +73,7 @@ have a `debug.log` file, for example.
7373
The location of the temporary data directory can be specified with the
7474
`-testdatadir` option. This can make debugging easier. The directory
7575
path used is the argument path appended with
76-
`/test_common_Bitcoin Core/<test-name>/datadir`.
76+
`/test_common bitcoin/<test-name>/datadir`.
7777
The directory path is created if necessary.
7878
Specifying this argument also causes the data directory
7979
not to be removed after the last test. This is useful for looking at
@@ -83,11 +83,11 @@ so no leftover state is used.)
8383

8484
```bash
8585
$ build/src/test/test_bitcoin --run_test=getarg_tests/doubledash -- -testdatadir=/somewhere/mydatadir
86-
Test directory (will not be deleted): "/somewhere/mydatadir/test_common_Bitcoin Core/getarg_tests/doubledash/datadir"
86+
Test directory (will not be deleted): "/somewhere/mydatadir/test_common bitcoin/getarg_tests/doubledash/datadir"
8787
Running 1 test case...
8888

8989
*** No errors detected
90-
$ ls -l '/somewhere/mydatadir/test_common_Bitcoin Core/getarg_tests/doubledash/datadir'
90+
$ ls -l '/somewhere/mydatadir/test_common bitcoin/getarg_tests/doubledash/datadir'
9191
total 8
9292
drwxrwxr-x 2 admin admin 4096 Nov 27 22:45 blocks
9393
-rw-rw-r-- 1 admin admin 1003 Nov 27 22:45 debug.log

src/test/util/random.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void SeedRandomStateForTest(SeedRand seedtype)
3636
return GetRandHash();
3737
}();
3838

39-
const uint256& seed{seedtype == SeedRand::SEED ? ctx_seed : uint256::ZERO};
39+
const uint256& seed{seedtype == SeedRand::FIXED_SEED ? ctx_seed : uint256::ZERO};
4040
LogInfo("Setting random seed for current tests to %s=%s\n", RANDOM_CTX_SEED, seed.GetHex());
4141
MakeRandDeterministicDANGEROUS(seed);
4242
}

src/test/util/random.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@
1212
#include <cstdint>
1313

1414
enum class SeedRand {
15-
ZEROS, //!< Seed with a compile time constant of zeros
16-
SEED, //!< Use (and report) random seed from environment, or a (truly) random one.
15+
/**
16+
* Seed with a compile time constant of zeros.
17+
*/
18+
ZEROS,
19+
/**
20+
* Seed with a fixed value that never changes over the lifetime of this
21+
* process. The seed is read from the RANDOM_CTX_SEED environment variable
22+
* if set, otherwise generated randomly once, saved, and reused.
23+
*/
24+
FIXED_SEED,
1725
};
1826

1927
/** Seed the global RNG state for testing and log the seed value. This affects all randomness, except GetStrongRandBytes(). */

src/test/util/setup_common.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#include <config/bitcoin-config.h> // IWYU pragma: keep
6-
75
#include <test/util/setup_common.h>
86

97
#include <addrman.h>
@@ -76,6 +74,7 @@ using node::VerifyLoadedChainstate;
7674

7775
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
7876

77+
constexpr inline auto TEST_DIR_PATH_ELEMENT{"test_common bitcoin"}; // Includes a space to catch possible path escape issues.
7978
/** Random context to get unique temp data dirs. Separate from m_rng, which can be seeded from a const env var */
8079
static FastRandomContext g_rng_temp_path;
8180

@@ -109,7 +108,7 @@ static NetworkSetup g_networksetup_instance;
109108
/** Register test-only arguments */
110109
static void SetupUnitTestArgs(ArgsManager& argsman)
111110
{
112-
argsman.AddArg("-testdatadir", strprintf("Custom data directory (default: %s<random_string>)", fs::PathToString(fs::temp_directory_path() / "test_common_" PACKAGE_NAME / "")),
111+
argsman.AddArg("-testdatadir", strprintf("Custom data directory (default: %s<random_string>)", fs::PathToString(fs::temp_directory_path() / TEST_DIR_PATH_ELEMENT / "")),
113112
ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
114113
}
115114

@@ -155,12 +154,12 @@ BasicTestingSetup::BasicTestingSetup(const ChainType chainType, TestOpts opts)
155154

156155
// Use randomly chosen seed for deterministic PRNG, so that (by default) test
157156
// data directories use a random name that doesn't overlap with other tests.
158-
SeedRandomForTest(SeedRand::SEED);
157+
SeedRandomForTest(SeedRand::FIXED_SEED);
159158

160159
if (!m_node.args->IsArgSet("-testdatadir")) {
161160
// By default, the data directory has a random name
162161
const auto rand_str{g_rng_temp_path.rand256().ToString()};
163-
m_path_root = fs::temp_directory_path() / "test_common_" PACKAGE_NAME / rand_str;
162+
m_path_root = fs::temp_directory_path() / TEST_DIR_PATH_ELEMENT / rand_str;
164163
TryCreateDirectories(m_path_root);
165164
} else {
166165
// Custom data directory
@@ -170,7 +169,7 @@ BasicTestingSetup::BasicTestingSetup(const ChainType chainType, TestOpts opts)
170169

171170
root_dir = fs::absolute(root_dir);
172171
const std::string test_path{G_TEST_GET_FULL_NAME ? G_TEST_GET_FULL_NAME() : ""};
173-
m_path_lock = root_dir / "test_common_" PACKAGE_NAME / fs::PathFromString(test_path);
172+
m_path_lock = root_dir / TEST_DIR_PATH_ELEMENT / fs::PathFromString(test_path);
174173
m_path_root = m_path_lock / "datadir";
175174

176175
// Try to obtain the lock; if unsuccessful don't disturb the existing test.

src/test/util/setup_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ struct BasicTestingSetup {
6868

6969
FastRandomContext m_rng;
7070
/** Seed the global RNG state and m_rng for testing and log the seed value. This affects all randomness, except GetStrongRandBytes(). */
71-
void SeedRandomForTest(SeedRand seed = SeedRand::SEED)
71+
void SeedRandomForTest(SeedRand seed)
7272
{
7373
SeedRandomStateForTest(seed);
7474
m_rng.Reseed(GetRandHash());

0 commit comments

Comments
 (0)