tests: support configurable temp directory for brietest via BRIETEST_TMPDIR (#65315)

close pingcap/tidb#65202
This commit is contained in:
ris
2026-01-07 16:42:08 +08:00
committed by GitHub
parent 2114fbf5b6
commit bb3fa45009
4 changed files with 35 additions and 5 deletions

View File

@ -758,6 +758,7 @@ bazel_golangcilinter:
.PHONY: bazel_brietest
bazel_brietest: failpoint-enable bazel_ci_simple_prepare
bazel $(BAZEL_GLOBAL_CONFIG) test $(BAZEL_CMD_CONFIG) --test_arg=-with-real-tikv --define gotags=$(REAL_TIKV_TEST_TAGS) --jobs=1 \
--test_env=BRIETEST_TMPDIR --sandbox_writable_path=$${BRIETEST_TMPDIR:-$(CURDIR)} \
-- //tests/realtikvtest/brietest/...
.PHONY: bazel_pessimistictest

View File

@ -27,6 +27,13 @@ import (
"github.com/stretchr/testify/require"
)
func getBackupTempDir() string {
if envDir := os.Getenv("BRIETEST_TMPDIR"); envDir != "" {
return envDir
}
return os.TempDir()
}
func initTestKit(t *testing.T) *testkit.TestKit {
store := realtikvtest.CreateMockStoreAndSetup(t)
@ -53,7 +60,7 @@ func TestBackupAndRestore(t *testing.T) {
tk.MustExec("use br02")
tk.MustExec("create table t1(v int)")
tmpDir := path.Join(os.TempDir(), "bk1")
tmpDir := path.Join(getBackupTempDir(), "bk1")
require.NoError(t, os.RemoveAll(tmpDir))
// backup database to tmp dir
tk.MustQuery("backup database br to 'local://" + tmpDir + "'")
@ -83,7 +90,7 @@ func TestRestoreMultiTables(t *testing.T) {
tablesNameSet[fmt.Sprintf("table_%d", i)] = struct{}{}
}
tmpDir := path.Join(os.TempDir(), "bk1")
tmpDir := path.Join(getBackupTempDir(), "bk1")
require.NoError(t, os.RemoveAll(tmpDir))
// backup database to tmp dir
tk.MustQuery("backup database br to 'local://" + tmpDir + "'")

View File

@ -32,7 +32,13 @@ import (
)
func makeTempDirForBackup(t *testing.T) string {
d, err := os.MkdirTemp(os.TempDir(), "briesql-*")
baseDir := os.TempDir()
if envDir := os.Getenv("BRIETEST_TMPDIR"); envDir != "" {
// Ensure the base directory exists
require.NoError(t, os.MkdirAll(envDir, 0755))
baseDir = envDir
}
d, err := os.MkdirTemp(baseDir, "briesql-*")
require.NoError(t, err)
t.Cleanup(func() {
os.RemoveAll(d)

View File

@ -133,6 +133,22 @@ type LogBackupKit struct {
checkerF func(err error)
}
// getTestTempDir returns a temporary directory for tests.
// If BRIETEST_TMPDIR is set, it creates a subdirectory there (useful for CI environments
// where TiKV and test processes need to share the same filesystem).
// Otherwise, it falls back to t.TempDir().
func getTestTempDir(t *testing.T) string {
if baseDir := os.Getenv("BRIETEST_TMPDIR"); baseDir != "" {
dir := filepath.Join(baseDir, t.Name())
require.NoError(t, os.MkdirAll(dir, 0755))
t.Cleanup(func() {
os.RemoveAll(dir)
})
return dir
}
return t.TempDir()
}
func NewLogBackupKit(t *testing.T) *LogBackupKit {
tk := initTestKit(t)
metaCli := streamhelper.NewMetaDataClient(domain.GetDomain(tk.Session()).GetEtcdClient())
@ -149,7 +165,7 @@ func NewLogBackupKit(t *testing.T) *LogBackupKit {
tk: tk,
t: t,
metaCli: metaCli,
base: t.TempDir(),
base: getTestTempDir(t),
checkerF: func(err error) {
require.NoError(t, err)
},
@ -157,7 +173,7 @@ func NewLogBackupKit(t *testing.T) *LogBackupKit {
}
func (kit *LogBackupKit) tempFile(name string, content []byte) string {
path := filepath.Join(kit.t.TempDir(), name)
path := filepath.Join(kit.base, name)
require.NoError(kit.t, os.WriteFile(path, content, 0o666))
return path
}