mirror of
https://gitee.com/milvus-io/milvus.git
synced 2024-12-01 03:18:29 +08:00
test: use T.TempDir
to create temporary test directory (#16758)
This commit replaces `ioutil.TempDir` with `t.TempDir` in tests. The directory created by `t.TempDir` is automatically removed when the test and all its subtests complete. Prior to this commit, temporary directory created using `ioutil.TempDir` needs to be removed manually by calling `os.RemoveAll`, which is omitted in some tests. The error handling boilerplate e.g. defer func() { if err := os.RemoveAll(dir); err != nil { t.Fatal(err) } } is also tedious, but `t.TempDir` handles this for us nicely. Fixes: #16759 Reference: https://pkg.go.dev/testing#T.TempDir Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
parent
21a1311f66
commit
5375161777
@ -18,7 +18,6 @@ package etcdkv_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -40,17 +39,11 @@ func TestEmbedEtcd(te *testing.T) {
|
|||||||
param.BaseTable.Save("etcd.use.embed", "true")
|
param.BaseTable.Save("etcd.use.embed", "true")
|
||||||
param.BaseTable.Save("etcd.config.path", "../../../configs/advanced/etcd.yaml")
|
param.BaseTable.Save("etcd.config.path", "../../../configs/advanced/etcd.yaml")
|
||||||
|
|
||||||
dir, err := ioutil.TempDir(os.TempDir(), "kv_etcd")
|
dir := te.TempDir()
|
||||||
assert.Nil(te, err)
|
|
||||||
param.BaseTable.Save("etcd.data.dir", dir)
|
param.BaseTable.Save("etcd.data.dir", dir)
|
||||||
|
|
||||||
param.EtcdCfg.LoadCfgToMemory()
|
param.EtcdCfg.LoadCfgToMemory()
|
||||||
|
|
||||||
//clean up data
|
|
||||||
defer func() {
|
|
||||||
err = os.RemoveAll(dir)
|
|
||||||
}()
|
|
||||||
|
|
||||||
te.Run("EtcdKV SaveAndLoad", func(t *testing.T) {
|
te.Run("EtcdKV SaveAndLoad", func(t *testing.T) {
|
||||||
rootPath := "/etcd/test/root/saveandload"
|
rootPath := "/etcd/test/root/saveandload"
|
||||||
metaKv, err := embed_etcd_kv.NewMetaKvFactory(rootPath, ¶m.EtcdCfg)
|
metaKv, err := embed_etcd_kv.NewMetaKvFactory(rootPath, ¶m.EtcdCfg)
|
||||||
|
@ -33,8 +33,6 @@ package log
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -82,8 +80,7 @@ func TestZapTextEncoder(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestInvalidFileConfig(t *testing.T) {
|
func TestInvalidFileConfig(t *testing.T) {
|
||||||
tmpDir, _ := ioutil.TempDir("/tmp", "invalid-log-test")
|
tmpDir := t.TempDir()
|
||||||
defer os.RemoveAll(tmpDir)
|
|
||||||
|
|
||||||
invalidFileConf := FileLogConfig{
|
invalidFileConf := FileLogConfig{
|
||||||
Filename: tmpDir,
|
Filename: tmpDir,
|
||||||
|
@ -35,7 +35,6 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math"
|
"math"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -114,14 +113,14 @@ func TestLog(t *testing.T) {
|
|||||||
zap.Duration("duration", 10*time.Second),
|
zap.Duration("duration", 10*time.Second),
|
||||||
)
|
)
|
||||||
ts.assertMessages(
|
ts.assertMessages(
|
||||||
`[INFO] [zap_log_test.go:66] ["failed to fetch URL"] [url=http://example.com] [attempt=3] [backoff=1s]`,
|
`[INFO] [zap_log_test.go:65] ["failed to fetch URL"] [url=http://example.com] [attempt=3] [backoff=1s]`,
|
||||||
`[INFO] [zap_log_test.go:71] ["failed to \"fetch\" [URL]: http://example.com"]`,
|
`[INFO] [zap_log_test.go:70] ["failed to \"fetch\" [URL]: http://example.com"]`,
|
||||||
`[DEBUG] [zap_log_test.go:72] ["Slow query"] [sql="SELECT * FROM TABLE\n\tWHERE ID=\"abc\""] [duration=1.3s] ["process keys"=1500]`,
|
`[DEBUG] [zap_log_test.go:71] ["Slow query"] [sql="SELECT * FROM TABLE\n\tWHERE ID=\"abc\""] [duration=1.3s] ["process keys"=1500]`,
|
||||||
`[INFO] [zap_log_test.go:78] [Welcome]`,
|
`[INFO] [zap_log_test.go:77] [Welcome]`,
|
||||||
`[INFO] [zap_log_test.go:79] [欢迎]`,
|
`[INFO] [zap_log_test.go:78] [欢迎]`,
|
||||||
`[WARN] [zap_log_test.go:80] [Type] [Counter=NaN] [Score=+Inf]`,
|
`[WARN] [zap_log_test.go:79] [Type] [Counter=NaN] [Score=+Inf]`,
|
||||||
`[INFO] [zap_log_test.go:85] ["new connection"] [connID=1] [traceID=dse1121]`,
|
`[INFO] [zap_log_test.go:84] ["new connection"] [connID=1] [traceID=dse1121]`,
|
||||||
`[INFO] [zap_log_test.go:86] ["Testing typs"] [filed1=noquote] `+
|
`[INFO] [zap_log_test.go:85] ["Testing typs"] [filed1=noquote] `+
|
||||||
`[filed2="in quote"] [urls="[http://mock1.com:2347,http://mock2.com:2432]"] `+
|
`[filed2="in quote"] [urls="[http://mock1.com:2347,http://mock2.com:2432]"] `+
|
||||||
`[urls-peer="[t1,\"t2 fine\"]"] ["store ids"="[1,4,5]"] [object="{username=user1}"] `+
|
`[urls-peer="[t1,\"t2 fine\"]"] ["store ids"="[1,4,5]"] [object="{username=user1}"] `+
|
||||||
`[object2="{username=\"user 2\"}"] [binary="YWIxMjM="] ["is processed"=true] `+
|
`[object2="{username=\"user 2\"}"] [binary="YWIxMjM="] ["is processed"=true] `+
|
||||||
@ -192,8 +191,8 @@ func TestLogJSON(t *testing.T) {
|
|||||||
"backoff", time.Second,
|
"backoff", time.Second,
|
||||||
)
|
)
|
||||||
logger.With(zap.String("connID", "1"), zap.String("traceID", "dse1121")).Info("new connection")
|
logger.With(zap.String("connID", "1"), zap.String("traceID", "dse1121")).Info("new connection")
|
||||||
ts.assertMessages("{\"level\":\"INFO\",\"caller\":\"zap_log_test.go:189\",\"message\":\"failed to fetch URL\",\"url\":\"http://example.com\",\"attempt\":3,\"backoff\":\"1s\"}",
|
ts.assertMessages("{\"level\":\"INFO\",\"caller\":\"zap_log_test.go:188\",\"message\":\"failed to fetch URL\",\"url\":\"http://example.com\",\"attempt\":3,\"backoff\":\"1s\"}",
|
||||||
"{\"level\":\"INFO\",\"caller\":\"zap_log_test.go:194\",\"message\":\"new connection\",\"connID\":\"1\",\"traceID\":\"dse1121\"}")
|
"{\"level\":\"INFO\",\"caller\":\"zap_log_test.go:193\",\"message\":\"new connection\",\"connID\":\"1\",\"traceID\":\"dse1121\"}")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRotateLog(t *testing.T) {
|
func TestRotateLog(t *testing.T) {
|
||||||
@ -207,7 +206,7 @@ func TestRotateLog(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
t.Run(c.desc, func(t *testing.T) {
|
t.Run(c.desc, func(t *testing.T) {
|
||||||
tempDir, _ := ioutil.TempDir("/tmp", "pd-test-log")
|
tempDir := t.TempDir()
|
||||||
conf := &Config{
|
conf := &Config{
|
||||||
Level: "info",
|
Level: "info",
|
||||||
File: FileLogConfig{
|
File: FileLogConfig{
|
||||||
@ -229,7 +228,6 @@ func TestRotateLog(t *testing.T) {
|
|||||||
}
|
}
|
||||||
files, _ := ioutil.ReadDir(tempDir)
|
files, _ := ioutil.ReadDir(tempDir)
|
||||||
assert.Len(t, files, c.expectedFileNum)
|
assert.Len(t, files, c.expectedFileNum)
|
||||||
_ = os.RemoveAll(tempDir)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,6 @@ package msgstream
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -42,13 +41,12 @@ func TestPmsFactory(t *testing.T) {
|
|||||||
func TestRmsFactory(t *testing.T) {
|
func TestRmsFactory(t *testing.T) {
|
||||||
defer os.Unsetenv("ROCKSMQ_PATH")
|
defer os.Unsetenv("ROCKSMQ_PATH")
|
||||||
|
|
||||||
dir, err := ioutil.TempDir(os.TempDir(), "mq")
|
dir := t.TempDir()
|
||||||
assert.Nil(t, err)
|
|
||||||
|
|
||||||
rmsFactory := NewRmsFactory(dir)
|
rmsFactory := NewRmsFactory(dir)
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
_, err = rmsFactory.NewMsgStream(ctx)
|
_, err := rmsFactory.NewMsgStream(ctx)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
_, err = rmsFactory.NewTtMsgStream(ctx)
|
_, err = rmsFactory.NewTtMsgStream(ctx)
|
||||||
@ -56,9 +54,6 @@ func TestRmsFactory(t *testing.T) {
|
|||||||
|
|
||||||
_, err = rmsFactory.NewQueryMsgStream(ctx)
|
_, err = rmsFactory.NewQueryMsgStream(ctx)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
err = os.RemoveAll(dir)
|
|
||||||
assert.Nil(t, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestKafkaFactory(t *testing.T) {
|
func TestKafkaFactory(t *testing.T) {
|
||||||
|
Loading…
Reference in New Issue
Block a user