Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
*: replace os.MkdirTemp with t.TempDir (#32071)
Browse files Browse the repository at this point in the history
  • Loading branch information
disksing authored Jan 30, 2022
1 parent 0c37906 commit ab8f063
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 96 deletions.
7 changes: 2 additions & 5 deletions pkg/lightning/checkpoints/checkpoints_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package checkpoints_test

import (
"context"
"os"
"path/filepath"
"sort"
"testing"
Expand All @@ -28,14 +27,13 @@ func newTestConfig() *config.Config {
}

func newFileCheckpointsDB(t *testing.T) (*checkpoints.FileCheckpointsDB, func()) {
dir, err := os.MkdirTemp("", "checkpointDB***")
require.NoError(t, err)
dir := t.TempDir()
cpdb := checkpoints.NewFileCheckpointsDB(filepath.Join(dir, "cp.pb"))
ctx := context.Background()

// 2. initialize with checkpoint data.
cfg := newTestConfig()
err = cpdb.Initialize(ctx, cfg, map[string]*checkpoints.TidbDBInfo{
err := cpdb.Initialize(ctx, cfg, map[string]*checkpoints.TidbDBInfo{
"db1": {
Name: "db1",
Tables: map[string]*checkpoints.TidbTableInfo{
Expand Down Expand Up @@ -124,7 +122,6 @@ func newFileCheckpointsDB(t *testing.T) (*checkpoints.FileCheckpointsDB, func())
return cpdb, func() {
err := cpdb.Close()
require.NoError(t, err)
os.RemoveAll(dir)
}
}

Expand Down
5 changes: 1 addition & 4 deletions pkg/lightning/checkpoints/checkpoints_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package checkpoints

import (
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -284,9 +283,7 @@ func TestApplyDiff(t *testing.T) {
}

func TestCheckpointMarshallUnmarshall(t *testing.T) {
dir, err := os.MkdirTemp("", "cptest")
require.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
path := filepath.Join(dir, "filecheckpoint")
fileChkp := NewFileCheckpointsDB(path)
fileChkp.checkpoints.Checkpoints["a"] = &checkpointspb.TableCheckpointModel{
Expand Down
6 changes: 2 additions & 4 deletions pkg/lightning/common/security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,9 @@ func TestGetJSONSecure(t *testing.T) {
}

func TestInvalidTLS(t *testing.T) {
tempDir, err := os.MkdirTemp("", "security_test")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()
caPath := filepath.Join(tempDir, "ca.pem")
_, err = common.NewTLS(caPath, "", "", "localhost")
_, err := common.NewTLS(caPath, "", "", "localhost")
require.Regexp(t, "could not read ca certificate:.*", err.Error())

err = os.WriteFile(caPath, []byte("invalid ca content"), 0o644)
Expand Down
5 changes: 1 addition & 4 deletions pkg/lightning/common/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package common_test

import (
"os"
"testing"

"github.com/pingcap/tidb/br/pkg/lightning/common"
Expand All @@ -24,9 +23,7 @@ import (

func TestGetStorageSize(t *testing.T) {
// only ensure we can get storage size.
d, err := os.MkdirTemp("", "store_size")
require.NoError(t, err)
defer os.RemoveAll(d)
d := t.TempDir()
size, err := common.GetStorageSize(d)
require.NoError(t, err)
require.Greater(t, size.Capacity, uint64(0))
Expand Down
15 changes: 5 additions & 10 deletions pkg/lightning/mydump/csv_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -955,11 +955,10 @@ type benchCSVParserSuite struct {
ioWorkers *worker.Pool
}

func newBenchCSVParserSuite(b *testing.B) (*benchCSVParserSuite, func()) {
func newBenchCSVParserSuite(b *testing.B) *benchCSVParserSuite {
var s benchCSVParserSuite
s.ioWorkers = worker.NewPool(context.Background(), 5, "bench_csv")
dir, err := os.MkdirTemp("", "bench_csv")
require.NoError(b, err)
dir := b.TempDir()
s.csvPath = filepath.Join(dir, "input.csv")
file, err := os.Create(s.csvPath)
require.NoError(b, err)
Expand All @@ -970,14 +969,11 @@ func newBenchCSVParserSuite(b *testing.B) (*benchCSVParserSuite, func()) {
_, err = file.WriteString("18,1,1,0.3650,GC,BARBARBAR,rw9AOV1AjoI1,50000.00,-10.00,10.00,1,1,djj3Q2XaIPoYVy1FuF,gc80Q2o82Au3C9xv,PYOolSxG3w,DI,265111111,7586538936787184,2020-02-26 20:06:00.193,OE,YCkSPBVqoJ2V5F8zWs87V5XzbaIY70aWCD4dgcB6bjUzCr5wOJCJ2TYH49J7yWyysbudJIxlTAEWSJahY7hswLtTsqyjEkrlsN8iDMAa9Poj29miJ08tnn2G8mL64IlyywvnRGbLbyGvWDdrOSF42RyUFTWVyqlDWc6Gr5wyMPYgvweKemzFDVD3kro5JsmBmJY08EK54nQoyfo2sScyb34zcM9GFo9ZQTwloINfPYQKXQm32m0XvU7jiNmYpFTFJQjdqA825SEvQqMMefG2WG4jVu9UPdhdUjRsFRd0Gw7YPKByOlcuY0eKxT7sAzMKXx2000RR6dqHNXe47oVYd\n")
require.NoError(b, err)
}
return &s, func() {
require.NoError(b, os.RemoveAll(dir))
}
return &s
}

func BenchmarkReadRowUsingMydumpCSVParser(b *testing.B) {
s, clean := newBenchCSVParserSuite(b)
defer clean()
s := newBenchCSVParserSuite(b)

file, err := os.Open(s.csvPath)
require.NoError(b, err)
Expand Down Expand Up @@ -1007,8 +1003,7 @@ func BenchmarkReadRowUsingMydumpCSVParser(b *testing.B) {
}

func BenchmarkReadRowUsingEncodingCSV(b *testing.B) {
s, clean := newBenchCSVParserSuite(b)
defer clean()
s := newBenchCSVParserSuite(b)

file, err := os.Open(s.csvPath)
require.NoError(b, err)
Expand Down
57 changes: 19 additions & 38 deletions pkg/lightning/mydump/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,13 @@ func newConfigWithSourceDir(sourceDir string) *config.Config {
}
}

func newTestMydumpLoaderSuite(t *testing.T) (*testMydumpLoaderSuite, func()) {
func newTestMydumpLoaderSuite(t *testing.T) *testMydumpLoaderSuite {
var s testMydumpLoaderSuite
var err error
s.sourceDir, err = os.MkdirTemp("", "mydump-loader-test")
s.sourceDir = t.TempDir()
require.Nil(t, err)
s.cfg = newConfigWithSourceDir(s.sourceDir)
return &s, func() {
err := os.RemoveAll(s.sourceDir)
require.NoError(t, err)
}
return &s
}

func (s *testMydumpLoaderSuite) touch(t *testing.T, filename ...string) {
Expand Down Expand Up @@ -106,8 +103,7 @@ func TestLoader(t *testing.T) {
}

func TestEmptyDB(t *testing.T) {
s, clean := newTestMydumpLoaderSuite(t)
defer clean()
s := newTestMydumpLoaderSuite(t)
_, err := md.NewMyDumpLoader(context.Background(), s.cfg)
// will check schema in tidb and data file later in DataCheck.
require.NoError(t, err)
Expand All @@ -121,8 +117,7 @@ func TestDuplicatedDB(t *testing.T) {
b/
db-schema-create.sql
*/
s, clean := newTestMydumpLoaderSuite(t)
defer clean()
s := newTestMydumpLoaderSuite(t)
s.mkdir(t, "a")
s.touch(t, "a", "db-schema-create.sql")
s.mkdir(t, "b")
Expand All @@ -138,8 +133,7 @@ func TestTableNoHostDB(t *testing.T) {
notdb-schema-create.sql
db.tbl-schema.sql
*/
s, clean := newTestMydumpLoaderSuite(t)
defer clean()
s := newTestMydumpLoaderSuite(t)

dir := s.sourceDir
err := os.WriteFile(filepath.Join(dir, "notdb-schema-create.sql"), nil, 0o644)
Expand All @@ -160,8 +154,7 @@ func TestDuplicatedTable(t *testing.T) {
b/
db.tbl-schema.sql
*/
s, clean := newTestMydumpLoaderSuite(t)
defer clean()
s := newTestMydumpLoaderSuite(t)

s.touch(t, "db-schema-create.sql")
s.mkdir(t, "a")
Expand All @@ -174,8 +167,7 @@ func TestDuplicatedTable(t *testing.T) {
}

func TestTableInfoNotFound(t *testing.T) {
s, clean := newTestMydumpLoaderSuite(t)
defer clean()
s := newTestMydumpLoaderSuite(t)

s.cfg.Mydumper.CharacterSet = "auto"

Expand All @@ -201,8 +193,7 @@ func TestTableInfoNotFound(t *testing.T) {
}

func TestTableUnexpectedError(t *testing.T) {
s, clean := newTestMydumpLoaderSuite(t)
defer clean()
s := newTestMydumpLoaderSuite(t)
s.touch(t, "db-schema-create.sql")
s.touch(t, "db.tbl-schema.sql")

Expand All @@ -227,8 +218,7 @@ func TestDataNoHostDB(t *testing.T) {
notdb-schema-create.sql
db.tbl.sql
*/
s, clean := newTestMydumpLoaderSuite(t)
defer clean()
s := newTestMydumpLoaderSuite(t)

s.touch(t, "notdb-schema-create.sql")
s.touch(t, "db.tbl.sql")
Expand All @@ -244,8 +234,7 @@ func TestDataNoHostTable(t *testing.T) {
db-schema-create.sql
db.tbl.sql
*/
s, clean := newTestMydumpLoaderSuite(t)
defer clean()
s := newTestMydumpLoaderSuite(t)

s.touch(t, "db-schema-create.sql")
s.touch(t, "db.tbl.sql")
Expand All @@ -261,8 +250,7 @@ func TestViewNoHostDB(t *testing.T) {
notdb-schema-create.sql
db.tbl-schema-view.sql
*/
s, clean := newTestMydumpLoaderSuite(t)
defer clean()
s := newTestMydumpLoaderSuite(t)

s.touch(t, "notdb-schema-create.sql")
s.touch(t, "db.tbl-schema-view.sql")
Expand All @@ -277,8 +265,7 @@ func TestViewNoHostTable(t *testing.T) {
db-schema-create.sql
db.tbl-schema-view.sql
*/
s, clean := newTestMydumpLoaderSuite(t)
defer clean()
s := newTestMydumpLoaderSuite(t)

s.touch(t, "db-schema-create.sql")
s.touch(t, "db.tbl-schema-view.sql")
Expand All @@ -288,8 +275,7 @@ func TestViewNoHostTable(t *testing.T) {
}

func TestDataWithoutSchema(t *testing.T) {
s, clean := newTestMydumpLoaderSuite(t)
defer clean()
s := newTestMydumpLoaderSuite(t)

dir := s.sourceDir
p := filepath.Join(dir, "db.tbl.sql")
Expand Down Expand Up @@ -319,8 +305,7 @@ func TestDataWithoutSchema(t *testing.T) {
}

func TestTablesWithDots(t *testing.T) {
s, clean := newTestMydumpLoaderSuite(t)
defer clean()
s := newTestMydumpLoaderSuite(t)

s.touch(t, "db-schema-create.sql")
s.touch(t, "db.tbl.with.dots-schema.sql")
Expand Down Expand Up @@ -361,8 +346,7 @@ func TestTablesWithDots(t *testing.T) {
}

func TestRouter(t *testing.T) {
s, clean := newTestMydumpLoaderSuite(t)
defer clean()
s := newTestMydumpLoaderSuite(t)
s.cfg.Routes = []*router.TableRule{
{
SchemaPattern: "a*",
Expand Down Expand Up @@ -526,8 +510,7 @@ func TestRouter(t *testing.T) {
}

func TestBadRouterRule(t *testing.T) {
s, clean := newTestMydumpLoaderSuite(t)
defer clean()
s := newTestMydumpLoaderSuite(t)

s.cfg.Routes = []*router.TableRule{{
SchemaPattern: "a*b",
Expand All @@ -541,8 +524,7 @@ func TestBadRouterRule(t *testing.T) {
}

func TestFileRouting(t *testing.T) {
s, clean := newTestMydumpLoaderSuite(t)
defer clean()
s := newTestMydumpLoaderSuite(t)

s.cfg.Mydumper.DefaultFileRules = false
s.cfg.Mydumper.FileRouters = []*config.FileRouteRule{
Expand Down Expand Up @@ -683,8 +665,7 @@ func TestInputWithSpecialChars(t *testing.T) {
db%22.t%2522-schema.sql
db%22.t%2522.0.csv
*/
s, clean := newTestMydumpLoaderSuite(t)
defer clean()
s := newTestMydumpLoaderSuite(t)

s.touch(t, "test-schema-create.sql")
s.touch(t, "test.t%22-schema.sql")
Expand Down
9 changes: 2 additions & 7 deletions pkg/lightning/mydump/parquet_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mydump
import (
"context"
"io"
"os"
"path/filepath"
"strconv"
"testing"
Expand All @@ -22,9 +21,7 @@ func TestParquetParser(t *testing.T) {
A int32 `parquet:"name=a_A, type=INT32"`
}

dir, err := os.MkdirTemp("", "parquet_test")
require.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
// prepare data
name := "test123.parquet"
testPath := filepath.Join(dir, name)
Expand Down Expand Up @@ -95,9 +92,7 @@ func TestParquetVariousTypes(t *testing.T) {
Decimal6 int32 `parquet:"name=decimal6, type=DECIMAL, scale=4, precision=4, basetype=INT32"`
}

dir, err := os.MkdirTemp("", "parquet_test")
require.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
// prepare data
name := "test123.parquet"
testPath := filepath.Join(dir, name)
Expand Down
16 changes: 4 additions & 12 deletions pkg/lightning/mydump/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ import (
)

func TestExportStatementNoTrailingNewLine(t *testing.T) {
dir, err := os.MkdirTemp("", "reader_test")
require.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
file, err := os.Create(filepath.Join(dir, "tidb_lightning_test_reader"))
require.NoError(t, err)
defer os.Remove(file.Name())
Expand Down Expand Up @@ -78,9 +76,7 @@ func TestExportStatementWithCommentNoTrailingNewLine(t *testing.T) {
}

func exportStatmentShouldBe(t *testing.T, stmt string, expected string) {
dir, err := os.MkdirTemp("", "reader_test")
require.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
file, err := os.Create(filepath.Join(dir, "tidb_lightning_test_reader"))
require.NoError(t, err)
defer os.Remove(file.Name())
Expand All @@ -101,9 +97,7 @@ func exportStatmentShouldBe(t *testing.T, stmt string, expected string) {
}

func TestExportStatementGBK(t *testing.T) {
dir, err := os.MkdirTemp("", "reader_test")
require.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
file, err := os.Create(filepath.Join(dir, "tidb_lightning_test_reader"))
require.NoError(t, err)
defer os.Remove(file.Name())
Expand All @@ -129,9 +123,7 @@ func TestExportStatementGBK(t *testing.T) {
}

func TestExportStatementGibberishError(t *testing.T) {
dir, err := os.MkdirTemp("", "reader_test")
require.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
file, err := os.Create(filepath.Join(dir, "tidb_lightning_test_reader"))
require.NoError(t, err)
defer os.Remove(file.Name())
Expand Down
Loading

0 comments on commit ab8f063

Please sign in to comment.