-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7600 from hashicorp/b-5767
tr/service_hook: prevent Update from running before Poststart finish
- Loading branch information
Showing
2 changed files
with
66 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,54 @@ | ||
package taskrunner | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/nomad/client/allocrunner/interfaces" | ||
"github.com/hashicorp/nomad/client/consul" | ||
"github.com/hashicorp/nomad/helper/testlog" | ||
"github.com/hashicorp/nomad/nomad/mock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// Statically assert the stats hook implements the expected interfaces | ||
var _ interfaces.TaskPoststartHook = (*serviceHook)(nil) | ||
var _ interfaces.TaskExitedHook = (*serviceHook)(nil) | ||
var _ interfaces.TaskPreKillHook = (*serviceHook)(nil) | ||
var _ interfaces.TaskUpdateHook = (*serviceHook)(nil) | ||
|
||
func TestUpdate_beforePoststart(t *testing.T) { | ||
alloc := mock.Alloc() | ||
logger := testlog.HCLogger(t) | ||
c := consul.NewMockConsulServiceClient(t, logger) | ||
|
||
hook := newServiceHook(serviceHookConfig{ | ||
alloc: alloc, | ||
task: alloc.LookupTask("web"), | ||
consul: c, | ||
logger: logger, | ||
}) | ||
require.NoError(t, hook.Update(context.Background(), &interfaces.TaskUpdateRequest{Alloc: alloc}, &interfaces.TaskUpdateResponse{})) | ||
require.Len(t, c.GetOps(), 0) | ||
require.NoError(t, hook.Poststart(context.Background(), &interfaces.TaskPoststartRequest{}, &interfaces.TaskPoststartResponse{})) | ||
require.Len(t, c.GetOps(), 1) | ||
require.NoError(t, hook.Update(context.Background(), &interfaces.TaskUpdateRequest{Alloc: alloc}, &interfaces.TaskUpdateResponse{})) | ||
require.Len(t, c.GetOps(), 2) | ||
|
||
// When a task exits it could be restarted with new driver info | ||
// so Update should again wait on Poststart. | ||
|
||
require.NoError(t, hook.Exited(context.Background(), &interfaces.TaskExitedRequest{}, &interfaces.TaskExitedResponse{})) | ||
require.Len(t, c.GetOps(), 4) | ||
require.NoError(t, hook.Update(context.Background(), &interfaces.TaskUpdateRequest{Alloc: alloc}, &interfaces.TaskUpdateResponse{})) | ||
require.Len(t, c.GetOps(), 4) | ||
require.NoError(t, hook.Poststart(context.Background(), &interfaces.TaskPoststartRequest{}, &interfaces.TaskPoststartResponse{})) | ||
require.Len(t, c.GetOps(), 5) | ||
require.NoError(t, hook.Update(context.Background(), &interfaces.TaskUpdateRequest{Alloc: alloc}, &interfaces.TaskUpdateResponse{})) | ||
require.Len(t, c.GetOps(), 6) | ||
require.NoError(t, hook.PreKilling(context.Background(), &interfaces.TaskPreKillRequest{}, &interfaces.TaskPreKillResponse{})) | ||
require.Len(t, c.GetOps(), 8) | ||
require.NoError(t, hook.Update(context.Background(), &interfaces.TaskUpdateRequest{Alloc: alloc}, &interfaces.TaskUpdateResponse{})) | ||
require.Len(t, c.GetOps(), 8) | ||
|
||
} |