From 107320604308bf8fe420b9449521ebce72a9113b Mon Sep 17 00:00:00 2001 From: Matt Lord Date: Fri, 17 Jan 2025 12:54:38 -0500 Subject: [PATCH] Reorgnaize the permissions tests Signed-off-by: Matt Lord --- .../vtctldclient/command/framework_test.go} | 23 ++++++++----------- .../vtctldclient/command/permissions_test.go | 7 +++--- 2 files changed, 13 insertions(+), 17 deletions(-) rename go/{vt/vtctl/workflow/testlib/fake_tablet.go => cmd/vtctldclient/command/framework_test.go} (92%) diff --git a/go/vt/vtctl/workflow/testlib/fake_tablet.go b/go/cmd/vtctldclient/command/framework_test.go similarity index 92% rename from go/vt/vtctl/workflow/testlib/fake_tablet.go rename to go/cmd/vtctldclient/command/framework_test.go index 4ab5603c807..3e4a6753411 100644 --- a/go/vt/vtctl/workflow/testlib/fake_tablet.go +++ b/go/cmd/vtctldclient/command/framework_test.go @@ -14,11 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -/* -Package testlib contains utility methods to include in unit tests to -deal with topology common tasks, like fake tablets and action loops. -*/ -package testlib +package command import ( "context" @@ -27,6 +23,7 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" "google.golang.org/grpc" "vitess.io/vitess/go/mysql/fakesqldb" @@ -54,7 +51,7 @@ import ( ) func init() { - tabletconntest.SetProtocol("go.vt.vtctl.workflow.testlib", "grpc") + tabletconntest.SetProtocol("go.cmd.vtctldclient.command", "grpc") } // This file contains utility methods for unit tests. @@ -97,7 +94,7 @@ func TabletKeyspaceShard(t *testing.T, keyspace, shard string) TabletOption { tablet.Keyspace = keyspace shard, kr, err := topo.ValidateShardName(shard) if err != nil { - t.Fatalf("cannot ValidateShardName value %v", shard) + require.FailNow(t, "cannot ValidateShardName value %v", shard) } tablet.Shard = shard tablet.KeyRange = kr @@ -130,7 +127,7 @@ func NewFakeTablet(t *testing.T, ts *topo.Server, cell string, uid uint32, table t.Helper() if uid > 99 { - t.Fatalf("uid has to be between 0 and 99: %v", uid) + require.FailNow(t, "uid has to be between 0 and 99: %v", uid) } mysqlPort := int32(3300 + uid) tablet := &topodatapb.Tablet{ @@ -154,7 +151,7 @@ func NewFakeTablet(t *testing.T, ts *topo.Server, cell string, uid uint32, table _, force := tablet.PortMap["force_init"] delete(tablet.PortMap, "force_init") if err := ts.InitTablet(context.Background(), tablet, force, true /* createShardAndKeyspace */, false /* allowUpdate */); err != nil { - t.Fatalf("cannot create tablet %v: %v", uid, err) + require.FailNow(t, "cannot create tablet %v: %v", uid, err) } // create a FakeMysqlDaemon with the right information by default. @@ -174,14 +171,14 @@ func NewFakeTablet(t *testing.T, ts *topo.Server, cell string, uid uint32, table func (ft *FakeTablet) StartActionLoop(t *testing.T, ts *topo.Server) { t.Helper() if ft.TM != nil { - t.Fatalf("TM for %v is already running", ft.Tablet.Alias) + require.FailNow(t, "TM for %v is already running", ft.Tablet.Alias) } // Listen on a random port for gRPC. var err error ft.Listener, err = net.Listen("tcp", "127.0.0.1:0") if err != nil { - t.Fatalf("Cannot listen: %v", err) + require.FailNow(t, "Cannot listen: %v", err) } gRPCPort := int32(ft.Listener.Addr().(*net.TCPAddr).Port) @@ -190,7 +187,7 @@ func (ft *FakeTablet) StartActionLoop(t *testing.T, ts *topo.Server) { if ft.StartHTTPServer { ft.HTTPListener, err = net.Listen("tcp", "127.0.0.1:0") if err != nil { - t.Fatalf("Cannot listen on http port: %v", err) + require.FailNow(t, "Cannot listen on http port: %v", err) } handler := http.NewServeMux() ft.HTTPServer = &http.Server{ @@ -217,7 +214,7 @@ func (ft *FakeTablet) StartActionLoop(t *testing.T, ts *topo.Server) { Env: vtenv.NewTestEnv(), } if err := ft.TM.Start(ft.Tablet, nil); err != nil { - t.Fatalf("Error in tablet - %v, err - %v", topoproto.TabletAliasString(ft.Tablet.Alias), err.Error()) + require.FailNow(t, "Error in tablet - %v, err - %v", topoproto.TabletAliasString(ft.Tablet.Alias), err.Error()) } ft.Tablet = ft.TM.Tablet() diff --git a/go/cmd/vtctldclient/command/permissions_test.go b/go/cmd/vtctldclient/command/permissions_test.go index fc010e9d444..a9ddba36f7f 100644 --- a/go/cmd/vtctldclient/command/permissions_test.go +++ b/go/cmd/vtctldclient/command/permissions_test.go @@ -1,5 +1,5 @@ /* -Copyright 2019 The Vitess Authors. +Copyright 2025 The Vitess Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -26,7 +26,6 @@ import ( "vitess.io/vitess/go/vt/discovery" "vitess.io/vitess/go/vt/topo/topoproto" "vitess.io/vitess/go/vt/vtctl/grpcvtctldserver" - "vitess.io/vitess/go/vt/vtctl/workflow/testlib" "vitess.io/vitess/go/vt/vtenv" "vitess.io/vitess/go/sqltypes" @@ -52,8 +51,8 @@ func TestPermissions(t *testing.T) { defer ts.Close() vtctld := grpcvtctldserver.NewVtctldServer(vtenv.NewTestEnv(), ts) - primary := testlib.NewFakeTablet(t, ts, "cell1", 0, topodatapb.TabletType_PRIMARY, nil) - replica := testlib.NewFakeTablet(t, ts, "cell1", 1, topodatapb.TabletType_REPLICA, nil) + primary := NewFakeTablet(t, ts, "cell1", 0, topodatapb.TabletType_PRIMARY, nil) + replica := NewFakeTablet(t, ts, "cell1", 1, topodatapb.TabletType_REPLICA, nil) // Mark the primary for the shard. _, err := ts.UpdateShardFields(ctx, primary.Tablet.Keyspace, primary.Tablet.Shard, func(si *topo.ShardInfo) error {