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