open-nomad/client/driver/qemu_test.go

138 lines
3.1 KiB
Go
Raw Normal View History

package driver
import (
"fmt"
2016-03-15 20:28:25 +00:00
"path/filepath"
"strings"
"testing"
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/nomad/structs"
2015-09-25 23:49:14 +00:00
ctestutils "github.com/hashicorp/nomad/client/testutil"
)
// The fingerprinter test should always pass, even if QEMU is not installed.
func TestQemuDriver_Fingerprint(t *testing.T) {
2015-09-25 23:49:14 +00:00
ctestutils.QemuCompatible(t)
2016-01-06 02:02:11 +00:00
driverCtx, _ := testDriverContexts(&structs.Task{Name: "foo"})
d := NewQemuDriver(driverCtx)
node := &structs.Node{
Attributes: make(map[string]string),
}
apply, err := d.Fingerprint(&config.Config{}, node)
if err != nil {
t.Fatalf("err: %v", err)
}
if !apply {
t.Fatalf("should apply")
}
if node.Attributes["driver.qemu"] == "" {
t.Fatalf("Missing Qemu driver")
}
if node.Attributes["driver.qemu.version"] == "" {
t.Fatalf("Missing Qemu driver version")
}
}
2015-11-06 18:41:42 +00:00
func TestQemuDriver_StartOpen_Wait(t *testing.T) {
ctestutils.QemuCompatible(t)
task := &structs.Task{
2015-09-25 23:49:14 +00:00
Name: "linux",
Config: map[string]interface{}{
2016-03-15 20:28:25 +00:00
"image_path": "linux-0.2.img",
"accelerator": "tcg",
"port_map": []map[string]int{{
"main": 22,
"web": 8080,
}},
},
2016-02-10 21:54:54 +00:00
LogConfig: &structs.LogConfig{
MaxFiles: 10,
MaxFileSizeMB: 10,
},
Resources: &structs.Resources{
2015-11-06 18:41:42 +00:00
CPU: 500,
MemoryMB: 512,
Networks: []*structs.NetworkResource{
&structs.NetworkResource{
ReservedPorts: []structs.Port{{"main", 22000}, {"web", 80}},
},
},
},
}
2016-01-06 02:02:11 +00:00
driverCtx, execCtx := testDriverContexts(task)
defer execCtx.AllocDir.Destroy()
2015-09-25 23:49:14 +00:00
d := NewQemuDriver(driverCtx)
2016-03-15 20:28:25 +00:00
// Copy the test image into the task's directory
dst, _ := execCtx.AllocDir.TaskDirs[task.Name]
copyFile("./test-resources/qemu/linux-0.2.img", filepath.Join(dst, "linux-0.2.img"), t)
2016-01-06 02:02:11 +00:00
handle, err := d.Start(execCtx, task)
if err != nil {
t.Fatalf("err: %v", err)
}
if handle == nil {
t.Fatalf("missing handle")
}
// Attempt to open
2016-01-06 02:02:11 +00:00
handle2, err := d.Open(execCtx, handle.ID())
if err != nil {
t.Fatalf("err: %v", err)
}
if handle2 == nil {
t.Fatalf("missing handle")
}
// Clean up
if err := handle.Kill(); err != nil {
fmt.Printf("\nError killing Qemu test: %s", err)
}
}
func TestQemuDriverUser(t *testing.T) {
ctestutils.QemuCompatible(t)
task := &structs.Task{
Name: "linux",
User: "alice",
Config: map[string]interface{}{
"image_path": "linux-0.2.img",
"accelerator": "tcg",
"port_map": []map[string]int{{
"main": 22,
"web": 8080,
}},
},
LogConfig: &structs.LogConfig{
MaxFiles: 10,
MaxFileSizeMB: 10,
},
Resources: &structs.Resources{
CPU: 500,
MemoryMB: 512,
Networks: []*structs.NetworkResource{
&structs.NetworkResource{
ReservedPorts: []structs.Port{{"main", 22000}, {"web", 80}},
},
},
},
}
driverCtx, execCtx := testDriverContexts(task)
defer execCtx.AllocDir.Destroy()
d := NewQemuDriver(driverCtx)
handle, err := d.Start(execCtx, task)
if err == nil {
handle.Kill()
t.Fatalf("Should've failed")
}
msg := "unknown user alice"
if !strings.Contains(err.Error(), msg) {
t.Fatalf("Expecting '%v' in '%v'", msg, err)
}
}