github.com/smithx10/nomad@v0.9.1-rc1/client/client_stats_endpoint_test.go (about)

     1  package client
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/nomad/acl"
     7  	"github.com/hashicorp/nomad/client/config"
     8  	"github.com/hashicorp/nomad/client/structs"
     9  	"github.com/hashicorp/nomad/nomad/mock"
    10  	nstructs "github.com/hashicorp/nomad/nomad/structs"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestClientStats_Stats(t *testing.T) {
    15  	t.Parallel()
    16  	require := require.New(t)
    17  	client, cleanup := TestClient(t, nil)
    18  	defer cleanup()
    19  
    20  	req := &nstructs.NodeSpecificRequest{}
    21  	var resp structs.ClientStatsResponse
    22  	require.Nil(client.ClientRPC("ClientStats.Stats", &req, &resp))
    23  	require.NotNil(resp.HostStats)
    24  	require.NotNil(resp.HostStats.AllocDirStats)
    25  	require.NotZero(resp.HostStats.Uptime)
    26  }
    27  
    28  func TestClientStats_Stats_ACL(t *testing.T) {
    29  	t.Parallel()
    30  	require := require.New(t)
    31  	server, addr, root := testACLServer(t, nil)
    32  	defer server.Shutdown()
    33  
    34  	client, cleanup := TestClient(t, func(c *config.Config) {
    35  		c.Servers = []string{addr}
    36  		c.ACLEnabled = true
    37  	})
    38  	defer cleanup()
    39  
    40  	// Try request without a token and expect failure
    41  	{
    42  		req := &nstructs.NodeSpecificRequest{}
    43  		var resp structs.ClientStatsResponse
    44  		err := client.ClientRPC("ClientStats.Stats", &req, &resp)
    45  		require.NotNil(err)
    46  		require.EqualError(err, nstructs.ErrPermissionDenied.Error())
    47  	}
    48  
    49  	// Try request with an invalid token and expect failure
    50  	{
    51  		token := mock.CreatePolicyAndToken(t, server.State(), 1005, "invalid", mock.NodePolicy(acl.PolicyDeny))
    52  		req := &nstructs.NodeSpecificRequest{}
    53  		req.AuthToken = token.SecretID
    54  
    55  		var resp structs.ClientStatsResponse
    56  		err := client.ClientRPC("ClientStats.Stats", &req, &resp)
    57  
    58  		require.NotNil(err)
    59  		require.EqualError(err, nstructs.ErrPermissionDenied.Error())
    60  	}
    61  
    62  	// Try request with a valid token
    63  	{
    64  		token := mock.CreatePolicyAndToken(t, server.State(), 1007, "valid", mock.NodePolicy(acl.PolicyRead))
    65  		req := &nstructs.NodeSpecificRequest{}
    66  		req.AuthToken = token.SecretID
    67  
    68  		var resp structs.ClientStatsResponse
    69  		err := client.ClientRPC("ClientStats.Stats", &req, &resp)
    70  
    71  		require.Nil(err)
    72  		require.NotNil(resp.HostStats)
    73  	}
    74  
    75  	// Try request with a management token
    76  	{
    77  		req := &nstructs.NodeSpecificRequest{}
    78  		req.AuthToken = root.SecretID
    79  
    80  		var resp structs.ClientStatsResponse
    81  		err := client.ClientRPC("ClientStats.Stats", &req, &resp)
    82  
    83  		require.Nil(err)
    84  		require.NotNil(resp.HostStats)
    85  	}
    86  }