diff --git a/pkg/engine/pattern.go b/pkg/engine/pattern.go index 56929a692d..c7d20ba650 100644 --- a/pkg/engine/pattern.go +++ b/pkg/engine/pattern.go @@ -240,6 +240,10 @@ func validateNumber(value, pattern interface{}, operator Operator) bool { } func getOperatorFromStringPattern(pattern string) Operator { + if len(pattern) < 2 { + return Equal + } + if pattern[:len(MoreEqual)] == string(MoreEqual) { return MoreEqual } @@ -260,7 +264,7 @@ func getOperatorFromStringPattern(pattern string) Operator { return NotEqual } - return "" + return Equal } func getNumberAndStringPartsFromPattern(pattern string) (number, str string) { diff --git a/pkg/engine/pattern_test.go b/pkg/engine/pattern_test.go index b61c0d2a4e..2ce21ddde2 100644 --- a/pkg/engine/pattern_test.go +++ b/pkg/engine/pattern_test.go @@ -269,3 +269,15 @@ func TestValidateNumberWithStr_MoreAndWildCard(t *testing.T) { func TestValidateNumberWithStr_NoStr(t *testing.T) { assert.Assert(t, validateNumberWithStr(2048, "1024", "", More)) } + +func TestGetOperatorFromStringPattern_OneChar(t *testing.T) { + assert.Equal(t, getOperatorFromStringPattern("f"), Equal) +} + +func TestGetOperatorFromStringPattern_EmptyString(t *testing.T) { + assert.Equal(t, getOperatorFromStringPattern(""), Equal) +} + +func TestGetOperatorFromStringPattern_OnlyOperator(t *testing.T) { + assert.Equal(t, getOperatorFromStringPattern(">="), MoreEqual) +}