Skip to content

Commit

Permalink
clh: basic/unit tests for clh driver
Browse files Browse the repository at this point in the history
- added clh unit tests
- removed some inconsistencies in the cli builder to enable unit tests
- suppressed version check for in startSandbox to enable unit tests
- added clh related constants and methods to virtcontainer test
- small corrections after review applied

Fixes: kata-containers#2205

Signed-off-by: Johan Kuijpers <[email protected]>
  • Loading branch information
ericooper committed Nov 28, 2019
1 parent 3ef8f6c commit e8cc87b
Show file tree
Hide file tree
Showing 3 changed files with 767 additions and 21 deletions.
76 changes: 55 additions & 21 deletions virtcontainers/clh.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (
supportedMinorVersion = 3
defaultClhPath = "/usr/local/bin/cloud-hypervisor"
virtioFsCacheAlways = "always"
maxClhVcpus = uint32(64)
)

type CloudHypervisorVersion struct {
Expand Down Expand Up @@ -126,20 +127,26 @@ func (clh *cloudHypervisor) createSandbox(ctx context.Context, id string, networ
clh.config = *hypervisorConfig
clh.state.state = clhNotReady

err = clh.getAvailableVersion()
if err != nil {
return err
// version check only applicable to 'cloud-hypervisor' executable
clhPath, perr := clh.clhPath()
if perr != nil {
return perr
}
if strings.HasSuffix(clhPath, "cloud-hypervisor") {
err = clh.getAvailableVersion()
if err != nil {
return err
}

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

clh.cliBuilder = &DefaultCLIBuilder{}

socketPath, err := clh.vsockSocketPath(id)
Expand Down Expand Up @@ -411,12 +418,6 @@ func (clh *cloudHypervisor) getPids() []int {
return pids
}

//###########################################################################
//
// Local helper methods related to the hypervisor interface implementation
//
//###########################################################################

func (clh *cloudHypervisor) addDevice(devInfo interface{}, devType deviceType) error {
span, _ := clh.trace("addDevice")
defer span.Finish()
Expand Down Expand Up @@ -449,6 +450,12 @@ func (clh *cloudHypervisor) addDevice(devInfo interface{}, devType deviceType) e
return err
}

//###########################################################################
//
// Local helper methods related to the hypervisor interface implementation
//
//###########################################################################

func (clh *cloudHypervisor) Logger() *log.Entry {
return virtLog.WithField("subsystem", "cloudHypervisor")
}
Expand Down Expand Up @@ -856,6 +863,11 @@ func (clh *cloudHypervisor) LaunchClh() (string, int, error) {
return errStr, cmd.Process.Pid, nil
}

// MaxClhVCPUs returns the maximum number of vCPUs supported
func MaxClhVCPUs() uint32 {
return maxClhVcpus
}

//###########################################################################
//
// Cloud-hypervisor CLI builder
Expand All @@ -865,6 +877,8 @@ func (clh *cloudHypervisor) LaunchClh() (string, int, error) {
const (
cctOFF string = "off"
cctFILE string = "file"
cctNULL string = "null"
cctTTY string = "tty"
)

const (
Expand Down Expand Up @@ -1083,6 +1097,7 @@ type CLIFs struct {
queues uint32
queueSize uint32
dax bool
cacheSize string
}

func (o *CLIFs) Build(cmdline *CommandLine) {
Expand All @@ -1092,6 +1107,9 @@ func (o *CLIFs) Build(cmdline *CommandLine) {
fsarg := "tag=" + o.tag + ",sock=" + o.socketPath
if o.dax {
fsarg += ",dax=on"
if o.cacheSize != "" {
fsarg += ",cache_size=" + o.cacheSize
}
} else {
fsarg += ",num_queues=" + strconv.FormatUint(uint64(o.queues), 10) + ",queue_size=" + strconv.FormatUint(uint64(o.queueSize), 10)
}
Expand All @@ -1103,6 +1121,8 @@ func (o *CLIFs) Build(cmdline *CommandLine) {
//****************************************
type CLINet struct {
device string
ip string
mask string
mac string
iommu bool
}
Expand All @@ -1118,13 +1138,23 @@ func (o *CLINets) Build(cmdline *CommandLine) {
networks := ""
netIndex := 1
for _, net := range o.networks {
tapName := "tap" + strconv.FormatUint(uint64(netIndex), 10)
netIndex++
cnet := "tap=tap" + strconv.FormatUint(uint64(netIndex), 10)
if net.ip != "" && net.mask != "" {
cnet += ",ip=" + net.ip + ",mask=" + net.mask
}
if net.mac != "" {
cnet += ",mac=" + net.mac
}

if net.iommu {
networks += "tap=" + tapName + ",mac=" + net.mac + ",iommu=on"
cnet += ",iommu=on"
}
if netIndex > 1 {
networks += "," + cnet
} else {
networks += "tap=" + tapName + ",mac=" + net.mac
networks += cnet
}
netIndex++
}
cmdline.args = append(cmdline.args, networks)
}
Expand Down Expand Up @@ -1259,6 +1289,10 @@ func (d *DefaultCLIBuilder) GetCommandLine() (*CommandLine, error) {
d.console.Build(cmdLine)
}

if d.apiSocket != nil {
d.apiSocket.Build(cmdLine)
}

if d.logFile != nil {
d.logFile.Build(cmdLine)
}
Expand Down
Loading

0 comments on commit e8cc87b

Please sign in to comment.