From 1d32ef065ca80421865b645d2bcb5f3fd8b1e4c8 Mon Sep 17 00:00:00 2001 From: fearful-symmetry Date: Thu, 12 Oct 2023 11:16:37 -0700 Subject: [PATCH] improve nil handling, add tests --- libbeat/idxmgmt/lifecycle/es_client_handler.go | 8 +++++--- libbeat/idxmgmt/lifecycle/es_client_test.go | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/libbeat/idxmgmt/lifecycle/es_client_handler.go b/libbeat/idxmgmt/lifecycle/es_client_handler.go index c9077547a5b6..5bc6d70eac83 100644 --- a/libbeat/idxmgmt/lifecycle/es_client_handler.go +++ b/libbeat/idxmgmt/lifecycle/es_client_handler.go @@ -64,9 +64,11 @@ func NewESClientHandler(c ESClient, info beat.Info, cfg RawConfig) (*ESClientHan // unpack name value separately dsName := DefaultDSLName() - err := cfg.DSL.Unpack(&dsName) - if err != nil { - return nil, fmt.Errorf("error unpacking DSL data stream name: %w", err) + if cfg.DSL != nil { + err := cfg.DSL.Unpack(&dsName) + if err != nil { + return nil, fmt.Errorf("error unpacking DSL data stream name: %w", err) + } } lifecycleCfg.PolicyName = dsName.DataStreamPattern diff --git a/libbeat/idxmgmt/lifecycle/es_client_test.go b/libbeat/idxmgmt/lifecycle/es_client_test.go index aa6c55f274f9..400285db2d6d 100644 --- a/libbeat/idxmgmt/lifecycle/es_client_test.go +++ b/libbeat/idxmgmt/lifecycle/es_client_test.go @@ -81,6 +81,14 @@ func TestESSetup(t *testing.T) { ILM: config.MustNewConfigFrom(mapstr.M{"enabled": true, "policy_name": "test", "check_exists": true}), DSL: config.MustNewConfigFrom(mapstr.M{"enabled": true, "data_stream_pattern": "%{[beat.name]}-%{[beat.version]}", "check_exists": true}), } + withDSLBlank := RawConfig{ + ILM: config.MustNewConfigFrom(mapstr.M{"enabled": false, "policy_name": "test", "check_exists": true}), + DSL: nil, + } + withILMBlank := RawConfig{ + ILM: nil, + DSL: config.MustNewConfigFrom(mapstr.M{"enabled": false, "data_stream_pattern": "%{[beat.name]}-%{[beat.version]}", "check_exists": true}), + } cases := map[string]struct { serverless bool @@ -174,6 +182,16 @@ func TestESSetup(t *testing.T) { cfg: bothDisabledConfig, err: false, }, + "serverless-with-bare-config": { + serverless: true, + cfg: withDSLBlank, + err: false, + }, + "stateful-with-bare-config": { + serverless: false, + cfg: withILMBlank, + err: false, + }, } for name, test := range cases {