diff --git a/pkg/apis/nfd/v1alpha1/expression.go b/pkg/apis/nfd/v1alpha1/expression.go index 1fd6c2b92..9a0507498 100644 --- a/pkg/apis/nfd/v1alpha1/expression.go +++ b/pkg/apis/nfd/v1alpha1/expression.go @@ -41,55 +41,6 @@ var matchOps = map[MatchOp]struct{}{ MatchIsFalse: {}, } -// Validate validates the expression. -func (m *MatchExpression) Validate() error { - if _, ok := matchOps[m.Op]; !ok { - return fmt.Errorf("invalid Op %q", m.Op) - } - switch m.Op { - case MatchExists, MatchDoesNotExist, MatchIsTrue, MatchIsFalse, MatchAny: - if len(m.Value) != 0 { - return fmt.Errorf("value must be empty for Op %q (have %v)", m.Op, m.Value) - } - case MatchGt, MatchLt: - if len(m.Value) != 1 { - return fmt.Errorf("value must contain exactly one element for Op %q (have %v)", m.Op, m.Value) - } - if _, err := strconv.Atoi(m.Value[0]); err != nil { - return fmt.Errorf("value must be an integer for Op %q (have %v)", m.Op, m.Value[0]) - } - case MatchGtLt: - if len(m.Value) != 2 { - return fmt.Errorf("value must contain exactly two elements for Op %q (have %v)", m.Op, m.Value) - } - var err error - v := make([]int, 2) - for i := 0; i < 2; i++ { - if v[i], err = strconv.Atoi(m.Value[i]); err != nil { - return fmt.Errorf("value must contain integers for Op %q (have %v)", m.Op, m.Value) - } - } - if v[0] >= v[1] { - return fmt.Errorf("value[0] must be less than Value[1] for Op %q (have %v)", m.Op, m.Value) - } - case MatchInRegexp: - if len(m.Value) == 0 { - return fmt.Errorf("value must be non-empty for Op %q", m.Op) - } - for _, v := range m.Value { - _, err := regexp.Compile(v) - if err != nil { - return fmt.Errorf("value must only contain valid regexps for Op %q (have %v)", m.Op, m.Value) - } - } - default: - if len(m.Value) == 0 { - return fmt.Errorf("value must be non-empty for Op %q", m.Op) - } - } - return nil -} - // Match evaluates the MatchExpression against a single input value. func (m *MatchExpression) Match(valid bool, value interface{}) (bool, error) { if _, ok := matchOps[m.Op]; !ok { diff --git a/pkg/apis/nfd/v1alpha1/expression_test.go b/pkg/apis/nfd/v1alpha1/expression_test.go index e255387e8..150f45415 100644 --- a/pkg/apis/nfd/v1alpha1/expression_test.go +++ b/pkg/apis/nfd/v1alpha1/expression_test.go @@ -29,73 +29,6 @@ type BoolAssertionFunc func(assert.TestingT, bool, ...interface{}) bool type ValueAssertionFunc func(assert.TestingT, interface{}, ...interface{}) bool -func TestMatchExpressionValidate(t *testing.T) { - type V = api.MatchValue - type TC struct { - name string - op api.MatchOp - values V - err ValueAssertionFunc - } - - tcs := []TC{ - {name: "1", op: api.MatchAny, err: assert.Nil}, // #0 - {name: "2", op: api.MatchAny, values: V{"1"}, err: assert.NotNil}, - - {name: "3", op: api.MatchIn, err: assert.NotNil}, - {name: "4", op: api.MatchIn, values: V{"1"}, err: assert.Nil}, - {name: "5", op: api.MatchIn, values: V{"1", "2", "3", "4"}, err: assert.Nil}, - - {name: "6", op: api.MatchNotIn, err: assert.NotNil}, - {name: "7", op: api.MatchNotIn, values: V{"1"}, err: assert.Nil}, - {name: "8", op: api.MatchNotIn, values: V{"1", "2"}, err: assert.Nil}, - - {name: "9", op: api.MatchInRegexp, err: assert.NotNil}, - {name: "10", op: api.MatchInRegexp, values: V{"1"}, err: assert.Nil}, - {name: "11", op: api.MatchInRegexp, values: V{"()", "2", "3"}, err: assert.Nil}, - {name: "12", op: api.MatchInRegexp, values: V{"("}, err: assert.NotNil}, - - {name: "13", op: api.MatchExists, err: assert.Nil}, - {name: "14", op: api.MatchExists, values: V{"1"}, err: assert.NotNil}, - - {name: "15", op: api.MatchDoesNotExist, err: assert.Nil}, - {name: "16", op: api.MatchDoesNotExist, values: V{"1"}, err: assert.NotNil}, - - {name: "17", op: api.MatchGt, err: assert.NotNil}, - {name: "18", op: api.MatchGt, values: V{"1"}, err: assert.Nil}, - {name: "19", op: api.MatchGt, values: V{"-10"}, err: assert.Nil}, - {name: "20", op: api.MatchGt, values: V{"1", "2"}, err: assert.NotNil}, - {name: "21", op: api.MatchGt, values: V{""}, err: assert.NotNil}, - - {name: "22", op: api.MatchLt, err: assert.NotNil}, - {name: "23", op: api.MatchLt, values: V{"1"}, err: assert.Nil}, - {name: "24", op: api.MatchLt, values: V{"-1"}, err: assert.Nil}, - {name: "25", op: api.MatchLt, values: V{"1", "2", "3"}, err: assert.NotNil}, - {name: "26", op: api.MatchLt, values: V{"a"}, err: assert.NotNil}, - - {name: "27", op: api.MatchGtLt, err: assert.NotNil}, - {name: "28", op: api.MatchGtLt, values: V{"1"}, err: assert.NotNil}, - {name: "29", op: api.MatchGtLt, values: V{"1", "2"}, err: assert.Nil}, - {name: "30", op: api.MatchGtLt, values: V{"2", "1"}, err: assert.NotNil}, - {name: "31", op: api.MatchGtLt, values: V{"1", "2", "3"}, err: assert.NotNil}, - {name: "32", op: api.MatchGtLt, values: V{"a", "2"}, err: assert.NotNil}, - - {name: "33", op: api.MatchIsTrue, err: assert.Nil}, - {name: "34", op: api.MatchIsTrue, values: V{"1"}, err: assert.NotNil}, - - {name: "35", op: api.MatchIsFalse, err: assert.Nil}, - {name: "36", op: api.MatchIsFalse, values: V{"1", "2"}, err: assert.NotNil}, - } - - for _, tc := range tcs { - t.Run(tc.name, func(t *testing.T) { - me := api.MatchExpression{Op: tc.op, Value: tc.values} - err := me.Validate() - tc.err(t, err) - }) - } -} - func TestMatch(t *testing.T) { type V = api.MatchValue type TC struct {