From 2bb3e63c0daa1497f2f9802e55dc22dd268dbb8e Mon Sep 17 00:00:00 2001 From: changhan Date: Wed, 23 Mar 2022 19:24:35 +0900 Subject: [PATCH] sql: Simplify expected message requirement in TestAnonymizeStatementsForReporting Release justification: Low impact, simplify expected message in assertion Release note (sql change): Change the equality assertion on the redacted error message string returned by sql.WithAnonymizedStatement to target only the prefix containing the error message with the redacted SQL query. Previously equality was required against the entire stack trace which can refer to architecture-specific assembly files. --- pkg/sql/conn_executor_test.go | 37 ++++++++++------------------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/pkg/sql/conn_executor_test.go b/pkg/sql/conn_executor_test.go index a3ec4c9683ae..77ac210a21f1 100644 --- a/pkg/sql/conn_executor_test.go +++ b/pkg/sql/conn_executor_test.go @@ -16,7 +16,6 @@ import ( "database/sql/driver" "fmt" "net/url" - "regexp" "strings" "sync/atomic" "testing" @@ -91,32 +90,18 @@ INSERT INTO sensitive(super, sensible) VALUES('that', 'nobody', 'must', 'see') t.Errorf("wanted: %s\ngot: %s", expMessage, actMessage) } - const expSafeRedactedMessage = `some error -(1) while executing: INSERT INTO _(_, _) VALUES ('_', '_', __more2__) -Wraps: (2) attached stack trace - -- stack trace: - | github.com/cockroachdb/cockroach/pkg/sql_test.TestAnonymizeStatementsForReporting - | ...conn_executor_test.go:NN - | testing.tRunner - | ...testing.go:NN - | runtime.goexit - | ...asm_scrubbed.s:NN -Wraps: (3) some error -Error types: (1) *safedetails.withSafeDetails (2) *withstack.withStack (3) *errutil.leafError` - - // Edit non-determinstic stack trace filenames from the message. - actSafeRedactedMessage := strings.ReplaceAll(strings.ReplaceAll(fileref.ReplaceAllString( - redact.Sprintf("%+v", safeErr).Redact().StripMarkers(), "...$2:NN"), - "asm_arm64", "asm_scrubbed"), - "asm_amd64", "asm_scrubbed") - - if actSafeRedactedMessage != expSafeRedactedMessage { + const expSafeRedactedMsgPrefix = `some error +(1) while executing: INSERT INTO _(_, _) VALUES ('_', '_', __more2__)` + + actSafeRedactedMessage := string(redact.Sprintf("%+v", safeErr)) + + if !strings.HasPrefix(actSafeRedactedMessage, expSafeRedactedMsgPrefix) { diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{ - A: difflib.SplitLines(expSafeRedactedMessage), - B: difflib.SplitLines(actSafeRedactedMessage), - FromFile: "Expected", + A: difflib.SplitLines(expSafeRedactedMsgPrefix), + B: difflib.SplitLines(actSafeRedactedMessage[:len(expSafeRedactedMsgPrefix)]), + FromFile: "Expected Message Prefix", FromDate: "", - ToFile: "Actual", + ToFile: "Actual Message Prefix", ToDate: "", Context: 1, }) @@ -124,8 +109,6 @@ Error types: (1) *safedetails.withSafeDetails (2) *withstack.withStack (3) *erru } } -var fileref = regexp.MustCompile(`((?:[a-zA-Z0-9\._@-]*/)*)([a-zA-Z0-9._@-]*\.(?:go|s)):\d+`) - // Test that a connection closed abruptly while a SQL txn is in progress results // in that txn being rolled back. //