Skip to content

Commit

Permalink
tests: fix all errors detected by testifylint and enable it (#7589)
Browse files Browse the repository at this point in the history
ref #4813

Fix all errors detected by testifylint and enable it.

Signed-off-by: JmPotato <ghzpotato@gmail.com>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
JmPotato and ti-chi-bot[bot] authored Dec 21, 2023
1 parent 66508f5 commit 7f156d9
Show file tree
Hide file tree
Showing 71 changed files with 2,969 additions and 2,729 deletions.
3 changes: 1 addition & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ linters:
- makezero
- gosec
- bodyclose
# TODO: enable when all existing errors are fixed
# - testifylint
- testifylint
disable:
- errcheck
linters-settings:
Expand Down
4 changes: 2 additions & 2 deletions client/http/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ func TestPDAddrNormalization(t *testing.T) {
re := require.New(t)
c := NewClient("test-http-pd-addr", []string{"127.0.0.1"})
pdAddrs, leaderAddrIdx := c.(*client).inner.getPDAddrs()
re.Equal(1, len(pdAddrs))
re.Len(pdAddrs, 1)
re.Equal(-1, leaderAddrIdx)
re.Contains(pdAddrs[0], httpScheme)
c = NewClient("test-https-pd-addr", []string{"127.0.0.1"}, WithTLSConfig(&tls.Config{}))
pdAddrs, leaderAddrIdx = c.(*client).inner.getPDAddrs()
re.Equal(1, len(pdAddrs))
re.Len(pdAddrs, 1)
re.Equal(-1, leaderAddrIdx)
re.Contains(pdAddrs[0], httpsScheme)
}
Expand Down
2 changes: 1 addition & 1 deletion client/http/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestMergeRegionsInfo(t *testing.T) {
}
regionsInfo := regionsInfo1.Merge(regionsInfo2)
re.Equal(int64(2), regionsInfo.Count)
re.Equal(2, len(regionsInfo.Regions))
re.Len(regionsInfo.Regions, 2)
re.Subset(regionsInfo.Regions, append(regionsInfo1.Regions, regionsInfo2.Regions...))
}

Expand Down
2 changes: 1 addition & 1 deletion client/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestDynamicOptionChange(t *testing.T) {
re.Equal(defaultEnableFollowerHandle, o.getEnableFollowerHandle())

// Check the invalid value setting.
re.NotNil(o.setMaxTSOBatchWaitInterval(time.Second))
re.Error(o.setMaxTSOBatchWaitInterval(time.Second))
re.Equal(defaultMaxTSOBatchWaitInterval, o.getMaxTSOBatchWaitInterval())
expectInterval := time.Millisecond
o.setMaxTSOBatchWaitInterval(expectInterval)
Expand Down
2 changes: 1 addition & 1 deletion client/resource_group/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestGroupControlBurstable(t *testing.T) {
counter.limiter.Reconfigure(time.Now(), args)
}
gc.updateAvgRequestResourcePerSec()
re.Equal(gc.burstable.Load(), true)
re.True(gc.burstable.Load())
}

