From 0eab1108914527764537803f2d017e4c8127e98c Mon Sep 17 00:00:00 2001 From: Yahor Yuzefovich Date: Mon, 14 Aug 2023 13:47:47 -0700 Subject: [PATCH] roachtest/costfuzz: create failure.log file on demand This commit makes it so that `failure.log` files are created lazily on demand in `costfuzz` and `unoptimized_query_oracle` roachtests. It seems nicer this way since for successful runs previously we'd see empty failure log files which were a bit confusing. Epic: None Release note: None --- .../roachtest/tests/query_comparison_util.go | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/pkg/cmd/roachtest/tests/query_comparison_util.go b/pkg/cmd/roachtest/tests/query_comparison_util.go index a7b6c4671b11..82ff3f97c213 100644 --- a/pkg/cmd/roachtest/tests/query_comparison_util.go +++ b/pkg/cmd/roachtest/tests/query_comparison_util.go @@ -158,15 +158,23 @@ func runOneRoundQueryComparison( t.L().Printf("\n\n") } - failureLogPath := filepath.Join( - t.ArtifactsDir(), fmt.Sprintf("%s%03d.failure.log", qct.name, iter), - ) - failureLog, err := os.Create(failureLogPath) - if err != nil { - t.Fatalf("could not create %s%03d.failure.log: %v", qct.name, iter, err) - } - defer failureLog.Close() + // We will create the failure log file on demand. + var failureLog *os.File + defer func() { + if failureLog != nil { + _ = failureLog.Close() + } + }() logFailure := func(stmt string, rows [][]string) { + if failureLog == nil { + failureLogName := fmt.Sprintf("%s%03d.failure.log", qct.name, iter) + failureLogPath := filepath.Join(t.ArtifactsDir(), failureLogName) + var err error + failureLog, err = os.Create(failureLogPath) + if err != nil { + t.Fatalf("could not create %s: %v", failureLogName, err) + } + } fmt.Fprint(failureLog, stmt) fmt.Fprint(failureLog, "\n----\n") fmt.Fprint(failureLog, sqlutils.MatrixToStr(rows))