2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-01-12 22:09:01 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2018-12-20 05:53:44 +00:00
|
|
|
"time"
|
2018-01-12 22:09:01 +00:00
|
|
|
|
2023-05-01 21:18:34 +00:00
|
|
|
msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc"
|
2018-01-31 20:13:57 +00:00
|
|
|
"github.com/hashicorp/nomad/acl"
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2018-01-12 22:09:01 +00:00
|
|
|
"github.com/hashicorp/nomad/client"
|
|
|
|
"github.com/hashicorp/nomad/client/config"
|
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2018-01-15 22:48:53 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2018-01-31 20:13:57 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2018-01-12 22:09:01 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestClientStats_Stats_Local(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-01-12 22:09:01 +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-01-12 22:09:01 +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-01-12 22:09:01 +00:00
|
|
|
c.Servers = []string{s.config.RPCAddr.String()}
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupC()
|
2018-01-12 22:09:01 +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
|
2018-02-06 01:20:42 +00:00
|
|
|
req := &structs.NodeSpecificRequest{
|
2018-01-12 22:09:01 +00:00
|
|
|
QueryOptions: structs.QueryOptions{Region: "global"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp cstructs.ClientStatsResponse
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ClientStats.Stats", req, &resp)
|
|
|
|
require.NotNil(err)
|
|
|
|
require.Contains(err.Error(), "missing")
|
|
|
|
|
|
|
|
// Fetch the response setting the node id
|
|
|
|
req.NodeID = c.NodeID()
|
|
|
|
var resp2 cstructs.ClientStatsResponse
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "ClientStats.Stats", req, &resp2)
|
|
|
|
require.Nil(err)
|
|
|
|
require.NotNil(resp2.HostStats)
|
|
|
|
}
|
2018-01-15 22:48:53 +00:00
|
|
|
|
2018-01-31 20:13:57 +00:00
|
|
|
func TestClientStats_Stats_Local_ACL(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-01-31 20:13:57 +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-01-31 20:13:57 +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.PolicyRead)
|
|
|
|
tokenGood := mock.CreatePolicyAndToken(t, s.State(), 1009, "valid2", policyGood)
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Token string
|
|
|
|
ExpectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "bad token",
|
|
|
|
Token: tokenBad.SecretID,
|
|
|
|
ExpectedError: structs.ErrPermissionDenied.Error(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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
|
2018-02-06 01:20:42 +00:00
|
|
|
req := &structs.NodeSpecificRequest{
|
2018-01-31 20:13:57 +00:00
|
|
|
NodeID: uuid.Generate(),
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
AuthToken: c.Token,
|
|
|
|
Region: "global",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp cstructs.ClientStatsResponse
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ClientStats.Stats", req, &resp)
|
|
|
|
require.NotNil(err)
|
|
|
|
require.Contains(err.Error(), c.ExpectedError)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-15 22:48:53 +00:00
|
|
|
func TestClientStats_Stats_NoNode(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-01-15 22:48:53 +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-01-15 22:48:53 +00:00
|
|
|
codec := rpcClient(t, s)
|
|
|
|
testutil.WaitForLeader(t, s.RPC)
|
|
|
|
|
2018-04-06 18:33:08 +00:00
|
|
|
// Make the request with a nonexistent node-id
|
2018-02-06 01:20:42 +00:00
|
|
|
req := &structs.NodeSpecificRequest{
|
2018-01-15 22:48:53 +00:00
|
|
|
NodeID: uuid.Generate(),
|
|
|
|
QueryOptions: structs.QueryOptions{Region: "global"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp cstructs.ClientStatsResponse
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ClientStats.Stats", req, &resp)
|
|
|
|
require.Nil(resp.HostStats)
|
|
|
|
require.NotNil(err)
|
|
|
|
require.Contains(err.Error(), "Unknown node")
|
|
|
|
}
|
|
|
|
|
2018-04-06 18:33:08 +00:00
|
|
|
func TestClientStats_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
|
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(structs.MsgTypeTestSetup, 1005, node))
|
2018-04-06 18:33:08 +00:00
|
|
|
|
|
|
|
req := &structs.NodeSpecificRequest{
|
|
|
|
NodeID: node.ID,
|
|
|
|
QueryOptions: structs.QueryOptions{Region: "global"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
var resp cstructs.ClientStatsResponse
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ClientStats.Stats", req, &resp)
|
|
|
|
require.True(structs.IsErrNodeLacksRpc(err), err.Error())
|
|
|
|
}
|
|
|
|
|
2018-01-15 22:48:53 +00:00
|
|
|
func TestClientStats_Stats_Remote(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-01-15 22:48:53 +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-01-15 22:48:53 +00:00
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupS2()
|
2018-01-15 22:48:53 +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-01-15 22:48:53 +00:00
|
|
|
c.Servers = []string{s2.config.RPCAddr.String()}
|
|
|
|
})
|
2018-10-29 21:21:05 +00:00
|
|
|
defer cleanup()
|
2018-01-15 22:48:53 +00:00
|
|
|
|
2018-12-20 05:53:44 +00:00
|
|
|
// Wait for client initialization
|
|
|
|
select {
|
|
|
|
case <-c.Ready():
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
require.Fail("client timedout on initialize")
|
|
|
|
}
|
|
|
|
|
2018-01-15 22:48:53 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
nodes := s2.connectedNodes()
|
|
|
|
return len(nodes) == 1, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("should have a clients")
|
|
|
|
})
|
|
|
|
|
2018-02-06 01:20:42 +00:00
|
|
|
// Force remove the connection locally in case it exists
|
|
|
|
s1.nodeConnsLock.Lock()
|
|
|
|
delete(s1.nodeConns, c.NodeID())
|
|
|
|
s1.nodeConnsLock.Unlock()
|
|
|
|
|
2018-01-15 22:48:53 +00:00
|
|
|
// Make the request without having a node-id
|
2018-02-06 01:20:42 +00:00
|
|
|
req := &structs.NodeSpecificRequest{
|
2018-01-15 22:48:53 +00:00
|
|
|
NodeID: uuid.Generate(),
|
|
|
|
QueryOptions: structs.QueryOptions{Region: "global"},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the response
|
|
|
|
req.NodeID = c.NodeID()
|
|
|
|
var resp cstructs.ClientStatsResponse
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ClientStats.Stats", req, &resp)
|
|
|
|
require.Nil(err)
|
|
|
|
require.NotNil(resp.HostStats)
|
|
|
|
}
|