Skip to content

Commit

Permalink
Fix the fixture to properly run the workflow exactly once for each pa…
Browse files Browse the repository at this point in the history
…ram.

Tested with extra logging in RunUpdateCorpusDatabase().

PiperOrigin-RevId: 724433749
  • Loading branch information
xinhaoyuan authored and copybara-github committed Feb 7, 2025
1 parent af84f4c commit 85e07aa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 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
41 changes: 23 additions & 18 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 UpdateCorpusDatabaseRunInfo {
std::unique_ptr<TempDir> workspace;
std::string std_err;
};

class UpdateCorpusDatabaseTest
: public ::testing::TestWithParam<ExecutionModelParam> {
protected:
Expand All @@ -74,36 +80,34 @@ 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_info_map_->contains(GetParam())) return;
auto &run_info = (*run_info_map_)[GetParam()];
run_info.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(
GetCorpusDatabaseTestingBinaryPath(), run_options);
*update_corpus_database_std_err_ = std::move(std_err);
const auto [status_unused, std_out_unused, std_err] =
RunBinaryMaybeWithCentipede(GetCorpusDatabaseTestingBinaryPath(),
run_options);
run_info.std_err = std::move(std_err);
}

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

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

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

static RunResults RunBinaryMaybeWithCentipede(absl::string_view binary_path,
Expand Down Expand Up @@ -135,13 +139,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, UpdateCorpusDatabaseRunInfo>>
run_info_map_;
};

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

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

0 comments on commit 85e07aa

Please sign in to comment.