forked from forgejo/forgejo
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:
parent
e4ef61ee0f
commit
49cab2b01f
12 changed files with 512 additions and 20 deletions
64
modules/log/buffer_test.go
Normal file
64
modules/log/buffer_test.go
Normal 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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue