2018-01-11 19:24:57 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/acl"
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2018-01-11 19:24:57 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
|
|
|
"github.com/hashicorp/nomad/client/structs"
|
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
nstructs "github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestClientStats_Stats(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-01-11 19:24:57 +00:00
|
|
|
require := require.New(t)
|
2022-03-15 12:42:43 +00:00
|
|
|
|
2018-10-16 03:07:16 +00:00
|
|
|
client, cleanup := TestClient(t, nil)
|
|
|
|
defer cleanup()
|
2018-01-11 19:24:57 +00:00
|
|
|
|
2018-02-06 01:20:42 +00:00
|
|
|
req := &nstructs.NodeSpecificRequest{}
|
2018-01-11 19:24:57 +00:00
|
|
|
var resp structs.ClientStatsResponse
|
|
|
|
require.Nil(client.ClientRPC("ClientStats.Stats", &req, &resp))
|
|
|
|
require.NotNil(resp.HostStats)
|
|
|
|
require.NotNil(resp.HostStats.AllocDirStats)
|
|
|
|
require.NotZero(resp.HostStats.Uptime)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientStats_Stats_ACL(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-01-11 19:24:57 +00:00
|
|
|
require := require.New(t)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
server, addr, root, cleanupS := testACLServer(t, nil)
|
|
|
|
defer cleanupS()
|
|
|
|
|
|
|
|
client, cleanupC := TestClient(t, func(c *config.Config) {
|
2018-01-11 19:24:57 +00:00
|
|
|
c.Servers = []string{addr}
|
|
|
|
c.ACLEnabled = true
|
|
|
|
})
|
2019-12-04 00:15:11 +00:00
|
|
|
defer cleanupC()
|
2018-01-11 19:24:57 +00:00
|
|
|
|
|
|
|
// Try request without a token and expect failure
|
|
|
|
{
|
2018-02-06 01:20:42 +00:00
|
|
|
req := &nstructs.NodeSpecificRequest{}
|
2018-01-11 19:24:57 +00:00
|
|
|
var resp structs.ClientStatsResponse
|
|
|
|
err := client.ClientRPC("ClientStats.Stats", &req, &resp)
|
|
|
|
require.NotNil(err)
|
|
|
|
require.EqualError(err, nstructs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with an invalid token and expect failure
|
|
|
|
{
|
|
|
|
token := mock.CreatePolicyAndToken(t, server.State(), 1005, "invalid", mock.NodePolicy(acl.PolicyDeny))
|
2018-02-06 01:20:42 +00:00
|
|
|
req := &nstructs.NodeSpecificRequest{}
|
2018-01-11 19:24:57 +00:00
|
|
|
req.AuthToken = token.SecretID
|
|
|
|
|
|
|
|
var resp structs.ClientStatsResponse
|
|
|
|
err := client.ClientRPC("ClientStats.Stats", &req, &resp)
|
|
|
|
|
|
|
|
require.NotNil(err)
|
|
|
|
require.EqualError(err, nstructs.ErrPermissionDenied.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a valid token
|
|
|
|
{
|
|
|
|
token := mock.CreatePolicyAndToken(t, server.State(), 1007, "valid", mock.NodePolicy(acl.PolicyRead))
|
2018-02-06 01:20:42 +00:00
|
|
|
req := &nstructs.NodeSpecificRequest{}
|
2018-01-11 19:24:57 +00:00
|
|
|
req.AuthToken = token.SecretID
|
|
|
|
|
|
|
|
var resp structs.ClientStatsResponse
|
|
|
|
err := client.ClientRPC("ClientStats.Stats", &req, &resp)
|
|
|
|
|
|
|
|
require.Nil(err)
|
|
|
|
require.NotNil(resp.HostStats)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try request with a management token
|
|
|
|
{
|
2018-02-06 01:20:42 +00:00
|
|
|
req := &nstructs.NodeSpecificRequest{}
|
2018-01-11 19:24:57 +00:00
|
|
|
req.AuthToken = root.SecretID
|
|
|
|
|
|
|
|
var resp structs.ClientStatsResponse
|
|
|
|
err := client.ClientRPC("ClientStats.Stats", &req, &resp)
|
|
|
|
|
|
|
|
require.Nil(err)
|
|
|
|
require.NotNil(resp.HostStats)
|
|
|
|
}
|
|
|
|
}
|