add SetMeta and SetParsed helpers (#2845)

* add SetMeta and SetParsed helpers
This commit is contained in:
Thibault "bui" Koechlin 2024-02-14 13:38:40 +01:00 committed by GitHub
parent 97c441dab6
commit 717fc97ca0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 98 additions and 0 deletions

View file

@ -46,6 +46,22 @@ type Event struct {
Meta map[string]string `yaml:"Meta,omitempty" json:"Meta,omitempty"`
}
func (e *Event) SetMeta(key string, value string) bool {
if e.Meta == nil {
e.Meta = make(map[string]string)
}
e.Meta[key] = value
return true
}
func (e *Event) SetParsed(key string, value string) bool {
if e.Parsed == nil {
e.Parsed = make(map[string]string)
}
e.Parsed[key] = value
return true
}
func (e *Event) GetType() string {
if e.Type == OVFLW {
return "overflow"

View file

@ -9,6 +9,88 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/models"
)
func TestSetParsed(t *testing.T) {
tests := []struct {
name string
evt *Event
key string
value string
expected bool
}{
{
name: "SetParsed: Valid",
evt: &Event{},
key: "test",
value: "test",
expected: true,
},
{
name: "SetParsed: Existing map",
evt: &Event{Parsed: map[string]string{}},
key: "test",
value: "test",
expected: true,
},
{
name: "SetParsed: Existing map+key",
evt: &Event{Parsed: map[string]string{"test": "foobar"}},
key: "test",
value: "test",
expected: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
tt.evt.SetParsed(tt.key, tt.value)
assert.Equal(t, tt.value, tt.evt.Parsed[tt.key])
})
}
}
func TestSetMeta(t *testing.T) {
tests := []struct {
name string
evt *Event
key string
value string
expected bool
}{
{
name: "SetMeta: Valid",
evt: &Event{},
key: "test",
value: "test",
expected: true,
},
{
name: "SetMeta: Existing map",
evt: &Event{Meta: map[string]string{}},
key: "test",
value: "test",
expected: true,
},
{
name: "SetMeta: Existing map+key",
evt: &Event{Meta: map[string]string{"test": "foobar"}},
key: "test",
value: "test",
expected: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
tt.evt.SetMeta(tt.key, tt.value)
assert.Equal(t, tt.value, tt.evt.GetMeta(tt.key))
})
}
}
func TestParseIPSources(t *testing.T) {
tests := []struct {
name string