github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/daemon/logger/journald/read_test.go (about) 1 //go:build linux && cgo && !static_build && journald 2 3 package journald // import "github.com/Prakhar-Agarwal-byte/moby/daemon/logger/journald" 4 5 import ( 6 "testing" 7 "time" 8 9 "github.com/coreos/go-systemd/v22/journal" 10 "gotest.tools/v3/assert" 11 12 "github.com/Prakhar-Agarwal-byte/moby/daemon/logger" 13 "github.com/Prakhar-Agarwal-byte/moby/daemon/logger/journald/internal/fake" 14 "github.com/Prakhar-Agarwal-byte/moby/daemon/logger/loggertest" 15 ) 16 17 func TestLogRead(t *testing.T) { 18 r := loggertest.Reader{ 19 Factory: func(t *testing.T, info logger.Info) func(*testing.T) logger.Logger { 20 journalDir := t.TempDir() 21 22 // Fill the journal with irrelevant events which the 23 // LogReader needs to filter out. 24 rotatedJournal := fake.NewT(t, journalDir+"/rotated.journal") 25 rotatedJournal.AssignEventTimestampFromSyslogTimestamp = true 26 l, err := new(logger.Info{ 27 ContainerID: "wrongone0001", 28 ContainerName: "fake", 29 }) 30 assert.NilError(t, err) 31 l.sendToJournal = rotatedJournal.Send 32 assert.NilError(t, l.Log(&logger.Message{Source: "stdout", Timestamp: time.Now().Add(-1 * 30 * time.Minute), Line: []byte("stdout of a different container in a rotated journal file")})) 33 assert.NilError(t, l.Log(&logger.Message{Source: "stderr", Timestamp: time.Now().Add(-1 * 30 * time.Minute), Line: []byte("stderr of a different container in a rotated journal file")})) 34 assert.NilError(t, rotatedJournal.Send("a log message from a totally different process in a rotated journal", journal.PriInfo, nil)) 35 36 activeJournal := fake.NewT(t, journalDir+"/fake.journal") 37 activeJournal.AssignEventTimestampFromSyslogTimestamp = true 38 l, err = new(logger.Info{ 39 ContainerID: "wrongone0002", 40 ContainerName: "fake", 41 }) 42 assert.NilError(t, err) 43 l.sendToJournal = activeJournal.Send 44 assert.NilError(t, l.Log(&logger.Message{Source: "stdout", Timestamp: time.Now().Add(-1 * 30 * time.Minute), Line: []byte("stdout of a different container in the active journal file")})) 45 assert.NilError(t, l.Log(&logger.Message{Source: "stderr", Timestamp: time.Now().Add(-1 * 30 * time.Minute), Line: []byte("stderr of a different container in the active journal file")})) 46 assert.NilError(t, rotatedJournal.Send("a log message from a totally different process in the active journal", journal.PriInfo, nil)) 47 48 return func(t *testing.T) logger.Logger { 49 s := make(chan sendit, 100) 50 t.Cleanup(func() { close(s) }) 51 go func() { 52 for m := range s { 53 <-m.after 54 activeJournal.Send(m.message, m.priority, m.vars) 55 if m.sent != nil { 56 close(m.sent) 57 } 58 } 59 }() 60 l, err := new(info) 61 assert.NilError(t, err) 62 l.journalReadDir = journalDir 63 64 sl := &syncLogger{journald: l} 65 l.sendToJournal = func(message string, priority journal.Priority, vars map[string]string) error { 66 sent := make(chan struct{}) 67 s <- sendit{ 68 message: message, 69 priority: priority, 70 vars: vars, 71 after: time.After(150 * time.Millisecond), 72 sent: sent, 73 } 74 sl.waitOn = sent 75 return nil 76 } 77 l.readSyncTimeout = 3 * time.Second 78 return sl 79 } 80 }, 81 } 82 t.Run("Tail", r.TestTail) 83 t.Run("Follow", r.TestFollow) 84 } 85 86 type sendit struct { 87 message string 88 priority journal.Priority 89 vars map[string]string 90 after <-chan time.Time 91 sent chan<- struct{} 92 } 93 94 type syncLogger struct { 95 *journald 96 waitOn <-chan struct{} 97 } 98 99 func (l *syncLogger) Sync() error { 100 if l.waitOn != nil { 101 <-l.waitOn 102 } 103 return nil 104 }