Skip to content

Commit

Permalink
Add tests for reading from all boots
Browse files Browse the repository at this point in the history
  • Loading branch information
belimawr committed Oct 18, 2024
1 parent f89c52b commit 0018ca3
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 52 deletions.
64 changes: 12 additions & 52 deletions filebeat/input/journald/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,59 +39,19 @@ import (
"github.com/elastic/elastic-agent-libs/mapstr"
)

// How to write to journal from CLI:
// https://www.baeldung.com/linux/systemd-journal-message-terminal
func TestInputCanReadAllBoots(t *testing.T) {
env := newInputTestingEnvironment(t)
cfg := mapstr.M{
"paths": []string{path.Join("testdata", "multiple-boots.journal")},
}
inp := env.mustCreateInput(cfg)

// TestGenerateJournalEntries generates entries in the user's journal.
// It is kept commented out at the top of the file as reference and
// easy access.
//
// How to generate a journal file with only the entries you want:
// 1. Add the dependencies for this test
// go get github.com/ssgreg/journald
// 2. Uncomment and run the test:
// 3. Add the following import:
// journaldlogger "github.com/ssgreg/journald"
// 4. Get a VM, ssh into it, make sure you can access the test from it
// 5. Find the journal file, usually at /var/log/journal/<machine ID>/user-1000.journal
// 7. Clean and rotate the journal
// sudo journalctl --vacuum-time=1s
// sudo journalctl --rotate
// 8. Run this test: `go test -run=TestGenerateJournalEntries`
// 9. Copy the journal file somewhere else
// cp /var/log/journal/21282bcb80a74c08a0d14a047372256c/user-1000.journal /tmp/foo.journal
// 10. Read the journal file:
// journalctl --file=/tmp/foo.journal -n 10
// 11. Read the journal with all fields as JSON
// journalctl --file=/tmp/foo.journal -n 10 -o json
// func TestGenerateJournalEntries(t *testing.T) {
// fields := []map[string]any{
// {
// "BAR": "bar",
// },
// {
// "FOO": "foo",
// },
// {
// "BAR": "bar",
// "FOO": "foo",
// },
// {
// "FOO_BAR": "foo",
// },
// {
// "FOO_BAR": "bar",
// },
// {
// "FOO_BAR": "foo bar",
// },
// }
// for i, m := range fields {
// if err := journaldlogger.Send(fmt.Sprintf("message %d", i), journaldlogger.PriorityInfo, m); err != nil {
// t.Fatal(err)
// }
// }
// }
ctx, cancelInput := context.WithCancel(context.Background())
t.Cleanup(cancelInput)

env.startInput(ctx, inp)
env.waitUntilEventCount(6)
}

