@@ -19,6 +19,7 @@ import (
1919 "math"
2020 "net"
2121 "os"
22+ "path/filepath"
2223 "strings"
2324 "testing"
2425 "time"
@@ -99,16 +100,16 @@ func TestLog(t *testing.T) {
99100 zap .Duration ("duration" , 10 * time .Second ),
100101 )
101102 ts .assertMessages (
102- `[INFO] [zap_log_test.go:49 ] ["failed to fetch URL"] [url=http://example.com] [attempt=3] [backoff=1s]` ,
103- `[INFO] [zap_log_test.go:54 ] ["failed to \"fetch\" [URL]: http://example.com"]` ,
104- `[DEBUG] [zap_log_test.go:55 ] ["Slow query"] [sql="SELECT * FROM TABLE\n\tWHERE ID=\"abc\""] [duration=1.3s] ["process keys"=1500]` ,
105- `[INFO] [zap_log_test.go:61 ] [Welcome]` ,
106- `[INFO] [zap_log_test.go:62 ] ["Welcome TiDB"]` ,
107- `[INFO] [zap_log_test.go:63 ] [欢迎]` ,
108- `[INFO] [zap_log_test.go:64 ] ["欢迎来到 TiDB"]` ,
109- `[WARN] [zap_log_test.go:65 ] [Type] [Counter=NaN] [Score=+Inf]` ,
110- `[INFO] [zap_log_test.go:70 ] ["new connection"] [connID=1] [traceID=dse1121]` ,
111- `[INFO] [zap_log_test.go:71 ] ["Testing typs"] [filed1=noquote] ` +
103+ `[INFO] [zap_log_test.go:50 ] ["failed to fetch URL"] [url=http://example.com] [attempt=3] [backoff=1s]` ,
104+ `[INFO] [zap_log_test.go:55 ] ["failed to \"fetch\" [URL]: http://example.com"]` ,
105+ `[DEBUG] [zap_log_test.go:56 ] ["Slow query"] [sql="SELECT * FROM TABLE\n\tWHERE ID=\"abc\""] [duration=1.3s] ["process keys"=1500]` ,
106+ `[INFO] [zap_log_test.go:62 ] [Welcome]` ,
107+ `[INFO] [zap_log_test.go:63 ] ["Welcome TiDB"]` ,
108+ `[INFO] [zap_log_test.go:64 ] [欢迎]` ,
109+ `[INFO] [zap_log_test.go:65 ] ["欢迎来到 TiDB"]` ,
110+ `[WARN] [zap_log_test.go:66 ] [Type] [Counter=NaN] [Score=+Inf]` ,
111+ `[INFO] [zap_log_test.go:71 ] ["new connection"] [connID=1] [traceID=dse1121]` ,
112+ `[INFO] [zap_log_test.go:72 ] ["Testing typs"] [filed1=noquote] ` +
112113 `[filed2="in quote"] [urls="[http://mock1.com:2347,http://mock2.com:2432]"] ` +
113114 `[urls-peer="[t1,\"t2 fine\"]"] ["store ids"="[1,4,5]"] [object="{username=user1}"] ` +
114115 `[object2="{username=\"user 2\"}"] [binary="YWIxMjM="] ["is processed"=true] ` +
@@ -231,8 +232,8 @@ func TestLogJSON(t *testing.T) {
231232 "backoff" , time .Second ,
232233 )
233234 logger .With (zap .String ("connID" , "1" ), zap .String ("traceID" , "dse1121" )).Info ("new connection" )
234- ts .assertMessages ("{\" level\" :\" INFO\" ,\" caller\" :\" zap_log_test.go:228 \" ,\" message\" :\" failed to fetch URL\" ,\" url\" :\" http://example.com\" ,\" attempt\" :3,\" backoff\" :\" 1s\" }" ,
235- "{\" level\" :\" INFO\" ,\" caller\" :\" zap_log_test.go:233 \" ,\" message\" :\" new connection\" ,\" connID\" :\" 1\" ,\" traceID\" :\" dse1121\" }" )
235+ ts .assertMessages ("{\" level\" :\" INFO\" ,\" caller\" :\" zap_log_test.go:229 \" ,\" message\" :\" failed to fetch URL\" ,\" url\" :\" http://example.com\" ,\" attempt\" :3,\" backoff\" :\" 1s\" }" ,
236+ "{\" level\" :\" INFO\" ,\" caller\" :\" zap_log_test.go:234 \" ,\" message\" :\" new connection\" ,\" connID\" :\" 1\" ,\" traceID\" :\" dse1121\" }" )
236237}
237238
238239func TestRotateLogWithCompress (t * testing.T ) {
@@ -288,23 +289,73 @@ func TestCompressError(t *testing.T) {
288289}
289290
290291func TestLogFileNoPermission (t * testing.T ) {
291- tempDir , _ := os .MkdirTemp ("/tmp" , "tests-log" )
292- defer os .RemoveAll (tempDir )
292+ tempDir := t .TempDir ()
293+
294+ // Directory permission denied
295+ dirPath := filepath .Join (tempDir , "noperm-dir" )
296+ require .NoError (t , os .Mkdir (dirPath , 0755 ))
293297 conf := & Config {
294298 Level : "info" ,
295299 File : FileLogConfig {
296- Filename : tempDir + "/ test.log" ,
300+ Filename : filepath . Join ( dirPath , " test.log") ,
297301 MaxSize : 1 ,
298302 },
299303 }
300304 _ , _ , err := InitLogger (conf )
301305 require .NoError (t , err )
306+ require .NoError (t , os .Chmod (dirPath , 0 ))
307+ _ , _ , err = InitLogger (conf )
308+ require .Contains (t , err .Error (), "permission denied" )
309+ require .NoError (t , os .Chmod (dirPath , 0755 ))
310+
311+ // Directory exists but doesn't allow file creation
312+ readOnlyDirPath := filepath .Join (tempDir , "readonly-dir" )
313+ require .NoError (t , os .Mkdir (readOnlyDirPath , 0755 ))
314+ require .NoError (t , os .Chmod (readOnlyDirPath , 0555 )) // Read-only directory
315+ conf = & Config {
316+ Level : "info" ,
317+ File : FileLogConfig {
318+ Filename : filepath .Join (readOnlyDirPath , "test.log" ),
319+ MaxSize : 1 ,
320+ },
321+ }
322+ _ , _ , err = InitLogger (conf )
323+ require .Contains (t , err .Error (), "permission denied" )
324+ require .NoError (t , os .Chmod (readOnlyDirPath , 0755 ))
325+
326+ // Using a directory as log file
327+ dirLogPath := filepath .Join (tempDir , "dir-as-log" )
328+ require .NoError (t , os .Mkdir (dirLogPath , 0755 ))
329+ conf = & Config {
330+ Level : "info" ,
331+ File : FileLogConfig {
332+ Filename : dirLogPath ,
333+ MaxSize : 1 ,
334+ },
335+ }
336+ _ , _ , err = InitLogger (conf )
337+ require .Contains (t , err .Error (), "can't use directory as log file name" )
302338
303- err = os .Chmod (tempDir , 0 )
339+ // File exists but is not writable
340+ filePath := filepath .Join (tempDir , "readonly.log" )
341+ file , err := os .Create (filePath )
304342 require .NoError (t , err )
343+ file .Close ()
344+ require .NoError (t , os .Chmod (filePath , 0444 ))
305345
346+ // Ensure parent directory is created successfully
347+ nestedPath := filepath .Join (tempDir , "nested/path/to" )
348+ conf = & Config {
349+ Level : "info" ,
350+ File : FileLogConfig {
351+ Filename : filepath .Join (nestedPath , "test.log" ),
352+ MaxSize : 1 ,
353+ },
354+ }
306355 _ , _ , err = InitLogger (conf )
307- require .Contains (t , err .Error (), "permission denied" )
356+ require .NoError (t , err )
357+ _ , err = os .Stat (nestedPath )
358+ require .NoError (t , err )
308359}
309360
310361// testLogSpy is a testing.TB that captures logged messages.
0 commit comments