Skip to content

Commit

Permalink
*: replace part of path with filepath (#8455)
Browse files Browse the repository at this point in the history
ref #8475

Signed-off-by: Ryan Leung <[email protected]>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
rleungx and ti-chi-bot[bot] authored Aug 1, 2024
1 parent 3ce31ef commit cb6e6e2
Show file tree
Hide file tree
Showing 19 changed files with 79 additions and 80 deletions.
13 changes: 5 additions & 8 deletions pkg/encryption/master_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package encryption
import (
"encoding/hex"
"os"
"path/filepath"
"testing"

"github.com/pingcap/kvproto/pkg/encryptionpb"
Expand Down Expand Up @@ -94,8 +95,7 @@ func TestNewFileMasterKeyMissingPath(t *testing.T) {

func TestNewFileMasterKeyMissingFile(t *testing.T) {
re := require.New(t)
dir := t.TempDir()
path := dir + "/key"
path := filepath.Join(t.TempDir(), "key")
config := &encryptionpb.MasterKey{
Backend: &encryptionpb.MasterKey_File{
File: &encryptionpb.MasterKeyFile{
Expand All @@ -109,8 +109,7 @@ func TestNewFileMasterKeyMissingFile(t *testing.T) {

func TestNewFileMasterKeyNotHexString(t *testing.T) {
re := require.New(t)
dir := t.TempDir()
path := dir + "/key"
path := filepath.Join(t.TempDir(), "key")
os.WriteFile(path, []byte("not-a-hex-string"), 0600)
config := &encryptionpb.MasterKey{
Backend: &encryptionpb.MasterKey_File{
Expand All @@ -125,8 +124,7 @@ func TestNewFileMasterKeyNotHexString(t *testing.T) {

func TestNewFileMasterKeyLengthMismatch(t *testing.T) {
re := require.New(t)
dir := t.TempDir()
path := dir + "/key"
path := filepath.Join(t.TempDir(), "key")
os.WriteFile(path, []byte("2f07ec61e5a50284f47f2b402a962ec6"), 0600)
config := &encryptionpb.MasterKey{
Backend: &encryptionpb.MasterKey_File{
Expand All @@ -142,8 +140,7 @@ func TestNewFileMasterKeyLengthMismatch(t *testing.T) {
func TestNewFileMasterKey(t *testing.T) {
re := require.New(t)
key := "2f07ec61e5a50284f47f2b402a962ec672e500b26cb3aa568bb1531300c74806" // #nosec G101
dir := t.TempDir()
path := dir + "/key"
path := filepath.Join(t.TempDir(), "key")
os.WriteFile(path, []byte(key), 0600)
config := &encryptionpb.MasterKey{
Backend: &encryptionpb.MasterKey_File{
Expand Down
3 changes: 2 additions & 1 deletion pkg/storage/hot_region_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"math/rand"
"os"
"path/filepath"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -287,7 +288,7 @@ func newTestHotRegionStorage(pullInterval time.Duration,
packHotRegionInfo *MockPackHotRegionInfo) (
hotRegionStorage *HotRegionStorage,
clear func(), err error) {
writePath := "./tmp"
writePath := strings.Join([]string{".", "tmp"}, string(filepath.Separator))
ctx := context.Background()
packHotRegionInfo.pullInterval = pullInterval
packHotRegionInfo.reservedDays = reservedDays
Expand Down
16 changes: 8 additions & 8 deletions pkg/utils/grpcutil/grpcutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"os"
"os/exec"
"path"
"path/filepath"
"testing"

"github.com/pingcap/errors"
Expand All @@ -14,8 +14,8 @@ import (
)

var (
certPath = "../../../tests/integrations/client/"
certScript = "cert_opt.sh"
certPath = filepath.Join("..", "..", "..", "tests", "integrations", "client") + string(filepath.Separator)
certScript = filepath.Join("..", "..", "..", "tests", "integrations", "client", "cert_opt.sh")
)

func loadTLSContent(re *require.Assertions, caPath, certPath, keyPath string) (caData, certData, keyData []byte) {
Expand All @@ -30,20 +30,20 @@ func loadTLSContent(re *require.Assertions, caPath, certPath, keyPath string) (c
}

func TestToTLSConfig(t *testing.T) {
if err := exec.Command(certPath+certScript, "generate", certPath).Run(); err != nil {
if err := exec.Command(certScript, "generate", certPath).Run(); err != nil {
t.Fatal(err)
}
defer func() {
if err := exec.Command(certPath+certScript, "cleanup", certPath).Run(); err != nil {
if err := exec.Command(certScript, "cleanup", certPath).Run(); err != nil {
t.Fatal(err)
}
}()

re := require.New(t)
tlsConfig := TLSConfig{
KeyPath: path.Join(certPath, "pd-server-key.pem"),
CertPath: path.Join(certPath, "pd-server.pem"),
CAPath: path.Join(certPath, "ca.pem"),
KeyPath: filepath.Join(certPath, "pd-server-key.pem"),
CertPath: filepath.Join(certPath, "pd-server.pem"),
CAPath: filepath.Join(certPath, "ca.pem"),
}
// test without bytes
_, err := tlsConfig.ToTLSConfig()
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func CleanServer(dataDir string) {
// InitTempFileLogger initializes the logger and redirects the log output to a temporary file.
func InitTempFileLogger(level string) (fname string) {
cfg := &log.Config{}
f, _ := os.CreateTemp("/tmp", "pd_tests")
f, _ := os.CreateTemp(os.TempDir(), "pd_tests")
fname = f.Name()
f.Close()
cfg.File.Filename = fname
Expand Down
4 changes: 2 additions & 2 deletions server/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"fmt"
"math"
"os"
"path"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -123,7 +123,7 @@ func TestValidation(t *testing.T) {
cfg := NewConfig()
re.NoError(cfg.Adjust(nil, false))

cfg.Log.File.Filename = path.Join(cfg.DataDir, "test")
cfg.Log.File.Filename = filepath.Join(cfg.DataDir, "test")
re.Error(cfg.Validate())

// check schedule config
Expand Down
6 changes: 3 additions & 3 deletions server/join/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package join
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -90,7 +90,7 @@ func PrepareJoinCluster(cfg *config.Config) error {
return errors.New("join self is forbidden")
}

filePath := path.Join(cfg.DataDir, "join")
filePath := filepath.Join(cfg.DataDir, "join")
// Read the persist join config
if _, err := os.Stat(filePath); !os.IsNotExist(err) {
s, err := os.ReadFile(filePath)
Expand All @@ -104,7 +104,7 @@ func PrepareJoinCluster(cfg *config.Config) error {

initialCluster := ""
// Cases with data directory.
if isDataExist(path.Join(cfg.DataDir, "member")) {
if isDataExist(filepath.Join(cfg.DataDir, "member")) {
cfg.InitialCluster = initialCluster
cfg.InitialClusterState = embed.ClusterStateFlagExisting
return nil
Expand Down
2 changes: 1 addition & 1 deletion server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func TestIsPathInDirectory(t *testing.T) {
path := filepath.Join(directory, fileName)
re.True(isPathInDirectory(path, directory))

fileName = "../../test"
fileName = filepath.Join("..", "..", "test")
path = filepath.Join(directory, fileName)
re.False(isPathInDirectory(path, directory))
}
Expand Down
2 changes: 1 addition & 1 deletion server/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func NewTestSingleConfig(c *assertutil.Checker) *config.Config {

cfg.AdvertiseClientUrls = cfg.ClientUrls
cfg.AdvertisePeerUrls = cfg.PeerUrls
cfg.DataDir, _ = os.MkdirTemp("/tmp", "test_pd")
cfg.DataDir, _ = os.MkdirTemp(os.TempDir(), "test_pd")
cfg.InitialCluster = fmt.Sprintf("pd=%s", cfg.PeerUrls)
cfg.DisableStrictReconfigCheck = true
cfg.TickInterval = typeutil.NewDuration(100 * time.Millisecond)
Expand Down
2 changes: 1 addition & 1 deletion tests/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type serverConfig struct {
}

func newServerConfig(name string, cc *clusterConfig, join bool) *serverConfig {
tempDir, _ := os.MkdirTemp("/tmp", "pd-tests")
tempDir, _ := os.MkdirTemp(os.TempDir(), "pd-tests")
return &serverConfig{
Name: name,
DataDir: tempDir,
Expand Down
24 changes: 12 additions & 12 deletions tests/integrations/client/client_tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,25 @@ import (
)

var (
certPath = "./cert"
certExpiredPath = "./cert-expired"
certScript = "./cert_opt.sh"
certPath = strings.Join([]string{".", "cert"}, string(filepath.Separator))
certExpiredPath = strings.Join([]string{".", "cert-expired"}, string(filepath.Separator))
certScript = strings.Join([]string{".", "cert_opt.sh"}, string(filepath.Separator))
testTLSInfo = transport.TLSInfo{
KeyFile: "./cert/pd-server-key.pem",
CertFile: "./cert/pd-server.pem",
TrustedCAFile: "./cert/ca.pem",
KeyFile: strings.Join([]string{".", "cert", "pd-server-key.pem"}, string(filepath.Separator)),
CertFile: strings.Join([]string{".", "cert", "pd-server.pem"}, string(filepath.Separator)),
TrustedCAFile: strings.Join([]string{".", "cert", "ca.pem"}, string(filepath.Separator)),
}

testClientTLSInfo = transport.TLSInfo{
KeyFile: "./cert/client-key.pem",
CertFile: "./cert/client.pem",
TrustedCAFile: "./cert/ca.pem",
KeyFile: strings.Join([]string{".", "cert", "client-key.pem"}, string(filepath.Separator)),
CertFile: strings.Join([]string{".", "cert", "client.pem"}, string(filepath.Separator)),
TrustedCAFile: strings.Join([]string{".", "cert", "ca.pem"}, string(filepath.Separator)),
}

testTLSInfoExpired = transport.TLSInfo{
KeyFile: "./cert-expired/pd-server-key.pem",
CertFile: "./cert-expired/pd-server.pem",
TrustedCAFile: "./cert-expired/ca.pem",
KeyFile: strings.Join([]string{".", "cert-expired", "pd-server-key.pem"}, string(filepath.Separator)),
CertFile: strings.Join([]string{".", "cert-expired", "pd-server.pem"}, string(filepath.Separator)),
TrustedCAFile: strings.Join([]string{".", "cert-expired", "ca.pem"}, string(filepath.Separator)),
}
)

Expand Down
6 changes: 3 additions & 3 deletions tests/server/join/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package join_test
import (
"context"
"os"
"path"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -56,7 +56,7 @@ func TestSimpleJoin(t *testing.T) {
re.NoError(err)
err = pd2.Run()
re.NoError(err)
_, err = os.Stat(path.Join(pd2.GetConfig().DataDir, "join"))
_, err = os.Stat(filepath.Join(pd2.GetConfig().DataDir, "join"))
re.False(os.IsNotExist(err))
members, err = etcdutil.ListEtcdMembers(ctx, client)
re.NoError(err)
Expand All @@ -71,7 +71,7 @@ func TestSimpleJoin(t *testing.T) {
re.NoError(err)
err = pd3.Run()
re.NoError(err)
_, err = os.Stat(path.Join(pd3.GetConfig().DataDir, "join"))
_, err = os.Stat(filepath.Join(pd3.GetConfig().DataDir, "join"))
re.False(os.IsNotExist(err))
members, err = etcdutil.ListEtcdMembers(ctx, client)
re.NoError(err)
Expand Down
3 changes: 2 additions & 1 deletion tools/pd-backup/pdbackup/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http/httptest"
"os"
"path"
"path/filepath"
"strconv"
"testing"
"time"
Expand Down Expand Up @@ -68,7 +69,7 @@ func setupServer() (*httptest.Server, *config.Config) {
AdvertiseClientUrls: "example.com:2380",
AdvertisePeerUrls: "example.com:2380",
Name: "test-svc",
DataDir: "/data",
DataDir: string(filepath.Separator) + "data",
ForceNewCluster: true,
EnableGRPCGateway: true,
InitialCluster: "pd1=http://127.0.0.1:10208",
Expand Down
11 changes: 6 additions & 5 deletions tools/pd-ctl/pdctl/command/global_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package command
import (
"os"
"os/exec"
"path/filepath"
"testing"

"github.com/spf13/cobra"
Expand All @@ -30,16 +31,16 @@ func TestParseTLSConfig(t *testing.T) {
Short: "Placement Driver control",
SilenceErrors: true,
}
certPath := "../../tests/cert"
rootCmd.Flags().String("cacert", certPath+"/ca.pem", "path of file that contains list of trusted SSL CAs")
rootCmd.Flags().String("cert", certPath+"/client.pem", "path of file that contains X509 certificate in PEM format")
rootCmd.Flags().String("key", certPath+"/client-key.pem", "path of file that contains X509 key in PEM format")
certPath := filepath.Join("..", "..", "tests", "cert")
rootCmd.Flags().String("cacert", filepath.Join(certPath, "ca.pem"), "path of file that contains list of trusted SSL CAs")
rootCmd.Flags().String("cert", filepath.Join(certPath, "client.pem"), "path of file that contains X509 certificate in PEM format")
rootCmd.Flags().String("key", filepath.Join(certPath, "client-key.pem"), "path of file that contains X509 key in PEM format")

// generate certs
if err := os.Mkdir(certPath, 0755); err != nil {
t.Fatal(err)
}
certScript := "../../tests/cert_opt.sh"
certScript := filepath.Join("..", "..", "tests", "cert_opt.sh")
if err := exec.Command(certScript, "generate", certPath).Run(); err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions tools/pd-ctl/tests/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func (suite *configTestSuite) checkConfigForwardControl(cluster *pdTests.TestClu
leaderServer := cluster.GetLeaderServer()
pdAddr := leaderServer.GetAddr()

f, _ := os.CreateTemp("/tmp", "pd_tests")
f, _ := os.CreateTemp(os.TempDir(), "pd_tests")
fname := f.Name()
f.Close()
defer os.RemoveAll(fname)
Expand Down Expand Up @@ -570,7 +570,7 @@ func (suite *configTestSuite) checkPlacementRules(cluster *pdTests.TestCluster)
// test show
checkShowRuleKey(re, pdAddr, [][2]string{{placement.DefaultGroupID, placement.DefaultRuleID}})

f, _ := os.CreateTemp("/tmp", "pd_tests")
f, _ := os.CreateTemp(os.TempDir(), "pd_tests")
fname := f.Name()
f.Close()
defer os.RemoveAll(fname)
Expand Down Expand Up @@ -717,7 +717,7 @@ func (suite *configTestSuite) checkPlacementRuleBundle(cluster *pdTests.TestClus
re.NoError(json.Unmarshal(output, &bundle))
re.Equal(placement.GroupBundle{ID: placement.DefaultGroupID, Index: 0, Override: false, Rules: []*placement.Rule{{GroupID: placement.DefaultGroupID, ID: placement.DefaultRuleID, Role: placement.Voter, Count: 3}}}, bundle)

f, err := os.CreateTemp("/tmp", "pd_tests")
f, err := os.CreateTemp(os.TempDir(), "pd_tests")
re.NoError(err)
fname := f.Name()
f.Close()
Expand Down
10 changes: 5 additions & 5 deletions tools/pd-ctl/tests/health/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ func TestHealthTLS(t *testing.T) {
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
certPath := "../cert"
certScript := "../cert_opt.sh"
certPath := filepath.Join("..", "cert")
certScript := filepath.Join("..", "cert_opt.sh")
// generate certs
if err := os.Mkdir(certPath, 0755); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -143,9 +143,9 @@ func TestHealthTLS(t *testing.T) {
pdAddr := tc.GetConfig().GetClientURL()
pdAddr = strings.ReplaceAll(pdAddr, "http", "https")
args := []string{"-u", pdAddr, "health",
"--cacert=../cert/ca.pem",
"--cert=../cert/client.pem",
"--key=../cert/client-key.pem"}
"--cacert=" + filepath.Join("..", "cert", "ca.pem"),
"--cert=" + filepath.Join("..", "cert", "client.pem"),
"--key=" + filepath.Join("..", "cert", "client-key.pem")}
output, err := tests.ExecuteCommand(cmd, args...)
re.NoError(err)
h := make([]api.Health, len(healths))
Expand Down
10 changes: 5 additions & 5 deletions tools/pd-ctl/tests/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,8 @@ func TestStoreTLS(t *testing.T) {
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
certPath := "../cert"
certScript := "../cert_opt.sh"
certPath := filepath.Join("..", "cert")
certScript := filepath.Join("..", "cert_opt.sh")
// generate certs
if err := os.Mkdir(certPath, 0755); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -674,9 +674,9 @@ func TestStoreTLS(t *testing.T) {
pdAddr = strings.ReplaceAll(pdAddr, "http", "https")
// store command
args := []string{"-u", pdAddr, "store",
"--cacert=../cert/ca.pem",
"--cert=../cert/client.pem",
"--key=../cert/client-key.pem"}
"--cacert=" + filepath.Join("..", "cert", "ca.pem"),
"--cert=" + filepath.Join("..", "cert", "client.pem"),
"--key=" + filepath.Join("..", "cert", "client-key.pem")}
output, err := tests.ExecuteCommand(cmd, args...)
re.NoError(err)
storesInfo := new(response.StoresInfo)
Expand Down
2 changes: 1 addition & 1 deletion tools/pd-simulator/simulator/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func NewSimConfig(serverLogLevel string) *SimConfig {

cfg.AdvertiseClientUrls = cfg.ClientUrls
cfg.AdvertisePeerUrls = cfg.PeerUrls
cfg.DataDir, _ = os.MkdirTemp("/tmp", "test_pd")
cfg.DataDir, _ = os.MkdirTemp(os.TempDir(), "test_pd")
cfg.InitialCluster = fmt.Sprintf("pd=%s", cfg.PeerUrls)
cfg.Log.Level = serverLogLevel
return &SimConfig{ServerConfig: cfg}
Expand Down
Loading

0 comments on commit cb6e6e2

Please sign in to comment.