diff --git a/client/rpc.go b/client/rpc.go index 0e9390f4d..8fa24c495 100644 --- a/client/rpc.go +++ b/client/rpc.go @@ -61,7 +61,6 @@ func (c *Client) RPC(method string, args interface{}, reply interface{}) error { return mErr.ErrorOrNil() } -// TODO This can't really be tested until Servers can dial back to the client. // setupClientRpc is used to setup the Client's RPC endpoints func (c *Client) setupClientRpc() { // Initialize the RPC handlers diff --git a/nomad/client_stats_endpoint_test.go b/nomad/client_stats_endpoint_test.go new file mode 100644 index 000000000..a3fa7fb22 --- /dev/null +++ b/nomad/client_stats_endpoint_test.go @@ -0,0 +1,53 @@ +package nomad + +import ( + "testing" + + msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc" + "github.com/hashicorp/nomad/client" + "github.com/hashicorp/nomad/client/config" + cstructs "github.com/hashicorp/nomad/client/structs" + "github.com/hashicorp/nomad/nomad/structs" + "github.com/hashicorp/nomad/testutil" + "github.com/stretchr/testify/require" +) + +func TestClientStats_Stats_Local(t *testing.T) { + t.Parallel() + require := require.New(t) + + // Start a server and client + s := TestServer(t, nil) + defer s.Shutdown() + codec := rpcClient(t, s) + testutil.WaitForLeader(t, s.RPC) + + c := client.TestClient(t, func(c *config.Config) { + c.Servers = []string{s.config.RPCAddr.String()} + }) + + testutil.WaitForResult(func() (bool, error) { + nodes := s.connectedNodes() + return len(nodes) == 1, nil + }, func(err error) { + t.Fatalf("should have a clients") + }) + + // Make the request without having a node-id + req := &cstructs.ClientStatsRequest{ + QueryOptions: structs.QueryOptions{Region: "global"}, + } + + // Fetch the response + var resp cstructs.ClientStatsResponse + err := msgpackrpc.CallWithCodec(codec, "ClientStats.Stats", req, &resp) + require.NotNil(err) + require.Contains(err.Error(), "missing") + + // Fetch the response setting the node id + req.NodeID = c.NodeID() + var resp2 cstructs.ClientStatsResponse + err = msgpackrpc.CallWithCodec(codec, "ClientStats.Stats", req, &resp2) + require.Nil(err) + require.NotNil(resp2.HostStats) +}