From 54e62a85a167cd06c562d1b3ee264275e3f78d83 Mon Sep 17 00:00:00 2001 From: Ed Bartosh Date: Tue, 6 Aug 2024 18:57:39 +0300 Subject: [PATCH] move minimal version check to spces-go/ValidateVersion Signed-off-by: Ed Bartosh Co-authored-by: Evan Lezar --- pkg/cdi/spec.go | 11 +---------- pkg/cdi/spec_test.go | 2 +- specs-go/version.go | 18 ++++++++++++++---- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/pkg/cdi/spec.go b/pkg/cdi/spec.go index 6d72179..e19e782 100644 --- a/pkg/cdi/spec.go +++ b/pkg/cdi/spec.go @@ -209,18 +209,9 @@ func MinimumRequiredVersion(spec *cdi.Spec) (string, error) { // Validate the Spec. func (s *Spec) validate() (map[string]*Device, error) { - if err := cdi.ValidateVersion(s.Version); err != nil { + if err := cdi.ValidateVersion(s.Spec); err != nil { return nil, err } - - minVersion, err := cdi.MinimumRequiredVersion(s.Spec) - if err != nil { - return nil, fmt.Errorf("could not determine minimum required version: %v", err) - } - if cdi.NewVersion(minVersion).IsGreaterThan(cdi.NewVersion(s.Version)) { - return nil, fmt.Errorf("the spec version must be at least v%v", minVersion) - } - if err := ValidateVendorName(s.vendor); err != nil { return nil, err } diff --git a/pkg/cdi/spec_test.go b/pkg/cdi/spec_test.go index df7ff68..649d223 100644 --- a/pkg/cdi/spec_test.go +++ b/pkg/cdi/spec_test.go @@ -525,7 +525,7 @@ func specType(content []byte) string { } func TestCurrentVersionIsValid(t *testing.T) { - require.NoError(t, cdi.ValidateVersion(cdi.CurrentVersion)) + require.NoError(t, cdi.ValidateVersion(&cdi.Spec{Version: cdi.CurrentVersion})) } func TestRequiredVersion(t *testing.T) { diff --git a/specs-go/version.go b/specs-go/version.go index 05c72dd..9feebca 100644 --- a/specs-go/version.go +++ b/specs-go/version.go @@ -58,10 +58,20 @@ var validSpecVersions = requiredVersionMap{ v080: requiresV080, } -// ValidateVersion checks whether the specified spec version is supported. -func ValidateVersion(version string) error { - if !validSpecVersions.isValidVersion(version) { - return fmt.Errorf("invalid version %q", version) +// ValidateVersion checks whether the specified spec version is valid. +// In addition to checking whether the spec version is in the set of known versions, +// the spec is inspected to determine whether the features used are available in specified +// version. +func ValidateVersion(spec *Spec) error { + if !validSpecVersions.isValidVersion(spec.Version) { + return fmt.Errorf("invalid version %q", spec.Version) + } + minVersion, err := MinimumRequiredVersion(spec) + if err != nil { + return fmt.Errorf("could not determine minimum required version: %w", err) + } + if newVersion(minVersion).IsGreaterThan(newVersion(spec.Version)) { + return fmt.Errorf("the spec version must be at least v%v", minVersion) } return nil }