@@ -15,7 +15,6 @@ import (
1515 "time"
1616
1717 "github.com/docker/docker/daemon/logger"
18- "github.com/docker/docker/pkg/pubsub"
1918 "github.com/docker/docker/pkg/tailfile"
2019 "gotest.tools/v3/assert"
2120 "gotest.tools/v3/poll"
@@ -247,60 +246,57 @@ func TestFollowLogsProducerGone(t *testing.T) {
247246}
248247
249248func TestCheckCapacityAndRotate (t * testing.T ) {
250- dir , err := os .MkdirTemp ("" , t .Name ())
251- assert .NilError (t , err )
252- defer os .RemoveAll (dir )
253-
254- f , err := os .CreateTemp (dir , "log" )
255- assert .NilError (t , err )
249+ dir := t .TempDir ()
256250
257- l := & LogFile {
258- f : f ,
259- capacity : 5 ,
260- maxFiles : 3 ,
261- compress : true ,
262- notifyReaders : pubsub .NewPublisher (0 , 1 ),
263- perms : 0600 ,
264- filesRefCounter : refCounter {counter : make (map [string ]int )},
265- getTailReader : func (ctx context.Context , r SizeReaderAt , lines int ) (io.Reader , int , error ) {
266- return tailfile .NewTailReader (ctx , r , lines )
267- },
268- createDecoder : func (io.Reader ) Decoder {
269- return dummyDecoder {}
270- },
271- marshal : func (msg * logger.Message ) ([]byte , error ) {
272- return msg .Line , nil
273- },
251+ logPath := filepath .Join (dir , "log" )
252+ getTailReader := func (ctx context.Context , r SizeReaderAt , lines int ) (io.Reader , int , error ) {
253+ return tailfile .NewTailReader (ctx , r , lines )
274254 }
255+ createDecoder := func (io.Reader ) Decoder {
256+ return dummyDecoder {}
257+ }
258+ marshal := func (msg * logger.Message ) ([]byte , error ) {
259+ return msg .Line , nil
260+ }
261+ l , err := NewLogFile (
262+ logPath ,
263+ 5 , // capacity
264+ 3 , // maxFiles
265+ true , // compress
266+ marshal ,
267+ createDecoder ,
268+ 0600 , // perms
269+ getTailReader ,
270+ )
271+ assert .NilError (t , err )
275272 defer l .Close ()
276273
277274 ls := dirStringer {dir }
278275
279276 assert .NilError (t , l .WriteLogEntry (& logger.Message {Line : []byte ("hello world!" )}))
280- _ , err = os .Stat (f . Name () + ".1" )
277+ _ , err = os .Stat (logPath + ".1" )
281278 assert .Assert (t , os .IsNotExist (err ), ls )
282279
283280 assert .NilError (t , l .WriteLogEntry (& logger.Message {Line : []byte ("hello world!" )}))
284- poll .WaitOn (t , checkFileExists (f . Name () + ".1.gz" ), poll .WithDelay (time .Millisecond ), poll .WithTimeout (30 * time .Second ))
281+ poll .WaitOn (t , checkFileExists (logPath + ".1.gz" ), poll .WithDelay (time .Millisecond ), poll .WithTimeout (30 * time .Second ))
285282
286283 assert .NilError (t , l .WriteLogEntry (& logger.Message {Line : []byte ("hello world!" )}))
287- poll .WaitOn (t , checkFileExists (f . Name () + ".1.gz" ), poll .WithDelay (time .Millisecond ), poll .WithTimeout (30 * time .Second ))
288- poll .WaitOn (t , checkFileExists (f . Name () + ".2.gz" ), poll .WithDelay (time .Millisecond ), poll .WithTimeout (30 * time .Second ))
284+ poll .WaitOn (t , checkFileExists (logPath + ".1.gz" ), poll .WithDelay (time .Millisecond ), poll .WithTimeout (30 * time .Second ))
285+ poll .WaitOn (t , checkFileExists (logPath + ".2.gz" ), poll .WithDelay (time .Millisecond ), poll .WithTimeout (30 * time .Second ))
289286
290287 t .Run ("closed log file" , func (t * testing.T ) {
291288 // Now let's simulate a failed rotation where the file was able to be closed but something else happened elsewhere
292289 // down the line.
293290 // We want to make sure that we can recover in the case that `l.f` was closed while attempting a rotation.
294291 l .f .Close ()
295292 assert .NilError (t , l .WriteLogEntry (& logger.Message {Line : []byte ("hello world!" )}))
296- assert .NilError (t , os .Remove (f . Name () + ".2.gz" ))
293+ assert .NilError (t , os .Remove (logPath + ".2.gz" ))
297294 })
298295
299296 t .Run ("with log reader" , func (t * testing.T ) {
300297 // Make sure rotate works with an active reader
301- lw := logger . NewLogWatcher ( )
298+ lw := l . ReadLogs (logger. ReadConfig { Follow : true , Tail : 1000 } )
302299 defer lw .ConsumerGone ()
303- go l .ReadLogs (logger.ReadConfig {Follow : true , Tail : 1000 }, lw )
304300
305301 assert .NilError (t , l .WriteLogEntry (& logger.Message {Line : []byte ("hello world 0!" )}), ls )
306302 // make sure the log reader is primed
@@ -310,7 +306,7 @@ func TestCheckCapacityAndRotate(t *testing.T) {
310306 assert .NilError (t , l .WriteLogEntry (& logger.Message {Line : []byte ("hello world 2!" )}), ls )
311307 assert .NilError (t , l .WriteLogEntry (& logger.Message {Line : []byte ("hello world 3!" )}), ls )
312308 assert .NilError (t , l .WriteLogEntry (& logger.Message {Line : []byte ("hello world 4!" )}), ls )
313- poll .WaitOn (t , checkFileExists (f . Name () + ".2.gz" ), poll .WithDelay (time .Millisecond ), poll .WithTimeout (30 * time .Second ))
309+ poll .WaitOn (t , checkFileExists (logPath + ".2.gz" ), poll .WithDelay (time .Millisecond ), poll .WithTimeout (30 * time .Second ))
314310 })
315311}
316312
0 commit comments