1
0

server_syslog_test.go 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. //go:build !windows
  2. package service
  3. import (
  4. "os"
  5. "path/filepath"
  6. "slices"
  7. "testing"
  8. "time"
  9. )
  10. // A journalctl that never answers must not hang the Syslog request (#6629).
  11. func TestGetLogsSyslogGivesUpOnAStalledJournalctl(t *testing.T) {
  12. dir := t.TempDir()
  13. if err := os.WriteFile(filepath.Join(dir, "journalctl"), []byte("#!/bin/sh\nexec sleep 5\n"), 0o755); err != nil {
  14. t.Fatalf("write fake journalctl: %v", err)
  15. }
  16. t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
  17. saved := syslogTimeout
  18. syslogTimeout = 200 * time.Millisecond
  19. t.Cleanup(func() { syslogTimeout = saved })
  20. start := time.Now()
  21. got := (&ServerService{}).GetLogs("10", "err", "true")
  22. want := []string{"journalctl did not answer in time. Try a smaller line count or a less strict level."}
  23. if !slices.Equal(got, want) {
  24. t.Fatalf("GetLogs = %q, want %q", got, want)
  25. }
  26. if elapsed := time.Since(start); elapsed > 3*time.Second {
  27. t.Fatalf("GetLogs waited %v for a stalled journalctl, want about %v", elapsed, syslogTimeout)
  28. }
  29. }