testutil

package
v0.11.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2021 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Overview

Package testutil contains utility functions for writing tests for the grpcvtctldserver.

Index

Constants

View Source
const BackupStorageImplementation = "grpcvtctldserver.testutil"

BackupStorageImplementation is the name this package registers its test backupstorage.BackupStorage implementation as. Users should set *backupstorage.BackupStorageImplementation to this value before use.

Variables

View Source
var BackupStorage = &backupStorage{
	Backups: map[string][]string{},
}

BackupStorage is the singleton test backupstorage.BackupStorage intastnce. It is public and singleton to allow tests to both mutate and assert against its state.

Functions

func AddKeyspace

func AddKeyspace(ctx context.Context, t *testing.T, ts *topo.Server, ks *vtctldatapb.Keyspace)

AddKeyspace adds a keyspace to a topology, failing a test if that keyspace could not be added. It shallow copies the proto struct to prevent XXX_ fields from changing in the marshalling.

func AddKeyspaces

func AddKeyspaces(ctx context.Context, t *testing.T, ts *topo.Server, keyspaces ...*vtctldatapb.Keyspace)

AddKeyspaces adds a list of keyspaces to the topology, failing a test if any of those keyspaces cannot be added. See AddKeyspace for details.

func AddShards

func AddShards(ctx context.Context, t *testing.T, ts *topo.Server, shards ...*vtctldatapb.Shard)

AddShards adds a list of shards to the topology, failing a test if any of the shard records could not be created. It also ensures that every shard's keyspace exists, or creates an empty keyspace if that shard's keyspace does not exist.

func AddSrvKeyspaces

func AddSrvKeyspaces(t *testing.T, ts *topo.Server, srvKeyspaces ...*SrvKeyspace)

AddSrvKeyspaces adds one or more SrvKeyspace objects to the topology. It fails the calling test if any of the objects fail to update.

func AddTablet

func AddTablet(ctx context.Context, t *testing.T, ts *topo.Server, tablet *topodatapb.Tablet, opts *AddTabletOptions)

AddTablet adds a tablet to the topology, failing a test if that tablet record could not be created. It shallow copies to prevent XXX_ fields from changing, including nested proto message fields.

AddTablet also optionally adds empty keyspace and shard records to the topology, if they are set on the tablet record and they cannot be retrieved from the topo server without error.

If AddTablet receives a tablet record with a keyspace and shard set, and that tablet's type is MASTER, and opts.AlsoSetShardMaster is set, then AddTablet will update the shard record to make that tablet the shard master and set the shard to serving. If that shard record already has a serving primary, then AddTablet will fail the test.

func AddTablets

func AddTablets(ctx context.Context, t *testing.T, ts *topo.Server, opts *AddTabletOptions, tablets ...*topodatapb.Tablet)

AddTablets adds a list of tablets to the topology. See AddTablet for more details.

func AssertEmergencyReparentShardResponsesEqual

func AssertEmergencyReparentShardResponsesEqual(t *testing.T, expected *vtctldatapb.EmergencyReparentShardResponse, actual *vtctldatapb.EmergencyReparentShardResponse, msgAndArgs ...interface{})

AssertEmergencyReparentShardResponsesEqual asserts that two vtctldatapb.EmergencyReparentShardResponse objects are equal, ignoring their respective Events field in the comparison.

func AssertKeyspacesEqual

func AssertKeyspacesEqual(t *testing.T, expected *vtctldatapb.Keyspace, actual *vtctldatapb.Keyspace, msgAndArgs ...interface{})

AssertKeyspacesEqual is a convenience function to assert that two vtctldatapb.Keyspace objects are equal, after clearing out any reserved proto XXX_ fields.

func AssertLogutilEventsOccurred

func AssertLogutilEventsOccurred(t *testing.T, container interface{ GetEvents() []*logutilpb.Event }, msgAndArgs ...interface{})

AssertLogutilEventsOccurred asserts that for something containing a slice of logutilpb.Event, that the container is non-nil, and the event slice is non-zero.

This test function is generalized with an anonymous interface that any protobuf type containing a slice of logutilpb.Event elements called Events, which is the convention in protobuf types in the Vitess codebase, already implements.

