diff --git a/pkg/cli/debug_merge_logs.go b/pkg/cli/debug_merge_logs.go index 52bb42bce647..38ef8ed1b62d 100644 --- a/pkg/cli/debug_merge_logs.go +++ b/pkg/cli/debug_merge_logs.go @@ -499,9 +499,8 @@ func (s *fileLogStream) open() bool { if s.err = seekToFirstAfterFrom(s.f, s.from, s.editMode, s.format); s.err != nil { return false } - var err error - if s.d, err = log.NewEntryDecoderWithFormat(bufio.NewReaderSize(s.f, readBufSize), s.editMode, s.format); err != nil { - panic(err) + if s.d, s.err = log.NewEntryDecoderWithFormat(bufio.NewReaderSize(s.f, readBufSize), s.editMode, s.format); s.err != nil { + return false } return true } diff --git a/pkg/cli/debug_merge_logs_test.go b/pkg/cli/debug_merge_logs_test.go index a26b68ac1b0d..ff90513d595f 100644 --- a/pkg/cli/debug_merge_logs_test.go +++ b/pkg/cli/debug_merge_logs_test.go @@ -12,6 +12,7 @@ package cli import ( "bytes" + "fmt" "io/ioutil" "path/filepath" "testing" @@ -184,14 +185,22 @@ func getCases(format string) []testCase { } } -func (c testCase) run(t *testing.T) { - outBuf := bytes.Buffer{} +func resetDebugMergeLogFlags(errorFn func(s string)) { debugMergeLogsCmd.Flags().Visit(func(f *pflag.Flag) { if err := f.Value.Set(f.DefValue); err != nil { - t.Fatalf("Failed to set flag to default: %v", err) + errorFn(fmt.Sprintf("Failed to set flag to default: %v", err)) } }) +} + +func (c testCase) run(t *testing.T) { + resetDebugMergeLogFlags(func(s string) { t.Fatal(s) }) + outBuf := bytes.Buffer{} debugMergeLogsCmd.SetOut(&outBuf) + // Ensure that the original writer is restored when the test + // completes. Otherwise, subsequent tests may not see their output. + defer debugMergeLogsCmd.SetOut(nil) + if err := debugMergeLogsCmd.ParseFlags(c.flags); err != nil { t.Fatalf("Failed to set flags: %v", err) } @@ -235,3 +244,16 @@ func TestCrdbV1DebugMergeLogs(t *testing.T) { t.Run(c.name, c.run) } } + +func Example_format_error() { + c := NewCLITest(TestCLIParams{NoServer: true}) + defer c.Cleanup() + + resetDebugMergeLogFlags(func(s string) { fmt.Fprintf(stderr, "ERROR: %v", s) }) + + c.RunWithArgs([]string{"debug", "merge-logs", "testdata/merge_logs_v1/missing_format/*"}) + + // Output: + // debug merge-logs testdata/merge_logs_v1/missing_format/* + // ERROR: decoding format: failed to extract log file format from the log +} diff --git a/pkg/cli/testdata/merge_logs_v1/missing_format/cockroach.test-0001.ubuntu.2018-11-30T22_06_47Z.004130.log b/pkg/cli/testdata/merge_logs_v1/missing_format/cockroach.test-0001.ubuntu.2018-11-30T22_06_47Z.004130.log new file mode 100644 index 000000000000..47ec4e95eca4 --- /dev/null +++ b/pkg/cli/testdata/merge_logs_v1/missing_format/cockroach.test-0001.ubuntu.2018-11-30T22_06_47Z.004130.log @@ -0,0 +1,3 @@ +I210801 21:05:59.364923 1 util/log/sync_buffer.go:70 [config] binary: CockroachDB CCL v20.1.17 (x86_64-apple-darwin14, built 2021/05/17 16:30:22, +I210801 21:05:59.364923 1 util/log/sync_buffer.go:70 [config] arguments: [./cockroach start] +I210801 21:05:59.364923 1 util/log/sync_buffer.go:70 line format: [IWEF]yymmdd hh:mm:ss.uuuuuu goid file:line msg utf8=✓