vitess.io/vitess@v0.16.2/go/vt/wrangler/testlib/vtctl_topo_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package testlib
    18  
    19  import (
    20  	"context"
    21  	"os"
    22  	"path"
    23  	"strings"
    24  	"testing"
    25  
    26  	"google.golang.org/protobuf/proto"
    27  
    28  	"vitess.io/vitess/go/vt/topo/memorytopo"
    29  
    30  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    31  )
    32  
    33  func testVtctlTopoCommand(t *testing.T, vp *VtctlPipe, args []string, want string) {
    34  	got, err := vp.RunAndOutput(args)
    35  	if err != nil {
    36  		t.Fatalf("testVtctlTopoCommand(%v) failed: %v", args, err)
    37  	}
    38  
    39  	// Remove the variable version numbers.
    40  	lines := strings.Split(got, "\n")
    41  	for i, line := range lines {
    42  		if vi := strings.Index(line, "version="); vi != -1 {
    43  			lines[i] = line[:vi+8] + "V"
    44  		}
    45  	}
    46  	got = strings.Join(lines, "\n")
    47  	if got != want {
    48  		t.Errorf("testVtctlTopoCommand(%v) failed: got:\n%vwant:\n%v", args, got, want)
    49  	}
    50  }
    51  
    52  // TestVtctlTopoCommands tests all vtctl commands from the
    53  // "Topo" group.
    54  func TestVtctlTopoCommands(t *testing.T) {
    55  	ts := memorytopo.NewServer("cell1", "cell2")
    56  	if err := ts.CreateKeyspace(context.Background(), "ks1", &topodatapb.Keyspace{KeyspaceType: topodatapb.KeyspaceType_NORMAL}); err != nil {
    57  		t.Fatalf("CreateKeyspace() failed: %v", err)
    58  	}
    59  	if err := ts.CreateKeyspace(context.Background(), "ks2", &topodatapb.Keyspace{KeyspaceType: topodatapb.KeyspaceType_SNAPSHOT}); err != nil {
    60  		t.Fatalf("CreateKeyspace() failed: %v", err)
    61  	}
    62  	vp := NewVtctlPipe(t, ts)
    63  	defer vp.Close()
    64  
    65  	tmp := t.TempDir()
    66  
    67  	// Test TopoCat.
    68  	testVtctlTopoCommand(t, vp, []string{"TopoCat", "--long", "--decode_proto", "/keyspaces/*/Keyspace"}, `path=/keyspaces/ks1/Keyspace version=V
    69  path=/keyspaces/ks2/Keyspace version=V
    70  keyspace_type:SNAPSHOT
    71  `)
    72  
    73  	// Test TopoCp from topo to disk.
    74  	ksFile := path.Join(tmp, "Keyspace")
    75  	_, err := vp.RunAndOutput([]string{"TopoCp", "/keyspaces/ks1/Keyspace", ksFile})
    76  	if err != nil {
    77  		t.Fatalf("TopoCp(/keyspaces/ks1/Keyspace) failed: %v", err)
    78  	}
    79  	contents, err := os.ReadFile(ksFile)
    80  	if err != nil {
    81  		t.Fatalf("copy failed: %v", err)
    82  	}
    83  	expected := &topodatapb.Keyspace{KeyspaceType: topodatapb.KeyspaceType_NORMAL}
    84  	got := &topodatapb.Keyspace{}
    85  	if err = proto.Unmarshal(contents, got); err != nil {
    86  		t.Fatalf("bad keyspace data %v", err)
    87  	}
    88  	if !proto.Equal(got, expected) {
    89  		t.Fatalf("bad proto data: Got %v expected %v", got, expected)
    90  	}
    91  
    92  	// Test TopoCp from disk to topo.
    93  	_, err = vp.RunAndOutput([]string{"TopoCp", "--to_topo", ksFile, "/keyspaces/ks3/Keyspace"})
    94  	if err != nil {
    95  		t.Fatalf("TopoCp(/keyspaces/ks3/Keyspace) failed: %v", err)
    96  	}
    97  	ks3, err := ts.GetKeyspace(context.Background(), "ks3")
    98  	if err != nil {
    99  		t.Fatalf("copy from disk to topo failed: %v", err)
   100  	}
   101  	if !proto.Equal(ks3.Keyspace, expected) {
   102  		t.Fatalf("copy data to topo failed, got %v expected %v", ks3.Keyspace, expected)
   103  	}
   104  }