2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-04-13 20:08:24 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2020-04-13 20:08:24 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-07-23 18:33:34 +00:00
|
|
|
func TestHeartbeatStop_allocHook(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2020-04-13 20:08:24 +00:00
|
|
|
|
|
|
|
server, _, cleanupS1 := testServer(t, nil)
|
|
|
|
defer cleanupS1()
|
|
|
|
testutil.WaitForLeader(t, server.RPC)
|
|
|
|
|
|
|
|
client, cleanupC1 := TestClient(t, func(c *config.Config) {
|
|
|
|
c.RPCHandler = server
|
|
|
|
})
|
|
|
|
defer cleanupC1()
|
|
|
|
|
|
|
|
// an allocation, with a tiny lease
|
|
|
|
d := 1 * time.Microsecond
|
|
|
|
alloc := &structs.Allocation{
|
|
|
|
ID: uuid.Generate(),
|
|
|
|
TaskGroup: "foo",
|
|
|
|
Job: &structs.Job{
|
|
|
|
TaskGroups: []*structs.TaskGroup{
|
|
|
|
{
|
|
|
|
Name: "foo",
|
|
|
|
StopAfterClientDisconnect: &d,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resources: &structs.Resources{
|
|
|
|
CPU: 100,
|
|
|
|
MemoryMB: 100,
|
|
|
|
DiskMB: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// alloc added to heartbeatStop.allocs
|
2020-04-28 20:13:09 +00:00
|
|
|
err := client.addAlloc(alloc, "")
|
2020-04-13 20:08:24 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
_, ok := client.heartbeatStop.allocInterval[alloc.ID]
|
|
|
|
return ok, nil
|
|
|
|
}, func(err error) {
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// the tiny lease causes the watch loop to destroy it
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
_, ok := client.heartbeatStop.allocInterval[alloc.ID]
|
|
|
|
return !ok, nil
|
|
|
|
}, func(err error) {
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
require.Empty(t, client.allocs[alloc.ID])
|
|
|
|
}
|