Skip to content

Commit

Permalink
Add tests for redis key selection
Browse files Browse the repository at this point in the history
  • Loading branch information
urso committed Jun 9, 2020
1 parent b93f27c commit 665e207
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
18 changes: 11 additions & 7 deletions libbeat/outputs/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,7 @@ func makeRedis(
return outputs.Fail(errors.New("Bad Redis data type"))
}

key, err := outil.BuildSelectorFromConfig(cfg, outil.Settings{
Key: "key",
MultiKey: "keys",
EnableSingleOnly: true,
FailEmpty: true,
Case: outil.SelectorKeepCase,
})
key, err := buildKeySelector(cfg)
if err != nil {
return outputs.Fail(err)
}
Expand Down Expand Up @@ -175,3 +169,13 @@ func makeRedis(

return outputs.SuccessNet(config.LoadBalance, config.BulkMaxSize, config.MaxRetries, clients)
}

func buildKeySelector(cfg *common.Config) (outil.Selector, error) {
return outil.BuildSelectorFromConfig(cfg, outil.Settings{
Key: "key",
MultiKey: "keys",
EnableSingleOnly: true,
FailEmpty: true,
Case: outil.SelectorKeepCase,
})
}
61 changes: 61 additions & 0 deletions libbeat/outputs/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,64 @@ func TestMakeRedis(t *testing.T) {
})
}
}

func TestKeySelection(t *testing.T) {
cases := map[string]struct {
cfg map[string]interface{}
event beat.Event
want string
}{
"key configured": {
cfg: map[string]interface{}{"key": "test"},
want: "test",
},
"key must keep case": {
cfg: map[string]interface{}{"key": "Test"},
want: "Test",
},
"key setting": {
cfg: map[string]interface{}{
"keys": []map[string]interface{}{{"key": "test"}},
},
want: "test",
},
"keys setting must keep case": {
cfg: map[string]interface{}{
"keys": []map[string]interface{}{{"key": "Test"}},
},
want: "Test",
},
"use event field": {
cfg: map[string]interface{}{"key": "test-%{[field]}"},
event: beat.Event{
Fields: common.MapStr{"field": "from-event"},
},
want: "test-from-event",
},
"use event field must keep case": {
cfg: map[string]interface{}{"key": "Test-%{[field]}"},
event: beat.Event{
Fields: common.MapStr{"field": "From-Event"},
},
want: "Test-From-Event",
},
}

for name, test := range cases {
t.Run(name, func(t *testing.T) {
selector, err := buildKeySelector(common.MustNewConfigFrom(test.cfg))
if err != nil {
t.Fatalf("Failed to parse configuration: %v", err)
}

got, err := selector.Select(&test.event)
if err != nil {
t.Fatalf("Failed to create key name: %v", err)
}

if test.want != got {
t.Errorf("Pipeline name missmatch (want: %v, got: %v)", test.want, got)
}
})
}
}

0 comments on commit 665e207

Please sign in to comment.