ooni-probe-cli/pkg/oonimkall/tasklogger_test.go
Simone Basso 273b70bacc
refactor: interfaces and data types into the model package (#642)
## Checklist

- [x] I have read the [contribution guidelines](https://github.com/ooni/probe-cli/blob/master/CONTRIBUTING.md)
- [x] reference issue for this pull request: https://github.com/ooni/probe/issues/1885
- [x] related ooni/spec pull request: N/A

Location of the issue tracker: https://github.com/ooni/probe

## Description

This PR contains a set of changes to move important interfaces and data types into the `./internal/model` package.

The criteria for including an interface or data type in here is roughly that the type should be important and used by several packages. We are especially interested to move more interfaces here to increase modularity.

An additional side effect is that, by reading this package, one should be able to understand more quickly how different parts of the codebase interact with each other.

This is what I want to move in `internal/model`:

- [x] most important interfaces from `internal/netxlite`
- [x] everything that was previously part of `internal/engine/model`
- [x] mocks from `internal/netxlite/mocks` should also be moved in here as a subpackage
2022-01-03 13:53:23 +01:00

107 lines
3.2 KiB
Go

package oonimkall
import (
"testing"
"github.com/ooni/probe-cli/v3/internal/model"
)
//
// This file contains tests for the taskLogger type.
//
func TestTaskLogger(t *testing.T) {
// debugMessage is the debug message we expect to see.
debugMessage := "debug message"
// infoMessage is the info message we expect to see.
infoMessage := "info message"
// warningMessage is the warning message we expect to see.
warningMessage := "warning message"
// emitMessages is an helper function for implementing this test.
emitMessages := func(logger model.Logger) {
logger.Debug(debugMessage)
logger.Debugf("%s", debugMessage)
logger.Info(infoMessage)
logger.Infof("%s", infoMessage)
logger.Warn(warningMessage)
logger.Warnf("%s", warningMessage)
}
// convertEventsToLogEvents converts the generic events to
// logEvents and fails if this operation is not possible.
convertEventsToLogEvents := func(t *testing.T, in []*event) (out []eventLog) {
for _, ev := range in {
if ev.Key != eventTypeLog {
t.Fatalf("expected log event, found %s", ev.Key)
}
out = append(out, ev.Value.(eventLog))
}
return
}
// checkNumberOfEvents ensures we've the right number of events.
checkNumberOfEvents := func(t *testing.T, events []eventLog, expect int) {
if len(events) != expect {
t.Fatalf(
"invalid number of log events %d (expected %d)",
len(events), expect,
)
}
}
// matchEvent ensures the given event has the right level and message.
matchEvent := func(t *testing.T, event eventLog, level, msg string) {
if event.LogLevel != level {
t.Fatalf(
"invalid log level %s (expected %s)",
event.LogLevel, level,
)
}
if event.Message != msg {
t.Fatalf(
"invalid log message '%s' (expected '%s')",
event.Message, msg,
)
}
}
t.Run("debug logger", func(t *testing.T) {
emitter := &CollectorTaskEmitter{}
logger := newTaskLogger(emitter, logLevelDebug)
emitMessages(logger)
logEvents := convertEventsToLogEvents(t, emitter.Collect())
checkNumberOfEvents(t, logEvents, 6)
matchEvent(t, logEvents[0], logLevelDebug, debugMessage)
matchEvent(t, logEvents[1], logLevelDebug, debugMessage)
matchEvent(t, logEvents[2], logLevelInfo, infoMessage)
matchEvent(t, logEvents[3], logLevelInfo, infoMessage)
matchEvent(t, logEvents[4], logLevelWarning, warningMessage)
matchEvent(t, logEvents[5], logLevelWarning, warningMessage)
})
t.Run("info logger", func(t *testing.T) {
emitter := &CollectorTaskEmitter{}
logger := newTaskLogger(emitter, logLevelInfo)
emitMessages(logger)
logEvents := convertEventsToLogEvents(t, emitter.Collect())
checkNumberOfEvents(t, logEvents, 4)
matchEvent(t, logEvents[0], logLevelInfo, infoMessage)
matchEvent(t, logEvents[1], logLevelInfo, infoMessage)
matchEvent(t, logEvents[2], logLevelWarning, warningMessage)
matchEvent(t, logEvents[3], logLevelWarning, warningMessage)
})
t.Run("warn logger", func(t *testing.T) {
emitter := &CollectorTaskEmitter{}
logger := newTaskLogger(emitter, logLevelWarning)
emitMessages(logger)
logEvents := convertEventsToLogEvents(t, emitter.Collect())
checkNumberOfEvents(t, logEvents, 2)
matchEvent(t, logEvents[0], logLevelWarning, warningMessage)
matchEvent(t, logEvents[1], logLevelWarning, warningMessage)
})
}