func AssertNoLogutilEventsOccurred

func AssertNoLogutilEventsOccurred(t *testing.T, container interface{ GetEvents() []*logutilpb.Event }, msgAndArgs ...interface{})

AssertNoLogutilEventsOccurred asserts that for something containing a slice of logutilpb.Event, that the container is either nil, or that the event slice is exactly zero length.

This test function is generalized with an anonymous interface that any protobuf type containing a slice of logutilpb.Event elements called Events, which is the convention in protobuf types in the Vitess codebase, already implements.

func AssertPlannedReparentShardResponsesEqual

func AssertPlannedReparentShardResponsesEqual(t *testing.T, expected *vtctldatapb.PlannedReparentShardResponse, actual *vtctldatapb.PlannedReparentShardResponse)

AssertPlannedReparentShardResponsesEqual asserts that two vtctldatapb.PlannedReparentShardResponse objects are equal, ignoring their respective Events field in the comparison.

func AssertSameTablets added in v0.11.0

func AssertSameTablets(t *testing.T, expected, actual []*topodatapb.Tablet)

func NewVtctldServerWithTabletManagerClient

func NewVtctldServerWithTabletManagerClient(t *testing.T, ts *topo.Server, tmc tmclient.TabletManagerClient, newVtctldServerFn func(ts *topo.Server) vtctlservicepb.VtctldServer) vtctlservicepb.VtctldServer

NewVtctldServerWithTabletManagerClient returns a new grpcvtctldserver.VtctldServer configured with the given topo server and tmclient.TabletManagerClient implementation for testing.

It synchronizes on private locks to prevent multiple goroutines from stepping on each other during VtctldServer initialization, but still run the rest of the test in parallel.

NOTE, THE FIRST: It is only safe to use in parallel with other tests using this method of creating a VtctldServer, or with tests that do not depend on a VtctldServer's tmclient.TabletManagerClient implementation.

NOTE, THE SECOND: It needs to register a unique name to the tmclient factory registry, so we keep a shadow map of factories registered for "protocols" by this function. That way, if we happen to have multiple tests with the same name, we can swap out the return value for the factory and allow both tests to run, rather than the second test failing when it attempts to register a second factory for the same "protocol" name.

NOTE, THE THIRD: we take a "new" func to produce a valid vtctlservicepb.VtctldServer implementation, rather than constructing directly ourselves with grpcvtctldserver.NewVtctldServer. This is to prevent an import cycle between this package and package grpcvtctldserver. Further, because the return type of NewVtctldServer is the struct type (*grpcvtctldserver.VtctldServer) and not the interface type vtctlservicepb.VtctldServer, tests will need to indirect that call through an extra layer rather than passing the function identifier directly, e.g.:

vtctld := testutil.NewVtctldServerWithTabletManagerClient(t, ts, &testutil.TabletManagerClient{
	...
}, func(ts *topo.Server) vtctlservicepb.VtctldServer { return NewVtctldServer(ts) })

func SetupReplicationGraphs

func SetupReplicationGraphs(ctx context.Context, t *testing.T, ts *topo.Server, replicationGraphs ...*topo.ShardReplicationInfo)

SetupReplicationGraphs creates a set of ShardReplication objects in the topo, failing the test if any of the records could not be created.

func UpdateSrvKeyspaces

func UpdateSrvKeyspaces(ctx context.Context, t *testing.T, ts *topo.Server, srvkeyspacesByCellByKeyspace map[string]map[string]*topodatapb.SrvKeyspace)

UpdateSrvKeyspaces updates a set of SrvKeyspace records, grouped by cell and then by keyspace. It fails the test if any records cannot be updated.

func WithTestServer

func WithTestServer(
	t *testing.T,
	server vtctlservicepb.VtctldServer,
	test func(t *testing.T, client vtctldclient.VtctldClient),
)

WithTestServer creates a gRPC server listening locally with the given RPC implementation, then runs the test func with a client created to point at that server.

func WithTestServers

func WithTestServers(
	t *testing.T,
	test func(t *testing.T, clients ...vtctldclient.VtctldClient),
	servers ...vtctlservicepb.VtctldServer,
)

