|
| 1 | +// Copyright 2025 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package inspect |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + gojson "encoding/json" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/cockroachdb/cockroach/pkg/base" |
| 15 | + "github.com/cockroachdb/cockroach/pkg/jobs/jobspb" |
| 16 | + "github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" |
| 17 | + "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" |
| 18 | + "github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" |
| 19 | + "github.com/cockroachdb/cockroach/pkg/util/leaktest" |
| 20 | + "github.com/cockroachdb/cockroach/pkg/util/log" |
| 21 | + "github.com/cockroachdb/redact" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | +) |
| 24 | + |
| 25 | +func TestTableSink(t *testing.T) { |
| 26 | + defer leaktest.AfterTest(t)() |
| 27 | + defer log.Scope(t).Close(t) |
| 28 | + |
| 29 | + s, db, _ := serverutils.StartServer(t, base.TestServerArgs{}) |
| 30 | + defer s.Stopper().Stop(context.Background()) |
| 31 | + runner := sqlutils.MakeSQLRunner(db) |
| 32 | + |
| 33 | + const jobID jobspb.JobID = 50 |
| 34 | + |
| 35 | + var tableLogger inspectLogger = &tableSink{ |
| 36 | + db: s.InternalDB().(descs.DB), |
| 37 | + jobID: jobID, |
| 38 | + } |
| 39 | + |
| 40 | + issue := inspectIssue{ |
| 41 | + ErrorType: MissingSecondaryIndexEntry, |
| 42 | + DatabaseID: 1, |
| 43 | + SchemaID: 2, |
| 44 | + ObjectID: 3, |
| 45 | + PrimaryKey: "key", |
| 46 | + Details: map[redact.RedactableString]interface{}{ |
| 47 | + "foo": `"bar"`, |
| 48 | + "biz": "\u2603baz", |
| 49 | + "titi": "toto\n", |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + require.NoError(t, tableLogger.logIssue(context.Background(), &issue)) |
| 54 | + |
| 55 | + // Query the system.inspect_errors table and expect one entry |
| 56 | + var count int |
| 57 | + runner.QueryRow(t, `SELECT count(*) FROM system.inspect_errors WHERE job_id = $1`, jobID).Scan(&count) |
| 58 | + require.Equal(t, 1, count, "Expected exactly one entry in system.inspect_errors") |
| 59 | + |
| 60 | + // Compare the entry against the test instance |
| 61 | + var actualJobID int64 |
| 62 | + var actualErrorType, actualPrimaryKey string |
| 63 | + var actualAOST time.Time |
| 64 | + var actualDatabaseID, actualSchemaID, actualObjectID int64 |
| 65 | + var actualDetailsBytes []byte |
| 66 | + |
| 67 | + runner.QueryRow(t, `SELECT job_id, error_type, aost, database_id, schema_id, id, primary_key, details |
| 68 | + FROM system.inspect_errors WHERE job_id = $1`, jobID).Scan( |
| 69 | + &actualJobID, &actualErrorType, &actualAOST, &actualDatabaseID, &actualSchemaID, |
| 70 | + &actualObjectID, &actualPrimaryKey, &actualDetailsBytes) |
| 71 | + |
| 72 | + require.Equal(t, int64(jobID), actualJobID, "job_id should match") |
| 73 | + require.Equal(t, string(issue.ErrorType), actualErrorType, "error_type should match") |
| 74 | + require.Equal(t, issue.AOST, actualAOST, "aost should match") |
| 75 | + require.Equal(t, int64(issue.DatabaseID), actualDatabaseID, "database_id should match") |
| 76 | + require.Equal(t, int64(issue.SchemaID), actualSchemaID, "schema_id should match") |
| 77 | + require.Equal(t, int64(issue.ObjectID), actualObjectID, "id should match") |
| 78 | + require.Equal(t, issue.PrimaryKey, actualPrimaryKey, "primary_key should match") |
| 79 | + |
| 80 | + var detailsMap map[string]interface{} |
| 81 | + err := gojson.Unmarshal(actualDetailsBytes, &detailsMap) |
| 82 | + require.NoError(t, err) |
| 83 | + require.Len(t, detailsMap, 3) |
| 84 | + require.Equal(t, `"bar"`, detailsMap["foo"], "details[foo] should match") |
| 85 | + require.Equal(t, "\u2603baz", detailsMap["biz"], "details[biz] should match") |
| 86 | + require.Equal(t, "toto\n", detailsMap["titi"], "details[titi] should match") |
| 87 | +} |
0 commit comments