use debug logging in tests

by setting the loglevel to debug in package mlog.
we restore the "info" logging in main.
except for "mox localserve", which still sets debug by default.
This commit is contained in:
Mechiel Lukkien 2024-05-10 15:15:56 +02:00
parent bf8cfd9724
commit 9152384fd3
No known key found for this signature in database
9 changed files with 44 additions and 20 deletions

View file

@ -12,7 +12,6 @@ import (
"crypto/x509/pkix" "crypto/x509/pkix"
"errors" "errors"
"fmt" "fmt"
"log/slog"
"math/big" "math/big"
"net" "net"
"reflect" "reflect"
@ -36,7 +35,6 @@ func tcheckf(t *testing.T, err error, format string, args ...any) {
// Test dialing and DANE TLS verification. // Test dialing and DANE TLS verification.
func TestDial(t *testing.T) { func TestDial(t *testing.T) {
mlog.SetConfig(map[string]slog.Level{"": mlog.LevelDebug})
log := mlog.New("dane", nil) log := mlog.New("dane", nil)
// Create fake CA/trusted-anchor certificate. // Create fake CA/trusted-anchor certificate.

View file

@ -6,7 +6,6 @@ import (
"encoding/xml" "encoding/xml"
"fmt" "fmt"
"io" "io"
"log/slog"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -157,8 +156,6 @@ func TestEvaluations(t *testing.T) {
} }
func TestSendReports(t *testing.T) { func TestSendReports(t *testing.T) {
mlog.SetConfig(map[string]slog.Level{"": slog.LevelDebug})
os.RemoveAll("../testdata/dmarcdb/data") os.RemoveAll("../testdata/dmarcdb/data")
mox.Context = ctxbg mox.Context = ctxbg
mox.ConfigStaticPath = filepath.FromSlash("../testdata/dmarcdb/mox.conf") mox.ConfigStaticPath = filepath.FromSlash("../testdata/dmarcdb/mox.conf")

View file

@ -120,11 +120,14 @@ during those commands instead of during "data".
existingConfig = true existingConfig = true
} }
if level, ok := mlog.Levels[loglevel]; loglevel != "" && ok { // For new configs, we keep the "info" loglevel set by writeLocalConfig until after
mox.Conf.Log[""] = level // initializing database files, to prevent lots of schema upgrade logging.
mlog.SetConfig(mox.Conf.Log) fallbackLevel := mox.Conf.Static.LogLevel
} else if loglevel != "" && !ok { if fallbackLevel == "" {
log.Fatal("unknown loglevel", slog.String("loglevel", loglevel)) fallbackLevel = "debug"
}
if existingConfig {
loadLoglevel(log, fallbackLevel)
} }
// Initialize receivedid. // Initialize receivedid.
@ -158,6 +161,9 @@ during those commands instead of during "data".
if err := start(mtastsdbRefresher, sendDMARCReports, sendTLSReports, skipForkExec); err != nil { if err := start(mtastsdbRefresher, sendDMARCReports, sendTLSReports, skipForkExec); err != nil {
log.Fatalx("starting mox", err) log.Fatalx("starting mox", err)
} }
loadLoglevel(log, fallbackLevel)
golog.Printf("mox, version %s, %s %s/%s", moxvar.Version, runtime.Version(), runtime.GOOS, runtime.GOARCH) golog.Printf("mox, version %s, %s %s/%s", moxvar.Version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
golog.Print("") golog.Print("")
golog.Printf("the default user is mox@localhost, with password moxmoxmox") golog.Printf("the default user is mox@localhost, with password moxmoxmox")
@ -471,6 +477,9 @@ func writeLocalConfig(log mlog.Log, dir, ip string) (rerr error) {
err = localLoadConfig(log, dir) err = localLoadConfig(log, dir)
xcheck(err, "loading config") xcheck(err, "loading config")
// Info so we don't log lots about initializing database.
loadLoglevel(log, "info")
// Set password on account. // Set password on account.
a, _, err := store.OpenEmail(log, "mox@localhost") a, _, err := store.OpenEmail(log, "mox@localhost")
xcheck(err, "opening account to set password") xcheck(err, "opening account to set password")
@ -484,6 +493,19 @@ func writeLocalConfig(log mlog.Log, dir, ip string) (rerr error) {
return nil return nil
} }
func loadLoglevel(log mlog.Log, fallback string) {
ll := loglevel
if ll == "" {
ll = fallback
}
if level, ok := mlog.Levels[ll]; ok {
mox.Conf.Log[""] = level
mlog.SetConfig(mox.Conf.Log)
} else {
log.Fatal("unknown loglevel", slog.String("loglevel", loglevel))
}
}
func localLoadConfig(log mlog.Log, dir string) error { func localLoadConfig(log mlog.Log, dir string) error {
mox.ConfigStaticPath = filepath.Join(dir, "mox.conf") mox.ConfigStaticPath = filepath.Join(dir, "mox.conf")
mox.ConfigDynamicPath = filepath.Join(dir, "domains.conf") mox.ConfigDynamicPath = filepath.Join(dir, "domains.conf")

18
main.go
View file

@ -416,7 +416,7 @@ func usage(l []cmd, unlisted bool) {
os.Exit(2) os.Exit(2)
} }
var loglevel string var loglevel string // Empty will be interpreted as info, except by localserve.
var pedantic bool var pedantic bool
// subcommands that are not "serve" should use this function to load the config, it // subcommands that are not "serve" should use this function to load the config, it
@ -424,10 +424,14 @@ var pedantic bool
// loglevels from the config file and it does not load files like TLS keys/certs. // loglevels from the config file and it does not load files like TLS keys/certs.
func mustLoadConfig() { func mustLoadConfig() {
mox.MustLoadConfig(false, false) mox.MustLoadConfig(false, false)
if level, ok := mlog.Levels[loglevel]; loglevel != "" && ok { ll := loglevel
if ll == "" {
ll = "info"
}
if level, ok := mlog.Levels[ll]; ok {
mox.Conf.Log[""] = level mox.Conf.Log[""] = level
mlog.SetConfig(mox.Conf.Log) mlog.SetConfig(mox.Conf.Log)
} else if loglevel != "" && !ok { } else {
log.Fatal("unknown loglevel", slog.String("loglevel", loglevel)) log.Fatal("unknown loglevel", slog.String("loglevel", loglevel))
} }
if pedantic { if pedantic {
@ -486,10 +490,16 @@ func main() {
} }
mox.ConfigDynamicPath = filepath.Join(filepath.Dir(mox.ConfigStaticPath), "domains.conf") mox.ConfigDynamicPath = filepath.Join(filepath.Dir(mox.ConfigStaticPath), "domains.conf")
if level, ok := mlog.Levels[loglevel]; ok && loglevel != "" { ll := loglevel
if ll == "" {
ll = "info"
}
if level, ok := mlog.Levels[ll]; ok {
mox.Conf.Log[""] = level mox.Conf.Log[""] = level
mlog.SetConfig(mox.Conf.Log) mlog.SetConfig(mox.Conf.Log)
// note: SetConfig may be called again when subcommands loads config. // note: SetConfig may be called again when subcommands loads config.
} else {
log.Fatalf("unknown loglevel %q", loglevel)
} }
var partial []cmd var partial []cmd

View file

@ -43,7 +43,7 @@ var lowestLevel atomic.Int32 // For quick initial check.
var config atomic.Pointer[map[string]slog.Level] // For secondary complete check for match. var config atomic.Pointer[map[string]slog.Level] // For secondary complete check for match.
func init() { func init() {
SetConfig(map[string]slog.Level{"": LevelInfo}) SetConfig(map[string]slog.Level{"": LevelDebug})
} }
// SetConfig atomically sets the new log levels used by all Log instances. // SetConfig atomically sets the new log levels used by all Log instances.

View file

@ -9,7 +9,6 @@ import (
"errors" "errors"
"io" "io"
golog "log" golog "log"
"log/slog"
"math/big" "math/big"
"net" "net"
"net/http" "net/http"
@ -26,7 +25,6 @@ import (
) )
func TestLookup(t *testing.T) { func TestLookup(t *testing.T) {
mlog.SetConfig(map[string]slog.Level{"": mlog.LevelDebug})
log := mlog.New("mtasts", nil) log := mlog.New("mtasts", nil)
resolver := dns.MockResolver{ resolver := dns.MockResolver{

View file

@ -37,6 +37,7 @@ func TestClient(t *testing.T) {
log := mlog.New("smtpclient", nil) log := mlog.New("smtpclient", nil)
mlog.SetConfig(map[string]slog.Level{"": mlog.LevelTrace}) mlog.SetConfig(map[string]slog.Level{"": mlog.LevelTrace})
defer mlog.SetConfig(map[string]slog.Level{"": mlog.LevelDebug})
type options struct { type options struct {
// Server behaviour. // Server behaviour.

View file

@ -1119,6 +1119,7 @@ func TestRatelimitConnectionrate(t *testing.T) {
// We'll be creating 300 connections, no TLS and reduce noise. // We'll be creating 300 connections, no TLS and reduce noise.
ts.tlsmode = smtpclient.TLSSkip ts.tlsmode = smtpclient.TLSSkip
mlog.SetConfig(map[string]slog.Level{"": mlog.LevelInfo}) mlog.SetConfig(map[string]slog.Level{"": mlog.LevelInfo})
defer mlog.SetConfig(map[string]slog.Level{"": mlog.LevelDebug})
// We may be passing a window boundary during this tests. The limit is 300/minute. // We may be passing a window boundary during this tests. The limit is 300/minute.
// So make twice that many connections and hope the tests don't take too long. // So make twice that many connections and hope the tests don't take too long.

View file

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"log/slog"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -40,8 +39,6 @@ func tcompare(t *testing.T, got, expect any) {
} }
func TestSendReports(t *testing.T) { func TestSendReports(t *testing.T) {
mlog.SetConfig(map[string]slog.Level{"": mlog.LevelDebug})
os.RemoveAll("../testdata/tlsrptsend/data") os.RemoveAll("../testdata/tlsrptsend/data")
mox.Context = ctxbg mox.Context = ctxbg
mox.ConfigStaticPath = filepath.FromSlash("../testdata/tlsrptsend/mox.conf") mox.ConfigStaticPath = filepath.FromSlash("../testdata/tlsrptsend/mox.conf")