WithTestServers creates N gRPC servers listening locally with the given RPC implementations, and then runs the test func with N clients created, where clients[i] points at servers[i].

Types

type AddTabletOptions

type AddTabletOptions struct {
	// AlsoSetShardMaster is an option to control additional setup to take when
	// AddTablet receives a tablet of type MASTER. When set, AddTablet will also
	// update the shard record to make that tablet the primary, and fail the
	// test if the shard record has a serving primary already.
	AlsoSetShardMaster bool
	// ForceSetShardMaster, when combined with AlsoSetShardMaster, will ignore
	// any existing primary in the shard, making the current tablet the serving
	// primary (given it is type MASTER), and log that it has done so.
	ForceSetShardMaster bool
	// SkipShardCreation, when set, makes AddTablet never attempt to create a
	// shard record in the topo under any circumstances.
	SkipShardCreation bool
}

AddTabletOptions is a container for different behaviors tests need from AddTablet.

type SrvKeyspace

type SrvKeyspace struct {
	Keyspace    string
	Cell        string
	SrvKeyspace *topodatapb.SrvKeyspace
}

SrvKeyspace groups a topodatapb.SrvKeyspace together with a keyspace and cell.

type TabletManagerClient

type TabletManagerClient struct {
	tmclient.TabletManagerClient
	// TopoServer is used for certain TabletManagerClient rpcs that update topo
	// information, e.g. ChangeType. To force an error result for those rpcs in
	// a test, set tmc.TopoServer = nil.
	TopoServer *topo.Server
	// keyed by tablet alias.
	DemoteMasterDelays map[string]time.Duration
	// keyed by tablet alias.
	DemoteMasterResults map[string]struct {
		Status *replicationdatapb.MasterStatus
		Error  error
	}
	// keyed by tablet alias.
	GetSchemaDelays map[string]time.Duration
	// keyed by tablet alias.
	GetSchemaResults map[string]struct {
		Schema *tabletmanagerdatapb.SchemaDefinition
		Error  error
	}
	// keyed by tablet alias.
	MasterPositionDelays map[string]time.Duration
	// keyed by tablet alias.
	MasterPositionResults map[string]struct {
		Position string
		Error    error
	}
	// keyed by tablet alias.
	PopulateReparentJournalDelays map[string]time.Duration
	// keyed by tablet alias
	PopulateReparentJournalResults map[string]error
	// keyed by tablet alias.
	PromoteReplicaDelays map[string]time.Duration
	// keyed by tablet alias. injects a sleep to the end of the function
	// regardless of parent context timeout or error result.
	PromoteReplicaPostDelays map[string]time.Duration
	// keyed by tablet alias.
	PromoteReplicaResults map[string]struct {
		Result string
		Error  error
	}
	// keyed by tablet alias.
	RefreshStateResults      map[string]error
	ReplicationStatusDelays  map[string]time.Duration
	ReplicationStatusResults map[string]struct {
		Position *replicationdatapb.Status
		Error    error
	}
	// keyed by tablet alias.
	SetMasterDelays map[string]time.Duration
	// keyed by tablet alias.
	SetMasterResults map[string]error
	// keyed by tablet alias.
	SetReadWriteDelays map[string]time.Duration
	// keyed by tablet alias.
	SetReadWriteResults map[string]error
	// keyed by tablet alias.
	StopReplicationAndGetStatusDelays map[string]time.Duration
	// keyed by tablet alias.
	StopReplicationAndGetStatusResults map[string]struct {
		Status     *replicationdatapb.Status
		StopStatus *replicationdatapb.StopReplicationStatus
		Error      error
	}
	// keyed by tablet alias.
	UndoDemoteMasterDelays map[string]time.Duration
	// keyed by tablet alias
	UndoDemoteMasterResults map[string]error
	// tablet alias => duration
	VReplicationExecDelays map[string]time.Duration
	// tablet alias => query string => result
	VReplicationExecResults map[string]map[string]struct {
		Result *querypb.QueryResult
		Error  error
	}
	// keyed by tablet alias.
	WaitForPositionDelays map[string]time.Duration
	// keyed by tablet alias. injects a sleep to the end of the function
	// regardless of parent context timeout or error result.
	WaitForPositionPostDelays map[string]time.Duration
	// WaitForPosition(tablet *topodatapb.Tablet, position string) error, so we
	// key by tablet alias and then by position.
	WaitForPositionResults map[string]map[string]error
}

