Skip to content

Commit

Permalink
move minimal version check to spces-go/ValidateVersion
Browse files Browse the repository at this point in the history
Signed-off-by: Ed Bartosh <[email protected]>
Co-authored-by: Evan Lezar <[email protected]>
  • Loading branch information
bart0sh and elezar committed Aug 8, 2024
1 parent 6abc5e0 commit 54e62a8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
11 changes: 1 addition & 10 deletions pkg/cdi/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cdi/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 14 additions & 4 deletions specs-go/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 54e62a8

Please sign in to comment.