2019-01-09 21:37:08 +00:00
|
|
|
package taskrunner
|
|
|
|
|
|
|
|
import (
|
2020-04-02 16:17:36 +00:00
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2019-02-12 23:28:13 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
|
2020-04-02 16:17:36 +00:00
|
|
|
"github.com/hashicorp/nomad/client/consul"
|
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-01-09 21:37:08 +00:00
|
|
|
)
|
|
|
|
|
2019-02-12 23:28:13 +00:00
|
|
|
// 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)
|
2020-04-02 16:17:36 +00:00
|
|
|
|
|
|
|
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)
|
2020-04-02 16:48:19 +00:00
|
|
|
|
|
|
|
// 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)
|
2020-04-06 20:05:36 +00:00
|
|
|
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)
|
2020-04-02 16:48:19 +00:00
|
|
|
|
2020-04-02 16:17:36 +00:00
|
|
|
}
|