github.com/manicqin/nomad@v0.9.5/client/rpc_test.go (about)

     1  package client
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/nomad/client/config"
     8  	"github.com/hashicorp/nomad/nomad"
     9  	"github.com/hashicorp/nomad/nomad/structs"
    10  	sconfig "github.com/hashicorp/nomad/nomad/structs/config"
    11  	"github.com/hashicorp/nomad/testutil"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestRpc_streamingRpcConn_badEndpoint(t *testing.T) {
    16  	t.Parallel()
    17  	require := require.New(t)
    18  
    19  	s1, cleanupS1 := nomad.TestServer(t, nil)
    20  	defer cleanupS1()
    21  	testutil.WaitForLeader(t, s1.RPC)
    22  
    23  	c, cleanupC := TestClient(t, func(c *config.Config) {
    24  		c.Servers = []string{s1.GetConfig().RPCAddr.String()}
    25  	})
    26  	defer cleanupC()
    27  
    28  	// Wait for the client to connect
    29  	testutil.WaitForResult(func() (bool, error) {
    30  		node, err := s1.State().NodeByID(nil, c.NodeID())
    31  		if err != nil {
    32  			return false, err
    33  		}
    34  		if node == nil {
    35  			return false, errors.New("no node")
    36  		}
    37  
    38  		return node.Status == structs.NodeStatusReady, errors.New("wrong status")
    39  	}, func(err error) {
    40  		t.Fatalf("should have a clients")
    41  	})
    42  
    43  	// Get the server
    44  	server := c.servers.FindServer()
    45  	require.NotNil(server)
    46  
    47  	conn, err := c.streamingRpcConn(server, "Bogus")
    48  	require.Nil(conn)
    49  	require.NotNil(err)
    50  	require.Contains(err.Error(), "Unknown rpc method: \"Bogus\"")
    51  }
    52  
    53  func TestRpc_streamingRpcConn_badEndpoint_TLS(t *testing.T) {
    54  	t.Parallel()
    55  	require := require.New(t)
    56  
    57  	const (
    58  		cafile  = "../helper/tlsutil/testdata/ca.pem"
    59  		foocert = "../helper/tlsutil/testdata/nomad-foo.pem"
    60  		fookey  = "../helper/tlsutil/testdata/nomad-foo-key.pem"
    61  	)
    62  
    63  	s1, cleanupS1 := nomad.TestServer(t, func(c *nomad.Config) {
    64  		c.Region = "regionFoo"
    65  		c.BootstrapExpect = 1
    66  		c.DevDisableBootstrap = true
    67  		c.TLSConfig = &sconfig.TLSConfig{
    68  			EnableHTTP:           true,
    69  			EnableRPC:            true,
    70  			VerifyServerHostname: true,
    71  			CAFile:               cafile,
    72  			CertFile:             foocert,
    73  			KeyFile:              fookey,
    74  		}
    75  	})
    76  	defer cleanupS1()
    77  	testutil.WaitForLeader(t, s1.RPC)
    78  
    79  	c, cleanupC := TestClient(t, func(c *config.Config) {
    80  		c.Region = "regionFoo"
    81  		c.Servers = []string{s1.GetConfig().RPCAddr.String()}
    82  		c.TLSConfig = &sconfig.TLSConfig{
    83  			EnableHTTP:           true,
    84  			EnableRPC:            true,
    85  			VerifyServerHostname: true,
    86  			CAFile:               cafile,
    87  			CertFile:             foocert,
    88  			KeyFile:              fookey,
    89  		}
    90  	})
    91  	defer cleanupC()
    92  
    93  	// Wait for the client to connect
    94  	testutil.WaitForResult(func() (bool, error) {
    95  		node, err := s1.State().NodeByID(nil, c.NodeID())
    96  		if err != nil {
    97  			return false, err
    98  		}
    99  		if node == nil {
   100  			return false, errors.New("no node")
   101  		}
   102  
   103  		return node.Status == structs.NodeStatusReady, errors.New("wrong status")
   104  	}, func(err error) {
   105  		t.Fatalf("should have a clients")
   106  	})
   107  
   108  	// Get the server
   109  	server := c.servers.FindServer()
   110  	require.NotNil(server)
   111  
   112  	conn, err := c.streamingRpcConn(server, "Bogus")
   113  	require.Nil(conn)
   114  	require.NotNil(err)
   115  	require.Contains(err.Error(), "Unknown rpc method: \"Bogus\"")
   116  }