first-commit
This commit is contained in:
105
modules/test/logchecker.go
Normal file
105
modules/test/logchecker.go
Normal file
@@ -0,0 +1,105 @@
|
||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
)
|
||||
|
||||
type LogChecker struct {
|
||||
*log.EventWriterBaseImpl
|
||||
|
||||
filterMessages []string
|
||||
filtered []bool
|
||||
|
||||
stopMark string
|
||||
stopped bool
|
||||
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func (lc *LogChecker) Run(ctx context.Context) {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case event, ok := <-lc.Queue:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
lc.checkLogEvent(event)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (lc *LogChecker) checkLogEvent(event *log.EventFormatted) {
|
||||
lc.mu.Lock()
|
||||
defer lc.mu.Unlock()
|
||||
for i, msg := range lc.filterMessages {
|
||||
if strings.Contains(event.Origin.MsgSimpleText, msg) {
|
||||
lc.filtered[i] = true
|
||||
}
|
||||
}
|
||||
if strings.Contains(event.Origin.MsgSimpleText, lc.stopMark) {
|
||||
lc.stopped = true
|
||||
}
|
||||
}
|
||||
|
||||
var checkerIndex int64
|
||||
|
||||
func NewLogChecker(namePrefix string) (logChecker *LogChecker, cancel func()) {
|
||||
logger := log.GetManager().GetLogger(namePrefix)
|
||||
newCheckerIndex := atomic.AddInt64(&checkerIndex, 1)
|
||||
writerName := namePrefix + "-" + strconv.FormatInt(newCheckerIndex, 10)
|
||||
|
||||
lc := &LogChecker{}
|
||||
lc.EventWriterBaseImpl = log.NewEventWriterBase(writerName, "test-log-checker", log.WriterMode{})
|
||||
logger.AddWriters(lc)
|
||||
return lc, func() { _ = logger.RemoveWriter(writerName) }
|
||||
}
|
||||
|
||||
// Filter will make the `Check` function to check if these logs are outputted.
|
||||
func (lc *LogChecker) Filter(msgs ...string) *LogChecker {
|
||||
lc.mu.Lock()
|
||||
defer lc.mu.Unlock()
|
||||
lc.filterMessages = make([]string, len(msgs))
|
||||
copy(lc.filterMessages, msgs)
|
||||
lc.filtered = make([]bool, len(lc.filterMessages))
|
||||
return lc
|
||||
}
|
||||
|
||||
func (lc *LogChecker) StopMark(msg string) *LogChecker {
|
||||
lc.mu.Lock()
|
||||
defer lc.mu.Unlock()
|
||||
lc.stopMark = msg
|
||||
lc.stopped = false
|
||||
return lc
|
||||
}
|
||||
|
||||
// Check returns the filtered slice and whether the stop mark is reached.
|
||||
func (lc *LogChecker) Check(d time.Duration) (filtered []bool, stopped bool) {
|
||||
stop := time.Now().Add(d)
|
||||
|
||||
for {
|
||||
lc.mu.Lock()
|
||||
stopped = lc.stopped
|
||||
lc.mu.Unlock()
|
||||
|
||||
if time.Now().After(stop) || stopped {
|
||||
lc.mu.Lock()
|
||||
f := make([]bool, len(lc.filtered))
|
||||
copy(f, lc.filtered)
|
||||
lc.mu.Unlock()
|
||||
return f, stopped
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
}
|
45
modules/test/logchecker_test.go
Normal file
45
modules/test/logchecker_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestLogChecker(t *testing.T) {
|
||||
lc, cleanup := NewLogChecker(log.DEFAULT)
|
||||
defer cleanup()
|
||||
|
||||
lc.Filter("First", "Third").StopMark("End")
|
||||
log.Info("test")
|
||||
|
||||
filtered, stopped := lc.Check(100 * time.Millisecond)
|
||||
assert.ElementsMatch(t, []bool{false, false}, filtered)
|
||||
assert.False(t, stopped)
|
||||
|
||||
log.Info("First")
|
||||
filtered, stopped = lc.Check(100 * time.Millisecond)
|
||||
assert.ElementsMatch(t, []bool{true, false}, filtered)
|
||||
assert.False(t, stopped)
|
||||
|
||||
log.Info("Second")
|
||||
filtered, stopped = lc.Check(100 * time.Millisecond)
|
||||
assert.ElementsMatch(t, []bool{true, false}, filtered)
|
||||
assert.False(t, stopped)
|
||||
|
||||
log.Info("Third")
|
||||
filtered, stopped = lc.Check(100 * time.Millisecond)
|
||||
assert.ElementsMatch(t, []bool{true, true}, filtered)
|
||||
assert.False(t, stopped)
|
||||
|
||||
log.Info("End")
|
||||
filtered, stopped = lc.Check(100 * time.Millisecond)
|
||||
assert.ElementsMatch(t, []bool{true, true}, filtered)
|
||||
assert.True(t, stopped)
|
||||
}
|
73
modules/test/utils.go
Normal file
73
modules/test/utils.go
Normal file
@@ -0,0 +1,73 @@
|
||||
// Copyright 2017 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
)
|
||||
|
||||
// RedirectURL returns the redirect URL of a http response.
|
||||
// It also works for JSONRedirect: `{"redirect": "..."}`
|
||||
// FIXME: it should separate the logic of checking from header and JSON body
|
||||
func RedirectURL(resp http.ResponseWriter) string {
|
||||
loc := resp.Header().Get("Location")
|
||||
if loc != "" {
|
||||
return loc
|
||||
}
|
||||
if r, ok := resp.(*httptest.ResponseRecorder); ok {
|
||||
m := map[string]any{}
|
||||
err := json.Unmarshal(r.Body.Bytes(), &m)
|
||||
if err == nil {
|
||||
if loc, ok := m["redirect"].(string); ok {
|
||||
return loc
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func ParseJSONError(buf []byte) (ret struct {
|
||||
ErrorMessage string `json:"errorMessage"`
|
||||
RenderFormat string `json:"renderFormat"`
|
||||
},
|
||||
) {
|
||||
_ = json.Unmarshal(buf, &ret)
|
||||
return ret
|
||||
}
|
||||
|
||||
func IsNormalPageCompleted(s string) bool {
|
||||
return strings.Contains(s, `<footer class="page-footer"`) && strings.Contains(s, `</html>`)
|
||||
}
|
||||
|
||||
func MockVariableValue[T any](p *T, v ...T) (reset func()) {
|
||||
old := *p
|
||||
if len(v) > 0 {
|
||||
*p = v[0]
|
||||
}
|
||||
return func() { *p = old }
|
||||
}
|
||||
|
||||
// SetupGiteaRoot Sets GITEA_ROOT if it is not already set and returns the value
|
||||
func SetupGiteaRoot() string {
|
||||
giteaRoot := os.Getenv("GITEA_ROOT")
|
||||
if giteaRoot != "" {
|
||||
return giteaRoot
|
||||
}
|
||||
_, filename, _, _ := runtime.Caller(0)
|
||||
giteaRoot = filepath.Dir(filepath.Dir(filepath.Dir(filename)))
|
||||
fixturesDir := filepath.Join(giteaRoot, "models", "fixtures")
|
||||
if exist, _ := util.IsDir(fixturesDir); !exist {
|
||||
panic("fixtures directory not found: " + fixturesDir)
|
||||
}
|
||||
_ = os.Setenv("GITEA_ROOT", giteaRoot)
|
||||
return giteaRoot
|
||||
}
|
17
modules/test/utils_test.go
Normal file
17
modules/test/utils_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
// Copyright 2024 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSetupGiteaRoot(t *testing.T) {
|
||||
t.Setenv("GITEA_ROOT", "test")
|
||||
assert.Equal(t, "test", SetupGiteaRoot())
|
||||
t.Setenv("GITEA_ROOT", "")
|
||||
assert.NotEqual(t, "test", SetupGiteaRoot())
|
||||
}
|
Reference in New Issue
Block a user