dumpling: precheck log file permisssion (#61101)
close pingcap/tidb#61100
This commit is contained in:
12
DEPS.bzl
12
DEPS.bzl
@ -6049,13 +6049,13 @@ def go_deps():
|
||||
name = "com_github_pingcap_log",
|
||||
build_file_proto_mode = "disable_global",
|
||||
importpath = "github.com/pingcap/log",
|
||||
sha256 = "8c5ac751f87626274ace3b615ff7b88552eede51c58eddb6c1f0a7ba5b191c6e",
|
||||
strip_prefix = "github.com/pingcap/log@v1.1.1-0.20250424032633-85a82d016f84",
|
||||
sha256 = "ca74400554017427f09ab72ffb5ba4b6e1f8b92235839f518fc102a3231aa267",
|
||||
strip_prefix = "github.com/pingcap/log@v1.1.1-0.20250514022801-14f3b4ca066e",
|
||||
urls = [
|
||||
"http://bazel-cache.pingcap.net:8080/gomod/github.com/pingcap/log/com_github_pingcap_log-v1.1.1-0.20250424032633-85a82d016f84.zip",
|
||||
"http://ats.apps.svc/gomod/github.com/pingcap/log/com_github_pingcap_log-v1.1.1-0.20250424032633-85a82d016f84.zip",
|
||||
"https://cache.hawkingrei.com/gomod/github.com/pingcap/log/com_github_pingcap_log-v1.1.1-0.20250424032633-85a82d016f84.zip",
|
||||
"https://storage.googleapis.com/pingcapmirror/gomod/github.com/pingcap/log/com_github_pingcap_log-v1.1.1-0.20250424032633-85a82d016f84.zip",
|
||||
"http://bazel-cache.pingcap.net:8080/gomod/github.com/pingcap/log/com_github_pingcap_log-v1.1.1-0.20250514022801-14f3b4ca066e.zip",
|
||||
"http://ats.apps.svc/gomod/github.com/pingcap/log/com_github_pingcap_log-v1.1.1-0.20250514022801-14f3b4ca066e.zip",
|
||||
"https://cache.hawkingrei.com/gomod/github.com/pingcap/log/com_github_pingcap_log-v1.1.1-0.20250514022801-14f3b4ca066e.zip",
|
||||
"https://storage.googleapis.com/pingcapmirror/gomod/github.com/pingcap/log/com_github_pingcap_log-v1.1.1-0.20250514022801-14f3b4ca066e.zip",
|
||||
],
|
||||
)
|
||||
go_repository(
|
||||
|
||||
@ -16,13 +16,19 @@ package log
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestInitLogNoPermission(t *testing.T) {
|
||||
if os.Geteuid() == 0 {
|
||||
t.Skip("Skip test when running as root user")
|
||||
}
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
conf := &Config{
|
||||
Level: "debug",
|
||||
File: tmpDir + "/test.log",
|
||||
@ -33,17 +39,44 @@ func TestInitLogNoPermission(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, logger)
|
||||
|
||||
err = os.Chmod(tmpDir, 0)
|
||||
require.NoError(t, err)
|
||||
// Directory permission denied
|
||||
require.NoError(t, os.Chmod(tmpDir, 0))
|
||||
_, _, err = InitAppLogger(conf)
|
||||
require.Contains(t, err.Error(), "permission denied")
|
||||
require.NoError(t, os.Chmod(tmpDir, 0755))
|
||||
|
||||
l, _, err := InitAppLogger(conf)
|
||||
if err == nil && os.Geteuid() == 0 {
|
||||
// current user is root, so we can write to the file
|
||||
l.Info("test")
|
||||
err = l.Sync()
|
||||
require.NoError(t, err)
|
||||
require.FileExists(t, tmpDir+"/test.log")
|
||||
return
|
||||
}
|
||||
// Directory exists but doesn't allow file creation
|
||||
readOnlyDirPath := filepath.Join(tmpDir, "readonly-dir")
|
||||
require.NoError(t, os.Mkdir(readOnlyDirPath, 0755))
|
||||
require.NoError(t, os.Chmod(readOnlyDirPath, 0555)) // Read-only directory
|
||||
conf.File = readOnlyDirPath + "/test.log"
|
||||
_, _, err = InitAppLogger(conf)
|
||||
require.ErrorContains(t, err, "permission denied")
|
||||
require.NoError(t, os.Chmod(readOnlyDirPath, 0755))
|
||||
|
||||
// Using a directory as log file
|
||||
dirLogPath := filepath.Join(tmpDir, "dir-as-log")
|
||||
require.NoError(t, os.Mkdir(dirLogPath, 0755))
|
||||
conf.File = dirLogPath
|
||||
_, _, err = InitAppLogger(conf)
|
||||
require.ErrorContains(t, err, "can't use directory as log file name")
|
||||
|
||||
// File exists but is not writable
|
||||
filePath := filepath.Join(tmpDir, "readonly.log")
|
||||
file, err := os.Create(filePath)
|
||||
require.NoError(t, err)
|
||||
file.Close()
|
||||
require.NoError(t, os.Chmod(filePath, 0444))
|
||||
conf.File = filePath
|
||||
_, _, err = InitAppLogger(conf)
|
||||
require.ErrorContains(t, err, "permission denied")
|
||||
require.NoError(t, os.Chmod(filePath, 0644))
|
||||
|
||||
// Ensure parent directory is created successfully
|
||||
nestedPath := filepath.Join(tmpDir, "nested/path/to")
|
||||
conf.File = nestedPath + "/test.log"
|
||||
_, _, err = InitAppLogger(conf)
|
||||
require.NoError(t, err)
|
||||
_, err = os.Stat(nestedPath)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
2
go.mod
2
go.mod
@ -90,7 +90,7 @@ require (
|
||||
github.com/pingcap/failpoint v0.0.0-20240528011301-b51a646c7c86
|
||||
github.com/pingcap/fn v1.0.0
|
||||
github.com/pingcap/kvproto v0.0.0-20250224053625-b6a98c6bf02d
|
||||
github.com/pingcap/log v1.1.1-0.20250424032633-85a82d016f84
|
||||
github.com/pingcap/log v1.1.1-0.20250514022801-14f3b4ca066e
|
||||
github.com/pingcap/sysutil v1.0.1-0.20240311050922-ae81ee01f3a5
|
||||
github.com/pingcap/tidb/pkg/parser v0.0.0-20211011031125-9b13dc409c5e
|
||||
github.com/pingcap/tipb v0.0.0-20250401143359-775c2379cbc7
|
||||
|
||||
4
go.sum
4
go.sum
@ -687,8 +687,8 @@ github.com/pingcap/kvproto v0.0.0-20250224053625-b6a98c6bf02d h1:52qhTQG8G8V/pHo
|
||||
github.com/pingcap/kvproto v0.0.0-20250224053625-b6a98c6bf02d/go.mod h1:rXxWk2UnwfUhLXha1jxRWPADw9eMZGWEWCg92Tgmb/8=
|
||||
github.com/pingcap/log v0.0.0-20210625125904-98ed8e2eb1c7/go.mod h1:8AanEdAHATuRurdGxZXBz0At+9avep+ub7U1AGYLIMM=
|
||||
github.com/pingcap/log v1.1.0/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4=
|
||||
github.com/pingcap/log v1.1.1-0.20250424032633-85a82d016f84 h1:ljnSbUq7LOkUtLtDXjTS5GanSWSAQ8pgVn7ucXMeMK8=
|
||||
github.com/pingcap/log v1.1.1-0.20250424032633-85a82d016f84/go.mod h1:ORfBOFp1eteu2odzsyaxI+b8TzJwgjwyQcGhI+9SfEA=
|
||||
github.com/pingcap/log v1.1.1-0.20250514022801-14f3b4ca066e h1:8AZZRv1Ox9FVGATVZBBgr6y1MrNBQFABEdovNJt1QIc=
|
||||
github.com/pingcap/log v1.1.1-0.20250514022801-14f3b4ca066e/go.mod h1:ORfBOFp1eteu2odzsyaxI+b8TzJwgjwyQcGhI+9SfEA=
|
||||
github.com/pingcap/sysutil v1.0.1-0.20240311050922-ae81ee01f3a5 h1:T4pXRhBflzDeAhmOQHNPRRogMYxP13V7BkYw3ZsoSfE=
|
||||
github.com/pingcap/sysutil v1.0.1-0.20240311050922-ae81ee01f3a5/go.mod h1:rlimy0GcTvjiJqvD5mXTRr8O2eNZPBrcUgiWVYp9530=
|
||||
github.com/pingcap/tipb v0.0.0-20250401143359-775c2379cbc7 h1:iRRoPMrpj4jkIzhMT+yabc98bhGeUzAFSjh5HUOn5Yg=
|
||||
|
||||
Reference in New Issue
Block a user