Skip to content

Commit

Permalink
test(filebeat): compress journal test files (#42142)
Browse files Browse the repository at this point in the history
* test(filebeat): compress journal test files

save around ~32MB from the module zip, relevant for program using beats as
a dependency.
Compress journal files with gzip and extract them to test tempdir

* lint: fix linter issues
  • Loading branch information
kruskall authored Dec 24, 2024
1 parent 9b92245 commit ef4c385
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 13 deletions.
17 changes: 11 additions & 6 deletions filebeat/input/journald/input_filtering_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ package journald
import (
"context"
"encoding/json"
"path"
"path/filepath"
"testing"
"time"

"github.com/elastic/elastic-agent-libs/mapstr"
)

func TestInputSyslogIdentifier(t *testing.T) {
out := decompress(t, filepath.Join("testdata", "input-multiline-parser.journal.gz"))

tests := map[string]struct {
identifiers []string
expectedMessages []string
Expand All @@ -53,7 +55,7 @@ func TestInputSyslogIdentifier(t *testing.T) {
t.Run(name, func(t *testing.T) {
env := newInputTestingEnvironment(t)
inp := env.mustCreateInput(mapstr.M{
"paths": []string{path.Join("testdata", "input-multiline-parser.journal")},
"paths": []string{out},
"syslog_identifiers": testCase.identifiers,
})

Expand All @@ -73,6 +75,7 @@ func TestInputSyslogIdentifier(t *testing.T) {
}

func TestInputUnits(t *testing.T) {
out := decompress(t, filepath.Join("testdata", "input-multiline-parser.journal.gz"))
tests := map[string]struct {
units []string
kernel bool
Expand Down Expand Up @@ -110,7 +113,7 @@ func TestInputUnits(t *testing.T) {
t.Run(name, func(t *testing.T) {
env := newInputTestingEnvironment(t)
inp := env.mustCreateInput(mapstr.M{
"paths": []string{path.Join("testdata", "input-multiline-parser.journal")},
"paths": []string{out},
"units": testCase.units,
"kernel": testCase.kernel,
})
Expand All @@ -131,6 +134,7 @@ func TestInputUnits(t *testing.T) {
}

func TestInputIncludeMatches(t *testing.T) {
out := decompress(t, filepath.Join("testdata", "input-multiline-parser.journal.gz"))
tests := map[string]struct {
includeMatches map[string]interface{}
expectedMessages []string
Expand Down Expand Up @@ -168,7 +172,7 @@ func TestInputIncludeMatches(t *testing.T) {
t.Run(name, func(t *testing.T) {
env := newInputTestingEnvironment(t)
inp := env.mustCreateInput(mapstr.M{
"paths": []string{path.Join("testdata", "input-multiline-parser.journal")},
"paths": []string{out},
"include_matches": testCase.includeMatches,
})

Expand All @@ -190,6 +194,7 @@ func TestInputIncludeMatches(t *testing.T) {
// TestInputSeek test the output of various seek modes while reading
// from input-multiline-parser.journal.
func TestInputSeek(t *testing.T) {
out := decompress(t, filepath.Join("testdata", "input-multiline-parser.journal.gz"))
// Uncomment the following line to see all logs during the test execution
// logp.DevelopmentSetup()
timeAfterFirstEvent := time.Date(2021, time.November, 22, 17, 10, 20, 0, time.UTC).In(time.Local)
Expand Down Expand Up @@ -256,13 +261,13 @@ func TestInputSeek(t *testing.T) {
if err := json.Unmarshal([]byte(testCase.cursor), &tmp); err != nil {
t.Fatal(err)
}
if err := store.Set("journald::testdata/input-multiline-parser.journal", tmp); err != nil {
if err := store.Set("journald::"+out, tmp); err != nil {
t.Fatal(err)
}
}

conf := mapstr.M{
"paths": []string{path.Join("testdata", "input-multiline-parser.journal")},
"paths": []string{out},
}

conf.DeepUpdate(testCase.config)
Expand Down
6 changes: 4 additions & 2 deletions filebeat/input/journald/input_parsers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package journald

import (
"context"
"path"
"path/filepath"
"testing"

"github.com/elastic/elastic-agent-libs/mapstr"
Expand All @@ -31,9 +31,11 @@ import (
// it only tests a single parser, but that is enough to ensure
// we're correctly using the parsers
func TestInputParsers(t *testing.T) {
out := decompress(t, filepath.Join("testdata", "ndjson-parser.journal.gz"))

env := newInputTestingEnvironment(t)
inp := env.mustCreateInput(mapstr.M{
"paths": []string{path.Join("testdata", "ndjson-parser.journal")},
"paths": []string{out},
"parsers": []mapstr.M{
{
"ndjson": mapstr.M{
Expand Down
41 changes: 36 additions & 5 deletions filebeat/input/journald/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
package journald

import (
"compress/gzip"
"context"
_ "embed"
"encoding/json"
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -40,9 +42,11 @@ import (
)

func TestInputCanReadAllBoots(t *testing.T) {
out := decompress(t, filepath.Join("testdata", "multiple-boots.journal.gz"))

env := newInputTestingEnvironment(t)
cfg := mapstr.M{
"paths": []string{path.Join("testdata", "multiple-boots.journal")},
"paths": []string{out},
}
inp := env.mustCreateInput(cfg)

Expand All @@ -54,6 +58,7 @@ func TestInputCanReadAllBoots(t *testing.T) {
}

func TestInputFieldsTranslation(t *testing.T) {
out := decompress(t, filepath.Join("testdata", "input-multiline-parser.journal.gz"))
// A few random keys to verify
keysToCheck := map[string]string{
"systemd.user_unit": "log-service.service",
Expand All @@ -74,7 +79,7 @@ func TestInputFieldsTranslation(t *testing.T) {
env := newInputTestingEnvironment(t)

inp := env.mustCreateInput(mapstr.M{
"paths": []string{path.Join("testdata", "input-multiline-parser.journal")},
"paths": []string{out},
"include_matches.match": []string{"_SYSTEMD_USER_UNIT=log-service.service"},
"save_remote_hostname": tc.saveRemoteHostname,
})
Expand Down Expand Up @@ -118,9 +123,10 @@ func TestInputFieldsTranslation(t *testing.T) {
// __CURSOR - it is added to the registry and there are other tests for it
// __MONOTONIC_TIMESTAMP - it is part of the cursor
func TestCompareGoSystemdWithJournalctl(t *testing.T) {
out := decompress(t, filepath.Join("testdata", "input-multiline-parser.journal.gz"))
env := newInputTestingEnvironment(t)
inp := env.mustCreateInput(mapstr.M{
"paths": []string{path.Join("testdata", "input-multiline-parser.journal")},
"paths": []string{out},
"seek": "head",
})

Expand Down Expand Up @@ -185,6 +191,7 @@ func TestCompareGoSystemdWithJournalctl(t *testing.T) {
}

func TestMatchers(t *testing.T) {
out := decompress(t, filepath.Join("testdata", "matchers.journal.gz"))
// If this test fails, uncomment the following line to see the debug logs
// logp.DevelopmentSetup()
testCases := []struct {
Expand Down Expand Up @@ -274,7 +281,7 @@ func TestMatchers(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
env := newInputTestingEnvironment(t)
cfg := mapstr.M{
"paths": []string{path.Join("testdata", "matchers.journal")},
"paths": []string{out},
"include_matches": tc.matchers,
}
cfg.Update(mapstr.M(tc.confiFields))
Expand Down Expand Up @@ -322,3 +329,27 @@ func TestReaderAdapterCanHandleNonStringFields(t *testing.T) {
})
}
}

func decompress(t *testing.T, namegz string) string {
t.Helper()

ingz, err := os.Open(namegz)
require.NoError(t, err)
defer ingz.Close()

out := filepath.Join(t.TempDir(), strings.TrimSuffix(filepath.Base(namegz), ".gz"))

dst, err := os.Create(out)
require.NoError(t, err)
defer dst.Close()

gr, err := gzip.NewReader(ingz)
require.NoError(t, err)
defer gr.Close()

//nolint:gosec // this is used in tests
_, err = io.Copy(dst, gr)
require.NoError(t, err)

return out
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit ef4c385

Please sign in to comment.