github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/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.TLSConfig = &sconfig.TLSConfig{ 67 EnableHTTP: true, 68 EnableRPC: true, 69 VerifyServerHostname: true, 70 CAFile: cafile, 71 CertFile: foocert, 72 KeyFile: fookey, 73 } 74 }) 75 defer cleanupS1() 76 testutil.WaitForLeader(t, s1.RPC) 77 78 c, cleanupC := TestClient(t, func(c *config.Config) { 79 c.Region = "regionFoo" 80 c.Servers = []string{s1.GetConfig().RPCAddr.String()} 81 c.TLSConfig = &sconfig.TLSConfig{ 82 EnableHTTP: true, 83 EnableRPC: true, 84 VerifyServerHostname: true, 85 CAFile: cafile, 86 CertFile: foocert, 87 KeyFile: fookey, 88 } 89 }) 90 defer cleanupC() 91 92 // Wait for the client to connect 93 testutil.WaitForResult(func() (bool, error) { 94 node, err := s1.State().NodeByID(nil, c.NodeID()) 95 if err != nil { 96 return false, err 97 } 98 if node == nil { 99 return false, errors.New("no node") 100 } 101 102 return node.Status == structs.NodeStatusReady, errors.New("wrong status") 103 }, func(err error) { 104 t.Fatalf("should have a clients") 105 }) 106 107 // Get the server 108 server := c.servers.FindServer() 109 require.NotNil(server) 110 111 conn, err := c.streamingRpcConn(server, "Bogus") 112 require.Nil(conn) 113 require.NotNil(err) 114 require.Contains(err.Error(), "Unknown rpc method: \"Bogus\"") 115 }