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