github.com/kardianos/nomad@v0.1.3-0.20151022182107-b13df73ee850/client/driver/rkt_test.go (about) 1 package driver 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "testing" 9 "time" 10 11 "github.com/hashicorp/nomad/client/allocdir" 12 "github.com/hashicorp/nomad/client/config" 13 "github.com/hashicorp/nomad/nomad/structs" 14 15 ctestutils "github.com/hashicorp/nomad/client/testutil" 16 ) 17 18 func TestRktVersionRegex(t *testing.T) { 19 input_rkt := "rkt version 0.8.1" 20 input_appc := "appc version 1.2.0" 21 expected_rkt := "0.8.1" 22 expected_appc := "1.2.0" 23 rktMatches := reRktVersion.FindStringSubmatch(input_rkt) 24 appcMatches := reAppcVersion.FindStringSubmatch(input_appc) 25 if rktMatches[1] != expected_rkt { 26 fmt.Printf("Test failed; got %q; want %q\n", rktMatches[1], expected_rkt) 27 } 28 if appcMatches[1] != expected_appc { 29 fmt.Printf("Test failed; got %q; want %q\n", appcMatches[1], expected_appc) 30 } 31 } 32 33 func TestRktDriver_Handle(t *testing.T) { 34 h := &rktHandle{ 35 proc: &os.Process{Pid: 123}, 36 image: "foo", 37 doneCh: make(chan struct{}), 38 waitCh: make(chan error, 1), 39 } 40 41 actual := h.ID() 42 expected := `Rkt:{"Pid":123,"Image":"foo"}` 43 if actual != expected { 44 t.Errorf("Expected `%s`, found `%s`", expected, actual) 45 } 46 } 47 48 // The fingerprinter test should always pass, even if rkt is not installed. 49 func TestRktDriver_Fingerprint(t *testing.T) { 50 ctestutils.RktCompatible(t) 51 d := NewRktDriver(testDriverContext("")) 52 node := &structs.Node{ 53 Attributes: make(map[string]string), 54 } 55 apply, err := d.Fingerprint(&config.Config{}, node) 56 if err != nil { 57 t.Fatalf("err: %v", err) 58 } 59 if !apply { 60 t.Fatalf("should apply") 61 } 62 if node.Attributes["driver.rkt"] != "1" { 63 t.Fatalf("Missing Rkt driver") 64 } 65 if node.Attributes["driver.rkt.version"] == "" { 66 t.Fatalf("Missing Rkt driver version") 67 } 68 if node.Attributes["driver.rkt.appc.version"] == "" { 69 t.Fatalf("Missing appc version for the Rkt driver") 70 } 71 } 72 73 func TestRktDriver_Start(t *testing.T) { 74 ctestutils.RktCompatible(t) 75 // TODO: use test server to load from a fixture 76 task := &structs.Task{ 77 Name: "etcd", 78 Config: map[string]string{ 79 "trust_prefix": "coreos.com/etcd", 80 "image": "coreos.com/etcd:v2.0.4", 81 "command": "/etcd", 82 }, 83 } 84 85 driverCtx := testDriverContext(task.Name) 86 ctx := testDriverExecContext(task, driverCtx) 87 d := NewRktDriver(driverCtx) 88 defer ctx.AllocDir.Destroy() 89 90 handle, err := d.Start(ctx, task) 91 if err != nil { 92 t.Fatalf("err: %v", err) 93 } 94 if handle == nil { 95 t.Fatalf("missing handle") 96 } 97 98 // Attempt to open 99 handle2, err := d.Open(ctx, handle.ID()) 100 if err != nil { 101 t.Fatalf("err: %v", err) 102 } 103 if handle2 == nil { 104 t.Fatalf("missing handle") 105 } 106 107 // Clean up 108 if err := handle.Kill(); err != nil { 109 fmt.Printf("\nError killing Rkt test: %s", err) 110 } 111 } 112 113 func TestRktDriver_Start_Wait(t *testing.T) { 114 ctestutils.RktCompatible(t) 115 task := &structs.Task{ 116 Name: "etcd", 117 Config: map[string]string{ 118 "trust_prefix": "coreos.com/etcd", 119 "image": "coreos.com/etcd:v2.0.4", 120 "command": "/etcd", 121 "args": "--version", 122 }, 123 } 124 125 driverCtx := testDriverContext(task.Name) 126 ctx := testDriverExecContext(task, driverCtx) 127 d := NewRktDriver(driverCtx) 128 defer ctx.AllocDir.Destroy() 129 130 handle, err := d.Start(ctx, task) 131 if err != nil { 132 t.Fatalf("err: %v", err) 133 } 134 if handle == nil { 135 t.Fatalf("missing handle") 136 } 137 defer handle.Kill() 138 139 // Update should be a no-op 140 err = handle.Update(task) 141 if err != nil { 142 t.Fatalf("err: %v", err) 143 } 144 145 select { 146 case err := <-handle.WaitCh(): 147 if err != nil { 148 t.Fatalf("err: %v", err) 149 } 150 case <-time.After(5 * time.Second): 151 t.Fatalf("timeout") 152 } 153 } 154 155 func TestRktDriver_Start_Wait_Skip_Trust(t *testing.T) { 156 ctestutils.RktCompatible(t) 157 task := &structs.Task{ 158 Name: "etcd", 159 Config: map[string]string{ 160 "image": "coreos.com/etcd:v2.0.4", 161 "command": "/etcd", 162 "args": "--version", 163 }, 164 } 165 166 driverCtx := testDriverContext(task.Name) 167 ctx := testDriverExecContext(task, driverCtx) 168 d := NewRktDriver(driverCtx) 169 defer ctx.AllocDir.Destroy() 170 171 handle, err := d.Start(ctx, task) 172 if err != nil { 173 t.Fatalf("err: %v", err) 174 } 175 if handle == nil { 176 t.Fatalf("missing handle") 177 } 178 defer handle.Kill() 179 180 // Update should be a no-op 181 err = handle.Update(task) 182 if err != nil { 183 t.Fatalf("err: %v", err) 184 } 185 186 select { 187 case err := <-handle.WaitCh(): 188 if err != nil { 189 t.Fatalf("err: %v", err) 190 } 191 case <-time.After(5 * time.Second): 192 t.Fatalf("timeout") 193 } 194 } 195 196 func TestRktDriver_Start_Wait_Logs(t *testing.T) { 197 ctestutils.RktCompatible(t) 198 task := &structs.Task{ 199 Name: "etcd", 200 Config: map[string]string{ 201 "trust_prefix": "coreos.com/etcd", 202 "image": "coreos.com/etcd:v2.0.4", 203 "command": "/etcd", 204 "args": "--version", 205 }, 206 } 207 208 driverCtx := testDriverContext(task.Name) 209 ctx := testDriverExecContext(task, driverCtx) 210 d := NewRktDriver(driverCtx) 211 defer ctx.AllocDir.Destroy() 212 213 handle, err := d.Start(ctx, task) 214 if err != nil { 215 t.Fatalf("err: %v", err) 216 } 217 if handle == nil { 218 t.Fatalf("missing handle") 219 } 220 defer handle.Kill() 221 222 select { 223 case err := <-handle.WaitCh(): 224 if err != nil { 225 t.Fatalf("err: %v", err) 226 } 227 case <-time.After(5 * time.Second): 228 t.Fatalf("timeout") 229 } 230 231 taskDir, ok := ctx.AllocDir.TaskDirs[task.Name] 232 if !ok { 233 t.Fatalf("Could not find task directory for task: %v", task) 234 } 235 stdout := filepath.Join(taskDir, allocdir.TaskLocal, fmt.Sprintf("%v.stdout", task.Name)) 236 data, err := ioutil.ReadFile(stdout) 237 if err != nil { 238 t.Fatalf("Failed to read tasks stdout: %v", err) 239 } 240 241 if len(data) == 0 { 242 t.Fatal("Task's stdout is empty") 243 } 244 }