github.com/huiliang/nomad@v0.2.1-0.20151124023127-7a8b664699ff/api/jobs_test.go (about) 1 package api 2 3 import ( 4 "reflect" 5 "sort" 6 "strings" 7 "testing" 8 ) 9 10 func TestJobs_Register(t *testing.T) { 11 c, s := makeClient(t, nil, nil) 12 defer s.Stop() 13 jobs := c.Jobs() 14 15 // Listing jobs before registering returns nothing 16 resp, qm, err := jobs.List(nil) 17 if err != nil { 18 t.Fatalf("err: %s", err) 19 } 20 if qm.LastIndex != 0 { 21 t.Fatalf("bad index: %d", qm.LastIndex) 22 } 23 if n := len(resp); n != 0 { 24 t.Fatalf("expected 0 jobs, got: %d", n) 25 } 26 27 // Create a job and attempt to register it 28 job := testJob() 29 eval, wm, err := jobs.Register(job, nil) 30 if err != nil { 31 t.Fatalf("err: %s", err) 32 } 33 if eval == "" { 34 t.Fatalf("missing eval id") 35 } 36 assertWriteMeta(t, wm) 37 38 // Query the jobs back out again 39 resp, qm, err = jobs.List(nil) 40 if err != nil { 41 t.Fatalf("err: %s", err) 42 } 43 assertQueryMeta(t, qm) 44 45 // Check that we got the expected response 46 if len(resp) != 1 || resp[0].ID != job.ID { 47 t.Fatalf("bad: %#v", resp[0]) 48 } 49 } 50 51 func TestJobs_Info(t *testing.T) { 52 c, s := makeClient(t, nil, nil) 53 defer s.Stop() 54 jobs := c.Jobs() 55 56 // Trying to retrieve a job by ID before it exists 57 // returns an error 58 _, _, err := jobs.Info("job1", nil) 59 if err == nil || !strings.Contains(err.Error(), "not found") { 60 t.Fatalf("expected not found error, got: %#v", err) 61 } 62 63 // Register the job 64 job := testJob() 65 _, wm, err := jobs.Register(job, nil) 66 if err != nil { 67 t.Fatalf("err: %s", err) 68 } 69 assertWriteMeta(t, wm) 70 71 // Query the job again and ensure it exists 72 result, qm, err := jobs.Info("job1", nil) 73 if err != nil { 74 t.Fatalf("err: %s", err) 75 } 76 assertQueryMeta(t, qm) 77 78 // Check that the result is what we expect 79 if result == nil || result.ID != job.ID { 80 t.Fatalf("expect: %#v, got: %#v", job, result) 81 } 82 } 83 84 func TestJobs_Allocations(t *testing.T) { 85 c, s := makeClient(t, nil, nil) 86 defer s.Stop() 87 jobs := c.Jobs() 88 89 // Looking up by a non-existent job returns nothing 90 allocs, qm, err := jobs.Allocations("job1", nil) 91 if err != nil { 92 t.Fatalf("err: %s", err) 93 } 94 if qm.LastIndex != 0 { 95 t.Fatalf("bad index: %d", qm.LastIndex) 96 } 97 if n := len(allocs); n != 0 { 98 t.Fatalf("expected 0 allocs, got: %d", n) 99 } 100 101 // TODO: do something here to create some allocations for 102 // an existing job, lookup again. 103 } 104 105 func TestJobs_Evaluations(t *testing.T) { 106 c, s := makeClient(t, nil, nil) 107 defer s.Stop() 108 jobs := c.Jobs() 109 110 // Looking up by a non-existent job ID returns nothing 111 evals, qm, err := jobs.Evaluations("job1", nil) 112 if err != nil { 113 t.Fatalf("err: %s", err) 114 } 115 if qm.LastIndex != 0 { 116 t.Fatalf("bad index: %d", qm.LastIndex) 117 } 118 if n := len(evals); n != 0 { 119 t.Fatalf("expected 0 evals, got: %d", n) 120 } 121 122 // Insert a job. This also creates an evaluation so we should 123 // be able to query that out after. 124 job := testJob() 125 evalID, wm, err := jobs.Register(job, nil) 126 if err != nil { 127 t.Fatalf("err: %s", err) 128 } 129 assertWriteMeta(t, wm) 130 131 // Look up the evaluations again. 132 evals, qm, err = jobs.Evaluations("job1", nil) 133 if err != nil { 134 t.Fatalf("err: %s", err) 135 } 136 assertQueryMeta(t, qm) 137 138 // Check that we got the evals back 139 if n := len(evals); n == 0 || evals[0].ID != evalID { 140 t.Fatalf("expected 1 eval (%s), got: %#v", evalID, evals) 141 } 142 } 143 144 func TestJobs_Deregister(t *testing.T) { 145 c, s := makeClient(t, nil, nil) 146 defer s.Stop() 147 jobs := c.Jobs() 148 149 // Register a new job 150 job := testJob() 151 _, wm, err := jobs.Register(job, nil) 152 if err != nil { 153 t.Fatalf("err: %s", err) 154 } 155 assertWriteMeta(t, wm) 156 157 // Attempting delete on non-existing job does not error 158 _, wm2, err := jobs.Deregister("nope", nil) 159 if err != nil { 160 t.Fatalf("err: %s", err) 161 } 162 assertWriteMeta(t, wm2) 163 164 // Deleting an existing job works 165 evalID, wm3, err := jobs.Deregister("job1", nil) 166 if err != nil { 167 t.Fatalf("err: %s", err) 168 } 169 assertWriteMeta(t, wm3) 170 if evalID == "" { 171 t.Fatalf("missing eval ID") 172 } 173 174 // Check that the job is really gone 175 result, qm, err := jobs.List(nil) 176 if err != nil { 177 t.Fatalf("err: %s", err) 178 } 179 assertQueryMeta(t, qm) 180 if n := len(result); n != 0 { 181 t.Fatalf("expected 0 jobs, got: %d", n) 182 } 183 } 184 185 func TestJobs_ForceEvaluate(t *testing.T) { 186 c, s := makeClient(t, nil, nil) 187 defer s.Stop() 188 jobs := c.Jobs() 189 190 // Force-eval on a non-existent job fails 191 _, _, err := jobs.ForceEvaluate("job1", nil) 192 if err == nil || !strings.Contains(err.Error(), "not found") { 193 t.Fatalf("expected not found error, got: %#v", err) 194 } 195 196 // Create a new job 197 _, wm, err := jobs.Register(testJob(), nil) 198 if err != nil { 199 t.Fatalf("err: %s", err) 200 } 201 assertWriteMeta(t, wm) 202 203 // Try force-eval again 204 evalID, wm, err := jobs.ForceEvaluate("job1", nil) 205 if err != nil { 206 t.Fatalf("err: %s", err) 207 } 208 assertWriteMeta(t, wm) 209 210 // Retrieve the evals and see if we get a matching one 211 evals, qm, err := jobs.Evaluations("job1", nil) 212 if err != nil { 213 t.Fatalf("err: %s", err) 214 } 215 assertQueryMeta(t, qm) 216 for _, eval := range evals { 217 if eval.ID == evalID { 218 return 219 } 220 } 221 t.Fatalf("evaluation %q missing", evalID) 222 } 223 224 func TestJobs_NewBatchJob(t *testing.T) { 225 job := NewBatchJob("job1", "myjob", "region1", 5) 226 expect := &Job{ 227 Region: "region1", 228 ID: "job1", 229 Name: "myjob", 230 Type: JobTypeBatch, 231 Priority: 5, 232 } 233 if !reflect.DeepEqual(job, expect) { 234 t.Fatalf("expect: %#v, got: %#v", expect, job) 235 } 236 } 237 238 func TestJobs_NewServiceJob(t *testing.T) { 239 job := NewServiceJob("job1", "myjob", "region1", 5) 240 expect := &Job{ 241 Region: "region1", 242 ID: "job1", 243 Name: "myjob", 244 Type: JobTypeService, 245 Priority: 5, 246 } 247 if !reflect.DeepEqual(job, expect) { 248 t.Fatalf("expect: %#v, got: %#v", expect, job) 249 } 250 } 251 252 func TestJobs_SetMeta(t *testing.T) { 253 job := &Job{Meta: nil} 254 255 // Initializes a nil map 256 out := job.SetMeta("foo", "bar") 257 if job.Meta == nil { 258 t.Fatalf("should initialize metadata") 259 } 260 261 // Check that the job was returned 262 if job != out { 263 t.Fatalf("expect: %#v, got: %#v", job, out) 264 } 265 266 // Setting another pair is additive 267 job.SetMeta("baz", "zip") 268 expect := map[string]string{"foo": "bar", "baz": "zip"} 269 if !reflect.DeepEqual(job.Meta, expect) { 270 t.Fatalf("expect: %#v, got: %#v", expect, job.Meta) 271 } 272 } 273 274 func TestJobs_Constrain(t *testing.T) { 275 job := &Job{Constraints: nil} 276 277 // Create and add a constraint 278 out := job.Constrain(NewConstraint("kernel.name", "=", "darwin")) 279 if n := len(job.Constraints); n != 1 { 280 t.Fatalf("expected 1 constraint, got: %d", n) 281 } 282 283 // Check that the job was returned 284 if job != out { 285 t.Fatalf("expect: %#v, got: %#v", job, out) 286 } 287 288 // Adding another constraint preserves the original 289 job.Constrain(NewConstraint("memory.totalbytes", ">=", "128000000")) 290 expect := []*Constraint{ 291 &Constraint{ 292 LTarget: "kernel.name", 293 RTarget: "darwin", 294 Operand: "=", 295 }, 296 &Constraint{ 297 LTarget: "memory.totalbytes", 298 RTarget: "128000000", 299 Operand: ">=", 300 }, 301 } 302 if !reflect.DeepEqual(job.Constraints, expect) { 303 t.Fatalf("expect: %#v, got: %#v", expect, job.Constraints) 304 } 305 } 306 307 func TestJobs_Sort(t *testing.T) { 308 jobs := []*JobListStub{ 309 &JobListStub{ID: "job2"}, 310 &JobListStub{ID: "job0"}, 311 &JobListStub{ID: "job1"}, 312 } 313 sort.Sort(JobIDSort(jobs)) 314 315 expect := []*JobListStub{ 316 &JobListStub{ID: "job0"}, 317 &JobListStub{ID: "job1"}, 318 &JobListStub{ID: "job2"}, 319 } 320 if !reflect.DeepEqual(jobs, expect) { 321 t.Fatalf("\n\n%#v\n\n%#v", jobs, expect) 322 } 323 }