Merge pull request #3288 from simar7/qemu-improvements

qemu: Add bound checks for memory assignment
This commit is contained in:
Michael Schurter 2017-10-02 14:47:05 -07:00 committed by GitHub
commit f788974f8a
2 changed files with 51 additions and 16 deletions

View File

@ -170,7 +170,10 @@ func (d *QemuDriver) Start(ctx *ExecContext, task *structs.Task) (*StartResponse
if d.driverConfig.Accelerator != "" {
accelerator = d.driverConfig.Accelerator
}
// TODO: Check a lower bounds, e.g. the default 128 of Qemu
if task.Resources.MemoryMB < 128 || task.Resources.MemoryMB > 4000000 {
return nil, fmt.Errorf("Qemu memory assignment out of bounds")
}
mem := fmt.Sprintf("%dM", task.Resources.MemoryMB)
absPath, err := GetAbsolutePath("qemu-system-x86_64")

View File

@ -121,7 +121,7 @@ func TestQemuDriverUser(t *testing.T) {
t.Parallel()
}
ctestutils.QemuCompatible(t)
task := &structs.Task{
tasks := []*structs.Task{{
Name: "linux",
Driver: "qemu",
User: "alice",
@ -133,6 +133,7 @@ func TestQemuDriverUser(t *testing.T) {
"web": 8080,
}},
"args": []string{"-nodefconfig", "-nodefaults"},
"msg": "unknown user alice",
},
LogConfig: &structs.LogConfig{
MaxFiles: 10,
@ -147,23 +148,54 @@ func TestQemuDriverUser(t *testing.T) {
},
},
},
},
{
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{
{
ReservedPorts: []structs.Port{{Label: "main", Value: 22000}, {Label: "web", Value: 80}},
},
},
},
},
}
ctx := testDriverContexts(t, task)
defer ctx.AllocDir.Destroy()
d := NewQemuDriver(ctx.DriverCtx)
for _, task := range tasks {
ctx := testDriverContexts(t, task)
defer ctx.AllocDir.Destroy()
d := NewQemuDriver(ctx.DriverCtx)
if _, err := d.Prestart(ctx.ExecCtx, task); err != nil {
t.Fatalf("Prestart faild: %v", err)
}
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")
}
msg := "unknown user alice"
if !strings.Contains(err.Error(), msg) {
t.Fatalf("Expecting '%v' in '%v'", msg, err)
resp, err := d.Start(ctx.ExecCtx, task)
if err == nil {
resp.Handle.Kill()
t.Fatalf("Should've failed")
}
msg := task.Config["msg"].(string)
if !strings.Contains(err.Error(), msg) {
t.Fatalf("Expecting '%v' in '%v'", msg, err)
}
}
}