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

     1  // Copyright 2019 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 sql_test
    12  
    13  import (
    14  	"context"
    15  	"fmt"
    16  	"strings"
    17  	"testing"
    18  
    19  	"github.com/cockroachdb/cockroach/pkg/base"
    20  	"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
    21  	"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
    22  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    23  )
    24  
    25  func TestShowRangesWithLocality(t *testing.T) {
    26  	defer leaktest.AfterTest(t)()
    27  
    28  	const numNodes = 3
    29  	ctx := context.Background()
    30  	tc := testcluster.StartTestCluster(t, numNodes, base.TestClusterArgs{})
    31  	defer tc.Stopper().Stop(ctx)
    32  
    33  	sqlDB := sqlutils.MakeSQLRunner(tc.Conns[0])
    34  	sqlDB.Exec(t, `CREATE TABLE t (x INT PRIMARY KEY)`)
    35  	sqlDB.Exec(t, `ALTER TABLE t SPLIT AT SELECT i FROM generate_series(0, 20) AS g(i)`)
    36  
    37  	const leaseHolderIdx = 0
    38  	const leaseHolderLocalityIdx = 1
    39  	const replicasColIdx = 2
    40  	const localitiesColIdx = 3
    41  	replicas := make([]int, 3)
    42  
    43  	// TestClusters get some localities by default.
    44  	q := `SELECT lease_holder, lease_holder_locality, replicas, replica_localities from [SHOW RANGES FROM TABLE t]`
    45  	result := sqlDB.QueryStr(t, q)
    46  	for _, row := range result {
    47  		// Verify the leaseholder localities.
    48  		leaseHolder := row[leaseHolderIdx]
    49  		leaseHolderLocalityExpected := fmt.Sprintf(`region=test,dc=dc%s`, leaseHolder)
    50  		if row[leaseHolderLocalityIdx] != leaseHolderLocalityExpected {
    51  			t.Fatalf("expected %s found %s", leaseHolderLocalityExpected, row[leaseHolderLocalityIdx])
    52  		}
    53  
    54  		// Verify the replica localities.
    55  		_, err := fmt.Sscanf(row[replicasColIdx], "{%d,%d,%d}", &replicas[0], &replicas[1], &replicas[2])
    56  		if err != nil {
    57  			t.Fatal(err)
    58  		}
    59  		var builder strings.Builder
    60  		builder.WriteString("{")
    61  		for i, replica := range replicas {
    62  			builder.WriteString(fmt.Sprintf(`"region=test,dc=dc%d"`, replica))
    63  			if i != len(replicas)-1 {
    64  				builder.WriteString(",")
    65  			}
    66  		}
    67  		builder.WriteString("}")
    68  		expected := builder.String()
    69  		if row[localitiesColIdx] != expected {
    70  			t.Fatalf("expected %s found %s", expected, row[localitiesColIdx])
    71  		}
    72  	}
    73  }