Repository: hashicorp/nomad
File: nomad/state/state_store_test.go (Go)

Code before:
	require.Equal(t, int64(1), summary.Children.Running)
	require.Equal(t, int64(0), summary.Children.Dead)

	require.False(t, watchFired(ws))
}

func TestStateStore_UpsertAlloc_NextAllocation(t *testing.T) {
	ci.Parallel(t)

	state := testStateStore(t)

	alloc1 := mock.Alloc()
	alloc2 := mock.Alloc()
	alloc2.PreviousAllocation = alloc1.ID

	err := state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{alloc1})
	require.NoError(t, err)

	err = state.UpsertAllocs(structs.MsgTypeTestSetup, 1001, []*structs.Allocation{alloc2})
	require.NoError(t, err)

	// upsertings alloc2 should update alloc1
	actual, err := state.AllocByID(nil, alloc1.ID)
	must.Eq(t, actual.NextAllocation, alloc2.ID)

	err = state.UpsertAllocs(structs.MsgTypeTestSetup, 1002, []*structs.Allocation{alloc1})
	require.NoError(t, err)

	// upserting alloc1 again should carry over the NextAllocation
	must.Eq(t, alloc1.NextAllocation, alloc2.ID)
}

func TestStateStore_UpdateAlloc_Alloc(t *testing.T) {
	ci.Parallel(t)

	state := testStateStore(t)
	alloc := mock.Alloc()

	if err := state.UpsertJob(structs.MsgTypeTestSetup, 999, nil, alloc.Job); err != nil {
		t.Fatalf("err: %v", err)
	}

	err := state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{alloc})
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	ws := memdb.NewWatchSet()
	summary, err := state.JobSummaryByID(ws, alloc.Namespace, alloc.JobID)
	if err != nil {
		t.Fatalf("err: %v", err)

Review comment (suggestion):
In the original issue the challenge was that we had two allocations in the `UpsertAlloc` call and the order mattered for messing up the `NextAllocation`. It might be nice if this test exercised that same condition.

Code after:
	require.Equal(t, int64(1), summary.Children.Running)
	require.Equal(t, int64(0), summary.Children.Dead)

	require.False(t, watchFired(ws))
}

func TestStateStore_UpsertAlloc_NextAllocation(t *testing.T) {
	ci.Parallel(t)

	state := testStateStore(t)

	alloc1 := mock.Alloc()
	alloc2 := mock.Alloc()
	alloc2.PreviousAllocation = alloc1.ID

	err := state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{alloc1, alloc2})
	must.NoError(t, err)

	// alloc1 should have the correct NextAllocation
	actual, err := state.AllocByID(nil, alloc1.ID)
	must.Eq(t, actual.NextAllocation, alloc2.ID)

	err = state.UpsertAllocs(structs.MsgTypeTestSetup, 1001, []*structs.Allocation{alloc2, alloc1})
	must.NoError(t, err)

	// upsert in a different order, alloc1 should still have the correct NextAllocation
	actual, err = state.AllocByID(nil, alloc1.ID)
	must.NoError(t, err)
	must.Eq(t, actual.NextAllocation, alloc2.ID)
}

func TestStateStore_UpdateAlloc_Alloc(t *testing.T) {
	ci.Parallel(t)

	state := testStateStore(t)
	alloc := mock.Alloc()

	if err := state.UpsertJob(structs.MsgTypeTestSetup, 999, nil, alloc.Job); err != nil {
		t.Fatalf("err: %v", err)
	}

	err := state.UpsertAllocs(structs.MsgTypeTestSetup, 1000, []*structs.Allocation{alloc})
	if err != nil {
		t.Fatalf("err: %v", err)
	}

	ws := memdb.NewWatchSet()
	summary, err := state.JobSummaryByID(ws, alloc.Namespace, alloc.JobID)
	if err != nil {
		t.Fatalf("err: %v", err)
	}