Skip to content

Commit

Permalink
Reorgnaize the permissions tests
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Lord <[email protected]>
  • Loading branch information
mattlord committed Jan 17, 2025
1 parent 60fca3a commit 1073206
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -27,6 +23,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"
"google.golang.org/grpc"

"vitess.io/vitess/go/mysql/fakesqldb"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand All @@ -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.
Expand All @@ -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)

Expand All @@ -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{
Expand All @@ -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()

Expand Down
7 changes: 3 additions & 4 deletions go/cmd/vtctldclient/command/permissions_test.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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"
Expand All @@ -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 {
Expand Down

0 comments on commit 1073206

Please sign in to comment.