Merge pull request #4108 from hashicorp/t-flaky-jobs-api

Fix flaky api/jobs tests based on index != 0
This commit is contained in:
Alex Dadgar 2018-04-04 10:31:40 -07:00 committed by GitHub
commit d96a5e02a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 45 deletions

View File

@ -24,9 +24,7 @@ func TestJobs_Register(t *testing.T) {
jobs := c.Jobs() jobs := c.Jobs()
// Listing jobs before registering returns nothing // Listing jobs before registering returns nothing
resp, qm, err := jobs.List(nil) resp, _, err := jobs.List(nil)
require.Nil(err)
assertQueryMeta(t, qm)
require.Emptyf(resp, "expected 0 jobs, got: %d", len(resp)) require.Emptyf(resp, "expected 0 jobs, got: %d", len(resp))
// Create a job and attempt to register it // Create a job and attempt to register it
@ -38,7 +36,8 @@ func TestJobs_Register(t *testing.T) {
assertWriteMeta(t, wm) assertWriteMeta(t, wm)
// Query the jobs back out again // Query the jobs back out again
resp, _, err = jobs.List(nil) resp, qm, err := jobs.List(nil)
assertQueryMeta(t, qm)
require.Nil(err) require.Nil(err)
// Check that we got the expected response // Check that we got the expected response
@ -640,69 +639,47 @@ func TestJobs_Canonicalize(t *testing.T) {
func TestJobs_EnforceRegister(t *testing.T) { func TestJobs_EnforceRegister(t *testing.T) {
t.Parallel() t.Parallel()
require := require.New(t)
c, s := makeClient(t, nil, nil) c, s := makeClient(t, nil, nil)
defer s.Stop() defer s.Stop()
jobs := c.Jobs() jobs := c.Jobs()
// Listing jobs before registering returns nothing // Listing jobs before registering returns nothing
resp, qm, err := jobs.List(nil) resp, _, err := jobs.List(nil)
if err != nil { require.Nil(err)
t.Fatalf("err: %s", err) require.Empty(resp)
}
assertQueryMeta(t, qm)
if n := len(resp); n != 0 {
t.Fatalf("expected 0 jobs, got: %d", n)
}
// Create a job and attempt to register it with an incorrect index. // Create a job and attempt to register it with an incorrect index.
job := testJob() job := testJob()
resp2, _, err := jobs.EnforceRegister(job, 10, nil) resp2, _, err := jobs.EnforceRegister(job, 10, nil)
if err == nil || !strings.Contains(err.Error(), RegisterEnforceIndexErrPrefix) { require.NotNil(err)
t.Fatalf("expected enforcement error: %v", err) require.Contains(err.Error(), RegisterEnforceIndexErrPrefix)
}
// Register // Register
resp2, wm, err := jobs.EnforceRegister(job, 0, nil) resp2, wm, err := jobs.EnforceRegister(job, 0, nil)
if err != nil { require.Nil(err)
t.Fatalf("err: %s", err) require.NotNil(resp2)
} require.NotZero(resp2.EvalID)
if resp2 == nil || resp2.EvalID == "" {
t.Fatalf("missing eval id")
}
assertWriteMeta(t, wm) assertWriteMeta(t, wm)
// Query the jobs back out again // Query the jobs back out again
resp, qm, err = jobs.List(nil) resp, qm, err := jobs.List(nil)
if err != nil { require.Nil(err)
t.Fatalf("err: %s", err) require.Len(resp, 1)
} require.Equal(*job.ID, resp[0].ID)
assertQueryMeta(t, qm) assertQueryMeta(t, qm)
// Check that we got the expected response
if len(resp) != 1 {
t.Fatalf("bad length: %d", len(resp))
}
if resp[0].ID != *job.ID {
t.Fatalf("bad: %#v", resp[0])
}
curIndex := resp[0].JobModifyIndex
// Fail at incorrect index // Fail at incorrect index
curIndex := resp[0].JobModifyIndex
resp2, _, err = jobs.EnforceRegister(job, 123456, nil) resp2, _, err = jobs.EnforceRegister(job, 123456, nil)
if err == nil || !strings.Contains(err.Error(), RegisterEnforceIndexErrPrefix) { require.NotNil(err)
t.Fatalf("expected enforcement error: %v", err) require.Contains(err.Error(), RegisterEnforceIndexErrPrefix)
}
// Works at correct index // Works at correct index
resp3, wm, err := jobs.EnforceRegister(job, curIndex, nil) resp3, wm, err := jobs.EnforceRegister(job, curIndex, nil)
if err != nil { require.Nil(err)
t.Fatalf("err: %s", err) require.NotNil(resp3)
} require.NotZero(resp3.EvalID)
if resp3 == nil || resp3.EvalID == "" {
t.Fatalf("missing eval id")
}
assertWriteMeta(t, wm) assertWriteMeta(t, wm)
} }