本文整理汇总了Golang中github.com/youtube/vitess/go/vt/zktopo/zktestserver.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: CreateFakeServers
// CreateFakeServers returns the servers to use for these tests
func CreateFakeServers(t *testing.T) (*tabletconntest.FakeQueryService, topo.Server, string) {
cell := "local"
// the FakeServer is just slightly modified
f := tabletconntest.CreateFakeServer(t)
f.TestingGateway = true
f.StreamHealthResponse = &querypb.StreamHealthResponse{
Target: tabletconntest.TestTarget,
Serving: true,
TabletExternallyReparentedTimestamp: 1234589,
RealtimeStats: &querypb.RealtimeStats{
SecondsBehindMaster: 1,
},
}
// The topo server has a single SrvKeyspace
ts := zktestserver.New(t, []string{cell})
if err := ts.UpdateSrvKeyspace(context.Background(), cell, tabletconntest.TestTarget.Keyspace, &topodatapb.SrvKeyspace{
Partitions: []*topodatapb.SrvKeyspace_KeyspacePartition{
{
ServedType: topodatapb.TabletType_MASTER,
ShardReferences: []*topodatapb.ShardReference{
{
Name: tabletconntest.TestTarget.Shard,
},
},
},
},
}); err != nil {
t.Fatalf("can't add srvKeyspace: %v", err)
}
return f, ts, cell
}
示例2: TestCreateShard
// TestCreateShard tests a few cases for CreateShard
func TestCreateShard(t *testing.T) {
ctx := context.Background()
cells := []string{"test_cell"}
// Set up topology.
ts := zktestserver.New(t, cells)
keyspace := "test_keyspace"
shard := "0"
// create shard in a non-existing keyspace
if err := CreateShard(ctx, ts, keyspace, shard); err == nil {
t.Fatalf("CreateShard(invalid keyspace) didn't fail")
}
// create keyspace
if err := ts.CreateKeyspace(ctx, keyspace, &topodatapb.Keyspace{}); err != nil {
t.Fatalf("CreateKeyspace failed: %v", err)
}
// create shard should now work
if err := CreateShard(ctx, ts, keyspace, shard); err != nil {
t.Fatalf("CreateShard failed: %v", err)
}
}
示例3: TestGetOrCreateShard
// TestGetOrCreateShard will create / get 100 shards in a keyspace
// for a long time in parallel, making sure the locking and everything
// works correctly.
func TestGetOrCreateShard(t *testing.T) {
ctx := context.Background()
cells := []string{"test_cell"}
// Set up topology.
ts := zktestserver.New(t, cells)
// and do massive parallel GetOrCreateShard
keyspace := "test_keyspace"
wg := sync.WaitGroup{}
rand.Seed(time.Now().UnixNano())
for i := 0; i < 100; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
for j := 0; j < 100; j++ {
index := rand.Intn(10)
shard := fmt.Sprintf("%v", index)
si, err := GetOrCreateShard(ctx, ts, keyspace, shard)
if err != nil {
t.Errorf("GetOrCreateShard(%v, %v) failed: %v", i, shard, err)
}
if si.ShardName() != shard {
t.Errorf("si.ShardName() is wrong, got %v expected %v", si.ShardName(), shard)
}
}
}(i)
}
wg.Wait()
}
示例4: TestInitMasterShardChecks
// TestInitMasterShardChecks makes sure the safety checks work
func TestInitMasterShardChecks(t *testing.T) {
ctx := context.Background()
db := fakesqldb.Register()
ts := zktestserver.New(t, []string{"cell1", "cell2"})
wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient())
master := NewFakeTablet(t, wr, "cell1", 0, topodatapb.TabletType_MASTER, db)
// InitShardMaster with an unknown tablet
if err := wr.InitShardMaster(ctx, master.Tablet.Keyspace, master.Tablet.Shard, &topodatapb.TabletAlias{
Cell: master.Tablet.Alias.Cell,
Uid: master.Tablet.Alias.Uid + 1,
}, false /*force*/, 10*time.Second); err == nil || !strings.Contains(err.Error(), "is not in the shard") {
t.Errorf("InitShardMaster with unknown alias returned wrong error: %v", err)
}
// InitShardMaster with two masters in the shard, no force flag
// (master2 needs to run InitTablet with -force, as it is the second
// master in the same shard)
master2 := NewFakeTablet(t, wr, "cell1", 1, topodatapb.TabletType_MASTER, db, ForceInitTablet())
if err := wr.InitShardMaster(ctx, master2.Tablet.Keyspace, master2.Tablet.Shard, master2.Tablet.Alias, false /*force*/, 10*time.Second); err == nil || !strings.Contains(err.Error(), "is not the only master in the shard") {
t.Errorf("InitShardMaster with two masters returned wrong error: %v", err)
}
// InitShardMaster where the new master fails (use force flag
// as we have 2 masters). We force the failure by making the
// SQL commands executed on the master unexpected by the test fixture
master.StartActionLoop(t, wr)
defer master.StopActionLoop(t)
master2.StartActionLoop(t, wr)
defer master2.StopActionLoop(t)
if err := wr.InitShardMaster(ctx, master.Tablet.Keyspace, master.Tablet.Shard, master.Tablet.Alias, true /*force*/, 10*time.Second); err == nil || !strings.Contains(err.Error(), "unexpected extra query") {
t.Errorf("InitShardMaster with new master failing in new master InitMaster returned wrong error: %v", err)
}
}
示例5: TestNewRestartableResultReader
// TestNewRestartableResultReader tests the correct error handling e.g.
// if the connection to a tablet fails due to a canceled context.
func TestNewRestartableResultReader(t *testing.T) {
wantErr := errors.New("restartable_result_reader_test.go: context canceled")
tabletconn.RegisterDialer("fake_dialer", func(tablet *topodatapb.Tablet, timeout time.Duration) (tabletconn.TabletConn, error) {
return nil, wantErr
})
protocol := flag.CommandLine.Lookup("tablet_protocol").Value.String()
flag.Set("tablet_protocol", "fake_dialer")
// Restore the previous flag value after the test.
defer flag.Set("tablet_protocol", protocol)
// Create dependencies e.g. a "singleTabletProvider" instance.
ts := zktestserver.New(t, []string{"cell1"})
wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient())
alias := &topodatapb.TabletAlias{
Cell: "cell1",
Uid: 1,
}
tablet := &topodatapb.Tablet{
Keyspace: "ks1",
Shard: "-80",
Alias: alias,
}
ctx := context.Background()
if err := ts.CreateTablet(ctx, tablet); err != nil {
t.Fatalf("CreateTablet failed: %v", err)
}
tp := newSingleTabletProvider(ctx, ts, alias)
_, err := NewRestartableResultReader(ctx, wr.Logger(), tp, nil /* td */, chunk{}, false)
if err == nil || !strings.Contains(err.Error(), wantErr.Error()) {
t.Fatalf("NewRestartableResultReader() should have failed because the context is canceled: %v", err)
}
}
示例6: createTestAgent
func createTestAgent(ctx context.Context, t *testing.T) *ActionAgent {
ts := zktestserver.New(t, []string{"cell1"})
if err := ts.CreateKeyspace(ctx, "test_keyspace", &topodatapb.Keyspace{}); err != nil {
t.Fatalf("CreateKeyspace failed: %v", err)
}
if err := ts.CreateShard(ctx, "test_keyspace", "0"); err != nil {
t.Fatalf("CreateShard failed: %v", err)
}
port := int32(1234)
tablet := &topodatapb.Tablet{
Alias: tabletAlias,
Hostname: "host",
PortMap: map[string]int32{
"vt": port,
},
Ip: "1.0.0.1",
Keyspace: "test_keyspace",
Shard: "0",
Type: topodatapb.TabletType_SPARE,
}
if err := ts.CreateTablet(ctx, tablet); err != nil {
t.Fatalf("CreateTablet failed: %v", err)
}
mysqlDaemon := &mysqlctl.FakeMysqlDaemon{MysqlPort: 3306}
agent := NewTestActionAgent(ctx, ts, tabletAlias, port, 0, mysqlDaemon)
agent.BinlogPlayerMap = NewBinlogPlayerMap(ts, nil, nil)
agent.HealthReporter = &fakeHealthCheck{}
return agent
}
示例7: TestVtctlThrottlerCommands
// TestVtctlThrottlerCommands tests all vtctl commands from the
// "Resharding Throttler" group.
func TestVtctlThrottlerCommands(t *testing.T) {
// Run a throttler server using the default process throttle manager.
listener, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatalf("Cannot listen: %v", err)
}
s := grpc.NewServer()
go s.Serve(listener)
grpcthrottlerserver.StartServer(s, throttler.GlobalManager)
addr := fmt.Sprintf("localhost:%v", listener.Addr().(*net.TCPAddr).Port)
ts := zktestserver.New(t, []string{"cell1", "cell2"})
vp := NewVtctlPipe(t, ts)
defer vp.Close()
// Get and set rate commands do not fail when no throttler is registered.
{
got, err := vp.RunAndOutput([]string{"ThrottlerMaxRates", "-server", addr})
if err != nil {
t.Fatalf("VtctlPipe.RunAndStreamOutput() failed: %v", err)
}
want := "no active throttlers"
if !strings.Contains(got, want) {
t.Fatalf("ThrottlerMaxRates() = %v, want substring = %v", got, want)
}
}
{
got, err := vp.RunAndOutput([]string{"ThrottlerSetMaxRate", "-server", addr, "23"})
if err != nil {
t.Fatalf("VtctlPipe.RunAndStreamOutput() failed: %v", err)
}
want := "no active throttlers"
if !strings.Contains(got, want) {
t.Fatalf("ThrottlerSetMaxRate(23) = %v, want substring = %v", got, want)
}
}
// Add a throttler and check the commands again.
t1, err := throttler.NewThrottler("t1", "TPS", 1 /* threadCount */, 2323, throttler.ReplicationLagModuleDisabled)
if err != nil {
t.Fatal(err)
}
defer t1.Close()
// MaxRates() will return the initial rate.
expectRate(t, vp, addr, "2323")
// Disable the module by setting the rate to 'unlimited'.
setRate(t, vp, addr, "unlimited")
expectRate(t, vp, addr, "unlimited")
// Re-enable it by setting a limit.
setRate(t, vp, addr, "9999")
expectRate(t, vp, addr, "9999")
}
示例8: TestCreateTablet
// TestCreateTablet tests all the logic in the topo.CreateTablet method.
func TestCreateTablet(t *testing.T) {
cell := "cell1"
keyspace := "ks1"
shard := "shard1"
ctx := context.Background()
ts := zktestserver.New(t, []string{cell})
// Create a tablet.
alias := &topodatapb.TabletAlias{
Cell: cell,
Uid: 1,
}
tablet := &topodatapb.Tablet{
Keyspace: keyspace,
Shard: shard,
Alias: alias,
}
if err := ts.CreateTablet(ctx, tablet); err != nil {
t.Fatalf("CreateTablet failed: %v", err)
}
// Get the tablet, make sure it's good. Also check ShardReplication.
ti, err := ts.GetTablet(ctx, alias)
if err != nil || !proto.Equal(ti.Tablet, tablet) {
t.Fatalf("Created Tablet doesn't match: %v %v", ti, err)
}
sri, err := ts.GetShardReplication(ctx, cell, keyspace, shard)
if err != nil || len(sri.Nodes) != 1 || !proto.Equal(sri.Nodes[0].TabletAlias, alias) {
t.Fatalf("Created ShardReplication doesn't match: %v %v", sri, err)
}
// Create the same tablet again, make sure it fails with ErrNodeExists.
if err := ts.CreateTablet(ctx, tablet); err != topo.ErrNodeExists {
t.Fatalf("CreateTablet(again) returned: %v", err)
}
// Remove the ShardReplication record, try to create the
// tablets again, make sure it's fixed.
if err := topo.RemoveShardReplicationRecord(ctx, ts, cell, keyspace, shard, alias); err != nil {
t.Fatalf("RemoveShardReplicationRecord failed: %v", err)
}
sri, err = ts.GetShardReplication(ctx, cell, keyspace, shard)
if err != nil || len(sri.Nodes) != 0 {
t.Fatalf("Modifed ShardReplication doesn't match: %v %v", sri, err)
}
if err := ts.CreateTablet(ctx, tablet); err != topo.ErrNodeExists {
t.Fatalf("CreateTablet(again and again) returned: %v", err)
}
sri, err = ts.GetShardReplication(ctx, cell, keyspace, shard)
if err != nil || len(sri.Nodes) != 1 || !proto.Equal(sri.Nodes[0].TabletAlias, alias) {
t.Fatalf("Created ShardReplication doesn't match: %v %v", sri, err)
}
}
示例9: TestTabletData
func TestTabletData(t *testing.T) {
db := fakesqldb.Register()
ts := zktestserver.New(t, []string{"cell1", "cell2"})
wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient())
if err := ts.CreateKeyspace(context.Background(), "ks", &topodatapb.Keyspace{
ShardingColumnName: "keyspace_id",
ShardingColumnType: topodatapb.KeyspaceIdType_UINT64,
}); err != nil {
t.Fatalf("CreateKeyspace failed: %v", err)
}
tablet1 := testlib.NewFakeTablet(t, wr, "cell1", 0, topodatapb.TabletType_MASTER, db, testlib.TabletKeyspaceShard(t, "ks", "-80"))
tablet1.StartActionLoop(t, wr)
defer tablet1.StopActionLoop(t)
shsq := newStreamHealthTabletServer(t)
grpcqueryservice.Register(tablet1.RPCServer, shsq)
thc := newTabletHealthCache(ts)
stats := &querypb.RealtimeStats{
HealthError: "testHealthError",
SecondsBehindMaster: 72,
CpuUsage: 1.1,
}
// Keep broadcasting until the first result goes through.
stop := make(chan struct{})
go func() {
for {
select {
case <-stop:
return
default:
shsq.BroadcastHealth(42, stats)
}
}
}()
// Start streaming and wait for the first result.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
result, err := thc.Get(ctx, tablet1.Tablet.Alias)
cancel()
close(stop)
if err != nil {
t.Fatalf("thc.Get failed: %v", err)
}
if got, want := result.RealtimeStats, stats; !proto.Equal(got, want) {
t.Errorf("RealtimeStats = %#v, want %#v", got, want)
}
}
示例10: TestTabletExternallyReparentedFailedOldMaster
func TestTabletExternallyReparentedFailedOldMaster(t *testing.T) {
tabletmanager.SetReparentFlags(time.Minute /* finalizeTimeout */)
ctx := context.Background()
db := fakesqldb.Register()
ts := zktestserver.New(t, []string{"cell1", "cell2"})
wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient())
// Create an old master, a new master, and a good slave.
oldMaster := NewFakeTablet(t, wr, "cell1", 0, topodatapb.TabletType_MASTER, db)
newMaster := NewFakeTablet(t, wr, "cell1", 1, topodatapb.TabletType_REPLICA, db)
goodSlave := NewFakeTablet(t, wr, "cell1", 2, topodatapb.TabletType_REPLICA, db)
// Reparent to a replica, and pretend the old master is not responding.
// On the elected master, we will respond to
// TabletActionSlaveWasPromoted
newMaster.StartActionLoop(t, wr)
defer newMaster.StopActionLoop(t)
// On the old master, we will only get a
// TabletActionSlaveWasRestarted call, let's just not
// respond to it at all
// On the good slave, we will respond to
// TabletActionSlaveWasRestarted.
goodSlave.StartActionLoop(t, wr)
defer goodSlave.StopActionLoop(t)
// The reparent should work as expected here
t.Logf("TabletExternallyReparented(new master) expecting success")
tmc := tmclient.NewTabletManagerClient()
ti, err := ts.GetTablet(ctx, newMaster.Tablet.Alias)
if err != nil {
t.Fatalf("GetTablet failed: %v", err)
}
waitID := makeWaitID()
if err := tmc.TabletExternallyReparented(context.Background(), ti.Tablet, waitID); err != nil {
t.Fatalf("TabletExternallyReparented(replica) failed: %v", err)
}
waitForExternalReparent(t, waitID)
// check the old master was converted to replica
tablet, err := ts.GetTablet(ctx, oldMaster.Tablet.Alias)
if err != nil {
t.Fatalf("GetTablet(%v) failed: %v", oldMaster.Tablet.Alias, err)
}
if tablet.Type != topodatapb.TabletType_REPLICA {
t.Fatalf("old master should be spare but is: %v", tablet.Type)
}
}
示例11: TestReparentTablet
func TestReparentTablet(t *testing.T) {
ctx := context.Background()
db := fakesqldb.Register()
ts := zktestserver.New(t, []string{"cell1", "cell2"})
wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient())
// create shard and tablets
if err := ts.CreateShard(ctx, "test_keyspace", "0"); err != nil {
t.Fatalf("CreateShard failed: %v", err)
}
master := NewFakeTablet(t, wr, "cell1", 1, topodatapb.TabletType_MASTER, db)
slave := NewFakeTablet(t, wr, "cell1", 2, topodatapb.TabletType_REPLICA, db)
// mark the master inside the shard
si, err := ts.GetShard(ctx, "test_keyspace", "0")
if err != nil {
t.Fatalf("GetShard failed: %v", err)
}
si.MasterAlias = master.Tablet.Alias
if err := ts.UpdateShard(ctx, si); err != nil {
t.Fatalf("UpdateShard failed: %v", err)
}
// master action loop (to initialize host and port)
master.StartActionLoop(t, wr)
defer master.StopActionLoop(t)
// slave loop
slave.FakeMysqlDaemon.SetMasterCommandsInput = fmt.Sprintf("%v:%v", master.Tablet.Hostname, master.Tablet.PortMap["mysql"])
slave.FakeMysqlDaemon.SetMasterCommandsResult = []string{"set master cmd 1"}
slave.FakeMysqlDaemon.ExpectedExecuteSuperQueryList = []string{
"set master cmd 1",
}
slave.StartActionLoop(t, wr)
defer slave.StopActionLoop(t)
// run ReparentTablet
if err := wr.ReparentTablet(ctx, slave.Tablet.Alias); err != nil {
t.Fatalf("ReparentTablet failed: %v", err)
}
// check what was run
if err := slave.FakeMysqlDaemon.CheckSuperQueryList(); err != nil {
t.Fatalf("slave.FakeMysqlDaemon.CheckSuperQueryList failed: %v", err)
}
checkSemiSyncEnabled(t, false, true, slave)
}
示例12: TestTabletExternallyReparentedWithDifferentMysqlPort
// TestTabletExternallyReparentedWithDifferentMysqlPort makes sure
// that if mysql is restarted on the master-elect tablet and has a different
// port, we pick it up correctly.
func TestTabletExternallyReparentedWithDifferentMysqlPort(t *testing.T) {
tabletmanager.SetReparentFlags(time.Minute /* finalizeTimeout */)
ctx := context.Background()
db := fakesqldb.Register()
ts := zktestserver.New(t, []string{"cell1"})
wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient())
// Create an old master, a new master, two good slaves, one bad slave
oldMaster := NewFakeTablet(t, wr, "cell1", 0, topodatapb.TabletType_MASTER, db)
newMaster := NewFakeTablet(t, wr, "cell1", 1, topodatapb.TabletType_REPLICA, db)
goodSlave := NewFakeTablet(t, wr, "cell1", 2, topodatapb.TabletType_REPLICA, db)
// Now we're restarting mysql on a different port, 3301->3303
// but without updating the Tablet record in topology.
// On the elected master, we will respond to
// TabletActionSlaveWasPromoted, so we need a MysqlDaemon
// that returns no master, and the new port (as returned by mysql)
newMaster.FakeMysqlDaemon.MysqlPort = 3303
newMaster.StartActionLoop(t, wr)
defer newMaster.StopActionLoop(t)
// On the old master, we will only respond to
// TabletActionSlaveWasRestarted and point to the new mysql port
oldMaster.StartActionLoop(t, wr)
defer oldMaster.StopActionLoop(t)
// On the good slaves, we will respond to
// TabletActionSlaveWasRestarted and point to the new mysql port
goodSlave.StartActionLoop(t, wr)
defer goodSlave.StopActionLoop(t)
// This tests the good case, where everything works as planned
t.Logf("TabletExternallyReparented(new master) expecting success")
tmc := tmclient.NewTabletManagerClient()
ti, err := ts.GetTablet(ctx, newMaster.Tablet.Alias)
if err != nil {
t.Fatalf("GetTablet failed: %v", err)
}
waitID := makeWaitID()
if err := tmc.TabletExternallyReparented(context.Background(), ti.Tablet, waitID); err != nil {
t.Fatalf("TabletExternallyReparented(replica) failed: %v", err)
}
waitForExternalReparent(t, waitID)
}
示例13: TestShard
func TestShard(t *testing.T) {
cell := "cell1"
keyspace := "ks1"
shard := "sh1"
ctx := context.Background()
ts := zktestserver.New(t, []string{cell})
// Create a Keyspace / Shard
if err := ts.CreateKeyspace(ctx, keyspace, &topodatapb.Keyspace{}); err != nil {
t.Fatalf("CreateKeyspace failed: %v", err)
}
if err := ts.CreateShard(ctx, keyspace, shard); err != nil {
t.Fatalf("CreateShard failed: %v", err)
}
// Hack the zookeeper backend to create an error for GetShard.
zconn := ts.Impl.(*zktestserver.TestServer).Impl.(*zktopo.Server).GetZConn()
if _, err := zconn.Set(path.Join(zktopo.GlobalKeyspacesPath, keyspace, "shards", shard), []byte{}, -1); err != nil {
t.Fatalf("failed to hack the shard: %v", err)
}
// Create the workflow, run the validator.
w := &Workflow{
logger: logutil.NewMemoryLogger(),
}
sv := &ShardValidator{}
if err := sv.Audit(ctx, ts, w); err != nil {
t.Fatalf("Audit failed: %v", err)
}
if len(w.fixers) != 1 {
t.Fatalf("fixer not added: %v", w.fixers)
}
if !strings.Contains(w.fixers[0].message, "bad shard data") {
t.Errorf("bad message: %v ", w.fixers[0].message)
}
// Run Delete, make sure the entry is removed.
if err := w.fixers[0].fixer.Action(ctx, "Delete"); err != nil {
t.Fatalf("Action failed: %v", err)
}
shards, err := ts.GetShardNames(ctx, keyspace)
if err != nil || len(shards) != 0 {
t.Errorf("bad GetShardNames output: %v %v ", shards, err)
}
}
示例14: TestVersion
func TestVersion(t *testing.T) {
// We need to run this test with the /debug/vars version of the
// plugin.
wrangler.ResetDebugVarsGetVersion()
// Initialize our environment
db := fakesqldb.Register()
ts := zktestserver.New(t, []string{"cell1", "cell2"})
wr := wrangler.New(logutil.NewConsoleLogger(), ts, tmclient.NewTabletManagerClient())
vp := NewVtctlPipe(t, ts)
defer vp.Close()
// couple tablets is enough
sourceMaster := NewFakeTablet(t, wr, "cell1", 10, topodatapb.TabletType_MASTER, db,
TabletKeyspaceShard(t, "source", "0"),
StartHTTPServer())
sourceReplica := NewFakeTablet(t, wr, "cell1", 11, topodatapb.TabletType_REPLICA, db,
TabletKeyspaceShard(t, "source", "0"),
StartHTTPServer())
// sourceMaster loop
sourceMasterGitRev := "fake git rev"
sourceMaster.StartActionLoop(t, wr)
sourceMaster.HTTPServer.Handler.(*http.ServeMux).HandleFunc("/debug/vars", expvarHandler(&sourceMasterGitRev))
defer sourceMaster.StopActionLoop(t)
// sourceReplica loop
sourceReplicaGitRev := "fake git rev"
sourceReplica.StartActionLoop(t, wr)
sourceReplica.HTTPServer.Handler.(*http.ServeMux).HandleFunc("/debug/vars", expvarHandler(&sourceReplicaGitRev))
defer sourceReplica.StopActionLoop(t)
// test when versions are the same
if err := vp.Run([]string{"ValidateVersionKeyspace", sourceMaster.Tablet.Keyspace}); err != nil {
t.Fatalf("ValidateVersionKeyspace(same) failed: %v", err)
}
// test when versions are different
sourceReplicaGitRev = "different fake git rev"
if err := vp.Run([]string{"ValidateVersionKeyspace", sourceMaster.Tablet.Keyspace}); err == nil || !strings.Contains(err.Error(), "is different than slave") {
t.Fatalf("ValidateVersionKeyspace(different) returned an unexpected error: %v", err)
}
}
示例15: TestHandleExplorerRedirect
func TestHandleExplorerRedirect(t *testing.T) {
ctx := context.Background()
ts := zktestserver.New(t, []string{"cell1"})
if err := ts.CreateTablet(ctx, &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: "cell1",
Uid: 123,
},
Keyspace: "test_keyspace",
Shard: "123-456",
}); err != nil {
t.Fatalf("CreateTablet failed: %v", err)
}
table := map[string]string{
"/explorers/redirect?type=keyspace&keyspace=test_keyspace": "/app/#/keyspaces/",
"/explorers/redirect?type=shard&keyspace=test_keyspace&shard=-80": "/app/#/shard/test_keyspace/-80",
"/explorers/redirect?type=srv_keyspace&keyspace=test_keyspace&cell=cell1": "/app/#/keyspaces/",
"/explorers/redirect?type=srv_shard&keyspace=test_keyspace&shard=-80&cell=cell1": "/app/#/shard/test_keyspace/-80",
"/explorers/redirect?type=srv_type&keyspace=test_keyspace&shard=-80&cell=cell1&tablet_type=replica": "/app/#/shard/test_keyspace/-80",
"/explorers/redirect?type=tablet&alias=cell1-123": "/app/#/shard/test_keyspace/123-456",
"/explorers/redirect?type=replication&keyspace=test_keyspace&shard=-80&cell=cell1": "/app/#/shard/test_keyspace/-80",
}
for input, want := range table {
request, err := http.NewRequest("GET", input, nil)
if err != nil {
t.Fatalf("NewRequest error: %v", err)
}
if err := request.ParseForm(); err != nil {
t.Fatalf("ParseForm error: %v", err)
}
got, err := handleExplorerRedirect(ctx, ts, request)
if err != nil {
t.Fatalf("handleExplorerRedirect error: %v", err)
}
if got != want {
t.Errorf("handlExplorerRedirect(%#v) = %#v, want %#v", input, got, want)
}
}
}