2015-09-08 21:08:49 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-03-15 20:28:25 +00:00
|
|
|
"path/filepath"
|
2016-03-24 07:47:23 +00:00
|
|
|
"strings"
|
2016-10-07 19:37:52 +00:00
|
|
|
"syscall"
|
2015-09-08 21:08:49 +00:00
|
|
|
"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"
|
2015-09-08 21:08:49 +00:00
|
|
|
)
|
|
|
|
|
2015-09-25 23:49:26 +00:00
|
|
|
// The fingerprinter test should always pass, even if QEMU is not installed.
|
2015-09-08 21:08:49 +00:00
|
|
|
func TestQemuDriver_Fingerprint(t *testing.T) {
|
2015-09-25 23:49:14 +00:00
|
|
|
ctestutils.QemuCompatible(t)
|
2016-08-24 17:25:02 +00:00
|
|
|
task := &structs.Task{
|
|
|
|
Name: "foo",
|
2016-12-03 01:04:07 +00:00
|
|
|
Driver: "qemu",
|
2016-08-24 17:25:02 +00:00
|
|
|
Resources: structs.DefaultResources(),
|
|
|
|
}
|
2016-12-03 01:04:07 +00:00
|
|
|
ctx := testDriverContexts(t, task)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewQemuDriver(ctx.DriverCtx)
|
|
|
|
|
2015-09-08 21:08:49 +00:00
|
|
|
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) {
|
2015-10-23 07:27:59 +00:00
|
|
|
ctestutils.QemuCompatible(t)
|
2015-09-08 21:08:49 +00:00
|
|
|
task := &structs.Task{
|
2016-12-03 01:04:07 +00:00
|
|
|
Name: "linux",
|
|
|
|
Driver: "qemu",
|
2015-11-15 08:37:00 +00:00
|
|
|
Config: map[string]interface{}{
|
2016-03-15 20:28:25 +00:00
|
|
|
"image_path": "linux-0.2.img",
|
|
|
|
"accelerator": "tcg",
|
2015-11-18 04:54:53 +00:00
|
|
|
"port_map": []map[string]int{{
|
|
|
|
"main": 22,
|
|
|
|
"web": 8080,
|
|
|
|
}},
|
2016-08-16 03:36:13 +00:00
|
|
|
"args": []string{"-nodefconfig", "-nodefaults"},
|
2015-09-24 04:15:28 +00:00
|
|
|
},
|
2016-02-10 21:54:54 +00:00
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
2015-09-24 04:15:28 +00:00
|
|
|
Resources: &structs.Resources{
|
2015-11-06 18:41:42 +00:00
|
|
|
CPU: 500,
|
2015-09-24 04:15:28 +00:00
|
|
|
MemoryMB: 512,
|
|
|
|
Networks: []*structs.NetworkResource{
|
|
|
|
&structs.NetworkResource{
|
2017-02-28 00:00:19 +00:00
|
|
|
ReservedPorts: []structs.Port{{Label: "main", Value: 22000}, {Label: "web", Value: 80}},
|
2015-09-24 04:15:28 +00:00
|
|
|
},
|
|
|
|
},
|
2015-09-08 21:08:49 +00:00
|
|
|
},
|
|
|
|
}
|
2015-09-09 19:28:16 +00:00
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
ctx := testDriverContexts(t, task)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewQemuDriver(ctx.DriverCtx)
|
2015-09-09 19:28:16 +00:00
|
|
|
|
2016-03-15 20:28:25 +00:00
|
|
|
// Copy the test image into the task's directory
|
2016-12-03 01:04:07 +00:00
|
|
|
dst := ctx.ExecCtx.TaskDir.Dir
|
2016-03-15 20:28:25 +00:00
|
|
|
copyFile("./test-resources/qemu/linux-0.2.img", filepath.Join(dst, "linux-0.2.img"), t)
|
|
|
|
|
2017-01-10 21:24:45 +00:00
|
|
|
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
t.Fatalf("Prestart faild: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-06-13 21:02:11 +00:00
|
|
|
resp, err := d.Start(ctx.ExecCtx, task)
|
2015-09-08 21:08:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-10-07 19:37:52 +00:00
|
|
|
// Ensure that sending a Signal returns an error
|
2017-06-13 21:02:11 +00:00
|
|
|
if err := resp.Handle.Signal(syscall.SIGINT); err == nil {
|
2016-10-07 19:37:52 +00:00
|
|
|
t.Fatalf("Expect an error when signalling")
|
|
|
|
}
|
|
|
|
|
2015-09-08 21:08:49 +00:00
|
|
|
// Attempt to open
|
2017-06-13 21:02:11 +00:00
|
|
|
handle2, err := d.Open(ctx.ExecCtx, resp.Handle.ID())
|
2015-09-08 21:08:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if handle2 == nil {
|
|
|
|
t.Fatalf("missing handle")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up
|
2017-06-13 21:02:11 +00:00
|
|
|
if err := resp.Handle.Kill(); err != nil {
|
2015-09-08 21:08:49 +00:00
|
|
|
fmt.Printf("\nError killing Qemu test: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2016-03-24 07:47:23 +00:00
|
|
|
|
|
|
|
func TestQemuDriverUser(t *testing.T) {
|
|
|
|
ctestutils.QemuCompatible(t)
|
|
|
|
task := &structs.Task{
|
2016-12-03 01:04:07 +00:00
|
|
|
Name: "linux",
|
|
|
|
Driver: "qemu",
|
|
|
|
User: "alice",
|
2016-03-24 07:47:23 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"image_path": "linux-0.2.img",
|
|
|
|
"accelerator": "tcg",
|
|
|
|
"port_map": []map[string]int{{
|
|
|
|
"main": 22,
|
|
|
|
"web": 8080,
|
|
|
|
}},
|
2016-08-16 03:36:13 +00:00
|
|
|
"args": []string{"-nodefconfig", "-nodefaults"},
|
2016-03-24 07:47:23 +00:00
|
|
|
},
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
CPU: 500,
|
|
|
|
MemoryMB: 512,
|
|
|
|
Networks: []*structs.NetworkResource{
|
|
|
|
&structs.NetworkResource{
|
2017-02-28 00:00:19 +00:00
|
|
|
ReservedPorts: []structs.Port{{Label: "main", Value: 22000}, {Label: "web", Value: 80}},
|
2016-03-24 07:47:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
ctx := testDriverContexts(t, task)
|
|
|
|
defer ctx.AllocDir.Destroy()
|
|
|
|
d := NewQemuDriver(ctx.DriverCtx)
|
|
|
|
|
2017-01-10 21:24:45 +00:00
|
|
|
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
|
2016-12-03 01:04:07 +00:00
|
|
|
t.Fatalf("Prestart faild: %v", err)
|
|
|
|
}
|
2016-03-24 07:47:23 +00:00
|
|
|
|
2017-06-13 21:02:11 +00:00
|
|
|
resp, err := d.Start(ctx.ExecCtx, task)
|
2016-03-24 07:47:23 +00:00
|
|
|
if err == nil {
|
2017-06-13 21:02:11 +00:00
|
|
|
resp.Handle.Kill()
|
2016-03-24 07:47:23 +00:00
|
|
|
t.Fatalf("Should've failed")
|
|
|
|
}
|
|
|
|
msg := "unknown user alice"
|
|
|
|
if !strings.Contains(err.Error(), msg) {
|
|
|
|
t.Fatalf("Expecting '%v' in '%v'", msg, err)
|
|
|
|
}
|
|
|
|
}
|