github.com/maier/nomad@v0.4.1-0.20161110003312-a9e3d0b8549d/client/driver/qemu_test.go (about) 1 package driver 2 3 import ( 4 "fmt" 5 "path/filepath" 6 "strings" 7 "syscall" 8 "testing" 9 10 "github.com/hashicorp/nomad/client/config" 11 "github.com/hashicorp/nomad/nomad/structs" 12 13 ctestutils "github.com/hashicorp/nomad/client/testutil" 14 ) 15 16 // The fingerprinter test should always pass, even if QEMU is not installed. 17 func TestQemuDriver_Fingerprint(t *testing.T) { 18 ctestutils.QemuCompatible(t) 19 task := &structs.Task{ 20 Name: "foo", 21 Resources: structs.DefaultResources(), 22 } 23 driverCtx, execCtx := testDriverContexts(task) 24 defer execCtx.AllocDir.Destroy() 25 d := NewQemuDriver(driverCtx) 26 node := &structs.Node{ 27 Attributes: make(map[string]string), 28 } 29 apply, err := d.Fingerprint(&config.Config{}, node) 30 if err != nil { 31 t.Fatalf("err: %v", err) 32 } 33 if !apply { 34 t.Fatalf("should apply") 35 } 36 if node.Attributes["driver.qemu"] == "" { 37 t.Fatalf("Missing Qemu driver") 38 } 39 if node.Attributes["driver.qemu.version"] == "" { 40 t.Fatalf("Missing Qemu driver version") 41 } 42 } 43 44 func TestQemuDriver_StartOpen_Wait(t *testing.T) { 45 ctestutils.QemuCompatible(t) 46 task := &structs.Task{ 47 Name: "linux", 48 Config: map[string]interface{}{ 49 "image_path": "linux-0.2.img", 50 "accelerator": "tcg", 51 "port_map": []map[string]int{{ 52 "main": 22, 53 "web": 8080, 54 }}, 55 "args": []string{"-nodefconfig", "-nodefaults"}, 56 }, 57 LogConfig: &structs.LogConfig{ 58 MaxFiles: 10, 59 MaxFileSizeMB: 10, 60 }, 61 Resources: &structs.Resources{ 62 CPU: 500, 63 MemoryMB: 512, 64 Networks: []*structs.NetworkResource{ 65 &structs.NetworkResource{ 66 ReservedPorts: []structs.Port{{"main", 22000}, {"web", 80}}, 67 }, 68 }, 69 }, 70 } 71 72 driverCtx, execCtx := testDriverContexts(task) 73 defer execCtx.AllocDir.Destroy() 74 d := NewQemuDriver(driverCtx) 75 76 // Copy the test image into the task's directory 77 dst, _ := execCtx.AllocDir.TaskDirs[task.Name] 78 copyFile("./test-resources/qemu/linux-0.2.img", filepath.Join(dst, "linux-0.2.img"), t) 79 80 handle, err := d.Start(execCtx, task) 81 if err != nil { 82 t.Fatalf("err: %v", err) 83 } 84 if handle == nil { 85 t.Fatalf("missing handle") 86 } 87 88 // Ensure that sending a Signal returns an error 89 if err := handle.Signal(syscall.SIGINT); err == nil { 90 t.Fatalf("Expect an error when signalling") 91 } 92 93 // Attempt to open 94 handle2, err := d.Open(execCtx, handle.ID()) 95 if err != nil { 96 t.Fatalf("err: %v", err) 97 } 98 if handle2 == nil { 99 t.Fatalf("missing handle") 100 } 101 102 // Clean up 103 if err := handle.Kill(); err != nil { 104 fmt.Printf("\nError killing Qemu test: %s", err) 105 } 106 } 107 108 func TestQemuDriverUser(t *testing.T) { 109 ctestutils.QemuCompatible(t) 110 task := &structs.Task{ 111 Name: "linux", 112 User: "alice", 113 Config: map[string]interface{}{ 114 "image_path": "linux-0.2.img", 115 "accelerator": "tcg", 116 "port_map": []map[string]int{{ 117 "main": 22, 118 "web": 8080, 119 }}, 120 "args": []string{"-nodefconfig", "-nodefaults"}, 121 }, 122 LogConfig: &structs.LogConfig{ 123 MaxFiles: 10, 124 MaxFileSizeMB: 10, 125 }, 126 Resources: &structs.Resources{ 127 CPU: 500, 128 MemoryMB: 512, 129 Networks: []*structs.NetworkResource{ 130 &structs.NetworkResource{ 131 ReservedPorts: []structs.Port{{"main", 22000}, {"web", 80}}, 132 }, 133 }, 134 }, 135 } 136 137 driverCtx, execCtx := testDriverContexts(task) 138 defer execCtx.AllocDir.Destroy() 139 d := NewQemuDriver(driverCtx) 140 141 handle, err := d.Start(execCtx, task) 142 if err == nil { 143 handle.Kill() 144 t.Fatalf("Should've failed") 145 } 146 msg := "unknown user alice" 147 if !strings.Contains(err.Error(), msg) { 148 t.Fatalf("Expecting '%v' in '%v'", msg, err) 149 } 150 }