github.com/djenriquez/nomad-1@v0.8.1/nomad/client_rpc_test.go (about) 1 package nomad 2 3 import ( 4 "net" 5 "testing" 6 7 "github.com/hashicorp/nomad/client" 8 "github.com/hashicorp/nomad/client/config" 9 "github.com/hashicorp/nomad/helper/uuid" 10 "github.com/hashicorp/nomad/nomad/structs" 11 "github.com/hashicorp/nomad/testutil" 12 "github.com/stretchr/testify/require" 13 ) 14 15 type namedConnWrapper struct { 16 net.Conn 17 name string 18 } 19 20 type namedAddr string 21 22 func (n namedAddr) String() string { return string(n) } 23 func (n namedAddr) Network() string { return string(n) } 24 25 func (n namedConnWrapper) LocalAddr() net.Addr { 26 return namedAddr(n.name) 27 } 28 29 func TestServer_removeNodeConn_differentAddrs(t *testing.T) { 30 t.Parallel() 31 require := require.New(t) 32 s1 := TestServer(t, nil) 33 defer s1.Shutdown() 34 testutil.WaitForLeader(t, s1.RPC) 35 36 p1, p2 := net.Pipe() 37 w1 := namedConnWrapper{ 38 Conn: p1, 39 name: "a", 40 } 41 w2 := namedConnWrapper{ 42 Conn: p2, 43 name: "b", 44 } 45 46 // Add the connections 47 nodeID := uuid.Generate() 48 ctx1 := &RPCContext{ 49 Conn: w1, 50 NodeID: nodeID, 51 } 52 ctx2 := &RPCContext{ 53 Conn: w2, 54 NodeID: nodeID, 55 } 56 57 s1.addNodeConn(ctx1) 58 s1.addNodeConn(ctx2) 59 require.Len(s1.connectedNodes(), 1) 60 61 // Check that the value is the second conn. 62 state, ok := s1.getNodeConn(nodeID) 63 require.True(ok) 64 require.Equal(state.Ctx.Conn.LocalAddr().String(), w2.name) 65 66 // Delete the first 67 s1.removeNodeConn(ctx1) 68 require.Len(s1.connectedNodes(), 1) 69 70 // Check that the value is the second conn. 71 state, ok = s1.getNodeConn(nodeID) 72 require.True(ok) 73 require.Equal(state.Ctx.Conn.LocalAddr().String(), w2.name) 74 75 // Delete the second 76 s1.removeNodeConn(ctx2) 77 require.Len(s1.connectedNodes(), 0) 78 } 79 80 func TestServerWithNodeConn_NoPath(t *testing.T) { 81 t.Parallel() 82 require := require.New(t) 83 s1 := TestServer(t, nil) 84 defer s1.Shutdown() 85 s2 := TestServer(t, func(c *Config) { 86 c.DevDisableBootstrap = true 87 }) 88 defer s2.Shutdown() 89 TestJoin(t, s1, s2) 90 testutil.WaitForLeader(t, s1.RPC) 91 testutil.WaitForLeader(t, s2.RPC) 92 93 nodeID := uuid.Generate() 94 srv, err := s1.serverWithNodeConn(nodeID, s1.Region()) 95 require.Nil(srv) 96 require.EqualError(err, structs.ErrNoNodeConn.Error()) 97 } 98 99 func TestServerWithNodeConn_NoPath_Region(t *testing.T) { 100 t.Parallel() 101 require := require.New(t) 102 s1 := TestServer(t, nil) 103 defer s1.Shutdown() 104 testutil.WaitForLeader(t, s1.RPC) 105 106 nodeID := uuid.Generate() 107 srv, err := s1.serverWithNodeConn(nodeID, "fake-region") 108 require.Nil(srv) 109 require.EqualError(err, structs.ErrNoRegionPath.Error()) 110 } 111 112 func TestServerWithNodeConn_Path(t *testing.T) { 113 t.Parallel() 114 require := require.New(t) 115 s1 := TestServer(t, nil) 116 defer s1.Shutdown() 117 s2 := TestServer(t, func(c *Config) { 118 c.DevDisableBootstrap = true 119 }) 120 defer s2.Shutdown() 121 TestJoin(t, s1, s2) 122 testutil.WaitForLeader(t, s1.RPC) 123 testutil.WaitForLeader(t, s2.RPC) 124 125 // Create a fake connection for the node on server 2 126 nodeID := uuid.Generate() 127 s2.addNodeConn(&RPCContext{ 128 NodeID: nodeID, 129 }) 130 131 srv, err := s1.serverWithNodeConn(nodeID, s1.Region()) 132 require.NotNil(srv) 133 require.Equal(srv.Addr.String(), s2.config.RPCAddr.String()) 134 require.Nil(err) 135 } 136 137 func TestServerWithNodeConn_Path_Region(t *testing.T) { 138 t.Parallel() 139 require := require.New(t) 140 s1 := TestServer(t, nil) 141 defer s1.Shutdown() 142 s2 := TestServer(t, func(c *Config) { 143 c.Region = "two" 144 }) 145 defer s2.Shutdown() 146 TestJoin(t, s1, s2) 147 testutil.WaitForLeader(t, s1.RPC) 148 testutil.WaitForLeader(t, s2.RPC) 149 150 // Create a fake connection for the node on server 2 151 nodeID := uuid.Generate() 152 s2.addNodeConn(&RPCContext{ 153 NodeID: nodeID, 154 }) 155 156 srv, err := s1.serverWithNodeConn(nodeID, s2.Region()) 157 require.NotNil(srv) 158 require.Equal(srv.Addr.String(), s2.config.RPCAddr.String()) 159 require.Nil(err) 160 } 161 162 func TestServerWithNodeConn_Path_Newest(t *testing.T) { 163 t.Parallel() 164 require := require.New(t) 165 s1 := TestServer(t, nil) 166 defer s1.Shutdown() 167 s2 := TestServer(t, func(c *Config) { 168 c.DevDisableBootstrap = true 169 }) 170 defer s2.Shutdown() 171 s3 := TestServer(t, func(c *Config) { 172 c.DevDisableBootstrap = true 173 }) 174 defer s3.Shutdown() 175 TestJoin(t, s1, s2, s3) 176 testutil.WaitForLeader(t, s1.RPC) 177 testutil.WaitForLeader(t, s2.RPC) 178 testutil.WaitForLeader(t, s3.RPC) 179 180 // Create a fake connection for the node on server 2 and 3 181 nodeID := uuid.Generate() 182 s2.addNodeConn(&RPCContext{ 183 NodeID: nodeID, 184 }) 185 s3.addNodeConn(&RPCContext{ 186 NodeID: nodeID, 187 }) 188 189 srv, err := s1.serverWithNodeConn(nodeID, s1.Region()) 190 require.NotNil(srv) 191 require.Equal(srv.Addr.String(), s3.config.RPCAddr.String()) 192 require.Nil(err) 193 } 194 195 func TestServerWithNodeConn_PathAndErr(t *testing.T) { 196 t.Parallel() 197 require := require.New(t) 198 s1 := TestServer(t, nil) 199 defer s1.Shutdown() 200 s2 := TestServer(t, func(c *Config) { 201 c.DevDisableBootstrap = true 202 }) 203 defer s2.Shutdown() 204 s3 := TestServer(t, func(c *Config) { 205 c.DevDisableBootstrap = true 206 }) 207 defer s3.Shutdown() 208 TestJoin(t, s1, s2, s3) 209 testutil.WaitForLeader(t, s1.RPC) 210 testutil.WaitForLeader(t, s2.RPC) 211 testutil.WaitForLeader(t, s3.RPC) 212 213 // Create a fake connection for the node on server 2 214 nodeID := uuid.Generate() 215 s2.addNodeConn(&RPCContext{ 216 NodeID: nodeID, 217 }) 218 219 // Shutdown the RPC layer for server 3 220 s3.rpcListener.Close() 221 222 srv, err := s1.serverWithNodeConn(nodeID, s1.Region()) 223 require.NotNil(srv) 224 require.Equal(srv.Addr.String(), s2.config.RPCAddr.String()) 225 require.Nil(err) 226 } 227 228 func TestServerWithNodeConn_NoPathAndErr(t *testing.T) { 229 t.Parallel() 230 require := require.New(t) 231 s1 := TestServer(t, nil) 232 defer s1.Shutdown() 233 s2 := TestServer(t, func(c *Config) { 234 c.DevDisableBootstrap = true 235 }) 236 defer s2.Shutdown() 237 s3 := TestServer(t, func(c *Config) { 238 c.DevDisableBootstrap = true 239 }) 240 defer s3.Shutdown() 241 TestJoin(t, s1, s2, s3) 242 testutil.WaitForLeader(t, s1.RPC) 243 testutil.WaitForLeader(t, s2.RPC) 244 testutil.WaitForLeader(t, s3.RPC) 245 246 // Shutdown the RPC layer for server 3 247 s3.rpcListener.Close() 248 249 srv, err := s1.serverWithNodeConn(uuid.Generate(), s1.Region()) 250 require.Nil(srv) 251 require.NotNil(err) 252 require.Contains(err.Error(), "failed querying") 253 } 254 255 func TestNodeStreamingRpc_badEndpoint(t *testing.T) { 256 t.Parallel() 257 require := require.New(t) 258 s1 := TestServer(t, nil) 259 defer s1.Shutdown() 260 testutil.WaitForLeader(t, s1.RPC) 261 262 c := client.TestClient(t, func(c *config.Config) { 263 c.Servers = []string{s1.config.RPCAddr.String()} 264 }) 265 defer c.Shutdown() 266 267 // Wait for the client to connect 268 testutil.WaitForResult(func() (bool, error) { 269 nodes := s1.connectedNodes() 270 return len(nodes) == 1, nil 271 }, func(err error) { 272 t.Fatalf("should have a clients") 273 }) 274 275 state, ok := s1.getNodeConn(c.NodeID()) 276 require.True(ok) 277 278 conn, err := NodeStreamingRpc(state.Session, "Bogus") 279 require.Nil(conn) 280 require.NotNil(err) 281 require.Contains(err.Error(), "Bogus") 282 require.True(structs.IsErrUnknownMethod(err)) 283 }