2018-02-06 01:20:42 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2019-04-28 21:25:27 +00:00
|
|
|
"encoding/json"
|
2018-02-06 01:20:42 +00:00
|
|
|
"fmt"
|
2019-04-28 21:25:27 +00:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"strings"
|
2018-02-06 01:20:42 +00:00
|
|
|
"testing"
|
2019-04-28 21:25:27 +00:00
|
|
|
"time"
|
2018-02-06 01:20:42 +00:00
|
|
|
|
2020-03-18 11:27:32 +00:00
|
|
|
"github.com/hashicorp/go-msgpack/codec"
|
2018-02-06 01:20:42 +00:00
|
|
|
msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc"
|
|
|
|
"github.com/hashicorp/nomad/acl"
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2018-02-06 01:20:42 +00:00
|
|
|
"github.com/hashicorp/nomad/client"
|
|
|
|
"github.com/hashicorp/nomad/client/config"
|
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2019-04-28 21:25:27 +00:00
|
|
|
nstructs "github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2018-02-06 01:20:42 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2019-01-18 23:57:18 +00:00
|
|
|
"github.com/kr/pretty"
|
2018-02-06 01:20:42 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestClientAllocations_GarbageCollectAll_Local(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-06 01:20:42 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Start a server and client
|
2019-12-04 00:15:11 +00:00
|
|
|
s, cleanupS := TestServer(t, nil)
|
|
|
|
defer cleanupS()
|
2018-02-06 01:20:42 +00:00
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
c, cleanupC := client.TestClient(t, func(c *config.Config) {
|
2018-02-06 01:20:42 +00:00
|
|
|
c.Servers = []string{s.config.RPCAddr.String()}
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupC()
|
2018-02-06 01:20:42 +00:00
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
nodes := s.connectedNodes()
|
|
|
|
return len(nodes) == 1, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have a clients")
|
|
|
|
})
|
|
|
|
|
|
|
|
// Make the request without having a node-id
|
2021-06-11 07:39:22 +00:00
|
|
|
req := &nstructs.NodeSpecificRequest{
|
|
|
|
QueryOptions: nstructs.QueryOptions{Region: "global"},
|
2018-02-06 01:20:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
2021-06-11 07:39:22 +00:00
|
|
|
var resp nstructs.GenericResponse
|
2018-02-06 01:20:42 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ClientAllocations.GarbageCollectAll", req, &resp)
|
|
|
|
require.NotNil(err)
|
|
|
|
require.Contains(err.Error(), "missing")
|
|
|
|
|
|
|
|
// Fetch the response setting the node id
|
|
|
|
req.NodeID = c.NodeID()
|
2021-06-11 07:39:22 +00:00
|
|
|
var resp2 nstructs.GenericResponse
|
2018-02-06 01:20:42 +00:00
|
|
|
err = msgpackrpc.CallWithCodec(codec, "ClientAllocations.GarbageCollectAll", req, &resp2)
|
|
|
|
require.Nil(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientAllocations_GarbageCollectAll_Local_ACL(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-06 01:20:42 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Start a server
|
2019-12-04 00:15:11 +00:00
|
|
|
s, root, cleanupS := TestACLServer(t, nil)
|
|
|
|
defer cleanupS()
|
2018-02-06 01:20:42 +00:00
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
|
|
|
// Create a bad token
|
|
|
|
policyBad := mock.NamespacePolicy("other", "", []string{acl.NamespaceCapabilityReadFS})
|
|
|
|
tokenBad := mock.CreatePolicyAndToken(t, s.State(), 1005, "invalid", policyBad)
|
|
|
|
|
|
|
|
policyGood := mock.NodePolicy(acl.PolicyWrite)
|
|
|
|
tokenGood := mock.CreatePolicyAndToken(t, s.State(), 1009, "valid2", policyGood)
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Token string
|
|
|
|
ExpectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "bad token",
|
|
|
|
Token: tokenBad.SecretID,
|
2021-06-11 07:39:22 +00:00
|
|
|
ExpectedError: nstructs.ErrPermissionDenied.Error(),
|
2018-02-06 01:20:42 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "good token",
|
|
|
|
Token: tokenGood.SecretID,
|
|
|
|
ExpectedError: "Unknown node",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "root token",
|
|
|
|
Token: root.SecretID,
|
|
|
|
ExpectedError: "Unknown node",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.Name, func(t *testing.T) {
|
|
|
|
|
|
|
|
// Make the request without having a node-id
|
2021-06-11 07:39:22 +00:00
|
|
|
req := &nstructs.NodeSpecificRequest{
|
2018-02-06 01:20:42 +00:00
|
|
|
NodeID: uuid.Generate(),
|
2021-06-11 07:39:22 +00:00
|
|
|
QueryOptions: nstructs.QueryOptions{
|
2018-02-06 01:20:42 +00:00
|
|
|
AuthToken: c.Token,
|
|
|
|
Region: "global",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
2021-06-11 07:39:22 +00:00
|
|
|
var resp nstructs.GenericResponse
|
2018-02-06 01:20:42 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ClientAllocations.GarbageCollectAll", req, &resp)
|
|
|
|
require.NotNil(err)
|
|
|
|
require.Contains(err.Error(), c.ExpectedError)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientAllocations_GarbageCollectAll_NoNode(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-06 01:20:42 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Start a server and client
|
2019-12-04 00:15:11 +00:00
|
|
|
s, cleanupS := TestServer(t, nil)
|
|
|
|
defer cleanupS()
|
2018-02-06 01:20:42 +00:00
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
|
|
|
// Make the request without having a node-id
|
2021-06-11 07:39:22 +00:00
|
|
|
req := &nstructs.NodeSpecificRequest{
|
2018-02-06 01:20:42 +00:00
|
|
|
NodeID: uuid.Generate(),
|
2021-06-11 07:39:22 +00:00
|
|
|
QueryOptions: nstructs.QueryOptions{Region: "global"},
|
2018-02-06 01:20:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
2021-06-11 07:39:22 +00:00
|
|
|
var resp nstructs.GenericResponse
|
2018-02-06 01:20:42 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ClientAllocations.GarbageCollectAll", req, &resp)
|
|
|
|
require.NotNil(err)
|
|
|
|
require.Contains(err.Error(), "Unknown node")
|
|
|
|
}
|
|
|
|
|
2018-04-06 18:33:08 +00:00
|
|
|
func TestClientAllocations_GarbageCollectAll_OldNode(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-04-06 18:33:08 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Start a server and fake an old client
|
2019-12-04 00:15:11 +00:00
|
|
|
s, cleanupS := TestServer(t, nil)
|
|
|
|
defer cleanupS()
|
2018-04-06 18:33:08 +00:00
|
|
|
state := s.State()
|
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
|
|
|
// Test for an old version error
|
|
|
|
node := mock.Node()
|
|
|
|
node.Attributes["nomad.version"] = "0.7.1"
|
2022-09-15 17:35:08 +00:00
|
|
|
require.Nil(state.UpsertNode(nstructs.MsgTypeTestSetup, 1005, node.Copy()))
|
2018-04-06 18:33:08 +00:00
|
|
|
|
2021-06-11 07:39:22 +00:00
|
|
|
req := &nstructs.NodeSpecificRequest{
|
2018-04-06 18:33:08 +00:00
|
|
|
NodeID: node.ID,
|
2021-06-11 07:39:22 +00:00
|
|
|
QueryOptions: nstructs.QueryOptions{Region: "global"},
|
2018-04-06 18:33:08 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 07:39:22 +00:00
|
|
|
var resp nstructs.GenericResponse
|
2018-04-06 18:33:08 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ClientAllocations.GarbageCollectAll", req, &resp)
|
2021-06-11 07:39:22 +00:00
|
|
|
require.True(nstructs.IsErrNodeLacksRpc(err))
|
2018-04-06 18:33:08 +00:00
|
|
|
|
|
|
|
// Test for a missing version error
|
|
|
|
delete(node.Attributes, "nomad.version")
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(state.UpsertNode(nstructs.MsgTypeTestSetup, 1006, node))
|
2018-04-06 18:33:08 +00:00
|
|
|
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "ClientAllocations.GarbageCollectAll", req, &resp)
|
2021-06-11 07:39:22 +00:00
|
|
|
require.True(nstructs.IsErrUnknownNomadVersion(err))
|
2018-04-06 18:33:08 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 01:20:42 +00:00
|
|
|
func TestClientAllocations_GarbageCollectAll_Remote(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-06 01:20:42 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Start a server and client
|
2020-03-02 15:29:24 +00:00
|
|
|
s1, cleanupS1 := TestServer(t, func(c *Config) {
|
|
|
|
c.BootstrapExpect = 2
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS1()
|
|
|
|
s2, cleanupS2 := TestServer(t, func(c *Config) {
|
2020-03-02 15:29:24 +00:00
|
|
|
c.BootstrapExpect = 2
|
2018-02-06 01:20:42 +00:00
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS2()
|
2018-02-06 01:20:42 +00:00
|
|
|
TestJoin(t, s1, s2)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
testutil.WaitForLeader(t, s2.RPC)
|
|
|
|
codec := rpcClient(t, s2)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
c, cleanupC := client.TestClient(t, func(c *config.Config) {
|
2018-02-06 01:20:42 +00:00
|
|
|
c.Servers = []string{s2.config.RPCAddr.String()}
|
2018-02-14 00:31:17 +00:00
|
|
|
c.GCDiskUsageThreshold = 100.0
|
2018-02-06 01:20:42 +00:00
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupC()
|
2018-02-06 01:20:42 +00:00
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
nodes := s2.connectedNodes()
|
2019-01-18 23:57:18 +00:00
|
|
|
if len(nodes) != 1 {
|
|
|
|
return false, fmt.Errorf("should have 1 client. found %d", len(nodes))
|
|
|
|
}
|
2021-06-11 07:39:22 +00:00
|
|
|
req := &nstructs.NodeSpecificRequest{
|
2019-01-18 23:57:18 +00:00
|
|
|
NodeID: c.NodeID(),
|
2021-06-11 07:39:22 +00:00
|
|
|
QueryOptions: nstructs.QueryOptions{Region: "global"},
|
2019-01-18 23:57:18 +00:00
|
|
|
}
|
2021-06-11 07:39:22 +00:00
|
|
|
resp := nstructs.SingleNodeResponse{}
|
2019-01-18 23:57:18 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.GetNode", req, &resp); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-06-11 07:39:22 +00:00
|
|
|
return resp.Node != nil && resp.Node.Status == nstructs.NodeStatusReady, fmt.Errorf(
|
2019-01-18 23:57:18 +00:00
|
|
|
"expected ready but found %s", pretty.Sprint(resp.Node))
|
2018-02-06 01:20:42 +00:00
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have a clients")
|
|
|
|
})
|
|
|
|
|
|
|
|
// Force remove the connection locally in case it exists
|
|
|
|
s1.nodeConnsLock.Lock()
|
|
|
|
delete(s1.nodeConns, c.NodeID())
|
|
|
|
s1.nodeConnsLock.Unlock()
|
|
|
|
|
|
|
|
// Make the request
|
2021-06-11 07:39:22 +00:00
|
|
|
req := &nstructs.NodeSpecificRequest{
|
2018-02-06 01:20:42 +00:00
|
|
|
NodeID: c.NodeID(),
|
2021-06-11 07:39:22 +00:00
|
|
|
QueryOptions: nstructs.QueryOptions{Region: "global"},
|
2018-02-06 01:20:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp cstructs.ClientStatsResponse
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ClientAllocations.GarbageCollectAll", req, &resp)
|
|
|
|
require.Nil(err)
|
|
|
|
}
|
|
|
|
|
2018-04-06 18:33:08 +00:00
|
|
|
func TestClientAllocations_GarbageCollect_OldNode(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-04-06 18:33:08 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Start a server and fake an old client
|
2019-12-04 00:15:11 +00:00
|
|
|
s, cleanupS := TestServer(t, nil)
|
|
|
|
defer cleanupS()
|
2018-04-06 18:33:08 +00:00
|
|
|
state := s.State()
|
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
|
|
|
// Test for an old version error
|
|
|
|
node := mock.Node()
|
|
|
|
node.Attributes["nomad.version"] = "0.7.1"
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(state.UpsertNode(nstructs.MsgTypeTestSetup, 1005, node))
|
2018-04-06 18:33:08 +00:00
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.NodeID = node.ID
|
2021-06-11 07:39:22 +00:00
|
|
|
require.Nil(state.UpsertAllocs(nstructs.MsgTypeTestSetup, 1006, []*nstructs.Allocation{alloc}))
|
2018-04-06 18:33:08 +00:00
|
|
|
|
2021-06-11 07:39:22 +00:00
|
|
|
req := &nstructs.AllocSpecificRequest{
|
2018-04-06 18:33:08 +00:00
|
|
|
AllocID: alloc.ID,
|
2021-06-11 07:39:22 +00:00
|
|
|
QueryOptions: nstructs.QueryOptions{
|
2018-04-06 18:33:08 +00:00
|
|
|
Region: "global",
|
2021-06-11 07:39:22 +00:00
|
|
|
Namespace: nstructs.DefaultNamespace,
|
2018-04-06 18:33:08 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-06-11 07:39:22 +00:00
|
|
|
var resp nstructs.GenericResponse
|
2018-04-06 18:33:08 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ClientAllocations.GarbageCollect", req, &resp)
|
2021-06-11 07:39:22 +00:00
|
|
|
require.True(nstructs.IsErrNodeLacksRpc(err), err.Error())
|
2018-04-06 18:33:08 +00:00
|
|
|
|
|
|
|
// Test for a missing version error
|
|
|
|
delete(node.Attributes, "nomad.version")
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(state.UpsertNode(nstructs.MsgTypeTestSetup, 1007, node))
|
2018-04-06 18:33:08 +00:00
|
|
|
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "ClientAllocations.GarbageCollect", req, &resp)
|
2021-06-11 07:39:22 +00:00
|
|
|
require.True(nstructs.IsErrUnknownNomadVersion(err), err.Error())
|
2018-04-06 18:33:08 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 01:20:42 +00:00
|
|
|
func TestClientAllocations_GarbageCollect_Local(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-06 01:20:42 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Start a server and client
|
2019-12-04 00:15:11 +00:00
|
|
|
s, cleanupS := TestServer(t, nil)
|
|
|
|
defer cleanupS()
|
2018-02-06 01:20:42 +00:00
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
c, cleanupC := client.TestClient(t, func(c *config.Config) {
|
2018-02-06 01:20:42 +00:00
|
|
|
c.Servers = []string{s.config.RPCAddr.String()}
|
2018-02-14 00:31:17 +00:00
|
|
|
c.GCDiskUsageThreshold = 100.0
|
2018-02-06 01:20:42 +00:00
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupC()
|
2018-02-06 01:20:42 +00:00
|
|
|
|
|
|
|
// Force an allocation onto the node
|
|
|
|
a := mock.Alloc()
|
2021-06-11 07:39:22 +00:00
|
|
|
a.Job.Type = nstructs.JobTypeBatch
|
2018-02-06 01:20:42 +00:00
|
|
|
a.NodeID = c.NodeID()
|
|
|
|
a.Job.TaskGroups[0].Count = 1
|
2021-06-11 07:39:22 +00:00
|
|
|
a.Job.TaskGroups[0].Tasks[0] = &nstructs.Task{
|
2018-02-06 01:20:42 +00:00
|
|
|
Name: "web",
|
|
|
|
Driver: "mock_driver",
|
|
|
|
Config: map[string]interface{}{
|
2018-11-13 02:52:12 +00:00
|
|
|
"run_for": "2s",
|
2018-02-06 01:20:42 +00:00
|
|
|
},
|
2021-06-11 07:39:22 +00:00
|
|
|
LogConfig: nstructs.DefaultLogConfig(),
|
|
|
|
Resources: &nstructs.Resources{
|
2018-02-06 01:20:42 +00:00
|
|
|
CPU: 500,
|
|
|
|
MemoryMB: 256,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
nodes := s.connectedNodes()
|
|
|
|
return len(nodes) == 1, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have a clients")
|
|
|
|
})
|
|
|
|
|
|
|
|
// Upsert the allocation
|
|
|
|
state := s.State()
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(state.UpsertJob(nstructs.MsgTypeTestSetup, 999, a.Job))
|
2021-06-11 07:39:22 +00:00
|
|
|
require.Nil(state.UpsertAllocs(nstructs.MsgTypeTestSetup, 1003, []*nstructs.Allocation{a}))
|
2018-02-06 01:20:42 +00:00
|
|
|
|
|
|
|
// Wait for the client to run the allocation
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
alloc, err := state.AllocByID(nil, a.ID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if alloc == nil {
|
|
|
|
return false, fmt.Errorf("unknown alloc")
|
|
|
|
}
|
2021-06-11 07:39:22 +00:00
|
|
|
if alloc.ClientStatus != nstructs.AllocClientStatusComplete {
|
2018-02-06 01:20:42 +00:00
|
|
|
return false, fmt.Errorf("alloc client status: %v", alloc.ClientStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("Alloc on node %q not finished: %v", c.NodeID(), err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Make the request without having an alloc id
|
2021-06-11 07:39:22 +00:00
|
|
|
req := &nstructs.AllocSpecificRequest{
|
|
|
|
QueryOptions: nstructs.QueryOptions{Region: "global"},
|
2018-02-06 01:20:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
2021-06-11 07:39:22 +00:00
|
|
|
var resp nstructs.GenericResponse
|
2018-02-06 01:20:42 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ClientAllocations.GarbageCollect", req, &resp)
|
|
|
|
require.NotNil(err)
|
|
|
|
require.Contains(err.Error(), "missing")
|
|
|
|
|
|
|
|
// Fetch the response setting the node id
|
|
|
|
req.AllocID = a.ID
|
2021-06-11 07:39:22 +00:00
|
|
|
var resp2 nstructs.GenericResponse
|
2018-02-06 01:20:42 +00:00
|
|
|
err = msgpackrpc.CallWithCodec(codec, "ClientAllocations.GarbageCollect", req, &resp2)
|
|
|
|
require.Nil(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientAllocations_GarbageCollect_Local_ACL(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-06 01:20:42 +00:00
|
|
|
|
|
|
|
// Start a server
|
2019-12-04 00:15:11 +00:00
|
|
|
s, root, cleanupS := TestACLServer(t, nil)
|
|
|
|
defer cleanupS()
|
2018-02-06 01:20:42 +00:00
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
|
|
|
// Create a bad token
|
|
|
|
policyBad := mock.NamespacePolicy("other", "", []string{acl.NamespaceCapabilityReadFS})
|
|
|
|
tokenBad := mock.CreatePolicyAndToken(t, s.State(), 1005, "invalid", policyBad)
|
|
|
|
|
2021-06-11 07:39:22 +00:00
|
|
|
policyGood := mock.NamespacePolicy(nstructs.DefaultNamespace, "", []string{acl.NamespaceCapabilitySubmitJob})
|
2018-02-06 01:20:42 +00:00
|
|
|
tokenGood := mock.CreatePolicyAndToken(t, s.State(), 1009, "valid2", policyGood)
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
// Upsert the allocation
|
|
|
|
state := s.State()
|
|
|
|
alloc := mock.Alloc()
|
2020-10-19 13:30:15 +00:00
|
|
|
require.NoError(t, state.UpsertJob(nstructs.MsgTypeTestSetup, 1010, alloc.Job))
|
2021-06-11 07:39:22 +00:00
|
|
|
require.NoError(t, state.UpsertAllocs(nstructs.MsgTypeTestSetup, 1011, []*nstructs.Allocation{alloc}))
|
2019-10-01 20:06:24 +00:00
|
|
|
|
2018-02-06 01:20:42 +00:00
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Token string
|
|
|
|
ExpectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "bad token",
|
|
|
|
Token: tokenBad.SecretID,
|
2021-06-11 07:39:22 +00:00
|
|
|
ExpectedError: nstructs.ErrPermissionDenied.Error(),
|
2018-02-06 01:20:42 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "good token",
|
|
|
|
Token: tokenGood.SecretID,
|
2021-06-11 07:39:22 +00:00
|
|
|
ExpectedError: nstructs.ErrUnknownNodePrefix,
|
2018-02-06 01:20:42 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "root token",
|
|
|
|
Token: root.SecretID,
|
2021-06-11 07:39:22 +00:00
|
|
|
ExpectedError: nstructs.ErrUnknownNodePrefix,
|
2018-02-06 01:20:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.Name, func(t *testing.T) {
|
|
|
|
|
|
|
|
// Make the request without having a node-id
|
2021-06-11 07:39:22 +00:00
|
|
|
req := &nstructs.AllocSpecificRequest{
|
2019-10-01 20:06:24 +00:00
|
|
|
AllocID: alloc.ID,
|
2021-06-11 07:39:22 +00:00
|
|
|
QueryOptions: nstructs.QueryOptions{
|
2018-02-06 01:20:42 +00:00
|
|
|
AuthToken: c.Token,
|
|
|
|
Region: "global",
|
2021-06-11 07:39:22 +00:00
|
|
|
Namespace: nstructs.DefaultNamespace,
|
2018-02-06 01:20:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
2021-06-11 07:39:22 +00:00
|
|
|
var resp nstructs.GenericResponse
|
2018-02-06 01:20:42 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ClientAllocations.GarbageCollect", req, &resp)
|
2019-10-01 20:06:24 +00:00
|
|
|
require.NotNil(t, err)
|
|
|
|
require.Contains(t, err.Error(), c.ExpectedError)
|
2018-02-06 01:20:42 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientAllocations_GarbageCollect_Remote(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-06 01:20:42 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Start a server and client
|
2020-03-02 15:29:24 +00:00
|
|
|
s1, cleanupS1 := TestServer(t, func(c *Config) {
|
|
|
|
c.BootstrapExpect = 2
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS1()
|
|
|
|
s2, cleanupS2 := TestServer(t, func(c *Config) {
|
2020-03-02 15:29:24 +00:00
|
|
|
c.BootstrapExpect = 2
|
2018-02-06 01:20:42 +00:00
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS2()
|
2018-02-06 01:20:42 +00:00
|
|
|
TestJoin(t, s1, s2)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
testutil.WaitForLeader(t, s2.RPC)
|
|
|
|
codec := rpcClient(t, s2)
|
|
|
|
|
2018-10-29 21:21:05 +00:00
|
|
|
c, cleanup := client.TestClient(t, func(c *config.Config) {
|
2018-02-06 01:20:42 +00:00
|
|
|
c.Servers = []string{s2.config.RPCAddr.String()}
|
2018-02-14 00:31:17 +00:00
|
|
|
c.GCDiskUsageThreshold = 100.0
|
2018-02-06 01:20:42 +00:00
|
|
|
})
|
2018-10-29 21:21:05 +00:00
|
|
|
defer cleanup()
|
2018-02-06 01:20:42 +00:00
|
|
|
|
|
|
|
// Force an allocation onto the node
|
|
|
|
a := mock.Alloc()
|
2021-06-11 07:39:22 +00:00
|
|
|
a.Job.Type = nstructs.JobTypeBatch
|
2018-02-06 01:20:42 +00:00
|
|
|
a.NodeID = c.NodeID()
|
|
|
|
a.Job.TaskGroups[0].Count = 1
|
2021-06-11 07:39:22 +00:00
|
|
|
a.Job.TaskGroups[0].Tasks[0] = &nstructs.Task{
|
2018-02-06 01:20:42 +00:00
|
|
|
Name: "web",
|
|
|
|
Driver: "mock_driver",
|
|
|
|
Config: map[string]interface{}{
|
2018-11-13 02:52:12 +00:00
|
|
|
"run_for": "2s",
|
2018-02-06 01:20:42 +00:00
|
|
|
},
|
2021-06-11 07:39:22 +00:00
|
|
|
LogConfig: nstructs.DefaultLogConfig(),
|
|
|
|
Resources: &nstructs.Resources{
|
2018-02-06 01:20:42 +00:00
|
|
|
CPU: 500,
|
|
|
|
MemoryMB: 256,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
nodes := s2.connectedNodes()
|
2019-01-19 14:07:27 +00:00
|
|
|
if len(nodes) != 1 {
|
|
|
|
return false, fmt.Errorf("should have 1 client. found %d", len(nodes))
|
|
|
|
}
|
2021-06-11 07:39:22 +00:00
|
|
|
req := &nstructs.NodeSpecificRequest{
|
2019-01-19 14:07:27 +00:00
|
|
|
NodeID: c.NodeID(),
|
2021-06-11 07:39:22 +00:00
|
|
|
QueryOptions: nstructs.QueryOptions{Region: "global"},
|
2019-01-19 14:07:27 +00:00
|
|
|
}
|
2021-06-11 07:39:22 +00:00
|
|
|
resp := nstructs.SingleNodeResponse{}
|
2019-01-19 14:07:27 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Node.GetNode", req, &resp); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-06-11 07:39:22 +00:00
|
|
|
return resp.Node != nil && resp.Node.Status == nstructs.NodeStatusReady, fmt.Errorf(
|
2019-01-19 14:07:27 +00:00
|
|
|
"expected ready but found %s", pretty.Sprint(resp.Node))
|
2018-02-06 01:20:42 +00:00
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have a clients")
|
|
|
|
})
|
|
|
|
|
|
|
|
// Upsert the allocation
|
|
|
|
state1 := s1.State()
|
|
|
|
state2 := s2.State()
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(state1.UpsertJob(nstructs.MsgTypeTestSetup, 999, a.Job))
|
2021-06-11 07:39:22 +00:00
|
|
|
require.Nil(state1.UpsertAllocs(nstructs.MsgTypeTestSetup, 1003, []*nstructs.Allocation{a}))
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(state2.UpsertJob(nstructs.MsgTypeTestSetup, 999, a.Job))
|
2021-06-11 07:39:22 +00:00
|
|
|
require.Nil(state2.UpsertAllocs(nstructs.MsgTypeTestSetup, 1003, []*nstructs.Allocation{a}))
|
2018-02-06 01:20:42 +00:00
|
|
|
|
|
|
|
// Wait for the client to run the allocation
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
alloc, err := state2.AllocByID(nil, a.ID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if alloc == nil {
|
|
|
|
return false, fmt.Errorf("unknown alloc")
|
|
|
|
}
|
2021-06-11 07:39:22 +00:00
|
|
|
if alloc.ClientStatus != nstructs.AllocClientStatusComplete {
|
2018-02-06 01:20:42 +00:00
|
|
|
return false, fmt.Errorf("alloc client status: %v", alloc.ClientStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("Alloc on node %q not finished: %v", c.NodeID(), err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Force remove the connection locally in case it exists
|
|
|
|
s1.nodeConnsLock.Lock()
|
|
|
|
delete(s1.nodeConns, c.NodeID())
|
|
|
|
s1.nodeConnsLock.Unlock()
|
|
|
|
|
|
|
|
// Make the request
|
2021-06-11 07:39:22 +00:00
|
|
|
req := &nstructs.AllocSpecificRequest{
|
2018-02-06 01:20:42 +00:00
|
|
|
AllocID: a.ID,
|
2021-06-11 07:39:22 +00:00
|
|
|
QueryOptions: nstructs.QueryOptions{Region: "global"},
|
2018-02-06 01:20:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp cstructs.ClientStatsResponse
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ClientAllocations.GarbageCollect", req, &resp)
|
|
|
|
require.Nil(err)
|
|
|
|
}
|
|
|
|
|
2018-04-06 18:33:08 +00:00
|
|
|
func TestClientAllocations_Stats_OldNode(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-04-06 18:33:08 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Start a server and fake an old client
|
2019-12-04 00:15:11 +00:00
|
|
|
s, cleanupS := TestServer(t, nil)
|
|
|
|
defer cleanupS()
|
2018-04-06 18:33:08 +00:00
|
|
|
state := s.State()
|
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
|
|
|
// Test for an old version error
|
|
|
|
node := mock.Node()
|
|
|
|
node.Attributes["nomad.version"] = "0.7.1"
|
2022-09-15 17:35:08 +00:00
|
|
|
require.Nil(state.UpsertNode(nstructs.MsgTypeTestSetup, 1005, node.Copy()))
|
2018-04-06 18:33:08 +00:00
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.NodeID = node.ID
|
2021-06-11 07:39:22 +00:00
|
|
|
require.Nil(state.UpsertAllocs(nstructs.MsgTypeTestSetup, 1006, []*nstructs.Allocation{alloc}))
|
2018-04-06 18:33:08 +00:00
|
|
|
|
2021-06-11 07:39:22 +00:00
|
|
|
req := &nstructs.AllocSpecificRequest{
|
2018-04-06 18:33:08 +00:00
|
|
|
AllocID: alloc.ID,
|
2021-06-11 07:39:22 +00:00
|
|
|
QueryOptions: nstructs.QueryOptions{
|
2018-04-06 18:33:08 +00:00
|
|
|
Region: "global",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-06-11 07:39:22 +00:00
|
|
|
var resp nstructs.GenericResponse
|
2018-04-06 18:33:08 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ClientAllocations.Stats", req, &resp)
|
2021-06-11 07:39:22 +00:00
|
|
|
require.True(nstructs.IsErrNodeLacksRpc(err), err.Error())
|
2018-04-06 18:33:08 +00:00
|
|
|
|
|
|
|
// Test for a missing version error
|
|
|
|
delete(node.Attributes, "nomad.version")
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(state.UpsertNode(nstructs.MsgTypeTestSetup, 1007, node))
|
2018-04-06 18:33:08 +00:00
|
|
|
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "ClientAllocations.Stats", req, &resp)
|
2021-06-11 07:39:22 +00:00
|
|
|
require.True(nstructs.IsErrUnknownNomadVersion(err), err.Error())
|
2018-04-06 18:33:08 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 01:20:42 +00:00
|
|
|
func TestClientAllocations_Stats_Local(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-06 01:20:42 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Start a server and client
|
2019-12-04 00:15:11 +00:00
|
|
|
s, cleanupS := TestServer(t, nil)
|
|
|
|
defer cleanupS()
|
2018-02-06 01:20:42 +00:00
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
c, cleanupC := client.TestClient(t, func(c *config.Config) {
|
2018-02-06 01:20:42 +00:00
|
|
|
c.Servers = []string{s.config.RPCAddr.String()}
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupC()
|
2018-02-06 01:20:42 +00:00
|
|
|
|
|
|
|
// Force an allocation onto the node
|
|
|
|
a := mock.Alloc()
|
2021-06-11 07:39:22 +00:00
|
|
|
a.Job.Type = nstructs.JobTypeBatch
|
2018-02-06 01:20:42 +00:00
|
|
|
a.NodeID = c.NodeID()
|
|
|
|
a.Job.TaskGroups[0].Count = 1
|
2021-06-11 07:39:22 +00:00
|
|
|
a.Job.TaskGroups[0].Tasks[0] = &nstructs.Task{
|
2018-02-06 01:20:42 +00:00
|
|
|
Name: "web",
|
|
|
|
Driver: "mock_driver",
|
|
|
|
Config: map[string]interface{}{
|
2018-11-13 02:52:12 +00:00
|
|
|
"run_for": "2s",
|
2018-02-06 01:20:42 +00:00
|
|
|
},
|
2021-06-11 07:39:22 +00:00
|
|
|
LogConfig: nstructs.DefaultLogConfig(),
|
|
|
|
Resources: &nstructs.Resources{
|
2018-02-06 01:20:42 +00:00
|
|
|
CPU: 500,
|
|
|
|
MemoryMB: 256,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
nodes := s.connectedNodes()
|
|
|
|
return len(nodes) == 1, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have a clients")
|
|
|
|
})
|
|
|
|
|
|
|
|
// Upsert the allocation
|
|
|
|
state := s.State()
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(state.UpsertJob(nstructs.MsgTypeTestSetup, 999, a.Job))
|
2021-06-11 07:39:22 +00:00
|
|
|
require.Nil(state.UpsertAllocs(nstructs.MsgTypeTestSetup, 1003, []*nstructs.Allocation{a}))
|
2018-02-06 01:20:42 +00:00
|
|
|
|
|
|
|
// Wait for the client to run the allocation
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
alloc, err := state.AllocByID(nil, a.ID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if alloc == nil {
|
|
|
|
return false, fmt.Errorf("unknown alloc")
|
|
|
|
}
|
2021-06-11 07:39:22 +00:00
|
|
|
if alloc.ClientStatus != nstructs.AllocClientStatusComplete {
|
2018-02-06 01:20:42 +00:00
|
|
|
return false, fmt.Errorf("alloc client status: %v", alloc.ClientStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("Alloc on node %q not finished: %v", c.NodeID(), err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Make the request without having an alloc id
|
2021-06-11 07:39:22 +00:00
|
|
|
req := &nstructs.AllocSpecificRequest{
|
|
|
|
QueryOptions: nstructs.QueryOptions{Region: "global"},
|
2018-02-06 01:20:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp cstructs.AllocStatsResponse
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ClientAllocations.Stats", req, &resp)
|
|
|
|
require.NotNil(err)
|
2021-06-11 07:39:22 +00:00
|
|
|
require.EqualError(err, nstructs.ErrMissingAllocID.Error(), "(%T) %v")
|
2018-02-06 01:20:42 +00:00
|
|
|
|
|
|
|
// Fetch the response setting the node id
|
|
|
|
req.AllocID = a.ID
|
|
|
|
var resp2 cstructs.AllocStatsResponse
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "ClientAllocations.Stats", req, &resp2)
|
|
|
|
require.Nil(err)
|
|
|
|
require.NotNil(resp2.Stats)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientAllocations_Stats_Local_ACL(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-06 01:20:42 +00:00
|
|
|
|
|
|
|
// Start a server
|
2019-12-04 00:15:11 +00:00
|
|
|
s, root, cleanupS := TestACLServer(t, nil)
|
|
|
|
defer cleanupS()
|
2018-02-06 01:20:42 +00:00
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
|
|
|
// Create a bad token
|
|
|
|
policyBad := mock.NamespacePolicy("other", "", []string{acl.NamespaceCapabilityReadFS})
|
|
|
|
tokenBad := mock.CreatePolicyAndToken(t, s.State(), 1005, "invalid", policyBad)
|
|
|
|
|
2021-06-11 07:39:22 +00:00
|
|
|
policyGood := mock.NamespacePolicy(nstructs.DefaultNamespace, "", []string{acl.NamespaceCapabilityReadJob})
|
2018-02-06 01:20:42 +00:00
|
|
|
tokenGood := mock.CreatePolicyAndToken(t, s.State(), 1009, "valid2", policyGood)
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
// Upsert the allocation
|
|
|
|
state := s.State()
|
|
|
|
alloc := mock.Alloc()
|
2020-10-19 13:30:15 +00:00
|
|
|
require.NoError(t, state.UpsertJob(nstructs.MsgTypeTestSetup, 1010, alloc.Job))
|
2021-06-11 07:39:22 +00:00
|
|
|
require.NoError(t, state.UpsertAllocs(nstructs.MsgTypeTestSetup, 1011, []*nstructs.Allocation{alloc}))
|
2019-10-01 20:06:24 +00:00
|
|
|
|
2018-02-06 01:20:42 +00:00
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Token string
|
|
|
|
ExpectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "bad token",
|
|
|
|
Token: tokenBad.SecretID,
|
2021-06-11 07:39:22 +00:00
|
|
|
ExpectedError: nstructs.ErrPermissionDenied.Error(),
|
2018-02-06 01:20:42 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "good token",
|
|
|
|
Token: tokenGood.SecretID,
|
2021-06-11 07:39:22 +00:00
|
|
|
ExpectedError: nstructs.ErrUnknownNodePrefix,
|
2018-02-06 01:20:42 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "root token",
|
|
|
|
Token: root.SecretID,
|
2021-06-11 07:39:22 +00:00
|
|
|
ExpectedError: nstructs.ErrUnknownNodePrefix,
|
2018-02-06 01:20:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.Name, func(t *testing.T) {
|
|
|
|
|
|
|
|
// Make the request without having a node-id
|
2021-06-11 07:39:22 +00:00
|
|
|
req := &nstructs.AllocSpecificRequest{
|
2019-10-01 20:06:24 +00:00
|
|
|
AllocID: alloc.ID,
|
2021-06-11 07:39:22 +00:00
|
|
|
QueryOptions: nstructs.QueryOptions{
|
2018-02-06 01:20:42 +00:00
|
|
|
AuthToken: c.Token,
|
|
|
|
Region: "global",
|
2021-06-11 07:39:22 +00:00
|
|
|
Namespace: nstructs.DefaultNamespace,
|
2018-02-06 01:20:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp cstructs.AllocStatsResponse
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ClientAllocations.Stats", req, &resp)
|
2019-10-01 20:06:24 +00:00
|
|
|
require.NotNil(t, err)
|
|
|
|
require.Contains(t, err.Error(), c.ExpectedError)
|
2018-02-06 01:20:42 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientAllocations_Stats_Remote(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-02-06 01:20:42 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Start a server and client
|
2020-03-02 15:29:24 +00:00
|
|
|
s1, cleanupS1 := TestServer(t, func(c *Config) {
|
|
|
|
c.BootstrapExpect = 2
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS1()
|
|
|
|
s2, cleanupS2 := TestServer(t, func(c *Config) {
|
2020-03-02 15:29:24 +00:00
|
|
|
c.BootstrapExpect = 2
|
2018-02-06 01:20:42 +00:00
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS2()
|
2018-02-06 01:20:42 +00:00
|
|
|
TestJoin(t, s1, s2)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
testutil.WaitForLeader(t, s2.RPC)
|
|
|
|
codec := rpcClient(t, s2)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
c, cleanupC := client.TestClient(t, func(c *config.Config) {
|
2018-02-06 01:20:42 +00:00
|
|
|
c.Servers = []string{s2.config.RPCAddr.String()}
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupC()
|
2018-02-06 01:20:42 +00:00
|
|
|
|
|
|
|
// Force an allocation onto the node
|
|
|
|
a := mock.Alloc()
|
2021-06-11 07:39:22 +00:00
|
|
|
a.Job.Type = nstructs.JobTypeBatch
|
2018-02-06 01:20:42 +00:00
|
|
|
a.NodeID = c.NodeID()
|
|
|
|
a.Job.TaskGroups[0].Count = 1
|
2021-06-11 07:39:22 +00:00
|
|
|
a.Job.TaskGroups[0].Tasks[0] = &nstructs.Task{
|
2018-02-06 01:20:42 +00:00
|
|
|
Name: "web",
|
|
|
|
Driver: "mock_driver",
|
|
|
|
Config: map[string]interface{}{
|
2018-11-13 02:52:12 +00:00
|
|
|
"run_for": "2s",
|
2018-02-06 01:20:42 +00:00
|
|
|
},
|
2021-06-11 07:39:22 +00:00
|
|
|
LogConfig: nstructs.DefaultLogConfig(),
|
|
|
|
Resources: &nstructs.Resources{
|
2018-02-06 01:20:42 +00:00
|
|
|
CPU: 500,
|
|
|
|
MemoryMB: 256,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
nodes := s2.connectedNodes()
|
|
|
|
return len(nodes) == 1, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have a clients")
|
|
|
|
})
|
|
|
|
|
|
|
|
// Upsert the allocation
|
|
|
|
state1 := s1.State()
|
|
|
|
state2 := s2.State()
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(state1.UpsertJob(nstructs.MsgTypeTestSetup, 999, a.Job))
|
2021-06-11 07:39:22 +00:00
|
|
|
require.Nil(state1.UpsertAllocs(nstructs.MsgTypeTestSetup, 1003, []*nstructs.Allocation{a}))
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(state2.UpsertJob(nstructs.MsgTypeTestSetup, 999, a.Job))
|
2021-06-11 07:39:22 +00:00
|
|
|
require.Nil(state2.UpsertAllocs(nstructs.MsgTypeTestSetup, 1003, []*nstructs.Allocation{a}))
|
2018-02-06 01:20:42 +00:00
|
|
|
|
|
|
|
// Wait for the client to run the allocation
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
alloc, err := state2.AllocByID(nil, a.ID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if alloc == nil {
|
|
|
|
return false, fmt.Errorf("unknown alloc")
|
|
|
|
}
|
2021-06-11 07:39:22 +00:00
|
|
|
if alloc.ClientStatus != nstructs.AllocClientStatusComplete {
|
2018-02-06 01:20:42 +00:00
|
|
|
return false, fmt.Errorf("alloc client status: %v", alloc.ClientStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("Alloc on node %q not finished: %v", c.NodeID(), err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Force remove the connection locally in case it exists
|
|
|
|
s1.nodeConnsLock.Lock()
|
|
|
|
delete(s1.nodeConns, c.NodeID())
|
|
|
|
s1.nodeConnsLock.Unlock()
|
|
|
|
|
|
|
|
// Make the request
|
2021-06-11 07:39:22 +00:00
|
|
|
req := &nstructs.AllocSpecificRequest{
|
2018-02-06 01:20:42 +00:00
|
|
|
AllocID: a.ID,
|
2021-06-11 07:39:22 +00:00
|
|
|
QueryOptions: nstructs.QueryOptions{Region: "global"},
|
2018-02-06 01:20:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp cstructs.AllocStatsResponse
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ClientAllocations.Stats", req, &resp)
|
|
|
|
require.Nil(err)
|
|
|
|
require.NotNil(resp.Stats)
|
|
|
|
}
|
2019-04-01 12:56:02 +00:00
|
|
|
|
|
|
|
func TestClientAllocations_Restart_Local(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-04-01 12:56:02 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Start a server and client
|
2019-12-04 00:15:11 +00:00
|
|
|
s, cleanupS := TestServer(t, nil)
|
|
|
|
defer cleanupS()
|
2019-04-01 12:56:02 +00:00
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
c, cleanupC := client.TestClient(t, func(c *config.Config) {
|
2019-04-01 12:56:02 +00:00
|
|
|
c.Servers = []string{s.config.RPCAddr.String()}
|
|
|
|
c.GCDiskUsageThreshold = 100.0
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupC()
|
2019-04-01 12:56:02 +00:00
|
|
|
|
|
|
|
// Force an allocation onto the node
|
|
|
|
a := mock.Alloc()
|
2021-06-11 07:39:22 +00:00
|
|
|
a.Job.Type = nstructs.JobTypeService
|
2019-04-01 12:56:02 +00:00
|
|
|
a.NodeID = c.NodeID()
|
|
|
|
a.Job.TaskGroups[0].Count = 1
|
2021-06-11 07:39:22 +00:00
|
|
|
a.Job.TaskGroups[0].Tasks[0] = &nstructs.Task{
|
2019-04-01 12:56:02 +00:00
|
|
|
Name: "web",
|
|
|
|
Driver: "mock_driver",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"run_for": "10s",
|
|
|
|
},
|
2021-06-11 07:39:22 +00:00
|
|
|
LogConfig: nstructs.DefaultLogConfig(),
|
|
|
|
Resources: &nstructs.Resources{
|
2019-04-01 12:56:02 +00:00
|
|
|
CPU: 500,
|
|
|
|
MemoryMB: 256,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
nodes := s.connectedNodes()
|
|
|
|
return len(nodes) == 1, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have a client")
|
|
|
|
})
|
|
|
|
|
|
|
|
// Upsert the allocation
|
|
|
|
state := s.State()
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(state.UpsertJob(nstructs.MsgTypeTestSetup, 999, a.Job))
|
2021-06-11 07:39:22 +00:00
|
|
|
require.Nil(state.UpsertAllocs(nstructs.MsgTypeTestSetup, 1003, []*nstructs.Allocation{a}))
|
2019-04-01 12:56:02 +00:00
|
|
|
|
|
|
|
// Wait for the client to run the allocation
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
alloc, err := state.AllocByID(nil, a.ID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if alloc == nil {
|
|
|
|
return false, fmt.Errorf("unknown alloc")
|
|
|
|
}
|
2021-06-11 07:39:22 +00:00
|
|
|
if alloc.ClientStatus != nstructs.AllocClientStatusRunning {
|
2019-04-01 12:56:02 +00:00
|
|
|
return false, fmt.Errorf("alloc client status: %v", alloc.ClientStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("Alloc on node %q not running: %v", c.NodeID(), err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Make the request without having an alloc id
|
2021-06-11 07:39:22 +00:00
|
|
|
req := &nstructs.AllocRestartRequest{
|
|
|
|
QueryOptions: nstructs.QueryOptions{Region: "global"},
|
2019-04-01 12:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
2021-06-11 07:39:22 +00:00
|
|
|
var resp nstructs.GenericResponse
|
2019-04-01 12:56:02 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ClientAllocations.Restart", req, &resp)
|
|
|
|
require.NotNil(err)
|
2021-06-11 07:39:22 +00:00
|
|
|
require.EqualError(err, nstructs.ErrMissingAllocID.Error(), "(%T) %v")
|
2019-04-01 12:56:02 +00:00
|
|
|
|
|
|
|
// Fetch the response setting the alloc id - This should not error because the
|
|
|
|
// alloc is running.
|
|
|
|
req.AllocID = a.ID
|
2021-06-11 07:39:22 +00:00
|
|
|
var resp2 nstructs.GenericResponse
|
2019-04-01 12:56:02 +00:00
|
|
|
err = msgpackrpc.CallWithCodec(codec, "ClientAllocations.Restart", req, &resp2)
|
|
|
|
require.Nil(err)
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
alloc, err := state.AllocByID(nil, a.ID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if alloc == nil {
|
|
|
|
return false, fmt.Errorf("unknown alloc")
|
|
|
|
}
|
|
|
|
|
|
|
|
taskState := alloc.TaskStates["web"]
|
|
|
|
if taskState == nil {
|
|
|
|
return false, fmt.Errorf("could not find task state")
|
|
|
|
}
|
|
|
|
|
|
|
|
if taskState.Restarts != 1 {
|
|
|
|
return false, fmt.Errorf("expected task 'web' to have 1 restart, got: %d", taskState.Restarts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("Alloc on node %q not running: %v", c.NodeID(), err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientAllocations_Restart_Remote(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-04-01 12:56:02 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
// Start a server and client
|
2020-03-02 15:29:24 +00:00
|
|
|
s1, cleanupS1 := TestServer(t, func(c *Config) {
|
|
|
|
c.BootstrapExpect = 2
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS1()
|
|
|
|
s2, cleanupS2 := TestServer(t, func(c *Config) {
|
2020-03-02 15:29:24 +00:00
|
|
|
c.BootstrapExpect = 2
|
2019-04-01 12:56:02 +00:00
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS2()
|
2019-04-01 12:56:02 +00:00
|
|
|
TestJoin(t, s1, s2)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
testutil.WaitForLeader(t, s2.RPC)
|
|
|
|
codec := rpcClient(t, s2)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
c, cleanupC := client.TestClient(t, func(c *config.Config) {
|
2019-04-01 12:56:02 +00:00
|
|
|
c.Servers = []string{s2.config.RPCAddr.String()}
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupC()
|
2019-04-01 12:56:02 +00:00
|
|
|
|
|
|
|
// Force an allocation onto the node
|
|
|
|
a := mock.Alloc()
|
2021-06-11 07:39:22 +00:00
|
|
|
a.Job.Type = nstructs.JobTypeService
|
2019-04-01 12:56:02 +00:00
|
|
|
a.NodeID = c.NodeID()
|
|
|
|
a.Job.TaskGroups[0].Count = 1
|
2021-06-11 07:39:22 +00:00
|
|
|
a.Job.TaskGroups[0].Tasks[0] = &nstructs.Task{
|
2019-04-01 12:56:02 +00:00
|
|
|
Name: "web",
|
|
|
|
Driver: "mock_driver",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"run_for": "10s",
|
|
|
|
},
|
2021-06-11 07:39:22 +00:00
|
|
|
LogConfig: nstructs.DefaultLogConfig(),
|
|
|
|
Resources: &nstructs.Resources{
|
2019-04-01 12:56:02 +00:00
|
|
|
CPU: 500,
|
|
|
|
MemoryMB: 256,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
nodes := s2.connectedNodes()
|
|
|
|
return len(nodes) == 1, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have a client")
|
|
|
|
})
|
|
|
|
|
|
|
|
// Upsert the allocation
|
|
|
|
state1 := s1.State()
|
|
|
|
state2 := s2.State()
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(state1.UpsertJob(nstructs.MsgTypeTestSetup, 999, a.Job))
|
2021-06-11 07:39:22 +00:00
|
|
|
require.Nil(state1.UpsertAllocs(nstructs.MsgTypeTestSetup, 1003, []*nstructs.Allocation{a}))
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(state2.UpsertJob(nstructs.MsgTypeTestSetup, 999, a.Job))
|
2021-06-11 07:39:22 +00:00
|
|
|
require.Nil(state2.UpsertAllocs(nstructs.MsgTypeTestSetup, 1003, []*nstructs.Allocation{a}))
|
2019-04-01 12:56:02 +00:00
|
|
|
|
|
|
|
// Wait for the client to run the allocation
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
alloc, err := state2.AllocByID(nil, a.ID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if alloc == nil {
|
|
|
|
return false, fmt.Errorf("unknown alloc")
|
|
|
|
}
|
2021-06-11 07:39:22 +00:00
|
|
|
if alloc.ClientStatus != nstructs.AllocClientStatusRunning {
|
2019-04-01 12:56:02 +00:00
|
|
|
return false, fmt.Errorf("alloc client status: %v", alloc.ClientStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("Alloc on node %q not running: %v", c.NodeID(), err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Make the request without having an alloc id
|
2021-06-11 07:39:22 +00:00
|
|
|
req := &nstructs.AllocRestartRequest{
|
|
|
|
QueryOptions: nstructs.QueryOptions{Region: "global"},
|
2019-04-01 12:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
2021-06-11 07:39:22 +00:00
|
|
|
var resp nstructs.GenericResponse
|
2019-04-01 12:56:02 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ClientAllocations.Restart", req, &resp)
|
|
|
|
require.NotNil(err)
|
2021-06-11 07:39:22 +00:00
|
|
|
require.EqualError(err, nstructs.ErrMissingAllocID.Error(), "(%T) %v")
|
2019-04-01 12:56:02 +00:00
|
|
|
|
|
|
|
// Fetch the response setting the alloc id - This should succeed because the
|
|
|
|
// alloc is running
|
|
|
|
req.AllocID = a.ID
|
2021-06-11 07:39:22 +00:00
|
|
|
var resp2 nstructs.GenericResponse
|
2019-04-01 12:56:02 +00:00
|
|
|
err = msgpackrpc.CallWithCodec(codec, "ClientAllocations.Restart", req, &resp2)
|
2019-10-01 20:06:24 +00:00
|
|
|
require.NoError(err)
|
2019-04-01 12:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientAllocations_Restart_ACL(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2019-04-01 12:56:02 +00:00
|
|
|
// Start a server
|
2019-12-04 00:15:11 +00:00
|
|
|
s, root, cleanupS := TestACLServer(t, nil)
|
|
|
|
defer cleanupS()
|
2019-04-01 12:56:02 +00:00
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
|
|
|
// Create a bad token
|
|
|
|
policyBad := mock.NamespacePolicy("other", "", []string{acl.NamespaceCapabilityReadFS})
|
|
|
|
tokenBad := mock.CreatePolicyAndToken(t, s.State(), 1005, "invalid", policyBad)
|
|
|
|
|
2021-06-11 07:39:22 +00:00
|
|
|
policyGood := mock.NamespacePolicy(nstructs.DefaultNamespace, acl.PolicyWrite, nil)
|
2019-04-01 12:56:02 +00:00
|
|
|
tokenGood := mock.CreatePolicyAndToken(t, s.State(), 1009, "valid2", policyGood)
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
// Upsert the allocation
|
|
|
|
state := s.State()
|
|
|
|
alloc := mock.Alloc()
|
2020-10-19 13:30:15 +00:00
|
|
|
require.NoError(t, state.UpsertJob(nstructs.MsgTypeTestSetup, 1010, alloc.Job))
|
2021-06-11 07:39:22 +00:00
|
|
|
require.NoError(t, state.UpsertAllocs(nstructs.MsgTypeTestSetup, 1011, []*nstructs.Allocation{alloc}))
|
2019-10-01 20:06:24 +00:00
|
|
|
|
2019-04-01 12:56:02 +00:00
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Token string
|
|
|
|
ExpectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "bad token",
|
|
|
|
Token: tokenBad.SecretID,
|
2021-06-11 07:39:22 +00:00
|
|
|
ExpectedError: nstructs.ErrPermissionDenied.Error(),
|
2019-04-01 12:56:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "good token",
|
|
|
|
Token: tokenGood.SecretID,
|
2019-10-01 20:06:24 +00:00
|
|
|
ExpectedError: "Unknown node",
|
2019-04-01 12:56:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "root token",
|
|
|
|
Token: root.SecretID,
|
2019-10-01 20:06:24 +00:00
|
|
|
ExpectedError: "Unknown node",
|
2019-04-01 12:56:02 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.Name, func(t *testing.T) {
|
|
|
|
|
|
|
|
// Make the request without having a node-id
|
2021-06-11 07:39:22 +00:00
|
|
|
req := &nstructs.AllocRestartRequest{
|
2019-10-01 20:06:24 +00:00
|
|
|
AllocID: alloc.ID,
|
2021-06-11 07:39:22 +00:00
|
|
|
QueryOptions: nstructs.QueryOptions{
|
|
|
|
Namespace: nstructs.DefaultNamespace,
|
2019-04-01 12:56:02 +00:00
|
|
|
AuthToken: c.Token,
|
|
|
|
Region: "global",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
2021-06-11 07:39:22 +00:00
|
|
|
var resp nstructs.GenericResponse
|
2019-04-01 12:56:02 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ClientAllocations.Restart", req, &resp)
|
|
|
|
require.NotNil(t, err)
|
|
|
|
require.Contains(t, err.Error(), c.ExpectedError)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-04-28 21:25:27 +00:00
|
|
|
|
|
|
|
// TestAlloc_ExecStreaming asserts that exec task requests are forwarded
|
|
|
|
// to appropriate server or remote regions
|
|
|
|
func TestAlloc_ExecStreaming(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-04-28 21:25:27 +00:00
|
|
|
|
|
|
|
////// Nomad clusters topology - not specific to test
|
2020-03-02 15:29:24 +00:00
|
|
|
localServer, cleanupLS := TestServer(t, func(c *Config) {
|
|
|
|
c.BootstrapExpect = 2
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupLS()
|
2019-04-28 21:25:27 +00:00
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
remoteServer, cleanupRS := TestServer(t, func(c *Config) {
|
2020-03-02 15:29:24 +00:00
|
|
|
c.BootstrapExpect = 2
|
2019-04-28 21:25:27 +00:00
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupRS()
|
2019-04-28 21:25:27 +00:00
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
remoteRegionServer, cleanupRRS := TestServer(t, func(c *Config) {
|
2019-04-28 21:25:27 +00:00
|
|
|
c.Region = "two"
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupRRS()
|
2019-04-28 21:25:27 +00:00
|
|
|
|
|
|
|
TestJoin(t, localServer, remoteServer)
|
|
|
|
TestJoin(t, localServer, remoteRegionServer)
|
|
|
|
testutil.WaitForLeader(t, localServer.RPC)
|
|
|
|
testutil.WaitForLeader(t, remoteServer.RPC)
|
|
|
|
testutil.WaitForLeader(t, remoteRegionServer.RPC)
|
|
|
|
|
|
|
|
c, cleanup := client.TestClient(t, func(c *config.Config) {
|
|
|
|
c.Servers = []string{localServer.config.RPCAddr.String()}
|
|
|
|
})
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// Wait for the client to connect
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
nodes := remoteServer.connectedNodes()
|
|
|
|
return len(nodes) == 1, nil
|
|
|
|
}, func(err error) {
|
|
|
|
require.NoError(t, err, "failed to have a client")
|
|
|
|
})
|
|
|
|
|
|
|
|
// Force remove the connection locally in case it exists
|
|
|
|
remoteServer.nodeConnsLock.Lock()
|
|
|
|
delete(remoteServer.nodeConns, c.NodeID())
|
|
|
|
remoteServer.nodeConnsLock.Unlock()
|
|
|
|
|
|
|
|
///// Start task
|
|
|
|
a := mock.BatchAlloc()
|
|
|
|
a.NodeID = c.NodeID()
|
2021-06-11 07:39:22 +00:00
|
|
|
a.Job.Type = nstructs.JobTypeBatch
|
2019-04-28 21:25:27 +00:00
|
|
|
a.Job.TaskGroups[0].Count = 1
|
|
|
|
a.Job.TaskGroups[0].Tasks[0].Config = map[string]interface{}{
|
|
|
|
"run_for": "20s",
|
|
|
|
"exec_command": map[string]interface{}{
|
|
|
|
"run_for": "1ms",
|
|
|
|
"stdout_string": "expected output",
|
|
|
|
"exit_code": 3,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upsert the allocation
|
|
|
|
localState := localServer.State()
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(t, localState.UpsertJob(nstructs.MsgTypeTestSetup, 999, a.Job))
|
2021-06-11 07:39:22 +00:00
|
|
|
require.Nil(t, localState.UpsertAllocs(nstructs.MsgTypeTestSetup, 1003, []*nstructs.Allocation{a}))
|
2019-04-28 21:25:27 +00:00
|
|
|
remoteState := remoteServer.State()
|
2020-10-19 13:30:15 +00:00
|
|
|
require.Nil(t, remoteState.UpsertJob(nstructs.MsgTypeTestSetup, 999, a.Job))
|
2021-06-11 07:39:22 +00:00
|
|
|
require.Nil(t, remoteState.UpsertAllocs(nstructs.MsgTypeTestSetup, 1003, []*nstructs.Allocation{a}))
|
2019-04-28 21:25:27 +00:00
|
|
|
|
|
|
|
// Wait for the client to run the allocation
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
alloc, err := localState.AllocByID(nil, a.ID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if alloc == nil {
|
|
|
|
return false, fmt.Errorf("unknown alloc")
|
|
|
|
}
|
2021-06-11 07:39:22 +00:00
|
|
|
if alloc.ClientStatus != nstructs.AllocClientStatusRunning {
|
2019-04-28 21:25:27 +00:00
|
|
|
return false, fmt.Errorf("alloc client status: %v", alloc.ClientStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
require.NoError(t, err, "task didn't start yet")
|
|
|
|
})
|
|
|
|
|
|
|
|
///////// Actually run query now
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
2021-06-11 07:39:22 +00:00
|
|
|
rpc func(string) (nstructs.StreamingRpcHandler, error)
|
2019-04-28 21:25:27 +00:00
|
|
|
}{
|
|
|
|
{"client", c.StreamingRpcHandler},
|
|
|
|
{"local_server", localServer.StreamingRpcHandler},
|
|
|
|
{"remote_server", remoteServer.StreamingRpcHandler},
|
|
|
|
{"remote_region", remoteRegionServer.StreamingRpcHandler},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
|
|
|
|
// Make the request
|
|
|
|
req := &cstructs.AllocExecRequest{
|
|
|
|
AllocID: a.ID,
|
|
|
|
Task: a.Job.TaskGroups[0].Tasks[0].Name,
|
|
|
|
Tty: true,
|
|
|
|
Cmd: []string{"placeholder command"},
|
|
|
|
QueryOptions: nstructs.QueryOptions{Region: "global"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the handler
|
|
|
|
handler, err := tc.rpc("Allocations.Exec")
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
// Create a pipe
|
|
|
|
p1, p2 := net.Pipe()
|
|
|
|
defer p1.Close()
|
|
|
|
defer p2.Close()
|
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
frames := make(chan *drivers.ExecTaskStreamingResponseMsg)
|
|
|
|
|
|
|
|
// Start the handler
|
|
|
|
go handler(p2)
|
|
|
|
go decodeFrames(t, p1, frames, errCh)
|
|
|
|
|
|
|
|
// Send the request
|
|
|
|
encoder := codec.NewEncoder(p1, nstructs.MsgpackHandle)
|
|
|
|
require.Nil(t, encoder.Encode(req))
|
|
|
|
|
|
|
|
timeout := time.After(3 * time.Second)
|
|
|
|
|
|
|
|
OUTER:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
require.FailNow(t, "timed out before getting exit code")
|
|
|
|
case err := <-errCh:
|
|
|
|
require.NoError(t, err)
|
|
|
|
case f := <-frames:
|
|
|
|
if f.Exited && f.Result != nil {
|
|
|
|
code := int(f.Result.ExitCode)
|
|
|
|
require.Equal(t, 3, code)
|
|
|
|
break OUTER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeFrames(t *testing.T, p1 net.Conn, frames chan<- *drivers.ExecTaskStreamingResponseMsg, errCh chan<- error) {
|
|
|
|
// Start the decoder
|
|
|
|
decoder := codec.NewDecoder(p1, nstructs.MsgpackHandle)
|
|
|
|
|
|
|
|
for {
|
|
|
|
var msg cstructs.StreamErrWrapper
|
|
|
|
if err := decoder.Decode(&msg); err != nil {
|
|
|
|
if err == io.EOF || strings.Contains(err.Error(), "closed") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Logf("received error decoding: %#v", err)
|
|
|
|
|
|
|
|
errCh <- fmt.Errorf("error decoding: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Error != nil {
|
|
|
|
errCh <- msg.Error
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var frame drivers.ExecTaskStreamingResponseMsg
|
|
|
|
json.Unmarshal(msg.Payload, &frame)
|
|
|
|
t.Logf("received message: %#v", msg)
|
|
|
|
frames <- &frame
|
|
|
|
}
|
|
|
|
}
|