Skip to content

Commit

Permalink
apis/nfd/validate: more comprehensive unit tests
Browse files Browse the repository at this point in the history
Also add license header to the test.go file and fix one bug in
MatchFeature validation.
  • Loading branch information
marquiz committed Apr 9, 2024
1 parent a9167e6 commit a9946b2
Show file tree
Hide file tree
Showing 2 changed files with 404 additions and 9 deletions.
16 changes: 12 additions & 4 deletions pkg/apis/nfd/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package validate

import (
"fmt"
"sort"
"strings"
"text/template"

Expand Down Expand Up @@ -57,7 +58,7 @@ func MatchFeatures(matchFeature nfdv1alpha1.FeatureMatcher) []error {
var validationErr []error

for _, match := range matchFeature {
nameSplit := strings.SplitN(match.Feature, ".", 2)
nameSplit := strings.Split(match.Feature, ".")
if len(nameSplit) != 2 {
validationErr = append(validationErr, fmt.Errorf("invalid feature name %v (not <domain>.<feature>), cannot be used for templating", match.Feature))
}
Expand All @@ -79,6 +80,13 @@ func Template(labelsTemplate string) []error {
return validationErr
}

func sortErrors(errs []error) []error {
sort.Slice(errs, func(i, j int) bool {
return errs[i].Error() < errs[j].Error()
})
return errs
}

// Labels validates a map of labels and returns a slice of errors if any of the
// labels are invalid.
func Labels(labels map[string]string) []error {
Expand All @@ -88,7 +96,7 @@ func Labels(labels map[string]string) []error {
errs = append(errs, fmt.Errorf("invalid label %q:%q %w", key, value, err))
}
}
return errs
return sortErrors(errs)
}

// Label validates a label key and value and returns an error if the key or
Expand Down Expand Up @@ -130,7 +138,7 @@ func Annotations(annotations map[string]string) []error {
errs = append(errs, fmt.Errorf("invalid annotation %q:%q %w", key, value, err))
}
}
return errs
return sortErrors(errs)
}

// Annotation validates an annotation key and value and returns an error if the
Expand Down Expand Up @@ -213,7 +221,7 @@ func ExtendedResources(extendedResources map[string]string) []error {
errs = append(errs, fmt.Errorf("invalid extended resource %q:%q %w", key, value, err))
}
}
return errs
return sortErrors(errs)
}

// ExtendedResource validates an extended resource key and value and returns an
Expand Down
Loading

0 comments on commit a9946b2

Please sign in to comment.