2019-08-14 22:02:00 +00:00
|
|
|
package allocrunner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-11-18 18:04:01 +00:00
|
|
|
"time"
|
2019-08-14 22:02:00 +00:00
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2019-08-14 22:02:00 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
|
2022-03-15 08:38:30 +00:00
|
|
|
regMock "github.com/hashicorp/nomad/client/serviceregistration/mock"
|
2022-03-21 09:29:57 +00:00
|
|
|
"github.com/hashicorp/nomad/client/serviceregistration/wrapper"
|
2019-11-18 18:04:01 +00:00
|
|
|
"github.com/hashicorp/nomad/client/taskenv"
|
2019-08-14 22:02:00 +00:00
|
|
|
agentconsul "github.com/hashicorp/nomad/command/agent/consul"
|
2022-08-17 16:26:34 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pointer"
|
2019-08-14 22:02:00 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ interfaces.RunnerPrerunHook = (*groupServiceHook)(nil)
|
|
|
|
var _ interfaces.RunnerUpdateHook = (*groupServiceHook)(nil)
|
|
|
|
var _ interfaces.RunnerPostrunHook = (*groupServiceHook)(nil)
|
2019-11-18 16:16:25 +00:00
|
|
|
var _ interfaces.RunnerPreKillHook = (*groupServiceHook)(nil)
|
2021-01-21 16:36:00 +00:00
|
|
|
var _ interfaces.RunnerTaskRestartHook = (*groupServiceHook)(nil)
|
2019-08-14 22:02:00 +00:00
|
|
|
|
|
|
|
// TestGroupServiceHook_NoGroupServices asserts calling group service hooks
|
|
|
|
// without group services does not error.
|
|
|
|
func TestGroupServiceHook_NoGroupServices(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-08-14 22:02:00 +00:00
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
2019-11-18 18:04:01 +00:00
|
|
|
alloc.Job.TaskGroups[0].Services = []*structs.Service{{
|
|
|
|
Name: "foo",
|
2022-03-21 09:29:57 +00:00
|
|
|
Provider: "consul",
|
2019-11-18 18:04:01 +00:00
|
|
|
PortLabel: "9999",
|
|
|
|
}}
|
2019-08-14 22:02:00 +00:00
|
|
|
logger := testlog.HCLogger(t)
|
2022-03-21 09:29:57 +00:00
|
|
|
|
|
|
|
consulMockClient := regMock.NewServiceRegistrationHandler(logger)
|
|
|
|
|
|
|
|
regWrapper := wrapper.NewHandlerWrapper(
|
|
|
|
logger,
|
|
|
|
consulMockClient,
|
|
|
|
regMock.NewServiceRegistrationHandler(logger))
|
2019-08-14 22:02:00 +00:00
|
|
|
|
2019-11-18 18:04:01 +00:00
|
|
|
h := newGroupServiceHook(groupServiceHookConfig{
|
2022-03-21 09:29:57 +00:00
|
|
|
alloc: alloc,
|
|
|
|
serviceRegWrapper: regWrapper,
|
|
|
|
restarter: agentconsul.NoopRestarter(),
|
|
|
|
taskEnvBuilder: taskenv.NewBuilder(mock.Node(), alloc, nil, alloc.Job.Region),
|
|
|
|
logger: logger,
|
2019-11-18 18:04:01 +00:00
|
|
|
})
|
2019-08-14 22:02:00 +00:00
|
|
|
require.NoError(t, h.Prerun())
|
|
|
|
|
|
|
|
req := &interfaces.RunnerUpdateRequest{Alloc: alloc}
|
|
|
|
require.NoError(t, h.Update(req))
|
|
|
|
|
|
|
|
require.NoError(t, h.Postrun())
|
|
|
|
|
2021-01-21 16:36:00 +00:00
|
|
|
require.NoError(t, h.PreTaskRestart())
|
|
|
|
|
2022-03-21 09:29:57 +00:00
|
|
|
ops := consulMockClient.GetOps()
|
2021-07-06 14:37:53 +00:00
|
|
|
require.Len(t, ops, 5)
|
2021-01-21 16:36:00 +00:00
|
|
|
require.Equal(t, "add", ops[0].Op) // Prerun
|
|
|
|
require.Equal(t, "update", ops[1].Op) // Update
|
2021-07-06 14:37:53 +00:00
|
|
|
require.Equal(t, "remove", ops[2].Op) // Postrun
|
|
|
|
require.Equal(t, "remove", ops[3].Op) // Restart -> preKill
|
|
|
|
require.Equal(t, "add", ops[4].Op) // Restart -> preRun
|
2019-08-14 22:02:00 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 21:30:25 +00:00
|
|
|
// TestGroupServiceHook_ShutdownDelayUpdate asserts calling group service hooks
|
|
|
|
// update updates the hooks delay value.
|
|
|
|
func TestGroupServiceHook_ShutdownDelayUpdate(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-04-05 21:30:25 +00:00
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
2022-08-17 16:26:34 +00:00
|
|
|
alloc.Job.TaskGroups[0].ShutdownDelay = pointer.Of(10 * time.Second)
|
2020-04-05 21:30:25 +00:00
|
|
|
|
|
|
|
logger := testlog.HCLogger(t)
|
2022-03-21 09:29:57 +00:00
|
|
|
consulMockClient := regMock.NewServiceRegistrationHandler(logger)
|
|
|
|
|
|
|
|
regWrapper := wrapper.NewHandlerWrapper(
|
|
|
|
logger,
|
|
|
|
consulMockClient,
|
|
|
|
regMock.NewServiceRegistrationHandler(logger),
|
|
|
|
)
|
2020-04-05 21:30:25 +00:00
|
|
|
|
|
|
|
h := newGroupServiceHook(groupServiceHookConfig{
|
2022-03-21 09:29:57 +00:00
|
|
|
alloc: alloc,
|
|
|
|
serviceRegWrapper: regWrapper,
|
|
|
|
restarter: agentconsul.NoopRestarter(),
|
|
|
|
taskEnvBuilder: taskenv.NewBuilder(mock.Node(), alloc, nil, alloc.Job.Region),
|
|
|
|
logger: logger,
|
2020-04-05 21:30:25 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, h.Prerun())
|
|
|
|
|
2020-04-06 15:33:04 +00:00
|
|
|
// Incease shutdown Delay
|
2022-08-17 16:26:34 +00:00
|
|
|
alloc.Job.TaskGroups[0].ShutdownDelay = pointer.Of(15 * time.Second)
|
2020-04-05 21:30:25 +00:00
|
|
|
req := &interfaces.RunnerUpdateRequest{Alloc: alloc}
|
|
|
|
require.NoError(t, h.Update(req))
|
|
|
|
|
|
|
|
// Assert that update updated the delay value
|
|
|
|
require.Equal(t, h.delay, 15*time.Second)
|
2020-04-06 15:33:04 +00:00
|
|
|
|
|
|
|
// Remove shutdown delay
|
|
|
|
alloc.Job.TaskGroups[0].ShutdownDelay = nil
|
|
|
|
req = &interfaces.RunnerUpdateRequest{Alloc: alloc}
|
|
|
|
require.NoError(t, h.Update(req))
|
|
|
|
|
|
|
|
// Assert that update updated the delay value
|
|
|
|
require.Equal(t, h.delay, 0*time.Second)
|
2020-04-05 21:30:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 22:02:00 +00:00
|
|
|
// TestGroupServiceHook_GroupServices asserts group service hooks with group
|
|
|
|
// services does not error.
|
|
|
|
func TestGroupServiceHook_GroupServices(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-08-14 22:02:00 +00:00
|
|
|
|
2019-09-03 15:43:38 +00:00
|
|
|
alloc := mock.ConnectAlloc()
|
2022-03-21 09:29:57 +00:00
|
|
|
alloc.Job.Canonicalize()
|
2019-08-14 22:02:00 +00:00
|
|
|
logger := testlog.HCLogger(t)
|
2022-03-21 09:29:57 +00:00
|
|
|
consulMockClient := regMock.NewServiceRegistrationHandler(logger)
|
|
|
|
|
|
|
|
regWrapper := wrapper.NewHandlerWrapper(
|
|
|
|
logger,
|
|
|
|
consulMockClient,
|
|
|
|
regMock.NewServiceRegistrationHandler(logger))
|
2019-08-14 22:02:00 +00:00
|
|
|
|
2019-11-18 18:04:01 +00:00
|
|
|
h := newGroupServiceHook(groupServiceHookConfig{
|
2022-03-21 09:29:57 +00:00
|
|
|
alloc: alloc,
|
|
|
|
serviceRegWrapper: regWrapper,
|
|
|
|
restarter: agentconsul.NoopRestarter(),
|
|
|
|
taskEnvBuilder: taskenv.NewBuilder(mock.Node(), alloc, nil, alloc.Job.Region),
|
|
|
|
logger: logger,
|
2019-11-18 18:04:01 +00:00
|
|
|
})
|
2019-08-14 22:02:00 +00:00
|
|
|
require.NoError(t, h.Prerun())
|
|
|
|
|
|
|
|
req := &interfaces.RunnerUpdateRequest{Alloc: alloc}
|
|
|
|
require.NoError(t, h.Update(req))
|
|
|
|
|
|
|
|
require.NoError(t, h.Postrun())
|
|
|
|
|
2021-01-21 16:36:00 +00:00
|
|
|
require.NoError(t, h.PreTaskRestart())
|
|
|
|
|
2022-03-21 09:29:57 +00:00
|
|
|
ops := consulMockClient.GetOps()
|
2021-07-06 14:37:53 +00:00
|
|
|
require.Len(t, ops, 5)
|
2021-01-21 16:36:00 +00:00
|
|
|
require.Equal(t, "add", ops[0].Op) // Prerun
|
|
|
|
require.Equal(t, "update", ops[1].Op) // Update
|
2021-07-06 14:37:53 +00:00
|
|
|
require.Equal(t, "remove", ops[2].Op) // Postrun
|
|
|
|
require.Equal(t, "remove", ops[3].Op) // Restart -> preKill
|
|
|
|
require.Equal(t, "add", ops[4].Op) // Restart -> preRun
|
2019-08-14 22:02:00 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 09:29:57 +00:00
|
|
|
// TestGroupServiceHook_GroupServices_Nomad asserts group service hooks with
|
|
|
|
// group services does not error when using the Nomad provider.
|
|
|
|
func TestGroupServiceHook_GroupServices_Nomad(t *testing.T) {
|
2022-03-24 07:45:13 +00:00
|
|
|
ci.Parallel(t)
|
2022-03-21 09:29:57 +00:00
|
|
|
|
|
|
|
// Create a mock alloc, and add a group service using provider Nomad.
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.Job.TaskGroups[0].Services = []*structs.Service{
|
|
|
|
{
|
|
|
|
Name: "nomad-provider-service",
|
|
|
|
Provider: structs.ServiceProviderNomad,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create our base objects and our subsequent wrapper.
|
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
consulMockClient := regMock.NewServiceRegistrationHandler(logger)
|
|
|
|
nomadMockClient := regMock.NewServiceRegistrationHandler(logger)
|
|
|
|
|
|
|
|
regWrapper := wrapper.NewHandlerWrapper(logger, consulMockClient, nomadMockClient)
|
|
|
|
|
|
|
|
h := newGroupServiceHook(groupServiceHookConfig{
|
|
|
|
alloc: alloc,
|
|
|
|
serviceRegWrapper: regWrapper,
|
|
|
|
restarter: agentconsul.NoopRestarter(),
|
|
|
|
taskEnvBuilder: taskenv.NewBuilder(mock.Node(), alloc, nil, alloc.Job.Region),
|
|
|
|
logger: logger,
|
|
|
|
})
|
|
|
|
require.NoError(t, h.Prerun())
|
|
|
|
|
|
|
|
// Trigger our hook requests.
|
|
|
|
req := &interfaces.RunnerUpdateRequest{Alloc: alloc}
|
|
|
|
require.NoError(t, h.Update(req))
|
|
|
|
require.NoError(t, h.Postrun())
|
|
|
|
require.NoError(t, h.PreTaskRestart())
|
|
|
|
|
|
|
|
// Ensure the Nomad mock provider has the expected operations.
|
|
|
|
ops := nomadMockClient.GetOps()
|
|
|
|
require.Len(t, ops, 5)
|
|
|
|
require.Equal(t, "add", ops[0].Op) // Prerun
|
|
|
|
require.Equal(t, "update", ops[1].Op) // Update
|
|
|
|
require.Equal(t, "remove", ops[2].Op) // Postrun
|
|
|
|
require.Equal(t, "remove", ops[3].Op) // Restart -> preKill
|
|
|
|
require.Equal(t, "add", ops[4].Op) // Restart -> preRun
|
|
|
|
|
|
|
|
// Ensure the Consul mock provider has zero operations.
|
|
|
|
require.Len(t, consulMockClient.GetOps(), 0)
|
|
|
|
}
|
|
|
|
|
2019-08-14 22:02:00 +00:00
|
|
|
// TestGroupServiceHook_Error asserts group service hooks with group
|
2021-01-21 16:36:00 +00:00
|
|
|
// services but no group network is handled gracefully.
|
2019-11-18 18:04:01 +00:00
|
|
|
func TestGroupServiceHook_NoNetwork(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-08-14 22:02:00 +00:00
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
2019-11-18 18:04:01 +00:00
|
|
|
alloc.Job.TaskGroups[0].Networks = []*structs.NetworkResource{}
|
2019-08-14 22:02:00 +00:00
|
|
|
tg := alloc.Job.LookupTaskGroup(alloc.TaskGroup)
|
|
|
|
tg.Services = []*structs.Service{
|
|
|
|
{
|
|
|
|
Name: "testconnect",
|
2022-03-21 09:29:57 +00:00
|
|
|
Provider: "consul",
|
2019-08-14 22:02:00 +00:00
|
|
|
PortLabel: "9999",
|
|
|
|
Connect: &structs.ConsulConnect{
|
|
|
|
SidecarService: &structs.ConsulSidecarService{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
|
2022-03-21 09:29:57 +00:00
|
|
|
consulMockClient := regMock.NewServiceRegistrationHandler(logger)
|
|
|
|
|
|
|
|
regWrapper := wrapper.NewHandlerWrapper(
|
|
|
|
logger,
|
|
|
|
consulMockClient,
|
|
|
|
regMock.NewServiceRegistrationHandler(logger))
|
2019-08-14 22:02:00 +00:00
|
|
|
|
2019-11-18 18:04:01 +00:00
|
|
|
h := newGroupServiceHook(groupServiceHookConfig{
|
2022-03-21 09:29:57 +00:00
|
|
|
alloc: alloc,
|
|
|
|
serviceRegWrapper: regWrapper,
|
|
|
|
restarter: agentconsul.NoopRestarter(),
|
|
|
|
taskEnvBuilder: taskenv.NewBuilder(mock.Node(), alloc, nil, alloc.Job.Region),
|
|
|
|
logger: logger,
|
2019-11-18 18:04:01 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, h.Prerun())
|
2019-08-14 22:02:00 +00:00
|
|
|
|
|
|
|
req := &interfaces.RunnerUpdateRequest{Alloc: alloc}
|
2019-11-18 18:04:01 +00:00
|
|
|
require.NoError(t, h.Update(req))
|
|
|
|
|
|
|
|
require.NoError(t, h.Postrun())
|
|
|
|
|
2021-01-21 16:36:00 +00:00
|
|
|
require.NoError(t, h.PreTaskRestart())
|
|
|
|
|
2022-03-21 09:29:57 +00:00
|
|
|
ops := consulMockClient.GetOps()
|
2021-07-06 14:37:53 +00:00
|
|
|
require.Len(t, ops, 5)
|
2021-01-21 16:36:00 +00:00
|
|
|
require.Equal(t, "add", ops[0].Op) // Prerun
|
|
|
|
require.Equal(t, "update", ops[1].Op) // Update
|
2021-07-06 14:37:53 +00:00
|
|
|
require.Equal(t, "remove", ops[2].Op) // Postrun
|
|
|
|
require.Equal(t, "remove", ops[3].Op) // Restart -> preKill
|
|
|
|
require.Equal(t, "add", ops[4].Op) // Restart -> preRun
|
2019-11-18 18:04:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGroupServiceHook_getWorkloadServices(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-11-18 18:04:01 +00:00
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.Job.TaskGroups[0].Networks = []*structs.NetworkResource{}
|
|
|
|
tg := alloc.Job.LookupTaskGroup(alloc.TaskGroup)
|
|
|
|
tg.Services = []*structs.Service{
|
|
|
|
{
|
|
|
|
Name: "testconnect",
|
|
|
|
PortLabel: "9999",
|
|
|
|
Connect: &structs.ConsulConnect{
|
|
|
|
SidecarService: &structs.ConsulSidecarService{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
|
2022-03-21 09:29:57 +00:00
|
|
|
consulMockClient := regMock.NewServiceRegistrationHandler(logger)
|
|
|
|
|
|
|
|
regWrapper := wrapper.NewHandlerWrapper(
|
|
|
|
logger,
|
|
|
|
consulMockClient,
|
|
|
|
regMock.NewServiceRegistrationHandler(logger))
|
2019-11-18 18:04:01 +00:00
|
|
|
|
|
|
|
h := newGroupServiceHook(groupServiceHookConfig{
|
2022-03-21 09:29:57 +00:00
|
|
|
alloc: alloc,
|
|
|
|
serviceRegWrapper: regWrapper,
|
|
|
|
restarter: agentconsul.NoopRestarter(),
|
|
|
|
taskEnvBuilder: taskenv.NewBuilder(mock.Node(), alloc, nil, alloc.Job.Region),
|
|
|
|
logger: logger,
|
2019-11-18 18:04:01 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
services := h.getWorkloadServices()
|
|
|
|
require.Len(t, services.Services, 1)
|
|
|
|
}
|