Skip to content

Commit

Permalink
Added serial console support
Browse files Browse the repository at this point in the history
  • Loading branch information
Janos Bonic committed May 27, 2022
1 parent 32397f9 commit 97421d8
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 2 deletions.
42 changes: 41 additions & 1 deletion vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,9 @@ type VM interface {

// ListGraphicsConsoles lists the graphics consoles on the VM.
ListGraphicsConsoles(retries ...RetryStrategy) ([]VMGraphicsConsole, error)

// SerialConsole returns true if the VM has a serial console.
SerialConsole() bool
}

// VMSearchParameters declares the parameters that can be passed to a VM search. Each parameter
Expand Down Expand Up @@ -682,6 +685,9 @@ type OptionalVMParameters interface {

// OS returns the operating system parameters, and true if the OS parameter has been set.
OS() (VMOSParameters, bool)

// SerialConsole returns if a serial console should be created or not.
SerialConsole() *bool
}

// BuildableVMParameters is a variant of OptionalVMParameters that can be changed using the supplied
Expand Down Expand Up @@ -751,6 +757,9 @@ type BuildableVMParameters interface {

// WithOS adds the operating system parameters to the VM creation.
WithOS(parameters VMOSParameters) BuildableVMParameters

// WithSerialConsole adds or removes a serial console to the VM.
WithSerialConsole(serialConsole bool) BuildableVMParameters
}

