github.imxd.top/hashicorp/consul@v1.4.5/agent/consul/server_test.go (about) 1 package consul 2 3 import ( 4 "fmt" 5 "log" 6 "net" 7 "os" 8 "strings" 9 "sync/atomic" 10 "testing" 11 "time" 12 13 "github.com/hashicorp/consul/agent/connect" 14 "github.com/hashicorp/consul/agent/metadata" 15 "github.com/hashicorp/consul/agent/structs" 16 "github.com/hashicorp/consul/agent/token" 17 "github.com/hashicorp/consul/lib/freeport" 18 "github.com/hashicorp/consul/testrpc" 19 "github.com/hashicorp/consul/testutil" 20 "github.com/hashicorp/consul/testutil/retry" 21 "github.com/hashicorp/consul/tlsutil" 22 "github.com/hashicorp/consul/types" 23 "github.com/hashicorp/go-uuid" 24 25 "github.com/stretchr/testify/require" 26 ) 27 28 func configureTLS(config *Config) { 29 config.CAFile = "../../test/ca/root.cer" 30 config.CertFile = "../../test/key/ourdomain.cer" 31 config.KeyFile = "../../test/key/ourdomain.key" 32 } 33 34 var id int64 35 36 func uniqueNodeName(name string) string { 37 return fmt.Sprintf("%s-node-%d", name, atomic.AddInt64(&id, 1)) 38 } 39 40 func testServerConfig(t *testing.T) (string, *Config) { 41 dir := testutil.TempDir(t, "consul") 42 config := DefaultConfig() 43 44 ports := freeport.Get(3) 45 config.NodeName = uniqueNodeName(t.Name()) 46 config.Bootstrap = true 47 config.Datacenter = "dc1" 48 config.DataDir = dir 49 config.LogOutput = testutil.TestWriter(t) 50 51 // bind the rpc server to a random port. config.RPCAdvertise will be 52 // set to the listen address unless it was set in the configuration. 53 // In that case get the address from srv.Listener.Addr(). 54 config.RPCAddr = &net.TCPAddr{IP: []byte{127, 0, 0, 1}, Port: ports[0]} 55 56 nodeID, err := uuid.GenerateUUID() 57 if err != nil { 58 t.Fatal(err) 59 } 60 config.NodeID = types.NodeID(nodeID) 61 62 // set the memberlist bind port to 0 to bind to a random port. 63 // memberlist will update the value of BindPort after bind 64 // to the actual value. 65 config.SerfLANConfig.MemberlistConfig.BindAddr = "127.0.0.1" 66 config.SerfLANConfig.MemberlistConfig.BindPort = ports[1] 67 config.SerfLANConfig.MemberlistConfig.AdvertisePort = ports[1] 68 config.SerfLANConfig.MemberlistConfig.SuspicionMult = 2 69 config.SerfLANConfig.MemberlistConfig.ProbeTimeout = 50 * time.Millisecond 70 config.SerfLANConfig.MemberlistConfig.ProbeInterval = 100 * time.Millisecond 71 config.SerfLANConfig.MemberlistConfig.GossipInterval = 100 * time.Millisecond 72 73 config.SerfWANConfig.MemberlistConfig.BindAddr = "127.0.0.1" 74 config.SerfWANConfig.MemberlistConfig.BindPort = ports[2] 75 config.SerfWANConfig.MemberlistConfig.AdvertisePort = ports[2] 76 config.SerfWANConfig.MemberlistConfig.SuspicionMult = 2 77 config.SerfWANConfig.MemberlistConfig.ProbeTimeout = 50 * time.Millisecond 78 config.SerfWANConfig.MemberlistConfig.ProbeInterval = 100 * time.Millisecond 79 config.SerfWANConfig.MemberlistConfig.GossipInterval = 100 * time.Millisecond 80 81 config.RaftConfig.LeaderLeaseTimeout = 100 * time.Millisecond 82 config.RaftConfig.HeartbeatTimeout = 200 * time.Millisecond 83 config.RaftConfig.ElectionTimeout = 200 * time.Millisecond 84 85 config.ReconcileInterval = 300 * time.Millisecond 86 87 config.AutopilotConfig.ServerStabilizationTime = 100 * time.Millisecond 88 config.ServerHealthInterval = 50 * time.Millisecond 89 config.AutopilotInterval = 100 * time.Millisecond 90 91 config.Build = "1.4.0" 92 93 config.CoordinateUpdatePeriod = 100 * time.Millisecond 94 config.LeaveDrainTime = 1 * time.Millisecond 95 96 // TODO (slackpad) - We should be able to run all tests w/o this, but it 97 // looks like several depend on it. 98 config.RPCHoldTimeout = 5 * time.Second 99 100 config.ConnectEnabled = true 101 config.CAConfig = &structs.CAConfiguration{ 102 ClusterID: connect.TestClusterID, 103 Provider: structs.ConsulCAProvider, 104 Config: map[string]interface{}{ 105 "PrivateKey": "", 106 "RootCert": "", 107 "RotationPeriod": "2160h", 108 "LeafCertTTL": "72h", 109 }, 110 } 111 112 return dir, config 113 } 114 115 func testServer(t *testing.T) (string, *Server) { 116 return testServerWithConfig(t, func(c *Config) { 117 c.Datacenter = "dc1" 118 c.Bootstrap = true 119 }) 120 } 121 122 func testServerDC(t *testing.T, dc string) (string, *Server) { 123 return testServerWithConfig(t, func(c *Config) { 124 c.Datacenter = dc 125 c.Bootstrap = true 126 }) 127 } 128 129 func testServerDCBootstrap(t *testing.T, dc string, bootstrap bool) (string, *Server) { 130 return testServerWithConfig(t, func(c *Config) { 131 c.Datacenter = dc 132 c.Bootstrap = bootstrap 133 }) 134 } 135 136 func testServerDCExpect(t *testing.T, dc string, expect int) (string, *Server) { 137 return testServerWithConfig(t, func(c *Config) { 138 c.Datacenter = dc 139 c.Bootstrap = false 140 c.BootstrapExpect = expect 141 }) 142 } 143 144 func testServerDCExpectNonVoter(t *testing.T, dc string, expect int) (string, *Server) { 145 return testServerWithConfig(t, func(c *Config) { 146 c.Datacenter = dc 147 c.Bootstrap = false 148 c.BootstrapExpect = expect 149 c.NonVoter = true 150 }) 151 } 152 153 func testServerWithConfig(t *testing.T, cb func(*Config)) (string, *Server) { 154 dir, config := testServerConfig(t) 155 if cb != nil { 156 cb(config) 157 } 158 srv, err := newServer(config) 159 if err != nil { 160 t.Fatalf("err: %v", err) 161 } 162 return dir, srv 163 } 164 165 func newServer(c *Config) (*Server, error) { 166 // chain server up notification 167 oldNotify := c.NotifyListen 168 up := make(chan struct{}) 169 c.NotifyListen = func() { 170 close(up) 171 if oldNotify != nil { 172 oldNotify() 173 } 174 } 175 176 // start server 177 w := c.LogOutput 178 if w == nil { 179 w = os.Stderr 180 } 181 logger := log.New(w, c.NodeName+" - ", log.LstdFlags|log.Lmicroseconds) 182 tlsConf, err := tlsutil.NewConfigurator(c.ToTLSUtilConfig(), logger) 183 if err != nil { 184 return nil, err 185 } 186 srv, err := NewServerLogger(c, logger, new(token.Store), tlsConf) 187 if err != nil { 188 return nil, err 189 } 190 191 // wait until after listen 192 <-up 193 194 // get the real address 195 // 196 // the server already sets the RPCAdvertise address 197 // if it wasn't configured since it needs it for 198 // some initialization 199 // 200 // todo(fs): setting RPCAddr should probably be guarded 201 // todo(fs): but for now it is a shortcut to avoid fixing 202 // todo(fs): tests which depend on that value. They should 203 // todo(fs): just get the listener address instead. 204 c.RPCAddr = srv.Listener.Addr().(*net.TCPAddr) 205 return srv, nil 206 } 207 208 func TestServer_StartStop(t *testing.T) { 209 t.Parallel() 210 // Start up a server and then stop it. 211 dir1, s1 := testServer(t) 212 defer os.RemoveAll(dir1) 213 if err := s1.Shutdown(); err != nil { 214 t.Fatalf("err: %v", err) 215 } 216 217 // Shut down again, which should be idempotent. 218 if err := s1.Shutdown(); err != nil { 219 t.Fatalf("err: %v", err) 220 } 221 } 222 223 func TestServer_JoinLAN(t *testing.T) { 224 t.Parallel() 225 dir1, s1 := testServer(t) 226 defer os.RemoveAll(dir1) 227 defer s1.Shutdown() 228 229 dir2, s2 := testServer(t) 230 defer os.RemoveAll(dir2) 231 defer s2.Shutdown() 232 233 // Try to join 234 joinLAN(t, s2, s1) 235 retry.Run(t, func(r *retry.R) { 236 if got, want := len(s1.LANMembers()), 2; got != want { 237 r.Fatalf("got %d s1 LAN members want %d", got, want) 238 } 239 if got, want := len(s2.LANMembers()), 2; got != want { 240 r.Fatalf("got %d s2 LAN members want %d", got, want) 241 } 242 }) 243 } 244 245 func TestServer_LANReap(t *testing.T) { 246 t.Parallel() 247 248 configureServer := func(c *Config) { 249 c.SerfFloodInterval = 100 * time.Millisecond 250 c.SerfLANConfig.ReconnectTimeout = 250 * time.Millisecond 251 c.SerfLANConfig.TombstoneTimeout = 250 * time.Millisecond 252 c.SerfLANConfig.ReapInterval = 300 * time.Millisecond 253 } 254 255 dir1, s1 := testServerWithConfig(t, func(c *Config) { 256 c.Datacenter = "dc1" 257 c.Bootstrap = true 258 configureServer(c) 259 }) 260 defer os.RemoveAll(dir1) 261 defer s1.Shutdown() 262 263 dir2, s2 := testServerWithConfig(t, func(c *Config) { 264 c.Datacenter = "dc1" 265 c.Bootstrap = false 266 configureServer(c) 267 }) 268 defer os.RemoveAll(dir2) 269 270 dir3, s3 := testServerWithConfig(t, func(c *Config) { 271 c.Datacenter = "dc1" 272 c.Bootstrap = false 273 configureServer(c) 274 }) 275 defer os.RemoveAll(dir3) 276 defer s3.Shutdown() 277 278 // Try to join 279 joinLAN(t, s2, s1) 280 joinLAN(t, s3, s1) 281 282 testrpc.WaitForLeader(t, s1.RPC, "dc1") 283 testrpc.WaitForLeader(t, s2.RPC, "dc1") 284 testrpc.WaitForLeader(t, s3.RPC, "dc1") 285 286 retry.Run(t, func(r *retry.R) { 287 require.Len(r, s1.LANMembers(), 3) 288 require.Len(r, s2.LANMembers(), 3) 289 require.Len(r, s3.LANMembers(), 3) 290 }) 291 292 // Check the router has both 293 retry.Run(t, func(r *retry.R) { 294 require.Len(r, s1.serverLookup.Servers(), 3) 295 require.Len(r, s2.serverLookup.Servers(), 3) 296 require.Len(r, s3.serverLookup.Servers(), 3) 297 }) 298 299 // shutdown the second dc 300 s2.Shutdown() 301 302 retry.Run(t, func(r *retry.R) { 303 require.Len(r, s1.LANMembers(), 2) 304 servers := s1.serverLookup.Servers() 305 require.Len(r, servers, 2) 306 // require.Equal(r, s1.config.NodeName, servers[0].Name) 307 }) 308 } 309 310 func TestServer_JoinWAN(t *testing.T) { 311 t.Parallel() 312 dir1, s1 := testServer(t) 313 defer os.RemoveAll(dir1) 314 defer s1.Shutdown() 315 316 dir2, s2 := testServerDC(t, "dc2") 317 defer os.RemoveAll(dir2) 318 defer s2.Shutdown() 319 320 // Try to join 321 joinWAN(t, s2, s1) 322 retry.Run(t, func(r *retry.R) { 323 if got, want := len(s1.WANMembers()), 2; got != want { 324 r.Fatalf("got %d s1 WAN members want %d", got, want) 325 } 326 if got, want := len(s2.WANMembers()), 2; got != want { 327 r.Fatalf("got %d s2 WAN members want %d", got, want) 328 } 329 }) 330 331 // Check the router has both 332 retry.Run(t, func(r *retry.R) { 333 if got, want := len(s1.router.GetDatacenters()), 2; got != want { 334 r.Fatalf("got %d routes want %d", got, want) 335 } 336 if got, want := len(s2.router.GetDatacenters()), 2; got != want { 337 r.Fatalf("got %d datacenters want %d", got, want) 338 } 339 }) 340 } 341 342 func TestServer_WANReap(t *testing.T) { 343 t.Parallel() 344 dir1, s1 := testServerWithConfig(t, func(c *Config) { 345 c.Datacenter = "dc1" 346 c.Bootstrap = true 347 c.SerfFloodInterval = 100 * time.Millisecond 348 c.SerfWANConfig.ReconnectTimeout = 250 * time.Millisecond 349 c.SerfWANConfig.TombstoneTimeout = 250 * time.Millisecond 350 c.SerfWANConfig.ReapInterval = 500 * time.Millisecond 351 }) 352 defer os.RemoveAll(dir1) 353 defer s1.Shutdown() 354 355 dir2, s2 := testServerDC(t, "dc2") 356 defer os.RemoveAll(dir2) 357 358 // Try to join 359 joinWAN(t, s2, s1) 360 retry.Run(t, func(r *retry.R) { 361 require.Len(r, s1.WANMembers(), 2) 362 require.Len(r, s2.WANMembers(), 2) 363 }) 364 365 // Check the router has both 366 retry.Run(t, func(r *retry.R) { 367 require.Len(r, s1.router.GetDatacenters(), 2) 368 require.Len(r, s2.router.GetDatacenters(), 2) 369 }) 370 371 // shutdown the second dc 372 s2.Shutdown() 373 374 retry.Run(t, func(r *retry.R) { 375 require.Len(r, s1.WANMembers(), 1) 376 datacenters := s1.router.GetDatacenters() 377 require.Len(r, datacenters, 1) 378 require.Equal(r, "dc1", datacenters[0]) 379 }) 380 381 } 382 383 func TestServer_JoinWAN_Flood(t *testing.T) { 384 t.Parallel() 385 // Set up two servers in a WAN. 386 dir1, s1 := testServerDCBootstrap(t, "dc1", true) 387 defer os.RemoveAll(dir1) 388 defer s1.Shutdown() 389 390 dir2, s2 := testServerDCBootstrap(t, "dc2", true) 391 defer os.RemoveAll(dir2) 392 defer s2.Shutdown() 393 394 joinWAN(t, s2, s1) 395 396 for _, s := range []*Server{s1, s2} { 397 retry.Run(t, func(r *retry.R) { 398 if got, want := len(s.WANMembers()), 2; got != want { 399 r.Fatalf("got %d WAN members want %d", got, want) 400 } 401 }) 402 } 403 404 dir3, s3 := testServerDCBootstrap(t, "dc1", false) 405 defer os.RemoveAll(dir3) 406 defer s3.Shutdown() 407 408 // Do just a LAN join for the new server and make sure it 409 // shows up in the WAN. 410 joinLAN(t, s3, s1) 411 412 for _, s := range []*Server{s1, s2, s3} { 413 retry.Run(t, func(r *retry.R) { 414 if got, want := len(s.WANMembers()), 3; got != want { 415 r.Fatalf("got %d WAN members for %s want %d", got, s.config.NodeName, want) 416 } 417 }) 418 } 419 } 420 421 func TestServer_JoinSeparateLanAndWanAddresses(t *testing.T) { 422 t.Parallel() 423 dir1, s1 := testServerWithConfig(t, func(c *Config) { 424 c.NodeName = t.Name() + "-s1" 425 c.Datacenter = "dc1" 426 c.Bootstrap = true 427 c.SerfFloodInterval = 100 * time.Millisecond 428 }) 429 defer os.RemoveAll(dir1) 430 defer s1.Shutdown() 431 432 s2Name := t.Name() + "-s2" 433 dir2, s2 := testServerWithConfig(t, func(c *Config) { 434 c.NodeName = s2Name 435 c.Datacenter = "dc2" 436 c.Bootstrap = false 437 // This wan address will be expected to be seen on s1 438 c.SerfWANConfig.MemberlistConfig.AdvertiseAddr = "127.0.0.2" 439 // This lan address will be expected to be seen on s3 440 c.SerfLANConfig.MemberlistConfig.AdvertiseAddr = "127.0.0.3" 441 c.SerfFloodInterval = 100 * time.Millisecond 442 }) 443 444 defer os.RemoveAll(dir2) 445 defer s2.Shutdown() 446 447 dir3, s3 := testServerWithConfig(t, func(c *Config) { 448 c.NodeName = t.Name() + "-s3" 449 c.Datacenter = "dc2" 450 c.Bootstrap = true 451 c.SerfFloodInterval = 100 * time.Millisecond 452 }) 453 defer os.RemoveAll(dir3) 454 defer s3.Shutdown() 455 456 // Join s2 to s1 on wan 457 joinWAN(t, s2, s1) 458 459 // Join s3 to s2 on lan 460 joinLAN(t, s3, s2) 461 462 // We rely on flood joining to fill across the LAN, so we expect s3 to 463 // show up on the WAN as well, even though it's not explicitly joined. 464 retry.Run(t, func(r *retry.R) { 465 if got, want := len(s1.WANMembers()), 3; got != want { 466 r.Fatalf("got %d s1 WAN members want %d", got, want) 467 } 468 if got, want := len(s2.WANMembers()), 3; got != want { 469 r.Fatalf("got %d s2 WAN members want %d", got, want) 470 } 471 if got, want := len(s2.LANMembers()), 2; got != want { 472 r.Fatalf("got %d s2 LAN members want %d", got, want) 473 } 474 if got, want := len(s3.LANMembers()), 2; got != want { 475 r.Fatalf("got %d s3 LAN members want %d", got, want) 476 } 477 }) 478 479 // Check the router has both 480 retry.Run(t, func(r *retry.R) { 481 if len(s1.router.GetDatacenters()) != 2 { 482 r.Fatalf("remote consul missing") 483 } 484 if len(s2.router.GetDatacenters()) != 2 { 485 r.Fatalf("remote consul missing") 486 } 487 if len(s2.serverLookup.Servers()) != 2 { 488 r.Fatalf("local consul fellow s3 for s2 missing") 489 } 490 }) 491 492 // Get and check the wan address of s2 from s1 493 var s2WanAddr string 494 for _, member := range s1.WANMembers() { 495 if member.Name == s2Name+".dc2" { 496 s2WanAddr = member.Addr.String() 497 } 498 } 499 if s2WanAddr != "127.0.0.2" { 500 t.Fatalf("s1 sees s2 on a wrong address: %s, expecting: %s", s2WanAddr, "127.0.0.2") 501 } 502 503 // Get and check the lan address of s2 from s3 504 var s2LanAddr string 505 for _, lanmember := range s3.LANMembers() { 506 if lanmember.Name == s2Name { 507 s2LanAddr = lanmember.Addr.String() 508 } 509 } 510 if s2LanAddr != "127.0.0.3" { 511 t.Fatalf("s3 sees s2 on a wrong address: %s, expecting: %s", s2LanAddr, "127.0.0.3") 512 } 513 } 514 515 func TestServer_LeaveLeader(t *testing.T) { 516 t.Parallel() 517 dir1, s1 := testServer(t) 518 defer os.RemoveAll(dir1) 519 defer s1.Shutdown() 520 521 dir2, s2 := testServerDCBootstrap(t, "dc1", false) 522 defer os.RemoveAll(dir2) 523 defer s2.Shutdown() 524 525 dir3, s3 := testServerDCBootstrap(t, "dc1", false) 526 defer os.RemoveAll(dir3) 527 defer s3.Shutdown() 528 529 testrpc.WaitForLeader(t, s1.RPC, "dc1") 530 joinLAN(t, s2, s1) 531 joinLAN(t, s3, s1) 532 retry.Run(t, func(r *retry.R) { 533 r.Check(wantPeers(s1, 3)) 534 r.Check(wantPeers(s2, 3)) 535 r.Check(wantPeers(s3, 3)) 536 }) 537 // Issue a leave to the leader 538 var leader *Server 539 switch { 540 case s1.IsLeader(): 541 leader = s1 542 case s2.IsLeader(): 543 leader = s2 544 case s3.IsLeader(): 545 leader = s3 546 default: 547 t.Fatal("no leader") 548 } 549 if err := leader.Leave(); err != nil { 550 t.Fatal("leave failed: ", err) 551 } 552 553 // Should lose a peer 554 retry.Run(t, func(r *retry.R) { 555 r.Check(wantPeers(s1, 2)) 556 r.Check(wantPeers(s2, 2)) 557 r.Check(wantPeers(s3, 2)) 558 }) 559 } 560 561 func TestServer_Leave(t *testing.T) { 562 t.Parallel() 563 dir1, s1 := testServer(t) 564 defer os.RemoveAll(dir1) 565 defer s1.Shutdown() 566 567 // Second server not in bootstrap mode 568 dir2, s2 := testServerDCBootstrap(t, "dc1", false) 569 defer os.RemoveAll(dir2) 570 defer s2.Shutdown() 571 572 // Try to join 573 joinLAN(t, s2, s1) 574 575 testrpc.WaitForLeader(t, s1.RPC, "dc1") 576 testrpc.WaitForLeader(t, s2.RPC, "dc1") 577 578 // Issue a leave to the non-leader 579 var nonleader *Server 580 switch { 581 case s1.IsLeader(): 582 nonleader = s2 583 case s2.IsLeader(): 584 nonleader = s1 585 default: 586 t.Fatal("no leader") 587 } 588 if err := nonleader.Leave(); err != nil { 589 t.Fatal("leave failed: ", err) 590 } 591 592 // Should lose a peer 593 retry.Run(t, func(r *retry.R) { 594 r.Check(wantPeers(s1, 1)) 595 r.Check(wantPeers(s2, 1)) 596 }) 597 } 598 599 func TestServer_RPC(t *testing.T) { 600 t.Parallel() 601 dir1, s1 := testServer(t) 602 defer os.RemoveAll(dir1) 603 defer s1.Shutdown() 604 605 var out struct{} 606 if err := s1.RPC("Status.Ping", struct{}{}, &out); err != nil { 607 t.Fatalf("err: %v", err) 608 } 609 } 610 611 func TestServer_JoinLAN_TLS(t *testing.T) { 612 t.Parallel() 613 dir1, conf1 := testServerConfig(t) 614 conf1.VerifyIncoming = true 615 conf1.VerifyOutgoing = true 616 configureTLS(conf1) 617 s1, err := newServer(conf1) 618 if err != nil { 619 t.Fatalf("err: %v", err) 620 } 621 defer os.RemoveAll(dir1) 622 defer s1.Shutdown() 623 testrpc.WaitForTestAgent(t, s1.RPC, "dc1") 624 625 dir2, conf2 := testServerConfig(t) 626 conf2.Bootstrap = false 627 conf2.VerifyIncoming = true 628 conf2.VerifyOutgoing = true 629 configureTLS(conf2) 630 s2, err := newServer(conf2) 631 if err != nil { 632 t.Fatalf("err: %v", err) 633 } 634 defer os.RemoveAll(dir2) 635 defer s2.Shutdown() 636 637 // Try to join 638 joinLAN(t, s2, s1) 639 testrpc.WaitForTestAgent(t, s2.RPC, "dc1") 640 641 // Verify Raft has established a peer 642 retry.Run(t, func(r *retry.R) { 643 r.Check(wantRaft([]*Server{s1, s2})) 644 }) 645 } 646 647 func TestServer_Expect(t *testing.T) { 648 t.Parallel() 649 // All test servers should be in expect=3 mode, except for the 3rd one, 650 // but one with expect=0 can cause a bootstrap to occur from the other 651 // servers as currently implemented. 652 dir1, s1 := testServerDCExpect(t, "dc1", 3) 653 defer os.RemoveAll(dir1) 654 defer s1.Shutdown() 655 656 dir2, s2 := testServerDCExpect(t, "dc1", 3) 657 defer os.RemoveAll(dir2) 658 defer s2.Shutdown() 659 660 dir3, s3 := testServerDCExpect(t, "dc1", 0) 661 defer os.RemoveAll(dir3) 662 defer s3.Shutdown() 663 664 dir4, s4 := testServerDCExpect(t, "dc1", 3) 665 defer os.RemoveAll(dir4) 666 defer s4.Shutdown() 667 668 // Join the first two servers. 669 joinLAN(t, s2, s1) 670 671 // Should have no peers yet since the bootstrap didn't occur. 672 retry.Run(t, func(r *retry.R) { 673 r.Check(wantPeers(s1, 0)) 674 r.Check(wantPeers(s2, 0)) 675 }) 676 677 // Join the third node. 678 joinLAN(t, s3, s1) 679 680 // Now we have three servers so we should bootstrap. 681 retry.Run(t, func(r *retry.R) { 682 r.Check(wantPeers(s1, 3)) 683 r.Check(wantPeers(s2, 3)) 684 r.Check(wantPeers(s3, 3)) 685 }) 686 687 // Make sure a leader is elected, grab the current term and then add in 688 // the fourth server. 689 testrpc.WaitForLeader(t, s1.RPC, "dc1") 690 termBefore := s1.raft.Stats()["last_log_term"] 691 joinLAN(t, s4, s1) 692 693 // Wait for the new server to see itself added to the cluster. 694 retry.Run(t, func(r *retry.R) { 695 r.Check(wantRaft([]*Server{s1, s2, s3, s4})) 696 }) 697 698 // Make sure there's still a leader and that the term didn't change, 699 // so we know an election didn't occur. 700 testrpc.WaitForLeader(t, s1.RPC, "dc1") 701 termAfter := s1.raft.Stats()["last_log_term"] 702 if termAfter != termBefore { 703 t.Fatalf("looks like an election took place") 704 } 705 } 706 707 func TestServer_Expect_NonVoters(t *testing.T) { 708 t.Parallel() 709 dir1, s1 := testServerDCExpectNonVoter(t, "dc1", 3) 710 defer os.RemoveAll(dir1) 711 defer s1.Shutdown() 712 713 dir2, s2 := testServerDCExpectNonVoter(t, "dc1", 3) 714 defer os.RemoveAll(dir2) 715 defer s2.Shutdown() 716 717 dir3, s3 := testServerDCExpect(t, "dc1", 3) 718 defer os.RemoveAll(dir3) 719 defer s3.Shutdown() 720 721 dir4, s4 := testServerDCExpect(t, "dc1", 3) 722 defer os.RemoveAll(dir4) 723 defer s4.Shutdown() 724 725 dir5, s5 := testServerDCExpect(t, "dc1", 3) 726 defer os.RemoveAll(dir5) 727 defer s5.Shutdown() 728 729 // Join the first three servers. 730 joinLAN(t, s2, s1) 731 joinLAN(t, s3, s1) 732 733 // Should have no peers yet since the bootstrap didn't occur. 734 retry.Run(t, func(r *retry.R) { 735 r.Check(wantPeers(s1, 0)) 736 r.Check(wantPeers(s2, 0)) 737 r.Check(wantPeers(s3, 0)) 738 }) 739 740 // Join the fourth node. 741 joinLAN(t, s4, s1) 742 joinLAN(t, s5, s1) 743 744 // Now we have three servers so we should bootstrap. 745 retry.Run(t, func(r *retry.R) { 746 r.Check(wantPeers(s1, 3)) 747 r.Check(wantPeers(s2, 3)) 748 r.Check(wantPeers(s3, 3)) 749 r.Check(wantPeers(s4, 3)) 750 }) 751 752 // Make sure a leader is elected 753 testrpc.WaitForLeader(t, s1.RPC, "dc1") 754 retry.Run(t, func(r *retry.R) { 755 r.Check(wantRaft([]*Server{s1, s2, s3, s4, s5})) 756 }) 757 } 758 759 func TestServer_BadExpect(t *testing.T) { 760 t.Parallel() 761 // this one is in expect=3 mode 762 dir1, s1 := testServerDCExpect(t, "dc1", 3) 763 defer os.RemoveAll(dir1) 764 defer s1.Shutdown() 765 766 // this one is in expect=2 mode 767 dir2, s2 := testServerDCExpect(t, "dc1", 2) 768 defer os.RemoveAll(dir2) 769 defer s2.Shutdown() 770 771 // and this one is in expect=3 mode 772 dir3, s3 := testServerDCExpect(t, "dc1", 3) 773 defer os.RemoveAll(dir3) 774 defer s3.Shutdown() 775 776 // Try to join 777 joinLAN(t, s2, s1) 778 779 // should have no peers yet 780 retry.Run(t, func(r *retry.R) { 781 r.Check(wantPeers(s1, 0)) 782 r.Check(wantPeers(s2, 0)) 783 }) 784 785 // join the third node 786 joinLAN(t, s3, s1) 787 788 // should still have no peers (because s2 is in expect=2 mode) 789 retry.Run(t, func(r *retry.R) { 790 r.Check(wantPeers(s1, 0)) 791 r.Check(wantPeers(s2, 0)) 792 r.Check(wantPeers(s3, 0)) 793 }) 794 } 795 796 type fakeGlobalResp struct{} 797 798 func (r *fakeGlobalResp) Add(interface{}) { 799 return 800 } 801 802 func (r *fakeGlobalResp) New() interface{} { 803 return struct{}{} 804 } 805 806 func TestServer_globalRPCErrors(t *testing.T) { 807 t.Parallel() 808 dir1, s1 := testServerDC(t, "dc1") 809 defer os.RemoveAll(dir1) 810 defer s1.Shutdown() 811 retry.Run(t, func(r *retry.R) { 812 if len(s1.router.GetDatacenters()) != 1 { 813 r.Fatal(nil) 814 } 815 }) 816 817 // Check that an error from a remote DC is returned 818 err := s1.globalRPC("Bad.Method", nil, &fakeGlobalResp{}) 819 if err == nil { 820 t.Fatalf("should have errored") 821 } 822 if !strings.Contains(err.Error(), "Bad.Method") { 823 t.Fatalf("unexpected error: %s", err) 824 } 825 } 826 827 func TestServer_Encrypted(t *testing.T) { 828 t.Parallel() 829 dir1, s1 := testServer(t) 830 defer os.RemoveAll(dir1) 831 defer s1.Shutdown() 832 833 key := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15} 834 dir2, s2 := testServerWithConfig(t, func(c *Config) { 835 c.SerfLANConfig.MemberlistConfig.SecretKey = key 836 c.SerfWANConfig.MemberlistConfig.SecretKey = key 837 }) 838 defer os.RemoveAll(dir2) 839 defer s2.Shutdown() 840 841 if s1.Encrypted() { 842 t.Fatalf("should not be encrypted") 843 } 844 if !s2.Encrypted() { 845 t.Fatalf("should be encrypted") 846 } 847 } 848 849 func testVerifyRPC(s1, s2 *Server, t *testing.T) (bool, error) { 850 joinLAN(t, s1, s2) 851 retry.Run(t, func(r *retry.R) { 852 r.Check(wantRaft([]*Server{s1, s2})) 853 }) 854 855 // Have s2 make an RPC call to s1 856 var leader *metadata.Server 857 for _, server := range s2.serverLookup.Servers() { 858 if server.Name == s1.config.NodeName { 859 leader = server 860 } 861 } 862 if leader == nil { 863 t.Fatal("no leader") 864 } 865 return s2.connPool.Ping(leader.Datacenter, leader.Addr, leader.Version, leader.UseTLS) 866 } 867 868 func TestServer_TLSToNoTLS(t *testing.T) { 869 t.Parallel() 870 // Set up a server with no TLS configured 871 dir1, s1 := testServer(t) 872 defer os.RemoveAll(dir1) 873 defer s1.Shutdown() 874 875 testrpc.WaitForLeader(t, s1.RPC, "dc1") 876 877 // Add a second server with TLS configured 878 dir2, s2 := testServerWithConfig(t, func(c *Config) { 879 c.Bootstrap = false 880 c.CAFile = "../../test/client_certs/rootca.crt" 881 c.CertFile = "../../test/client_certs/server.crt" 882 c.KeyFile = "../../test/client_certs/server.key" 883 }) 884 defer os.RemoveAll(dir2) 885 defer s2.Shutdown() 886 887 success, err := testVerifyRPC(s1, s2, t) 888 if err != nil { 889 t.Fatal(err) 890 } 891 if !success { 892 t.Fatalf("bad: %v", success) 893 } 894 } 895 896 func TestServer_TLSForceOutgoingToNoTLS(t *testing.T) { 897 t.Parallel() 898 // Set up a server with no TLS configured 899 dir1, s1 := testServer(t) 900 defer os.RemoveAll(dir1) 901 defer s1.Shutdown() 902 903 testrpc.WaitForLeader(t, s1.RPC, "dc1") 904 905 // Add a second server with TLS and VerifyOutgoing set 906 dir2, s2 := testServerWithConfig(t, func(c *Config) { 907 c.Bootstrap = false 908 c.CAFile = "../../test/client_certs/rootca.crt" 909 c.CertFile = "../../test/client_certs/server.crt" 910 c.KeyFile = "../../test/client_certs/server.key" 911 c.VerifyOutgoing = true 912 }) 913 defer os.RemoveAll(dir2) 914 defer s2.Shutdown() 915 916 _, err := testVerifyRPC(s1, s2, t) 917 if err == nil || !strings.Contains(err.Error(), "remote error: tls") { 918 t.Fatalf("should fail") 919 } 920 } 921 922 func TestServer_TLSToFullVerify(t *testing.T) { 923 t.Parallel() 924 // Set up a server with TLS and VerifyIncoming set 925 dir1, s1 := testServerWithConfig(t, func(c *Config) { 926 c.CAFile = "../../test/client_certs/rootca.crt" 927 c.CertFile = "../../test/client_certs/server.crt" 928 c.KeyFile = "../../test/client_certs/server.key" 929 c.VerifyIncoming = true 930 c.VerifyOutgoing = true 931 }) 932 defer os.RemoveAll(dir1) 933 defer s1.Shutdown() 934 935 testrpc.WaitForLeader(t, s1.RPC, "dc1") 936 937 // Add a second server with TLS configured 938 dir2, s2 := testServerWithConfig(t, func(c *Config) { 939 c.Bootstrap = false 940 c.CAFile = "../../test/client_certs/rootca.crt" 941 c.CertFile = "../../test/client_certs/server.crt" 942 c.KeyFile = "../../test/client_certs/server.key" 943 }) 944 defer os.RemoveAll(dir2) 945 defer s2.Shutdown() 946 947 success, err := testVerifyRPC(s1, s2, t) 948 if err != nil { 949 t.Fatal(err) 950 } 951 if !success { 952 t.Fatalf("bad: %v", success) 953 } 954 } 955 956 func TestServer_RevokeLeadershipIdempotent(t *testing.T) { 957 t.Parallel() 958 dir1, s1 := testServer(t) 959 defer os.RemoveAll(dir1) 960 defer s1.Shutdown() 961 962 testrpc.WaitForLeader(t, s1.RPC, "dc1") 963 964 err := s1.revokeLeadership() 965 if err != nil { 966 t.Fatal(err) 967 } 968 err = s1.revokeLeadership() 969 if err != nil { 970 t.Fatal(err) 971 } 972 }