2018-10-04 23:22:01 +00:00
|
|
|
package allocrunner
|
2018-09-12 16:36:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-09-14 00:25:01 +00:00
|
|
|
consulapi "github.com/hashicorp/consul/api"
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2018-10-04 23:22:01 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
|
2022-03-15 08:38:30 +00:00
|
|
|
"github.com/hashicorp/nomad/client/serviceregistration"
|
|
|
|
regMock "github.com/hashicorp/nomad/client/serviceregistration/mock"
|
2018-09-12 16:36:04 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2023-03-10 19:43:31 +00:00
|
|
|
"github.com/hashicorp/nomad/client/taskenv"
|
2018-09-12 16:36:04 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2018-09-14 00:25:01 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2018-09-12 16:36:04 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2018-09-14 00:25:01 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-09-12 16:36:04 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// statically assert health hook implements the expected interfaces
|
|
|
|
var _ interfaces.RunnerPrerunHook = (*allocHealthWatcherHook)(nil)
|
|
|
|
var _ interfaces.RunnerUpdateHook = (*allocHealthWatcherHook)(nil)
|
2018-12-06 20:30:31 +00:00
|
|
|
var _ interfaces.RunnerPostrunHook = (*allocHealthWatcherHook)(nil)
|
2018-11-14 18:29:07 +00:00
|
|
|
var _ interfaces.ShutdownHook = (*allocHealthWatcherHook)(nil)
|
2018-09-12 16:36:04 +00:00
|
|
|
|
2018-09-14 00:25:01 +00:00
|
|
|
// allocHealth is emitted to a chan whenever SetHealth is called
|
|
|
|
type allocHealth struct {
|
|
|
|
healthy bool
|
|
|
|
taskEvents map[string]*structs.TaskEvent
|
|
|
|
}
|
|
|
|
|
2018-09-12 16:36:04 +00:00
|
|
|
// mockHealthSetter implements healthSetter that stores health internally
|
|
|
|
type mockHealthSetter struct {
|
|
|
|
setCalls int
|
|
|
|
clearCalls int
|
|
|
|
healthy *bool
|
|
|
|
isDeploy *bool
|
|
|
|
taskEvents map[string]*structs.TaskEvent
|
|
|
|
mu sync.Mutex
|
2018-09-14 00:25:01 +00:00
|
|
|
|
|
|
|
healthCh chan allocHealth
|
|
|
|
}
|
|
|
|
|
|
|
|
// newMockHealthSetter returns a mock HealthSetter that emits all SetHealth
|
|
|
|
// calls on a buffered chan. Callers who do need need notifications of health
|
|
|
|
// changes may just create the struct directly.
|
|
|
|
func newMockHealthSetter() *mockHealthSetter {
|
|
|
|
return &mockHealthSetter{
|
|
|
|
healthCh: make(chan allocHealth, 1),
|
|
|
|
}
|
2018-09-12 16:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockHealthSetter) SetHealth(healthy, isDeploy bool, taskEvents map[string]*structs.TaskEvent) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
m.setCalls++
|
|
|
|
m.healthy = &healthy
|
|
|
|
m.isDeploy = &isDeploy
|
|
|
|
m.taskEvents = taskEvents
|
2018-09-14 00:25:01 +00:00
|
|
|
|
|
|
|
if m.healthCh != nil {
|
|
|
|
m.healthCh <- allocHealth{healthy, taskEvents}
|
|
|
|
}
|
2018-09-12 16:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockHealthSetter) ClearHealth() {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
m.clearCalls++
|
|
|
|
m.healthy = nil
|
|
|
|
m.isDeploy = nil
|
|
|
|
m.taskEvents = nil
|
|
|
|
}
|
|
|
|
|
2018-12-19 00:53:18 +00:00
|
|
|
func (m *mockHealthSetter) HasHealth() bool {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
return m.healthy != nil
|
|
|
|
}
|
|
|
|
|
2018-12-06 20:30:31 +00:00
|
|
|
// TestHealthHook_PrerunPostrun asserts a health hook does not error if it is
|
|
|
|
// run and postrunned.
|
|
|
|
func TestHealthHook_PrerunPostrun(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-09-12 16:36:04 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
2018-09-18 00:44:25 +00:00
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
|
2018-11-28 00:34:01 +00:00
|
|
|
b := cstructs.NewAllocBroadcaster(logger)
|
|
|
|
defer b.Close()
|
|
|
|
|
2022-03-15 08:38:30 +00:00
|
|
|
consul := regMock.NewServiceRegistrationHandler(logger)
|
2018-09-12 16:36:04 +00:00
|
|
|
hs := &mockHealthSetter{}
|
|
|
|
|
2022-06-07 14:18:19 +00:00
|
|
|
checks := new(mock.CheckShim)
|
2023-03-10 19:43:31 +00:00
|
|
|
alloc := mock.Alloc()
|
|
|
|
h := newAllocHealthWatcherHook(logger, alloc.Copy(), taskEnvBuilderFactory(alloc), hs, b.Listen(), consul, checks)
|
2018-09-12 16:36:04 +00:00
|
|
|
|
|
|
|
// Assert we implemented the right interfaces
|
|
|
|
prerunh, ok := h.(interfaces.RunnerPrerunHook)
|
|
|
|
require.True(ok)
|
|
|
|
_, ok = h.(interfaces.RunnerUpdateHook)
|
|
|
|
require.True(ok)
|
2018-12-06 20:30:31 +00:00
|
|
|
postrunh, ok := h.(interfaces.RunnerPostrunHook)
|
2018-09-12 16:36:04 +00:00
|
|
|
require.True(ok)
|
|
|
|
|
|
|
|
// Prerun
|
2019-03-12 22:03:54 +00:00
|
|
|
require.NoError(prerunh.Prerun())
|
2018-09-12 16:36:04 +00:00
|
|
|
|
2018-09-14 00:25:01 +00:00
|
|
|
// Assert isDeploy is false (other tests peek at isDeploy to determine
|
|
|
|
// if an Update applied)
|
|
|
|
ahw := h.(*allocHealthWatcherHook)
|
|
|
|
ahw.hookLock.Lock()
|
|
|
|
assert.False(t, ahw.isDeploy)
|
|
|
|
ahw.hookLock.Unlock()
|
|
|
|
|
2018-12-06 20:30:31 +00:00
|
|
|
// Postrun
|
|
|
|
require.NoError(postrunh.Postrun())
|
2018-09-12 16:36:04 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 20:30:31 +00:00
|
|
|
// TestHealthHook_PrerunUpdatePostrun asserts Updates may be applied concurrently.
|
|
|
|
func TestHealthHook_PrerunUpdatePostrun(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-09-14 00:25:01 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
|
2018-11-28 00:34:01 +00:00
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
b := cstructs.NewAllocBroadcaster(logger)
|
2018-09-14 00:25:01 +00:00
|
|
|
defer b.Close()
|
|
|
|
|
2022-03-15 08:38:30 +00:00
|
|
|
consul := regMock.NewServiceRegistrationHandler(logger)
|
2018-09-14 00:25:01 +00:00
|
|
|
hs := &mockHealthSetter{}
|
|
|
|
|
2022-06-07 14:18:19 +00:00
|
|
|
checks := new(mock.CheckShim)
|
2023-03-10 19:43:31 +00:00
|
|
|
h := newAllocHealthWatcherHook(logger, alloc.Copy(), taskEnvBuilderFactory(alloc), hs, b.Listen(), consul, checks).(*allocHealthWatcherHook)
|
2018-09-14 00:25:01 +00:00
|
|
|
|
|
|
|
// Prerun
|
2019-03-12 22:03:54 +00:00
|
|
|
require.NoError(h.Prerun())
|
2018-09-14 00:25:01 +00:00
|
|
|
|
|
|
|
// Update multiple times in a goroutine to mimic Client behavior
|
|
|
|
// (Updates are concurrent with alloc runner but are applied serially).
|
|
|
|
errs := make(chan error, 2)
|
|
|
|
go func() {
|
|
|
|
defer close(errs)
|
|
|
|
for i := 0; i < cap(errs); i++ {
|
|
|
|
alloc.AllocModifyIndex++
|
|
|
|
errs <- h.Update(&interfaces.RunnerUpdateRequest{Alloc: alloc.Copy()})
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for err := range errs {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2018-12-06 20:30:31 +00:00
|
|
|
// Postrun
|
|
|
|
require.NoError(h.Postrun())
|
2018-09-14 00:25:01 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 20:30:31 +00:00
|
|
|
// TestHealthHook_UpdatePrerunPostrun asserts that a hook may have Update
|
2018-09-14 00:25:01 +00:00
|
|
|
// called before Prerun.
|
2018-12-06 20:30:31 +00:00
|
|
|
func TestHealthHook_UpdatePrerunPostrun(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-09-14 00:25:01 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
|
2018-11-28 00:34:01 +00:00
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
b := cstructs.NewAllocBroadcaster(logger)
|
2018-09-14 00:25:01 +00:00
|
|
|
defer b.Close()
|
|
|
|
|
2022-03-15 08:38:30 +00:00
|
|
|
consul := regMock.NewServiceRegistrationHandler(logger)
|
2018-09-14 00:25:01 +00:00
|
|
|
hs := &mockHealthSetter{}
|
|
|
|
|
2022-06-07 14:18:19 +00:00
|
|
|
checks := new(mock.CheckShim)
|
2023-03-10 19:43:31 +00:00
|
|
|
h := newAllocHealthWatcherHook(logger, alloc.Copy(), taskEnvBuilderFactory(alloc), hs, b.Listen(), consul, checks).(*allocHealthWatcherHook)
|
2018-09-14 00:25:01 +00:00
|
|
|
|
|
|
|
// Set a DeploymentID to cause ClearHealth to be called
|
|
|
|
alloc.DeploymentID = uuid.Generate()
|
|
|
|
|
|
|
|
// Update in a goroutine to mimic Client behavior (Updates are
|
|
|
|
// concurrent with alloc runner).
|
|
|
|
errs := make(chan error, 1)
|
|
|
|
go func(alloc *structs.Allocation) {
|
|
|
|
errs <- h.Update(&interfaces.RunnerUpdateRequest{Alloc: alloc})
|
|
|
|
close(errs)
|
|
|
|
}(alloc.Copy())
|
|
|
|
|
|
|
|
for err := range errs {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prerun should be a noop
|
2019-03-12 22:03:54 +00:00
|
|
|
require.NoError(h.Prerun())
|
2018-09-14 00:25:01 +00:00
|
|
|
|
|
|
|
// Assert that the Update took affect by isDeploy being true
|
|
|
|
h.hookLock.Lock()
|
|
|
|
assert.True(t, h.isDeploy)
|
|
|
|
h.hookLock.Unlock()
|
|
|
|
|
2018-12-06 20:30:31 +00:00
|
|
|
// Postrun
|
|
|
|
require.NoError(h.Postrun())
|
2018-09-14 00:25:01 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 20:30:31 +00:00
|
|
|
// TestHealthHook_Postrun asserts that a hook may have only Postrun called.
|
|
|
|
func TestHealthHook_Postrun(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-09-14 00:25:01 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
2018-11-28 00:34:01 +00:00
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
b := cstructs.NewAllocBroadcaster(logger)
|
2018-09-14 00:25:01 +00:00
|
|
|
defer b.Close()
|
|
|
|
|
2022-03-15 08:38:30 +00:00
|
|
|
consul := regMock.NewServiceRegistrationHandler(logger)
|
2018-09-14 00:25:01 +00:00
|
|
|
hs := &mockHealthSetter{}
|
|
|
|
|
2023-03-10 19:43:31 +00:00
|
|
|
alloc := mock.Alloc()
|
2022-06-07 14:18:19 +00:00
|
|
|
checks := new(mock.CheckShim)
|
2023-03-10 19:43:31 +00:00
|
|
|
h := newAllocHealthWatcherHook(logger, alloc.Copy(), taskEnvBuilderFactory(alloc), hs, b.Listen(), consul, checks).(*allocHealthWatcherHook)
|
2018-09-14 00:25:01 +00:00
|
|
|
|
2018-12-06 20:30:31 +00:00
|
|
|
// Postrun
|
|
|
|
require.NoError(h.Postrun())
|
2018-09-14 00:25:01 +00:00
|
|
|
}
|
|
|
|
|
2020-03-17 15:43:19 +00:00
|
|
|
// TestHealthHook_SetHealth_healthy asserts SetHealth is called when health status is
|
2018-09-14 00:25:01 +00:00
|
|
|
// set. Uses task state and health checks.
|
2020-03-17 15:43:19 +00:00
|
|
|
func TestHealthHook_SetHealth_healthy(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-09-14 00:25:01 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.Job.TaskGroups[0].Migrate.MinHealthyTime = 1 // let's speed things up
|
|
|
|
task := alloc.Job.TaskGroups[0].Tasks[0]
|
|
|
|
|
|
|
|
// Synthesize running alloc and tasks
|
|
|
|
alloc.ClientStatus = structs.AllocClientStatusRunning
|
|
|
|
alloc.TaskStates = map[string]*structs.TaskState{
|
|
|
|
task.Name: {
|
|
|
|
State: structs.TaskStateRunning,
|
|
|
|
StartedAt: time.Now(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make Consul response
|
|
|
|
check := &consulapi.AgentCheck{
|
|
|
|
Name: task.Services[0].Checks[0].Name,
|
|
|
|
Status: consulapi.HealthPassing,
|
|
|
|
}
|
2022-03-15 08:38:30 +00:00
|
|
|
taskRegs := map[string]*serviceregistration.ServiceRegistrations{
|
2018-09-14 00:25:01 +00:00
|
|
|
task.Name: {
|
2022-03-15 08:38:30 +00:00
|
|
|
Services: map[string]*serviceregistration.ServiceRegistration{
|
2018-09-14 00:25:01 +00:00
|
|
|
task.Services[0].Name: {
|
|
|
|
Service: &consulapi.AgentService{
|
|
|
|
ID: "foo",
|
|
|
|
Service: task.Services[0].Name,
|
|
|
|
},
|
|
|
|
Checks: []*consulapi.AgentCheck{check},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-18 00:44:25 +00:00
|
|
|
logger := testlog.HCLogger(t)
|
2018-11-28 00:34:01 +00:00
|
|
|
b := cstructs.NewAllocBroadcaster(logger)
|
|
|
|
defer b.Close()
|
2018-09-18 00:44:25 +00:00
|
|
|
|
2018-09-14 00:25:01 +00:00
|
|
|
// Don't reply on the first call
|
|
|
|
called := false
|
2022-03-15 08:38:30 +00:00
|
|
|
consul := regMock.NewServiceRegistrationHandler(logger)
|
|
|
|
consul.AllocRegistrationsFn = func(string) (*serviceregistration.AllocRegistration, error) {
|
2018-09-14 00:25:01 +00:00
|
|
|
if !called {
|
|
|
|
called = true
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-03-15 08:38:30 +00:00
|
|
|
reg := &serviceregistration.AllocRegistration{
|
2018-09-14 00:25:01 +00:00
|
|
|
Tasks: taskRegs,
|
|
|
|
}
|
|
|
|
|
|
|
|
return reg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
hs := newMockHealthSetter()
|
|
|
|
|
2022-06-07 14:18:19 +00:00
|
|
|
checks := new(mock.CheckShim)
|
2023-03-10 19:43:31 +00:00
|
|
|
h := newAllocHealthWatcherHook(logger, alloc.Copy(), taskEnvBuilderFactory(alloc), hs, b.Listen(), consul, checks).(*allocHealthWatcherHook)
|
2018-09-14 00:25:01 +00:00
|
|
|
|
|
|
|
// Prerun
|
2019-03-12 22:03:54 +00:00
|
|
|
require.NoError(h.Prerun())
|
2018-09-14 00:25:01 +00:00
|
|
|
|
|
|
|
// Wait for health to be set (healthy)
|
|
|
|
select {
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatalf("timeout waiting for health to be set")
|
|
|
|
case health := <-hs.healthCh:
|
|
|
|
require.True(health.healthy)
|
|
|
|
|
|
|
|
// Healthy allocs shouldn't emit task events
|
|
|
|
ev := health.taskEvents[task.Name]
|
|
|
|
require.Nilf(ev, "%#v", health.taskEvents)
|
|
|
|
}
|
|
|
|
|
2018-12-06 20:30:31 +00:00
|
|
|
// Postrun
|
|
|
|
require.NoError(h.Postrun())
|
2018-09-14 00:25:01 +00:00
|
|
|
}
|
|
|
|
|
2020-03-17 15:43:19 +00:00
|
|
|
// TestHealthHook_SetHealth_unhealthy asserts SetHealth notices unhealthy allocs
|
|
|
|
func TestHealthHook_SetHealth_unhealthy(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-03-17 15:43:19 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.Job.TaskGroups[0].Migrate.MinHealthyTime = 1 // let's speed things up
|
|
|
|
task := alloc.Job.TaskGroups[0].Tasks[0]
|
|
|
|
|
|
|
|
newCheck := task.Services[0].Checks[0].Copy()
|
|
|
|
newCheck.Name = "failing-check"
|
|
|
|
task.Services[0].Checks = append(task.Services[0].Checks, newCheck)
|
|
|
|
|
|
|
|
// Synthesize running alloc and tasks
|
|
|
|
alloc.ClientStatus = structs.AllocClientStatusRunning
|
|
|
|
alloc.TaskStates = map[string]*structs.TaskState{
|
|
|
|
task.Name: {
|
|
|
|
State: structs.TaskStateRunning,
|
|
|
|
StartedAt: time.Now(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make Consul response
|
|
|
|
checkHealthy := &consulapi.AgentCheck{
|
|
|
|
Name: task.Services[0].Checks[0].Name,
|
|
|
|
Status: consulapi.HealthPassing,
|
|
|
|
}
|
|
|
|
checksUnhealthy := &consulapi.AgentCheck{
|
2020-03-17 15:49:42 +00:00
|
|
|
Name: task.Services[0].Checks[1].Name,
|
2020-03-17 15:43:19 +00:00
|
|
|
Status: consulapi.HealthCritical,
|
|
|
|
}
|
2022-03-15 08:38:30 +00:00
|
|
|
taskRegs := map[string]*serviceregistration.ServiceRegistrations{
|
2020-03-17 15:43:19 +00:00
|
|
|
task.Name: {
|
2022-03-15 08:38:30 +00:00
|
|
|
Services: map[string]*serviceregistration.ServiceRegistration{
|
2020-03-17 15:43:19 +00:00
|
|
|
task.Services[0].Name: {
|
|
|
|
Service: &consulapi.AgentService{
|
|
|
|
ID: "foo",
|
|
|
|
Service: task.Services[0].Name,
|
|
|
|
},
|
|
|
|
Checks: []*consulapi.AgentCheck{checkHealthy, checksUnhealthy},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := testlog.HCLogger(t)
|
|
|
|
b := cstructs.NewAllocBroadcaster(logger)
|
|
|
|
defer b.Close()
|
|
|
|
|
|
|
|
// Don't reply on the first call
|
|
|
|
called := false
|
2022-03-15 08:38:30 +00:00
|
|
|
consul := regMock.NewServiceRegistrationHandler(logger)
|
|
|
|
consul.AllocRegistrationsFn = func(string) (*serviceregistration.AllocRegistration, error) {
|
2020-03-17 15:43:19 +00:00
|
|
|
if !called {
|
|
|
|
called = true
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-03-15 08:38:30 +00:00
|
|
|
reg := &serviceregistration.AllocRegistration{
|
2020-03-17 15:43:19 +00:00
|
|
|
Tasks: taskRegs,
|
|
|
|
}
|
|
|
|
|
|
|
|
return reg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
hs := newMockHealthSetter()
|
|
|
|
|
2022-06-07 14:18:19 +00:00
|
|
|
checks := new(mock.CheckShim)
|
2023-03-10 19:43:31 +00:00
|
|
|
h := newAllocHealthWatcherHook(logger, alloc.Copy(), taskEnvBuilderFactory(alloc), hs, b.Listen(), consul, checks).(*allocHealthWatcherHook)
|
2020-03-17 15:43:19 +00:00
|
|
|
|
|
|
|
// Prerun
|
|
|
|
require.NoError(h.Prerun())
|
|
|
|
|
|
|
|
// Wait to ensure we don't get a healthy status
|
|
|
|
select {
|
2020-03-17 15:49:42 +00:00
|
|
|
case <-time.After(2 * time.Second):
|
2020-03-17 15:43:19 +00:00
|
|
|
// great no healthy status
|
|
|
|
case health := <-hs.healthCh:
|
2020-03-17 15:49:42 +00:00
|
|
|
require.Fail("expected no health event", "got %v", health)
|
2020-03-17 15:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Postrun
|
|
|
|
require.NoError(h.Postrun())
|
|
|
|
}
|
|
|
|
|
2018-09-12 16:36:04 +00:00
|
|
|
// TestHealthHook_SystemNoop asserts that system jobs return the noop tracker.
|
|
|
|
func TestHealthHook_SystemNoop(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-09-12 16:36:04 +00:00
|
|
|
|
2023-03-10 19:43:31 +00:00
|
|
|
alloc := mock.SystemAlloc()
|
|
|
|
h := newAllocHealthWatcherHook(testlog.HCLogger(t), alloc.Copy(), taskEnvBuilderFactory(alloc), nil, nil, nil, nil)
|
2018-09-12 16:36:04 +00:00
|
|
|
|
|
|
|
// Assert that it's the noop impl
|
|
|
|
_, ok := h.(noopAllocHealthWatcherHook)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
// Assert the noop impl does not implement any hooks
|
|
|
|
_, ok = h.(interfaces.RunnerPrerunHook)
|
|
|
|
require.False(t, ok)
|
|
|
|
_, ok = h.(interfaces.RunnerUpdateHook)
|
|
|
|
require.False(t, ok)
|
2018-12-06 20:30:31 +00:00
|
|
|
_, ok = h.(interfaces.RunnerPostrunHook)
|
|
|
|
require.False(t, ok)
|
|
|
|
_, ok = h.(interfaces.ShutdownHook)
|
2018-09-12 16:36:04 +00:00
|
|
|
require.False(t, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestHealthHook_BatchNoop asserts that batch jobs return the noop tracker.
|
|
|
|
func TestHealthHook_BatchNoop(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-09-12 16:36:04 +00:00
|
|
|
|
2023-03-10 19:43:31 +00:00
|
|
|
alloc := mock.BatchAlloc()
|
|
|
|
h := newAllocHealthWatcherHook(testlog.HCLogger(t), alloc.Copy(), taskEnvBuilderFactory(alloc), nil, nil, nil, nil)
|
2018-09-12 16:36:04 +00:00
|
|
|
|
|
|
|
// Assert that it's the noop impl
|
|
|
|
_, ok := h.(noopAllocHealthWatcherHook)
|
|
|
|
require.True(t, ok)
|
|
|
|
}
|
2023-03-10 19:43:31 +00:00
|
|
|
|
|
|
|
func taskEnvBuilderFactory(alloc *structs.Allocation) func() *taskenv.Builder {
|
|
|
|
return func() *taskenv.Builder {
|
|
|
|
return taskenv.NewBuilder(mock.Node(), alloc, nil, alloc.Job.Region)
|
|
|
|
}
|
|
|
|
}
|