190 lines
6.0 KiB
Go
190 lines
6.0 KiB
Go
package main
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/json"
|
||
|
|
"errors"
|
||
|
|
"io"
|
||
|
|
"net/http"
|
||
|
|
"net/http/httptest"
|
||
|
|
"os"
|
||
|
|
"strings"
|
||
|
|
"sync"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"repos.gfranz.ch/Gabriel20xx/shared/backend/coreconfig"
|
||
|
|
"repos.gfranz.ch/Gabriel20xx/shared/backend/logshipper"
|
||
|
|
)
|
||
|
|
|
||
|
|
// fakeOmniLogger is a stand-in OmniLogger that accepts only the bearer token it was started with and
|
||
|
|
// records every entry it receives.
|
||
|
|
type fakeOmniLogger struct {
|
||
|
|
*httptest.Server
|
||
|
|
mu sync.Mutex
|
||
|
|
requests int
|
||
|
|
entries []map[string]any
|
||
|
|
}
|
||
|
|
|
||
|
|
func newFakeOmniLogger(t *testing.T, token string) *fakeOmniLogger {
|
||
|
|
t.Helper()
|
||
|
|
f := &fakeOmniLogger{}
|
||
|
|
f.Server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
|
f.mu.Lock()
|
||
|
|
f.requests++
|
||
|
|
f.mu.Unlock()
|
||
|
|
if r.Header.Get("Authorization") != "Bearer "+token {
|
||
|
|
w.WriteHeader(http.StatusUnauthorized)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
var batch []map[string]any
|
||
|
|
if err := json.NewDecoder(r.Body).Decode(&batch); err != nil {
|
||
|
|
w.WriteHeader(http.StatusBadRequest)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
f.mu.Lock()
|
||
|
|
f.entries = append(f.entries, batch...)
|
||
|
|
f.mu.Unlock()
|
||
|
|
w.WriteHeader(http.StatusAccepted)
|
||
|
|
}))
|
||
|
|
t.Cleanup(f.Close)
|
||
|
|
return f
|
||
|
|
}
|
||
|
|
|
||
|
|
// received reports how many requests arrived and the entries that were accepted.
|
||
|
|
func (f *fakeOmniLogger) received() (int, []map[string]any) {
|
||
|
|
f.mu.Lock()
|
||
|
|
defer f.mu.Unlock()
|
||
|
|
return f.requests, append([]map[string]any(nil), f.entries...)
|
||
|
|
}
|
||
|
|
|
||
|
|
// hasMessage reports whether an entry for the collabtable service contains text.
|
||
|
|
func hasMessage(entries []map[string]any, text string) bool {
|
||
|
|
for _, entry := range entries {
|
||
|
|
message, _ := entry["message"].(string)
|
||
|
|
if entry["service"] == "collabtable" && strings.Contains(message, text) {
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
|
||
|
|
// useOmniShipper makes shipper the package's omniShipper for the rest of the test. The helper Loggers are rebuilt
|
||
|
|
// on it with their consoles discarded, since they are made once at start-up and would otherwise keep the old shipper.
|
||
|
|
func useOmniShipper(t *testing.T, shipper *logshipper.Shipper) {
|
||
|
|
t.Helper()
|
||
|
|
previous, previousOut, previousErr := omniShipper, stdoutLog, stderrLog
|
||
|
|
omniShipper = shipper
|
||
|
|
stdoutLog = newConsoleLogger(io.Discard, shipper)
|
||
|
|
stderrLog = newConsoleLogger(io.Discard, shipper)
|
||
|
|
t.Cleanup(func() {
|
||
|
|
_ = shipper.Close()
|
||
|
|
omniShipper = previous
|
||
|
|
stdoutLog, stderrLog = previousOut, previousErr
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestOmniLoggerShipperIsNeverNilAndDisabledWithoutConfig(t *testing.T) {
|
||
|
|
if omniShipper == nil {
|
||
|
|
t.Fatal("omniShipper must never be nil")
|
||
|
|
}
|
||
|
|
|
||
|
|
t.Setenv("OMNILOGGER_URL", "")
|
||
|
|
t.Setenv("OMNILOGGER_INGEST_TOKEN", "")
|
||
|
|
shipper := newOmniLoggerShipper()
|
||
|
|
if shipper == nil {
|
||
|
|
t.Fatal("shipper must never be nil; with no configuration it is simply disabled")
|
||
|
|
}
|
||
|
|
useOmniShipper(t, shipper)
|
||
|
|
logPrintf(logshipper.LevelInfo, "no configuration, nothing should ship\n")
|
||
|
|
shipper.Flush()
|
||
|
|
|
||
|
|
// A URL without a token is still disabled, so nothing reaches a reachable OmniLogger.
|
||
|
|
sink := newFakeOmniLogger(t, "test-token")
|
||
|
|
t.Setenv("OMNILOGGER_URL", sink.URL)
|
||
|
|
shipper = newOmniLoggerShipper()
|
||
|
|
useOmniShipper(t, shipper)
|
||
|
|
logPrintf(logshipper.LevelInfo, "no token, nothing should ship\n")
|
||
|
|
shipper.Flush()
|
||
|
|
if requests, _ := sink.received(); requests != 0 {
|
||
|
|
t.Fatalf("disabled shipper sent %d requests to OmniLogger", requests)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestLogPrintfReachesOmniLogger(t *testing.T) {
|
||
|
|
sink := newFakeOmniLogger(t, "test-token")
|
||
|
|
t.Setenv("OMNILOGGER_URL", sink.URL)
|
||
|
|
t.Setenv("OMNILOGGER_INGEST_TOKEN", "test-token")
|
||
|
|
useOmniShipper(t, newOmniLoggerShipper())
|
||
|
|
|
||
|
|
logPrintf(logshipper.LevelInfo, "[test] hello from collabtable %d\n", 7)
|
||
|
|
_ = omniShipper.Close()
|
||
|
|
|
||
|
|
if _, entries := sink.received(); !hasMessage(entries, "hello from collabtable 7") {
|
||
|
|
t.Fatalf("OmniLogger did not receive the line; entries: %v", entries)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestOmniLoggerFollowsCoreConfigPush(t *testing.T) {
|
||
|
|
sink := newFakeOmniLogger(t, "pushed-token")
|
||
|
|
t.Setenv("OMNILOGGER_URL", "")
|
||
|
|
t.Setenv("OMNILOGGER_INGEST_TOKEN", "")
|
||
|
|
shipper := newOmniLoggerShipper()
|
||
|
|
useOmniShipper(t, shipper)
|
||
|
|
store := coreconfig.NewStore(coreconfig.Info{})
|
||
|
|
shipper.Follow(store)
|
||
|
|
|
||
|
|
logPrintf(logshipper.LevelInfo, "before the push\n")
|
||
|
|
shipper.Flush()
|
||
|
|
if requests, _ := sink.received(); requests != 0 {
|
||
|
|
t.Fatalf("shipper sent %d requests before ProjectConfigurator pushed a token", requests)
|
||
|
|
}
|
||
|
|
|
||
|
|
store.Set(coreconfig.Info{OmniLoggerURL: sink.URL, OmniLoggerIngestToken: "pushed-token"})
|
||
|
|
logPrintf(logshipper.LevelInfo, "after the push\n")
|
||
|
|
_ = shipper.Close()
|
||
|
|
|
||
|
|
_, entries := sink.received()
|
||
|
|
if !hasMessage(entries, "after the push") {
|
||
|
|
t.Fatalf("OmniLogger did not receive the line after the push; entries: %v", entries)
|
||
|
|
}
|
||
|
|
if hasMessage(entries, "before the push") {
|
||
|
|
t.Fatal("a line logged before the push was shipped")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestConvertedNotificationLineShipsOnceAtWarnAndKeepsConsoleLine runs a converted call site: a failed
|
||
|
|
// notification is printed on stdout as before, and reaches OmniLogger once, at warn.
|
||
|
|
func TestConvertedNotificationLineShipsOnceAtWarnAndKeepsConsoleLine(t *testing.T) {
|
||
|
|
sink := newFakeOmniLogger(t, "test-token")
|
||
|
|
t.Setenv("OMNILOGGER_URL", sink.URL)
|
||
|
|
t.Setenv("OMNILOGGER_INGEST_TOKEN", "test-token")
|
||
|
|
useOmniShipper(t, newOmniLoggerShipper())
|
||
|
|
|
||
|
|
reader, writer, err := os.Pipe()
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
previousStdout := stdoutLog
|
||
|
|
stdoutLog = newConsoleLogger(writer, omniShipper)
|
||
|
|
ignoreNotificationError(errors.New("disk full"))
|
||
|
|
stdoutLog = previousStdout
|
||
|
|
_ = writer.Close()
|
||
|
|
console, _ := io.ReadAll(reader)
|
||
|
|
_ = reader.Close()
|
||
|
|
if got, want := string(console), "[notifications] enqueue failed: disk full\n"; got != want {
|
||
|
|
t.Fatalf("console output = %q, want %q", got, want)
|
||
|
|
}
|
||
|
|
|
||
|
|
_ = omniShipper.Close()
|
||
|
|
_, entries := sink.received()
|
||
|
|
var levels []string
|
||
|
|
for _, entry := range entries {
|
||
|
|
message, _ := entry["message"].(string)
|
||
|
|
if entry["service"] == "collabtable" && message == "[notifications] enqueue failed: disk full" {
|
||
|
|
level, _ := entry["level"].(string)
|
||
|
|
levels = append(levels, level)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if len(levels) != 1 || levels[0] != "warn" {
|
||
|
|
t.Fatalf("OmniLogger levels for the notification line = %v, want exactly one warn (entries %v)", levels, entries)
|
||
|
|
}
|
||
|
|
}
|