Skip to content

Commit

Permalink
ciao-launcher: Add TestGenerateQEMULaunchParams
Browse files Browse the repository at this point in the history
Add a new unit test case to test the code that generates qemu launch
parameters.

Partial fix for issue ciao-project#1

Signed-off-by: Mark Ryan <[email protected]>
  • Loading branch information
Mark Ryan committed Jul 18, 2016
1 parent c88d0ef commit a8d0b08
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 30 deletions.
68 changes: 38 additions & 30 deletions ciao-launcher/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,21 +419,45 @@ func launchQemuWithSpice(params []string, fds []*os.File, ipAddress string) (int
return port, err
}

func (q *qemu) startVM(vnicName, ipAddress string) error {

var fds []*os.File

glog.Info("Launching qemu")

vmImage := path.Join(q.instanceDir, "image.qcow2")
qmpSocket := path.Join(q.instanceDir, "socket")
func generateQEMULaunchParams(cfg *vmConfig, isoPath, instanceDir string, networkParams []string) []string {
vmImage := path.Join(instanceDir, "image.qcow2")
qmpSocket := path.Join(instanceDir, "socket")
fileParam := fmt.Sprintf("file=%s,if=virtio,aio=threads,format=qcow2", vmImage)
isoParam := fmt.Sprintf("file=%s,if=virtio,media=cdrom", q.isoPath)
isoParam := fmt.Sprintf("file=%s,if=virtio,media=cdrom", isoPath)
qmpParam := fmt.Sprintf("unix:%s,server,nowait", qmpSocket)

params := make([]string, 0, 32)
params = append(params, "-drive", fileParam)
params = append(params, "-drive", isoParam)
params = append(params, networkParams...)

params = append(params, "-enable-kvm")
params = append(params, "-cpu", "host")
params = append(params, "-daemonize")
params = append(params, "-qmp", qmpParam)

if cfg.Mem > 0 {
memoryParam := fmt.Sprintf("%d", cfg.Mem)
params = append(params, "-m", memoryParam)
}
if cfg.Cpus > 0 {
cpusParam := fmt.Sprintf("cpus=%d", cfg.Cpus)
params = append(params, "-smp", cpusParam)
}

if !cfg.Legacy {
params = append(params, "-bios", qemuEfiFw)
}
return params
}

func (q *qemu) startVM(vnicName, ipAddress string) error {

var fds []*os.File

glog.Info("Launching qemu")

networkParams := make([]string, 0, 32)

if vnicName != "" {
if q.cfg.NetworkNode {
Expand All @@ -446,36 +470,20 @@ func (q *qemu) startVM(vnicName, ipAddress string) error {
return err
}
defer cleanupFds(fds, len(fds))
params = append(params, macvtapParam...)
networkParams = append(networkParams, macvtapParam...)
} else {
tapParam, err := computeTapParam(vnicName, q.cfg.VnicMAC)
if err != nil {
return err
}
params = append(params, tapParam...)
networkParams = append(networkParams, tapParam...)
}
} else {
params = append(params, "-net", "nic,model=virtio")
params = append(params, "-net", "user")
networkParams = append(networkParams, "-net", "nic,model=virtio")
networkParams = append(networkParams, "-net", "user")
}

params = append(params, "-enable-kvm")
params = append(params, "-cpu", "host")
params = append(params, "-daemonize")
params = append(params, "-qmp", qmpParam)

if q.cfg.Mem > 0 {
memoryParam := fmt.Sprintf("%d", q.cfg.Mem)
params = append(params, "-m", memoryParam)
}
if q.cfg.Cpus > 0 {
cpusParam := fmt.Sprintf("cpus=%d", q.cfg.Cpus)
params = append(params, "-smp", cpusParam)
}

if !q.cfg.Legacy {
params = append(params, "-bios", qemuEfiFw)
}
params := generateQEMULaunchParams(q.cfg, q.isoPath, q.instanceDir, networkParams)

var err error

Expand Down
71 changes: 71 additions & 0 deletions ciao-launcher/qemu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"bytes"
"reflect"
"testing"
)

Expand Down Expand Up @@ -126,3 +127,73 @@ func TestExtractImageInfo(t *testing.T) {
}
}
}

func genQEMUParams(networkParams []string) []string {
baseParams := []string{
"-drive",
"file=/var/lib/ciao/instance/1/image.qcow2,if=virtio,aio=threads,format=qcow2",
"-drive",
"file=/var/lib/ciao/instance/1/seed.iso,if=virtio,media=cdrom",
}
baseParams = append(baseParams, networkParams...)
baseParams = append(baseParams, "-enable-kvm", "-cpu", "host", "-daemonize",
"-qmp", "unix:/var/lib/ciao/instance/1/socket,server,nowait")

return baseParams
}

func TestGenerateQEMULaunchParams(t *testing.T) {
var cfg vmConfig

params := genQEMUParams(nil)
cfg.Legacy = true
genParams := generateQEMULaunchParams(&cfg, "/var/lib/ciao/instance/1/seed.iso",
"/var/lib/ciao/instance/1", nil)
if !reflect.DeepEqual(params, genParams) {
t.Fatalf("%s and %s do not match", params, genParams)
}

params = genQEMUParams(nil)
cfg.Legacy = false
cfg.Mem = 0
cfg.Cpus = 0
params = append(params, "-bios", qemuEfiFw)
genParams = generateQEMULaunchParams(&cfg, "/var/lib/ciao/instance/1/seed.iso",
"/var/lib/ciao/instance/1", nil)
if !reflect.DeepEqual(params, genParams) {
t.Fatalf("%s and %s do not match", params, genParams)
}

params = genQEMUParams(nil)
cfg.Mem = 100
cfg.Cpus = 0
cfg.Legacy = true
params = append(params, "-m", "100")
genParams = generateQEMULaunchParams(&cfg, "/var/lib/ciao/instance/1/seed.iso",
"/var/lib/ciao/instance/1", nil)
if !reflect.DeepEqual(params, genParams) {
t.Fatalf("%s and %s do not match", params, genParams)
}

params = genQEMUParams(nil)
cfg.Mem = 0
cfg.Cpus = 4
cfg.Legacy = true
params = append(params, "-smp", "cpus=4")
genParams = generateQEMULaunchParams(&cfg, "/var/lib/ciao/instance/1/seed.iso",
"/var/lib/ciao/instance/1", nil)
if !reflect.DeepEqual(params, genParams) {
t.Fatalf("%s and %s do not match", params, genParams)
}

netParams := []string{"-net", "nic,model=virtio", "-net", "user"}
params = genQEMUParams(netParams)
cfg.Mem = 0
cfg.Cpus = 0
cfg.Legacy = true
genParams = generateQEMULaunchParams(&cfg, "/var/lib/ciao/instance/1/seed.iso",
"/var/lib/ciao/instance/1", netParams)
if !reflect.DeepEqual(params, genParams) {
t.Fatalf("%s and %s do not match", params, genParams)
}
}

0 comments on commit a8d0b08

Please sign in to comment.