open-nomad/client/client_stats_endpoint_test.go

87 lines
2.3 KiB
Go
Raw Normal View History

2018-01-11 19:24:57 +00:00
package client
import (
"testing"
"github.com/hashicorp/nomad/acl"
"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) {
t.Parallel()
require := require.New(t)
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) {
t.Parallel()
require := require.New(t)
server, addr, root := testACLServer(t, nil)
defer server.Shutdown()
client, cleanup := TestClient(t, func(c *config.Config) {
2018-01-11 19:24:57 +00:00
c.Servers = []string{addr}
c.ACLEnabled = true
})
defer cleanup()
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)
}
}