99b28c1d95
* refactor: start building an Android package Part of https://github.com/ooni/probe/issues/1335. This seems also a good moment to move some packages out of the engine, e.g., oonimkall. This package, for example, is a consumer of the engine, so it makes sense it's not _inside_ it. * fix: committed some stuff I didn't need to commit * fix: oonimkall needs to be public to build The side effect is that we will probably need to bump the major version number every time we change one of these APIs. (We can also of course choose to violate the basic guidelines of Go software, but I believe this is bad form.) I have no problem in bumping the major quite frequently and in any case this monorepo solution is convinving me more than continuing to keep a split between engine and cli. The need to embed assets to make the probe more reliable trumps the negative effects of having to ~frequently bump major because we expose a public API. * fix: let's not forget about libooniffi Honestly, I don't know what to do with this library. I added it to provide a drop in replacement for MK but I have no idea whether it's used and useful. I would not feel comfortable exposing it, unlike oonimkall, since we're not using it. It may be that the right thing to do here is just to delete the package and reduce the amount of code we're maintaining? * woops, we're still missing the publish android script * fix(publish-android.bash): add proper API key * ouch fix another place where the name changed
119 lines
2.9 KiB
Go
119 lines
2.9 KiB
Go
package oonimkall
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
|
)
|
|
|
|
type RecordingLogger struct {
|
|
DebugLogs []string
|
|
InfoLogs []string
|
|
WarnLogs []string
|
|
mu sync.Mutex
|
|
}
|
|
|
|
func (rl *RecordingLogger) Debug(msg string) {
|
|
rl.mu.Lock()
|
|
defer rl.mu.Unlock()
|
|
rl.DebugLogs = append(rl.DebugLogs, msg)
|
|
}
|
|
|
|
func (rl *RecordingLogger) Info(msg string) {
|
|
rl.mu.Lock()
|
|
defer rl.mu.Unlock()
|
|
rl.InfoLogs = append(rl.InfoLogs, msg)
|
|
}
|
|
|
|
func (rl *RecordingLogger) Warn(msg string) {
|
|
rl.mu.Lock()
|
|
defer rl.mu.Unlock()
|
|
rl.WarnLogs = append(rl.WarnLogs, msg)
|
|
}
|
|
|
|
func LoggerEmitMessages(logger model.Logger) {
|
|
logger.Warnf("a formatted warn message: %+v", io.EOF)
|
|
logger.Warn("a warn string")
|
|
logger.Infof("a formatted info message: %+v", io.EOF)
|
|
logger.Info("a info string")
|
|
logger.Debugf("a formatted debug message: %+v", io.EOF)
|
|
logger.Debug("a debug string")
|
|
}
|
|
|
|
func TestNewLoggerNilLogger(t *testing.T) {
|
|
// The objective of this test is to make sure that, even if the
|
|
// Logger instance is nil, we get back something that works, that
|
|
// is, something that does not crash when it is used.
|
|
logger := newLogger(nil, true)
|
|
LoggerEmitMessages(logger)
|
|
}
|
|
|
|
func (rl *RecordingLogger) VerifyNumberOfEntries(debugEntries int) error {
|
|
if len(rl.DebugLogs) != debugEntries {
|
|
return errors.New("unexpected number of debug messages")
|
|
}
|
|
if len(rl.InfoLogs) != 2 {
|
|
return errors.New("unexpected number of info messages")
|
|
}
|
|
if len(rl.WarnLogs) != 2 {
|
|
return errors.New("unexpected number of warn messages")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (rl *RecordingLogger) ExpectedEntries(level string) []string {
|
|
return []string{
|
|
fmt.Sprintf("a formatted %s message: %+v", level, io.EOF),
|
|
fmt.Sprintf("a %s string", level),
|
|
}
|
|
}
|
|
|
|
func (rl *RecordingLogger) CheckNonVerboseEntries() error {
|
|
if diff := cmp.Diff(rl.InfoLogs, rl.ExpectedEntries("info")); diff != "" {
|
|
return errors.New(diff)
|
|
}
|
|
if diff := cmp.Diff(rl.WarnLogs, rl.ExpectedEntries("warn")); diff != "" {
|
|
return errors.New(diff)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (rl *RecordingLogger) CheckVerboseEntries() error {
|
|
if diff := cmp.Diff(rl.DebugLogs, rl.ExpectedEntries("debug")); diff != "" {
|
|
return errors.New(diff)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func TestNewLoggerQuietLogger(t *testing.T) {
|
|
handler := new(RecordingLogger)
|
|
logger := newLogger(handler, false)
|
|
LoggerEmitMessages(logger)
|
|
if err := handler.VerifyNumberOfEntries(0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := handler.CheckNonVerboseEntries(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestNewLoggerVerboseLogger(t *testing.T) {
|
|
handler := new(RecordingLogger)
|
|
logger := newLogger(handler, true)
|
|
LoggerEmitMessages(logger)
|
|
if err := handler.VerifyNumberOfEntries(2); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := handler.CheckNonVerboseEntries(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := handler.CheckVerboseEntries(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|