-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding test cases and incorporate review comments
- Loading branch information
Showing
25 changed files
with
826 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...iscover/builder/log_annotations/config.go → filebeat/autodiscover/builder/logs/config.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package log_annotations | ||
package logs | ||
|
||
import "github.com/elastic/beats/libbeat/common" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package logs | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/libbeat/common/bus" | ||
) | ||
|
||
func TestGenerateHints(t *testing.T) { | ||
tests := []struct { | ||
event bus.Event | ||
len int | ||
result common.MapStr | ||
}{ | ||
// Hints without host should return nothing | ||
{ | ||
event: bus.Event{ | ||
"hints": common.MapStr{ | ||
"metrics": common.MapStr{ | ||
"module": "prometheus", | ||
}, | ||
}, | ||
}, | ||
len: 0, | ||
result: common.MapStr{}, | ||
}, | ||
// Empty event hints should return default config | ||
{ | ||
event: bus.Event{ | ||
"host": "1.2.3.4", | ||
"kubernetes": common.MapStr{ | ||
"container": common.MapStr{ | ||
"name": "foobar", | ||
"id": "abc", | ||
}, | ||
}, | ||
"docker": common.MapStr{ | ||
"container": common.MapStr{ | ||
"name": "foobar", | ||
"id": "abc", | ||
}, | ||
}, | ||
}, | ||
len: 1, | ||
result: common.MapStr{ | ||
"type": "docker", | ||
"containers": map[string]interface{}{ | ||
"ids": []interface{}{"abc"}, | ||
}, | ||
}, | ||
}, | ||
// Hint with include|exclude_lines must be part of the input config | ||
{ | ||
event: bus.Event{ | ||
"host": "1.2.3.4", | ||
"kubernetes": common.MapStr{ | ||
"container": common.MapStr{ | ||
"name": "foobar", | ||
"id": "abc", | ||
}, | ||
}, | ||
"docker": common.MapStr{ | ||
"container": common.MapStr{ | ||
"name": "foobar", | ||
"id": "abc", | ||
}, | ||
}, | ||
"hints": common.MapStr{ | ||
"logs": common.MapStr{ | ||
"include_lines": "^test, ^test1", | ||
"exclude_lines": "^test2, ^test3", | ||
}, | ||
}, | ||
}, | ||
len: 1, | ||
result: common.MapStr{ | ||
"type": "docker", | ||
"containers": map[string]interface{}{ | ||
"ids": []interface{}{"abc"}, | ||
}, | ||
"include_lines": []interface{}{"^test", "^test1"}, | ||
"exclude_lines": []interface{}{"^test2", "^test3"}, | ||
}, | ||
}, | ||
// Hint with multiline config must have a multiline in the input config | ||
{ | ||
event: bus.Event{ | ||
"host": "1.2.3.4", | ||
"kubernetes": common.MapStr{ | ||
"container": common.MapStr{ | ||
"name": "foobar", | ||
"id": "abc", | ||
}, | ||
}, | ||
"docker": common.MapStr{ | ||
"container": common.MapStr{ | ||
"name": "foobar", | ||
"id": "abc", | ||
}, | ||
}, | ||
"hints": common.MapStr{ | ||
"logs": common.MapStr{ | ||
"multiline": common.MapStr{ | ||
"pattern": "^test", | ||
"negate": "true", | ||
}, | ||
}, | ||
}, | ||
}, | ||
len: 1, | ||
result: common.MapStr{ | ||
"type": "docker", | ||
"containers": map[string]interface{}{ | ||
"ids": []interface{}{"abc"}, | ||
}, | ||
"multiline": map[string]interface{}{ | ||
"pattern": "^test", | ||
"negate": "true", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
cfg := defaultConfig() | ||
l := logAnnotations{ | ||
Key: cfg.Key, | ||
Config: cfg.Config, | ||
} | ||
cfgs := l.CreateConfig(test.event) | ||
assert.Equal(t, len(cfgs), test.len) | ||
|
||
if test.len != 0 { | ||
config := common.MapStr{} | ||
err := cfgs[0].Unpack(&config) | ||
assert.Nil(t, err) | ||
|
||
assert.Equal(t, config, test.result) | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package builder | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/elastic/beats/libbeat/common" | ||
) | ||
|
||
func TestGenerateHints(t *testing.T) { | ||
tests := []struct { | ||
annotations map[string]string | ||
result common.MapStr | ||
}{ | ||
// Empty annotations should return empty hints | ||
{ | ||
annotations: map[string]string{}, | ||
result: common.MapStr{}, | ||
}, | ||
|
||
// Scenarios being tested: | ||
// logs/multiline.pattern must be a nested common.MapStr under hints.logs | ||
// metrics/module must be found in hints.metrics | ||
// not.to.include must not be part of hints | ||
// period is annotated at both container and pod level. Container level value must be in hints | ||
{ | ||
annotations: map[string]string{ | ||
"co.elastic.logs/multiline.pattern": "^test", | ||
"co.elastic.metrics/module": "prometheus", | ||
"co.elastic.metrics/period": "10s", | ||
"co.elastic.metrics.foobar/period": "15s", | ||
"not.to.include": "true", | ||
}, | ||
result: common.MapStr{ | ||
"logs": common.MapStr{ | ||
"multiline": common.MapStr{ | ||
"pattern": "^test", | ||
}, | ||
}, | ||
"metrics": common.MapStr{ | ||
"module": "prometheus", | ||
"period": "15s", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
assert.Equal(t, GenerateHints(test.annotations, "foobar", "co.elastic."), test.result) | ||
} | ||
} |
Oops, something went wrong.