Test RPC from server

This commit is contained in:
Alex Dadgar
2018-01-12 14:09:01 -08:00
parent d15bb76538
commit 9c570daaa8
2 changed files with 53 additions and 1 deletions

View File

@@ -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

View File

@@ -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)
}