Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #547 from jodh-intel/cc-env-add-hypervisor-version
Browse files Browse the repository at this point in the history
cc env add hypervisor version
  • Loading branch information
Graham Whaley authored Sep 13, 2017
2 parents fa07228 + 098d7bd commit 97aa6e6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 21 deletions.
29 changes: 20 additions & 9 deletions cc-env.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
//
// XXX: Increment for every change to the output format
// (meaning any change to the EnvInfo type).
const formatVersion = "1.0.2"
const formatVersion = "1.0.3"

// MetaInfo stores information on the format of the output itself
type MetaInfo struct {
Expand Down Expand Up @@ -79,8 +79,9 @@ type RuntimeVersionInfo struct {

// HypervisorInfo stores hypervisor details
type HypervisorInfo struct {
Location PathInfo
MachineType string
Version string
Location PathInfo
}

// ProxyInfo stores proxy details
Expand Down Expand Up @@ -285,6 +286,22 @@ func getAgentInfo(config oci.RuntimeConfig) (AgentInfo, error) {
return ccAgent, nil
}

func getHypervisorInfo(config oci.RuntimeConfig, hypervisorDetails hypervisorDetails) HypervisorInfo {
version, err := getCommandVersion(hypervisorDetails.HypervisorPath)
if err != nil {
version = unknown
}

return HypervisorInfo{
MachineType: config.HypervisorConfig.HypervisorMachineType,
Version: version,
Location: PathInfo{
Path: config.HypervisorConfig.HypervisorPath,
Resolved: hypervisorDetails.HypervisorPath,
},
}
}

func getEnvInfo(configFile, logfilePath string, config oci.RuntimeConfig) (env EnvInfo, err error) {
meta := getMetaInfo()

Expand Down Expand Up @@ -315,13 +332,7 @@ func getEnvInfo(configFile, logfilePath string, config oci.RuntimeConfig) (env E
return EnvInfo{}, err
}

hypervisor := HypervisorInfo{
Location: PathInfo{
Path: config.HypervisorConfig.HypervisorPath,
Resolved: resolvedHypervisor.HypervisorPath,
},
MachineType: config.HypervisorConfig.HypervisorMachineType,
}
hypervisor := getHypervisorInfo(config, resolvedHypervisor)

image := PathInfo{
Path: config.HypervisorConfig.ImagePath,
Expand Down
53 changes: 41 additions & 12 deletions cc-env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ import (
const testProxyURL = "file:///proxyURL"
const testProxyVersion = "proxy version 0.1"
const testShimVersion = "shim version 0.1"
const testHypervisorVersion = "QEMU emulator version 2.7.0+git.741f430a96-6.1, Copyright (c) 2003-2016 Fabrice Bellard and the QEMU Project developers"

// makeVersionBinary creates a shell script with the specified file
// name. When run as "file --version", it will display the specified
// version to stdout and exit successfully.
func makeVersionBinary(file, version string) error {
err := createFile(file,
fmt.Sprintf(`#!/bin/sh
[ "$1" = "--version" ] && echo "%s"`, version))
if err != nil {
return err
}

err = os.Chmod(file, testExeFileMode)
if err != nil {
return err
}

return nil
}

func makeRuntimeConfig(prefixDir string) (configFile string, config oci.RuntimeConfig, err error) {
const logPath = "/log/path"
Expand Down Expand Up @@ -75,26 +95,17 @@ func makeRuntimeConfig(prefixDir string) (configFile string, config oci.RuntimeC
}
}

err = createFile(shimPath,
fmt.Sprintf(`#!/bin/sh
[ "$1" = "--version" ] && echo "%s"`, testShimVersion))
err = makeVersionBinary(shimPath, testShimVersion)
if err != nil {
return "", oci.RuntimeConfig{}, err
}

err = os.Chmod(shimPath, testExeFileMode)
err = makeVersionBinary(proxyPath, testProxyVersion)
if err != nil {
return "", oci.RuntimeConfig{}, err
}

err = createFile(proxyPath,
fmt.Sprintf(`#!/bin/sh
[ "$1" = "--version" ] && echo "%s"`, testProxyVersion))
if err != nil {
return "", oci.RuntimeConfig{}, err
}

err = os.Chmod(proxyPath, testExeFileMode)
err = makeVersionBinary(hypervisorPath, testHypervisorVersion)
if err != nil {
return "", oci.RuntimeConfig{}, err
}
Expand Down Expand Up @@ -248,6 +259,7 @@ model name : %s

func getExpectedHypervisor(config oci.RuntimeConfig) HypervisorInfo {
return HypervisorInfo{
Version: testHypervisorVersion,
Location: PathInfo{
Path: config.HypervisorConfig.HypervisorPath,
Resolved: config.HypervisorConfig.HypervisorPath,
Expand Down Expand Up @@ -1222,3 +1234,20 @@ func TestCCEnvCLIFunctionFail(t *testing.T) {
err = fn(ctx)
assert.Error(t, err)
}

func TestGetHypervisorInfo(t *testing.T) {
assert := assert.New(t)

tmpdir, err := ioutil.TempDir("", "")
assert.NoError(err)
defer os.RemoveAll(tmpdir)

const logFile = "/tmp/file.log"

_, config, err := makeRuntimeConfig(tmpdir)
assert.NoError(err)

info := getHypervisorInfo(config, hypervisorDetails{})

assert.Equal(info.Version, unknown)
}

0 comments on commit 97aa6e6

Please sign in to comment.