Skip to content

Commit d96d442

Browse files
committed
Add validateVersion helper to spec validation
Signed-off-by: Evan Lezar <[email protected]>
1 parent 59e304a commit d96d442

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

pkg/cdi/spec.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ func (s *Spec) edits() *ContainerEdits {
131131

132132
// Validate the Spec.
133133
func (s *Spec) validate() (map[string]*Device, error) {
134-
if _, ok := validSpecVersions[s.Version]; !ok {
135-
return nil, errors.Errorf("invalid version %q", s.Version)
134+
if err := validateVersion(s.Version); err != nil {
135+
return nil, err
136136
}
137137
if err := ValidateVendorName(s.vendor); err != nil {
138138
return nil, err
@@ -159,6 +159,15 @@ func (s *Spec) validate() (map[string]*Device, error) {
159159
return devices, nil
160160
}
161161

162+
// validateVersion checks whether the specified spec version is supported.
163+
func validateVersion(version string) error {
164+
if _, ok := validSpecVersions[version]; !ok {
165+
return errors.Errorf("invalid version %q", version)
166+
}
167+
168+
return nil
169+
}
170+
162171
// Parse raw CDI Spec file data.
163172
func parseSpec(data []byte) (*cdi.Spec, error) {
164173
raw := &cdi.Spec{}

pkg/cdi/spec_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,7 @@ func mkTestFile(t *testing.T, data []byte) (string, error) {
346346
tmp.Close()
347347
return file, nil
348348
}
349+
350+
func TestCurrentVersionIsValid(t *testing.T) {
351+
require.NoError(t, validateVersion(cdi.CurrentVersion))
352+
}

0 commit comments

Comments
 (0)