GODT-1913: pass reporter to gluon, limit restarts, add crash handlers.

This commit is contained in:
Jakub
2022-10-21 18:41:31 +02:00
committed by James Houlahan
parent 31fb878bbd
commit ae87d7b236
28 changed files with 811 additions and 34 deletions

View File

@ -125,6 +125,7 @@ func TestFeatures(testingT *testing.T) {
ctx.Step(`^bridge sends an update installed event for version "([^"]*)"$`, s.bridgeSendsAnUpdateInstalledEventForVersion)
ctx.Step(`^bridge sends an update not available event$`, s.bridgeSendsAnUpdateNotAvailableEvent)
ctx.Step(`^bridge sends a forced update event$`, s.bridgeSendsAForcedUpdateEvent)
ctx.Step(`^bridge reports a message with "([^"]*)"$`, s.bridgeReportsMessage)
// ==== FRONTEND ====
ctx.Step(`^frontend sees that bridge is version "([^"]*)"$`, s.frontendSeesThatBridgeIsVersion)

View File

@ -27,6 +27,7 @@ import (
"github.com/Masterminds/semver/v3"
"github.com/ProtonMail/proton-bridge/v2/internal/events"
"github.com/ProtonMail/proton-bridge/v2/internal/vault"
"github.com/golang/mock/gomock"
)
func (s *scenario) bridgeStarts() error {
@ -263,6 +264,16 @@ func (s *scenario) bridgeSendsAForcedUpdateEvent() error {
return nil
}
func (s *scenario) bridgeReportsMessage(message string) error {
s.t.reporter.removeMatchingRecords(
gomock.Eq(false),
gomock.Eq(message),
gomock.Any(),
1,
)
return nil
}
func (s *scenario) theUserHidesAllMail() error {
return s.t.bridge.SetShowAllMail(false)
}

View File

@ -159,6 +159,8 @@ func (t *testCtx) initBridge() (<-chan events.Event, error) {
t.mocks.TLSReporter,
liteapi.NewDialer(t.netCtl, &tls.Config{InsecureSkipVerify: true}).GetRoundTripper(),
t.mocks.ProxyCtl,
t.mocks.CrashHandler,
t.reporter,
// Logging stuff
logIMAP,

View File

@ -21,9 +21,16 @@ import (
"context"
"fmt"
"runtime"
"sync"
"testing"
"github.com/ProtonMail/gluon/reporter"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/bradenaw/juniper/stream"
"github.com/bradenaw/juniper/xslices"
"github.com/golang/mock/gomock"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"gitlab.protontech.ch/go/liteapi"
)
@ -100,3 +107,123 @@ func (t *testCtx) createMessages(ctx context.Context, username, addrID string, r
})
})
}
type reportRecord struct {
isException bool
message string
context reporter.Context
}
type reportRecorder struct {
assert *assert.Assertions
reports []reportRecord
lock sync.Locker
isClosed bool
}
func newReportRecorder(tb testing.TB) *reportRecorder {
return &reportRecorder{
assert: assert.New(tb),
reports: []reportRecord{},
lock: &sync.Mutex{},
isClosed: false,
}
}
func (r *reportRecorder) add(isException bool, message string, context reporter.Context) {
r.lock.Lock()
defer r.lock.Unlock()
l := logrus.WithFields(logrus.Fields{
"isException": isException,
"message": message,
"context": context,
"pkg": "test/reportRecorder",
})
if r.isClosed {
l.Warn("Reporter closed, report skipped")
return
}
r.reports = append(r.reports, reportRecord{
isException: isException,
message: message,
context: context,
})
l.Warn("Report recorded")
}
func (r *reportRecorder) close() {
r.lock.Lock()
defer r.lock.Unlock()
r.isClosed = true
}
func (r *reportRecorder) assertEmpty() {
r.assert.Empty(r.reports)
}
func (r *reportRecorder) removeMatchingRecords(isException, message, context gomock.Matcher, n int) {
if n == 0 {
n = len(r.reports)
}
r.reports = xslices.Filter(r.reports, func(rec reportRecord) bool {
if n <= 0 {
return true
}
l := logrus.WithFields(logrus.Fields{
"rec": rec,
})
if !isException.Matches(rec.isException) {
l.WithField("matcher", isException).Debug("Not matching")
return true
}
if !message.Matches(rec.message) {
l.WithField("matcher", message).Debug("Not matching")
return true
}
if !context.Matches(rec.context) {
l.WithField("matcher", context).Debug("Not matching")
return true
}
n--
return false
})
}
func (r *reportRecorder) ReportException(data any) error {
r.add(true, "exception", reporter.Context{"data": data})
return nil
}
func (r *reportRecorder) ReportMessage(message string) error {
r.add(false, message, reporter.Context{})
return nil
}
func (r *reportRecorder) ReportMessageWithContext(message string, context reporter.Context) error {
r.add(false, message, context)
return nil
}
func (r *reportRecorder) ReportExceptionWithContext(data any, context reporter.Context) error {
if context == nil {
context = reporter.Context{}
}
context["data"] = data
r.add(true, "exception", context)
return nil
}

View File

@ -32,6 +32,7 @@ import (
"github.com/ProtonMail/proton-bridge/v2/internal/locations"
"github.com/bradenaw/juniper/xslices"
"github.com/emersion/go-imap/client"
"github.com/golang/mock/gomock"
"github.com/sirupsen/logrus"
"gitlab.protontech.ch/go/liteapi"
"gitlab.protontech.ch/go/liteapi/server"
@ -51,6 +52,7 @@ type testCtx struct {
version *semver.Version
mocks *bridge.Mocks
events *eventCollector
reporter *reportRecorder
// bridge holds the bridge app under test.
bridge *bridge.Bridge
@ -105,6 +107,7 @@ func newTestCtx(tb testing.TB) *testCtx {
version: defaultVersion,
mocks: bridge.NewMocks(tb, defaultVersion, defaultVersion),
events: newEventCollector(),
reporter: newReportRecorder(tb),
userIDByName: make(map[string]string),
userAddrByEmail: make(map[string]map[string]string),
@ -324,4 +327,16 @@ func (t *testCtx) close(ctx context.Context) {
t.api.Close()
t.events.close()
t.reporter.close()
// Closed connection can happen in the end of scenario
t.reporter.removeMatchingRecords(
gomock.Eq(false),
gomock.Eq("Failed to parse imap command"),
gomock.Any(), // mocks.NewClosedConnectionMatcher(),
0,
)
t.reporter.assertEmpty()
}

View File

@ -23,12 +23,16 @@ Feature: IMAP create mailbox
Scenario: Creating folder or label with existing name is not possible
When IMAP client "1" creates "Folders/f1"
Then it fails
And bridge reports a message with "Failed to create mailbox"
When IMAP client "1" creates "Folders/f2"
Then it fails
And bridge reports a message with "Failed to create mailbox"
When IMAP client "1" creates "Labels/l1"
Then it fails
And bridge reports a message with "Failed to create mailbox"
When IMAP client "1" creates "Labels/l2"
Then it fails
And bridge reports a message with "Failed to create mailbox"
When IMAP client "1" creates "Folders/f3"
Then it succeeds
When IMAP client "1" creates "Labels/l3"
@ -182,4 +186,4 @@ Feature: IMAP create mailbox
| Folders/f2/f22 |
| Labels |
| Labels/l1 |
| Labels/l2 |
| Labels/l2 |