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>
This commit is contained in:
singuliere 2022-02-25 10:20:50 +01:00 committed by GitHub
parent e4ef61ee0f
commit 49cab2b01f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 512 additions and 20 deletions

72
modules/log/buffer.go Normal file
View file

@ -0,0 +1,72 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package log
import (
"bytes"
"sync"
)
type bufferWriteCloser struct {
mu sync.Mutex
buffer bytes.Buffer
}
func (b *bufferWriteCloser) Write(p []byte) (int, error) {
b.mu.Lock()
defer b.mu.Unlock()
return b.buffer.Write(p)
}
func (b *bufferWriteCloser) Close() error {
return nil
}
func (b *bufferWriteCloser) String() string {
b.mu.Lock()
defer b.mu.Unlock()
return b.buffer.String()
}
// BufferLogger implements LoggerProvider and writes messages in a buffer.
type BufferLogger struct {
WriterLogger
}
// NewBufferLogger create BufferLogger returning as LoggerProvider.
func NewBufferLogger() LoggerProvider {
log := &BufferLogger{}
log.NewWriterLogger(&bufferWriteCloser{})
return log
}
// Init inits connection writer
func (log *BufferLogger) Init(string) error {
log.NewWriterLogger(log.out)
return nil
}
// Content returns the content accumulated in the content provider
func (log *BufferLogger) Content() (string, error) {
return log.out.(*bufferWriteCloser).String(), nil
}
// Flush when log should be flushed
func (log *BufferLogger) Flush() {
}
// ReleaseReopen does nothing
func (log *BufferLogger) ReleaseReopen() error {
return nil
}
// GetName returns the default name for this implementation
func (log *BufferLogger) GetName() string {
return "buffer"
}
func init() {
Register("buffer", NewBufferLogger)
}

View file

@ -0,0 +1,64 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package log
import (
"fmt"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestBufferLogger(t *testing.T) {
logger := NewBufferLogger()
bufferLogger := logger.(*BufferLogger)
assert.NotNil(t, bufferLogger)
err := logger.Init("")
assert.NoError(t, err)
location, _ := time.LoadLocation("EST")
date := time.Date(2019, time.January, 13, 22, 3, 30, 15, location)
msg := "TEST MSG"
event := Event{
level: INFO,
msg: msg,
caller: "CALLER",
filename: "FULL/FILENAME",
line: 1,
time: date,
}
logger.LogEvent(&event)
content, err := bufferLogger.Content()
assert.NoError(t, err)
assert.Contains(t, content, msg)
logger.Close()
}
func TestBufferLoggerContent(t *testing.T) {
level := INFO
logger := NewLogger(0, "console", "console", fmt.Sprintf(`{"level":"%s"}`, level.String()))
logger.SetLogger("buffer", "buffer", "{}")
defer logger.DelLogger("buffer")
msg := "A UNIQUE MESSAGE"
Error(msg)
found := false
for i := 0; i < 30000; i++ {
content, err := logger.GetLoggerProviderContent("buffer")
assert.NoError(t, err)
if strings.Contains(content, msg) {
found = true
break
}
time.Sleep(1 * time.Millisecond)
}
assert.True(t, found)
}

View file

@ -119,6 +119,11 @@ func (log *ConnLogger) Init(jsonconfig string) error {
return nil
}
// Content returns the content accumulated in the content provider
func (log *ConnLogger) Content() (string, error) {
return "", fmt.Errorf("not supported")
}
// Flush does nothing for this implementation
func (log *ConnLogger) Flush() {
}

View file

@ -66,6 +66,11 @@ func (log *ConsoleLogger) Init(config string) error {
return nil
}
// Content returns the content accumulated in the content provider
func (log *ConsoleLogger) Content() (string, error) {
return "", fmt.Errorf("not supported")
}
// Flush when log should be flushed
func (log *ConsoleLogger) Flush() {
}

View file

@ -216,6 +216,12 @@ func (m *MultiChannelledLog) GetEventLogger(name string) EventLogger {
return m.loggers[name]
}
// GetEventProvider returns a sub logger provider content from this MultiChannelledLog
func (m *MultiChannelledLog) GetLoggerProviderContent(name string) (string, error) {
channelledLogger := m.GetEventLogger(name).(*ChannelledLog)
return channelledLogger.loggerProvider.Content()
}
// GetEventLoggerNames returns a list of names
func (m *MultiChannelledLog) GetEventLoggerNames() []string {
m.rwmutex.RLock()

View file

@ -243,6 +243,15 @@ func (log *FileLogger) deleteOldLog() {
})
}
// Content returns the content accumulated in the content provider
func (log *FileLogger) Content() (string, error) {
b, err := os.ReadFile(log.Filename)
if err != nil {
return "", err
}
return string(b), nil
}
// Flush flush file logger.
// there are no buffering messages in file logger in memory.
// flush file means sync file from disk.

View file

@ -7,6 +7,7 @@ package log
// LoggerProvider represents behaviors of a logger provider.
type LoggerProvider interface {
Init(config string) error
Content() (string, error)
EventLogger
}

View file

@ -95,6 +95,11 @@ func (log *SMTPLogger) sendMail(p []byte) (int, error) {
)
}
// Content returns the content accumulated in the content provider
func (log *SMTPLogger) Content() (string, error) {
return "", fmt.Errorf("not supported")
}
// Flush when log should be flushed
func (log *SMTPLogger) Flush() {
}