2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-06-04 11:26:16 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-04-17 21:02:05 +00:00
|
|
|
msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc/v2"
|
2017-10-10 17:36:54 +00:00
|
|
|
"github.com/hashicorp/nomad/acl"
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2018-01-13 00:52:24 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2017-10-10 17:36:54 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2015-08-15 19:59:10 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-06-04 11:26:16 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2017-10-10 17:36:54 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-01-13 00:52:24 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-06-04 11:26:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStatusPing(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-12-04 00:15:11 +00:00
|
|
|
|
|
|
|
s1, cleanupS1 := TestServer(t, nil)
|
|
|
|
defer cleanupS1()
|
2015-06-04 11:26:16 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
|
2023-01-24 15:52:07 +00:00
|
|
|
arg := &structs.GenericRequest{}
|
2015-06-04 11:26:16 +00:00
|
|
|
var out struct{}
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Status.Ping", arg, &out); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatusLeader(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-12-04 00:15:11 +00:00
|
|
|
|
|
|
|
s1, cleanupS1 := TestServer(t, nil)
|
|
|
|
defer cleanupS1()
|
2015-06-04 11:26:16 +00:00
|
|
|
codec := rpcClient(t, s1)
|
2015-08-15 19:59:10 +00:00
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
2015-06-04 11:26:16 +00:00
|
|
|
|
2015-09-07 01:07:05 +00:00
|
|
|
arg := &structs.GenericRequest{
|
|
|
|
QueryOptions: structs.QueryOptions{
|
2015-09-14 01:18:40 +00:00
|
|
|
Region: "global",
|
2015-09-07 01:07:05 +00:00
|
|
|
AllowStale: true,
|
|
|
|
},
|
|
|
|
}
|
2015-06-04 11:26:16 +00:00
|
|
|
var leader string
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Status.Leader", arg, &leader); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if leader == "" {
|
|
|
|
t.Fatalf("unexpected leader: %v", leader)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatusPeers(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-12-04 00:15:11 +00:00
|
|
|
|
|
|
|
s1, cleanupS1 := TestServer(t, nil)
|
|
|
|
defer cleanupS1()
|
2015-06-04 11:26:16 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
|
2015-09-07 01:07:05 +00:00
|
|
|
arg := &structs.GenericRequest{
|
|
|
|
QueryOptions: structs.QueryOptions{
|
2015-09-14 01:18:40 +00:00
|
|
|
Region: "global",
|
2015-09-07 01:07:05 +00:00
|
|
|
AllowStale: true,
|
|
|
|
},
|
|
|
|
}
|
2015-06-04 11:26:16 +00:00
|
|
|
var peers []string
|
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Status.Peers", arg, &peers); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if len(peers) != 1 {
|
|
|
|
t.Fatalf("no peers: %v", peers)
|
|
|
|
}
|
|
|
|
}
|
2017-10-10 17:36:54 +00:00
|
|
|
|
|
|
|
func TestStatusMembers(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-12-04 00:15:11 +00:00
|
|
|
|
|
|
|
s1, cleanupS1 := TestServer(t, nil)
|
|
|
|
defer cleanupS1()
|
2017-10-10 17:36:54 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
arg := &structs.GenericRequest{
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
AllowStale: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.ServerMembersResponse
|
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Status.Members", arg, &out))
|
|
|
|
assert.Len(out.Members, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStatusMembers_ACL(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-12-04 00:15:11 +00:00
|
|
|
|
|
|
|
s1, root, cleanupS1 := TestACLServer(t, nil)
|
|
|
|
defer cleanupS1()
|
2017-10-10 17:36:54 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
assert := assert.New(t)
|
|
|
|
state := s1.fsm.State()
|
|
|
|
|
|
|
|
// Create the namespace policy and tokens
|
|
|
|
validToken := mock.CreatePolicyAndToken(t, state, 1001, "test-valid", mock.NodePolicy(acl.PolicyRead))
|
|
|
|
invalidToken := mock.CreatePolicyAndToken(t, state, 1003, "test-invalid", mock.AgentPolicy(acl.PolicyRead))
|
|
|
|
|
|
|
|
arg := &structs.GenericRequest{
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
AllowStale: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try without a token and expect failure
|
|
|
|
{
|
|
|
|
var out structs.ServerMembersResponse
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Status.Members", arg, &out)
|
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with an invalid token and expect failure
|
|
|
|
{
|
2017-10-12 22:16:33 +00:00
|
|
|
arg.AuthToken = invalidToken.SecretID
|
2017-10-10 17:36:54 +00:00
|
|
|
var out structs.ServerMembersResponse
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Status.Members", arg, &out)
|
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Equal(err.Error(), structs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with a valid token
|
|
|
|
{
|
2017-10-12 22:16:33 +00:00
|
|
|
arg.AuthToken = validToken.SecretID
|
2017-10-10 17:36:54 +00:00
|
|
|
var out structs.ServerMembersResponse
|
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Status.Members", arg, &out))
|
|
|
|
assert.Len(out.Members, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try with a management token
|
|
|
|
{
|
2017-10-12 22:16:33 +00:00
|
|
|
arg.AuthToken = root.SecretID
|
2017-10-10 17:36:54 +00:00
|
|
|
var out structs.ServerMembersResponse
|
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Status.Members", arg, &out))
|
|
|
|
assert.Len(out.Members, 1)
|
|
|
|
}
|
|
|
|
}
|
2018-01-13 00:52:24 +00:00
|
|
|
|
|
|
|
func TestStatus_HasClientConn(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-12-04 00:15:11 +00:00
|
|
|
|
|
|
|
s1, cleanupS1 := TestServer(t, nil)
|
|
|
|
defer cleanupS1()
|
2018-01-13 00:52:24 +00:00
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
arg := &structs.NodeSpecificRequest{
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
AllowStale: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try without setting a node id
|
|
|
|
var out structs.NodeConnQueryResponse
|
|
|
|
require.NotNil(msgpackrpc.CallWithCodec(codec, "Status.HasNodeConn", arg, &out))
|
|
|
|
|
|
|
|
// Set a bad node id
|
|
|
|
arg.NodeID = uuid.Generate()
|
|
|
|
var out2 structs.NodeConnQueryResponse
|
|
|
|
require.Nil(msgpackrpc.CallWithCodec(codec, "Status.HasNodeConn", arg, &out2))
|
|
|
|
require.False(out2.Connected)
|
|
|
|
|
|
|
|
// Create a connection on that node
|
|
|
|
s1.addNodeConn(&RPCContext{
|
|
|
|
NodeID: arg.NodeID,
|
|
|
|
})
|
|
|
|
var out3 structs.NodeConnQueryResponse
|
|
|
|
require.Nil(msgpackrpc.CallWithCodec(codec, "Status.HasNodeConn", arg, &out3))
|
|
|
|
require.True(out3.Connected)
|
|
|
|
require.NotZero(out3.Established)
|
|
|
|
}
|