Skip to content

Commit 845a4c5

Browse files
committed
add a log volume test
1 parent a960b8e commit 845a4c5

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

cli/server_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"runtime"
2626
"strconv"
2727
"strings"
28+
"sync"
2829
"sync/atomic"
2930
"testing"
3031
"time"
@@ -240,6 +241,70 @@ func TestServer(t *testing.T) {
240241
t.Fatalf("expected postgres URL to start with \"postgres://\", got %q", got)
241242
}
242243
})
244+
t.Run("SpammyLogs", func(t *testing.T) {
245+
// The purpose of this test is to ensure we don't show excessive logs when the server starts.
246+
t.Parallel()
247+
inv, cfg := clitest.New(t,
248+
"server",
249+
"--in-memory",
250+
"--http-address", ":0",
251+
"--access-url", "http://localhost:3000/",
252+
"--cache-dir", t.TempDir(),
253+
)
254+
stdoutRW := syncReaderWriter{}
255+
stderrRW := syncReaderWriter{}
256+
inv.Stdout = io.MultiWriter(os.Stdout, &stdoutRW)
257+
inv.Stderr = io.MultiWriter(os.Stderr, &stderrRW)
258+
clitest.Start(t, inv)
259+
260+
// Wait for startup
261+
_ = waitAccessURL(t, cfg)
262+
263+
// Wait a bit for more logs to be printed.
264+
time.Sleep(testutil.WaitShort)
265+
266+
// Lines containing these strings are printed because we're
267+
// running the server with a test config. They wouldn't be
268+
// normally shown to the user, so we'll ignore them.
269+
ignoreLines := []string{
270+
"isn't externally reachable",
271+
"install.sh will be unavailable",
272+
"telemetry disabled, unable to notify of security issues",
273+
}
274+
275+
countLines := func(fullOutput string) int {
276+
terminalWidth := 80
277+
linesByNewline := strings.Split(fullOutput, "\n")
278+
countByWidth := 0
279+
lineLoop:
280+
for _, line := range linesByNewline {
281+
for _, ignoreLine := range ignoreLines {
282+
if strings.Contains(line, ignoreLine) {
283+
continue lineLoop
284+
}
285+
}
286+
if line == "" {
287+
// Empty lines take up one line.
288+
countByWidth++
289+
} else {
290+
countByWidth += (len(line) + terminalWidth - 1) / terminalWidth
291+
}
292+
}
293+
return countByWidth
294+
}
295+
296+
stdout, err := io.ReadAll(&stdoutRW)
297+
if err != nil {
298+
t.Fatalf("failed to read stdout: %v", err)
299+
}
300+
stderr, err := io.ReadAll(&stderrRW)
301+
if err != nil {
302+
t.Fatalf("failed to read stderr: %v", err)
303+
}
304+
305+
numLines := countLines(string(stdout)) + countLines(string(stderr))
306+
require.Less(t, numLines, 20)
307+
})
243308

244309
// Validate that a warning is printed that it may not be externally
245310
// reachable.
@@ -2140,3 +2205,26 @@ func mockTelemetryServer(t *testing.T) (*url.URL, chan *telemetry.Deployment, ch
21402205

21412206
return serverURL, deployment, snapshot
21422207
}
2208+
2209+
type safeBuffer struct {
2210+
buf bytes.Buffer
2211+
mu sync.Mutex
2212+
}
2213+
2214+
// syncWriter provides a thread-safe io.ReadWriter implementation
2215+
type syncReaderWriter struct {
2216+
buf safeBuffer
2217+
}
2218+
2219+
func (w *syncReaderWriter) Write(p []byte) (n int, err error) {
2220+
w.buf.mu.Lock()
2221+
defer w.buf.mu.Unlock()
2222+
return w.buf.buf.Write(p)
2223+
}
2224+
2225+
func (w *syncReaderWriter) Read(p []byte) (n int, err error) {
2226+
w.buf.mu.Lock()
2227+
defer w.buf.mu.Unlock()
2228+
2229+
return w.buf.buf.Read(p)
2230+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy