#include #include #include #include #include #include #include namespace { using namespace Void::Testing; } // namespace Void::Testing::Test testing_fault_file_parses_valid_specs() { constexpr std::string_view text = "# storage fault plan\n" "\n" "sync_file 1 error code=EIO\n" "append 2 partial partial=16\n" "rename 1 pause # block once\n"; auto specs = parse_fault_file(text); Void::Testing::require(specs.has_value()); Void::Testing::expect_eq(specs->size(), std::size_t{3}); Void::Testing::expect_eq((*specs)[0].boundary, std::string{"sync_file"}); Void::Testing::expect_eq((*specs)[0].hit_index, std::uint64_t{1}); Void::Testing::expect((*specs)[0].action == FaultAction::Error); Void::Testing::expect_eq((*specs)[0].error_code, std::string{"EIO"}); Void::Testing::expect((*specs)[1].action == FaultAction::PartialWrite); Void::Testing::require((*specs)[1].partial_bytes.has_value()); Void::Testing::expect_eq(*(*specs)[1].partial_bytes, std::size_t{16}); Void::Testing::expect((*specs)[2].action == FaultAction::Pause); } Void::Testing::Test testing_fault_file_rejects_invalid_input() { const auto rejects = [](std::string_view text, FaultFileLimits limits = {}) { return !parse_fault_file(text, limits).has_value(); }; Void::Testing::expect(rejects("sync_file 1")); // missing action Void::Testing::expect(rejects("sync_file 0 error")); // hit 0 Void::Testing::expect(rejects("sync_file x error")); // bad hit Void::Testing::expect(rejects("sync_file 1 explode")); // unknown action Void::Testing::expect(rejects("sync_file 1 partial")); // no size Void::Testing::expect(rejects("sync_file 1 error partial=x")); // bad size Void::Testing::expect(rejects("sync_file 1 error nope=1")); // unknown field Void::Testing::expect(rejects("bad name 1 error")); // invalid boundary Void::Testing::expect( rejects("sync_file 1 error\nsync_file 1 error")); // duplicate Void::Testing::expect( rejects("a 1 error\nb 1 error", FaultFileLimits{.max_entries = 1})); Void::Testing::expect(rejects(std::string(128, 'a') + " 1 error", FaultFileLimits{.max_bytes = 32})); } Void::Testing::Test testing_fault_plan_fires_by_hit_index_and_reconciles() { auto specs = parse_fault_file( "io 1 error code=EAGAIN\n" "io 3 partial partial=8\n" "sync 2 pause\n"); Void::Testing::require(specs.has_value()); FaultPlan plan{std::move(*specs)}; // io occurrence 1 fires the error. auto first = plan.check("io"); Void::Testing::expect(first.triggered); Void::Testing::expect(first.action == FaultAction::Error); Void::Testing::expect_eq(first.error_code, std::string{"EAGAIN"}); // Occurrence 2 does not fire; occurrence 3 fires the partial write. Void::Testing::expect(!plan.check("io").triggered); auto third = plan.check("io"); Void::Testing::expect(third.triggered); Void::Testing::expect(third.action == FaultAction::PartialWrite); Void::Testing::require(third.partial_bytes.has_value()); Void::Testing::expect_eq(*third.partial_bytes, std::size_t{8}); // Each spec fires once. Void::Testing::expect(!plan.check("io").triggered); Void::Testing::expect_eq(plan.occurrences("io"), std::size_t{4}); // sync fires on its second occurrence. Void::Testing::expect(!plan.check("sync").triggered); Void::Testing::expect(plan.check("sync").triggered); auto totals = plan.totals(); Void::Testing::expect_eq(totals.configured, std::size_t{3}); Void::Testing::expect_eq(totals.fired, std::size_t{3}); Void::Testing::expect_eq(totals.remaining, std::size_t{0}); Void::Testing::expect_eq(totals.configured, totals.fired + totals.remaining); } Void::Testing::Test testing_fault_plan_bounds_boundary_names() { FaultPlan plan{{FaultSpec{"a", 1, FaultAction::Error, std::nullopt, {}}, FaultSpec{"b", 1, FaultAction::Error, std::nullopt, {}}}, 1}; // Only the first boundary is retained. Void::Testing::expect(plan.check("a").triggered); Void::Testing::expect(!plan.check("b").triggered); Void::Testing::expect_eq(plan.totals().configured, std::size_t{1}); }