Log fx's startup steps at debug; a refused setting is one line (closes #209)
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
fx's event lines, errors included, now log at debug level, so a normal start shows none and DEBUG shows them. RunServer starts and stops the fx app itself instead of calling Run: when the start fails, it logs the error's root cause as one "cannot start" line, the backend's last, and returns 1. Main returns the server's exit code. Model: opus-5-5
This commit is contained in:
1 parent
93bbd80d57
commit
0809337c63
5 files changed
+172
-41
No files matched your search
@@ -36,16 +36,94 @@ func TestMain(m *testing.M) {
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
// TestOutputIsJSON: off a terminal, every line the server writes from
|
||||
// start to stop is JSON, fx's own lines included.
|
||||
func TestOutputIsJSON(t *testing.T) {
|
||||
// TestOutputIsJSONWithoutFxLines: off a terminal, every line the server
|
||||
// writes from start to stop is JSON, and without DEBUG none of them is
|
||||
// one of fx's.
|
||||
func TestOutputIsJSONWithoutFxLines(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
stdout, stderr := runServerUntilStopped(t)
|
||||
|
||||
requireJSONLines(t, stdout, stderr)
|
||||
|
||||
if !strings.Contains(stdout.String(), `"msg":"starting"`) {
|
||||
t.Fatalf("no startup line in stdout:\n%s", stdout)
|
||||
}
|
||||
|
||||
if strings.Contains(stdout.String(), `"msg":"started"`) {
|
||||
t.Fatalf("fx line in stdout without DEBUG:\n%s", stdout)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDebugLogsFxLines: with DEBUG set, fx's lines are logged, as
|
||||
// JSON.
|
||||
func TestDebugLogsFxLines(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
stdout, stderr := runServerUntilStopped(t, "DEBUG=1")
|
||||
|
||||
requireJSONLines(t, stdout, stderr)
|
||||
|
||||
if !strings.Contains(stdout.String(), `"msg":"started"`) {
|
||||
t.Fatalf("no fx line in stdout with DEBUG set:\n%s", stdout)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRefusedSettingIsLastLine: a setting the server refuses stops the
|
||||
// start, and the last line logged is one error naming the setting and
|
||||
// why, without fx's text around it.
|
||||
func TestRefusedSettingIsLastLine(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx, cancel := context.WithTimeout(t.Context(), childTimeout)
|
||||
defer cancel()
|
||||
|
||||
child, stdout, stderr := startServer(ctx, t, t.TempDir(),
|
||||
freePort(ctx, t), "REPORTS_PER_MINUTE=0")
|
||||
|
||||
err := child.Wait()
|
||||
if child.ProcessState.ExitCode() != 1 {
|
||||
t.Fatalf("server exit = %v, want exit status 1", err)
|
||||
}
|
||||
|
||||
if stderr.Len() != 0 {
|
||||
t.Fatalf("stderr = %q, want nothing", stderr)
|
||||
}
|
||||
|
||||
lines := strings.Split(strings.TrimSpace(stdout.String()), "\n")
|
||||
|
||||
var last struct {
|
||||
Level string `json:"level"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
err = json.Unmarshal([]byte(lines[len(lines)-1]), &last)
|
||||
if err != nil {
|
||||
t.Fatalf("last line is not JSON: %v\n%s", err, stdout)
|
||||
}
|
||||
|
||||
want := `REPORTS_PER_MINUTE "0": must be a positive whole number`
|
||||
if last.Level != "ERROR" || last.Error != want {
|
||||
t.Fatalf("last line has level %q, error %q; want ERROR, %q\n%s",
|
||||
last.Level, last.Error, want, stdout)
|
||||
}
|
||||
}
|
||||
|
||||
// runServerUntilStopped runs main() in a child process with env added
|
||||
// to its environment, waits until it answers its health check, stops it
|
||||
// with SIGTERM and returns its stdout and stderr. It fails the test if
|
||||
// the child does not exit cleanly.
|
||||
func runServerUntilStopped(
|
||||
t *testing.T,
|
||||
env ...string,
|
||||
) (*bytes.Buffer, *bytes.Buffer) {
|
||||
t.Helper()
|
||||
|
||||
ctx, cancel := context.WithTimeout(t.Context(), childTimeout)
|
||||
defer cancel()
|
||||
|
||||
port := freePort(ctx, t)
|
||||
child, stdout, stderr := startServer(ctx, t, t.TempDir(), port)
|
||||
child, stdout, stderr := startServer(ctx, t, t.TempDir(), port, env...)
|
||||
|
||||
waitForHealthcheck(ctx, t, port)
|
||||
|
||||
@@ -72,11 +150,7 @@ func TestOutputIsJSON(t *testing.T) {
|
||||
t.Fatalf("server exit = %v, want success", err)
|
||||
}
|
||||
|
||||
requireJSONLines(t, stdout, stderr)
|
||||
|
||||
if !strings.Contains(stdout.String(), `"msg":"starting"`) {
|
||||
t.Fatalf("no startup line in stdout:\n%s", stdout)
|
||||
}
|
||||
return stdout, stderr
|
||||
}
|
||||
|
||||
// TestMalformedConfigFileStopsTheStart: a config file the server finds
|
||||
@@ -139,7 +213,8 @@ func TestRefusedSentryDSNStopsTheStart(t *testing.T) {
|
||||
|
||||
// startServer runs main() in a child process listening on
|
||||
// 127.0.0.1:port, with home as its HOME and working directory and its
|
||||
// data directory in home, so it touches nothing outside home. Its
|
||||
// data directory in home, so it touches nothing outside home. env is
|
||||
// added to its environment, as NAME=value strings. Its
|
||||
// stdout and stderr go to the two buffers returned, which hold all of
|
||||
// it once child.Wait returns. The child is killed when ctx ends, and
|
||||
// killed and reaped when the test ends if nothing waited for it.
|
||||
@@ -147,6 +222,7 @@ func startServer(
|
||||
ctx context.Context,
|
||||
t *testing.T,
|
||||
home, port string,
|
||||
env ...string,
|
||||
) (*exec.Cmd, *bytes.Buffer, *bytes.Buffer) {
|
||||
t.Helper()
|
||||
|
||||
@@ -166,6 +242,7 @@ func startServer(
|
||||
"BIND_ADDRESS=127.0.0.1",
|
||||
"PORT="+port,
|
||||
)
|
||||
child.Env = append(child.Env, env...)
|
||||
child.Stdout = &stdout
|
||||
child.Stderr = &stderr
|
||||
|
||||
|
||||
Reference in new issue
Block a user