Skip to content

Commit

Permalink
tests/server: migrate test framework to testify (tikv#5197)
Browse files Browse the repository at this point in the history
ref tikv#4813

Signed-off-by: lhy1024 <admin@liudos.us>

Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
  • Loading branch information
2 people authored and CabinfeverB committed Jul 14, 2022
1 parent 7014489 commit 2c5e434
Showing 1 changed file with 41 additions and 51 deletions.
92 changes: 41 additions & 51 deletions tests/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"context"
"testing"

. "github.com/pingcap/check"
"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/tempurl"
"github.com/tikv/pd/pkg/testutil"
"github.com/tikv/pd/server/config"
Expand All @@ -29,46 +29,30 @@ import (
_ "github.com/tikv/pd/server/schedulers"
)

func Test(t *testing.T) {
TestingT(t)
}

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m, testutil.LeakOptions...)
}

var _ = Suite(&serverTestSuite{})

type serverTestSuite struct {
ctx context.Context
cancel context.CancelFunc
}

func (s *serverTestSuite) SetUpSuite(c *C) {
s.ctx, s.cancel = context.WithCancel(context.Background())
}

func (s *serverTestSuite) TearDownSuite(c *C) {
s.cancel()
}

func (s *serverTestSuite) TestUpdateAdvertiseUrls(c *C) {
cluster, err := tests.NewTestCluster(s.ctx, 2)
func TestUpdateAdvertiseUrls(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
re := require.New(t)
cluster, err := tests.NewTestCluster(ctx, 2)
defer cluster.Destroy()
c.Assert(err, IsNil)
re.NoError(err)

err = cluster.RunInitialServers()
c.Assert(err, IsNil)
re.NoError(err)

// AdvertisePeerUrls should equals to PeerUrls.
for _, conf := range cluster.GetConfig().InitialServers {
serverConf := cluster.GetServer(conf.Name).GetConfig()
c.Assert(serverConf.AdvertisePeerUrls, Equals, conf.PeerURLs)
c.Assert(serverConf.AdvertiseClientUrls, Equals, conf.ClientURLs)
re.Equal(conf.PeerURLs, serverConf.AdvertisePeerUrls)
re.Equal(conf.ClientURLs, serverConf.AdvertiseClientUrls)
}

err = cluster.StopAll()
c.Assert(err, IsNil)
re.NoError(err)

// Change config will not affect peer urls.
// Recreate servers with new peer URLs.
Expand All @@ -77,66 +61,72 @@ func (s *serverTestSuite) TestUpdateAdvertiseUrls(c *C) {
}
for _, conf := range cluster.GetConfig().InitialServers {
serverConf, e := conf.Generate()
c.Assert(e, IsNil)
s, e := tests.NewTestServer(s.ctx, serverConf)
c.Assert(e, IsNil)
re.NoError(e)
s, e := tests.NewTestServer(ctx, serverConf)
re.NoError(e)
cluster.GetServers()[conf.Name] = s
}
err = cluster.RunInitialServers()
c.Assert(err, IsNil)
re.NoError(err)
for _, conf := range cluster.GetConfig().InitialServers {
serverConf := cluster.GetServer(conf.Name).GetConfig()
c.Assert(serverConf.AdvertisePeerUrls, Equals, conf.PeerURLs)
re.Equal(conf.PeerURLs, serverConf.AdvertisePeerUrls)
}
}

func (s *serverTestSuite) TestClusterID(c *C) {
cluster, err := tests.NewTestCluster(s.ctx, 3)
func TestClusterID(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
re := require.New(t)
cluster, err := tests.NewTestCluster(ctx, 3)
defer cluster.Destroy()
c.Assert(err, IsNil)
re.NoError(err)

err = cluster.RunInitialServers()
c.Assert(err, IsNil)
re.NoError(err)

clusterID := cluster.GetServer("pd1").GetClusterID()
for _, s := range cluster.GetServers() {
c.Assert(s.GetClusterID(), Equals, clusterID)
re.Equal(clusterID, s.GetClusterID())
}

// Restart all PDs.
err = cluster.StopAll()
c.Assert(err, IsNil)
re.NoError(err)
err = cluster.RunInitialServers()
c.Assert(err, IsNil)
re.NoError(err)

// All PDs should have the same cluster ID as before.
for _, s := range cluster.GetServers() {
c.Assert(s.GetClusterID(), Equals, clusterID)
re.Equal(clusterID, s.GetClusterID())
}

cluster2, err := tests.NewTestCluster(s.ctx, 3, func(conf *config.Config, serverName string) { conf.InitialClusterToken = "foobar" })
cluster2, err := tests.NewTestCluster(ctx, 3, func(conf *config.Config, serverName string) { conf.InitialClusterToken = "foobar" })
defer cluster2.Destroy()
c.Assert(err, IsNil)
re.NoError(err)
err = cluster2.RunInitialServers()
c.Assert(err, IsNil)
re.NoError(err)
clusterID2 := cluster2.GetServer("pd1").GetClusterID()
c.Assert(clusterID2, Not(Equals), clusterID)
re.NotEqual(clusterID, clusterID2)
}

func (s *serverTestSuite) TestLeader(c *C) {
cluster, err := tests.NewTestCluster(s.ctx, 3)
func TestLeader(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
re := require.New(t)
cluster, err := tests.NewTestCluster(ctx, 3)
defer cluster.Destroy()
c.Assert(err, IsNil)
re.NoError(err)

err = cluster.RunInitialServers()
c.Assert(err, IsNil)
re.NoError(err)

leader1 := cluster.WaitLeader()
c.Assert(leader1, Not(Equals), "")
re.NotEqual("", leader1)

err = cluster.GetServer(leader1).Stop()
c.Assert(err, IsNil)
testutil.WaitUntil(c, func() bool {
re.NoError(err)
testutil.Eventually(re, func() bool {
leader := cluster.GetLeader()
return leader != leader1
})
Expand Down

0 comments on commit 2c5e434

Please sign in to comment.