func TestRequestAndResponseConsumption(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion client/resource_group/controller/limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func TestCancel(t *testing.T) {
checkTokens(re, lim1, t2, 7)
checkTokens(re, lim2, t2, 2)
d, err := WaitReservations(ctx, t2, []*Reservation{r1, r2})
re.Equal(d, 4*time.Second)
re.Equal(4*time.Second, d)
re.Error(err)
checkTokens(re, lim1, t3, 13)
checkTokens(re, lim2, t3, 3)
Expand Down
2 changes: 1 addition & 1 deletion client/resource_group/controller/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ func TestDurationTOML(t *testing.T) {
example := &example{}

text := []byte(`interval = "1h1m1s"`)
re.Nil(toml.Unmarshal(text, example))
re.NoError(toml.Unmarshal(text, example))
re.Equal(float64(60*60+60+1), example.Interval.Seconds())
}
28 changes: 14 additions & 14 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,13 @@ func TestFifoCache(t *testing.T) {

elems := cache.Elems()
re.Len(elems, 3)
re.Equal(elems[0].Value, "2")
re.Equal(elems[1].Value, "3")
re.Equal(elems[2].Value, "4")
re.Equal("2", elems[0].Value)
re.Equal("3", elems[1].Value)
re.Equal("4", elems[2].Value)

elems = cache.FromElems(3)
re.Len(elems, 1)
re.Equal(elems[0].Value, "4")
re.Equal("4", elems[0].Value)

cache.Remove()
cache.Remove()
Expand Down Expand Up @@ -269,15 +269,15 @@ func TestTwoQueueCache(t *testing.T) {

val, ok := cache.Get(3)
re.True(ok)
re.Equal(val, "3")
re.Equal("3", val)

val, ok = cache.Get(2)
re.True(ok)
re.Equal(val, "2")
re.Equal("2", val)

val, ok = cache.Get(1)
re.True(ok)
re.Equal(val, "1")
re.Equal("1", val)

re.Equal(3, cache.Len())

Expand All @@ -291,27 +291,27 @@ func TestTwoQueueCache(t *testing.T) {

val, ok = cache.Get(1)
re.True(ok)
re.Equal(val, "1")
re.Equal("1", val)

val, ok = cache.Get(2)
re.True(ok)
re.Equal(val, "2")
re.Equal("2", val)

val, ok = cache.Get(4)
re.True(ok)
re.Equal(val, "4")
re.Equal("4", val)

re.Equal(3, cache.Len())

val, ok = cache.Peek(1)
re.True(ok)
re.Equal(val, "1")
re.Equal("1", val)

elems := cache.Elems()
re.Len(elems, 3)
re.Equal(elems[0].Value, "4")
re.Equal(elems[1].Value, "2")
re.Equal(elems[2].Value, "1")
re.Equal("4", elems[0].Value)
re.Equal("2", elems[1].Value)
re.Equal("1", elems[2].Value)

cache.Remove(1)
cache.Remove(2)
Expand Down
25 changes: 13 additions & 12 deletions pkg/cgroup/cgroup_cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import (
"github.com/stretchr/testify/require"
)

func checkKernelVersionNewerThan(t *testing.T, major, minor int) bool {
func checkKernelVersionNewerThan(re *require.Assertions, t *testing.T, major, minor int) bool {
u := syscall.Utsname{}
err := syscall.Uname(&u)
require.NoError(t, err)
re.NoError(err)
releaseBs := make([]byte, 0, len(u.Release))
for _, v := range u.Release {
if v == 0 {
Expand All @@ -42,16 +42,16 @@ func checkKernelVersionNewerThan(t *testing.T, major, minor int) bool {
releaseStr := string(releaseBs)
t.Log("kernel release string:", releaseStr)
versionInfoRE := regexp.MustCompile(`[0-9]+\.[0-9]+\.[0-9]+`)
kernelVerion := versionInfoRE.FindAllString(releaseStr, 1)
require.Equal(t, 1, len(kernelVerion), fmt.Sprintf("release str is %s", releaseStr))
kernelVersion := versionInfoRE.FindAllString(releaseStr, 1)
re.Len(kernelVersion, 1, fmt.Sprintf("release str is %s", releaseStr))
kernelVersionPartRE := regexp.MustCompile(`[0-9]+`)
kernelVersionParts := kernelVersionPartRE.FindAllString(kernelVerion[0], -1)
require.Equal(t, 3, len(kernelVersionParts), fmt.Sprintf("kernel verion str is %s", kernelVerion[0]))
kernelVersionParts := kernelVersionPartRE.FindAllString(kernelVersion[0], -1)
re.Len(kernelVersionParts, 3, fmt.Sprintf("kernel verion str is %s", kernelVersion[0]))
t.Logf("parsed kernel version parts: major %s, minor %s, patch %s",
kernelVersionParts[0], kernelVersionParts[1], kernelVersionParts[2])
mustConvInt := func(s string) int {
i, err := strconv.Atoi(s)
require.NoError(t, err, s)
re.NoError(err, s)
return i
}
versionNewerThanFlag := false
Expand All @@ -64,6 +64,7 @@ func checkKernelVersionNewerThan(t *testing.T, major, minor int) bool {
}

func TestGetCgroupCPU(t *testing.T) {
re := require.New(t)
exit := make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
Expand All @@ -83,15 +84,15 @@ func TestGetCgroupCPU(t *testing.T) {
cpu, err := GetCgroupCPU()
if err == errNoCPUControllerDetected {
// for more information, please refer https://github.com/pingcap/tidb/pull/41347
if checkKernelVersionNewerThan(t, 4, 7) {
require.NoError(t, err, "linux version > v4.7 and err still happens")
if checkKernelVersionNewerThan(re, t, 4, 7) {
re.NoError(err, "linux version > v4.7 and err still happens")
} else {
t.Logf("the error '%s' is ignored because the kernel is too old", err)
}
} else {
require.NoError(t, err)
require.NotZero(t, cpu.Period)
require.Less(t, int64(1), cpu.Period)
re.NoError(err)
re.NotZero(cpu.Period)
re.Less(int64(1), cpu.Period)
}
close(exit)
wg.Wait()
Expand Down
10 changes: 5 additions & 5 deletions pkg/dashboard/adapter/redirector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@ func (suite *redirectorTestSuite) TestReverseProxy() {
// Test normal forwarding
req, err := http.NewRequest(http.MethodGet, redirectorServer.URL, http.NoBody)
re.NoError(err)
checkHTTPRequest(suite.Require(), suite.noRedirectHTTPClient, req, http.StatusOK, suite.tempText)
checkHTTPRequest(re, suite.noRedirectHTTPClient, req, http.StatusOK, suite.tempText)
// Test the requests that are forwarded by others
req, err = http.NewRequest(http.MethodGet, redirectorServer.URL, http.NoBody)
re.NoError(err)
req.Header.Set(proxyHeader, "other")
checkHTTPRequest(suite.Require(), suite.noRedirectHTTPClient, req, http.StatusOK, suite.tempText)
checkHTTPRequest(re, suite.noRedirectHTTPClient, req, http.StatusOK, suite.tempText)
// Test LoopDetected
suite.redirector.SetAddress(redirectorServer.URL)
req, err = http.NewRequest(http.MethodGet, redirectorServer.URL, http.NoBody)
re.NoError(err)
checkHTTPRequest(suite.Require(), suite.noRedirectHTTPClient, req, http.StatusLoopDetected, "")
checkHTTPRequest(re, suite.noRedirectHTTPClient, req, http.StatusLoopDetected, "")
}

func (suite *redirectorTestSuite) TestTemporaryRedirect() {
Expand All @@ -94,11 +94,11 @@ func (suite *redirectorTestSuite) TestTemporaryRedirect() {
// Test TemporaryRedirect
req, err := http.NewRequest(http.MethodGet, redirectorServer.URL, http.NoBody)
re.NoError(err)
checkHTTPRequest(suite.Require(), suite.noRedirectHTTPClient, req, http.StatusTemporaryRedirect, "")
checkHTTPRequest(re, suite.noRedirectHTTPClient, req, http.StatusTemporaryRedirect, "")
// Test Response
req, err = http.NewRequest(http.MethodGet, redirectorServer.URL, http.NoBody)
re.NoError(err)
checkHTTPRequest(suite.Require(), http.DefaultClient, req, http.StatusOK, suite.tempText)
checkHTTPRequest(re, http.DefaultClient, req, http.StatusOK, suite.tempText)
}

func checkHTTPRequest(re *require.Assertions, client *http.Client, req *http.Request, expectedCode int, expectedText string) {
Expand Down
Loading

0 comments on commit 7f156d9

Please sign in to comment.