Skip to content

Commit 49cab2b

Browse files
singuliereLoïc Dacharyzeripathlunny
authored
migrations: add test for importing pull requests in gitea uploader (#18752)
* logs: add the buffer logger to inspect logs during testing Signed-off-by: Loïc Dachary <loic@dachary.org> * migrations: add test for importing pull requests in gitea uploader Signed-off-by: Loïc Dachary <loic@dachary.org> * for each git.OpenRepositoryCtx, call Close * Content is expected to return the content of the log * test for errors before defer Co-authored-by: Loïc Dachary <loic@dachary.org> Co-authored-by: zeripath <art27@cantab.net> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
1 parent e4ef61e commit 49cab2b

File tree

12 files changed

+512
-20
lines changed

12 files changed

+512
-20
lines changed

integrations/testlogger.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ func (log *TestLogger) Init(config string) error {
181181
return nil
182182
}
183183

184+
// Content returns the content accumulated in the content provider
185+
func (log *TestLogger) Content() (string, error) {
186+
return "", fmt.Errorf("not supported")
187+
}
188+
184189
// Flush when log should be flushed
185190
func (log *TestLogger) Flush() {
186191
}

models/migrations/testlogger_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ func (log *TestLogger) Init(config string) error {
166166
return nil
167167
}
168168

169+
// Content returns the content accumulated in the content provider
170+
func (log *TestLogger) Content() (string, error) {
171+
return "", fmt.Errorf("not supported")
172+
}
173+
169174
// Flush when log should be flushed
170175
func (log *TestLogger) Flush() {
171176
}

modules/log/buffer.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package log
6+
7+
import (
8+
"bytes"
9+
"sync"
10+
)
11+
12+
type bufferWriteCloser struct {
13+
mu sync.Mutex
14+
buffer bytes.Buffer
15+
}
16+
17+
func (b *bufferWriteCloser) Write(p []byte) (int, error) {
18+
b.mu.Lock()
19+
defer b.mu.Unlock()
20+
return b.buffer.Write(p)
21+
}
22+
23+
func (b *bufferWriteCloser) Close() error {
24+
return nil
25+
}
26+
27+
func (b *bufferWriteCloser) String() string {
28+
b.mu.Lock()
29+
defer b.mu.Unlock()
30+
return b.buffer.String()
31+
}
32+
33+
// BufferLogger implements LoggerProvider and writes messages in a buffer.
34+
type BufferLogger struct {
35+
WriterLogger
36+
}
37+
38+
// NewBufferLogger create BufferLogger returning as LoggerProvider.
39+
func NewBufferLogger() LoggerProvider {
40+
log := &BufferLogger{}
41+
log.NewWriterLogger(&bufferWriteCloser{})
42+
return log
43+
}
44+
45+
// Init inits connection writer
46+
func (log *BufferLogger) Init(string) error {
47+
log.NewWriterLogger(log.out)
48+
return nil
49+
}
50+
51+
// Content returns the content accumulated in the content provider
52+
func (log *BufferLogger) Content() (string, error) {
53+
return log.out.(*bufferWriteCloser).String(), nil
54+
}
55+
56+
// Flush when log should be flushed
57+
func (log *BufferLogger) Flush() {
58+
}
59+
60+
// ReleaseReopen does nothing
61+
func (log *BufferLogger) ReleaseReopen() error {
62+
return nil
63+
}
64+
65+
// GetName returns the default name for this implementation
66+
func (log *BufferLogger) GetName() string {
67+
return "buffer"
68+
}
69+
70+
func init() {
71+
Register("buffer", NewBufferLogger)
72+
}

modules/log/buffer_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package log
6+
7+
import (
8+
"fmt"
9+
"strings"
10+
"testing"
11+
"time"
12+
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestBufferLogger(t *testing.T) {
17+
logger := NewBufferLogger()
18+
bufferLogger := logger.(*BufferLogger)
19+
assert.NotNil(t, bufferLogger)
20+
21+
err := logger.Init("")
22+
assert.NoError(t, err)
23+
24+
location, _ := time.LoadLocation("EST")
25+
date := time.Date(2019, time.January, 13, 22, 3, 30, 15, location)
26+
27+
msg := "TEST MSG"
28+
event := Event{
29+
level: INFO,
30+
msg: msg,
31+
caller: "CALLER",
32+
filename: "FULL/FILENAME",
33+
line: 1,
34+
time: date,
35+
}
36+
logger.LogEvent(&event)
37+
content, err := bufferLogger.Content()
38+
assert.NoError(t, err)
39+
assert.Contains(t, content, msg)
40+
logger.Close()
41+
}
42+
43+
func TestBufferLoggerContent(t *testing.T) {
44+
level := INFO
45+
logger := NewLogger(0, "console", "console", fmt.Sprintf(`{"level":"%s"}`, level.String()))
46+
47+
logger.SetLogger("buffer", "buffer", "{}")
48+
defer logger.DelLogger("buffer")
49+
50+
msg := "A UNIQUE MESSAGE"
51+
Error(msg)
52+
53+
found := false
54+
for i := 0; i < 30000; i++ {
55+
content, err := logger.GetLoggerProviderContent("buffer")
56+
assert.NoError(t, err)
57+
if strings.Contains(content, msg) {
58+
found = true
59+
break
60+
}
61+
time.Sleep(1 * time.Millisecond)
62+
}
63+
assert.True(t, found)
64+
}

modules/log/conn.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ func (log *ConnLogger) Init(jsonconfig string) error {
119119
return nil
120120
}
121121

122+
// Content returns the content accumulated in the content provider
123+
func (log *ConnLogger) Content() (string, error) {
124+
return "", fmt.Errorf("not supported")
125+
}
126+
122127
// Flush does nothing for this implementation
123128
func (log *ConnLogger) Flush() {
124129
}

modules/log/console.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ func (log *ConsoleLogger) Init(config string) error {
6666
return nil
6767
}
6868

69+
// Content returns the content accumulated in the content provider
70+
func (log *ConsoleLogger) Content() (string, error) {
71+
return "", fmt.Errorf("not supported")
72+
}
73+
6974
// Flush when log should be flushed
7075
func (log *ConsoleLogger) Flush() {
7176
}

modules/log/event.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ func (m *MultiChannelledLog) GetEventLogger(name string) EventLogger {
216216
return m.loggers[name]
217217
}
218218

219+
// GetEventProvider returns a sub logger provider content from this MultiChannelledLog
220+
func (m *MultiChannelledLog) GetLoggerProviderContent(name string) (string, error) {
221+
channelledLogger := m.GetEventLogger(name).(*ChannelledLog)
222+
return channelledLogger.loggerProvider.Content()
223+
}
224+
219225
// GetEventLoggerNames returns a list of names
220226
func (m *MultiChannelledLog) GetEventLoggerNames() []string {
221227
m.rwmutex.RLock()

modules/log/file.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,15 @@ func (log *FileLogger) deleteOldLog() {
243243
})
244244
}
245245

246+
// Content returns the content accumulated in the content provider
247+
func (log *FileLogger) Content() (string, error) {
248+
b, err := os.ReadFile(log.Filename)
249+
if err != nil {
250+
return "", err
251+
}
252+
return string(b), nil
253+
}
254+
246255
// Flush flush file logger.
247256
// there are no buffering messages in file logger in memory.
248257
// flush file means sync file from disk.

modules/log/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package log
77
// LoggerProvider represents behaviors of a logger provider.
88
type LoggerProvider interface {
99
Init(config string) error
10+
Content() (string, error)
1011
EventLogger
1112
}
1213

modules/log/smtp.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ func (log *SMTPLogger) sendMail(p []byte) (int, error) {
9595
)
9696
}
9797

98+
// Content returns the content accumulated in the content provider
99+
func (log *SMTPLogger) Content() (string, error) {
100+
return "", fmt.Errorf("not supported")
101+
}
102+
98103
// Flush when log should be flushed
99104
func (log *SMTPLogger) Flush() {
100105
}

0 commit comments

Comments
 (0)