2022-03-30 13:12:44 +00:00
|
|
|
package disconnectedclients
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-04-11 15:34:27 +00:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2022-03-30 13:12:44 +00:00
|
|
|
"github.com/hashicorp/nomad/e2e/e2eutil"
|
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2022-11-22 14:51:53 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2022-03-30 13:12:44 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
const ns = ""
|
|
|
|
|
|
|
|
// typical wait times for this test package
|
|
|
|
var wait30s = &e2eutil.WaitConfig{Interval: time.Second, Retries: 30}
|
|
|
|
var wait60s = &e2eutil.WaitConfig{Interval: time.Second, Retries: 60}
|
|
|
|
|
2022-04-11 15:34:27 +00:00
|
|
|
type expectedAllocStatus struct {
|
|
|
|
disconnected string
|
|
|
|
unchanged string
|
|
|
|
replacement string
|
|
|
|
}
|
|
|
|
|
2022-03-30 13:12:44 +00:00
|
|
|
func TestDisconnectedClients(t *testing.T) {
|
2023-01-06 14:52:37 +00:00
|
|
|
t.Skip("disconnected clients tests disabled for now")
|
2022-03-30 13:12:44 +00:00
|
|
|
|
2022-04-11 15:34:27 +00:00
|
|
|
nomad := e2eutil.NomadClient(t)
|
2022-03-30 13:12:44 +00:00
|
|
|
e2eutil.WaitForLeader(t, nomad)
|
|
|
|
e2eutil.WaitForNodesReady(t, nomad, 2) // needs at least 2 to test replacement
|
|
|
|
|
2022-04-11 15:34:27 +00:00
|
|
|
testCases := []struct {
|
2022-11-22 14:51:53 +00:00
|
|
|
skip bool
|
2022-04-11 15:34:27 +00:00
|
|
|
name string
|
|
|
|
jobFile string
|
|
|
|
disconnectFn func(string, time.Duration) (string, error)
|
|
|
|
expectedAfterDisconnect expectedAllocStatus
|
|
|
|
expectedAfterReconnect expectedAllocStatus
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// test that allocations on clients that are netsplit and
|
|
|
|
// marked disconnected are replaced
|
|
|
|
name: "netsplit client no max disconnect",
|
|
|
|
jobFile: "./input/lost_simple.nomad",
|
|
|
|
disconnectFn: e2eutil.AgentDisconnect,
|
|
|
|
expectedAfterDisconnect: expectedAllocStatus{
|
|
|
|
disconnected: "lost",
|
|
|
|
unchanged: "running",
|
|
|
|
replacement: "running",
|
|
|
|
},
|
|
|
|
expectedAfterReconnect: expectedAllocStatus{
|
|
|
|
disconnected: "complete",
|
|
|
|
unchanged: "running",
|
|
|
|
replacement: "running",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// test that allocations on clients that are netsplit and
|
|
|
|
// marked disconnected are replaced but that the
|
|
|
|
// replacements are rolled back after reconnection
|
2022-11-22 14:51:53 +00:00
|
|
|
skip: true,
|
2022-04-11 15:34:27 +00:00
|
|
|
name: "netsplit client with max disconnect",
|
|
|
|
jobFile: "./input/lost_max_disconnect.nomad",
|
|
|
|
disconnectFn: e2eutil.AgentDisconnect,
|
|
|
|
expectedAfterDisconnect: expectedAllocStatus{
|
|
|
|
disconnected: "unknown",
|
|
|
|
unchanged: "running",
|
|
|
|
replacement: "running",
|
|
|
|
},
|
|
|
|
expectedAfterReconnect: expectedAllocStatus{
|
|
|
|
disconnected: "running",
|
|
|
|
unchanged: "running",
|
|
|
|
replacement: "complete",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// test that allocations on clients that are shutdown and
|
|
|
|
// marked disconnected are replaced
|
2022-11-22 14:51:53 +00:00
|
|
|
skip: true,
|
2022-04-11 15:34:27 +00:00
|
|
|
name: "shutdown client no max disconnect",
|
|
|
|
jobFile: "./input/lost_simple.nomad",
|
|
|
|
disconnectFn: e2eutil.AgentDisconnect,
|
|
|
|
expectedAfterDisconnect: expectedAllocStatus{
|
|
|
|
disconnected: "lost",
|
|
|
|
unchanged: "running",
|
|
|
|
replacement: "running",
|
|
|
|
},
|
|
|
|
expectedAfterReconnect: expectedAllocStatus{
|
|
|
|
disconnected: "complete",
|
|
|
|
unchanged: "running",
|
|
|
|
replacement: "running",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// test that allocations on clients that are shutdown and
|
|
|
|
// marked disconnected are replaced
|
2022-11-22 14:51:53 +00:00
|
|
|
skip: true,
|
2022-04-11 15:34:27 +00:00
|
|
|
name: "shutdown client with max disconnect",
|
|
|
|
jobFile: "./input/lost_max_disconnect.nomad",
|
|
|
|
disconnectFn: e2eutil.AgentDisconnect,
|
|
|
|
expectedAfterDisconnect: expectedAllocStatus{
|
|
|
|
disconnected: "unknown",
|
|
|
|
unchanged: "running",
|
|
|
|
replacement: "running",
|
|
|
|
},
|
|
|
|
expectedAfterReconnect: expectedAllocStatus{
|
|
|
|
disconnected: "running",
|
|
|
|
unchanged: "running",
|
|
|
|
replacement: "complete",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
|
2022-11-22 14:51:53 +00:00
|
|
|
if tc.skip {
|
|
|
|
t.Skip("SKIP BROKEN TEST")
|
|
|
|
}
|
|
|
|
|
2022-04-11 15:34:27 +00:00
|
|
|
jobIDs := []string{}
|
|
|
|
t.Cleanup(disconnectedClientsCleanup(t))
|
|
|
|
t.Cleanup(e2eutil.CleanupJobsAndGC(t, &jobIDs))
|
|
|
|
|
|
|
|
jobID := "test-disconnected-clients-" + uuid.Short()
|
|
|
|
|
|
|
|
err := e2eutil.Register(jobID, tc.jobFile)
|
2022-11-22 14:51:53 +00:00
|
|
|
must.NoError(t, err, must.Sprint("failed to register job"))
|
2022-04-11 15:34:27 +00:00
|
|
|
jobIDs = append(jobIDs, jobID)
|
|
|
|
|
|
|
|
err = e2eutil.WaitForAllocStatusExpected(jobID, ns,
|
|
|
|
[]string{"running", "running"})
|
2022-11-22 14:51:53 +00:00
|
|
|
must.NoError(t, err, must.Sprint("job did not become running"))
|
2022-04-11 15:34:27 +00:00
|
|
|
|
2022-04-27 16:27:03 +00:00
|
|
|
err = e2eutil.WaitForLastDeploymentStatus(jobID, ns, "successful", nil)
|
2022-11-22 14:51:53 +00:00
|
|
|
must.NoError(t, err, must.Sprint("deployment did not complete"))
|
2022-04-27 16:27:03 +00:00
|
|
|
|
2022-04-11 15:34:27 +00:00
|
|
|
// pick one alloc to make our disconnected alloc (and its node)
|
|
|
|
allocs, err := e2eutil.AllocsForJob(jobID, ns)
|
2022-11-22 14:51:53 +00:00
|
|
|
must.NoError(t, err, must.Sprint("could not query allocs for job"))
|
|
|
|
must.SliceLen(t, 2, allocs, must.Sprint("could not find 2 allocs for job"))
|
2022-04-11 15:34:27 +00:00
|
|
|
|
|
|
|
disconnectedAllocID := allocs[0]["ID"]
|
|
|
|
disconnectedNodeID := allocs[0]["Node ID"]
|
|
|
|
unchangedAllocID := allocs[1]["ID"]
|
|
|
|
|
|
|
|
// disconnect the node and wait for the results
|
|
|
|
|
|
|
|
restartJobID, err := tc.disconnectFn(disconnectedNodeID, 30*time.Second)
|
2022-11-22 14:51:53 +00:00
|
|
|
must.NoError(t, err, must.Sprint("expected agent disconnect job to register"))
|
2022-04-11 15:34:27 +00:00
|
|
|
jobIDs = append(jobIDs, restartJobID)
|
|
|
|
|
|
|
|
err = e2eutil.WaitForNodeStatus(disconnectedNodeID, "disconnected", wait60s)
|
2022-11-22 14:51:53 +00:00
|
|
|
must.NoError(t, err, must.Sprint("expected node to go down"))
|
|
|
|
must.NoError(t, waitForAllocStatusMap(
|
|
|
|
jobID, disconnectedAllocID, unchangedAllocID, tc.expectedAfterDisconnect, wait60s),
|
|
|
|
)
|
2022-04-11 15:34:27 +00:00
|
|
|
|
2022-11-22 14:51:53 +00:00
|
|
|
// wait for the client reconnect
|
2022-04-11 15:34:27 +00:00
|
|
|
|
2022-11-22 14:51:53 +00:00
|
|
|
err = e2eutil.WaitForNodeStatus(disconnectedNodeID, "ready", wait30s)
|
|
|
|
must.NoError(t, err, must.Sprint("expected node to come back up"))
|
|
|
|
must.NoError(t, waitForAllocStatusMap(
|
|
|
|
jobID, disconnectedAllocID, unchangedAllocID, tc.expectedAfterReconnect, wait60s),
|
|
|
|
)
|
2022-04-11 15:34:27 +00:00
|
|
|
|
2022-11-22 14:51:53 +00:00
|
|
|
// now get the resulting allocations, should be 3
|
2022-04-11 15:34:27 +00:00
|
|
|
|
2022-11-22 14:51:53 +00:00
|
|
|
allocs, err = e2eutil.AllocsForJob(jobID, ns)
|
|
|
|
must.NoError(t, err, must.Sprint("could not query allocs for job"))
|
|
|
|
must.SliceLen(t, 3, allocs, must.Sprint("could not find 3 allocs for job"))
|
2022-04-11 15:34:27 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-30 13:12:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// disconnectedClientsCleanup sets up a cleanup function to make sure
|
|
|
|
// we've waited for all the nodes to come back up between tests
|
|
|
|
func disconnectedClientsCleanup(t *testing.T) func() {
|
|
|
|
nodeIDs := []string{}
|
|
|
|
nodeStatuses, err := e2eutil.NodeStatusList()
|
|
|
|
require.NoError(t, err)
|
|
|
|
for _, nodeStatus := range nodeStatuses {
|
|
|
|
nodeIDs = append(nodeIDs, nodeStatus["ID"])
|
|
|
|
}
|
|
|
|
return func() {
|
|
|
|
nomad := e2eutil.NomadClient(t)
|
2022-04-11 15:34:27 +00:00
|
|
|
t.Logf("waiting for %d nodes to become ready again", len(nodeIDs))
|
2022-03-30 13:12:44 +00:00
|
|
|
e2eutil.WaitForNodesReady(t, nomad, len(nodeIDs))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-11 15:34:27 +00:00
|
|
|
func waitForAllocStatusMap(jobID, disconnectedAllocID, unchangedAllocID string, expected expectedAllocStatus, wc *e2eutil.WaitConfig) error {
|
2022-03-30 13:12:44 +00:00
|
|
|
var err error
|
|
|
|
interval, retries := wc.OrDefault()
|
|
|
|
testutil.WaitForResultRetries(retries, func() (bool, error) {
|
|
|
|
time.Sleep(interval)
|
|
|
|
allocs, err := e2eutil.AllocsForJob(jobID, ns)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2022-04-11 15:34:27 +00:00
|
|
|
|
|
|
|
var merr *multierror.Error
|
|
|
|
|
2022-03-30 13:12:44 +00:00
|
|
|
for _, alloc := range allocs {
|
2022-04-11 15:34:27 +00:00
|
|
|
switch allocID, allocStatus := alloc["ID"], alloc["Status"]; allocID {
|
|
|
|
case disconnectedAllocID:
|
|
|
|
if allocStatus != expected.disconnected {
|
|
|
|
merr = multierror.Append(merr, fmt.Errorf(
|
2022-04-22 13:07:05 +00:00
|
|
|
"disconnected alloc %q on node %q should be %q, got %q",
|
|
|
|
allocID, alloc["Node ID"], expected.disconnected, allocStatus))
|
2022-04-11 15:34:27 +00:00
|
|
|
}
|
|
|
|
case unchangedAllocID:
|
|
|
|
if allocStatus != expected.unchanged {
|
|
|
|
merr = multierror.Append(merr, fmt.Errorf(
|
2022-04-22 13:07:05 +00:00
|
|
|
"unchanged alloc %q on node %q should be %q, got %q",
|
|
|
|
allocID, alloc["Node ID"], expected.unchanged, allocStatus))
|
2022-03-30 13:12:44 +00:00
|
|
|
}
|
2022-04-11 15:34:27 +00:00
|
|
|
default:
|
|
|
|
if allocStatus != expected.replacement {
|
|
|
|
merr = multierror.Append(merr, fmt.Errorf(
|
2022-04-22 13:07:05 +00:00
|
|
|
"replacement alloc %q on node %q should be %q, got %q",
|
|
|
|
allocID, alloc["Node ID"], expected.replacement, allocStatus))
|
2022-03-30 13:12:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-11 15:34:27 +00:00
|
|
|
if merr != nil {
|
|
|
|
return false, merr.ErrorOrNil()
|
|
|
|
}
|
2022-03-30 13:12:44 +00:00
|
|
|
return true, nil
|
|
|
|
}, func(e error) {
|
|
|
|
err = e
|
|
|
|
})
|
2022-04-22 13:07:05 +00:00
|
|
|
|
|
|
|
// TODO(tgross): remove this block once this test has stabilized
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("test failed, printing allocation status of all %q allocs for analysis\n", jobID)
|
|
|
|
fmt.Println("----------------")
|
|
|
|
allocs, _ := e2eutil.AllocsForJob(jobID, ns)
|
|
|
|
for _, alloc := range allocs {
|
|
|
|
out, _ := e2eutil.Command("nomad", "alloc", "status", alloc["ID"])
|
|
|
|
fmt.Println(out)
|
|
|
|
fmt.Println("----------------")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-30 13:12:44 +00:00
|
|
|
return err
|
|
|
|
}
|