diff --git a/libbeat/outputs/fileout/config.go b/libbeat/outputs/fileout/config.go index 69af40e4289b..9228430d6d9b 100644 --- a/libbeat/outputs/fileout/config.go +++ b/libbeat/outputs/fileout/config.go @@ -38,6 +38,7 @@ type fileOutConfig struct { func defaultConfig() fileOutConfig { return fileOutConfig{ + Path: &PathFormatString{}, NumberOfFiles: 7, RotateEveryKb: 10 * 1024, Permissions: 0600, diff --git a/libbeat/outputs/fileout/config_test.go b/libbeat/outputs/fileout/config_test.go index 7e149173f6df..0bf0d6f7cc13 100644 --- a/libbeat/outputs/fileout/config_test.go +++ b/libbeat/outputs/fileout/config_test.go @@ -37,6 +37,7 @@ func TestConfig(t *testing.T) { config: config.MustNewConfigFrom([]byte(`{ }`)), assertion: func(t *testing.T, actual *fileOutConfig, err error) { expectedConfig := &fileOutConfig{ + Path: &PathFormatString{}, NumberOfFiles: 7, RotateEveryKb: 10 * 1024, Permissions: 0600, diff --git a/libbeat/outputs/fileout/pathformatstring.go b/libbeat/outputs/fileout/pathformatstring.go index acd2a7605fe6..fdaff2177872 100644 --- a/libbeat/outputs/fileout/pathformatstring.go +++ b/libbeat/outputs/fileout/pathformatstring.go @@ -18,6 +18,7 @@ package fileout import ( + "fmt" "os" "strings" "time" @@ -44,6 +45,9 @@ func (fs *PathFormatString) Run(timestamp time.Time) (string, error) { placeholderEvent := &beat.Event{ Timestamp: timestamp, } + if fs.efs == nil { + return "", fmt.Errorf("path format string is nil; check if `path` option is configured correctly") + } return fs.efs.Run(placeholderEvent) } diff --git a/libbeat/outputs/fileout/pathformatstring_test.go b/libbeat/outputs/fileout/pathformatstring_test.go index b8eee4e44eaa..fa3c9ee08fe0 100644 --- a/libbeat/outputs/fileout/pathformatstring_test.go +++ b/libbeat/outputs/fileout/pathformatstring_test.go @@ -22,8 +22,20 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/elastic/elastic-agent-libs/config" ) +func TestCheckNilDoesntPanic(t *testing.T) { + newCfg := config.NewConfig() + handler, err := readConfig(newCfg) + require.NoError(t, err) + _, err = handler.Path.Run(time.Now()) + require.Error(t, err) + +} + func TestPathFormatString(t *testing.T) { tests := []struct { title string