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

     1  // Copyright 2018 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 cli
    12  
    13  import (
    14  	"path/filepath"
    15  	"strings"
    16  	"testing"
    17  
    18  	"github.com/cockroachdb/cockroach/pkg/testutils"
    19  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    20  )
    21  
    22  func Example_debug_decode_key_value() {
    23  	cliTest{}.RunWithArgs([]string{"debug", "decode-value", "016b12bd8980c0b6c2e211ba5182000172647363", "884d186d03089b09120bbd8980c0b6c2e211ba51821a0bbd8980c0b9e7c5c610e99622060801100118012206080410041802220608021002180428053004"})
    24  
    25  	// Output:
    26  	// debug decode-value 016b12bd8980c0b6c2e211ba5182000172647363 884d186d03089b09120bbd8980c0b6c2e211ba51821a0bbd8980c0b9e7c5c610e99622060801100118012206080410041802220608021002180428053004
    27  	// unable to decode key: invalid encoded mvcc key: 016b12bd8980c0b6c2e211ba5182000172647363, assuming it's a roachpb.Key with fake timestamp;
    28  	// if the result below looks like garbage, then it likely is:
    29  	//
    30  	// 0.987654321,0 /Local/Range/Table/53/1/-4560243296450227838/RangeDescriptor (0x016b12bd8980c0b6c2e211ba518200017264736300000000003ade68b109): [/Table/53/1/-4560243296450227838, /Table/53/1/-4559358311118345834)
    31  	// 	Raw:r1179:/Table/53/1/-45{60243296450227838-59358311118345834} [(n1,s1):1, (n4,s4):2, (n2,s2):4, next=5, gen=4]
    32  }
    33  
    34  func TestDebugKeysHex(t *testing.T) {
    35  	defer leaktest.AfterTest(t)()
    36  
    37  	baseDir, dirCleanupFn := testutils.TempDir(t)
    38  	defer dirCleanupFn()
    39  
    40  	storePath := filepath.Join(baseDir, "store")
    41  	createStore(t, storePath)
    42  
    43  	{
    44  		out, err := cliTest{}.RunWithCapture("debug keys " + storePath +
    45  			" --from hex:016b12bd898090d79e52e79b0144000174786e2d733fb094e9aa4d67974c71486b9823b900" +
    46  			" --to   hex:016b12bd898090d79e52e79b0144000174786e2d733fb094e9aa4d67974c71486b9823b900")
    47  		if err != nil {
    48  			t.Fatal(err)
    49  		}
    50  		// Should just output the command invocation and no results.
    51  		if !strings.HasSuffix(strings.TrimSpace(out), "b900") {
    52  			t.Fatalf("%q", out)
    53  		}
    54  	}
    55  
    56  	// Test invalid key, verify we get a good suggestion back.
    57  	out, err := cliTest{}.RunWithCapture("debug keys " + storePath +
    58  		" --to hex:01")
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	expOut := `invalid argument "hex:01" for "--to" flag: perhaps this is just a hex-encoded key; ` +
    63  		`you need an encoded MVCCKey (i.e. with a timestamp component); here's one with a zero timestamp: ` +
    64  		`0100: invalid encoded mvcc key: 01`
    65  	if !strings.Contains(out, expOut) {
    66  		t.Fatalf("%q", out)
    67  	}
    68  }