Skip to content

Commit

Permalink
Fix the fixture to run the workflow exactly once for each param.
Browse files Browse the repository at this point in the history
Previously it was run once globally.

Tested with extra logging in RunUpdateCorpusDatabase().

PiperOrigin-RevId: 725711084
  • Loading branch information
xinhaoyuan authored and copybara-github committed Feb 11, 2025
1 parent e111969 commit 58dda9b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
1 change: 1 addition & 0 deletions e2e_tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ cc_test(
deps = [
":test_binary_util",
"@com_google_absl//absl/base:no_destructor",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
Expand Down
38 changes: 21 additions & 17 deletions e2e_tests/corpus_database_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <csignal>
#include <filesystem> // NOLINT
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/base/no_destructor.h"
#include "absl/container/flat_hash_map.h"
#include "absl/log/check.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
Expand Down Expand Up @@ -60,6 +61,11 @@ enum class ExecutionModelParam {
kWithCentipedeBinary,
};

struct UpdateCorpusDatabaseRun {
std::unique_ptr<TempDir> workspace;
std::string std_err;
};

class UpdateCorpusDatabaseTest
: public ::testing::TestWithParam<ExecutionModelParam> {
protected:
Expand All @@ -74,36 +80,33 @@ class UpdateCorpusDatabaseTest
"Please run with --config=fuzztest-experimental.";
#endif
#endif
CHECK(temp_dir_ == nullptr);
}

static void RunUpdateCorpusDatabase() {
if (temp_dir_ != nullptr) return;
temp_dir_ = new TempDir();
if (run_map_->contains(GetParam())) return;
auto &run = (*run_map_)[GetParam()];
run.workspace = std::make_unique<TempDir>();
RunOptions run_options;
run_options.fuzztest_flags = {
{"corpus_database", GetCorpusDatabasePath()},
{"fuzz_for", "30s"},
{"jobs", "2"},
};
auto [status, std_out, std_err] = RunBinaryMaybeWithCentipede(
auto [status_unused, std_out_unused, std_err] = RunBinaryMaybeWithCentipede(
GetCorpusDatabaseTestingBinaryPath(), run_options);
*update_corpus_database_std_err_ = std::move(std_err);
run.std_err = std::move(std_err);
}

static void TearDownTestSuite() {
delete temp_dir_;
temp_dir_ = nullptr;
}
static void TearDownTestSuite() { run_map_->clear(); }

static std::string GetCorpusDatabasePath() {
RunUpdateCorpusDatabase();
return temp_dir_->path() / "corpus_database";
return (*run_map_)[GetParam()].workspace->path() / "corpus_database";
}

static absl::string_view GetUpdateCorpusDatabaseStdErr() {
RunUpdateCorpusDatabase();
return *update_corpus_database_std_err_;
return (*run_map_)[GetParam()].std_err;
}

static RunResults RunBinaryMaybeWithCentipede(absl::string_view binary_path,
Expand Down Expand Up @@ -135,13 +138,14 @@ class UpdateCorpusDatabaseTest
}

private:
static TempDir *temp_dir_;
static absl::NoDestructor<std::string> update_corpus_database_std_err_;
static absl::NoDestructor<
absl::flat_hash_map<ExecutionModelParam, UpdateCorpusDatabaseRun>>
run_map_;
};

TempDir *UpdateCorpusDatabaseTest::temp_dir_ = nullptr;
absl::NoDestructor<std::string>
UpdateCorpusDatabaseTest::update_corpus_database_std_err_{};
absl::NoDestructor<
absl::flat_hash_map<ExecutionModelParam, UpdateCorpusDatabaseRun>>
UpdateCorpusDatabaseTest::run_map_{};

TEST_P(UpdateCorpusDatabaseTest, RunsFuzzTests) {
EXPECT_THAT(GetUpdateCorpusDatabaseStdErr(),
Expand Down

0 comments on commit 58dda9b

Please sign in to comment.