From 95e8cf9d1ba9bbbfb7cddcc914bbce11eebfa076 Mon Sep 17 00:00:00 2001 From: tison Date: Fri, 9 Jul 2021 14:10:42 +0800 Subject: [PATCH 1/6] errno: migrate test-infra to testify Signed-off-by: tison --- errno/infoschema_test.go | 55 ++++++++++++++++++---------------------- go.mod | 1 + 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/errno/infoschema_test.go b/errno/infoschema_test.go index 1a8d0f0c3984c..17d217b2e6c80 100644 --- a/errno/infoschema_test.go +++ b/errno/infoschema_test.go @@ -16,18 +16,11 @@ package errno import ( "testing" - . "github.com/pingcap/check" + "github.com/stretchr/testify/assert" ) -func TestT(t *testing.T) { - TestingT(t) -} - -var _ = Suite(&testErrno{}) - -type testErrno struct{} - -func (s *testErrno) TestCopySafety(c *C) { +func TestCopySafety(t *testing.T) { + t.Parallel() IncrementError(123, "user", "host") IncrementError(321, "user2", "host2") @@ -48,42 +41,42 @@ func (s *testErrno) TestCopySafety(c *C) { IncrementWarning(333, "c", "d") // global stats - c.Assert(stats.global[123].ErrorCount, Equals, 3) - c.Assert(globalCopy[123].ErrorCount, Equals, 1) + assert.Equal(t, 3, stats.global[123].ErrorCount) + assert.Equal(t, 1, globalCopy[123].ErrorCount) // user stats - c.Assert(len(stats.users), Equals, 6) - c.Assert(len(userCopy), Equals, 3) - c.Assert(stats.users["user"][123].ErrorCount, Equals, 2) - c.Assert(stats.users["user"][123].WarningCount, Equals, 2) - c.Assert(userCopy["user"][123].ErrorCount, Equals, 1) - c.Assert(userCopy["user"][123].WarningCount, Equals, 1) + assert.Len(t, stats.users, 6) + assert.Len(t, userCopy, 3) + assert.Equal(t, 2, stats.users["user"][123].ErrorCount) + assert.Equal(t, 2, stats.users["user"][123].WarningCount) + assert.Equal(t, 1, userCopy["user"][123].ErrorCount) + assert.Equal(t, 1, userCopy["user"][123].WarningCount) // ensure there is no user3 in userCopy _, ok := userCopy["user3"] - c.Assert(ok, IsFalse) + assert.False(t, ok) _, ok = stats.users["user3"] - c.Assert(ok, IsTrue) + assert.True(t, ok) _, ok = userCopy["a"] - c.Assert(ok, IsFalse) + assert.False(t, ok) _, ok = stats.users["a"] - c.Assert(ok, IsTrue) + assert.True(t, ok) // host stats - c.Assert(len(stats.hosts), Equals, 5) - c.Assert(len(hostCopy), Equals, 3) + assert.Len(t, stats.hosts, 5) + assert.Len(t, hostCopy, 3) + IncrementError(123, "user3", "newhost") - c.Assert(len(stats.hosts), Equals, 6) - c.Assert(len(hostCopy), Equals, 3) + assert.Len(t, stats.hosts, 6) + assert.Len(t, hostCopy, 3) // ensure there is no newhost in hostCopy _, ok = hostCopy["newhost"] - c.Assert(ok, IsFalse) + assert.False(t, ok) _, ok = stats.hosts["newhost"] - c.Assert(ok, IsTrue) + assert.True(t, ok) _, ok = hostCopy["b"] - c.Assert(ok, IsFalse) + assert.False(t, ok) _, ok = stats.hosts["b"] - c.Assert(ok, IsTrue) - + assert.True(t, ok) } diff --git a/go.mod b/go.mod index c21ad7064baba..7d325f38ad76e 100644 --- a/go.mod +++ b/go.mod @@ -54,6 +54,7 @@ require ( github.com/rivo/uniseg v0.2.0 // indirect github.com/shirou/gopsutil v3.21.2+incompatible github.com/soheilhy/cmux v0.1.4 + github.com/stretchr/testify v1.7.0 github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2 github.com/tikv/client-go/v2 v2.0.0-alpha.0.20210708041317-16fb6792bffa github.com/tikv/pd v1.1.0-beta.0.20210323121136-78679e5e209d From 2fd97fe96456d6ad9d4311d6f104c86070a27e03 Mon Sep 17 00:00:00 2001 From: tison Date: Fri, 9 Jul 2021 14:50:33 +0800 Subject: [PATCH 2/6] setup bridge between test infras Signed-off-by: tison --- .build.ps1 | 2 +- Makefile | 4 ++-- errno/infoschema_test.go | 1 + util/testbridge/bridge.go | 25 +++++++++++++++++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 util/testbridge/bridge.go diff --git a/.build.ps1 b/.build.ps1 index bcd506c49f874..9674bdaf11464 100644 --- a/.build.ps1 +++ b/.build.ps1 @@ -296,7 +296,7 @@ task GoTest BuildFailPoint, { } $env:log_level = 'fatal' $env:TZ = 'Asia/Shanghai' - exec { & $GO test -p $P -ldflags "`"$testFlags`"" -cover $packages '-check.p' true '-check.timeout' 4s } + exec { & $GO test -p $P -ldflags "`"$testFlags`"" -cover $packages '-check.p' true '-check.timeout' 4s '-timeout' 4s } } -Done { Disable-FailPoint $env:log_level = $Task.Data.logLevel diff --git a/Makefile b/Makefile index 50ae4507a3ed2..f9424385bea53 100644 --- a/Makefile +++ b/Makefile @@ -139,7 +139,7 @@ ifeq ("$(TRAVIS_COVERAGE)", "1") else @echo "Running in native mode." @export log_level=info; export TZ='Asia/Shanghai'; \ - $(GOTEST) -ldflags '$(TEST_LDFLAGS)' $(EXTRA_TEST_ARGS) -cover $(PACKAGES) -check.p true -check.timeout 4s || { $(FAILPOINT_DISABLE); exit 1; } + $(GOTEST) -ldflags '$(TEST_LDFLAGS)' $(EXTRA_TEST_ARGS) -cover $(PACKAGES) -check.p true -check.timeout 4s -timeout 4s || { $(FAILPOINT_DISABLE); exit 1; } endif @$(FAILPOINT_DISABLE) @@ -259,7 +259,7 @@ ifeq ("$(pkg)", "") else @echo "Running unit test for github.com/pingcap/tidb/$(pkg)" @export log_level=fatal; export TZ='Asia/Shanghai'; \ - $(GOTEST) -ldflags '$(TEST_LDFLAGS)' -cover github.com/pingcap/tidb/$(pkg) -check.p true -check.timeout 4s || { $(FAILPOINT_DISABLE); exit 1; } + $(GOTEST) -ldflags '$(TEST_LDFLAGS)' -cover github.com/pingcap/tidb/$(pkg) -check.p true -check.timeout 4s -timeout 4s || { $(FAILPOINT_DISABLE); exit 1; } endif @$(FAILPOINT_DISABLE) diff --git a/errno/infoschema_test.go b/errno/infoschema_test.go index 17d217b2e6c80..c30f540c51aa1 100644 --- a/errno/infoschema_test.go +++ b/errno/infoschema_test.go @@ -16,6 +16,7 @@ package errno import ( "testing" + _ "github.com/pingcap/tidb/util/testbridge" "github.com/stretchr/testify/assert" ) diff --git a/util/testbridge/bridge.go b/util/testbridge/bridge.go new file mode 100644 index 0000000000000..7ebd592843c3c --- /dev/null +++ b/util/testbridge/bridge.go @@ -0,0 +1,25 @@ +// Copyright 2021 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build !codes + +package testbridge + +import ( + "flag" +) + +var ( + _ = flag.Duration("check.timeout", 0, "timeout value for each function") + _ = flag.Bool("check.p", false, "Run suites in parallel") +) From f656a45cc0529406ee509fe8d9ad2785c42dac60 Mon Sep 17 00:00:00 2001 From: tison Date: Fri, 9 Jul 2021 15:07:39 +0800 Subject: [PATCH 3/6] Apply suggestions from code review --- .build.ps1 | 2 +- Makefile | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.build.ps1 b/.build.ps1 index 9674bdaf11464..bcd506c49f874 100644 --- a/.build.ps1 +++ b/.build.ps1 @@ -296,7 +296,7 @@ task GoTest BuildFailPoint, { } $env:log_level = 'fatal' $env:TZ = 'Asia/Shanghai' - exec { & $GO test -p $P -ldflags "`"$testFlags`"" -cover $packages '-check.p' true '-check.timeout' 4s '-timeout' 4s } + exec { & $GO test -p $P -ldflags "`"$testFlags`"" -cover $packages '-check.p' true '-check.timeout' 4s } } -Done { Disable-FailPoint $env:log_level = $Task.Data.logLevel diff --git a/Makefile b/Makefile index f9424385bea53..50ae4507a3ed2 100644 --- a/Makefile +++ b/Makefile @@ -139,7 +139,7 @@ ifeq ("$(TRAVIS_COVERAGE)", "1") else @echo "Running in native mode." @export log_level=info; export TZ='Asia/Shanghai'; \ - $(GOTEST) -ldflags '$(TEST_LDFLAGS)' $(EXTRA_TEST_ARGS) -cover $(PACKAGES) -check.p true -check.timeout 4s -timeout 4s || { $(FAILPOINT_DISABLE); exit 1; } + $(GOTEST) -ldflags '$(TEST_LDFLAGS)' $(EXTRA_TEST_ARGS) -cover $(PACKAGES) -check.p true -check.timeout 4s || { $(FAILPOINT_DISABLE); exit 1; } endif @$(FAILPOINT_DISABLE) @@ -259,7 +259,7 @@ ifeq ("$(pkg)", "") else @echo "Running unit test for github.com/pingcap/tidb/$(pkg)" @export log_level=fatal; export TZ='Asia/Shanghai'; \ - $(GOTEST) -ldflags '$(TEST_LDFLAGS)' -cover github.com/pingcap/tidb/$(pkg) -check.p true -check.timeout 4s -timeout 4s || { $(FAILPOINT_DISABLE); exit 1; } + $(GOTEST) -ldflags '$(TEST_LDFLAGS)' -cover github.com/pingcap/tidb/$(pkg) -check.p true -check.timeout 4s || { $(FAILPOINT_DISABLE); exit 1; } endif @$(FAILPOINT_DISABLE) From 9d49ce9d64d79e9f0c48f43363829711dd938314 Mon Sep 17 00:00:00 2001 From: tison Date: Fri, 9 Jul 2021 15:52:39 +0800 Subject: [PATCH 4/6] improve bridges Signed-off-by: tison --- errno/infoschema_test.go | 1 - errno/main_test.go | 13 +++++++++++++ util/testbridge/bridge.go | 12 ++++++++---- 3 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 errno/main_test.go diff --git a/errno/infoschema_test.go b/errno/infoschema_test.go index c30f540c51aa1..17d217b2e6c80 100644 --- a/errno/infoschema_test.go +++ b/errno/infoschema_test.go @@ -16,7 +16,6 @@ package errno import ( "testing" - _ "github.com/pingcap/tidb/util/testbridge" "github.com/stretchr/testify/assert" ) diff --git a/errno/main_test.go b/errno/main_test.go new file mode 100644 index 0000000000000..765133a09187c --- /dev/null +++ b/errno/main_test.go @@ -0,0 +1,13 @@ +package errno + +import ( + "os" + "testing" + + "github.com/pingcap/tidb/util/testbridge" +) + +func TestMain(m *testing.M) { + testbridge.WorkaroundGoCheckFlags() + os.Exit(m.Run()) +} diff --git a/util/testbridge/bridge.go b/util/testbridge/bridge.go index 7ebd592843c3c..246a5d2819a4f 100644 --- a/util/testbridge/bridge.go +++ b/util/testbridge/bridge.go @@ -19,7 +19,11 @@ import ( "flag" ) -var ( - _ = flag.Duration("check.timeout", 0, "timeout value for each function") - _ = flag.Bool("check.p", false, "Run suites in parallel") -) +func WorkaroundGoCheckFlags() { + if flag.Lookup("check.timeout") == nil { + _ = flag.Duration("check.timeout", 0, "WorkaroundGoCheckFlags: check.timeout") + } + if flag.Lookup("check.p") == nil { + _ = flag.Bool("check.p", false, "WorkaroundGoCheckFlags: check.p") + } +} From e9fca633f46272e8069c8b4284756ff3472ceabb Mon Sep 17 00:00:00 2001 From: tison Date: Fri, 9 Jul 2021 15:53:05 +0800 Subject: [PATCH 5/6] license Signed-off-by: tison --- errno/main_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/errno/main_test.go b/errno/main_test.go index 765133a09187c..f8becfac4d3f0 100644 --- a/errno/main_test.go +++ b/errno/main_test.go @@ -1,3 +1,16 @@ +// Copyright 2021 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// See the License for the specific language governing permissions and +// limitations under the License. + package errno import ( From f4d30adaa5ede227dcdbe2326a85e54ce0fa1b9d Mon Sep 17 00:00:00 2001 From: tison Date: Fri, 9 Jul 2021 22:22:25 +0800 Subject: [PATCH 6/6] testbridge: add func comment Signed-off-by: tison --- util/testbridge/bridge.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/util/testbridge/bridge.go b/util/testbridge/bridge.go index 246a5d2819a4f..e792bfbb879ac 100644 --- a/util/testbridge/bridge.go +++ b/util/testbridge/bridge.go @@ -19,6 +19,11 @@ import ( "flag" ) +// WorkaroundGoCheckFlags registers flags of go-check for pkg does not import go-check +// to workaround the go-check flags passed in Makefile. +// +// TODO: Remove this function when the migration from go-check to testify[1] is done. +// [1] https://github.com/pingcap/tidb/issues/26022 func WorkaroundGoCheckFlags() { if flag.Lookup("check.timeout") == nil { _ = flag.Duration("check.timeout", 0, "WorkaroundGoCheckFlags: check.timeout")