diff --git a/src/runtime/virtcontainers/clh.go b/src/runtime/virtcontainers/clh.go index 578df8c91..c77e2b2b8 100644 --- a/src/runtime/virtcontainers/clh.go +++ b/src/runtime/virtcontainers/clh.go @@ -150,6 +150,18 @@ var clhDebugKernelParams = []Param{ // //########################################################### +func (clh *cloudHypervisor) checkVersion() error { + if clh.version.Major < supportedMajorVersion || (clh.version.Major == supportedMajorVersion && clh.version.Minor < supportedMinorVersion) { + errorMessage := fmt.Sprintf("Unsupported version: cloud-hypervisor %d.%d not supported by this driver version (%d.%d)", + clh.version.Major, + clh.version.Minor, + supportedMajorVersion, + supportedMinorVersion) + return errors.New(errorMessage) + } + return nil +} + // For cloudHypervisor this call only sets the internal structure up. // The VM will be created and started through startSandbox(). func (clh *cloudHypervisor) createSandbox(ctx context.Context, id string, networkNS NetworkNamespace, hypervisorConfig *HypervisorConfig, stateful bool) error { @@ -180,13 +192,8 @@ func (clh *cloudHypervisor) createSandbox(ctx context.Context, id string, networ } - if clh.version.Major < supportedMajorVersion && clh.version.Minor < supportedMinorVersion { - errorMessage := fmt.Sprintf("Unsupported version: cloud-hypervisor %d.%d not supported by this driver version (%d.%d)", - clh.version.Major, - clh.version.Minor, - supportedMajorVersion, - supportedMinorVersion) - return errors.New(errorMessage) + if err := clh.checkVersion(); err != nil { + return err } } diff --git a/src/runtime/virtcontainers/clh_test.go b/src/runtime/virtcontainers/clh_test.go index 5f2e6b281..594762875 100644 --- a/src/runtime/virtcontainers/clh_test.go +++ b/src/runtime/virtcontainers/clh_test.go @@ -7,6 +7,7 @@ package virtcontainers import ( "context" + "fmt" "net/http" "os" "path/filepath" @@ -312,3 +313,47 @@ func TestCloudHypervisorResizeMemory(t *testing.T) { }) } } + +func TestCheckVersion(t *testing.T) { + clh := &cloudHypervisor{} + assert := assert.New(t) + testcases := []struct { + name string + major int + minor int + pass bool + }{ + { + name: "minor lower than supported version", + major: supportedMajorVersion, + minor: 2, + pass: false, + }, + { + name: "minor equal to supported version", + major: supportedMajorVersion, + minor: supportedMinorVersion, + pass: true, + }, + { + name: "major exceeding supported version", + major: 1, + minor: supportedMinorVersion, + pass: true, + }, + } + for _, tc := range testcases { + clh.version = CloudHypervisorVersion{ + Major: tc.major, + Minor: tc.minor, + Revision: 0, + } + err := clh.checkVersion() + msg := fmt.Sprintf("test: %+v, clh.version: %v, result: %v", tc, clh.version, err) + if tc.pass { + assert.NoError(err, msg) + } else { + assert.Error(err, msg) + } + } +}