Skip to content

Commit

Permalink
add state store test to ensure PlacedCanaries is updated
Browse files Browse the repository at this point in the history
  • Loading branch information
drewbailey committed Jan 22, 2020
1 parent 625eaa2 commit 9d86337
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
33 changes: 33 additions & 0 deletions nomad/state/state_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6382,6 +6382,39 @@ func TestStateStore_UpsertDeploymentAllocHealth_BadAlloc_Nonexistent(t *testing.
}
}

// Test that a deployments PlacedCanaries is properly updated
func TestStateStore_UpsertDeploymentAlloc_Canaries(t *testing.T) {
t.Parallel()

state := testStateStore(t)

// Create a deployment
d1 := mock.Deployment()
require.NoError(t, state.UpsertDeployment(2, d1))

// Create a Job
job := mock.Job()
require.NoError(t, state.UpsertJob(3, job))

// Create alloc with canary status
a := mock.Alloc()
a.JobID = job.ID
a.DeploymentID = d1.ID
a.DeploymentStatus = &structs.AllocDeploymentStatus{
Healthy: helper.BoolToPtr(false),
Canary: true,
}
require.NoError(t, state.UpsertAllocs(4, []*structs.Allocation{a}))

// Pull the deployment from state
ws := memdb.NewWatchSet()
deploy, err := state.DeploymentByID(ws, d1.ID)
require.NoError(t, err)

// Ensure that PlacedCanaries is accurate
require.Equal(t, 1, len(deploy.TaskGroups[job.TaskGroups[0].Name].PlacedCanaries))
}

// Test that allocation health can't be set for an alloc with mismatched
// deployment ids
func TestStateStore_UpsertDeploymentAllocHealth_BadAlloc_MismatchDeployment(t *testing.T) {
Expand Down
24 changes: 18 additions & 6 deletions scheduler/generic_sched_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2029,17 +2029,29 @@ func TestServiceSched_JobModify_Canaries(t *testing.T) {
if plan.Deployment == nil {
t.Fatalf("bad: %#v", plan)
}
state, ok := plan.Deployment.TaskGroups[job.TaskGroups[0].Name]
if !ok {
t.Fatalf("bad: %#v", plan)
}

// Ensure local state was not altered in scheduler
staleState, ok := plan.Deployment.TaskGroups[job.TaskGroups[0].Name]
assert.True(t, ok)

assert.Equal(t, 0, len(staleState.PlacedCanaries))

ws := memdb.NewWatchSet()

// Grab the latest state
deploy, err := h.State.DeploymentByID(ws, plan.Deployment.ID)
assert.NoError(t, err)

state, ok := deploy.TaskGroups[job.TaskGroups[0].Name]
assert.True(t, ok)

if state.DesiredTotal != 10 && state.DesiredCanaries != desiredUpdates {
t.Fatalf("bad: %#v", state)
assert.Fail(t, "expected desired total 10, canaries to match updates")
}

// Assert the canaries were added to the placed list
if len(state.PlacedCanaries) != desiredUpdates {
t.Fatalf("bad: %#v", state)
assert.Fail(t, "expected PlacedCanaries to equal desiredUpdates", state)
}
}

Expand Down

0 comments on commit 9d86337

Please sign in to comment.