// VMCPUParams contain the CPU parameters for a VM.
Expand Down Expand Up @@ -1005,7 +1014,6 @@ const (
// options:
//
// - Enable headless mode.
// - Enable serial console.
// - Enable pass-through host CPU.
// - Enable I/O threads.
// - Enable I/O threads pinning and set the pinning topology.
Expand Down Expand Up @@ -1523,6 +1531,17 @@ type vmParams struct {

os VMOSParameters
osSet bool

serialConsole *bool
}

func (v *vmParams) SerialConsole() *bool {
return v.serialConsole
}

func (v *vmParams) WithSerialConsole(serialConsole bool) BuildableVMParameters {
v.serialConsole = &serialConsole
return v
}

func (v *vmParams) OS() (VMOSParameters, bool) {
Expand Down Expand Up @@ -1800,6 +1819,11 @@ type vm struct {
instanceTypeID *InstanceTypeID
vmType VMType
os *vmOS
serialConsole bool
}

func (v *vm) SerialConsole() bool {
return v.serialConsole
}

func (v *vm) ListGraphicsConsoles(retries ...RetryStrategy) ([]VMGraphicsConsole, error) {
Expand Down Expand Up @@ -1916,6 +1940,7 @@ func (v *vm) withName(name string) *vm {
v.instanceTypeID,
v.vmType,
v.os,
v.serialConsole,
}
}

Expand All @@ -1941,6 +1966,7 @@ func (v *vm) withComment(comment string) *vm {
v.instanceTypeID,
v.vmType,
v.os,
v.serialConsole,
}
}

Expand Down Expand Up @@ -2066,6 +2092,7 @@ func convertSDKVM(sdkObject *ovirtsdk.Vm, client Client) (VM, error) {
vmInstanceTypeIDConverter,
vmTypeConverter,
vmOSConverter,
vmSerialConsoleConverter,
}
for _, converter := range vmConverters {
if err := converter(sdkObject, vmObject); err != nil {
Expand All @@ -2076,6 +2103,19 @@ func convertSDKVM(sdkObject *ovirtsdk.Vm, client Client) (VM, error) {
return vmObject, nil
}

func vmSerialConsoleConverter(object *ovirtsdk.Vm, v *vm) error {
console, ok := object.Console()
if !ok {
return newFieldNotFound("vm", "serial console")
}
enabled, ok := console.Enabled()
if !ok {
return newFieldNotFound("serial console", "enabled")
}
v.serialConsole = enabled
return nil
}

func vmOSConverter(object *ovirtsdk.Vm, v *vm) error {
sdkOS, ok := object.Os()
if !ok {
Expand Down
18 changes: 17 additions & 1 deletion vm_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func createSDKVM(
vmInstanceTypeID,
vmTypeCreator,
vmOSCreator,
vmSerialConsoleCreator,
}

for _, part := range parts {
Expand Down Expand Up @@ -195,6 +196,14 @@ func createSDKVM(
return vm, nil
}

func vmSerialConsoleCreator(params OptionalVMParameters, builder *ovirtsdk.VmBuilder) {
serial := params.SerialConsole()
if serial == nil {
return
}
builder.ConsoleBuilder(ovirtsdk.NewConsoleBuilder().Enabled(*serial))
}

func vmOSCreator(params OptionalVMParameters, builder *ovirtsdk.VmBuilder) {
if os, ok := params.OS(); ok {
osBuilder := ovirtsdk.NewOperatingSystemBuilder()
Expand Down Expand Up @@ -382,6 +391,12 @@ func (m *mockClient) createVM(
init = &initialization{}
}

vmType := m.createVMType(params)
console := false
if serialConsole := params.SerialConsole(); serialConsole != nil {
console = *serialConsole
}

vm := &vm{
m,
VMID(id),
Expand All @@ -399,8 +414,9 @@ func (m *mockClient) createVM(
m.createPlacementPolicy(params),
m.createVMMemoryPolicy(params),
params.InstanceTypeID(),
m.createVMType(params),
vmType,
m.createVMOS(params),
console,
}
m.vms[VMID(id)] = vm
return vm
Expand Down
85 changes: 85 additions & 0 deletions vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,91 @@ func TestVMDiskStorageDomain(t *testing.T) {
}
}

func TestVMSerialConsole(t *testing.T) {
testCases := getSerialConsoleTestCases()

for _, tc := range testCases {
vmType := "nil"
if tc.vmType != nil {
vmType = string(*tc.vmType)
}
set := "nil"
if tc.set != nil {
set = fmt.Sprintf("%t", *tc.set)
}
t.Run(
fmt.Sprintf("vmType=%s,console=%s", vmType, set),
func(t *testing.T) {
var err error
helper := getHelper(t)

t.Logf(
"Creating VM with vmType=%s and console=%s, expecting console to be %t...",
vmType,
set,
tc.expected,
)

params := ovirtclient.NewCreateVMParams()
if tc.vmType != nil {
params, err = params.WithVMType(*tc.vmType)
if err != nil {
t.Fatalf("Failed to set VM type (%v)", err)
}
}
if tc.set != nil {
params = params.WithSerialConsole(*tc.set)
}

vm := assertCanCreateVM(
t,
helper,
helper.GenerateTestResourceName(t),
params,
)
if vm.SerialConsole() != tc.expected {
t.Fatalf(
"Incorrect value for serial console (expected: %t, got: %t)",
tc.expected,
vm.SerialConsole(),
)
}
t.Logf("Found correct value for serial console: %t", tc.expected)
},
)
}
}

func getSerialConsoleTestCases() []struct {
vmType *ovirtclient.VMType
set *bool
expected bool
} {
yes := true
no := false
desktop := ovirtclient.VMTypeDesktop
server := ovirtclient.VMTypeServer
hp := ovirtclient.VMTypeHighPerformance
testCases := []struct {
vmType *ovirtclient.VMType
set *bool
expected bool
}{
{nil, &yes, true},
{nil, &no, false},
{&desktop, nil, false},
{&server, nil, false},
{&hp, nil, false},
{&desktop, &yes, true},
{&server, &yes, true},
{&hp, &yes, true},
{&desktop, &no, false},
{&server, &no, false},
{&hp, &no, false},
}
return testCases
}

func assertCanCreateVMFromTemplate(
t *testing.T,
helper ovirtclient.TestHelper,
Expand Down

0 comments on commit 97421d8

Please sign in to comment.