Skip to content

Commit

Permalink
improve nil handling, add tests (#36835)
Browse files Browse the repository at this point in the history
  • Loading branch information
fearful-symmetry authored Oct 12, 2023
1 parent 549aec1 commit 68bcbb0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
8 changes: 5 additions & 3 deletions libbeat/idxmgmt/lifecycle/es_client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions libbeat/idxmgmt/lifecycle/es_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 68bcbb0

Please sign in to comment.