TabletManagerClient implements the tmclient.TabletManagerClient interface with mock delays and response values, for use in unit tests.

func (*TabletManagerClient) ChangeType

func (fake *TabletManagerClient) ChangeType(ctx context.Context, tablet *topodatapb.Tablet, newType topodatapb.TabletType) error

ChangeType is part of the tmclient.TabletManagerClient interface.

func (*TabletManagerClient) DemoteMaster

DemoteMaster is part of the tmclient.TabletManagerClient interface.

func (*TabletManagerClient) GetSchema

func (fake *TabletManagerClient) GetSchema(ctx context.Context, tablet *topodatapb.Tablet, tablets []string, excludeTables []string, includeViews bool) (*tabletmanagerdatapb.SchemaDefinition, error)

GetSchema is part of the tmclient.TabletManagerClient interface.

func (*TabletManagerClient) MasterPosition

func (fake *TabletManagerClient) MasterPosition(ctx context.Context, tablet *topodatapb.Tablet) (string, error)

MasterPosition is part of the tmclient.TabletManagerClient interface.

func (*TabletManagerClient) PopulateReparentJournal

func (fake *TabletManagerClient) PopulateReparentJournal(ctx context.Context, tablet *topodatapb.Tablet, timeCreatedNS int64, actionName string, primaryAlias *topodatapb.TabletAlias, pos string) error

PopulateReparentJournal is part of the tmclient.TabletManagerClient interface.

func (*TabletManagerClient) PromoteReplica

func (fake *TabletManagerClient) PromoteReplica(ctx context.Context, tablet *topodatapb.Tablet) (string, error)

PromoteReplica is part of the tmclient.TabletManagerClient interface.

func (*TabletManagerClient) RefreshState added in v0.11.0

func (fake *TabletManagerClient) RefreshState(ctx context.Context, tablet *topodatapb.Tablet) error

RefreshState is part of the tmclient.TabletManagerClient interface.

func (*TabletManagerClient) ReplicationStatus

func (fake *TabletManagerClient) ReplicationStatus(ctx context.Context, tablet *topodatapb.Tablet) (*replicationdatapb.Status, error)

ReplicationStatus is part of the tmclient.TabletManagerClient interface.

func (*TabletManagerClient) SetMaster

func (fake *TabletManagerClient) SetMaster(ctx context.Context, tablet *topodatapb.Tablet, parent *topodatapb.TabletAlias, timeCreatedNS int64, waitPosition string, forceStartReplication bool) error

SetMaster is part of the tmclient.TabletManagerClient interface.

func (*TabletManagerClient) SetReadWrite

func (fake *TabletManagerClient) SetReadWrite(ctx context.Context, tablet *topodatapb.Tablet) error

SetReadWrite is part of the tmclient.TabletManagerClient interface.

func (*TabletManagerClient) StopReplicationAndGetStatus

StopReplicationAndGetStatus is part of the tmclient.TabletManagerClient interface.

func (*TabletManagerClient) UndoDemoteMaster

func (fake *TabletManagerClient) UndoDemoteMaster(ctx context.Context, tablet *topodatapb.Tablet) error

UndoDemoteMaster is part of the tmclient.TabletManagerClient interface.

func (*TabletManagerClient) VReplicationExec

func (fake *TabletManagerClient) VReplicationExec(ctx context.Context, tablet *topodatapb.Tablet, query string) (*querypb.QueryResult, error)

VReplicationExec is part of the tmclient.TabletManagerCLient interface.

func (*TabletManagerClient) WaitForPosition

func (fake *TabletManagerClient) WaitForPosition(ctx context.Context, tablet *topodatapb.Tablet, position string) error

WaitForPosition is part of the tmclient.TabletManagerClient interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL