2015-09-08 21:08:49 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
2018-01-31 01:41:34 +00:00
|
|
|
"bytes"
|
2015-09-08 21:08:49 +00:00
|
|
|
"fmt"
|
2018-01-31 01:41:34 +00:00
|
|
|
"net"
|
2016-03-15 20:28:25 +00:00
|
|
|
"path/filepath"
|
2018-01-31 01:41:34 +00:00
|
|
|
"strconv"
|
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"
|
2017-11-01 21:47:14 +00:00
|
|
|
"time"
|
2015-09-08 21:08:49 +00:00
|
|
|
|
2018-01-31 01:41:34 +00:00
|
|
|
"github.com/hashicorp/consul/lib/freeport"
|
2015-09-08 21:08:49 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2018-01-24 14:09:53 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2018-06-13 22:33:25 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2015-09-08 21:08:49 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2017-07-23 02:04:36 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
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) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !testutil.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
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)
|
2018-09-24 18:37:45 +00:00
|
|
|
defer ctx.Destroy()
|
2016-12-03 01:04:07 +00:00
|
|
|
d := NewQemuDriver(ctx.DriverCtx)
|
|
|
|
|
2015-09-08 21:08:49 +00:00
|
|
|
node := &structs.Node{
|
|
|
|
Attributes: make(map[string]string),
|
|
|
|
}
|
2018-01-24 14:09:53 +00:00
|
|
|
|
|
|
|
request := &cstructs.FingerprintRequest{Config: &config.Config{}, Node: node}
|
2018-01-26 16:21:07 +00:00
|
|
|
var response cstructs.FingerprintResponse
|
|
|
|
err := d.Fingerprint(request, &response)
|
2015-09-08 21:08:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-01-24 14:09:53 +00:00
|
|
|
|
2018-01-31 22:03:55 +00:00
|
|
|
if !response.Detected {
|
2018-01-30 17:57:37 +00:00
|
|
|
t.Fatalf("expected response to be applicable")
|
|
|
|
}
|
|
|
|
|
|
|
|
attributes := response.Attributes
|
|
|
|
if attributes == nil {
|
|
|
|
t.Fatalf("attributes should not be nil")
|
|
|
|
}
|
2018-01-26 16:21:07 +00:00
|
|
|
|
|
|
|
if attributes[qemuDriverAttr] == "" {
|
2015-09-08 21:08:49 +00:00
|
|
|
t.Fatalf("Missing Qemu driver")
|
|
|
|
}
|
2018-01-24 14:09:53 +00:00
|
|
|
|
2018-01-26 16:21:07 +00:00
|
|
|
if attributes[qemuDriverVersionAttr] == "" {
|
2015-09-08 21:08:49 +00:00
|
|
|
t.Fatalf("Missing Qemu driver version")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-06 18:41:42 +00:00
|
|
|
func TestQemuDriver_StartOpen_Wait(t *testing.T) {
|
2018-06-13 22:33:25 +00:00
|
|
|
logger := testlog.Logger(t)
|
2017-07-23 02:04:36 +00:00
|
|
|
if !testutil.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
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{}{
|
2017-10-18 00:54:22 +00:00
|
|
|
"image_path": "linux-0.2.img",
|
|
|
|
"accelerator": "tcg",
|
2017-11-01 21:47:14 +00:00
|
|
|
"graceful_shutdown": false,
|
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{
|
2017-10-02 20:16:48 +00:00
|
|
|
{
|
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)
|
2018-09-24 18:37:45 +00:00
|
|
|
defer ctx.Destroy()
|
2016-12-03 01:04:07 +00:00
|
|
|
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
|
2017-10-18 00:54:22 +00:00
|
|
|
|
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 {
|
2017-10-18 00:54:22 +00:00
|
|
|
t.Fatalf("Prestart failed: %v", err)
|
2016-12-03 01:04:07 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2017-11-01 23:52:44 +00:00
|
|
|
logger.Printf("Error killing Qemu test: %s", err)
|
2015-09-08 21:08:49 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-24 07:47:23 +00:00
|
|
|
|
2017-11-01 21:47:14 +00:00
|
|
|
func TestQemuDriver_GracefulShutdown(t *testing.T) {
|
2018-01-31 23:47:26 +00:00
|
|
|
testutil.SkipSlow(t)
|
2017-11-01 21:47:14 +00:00
|
|
|
if !testutil.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
ctestutils.QemuCompatible(t)
|
2018-01-31 01:41:34 +00:00
|
|
|
|
2018-06-13 22:33:25 +00:00
|
|
|
logger := testlog.Logger(t)
|
2018-01-31 23:47:26 +00:00
|
|
|
|
2018-01-31 01:41:34 +00:00
|
|
|
// Graceful shutdown may be really slow unfortunately
|
|
|
|
killTimeout := 3 * time.Minute
|
|
|
|
|
|
|
|
// Grab a free port so we can tell when the image has started
|
|
|
|
port := freeport.GetT(t, 1)[0]
|
|
|
|
|
2017-11-01 21:47:14 +00:00
|
|
|
task := &structs.Task{
|
2018-01-31 01:41:34 +00:00
|
|
|
Name: "alpine-shutdown-test",
|
2017-11-01 21:47:14 +00:00
|
|
|
Driver: "qemu",
|
|
|
|
Config: map[string]interface{}{
|
2018-01-31 01:41:34 +00:00
|
|
|
"image_path": "alpine.qcow2",
|
2017-11-01 21:47:14 +00:00
|
|
|
"graceful_shutdown": true,
|
2018-01-31 01:41:34 +00:00
|
|
|
"args": []string{"-nodefconfig", "-nodefaults"},
|
2017-11-01 21:47:14 +00:00
|
|
|
"port_map": []map[string]int{{
|
2018-01-31 01:41:34 +00:00
|
|
|
"ssh": 22,
|
2017-11-01 21:47:14 +00:00
|
|
|
}},
|
|
|
|
},
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
2018-01-31 01:41:34 +00:00
|
|
|
CPU: 1000,
|
|
|
|
MemoryMB: 256,
|
2017-11-01 21:47:14 +00:00
|
|
|
Networks: []*structs.NetworkResource{
|
|
|
|
{
|
2018-01-31 01:41:34 +00:00
|
|
|
ReservedPorts: []structs.Port{{Label: "ssh", Value: port}},
|
2017-11-01 21:47:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-01-31 01:41:34 +00:00
|
|
|
KillTimeout: killTimeout,
|
2017-11-01 21:47:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := testDriverContexts(t, task)
|
2018-01-31 01:41:34 +00:00
|
|
|
ctx.DriverCtx.config.MaxKillTimeout = killTimeout
|
2018-09-24 18:37:45 +00:00
|
|
|
defer ctx.Destroy()
|
2017-11-01 21:47:14 +00:00
|
|
|
d := NewQemuDriver(ctx.DriverCtx)
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
request := &cstructs.FingerprintRequest{Config: &config.Config{}, Node: ctx.DriverCtx.node}
|
2018-01-26 16:21:07 +00:00
|
|
|
var response cstructs.FingerprintResponse
|
|
|
|
err := d.Fingerprint(request, &response)
|
2017-11-03 00:37:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2018-01-24 14:09:53 +00:00
|
|
|
|
2018-01-30 17:57:37 +00:00
|
|
|
for name, value := range response.Attributes {
|
2018-01-24 14:09:53 +00:00
|
|
|
ctx.DriverCtx.node.Attributes[name] = value
|
2017-11-03 00:37:49 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 21:47:14 +00:00
|
|
|
dst := ctx.ExecCtx.TaskDir.Dir
|
|
|
|
|
2018-01-31 01:41:34 +00:00
|
|
|
copyFile("./test-resources/qemu/alpine.qcow2", filepath.Join(dst, "alpine.qcow2"), t)
|
2017-11-01 21:47:14 +00:00
|
|
|
|
|
|
|
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
|
|
|
|
t.Fatalf("Prestart failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := d.Start(ctx.ExecCtx, task)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-11-02 00:37:43 +00:00
|
|
|
// Clean up
|
|
|
|
defer func() {
|
2018-01-31 01:41:34 +00:00
|
|
|
select {
|
|
|
|
case <-resp.Handle.WaitCh():
|
|
|
|
// Already exited
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2017-11-02 00:37:43 +00:00
|
|
|
if err := resp.Handle.Kill(); err != nil {
|
2018-01-31 01:41:34 +00:00
|
|
|
logger.Printf("[TEST] Error killing Qemu test: %s", err)
|
2017-11-02 00:37:43 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-01-31 01:41:34 +00:00
|
|
|
// Wait until sshd starts before attempting to do a graceful shutdown
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
conn, err := net.Dial("tcp", net.JoinHostPort("127.0.0.1", strconv.Itoa(port)))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2017-11-01 21:47:14 +00:00
|
|
|
}
|
2018-01-31 01:41:34 +00:00
|
|
|
|
|
|
|
// Since the connection will be accepted by the QEMU process
|
|
|
|
// before sshd actually starts, we need to block until we can
|
|
|
|
// read the "SSH" magic bytes
|
|
|
|
header := make([]byte, 3)
|
|
|
|
conn.SetReadDeadline(time.Now().Add(10 * time.Second))
|
|
|
|
_, err = conn.Read(header)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if !bytes.Equal(header, []byte{'S', 'S', 'H'}) {
|
|
|
|
return false, fmt.Errorf("expected 'SSH' but received: %q %v", string(header), header)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Printf("[TEST] connected to sshd in VM")
|
|
|
|
conn.Close()
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("failed to connect to sshd in VM: %v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
monitorPath := filepath.Join(ctx.AllocDir.AllocDir, task.Name, qemuMonitorSocketName)
|
2017-11-01 21:47:14 +00:00
|
|
|
|
|
|
|
// userPid supplied in sendQemuShutdown calls is bogus (it's used only
|
|
|
|
// for log output)
|
|
|
|
if err := sendQemuShutdown(ctx.DriverCtx.logger, "", 0); err == nil {
|
|
|
|
t.Fatalf("sendQemuShutdown should return an error if monitorPath parameter is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sendQemuShutdown(ctx.DriverCtx.logger, "/path/that/does/not/exist", 0); err == nil {
|
|
|
|
t.Fatalf("sendQemuShutdown should return an error if file does not exist at monitorPath")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sendQemuShutdown(ctx.DriverCtx.logger, monitorPath, 0); err != nil {
|
|
|
|
t.Fatalf("unexpected error from sendQemuShutdown: %s", err)
|
|
|
|
}
|
2018-01-31 01:41:34 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-resp.Handle.WaitCh():
|
2018-02-12 19:25:52 +00:00
|
|
|
logger.Printf("[TEST] VM exited gracefully as expected")
|
2018-01-31 01:41:34 +00:00
|
|
|
case <-time.After(killTimeout):
|
|
|
|
t.Fatalf("VM did not exit gracefully exit before timeout: %s", killTimeout)
|
|
|
|
}
|
2017-11-01 21:47:14 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 07:47:23 +00:00
|
|
|
func TestQemuDriverUser(t *testing.T) {
|
2017-07-23 02:04:36 +00:00
|
|
|
if !testutil.IsTravis() {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
2016-03-24 07:47:23 +00:00
|
|
|
ctestutils.QemuCompatible(t)
|
2017-10-18 00:54:22 +00:00
|
|
|
tasks := []*structs.Task{
|
|
|
|
{
|
|
|
|
Name: "linux",
|
|
|
|
Driver: "qemu",
|
|
|
|
User: "alice",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"image_path": "linux-0.2.img",
|
|
|
|
"accelerator": "tcg",
|
|
|
|
"graceful_shutdown": false,
|
|
|
|
"port_map": []map[string]int{{
|
|
|
|
"main": 22,
|
|
|
|
"web": 8080,
|
|
|
|
}},
|
|
|
|
"args": []string{"-nodefconfig", "-nodefaults"},
|
|
|
|
"msg": "unknown user alice",
|
|
|
|
},
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
CPU: 500,
|
|
|
|
MemoryMB: 512,
|
|
|
|
Networks: []*structs.NetworkResource{
|
|
|
|
{
|
|
|
|
ReservedPorts: []structs.Port{{Label: "main", Value: 22000}, {Label: "web", Value: 80}},
|
|
|
|
},
|
2016-03-24 07:47:23 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-10-02 20:16:48 +00:00
|
|
|
{
|
2017-09-27 04:07:48 +00:00
|
|
|
Name: "linux",
|
|
|
|
Driver: "qemu",
|
|
|
|
User: "alice",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"image_path": "linux-0.2.img",
|
|
|
|
"accelerator": "tcg",
|
|
|
|
"port_map": []map[string]int{{
|
|
|
|
"main": 22,
|
|
|
|
"web": 8080,
|
|
|
|
}},
|
|
|
|
"args": []string{"-nodefconfig", "-nodefaults"},
|
|
|
|
"msg": "Qemu memory assignment out of bounds",
|
|
|
|
},
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
CPU: 500,
|
|
|
|
MemoryMB: -1,
|
|
|
|
Networks: []*structs.NetworkResource{
|
2017-10-02 20:16:48 +00:00
|
|
|
{
|
2017-09-27 04:07:48 +00:00
|
|
|
ReservedPorts: []structs.Port{{Label: "main", Value: 22000}, {Label: "web", Value: 80}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-03-24 07:47:23 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 04:07:48 +00:00
|
|
|
for _, task := range tasks {
|
|
|
|
ctx := testDriverContexts(t, task)
|
2018-09-24 18:37:45 +00:00
|
|
|
defer ctx.Destroy()
|
2017-09-27 04:07:48 +00:00
|
|
|
d := NewQemuDriver(ctx.DriverCtx)
|
|
|
|
|
|
|
|
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
|
|
|
|
t.Fatalf("Prestart faild: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := d.Start(ctx.ExecCtx, task)
|
|
|
|
if err == nil {
|
|
|
|
resp.Handle.Kill()
|
|
|
|
t.Fatalf("Should've failed")
|
|
|
|
}
|
2017-10-18 00:54:22 +00:00
|
|
|
|
2017-09-27 04:07:48 +00:00
|
|
|
msg := task.Config["msg"].(string)
|
|
|
|
if !strings.Contains(err.Error(), msg) {
|
|
|
|
t.Fatalf("Expecting '%v' in '%v'", msg, err)
|
|
|
|
}
|
2016-03-24 07:47:23 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-18 00:54:22 +00:00
|
|
|
|
2017-11-03 00:37:49 +00:00
|
|
|
func TestQemuDriverGetMonitorPathOldQemu(t *testing.T) {
|
|
|
|
task := &structs.Task{
|
|
|
|
Name: "linux",
|
|
|
|
Driver: "qemu",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"image_path": "linux-0.2.img",
|
|
|
|
"accelerator": "tcg",
|
|
|
|
"graceful_shutdown": true,
|
|
|
|
"port_map": []map[string]int{{
|
|
|
|
"main": 22,
|
|
|
|
"web": 8080,
|
|
|
|
}},
|
|
|
|
"args": []string{"-nodefconfig", "-nodefaults"},
|
|
|
|
},
|
|
|
|
KillTimeout: time.Duration(1 * time.Second),
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
CPU: 500,
|
|
|
|
MemoryMB: 512,
|
|
|
|
Networks: []*structs.NetworkResource{
|
|
|
|
{
|
|
|
|
ReservedPorts: []structs.Port{{Label: "main", Value: 22000}, {Label: "web", Value: 80}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := testDriverContexts(t, task)
|
2018-09-24 18:37:45 +00:00
|
|
|
defer ctx.Destroy()
|
2017-11-03 00:37:49 +00:00
|
|
|
|
|
|
|
// Simulate an older version of qemu which does not support long monitor socket paths
|
|
|
|
ctx.DriverCtx.node.Attributes[qemuDriverVersionAttr] = "2.0.0"
|
|
|
|
|
|
|
|
d := &QemuDriver{DriverContext: *ctx.DriverCtx}
|
|
|
|
|
2017-11-01 22:14:56 +00:00
|
|
|
shortPath := strings.Repeat("x", 10)
|
2017-11-03 00:37:49 +00:00
|
|
|
_, err := d.getMonitorPath(shortPath)
|
2017-10-18 00:54:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Should not have returned an error")
|
|
|
|
}
|
|
|
|
|
2017-11-03 00:37:49 +00:00
|
|
|
longPath := strings.Repeat("x", qemuLegacyMaxMonitorPathLen+100)
|
|
|
|
_, err = d.getMonitorPath(longPath)
|
2017-10-18 00:54:22 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Should have returned an error")
|
|
|
|
}
|
2017-11-03 00:37:49 +00:00
|
|
|
|
|
|
|
// Max length includes the '/' separator and socket name
|
|
|
|
maxLengthCount := qemuLegacyMaxMonitorPathLen - len(qemuMonitorSocketName) - 1
|
|
|
|
maxLengthLegacyPath := strings.Repeat("x", maxLengthCount)
|
|
|
|
_, err = d.getMonitorPath(maxLengthLegacyPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Should not have returned an error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQemuDriverGetMonitorPathNewQemu(t *testing.T) {
|
|
|
|
task := &structs.Task{
|
|
|
|
Name: "linux",
|
|
|
|
Driver: "qemu",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"image_path": "linux-0.2.img",
|
|
|
|
"accelerator": "tcg",
|
|
|
|
"graceful_shutdown": true,
|
|
|
|
"port_map": []map[string]int{{
|
|
|
|
"main": 22,
|
|
|
|
"web": 8080,
|
|
|
|
}},
|
|
|
|
"args": []string{"-nodefconfig", "-nodefaults"},
|
|
|
|
},
|
|
|
|
KillTimeout: time.Duration(1 * time.Second),
|
|
|
|
LogConfig: &structs.LogConfig{
|
|
|
|
MaxFiles: 10,
|
|
|
|
MaxFileSizeMB: 10,
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
CPU: 500,
|
|
|
|
MemoryMB: 512,
|
|
|
|
Networks: []*structs.NetworkResource{
|
|
|
|
{
|
|
|
|
ReservedPorts: []structs.Port{{Label: "main", Value: 22000}, {Label: "web", Value: 80}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := testDriverContexts(t, task)
|
2018-09-24 18:37:45 +00:00
|
|
|
defer ctx.Destroy()
|
2017-11-03 00:37:49 +00:00
|
|
|
|
|
|
|
// Simulate a version of qemu which supports long monitor socket paths
|
|
|
|
ctx.DriverCtx.node.Attributes[qemuDriverVersionAttr] = "2.99.99"
|
|
|
|
|
|
|
|
d := &QemuDriver{DriverContext: *ctx.DriverCtx}
|
|
|
|
|
|
|
|
shortPath := strings.Repeat("x", 10)
|
|
|
|
_, err := d.getMonitorPath(shortPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Should not have returned an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
longPath := strings.Repeat("x", qemuLegacyMaxMonitorPathLen+100)
|
|
|
|
_, err = d.getMonitorPath(longPath)
|
2017-10-18 00:54:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Should not have returned an error")
|
|
|
|
}
|
|
|
|
|
2017-11-03 00:37:49 +00:00
|
|
|
maxLengthCount := qemuLegacyMaxMonitorPathLen - len(qemuMonitorSocketName) - 1
|
|
|
|
maxLengthLegacyPath := strings.Repeat("x", maxLengthCount)
|
|
|
|
_, err = d.getMonitorPath(maxLengthLegacyPath)
|
2017-10-18 00:54:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Should not have returned an error")
|
|
|
|
}
|
|
|
|
}
|