open-nomad/client/client_stats_endpoint_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

93 lines
2.4 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
2018-01-11 19:24:57 +00:00
package client
import (
"testing"
"github.com/hashicorp/nomad/acl"
"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) {
ci.Parallel(t)
2018-01-11 19:24:57 +00:00
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) {
ci.Parallel(t)
2018-01-11 19:24:57 +00:00
require := require.New(t)
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
})
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)
}
}