From d14cfb188137aa76553f66ff3a6d809270db5bd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9mi=20V=C3=A1nyi?= Date: Thu, 15 Jul 2021 15:46:27 +0200 Subject: [PATCH] add tests for parser suffix --- libbeat/reader/parser/parser_test.go | 69 ++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/libbeat/reader/parser/parser_test.go b/libbeat/reader/parser/parser_test.go index 37eba5d15f9..1fdf09ef719 100644 --- a/libbeat/reader/parser/parser_test.go +++ b/libbeat/reader/parser/parser_test.go @@ -32,6 +32,75 @@ import ( "github.com/elastic/beats/v7/libbeat/reader/readfile/encoding" ) +func TestParsersConfigSuffix(t *testing.T) { + tests := map[string]struct { + parsers map[string]interface{} + expectedSuffix string + expectedError string + }{ + "parsers with no suffix config": { + parsers: map[string]interface{}{ + "parsers": []map[string]interface{}{ + map[string]interface{}{ + "container": map[string]interface{}{ + "stream": "all", + }, + }, + }, + }, + }, + "parsers with correct suffix config": { + parsers: map[string]interface{}{ + "parsers": []map[string]interface{}{ + map[string]interface{}{ + "container": map[string]interface{}{ + "stream": "stdout", + }, + }, + }, + }, + expectedSuffix: "stdout", + }, + "parsers with multiple suffix config": { + parsers: map[string]interface{}{ + "parsers": []map[string]interface{}{ + map[string]interface{}{ + "container": map[string]interface{}{ + "stream": "stdout", + }, + }, + map[string]interface{}{ + "container": map[string]interface{}{ + "stream": "stderr", + }, + }, + }, + }, + expectedError: "only one stream selection is allowed", + }, + } + + for name, test := range tests { + test := test + t.Run(name, func(t *testing.T) { + cfg := common.MustNewConfigFrom(test.parsers) + var parsersConfig testParsersConfig + err := cfg.Unpack(&parsersConfig) + require.NoError(t, err) + c, err := NewConfig(CommonConfig{MaxBytes: 1024, LineTerminator: readfile.AutoLineTerminator}, parsersConfig.Parsers) + + if test.expectedError == "" { + require.NoError(t, err) + } else { + require.Contains(t, err.Error(), test.expectedError) + return + } + require.Equal(t, c.Suffix, test.expectedSuffix) + }) + } + +} + func TestParsersConfigAndReading(t *testing.T) { tests := map[string]struct { lines string