ParseUnix() test fix: force UTC (#1970)
This commit is contained in:
parent
2d81e751a1
commit
033082a31e
|
@ -292,12 +292,20 @@ func LookupHost(value string) []string {
|
||||||
return addresses
|
return addresses
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseUnix(value string) string {
|
func ParseUnixTime(value string) (time.Time, error) {
|
||||||
//Splitting string here as some unix timestamp may have milliseconds and break ParseInt
|
//Splitting string here as some unix timestamp may have milliseconds and break ParseInt
|
||||||
i, err := strconv.ParseInt(strings.Split(value, ".")[0], 10, 64)
|
i, err := strconv.ParseInt(strings.Split(value, ".")[0], 10, 64)
|
||||||
if err != nil || i <= 0 {
|
if err != nil || i <= 0 {
|
||||||
log.Errorf("Unable to parse %s as unix timestamp.", value)
|
return time.Time{}, fmt.Errorf("unable to parse %s as unix timestamp", value)
|
||||||
|
}
|
||||||
|
return time.Unix(i, 0), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseUnix(value string) string {
|
||||||
|
t, err := ParseUnixTime(value)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return time.Unix(i, 0).Format(time.RFC3339)
|
return t.Format(time.RFC3339)
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/database"
|
"github.com/crowdsecurity/crowdsec/pkg/database"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
|
"github.com/crowdsecurity/crowdsec/pkg/cstest"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -972,62 +973,46 @@ func TestGetDecisionsSinceCount(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseUnix(t *testing.T) {
|
func TestParseUnixTime(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
env map[string]interface{}
|
value string
|
||||||
code string
|
expected time.Time
|
||||||
result string
|
expectedErr string
|
||||||
err string
|
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "ParseUnix() test: valid value with milli",
|
name: "ParseUnix() test: valid value with milli",
|
||||||
env: map[string]interface{}{
|
value: "1672239773.3590894",
|
||||||
"unix": "1672239773.3590894",
|
expected: time.Date(2022, 12, 28, 15, 02, 53, 0, time.UTC),
|
||||||
"ParseUnix": ParseUnix,
|
|
||||||
},
|
|
||||||
code: "ParseUnix(unix)",
|
|
||||||
result: "2022-12-28T15:02:53Z",
|
|
||||||
err: "",
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "ParseUnix() test: valid value without milli",
|
name: "ParseUnix() test: valid value without milli",
|
||||||
env: map[string]interface{}{
|
value: "1672239773",
|
||||||
"unix": "1672239773",
|
expected: time.Date(2022, 12, 28, 15, 02, 53, 0, time.UTC),
|
||||||
"ParseUnix": ParseUnix,
|
|
||||||
},
|
|
||||||
code: "ParseUnix(unix)",
|
|
||||||
result: "2022-12-28T15:02:53Z",
|
|
||||||
err: "",
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "ParseUnix() test: invalid input",
|
name: "ParseUnix() test: invalid input",
|
||||||
env: map[string]interface{}{
|
value: "AbcDefG!#",
|
||||||
"unix": "AbcDefG!#",
|
expected: time.Time{},
|
||||||
"ParseUnix": ParseUnix,
|
expectedErr: "unable to parse AbcDefG!# as unix timestamp",
|
||||||
},
|
|
||||||
code: "ParseUnix(unix)",
|
|
||||||
result: "",
|
|
||||||
err: "",
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "ParseUnix() test: negative value",
|
name: "ParseUnix() test: negative value",
|
||||||
env: map[string]interface{}{
|
value: "-1000",
|
||||||
"unix": "-1000",
|
expected: time.Time{},
|
||||||
"ParseUnix": ParseUnix,
|
expectedErr: "unable to parse -1000 as unix timestamp",
|
||||||
},
|
|
||||||
code: "ParseUnix(unix)",
|
|
||||||
result: "",
|
|
||||||
err: "",
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, tc := range tests {
|
||||||
program, err := expr.Compile(test.code, expr.Env(test.env))
|
tc := tc
|
||||||
require.NoError(t, err)
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
output, err := expr.Run(program, test.env)
|
output, err := ParseUnixTime(tc.value)
|
||||||
require.NoError(t, err)
|
cstest.RequireErrorContains(t, err, tc.expectedErr)
|
||||||
require.Equal(t, test.result, output)
|
if tc.expectedErr != "" {
|
||||||
log.Printf("test '%s' : OK", test.name)
|
return
|
||||||
|
}
|
||||||
|
require.WithinDuration(t, tc.expected, output, time.Second)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue