Skip to content

Commit

Permalink
Ensure call to systemd-cat worked correctly
Browse files Browse the repository at this point in the history
Test the bytes written to systemd-cat on every call, properly handle
errors when closing its stdin and better error reporting.
  • Loading branch information
belimawr committed Aug 20, 2024
1 parent 56af175 commit 93230ef
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions filebeat/tests/integration/journald_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,31 @@ func generateJournaldLogs(t *testing.T, ctx context.Context, syslogID string, ma
t.Fatalf("cannot start 'systemd-cat': %s", err)
}
defer func() {
// Make sure systemd-cat terminates successfully so the messages
// are correctly written to the journal
if err := cmd.Wait(); err != nil {
t.Errorf("error waiting for system-cat to finish: %s", err)
}

fmt.Println("Success?", cmd.ProcessState.Success(), "Exit code:", cmd.ProcessState.ExitCode())
if !cmd.ProcessState.Success() {
t.Errorf("systemd-cat exited with %d", cmd.ProcessState.ExitCode())
}
}()

for count := 1; count <= max; count++ {
i, err := fmt.Fprintf(w, "Count: %03d\n", count)
fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", i, "bytes written", err)
written, err := fmt.Fprintf(w, "Count: %03d\n", count)
if err != nil {
t.Errorf("could not write message to journald: %s", err)
}
if written != 11 {
t.Errorf("could not write the whole message, expecing to write 11 bytes, but wrote %d", written)
}
time.Sleep(time.Millisecond)
}

fmt.Println("closing stdin:", w.Close())
if err := w.Close(); err != nil {
t.Errorf("could not close stdin from systemd-cat, messages are likely not written to the journal: %s", err)
}
}

//go:embed testdata/filebeat_journald.yml
Expand Down

0 comments on commit 93230ef

Please sign in to comment.