vc: Version support check is ineffective in createSandbox

[ port from runtime repository commit 7e47046111 ]

If major version matches max supported major, we continue comparing the minor version.

Signed-off-by: Ted Yu <yuzhihong@gmail.com>
Signed-off-by: Peng Tao <bergwolf@hyper.sh>
This commit is contained in:
Ted Yu 2020-06-28 20:32:55 -07:00 committed by Peng Tao
parent 0c5ace57d5
commit 61e011e86b
2 changed files with 59 additions and 7 deletions

View File

@ -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
}
}

View File

@ -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)
}
}
}