Skip to content

Commit

Permalink
add SetMeta and SetParsed helpers (#2845)
Browse files Browse the repository at this point in the history
* add SetMeta and SetParsed helpers
  • Loading branch information
buixor authored Feb 14, 2024
1 parent 97c441d commit 717fc97
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pkg/types/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
82 changes: 82 additions & 0 deletions pkg/types/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 717fc97

Please sign in to comment.