github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/storage/error_test.go (about)

     1  // Copyright 2017 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package storage
    12  
    13  import (
    14  	"testing"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/base"
    17  	"github.com/cockroachdb/cockroach/pkg/settings/cluster"
    18  	"github.com/cockroachdb/cockroach/pkg/testutils"
    19  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    20  	"github.com/cockroachdb/errors"
    21  )
    22  
    23  // TestRocksDBErrorSafeMessage verifies that RocksDB errors have a chance of
    24  // being reported safely.
    25  func TestRocksDBErrorSafeMessage(t *testing.T) {
    26  	defer leaktest.AfterTest(t)()
    27  	dir, dirCleanup := testutils.TempDir(t)
    28  	defer dirCleanup()
    29  
    30  	open := func() (*RocksDB, error) {
    31  		return NewRocksDB(
    32  			RocksDBConfig{
    33  				StorageConfig: base.StorageConfig{
    34  					Settings: cluster.MakeTestingClusterSettings(),
    35  					Dir:      dir,
    36  				},
    37  			},
    38  			RocksDBCache{},
    39  		)
    40  	}
    41  
    42  	// Provoke a RocksDB error by opening two instances for the same directory.
    43  	r1, err := open()
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  	defer r1.Close()
    48  	r2, err := open()
    49  	if err == nil {
    50  		defer r2.Close()
    51  		t.Fatal("expected error")
    52  	}
    53  	var rErr *Error
    54  	if !errors.As(err, &rErr) {
    55  		t.Fatalf("unexpected error: %+v", err)
    56  	}
    57  
    58  	for _, test := range []struct {
    59  		err    *Error
    60  		expMsg string
    61  	}{
    62  		{
    63  			err: rErr,
    64  			// "locks" is redacted because this last part of the message is actually from `strerror` (ANSI-C).
    65  			expMsg: "io error lock <redacted> <redacted> no <redacted> available",
    66  		},
    67  		{
    68  			// A real-world example.
    69  			err: &Error{
    70  				msg: "Corruption: block checksum mismatch: expected 4187431493, got 3338436330  " +
    71  					"in /home/agent/activerecord-cockroachdb-adapter/cockroach-data/000012.sst " +
    72  					"offset 59661 size 7425",
    73  			},
    74  			expMsg: "corruption block checksum mismatch expected <redacted> got <redacted> in <redacted> offset <redacted> size <redacted>",
    75  		},
    76  		{
    77  			// An example that shows that paths containing dictionary words are still redacted.
    78  			err: &Error{
    79  				msg: "Corruption: block checksum mismatch in /block C:\\checksum /mismatch/corruption/Corruption C:\\checksum\\corruption",
    80  			},
    81  			expMsg: "corruption block checksum mismatch in <redacted> <redacted> <redacted> <redacted>",
    82  		},
    83  	} {
    84  		if act := test.err.SafeMessage(); act != test.expMsg {
    85  			t.Errorf("expected %q, got %q\nfrom original error %v", test.expMsg, act, test.err)
    86  		}
    87  	}
    88  }