-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test_utils: utility for getting a test directory
This adds a utility method to get the pathname for a directory that should be uniquely used by a single test case iteration.
- Loading branch information
Showing
5 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2024 Redpanda Data, Inc. | ||
* | ||
* Use of this software is governed by the Business Source License | ||
* included in the file licenses/BSL.md | ||
* | ||
* As of the Change Date specified in that file, in accordance with | ||
* the Business Source License, use of this software will be governed | ||
* by the Apache License, Version 2.0 | ||
*/ | ||
#include "test_utils/gtest_utils.h" | ||
|
||
#include "base/vassert.h" | ||
#include "random/generators.h" | ||
|
||
#include <seastar/core/lowres_clock.hh> | ||
|
||
#include <fmt/format.h> | ||
#include <gtest/gtest.h> | ||
|
||
namespace { | ||
int gtest_iteration = 0; | ||
} // anonymous namespace | ||
|
||
void rp_test_listener::OnTestIterationStart( | ||
const ::testing::UnitTest& /*unit_test*/, int iteration) { | ||
gtest_iteration = iteration; | ||
} | ||
|
||
ss::sstring get_test_directory() { | ||
const auto* test_info | ||
= ::testing::UnitTest::GetInstance()->current_test_info(); | ||
vassert(test_info != nullptr, "Must be a gtest!"); | ||
|
||
// The current timestamp uniquely identifies the process' test incantation, | ||
// and the test iteration uniquely identifies the individual runs of test | ||
// cases, e.g. in case of --gtest_repeat. | ||
// | ||
// This allows repeated test runs to operate independently without worrying | ||
// about leftover files from previous iterations. | ||
static auto now = ss::lowres_clock::now(); | ||
ss::sstring dir = fmt::format( | ||
"{}.{}.{}.{}", | ||
test_info->test_suite_name(), | ||
test_info->name(), | ||
now.time_since_epoch().count(), | ||
gtest_iteration); | ||
|
||
// Swap out any '/'s (may come from parameterized tests). | ||
std::replace(dir.begin(), dir.end(), '/', '_'); | ||
return dir; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2024 Redpanda Data, Inc. | ||
* | ||
* Use of this software is governed by the Business Source License | ||
* included in the file licenses/BSL.md | ||
* | ||
* As of the Change Date specified in that file, in accordance with | ||
* the Business Source License, use of this software will be governed | ||
* by the Apache License, Version 2.0 | ||
*/ | ||
#pragma once | ||
|
||
#include "base/seastarx.h" | ||
|
||
#include <seastar/core/seastar.hh> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
// Listens to the start of a gtest test iteration. May be used to track the | ||
// test iteration number. | ||
class rp_test_listener : public ::testing::EmptyTestEventListener { | ||
void | ||
OnTestIterationStart(const ::testing::UnitTest&, int iteration) override; | ||
}; | ||
|
||
// Returns a pathname that may be used for the currently running test. | ||
// Expects that the caller is in the context of a GTest. | ||
// | ||
// Relies on callers to create the directory. | ||
// | ||
// Examples: | ||
// | ||
// clang-format off | ||
// MySeastarFixture.TestGetTestDirectory.6125307633855650.4 | ||
// ^-- test suite ^-- test case ^-- timestamp ^-- iteration | ||
// | ||
// Divisible_MySeastarParamFixture.TestGetTestDirectory_0.6126774487959615.6 | ||
// ^-- parameter name ^-- parameter ^-- iteration | ||
// ^-- test suite ^-- test case ^-- timestamp | ||
// clang-format on | ||
ss::sstring get_test_directory(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters