crowdsec/pkg/parser/enrich_date_test.go

60 lines
1.2 KiB
Go
Raw Permalink Normal View History

package parser
import (
"testing"
log "github.com/sirupsen/logrus"
2023-12-18 08:35:28 +00:00
"github.com/stretchr/testify/assert"
2023-12-18 08:35:28 +00:00
"github.com/crowdsecurity/go-cs-lib/cstest"
"github.com/crowdsecurity/crowdsec/pkg/types"
)
func TestDateParse(t *testing.T) {
tests := []struct {
2023-12-18 08:35:28 +00:00
name string
evt types.Event
expectedErr string
expected string
}{
{
name: "RFC3339",
evt: types.Event{
StrTime: "2019-10-12T07:20:50.52Z",
},
2023-12-18 08:35:28 +00:00
expected: "2019-10-12T07:20:50.52Z",
},
{
name: "02/Jan/2006:15:04:05 -0700",
evt: types.Event{
StrTime: "02/Jan/2006:15:04:05 -0700",
},
2023-12-18 08:35:28 +00:00
expected: "2006-01-02T15:04:05-07:00",
},
{
name: "Dec 17 08:17:43",
evt: types.Event{
StrTime: "2011 X 17 zz 08X17X43 oneone Dec",
StrTimeFormat: "2006 X 2 zz 15X04X05 oneone Jan",
},
2023-12-18 08:35:28 +00:00
expected: "2011-12-17T08:17:43Z",
},
}
logger := log.WithFields(log.Fields{
"test": "test",
})
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
strTime, err := ParseDate(tt.evt.StrTime, &tt.evt, nil, logger)
2023-12-18 08:35:28 +00:00
cstest.RequireErrorContains(t, err, tt.expectedErr)
if tt.expectedErr != "" {
return
}
2023-12-18 08:35:28 +00:00
assert.Equal(t, tt.expected, strTime["MarshaledTime"])
})
}
}