func TestInputFieldsTranslation(t *testing.T) {
// A few random keys to verify
Expand Down
86 changes: 86 additions & 0 deletions filebeat/input/journald/testdata/multiple-boots.export
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
__CURSOR=s=8c7196499b954413a742eb1e2107fa5d;i=1;b=0ffe5f74a4bd49ca8597eb05fe1a512a;m=39f445;t=6225212a5b6da;x=3f056d2626450d83
__REALTIME_TIMESTAMP=1726585755776730
__MONOTONIC_TIMESTAMP=3798085
_BOOT_ID=0ffe5f74a4bd49ca8597eb05fe1a512a
_SOURCE_MONOTONIC_TIMESTAMP=0
_TRANSPORT=kernel
PRIORITY=5
SYSLOG_FACILITY=0
SYSLOG_IDENTIFIER=kernel
MESSAGE=Linux version 6.1.0-25-amd64 ([email protected]) (gcc-12 (Debian 12.2.0-14) 12.2.0, GNU ld (GNU Binutils for Debian) 2.40) #1 SMP PREEMPT_DYNAMIC Debian 6.1.106-3 (2024-08-26)
_MACHINE_ID=ad88a1859979427ea1a7c24f0ae0320a
_HOSTNAME=Debian12
_RUNTIME_SCOPE=system

__CURSOR=s=8c7196499b954413a742eb1e2107fa5d;i=2;b=0ffe5f74a4bd49ca8597eb05fe1a512a;m=39f452;t=6225212a5b6e7;x=67b36f81fa43ba68
__REALTIME_TIMESTAMP=1726585755776743
__MONOTONIC_TIMESTAMP=3798098
_BOOT_ID=0ffe5f74a4bd49ca8597eb05fe1a512a
_SOURCE_MONOTONIC_TIMESTAMP=0
_TRANSPORT=kernel
SYSLOG_FACILITY=0
SYSLOG_IDENTIFIER=kernel
_MACHINE_ID=ad88a1859979427ea1a7c24f0ae0320a
_HOSTNAME=Debian12
_RUNTIME_SCOPE=system
PRIORITY=6
MESSAGE=Command line: BOOT_IMAGE=/boot/vmlinuz-6.1.0-25-amd64 root=UUID=3841998b-4e88-4231-93c8-3fc24b549223 ro quiet

Sep 17 11:26:36 Debian12 kernel: Linux version 6.1.0-25-amd64 ([email protected]) (gcc-12 (Debian 12.2.0-14) 12.2.0, GNU ld (GNU Binutils for Debian) 2.40) #1 SMP PREEMPT_DYNAMIC Debian 6.1.106-3 (2024-08-26)
Sep 17 11:26:36 Debian12 kernel: Command line: BOOT_IMAGE=/boot/vmlinuz-6.1.0-25-amd64 root=UUID=3841998b-4e88-4231-93c8-3fc24b549223 ro quiet
__CURSOR=s=8c7196499b954413a742eb1e2107fa5d;i=22e3;b=457105b2d84547a4b4549f0eaa700b61;m=35bc29;t=6227ecec5b11f;x=a46eaad8c3930985
__REALTIME_TIMESTAMP=1726777890550047
__MONOTONIC_TIMESTAMP=3521577
_BOOT_ID=457105b2d84547a4b4549f0eaa700b61
_SOURCE_MONOTONIC_TIMESTAMP=0
_TRANSPORT=kernel
PRIORITY=5
SYSLOG_FACILITY=0
SYSLOG_IDENTIFIER=kernel
MESSAGE=Linux version 6.1.0-25-amd64 ([email protected]) (gcc-12 (Debian 12.2.0-14) 12.2.0, GNU ld (GNU Binutils for Debian) 2.40) #1 SMP PREEMPT_DYNAMIC Debian 6.1.106-3 (2024-08-26)
_MACHINE_ID=ad88a1859979427ea1a7c24f0ae0320a
_HOSTNAME=Debian12
_RUNTIME_SCOPE=system

__CURSOR=s=8c7196499b954413a742eb1e2107fa5d;i=22e4;b=457105b2d84547a4b4549f0eaa700b61;m=35bc37;t=6227ecec5b12d;x=fcd8a87f1f95be6e
__REALTIME_TIMESTAMP=1726777890550061
__MONOTONIC_TIMESTAMP=3521591
_BOOT_ID=457105b2d84547a4b4549f0eaa700b61
_SOURCE_MONOTONIC_TIMESTAMP=0
_TRANSPORT=kernel
SYSLOG_FACILITY=0
SYSLOG_IDENTIFIER=kernel
_MACHINE_ID=ad88a1859979427ea1a7c24f0ae0320a
_HOSTNAME=Debian12
_RUNTIME_SCOPE=system
PRIORITY=6
MESSAGE=Command line: BOOT_IMAGE=/boot/vmlinuz-6.1.0-25-amd64 root=UUID=3841998b-4e88-4231-93c8-3fc24b549223 ro quiet

__CURSOR=s=8c7196499b954413a742eb1e2107fa5d;i=451d;b=e2fca45429e54522bb2927112eb8e0b5;m=2aad67;t=6228fba6fbe98;x=ab82fca7956545cf
__REALTIME_TIMESTAMP=1726850563817112
__MONOTONIC_TIMESTAMP=2796903
_BOOT_ID=e2fca45429e54522bb2927112eb8e0b5
_SOURCE_MONOTONIC_TIMESTAMP=0
_TRANSPORT=kernel
PRIORITY=5
SYSLOG_FACILITY=0
SYSLOG_IDENTIFIER=kernel
MESSAGE=Linux version 6.1.0-25-amd64 ([email protected]) (gcc-12 (Debian 12.2.0-14) 12.2.0, GNU ld (GNU Binutils for Debian) 2.40) #1 SMP PREEMPT_DYNAMIC Debian 6.1.106-3 (2024-08-26)
_MACHINE_ID=ad88a1859979427ea1a7c24f0ae0320a
_HOSTNAME=Debian12
_RUNTIME_SCOPE=system

__CURSOR=s=8c7196499b954413a742eb1e2107fa5d;i=451e;b=e2fca45429e54522bb2927112eb8e0b5;m=2aad75;t=6228fba6fbea7;x=f334fe004963f224
__REALTIME_TIMESTAMP=1726850563817127
__MONOTONIC_TIMESTAMP=2796917
_BOOT_ID=e2fca45429e54522bb2927112eb8e0b5
_SOURCE_MONOTONIC_TIMESTAMP=0
_TRANSPORT=kernel
SYSLOG_FACILITY=0
SYSLOG_IDENTIFIER=kernel
_MACHINE_ID=ad88a1859979427ea1a7c24f0ae0320a
_HOSTNAME=Debian12
_RUNTIME_SCOPE=system
PRIORITY=6
MESSAGE=Command line: BOOT_IMAGE=/boot/vmlinuz-6.1.0-25-amd64 root=UUID=3841998b-4e88-4231-93c8-3fc24b549223 ro quiet

Binary file not shown.

0 comments on commit 0018ca3

Please sign in to comment.