remove_node_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package test
  2. import (
  3. "bytes"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "testing"
  8. "time"
  9. "github.com/coreos/etcd/third_party/github.com/coreos/go-etcd/etcd"
  10. "github.com/coreos/etcd/tests"
  11. "github.com/coreos/etcd/third_party/github.com/stretchr/testify/assert"
  12. )
  13. // remove the node and node rejoin with previous log
  14. func TestRemoveNode(t *testing.T) {
  15. procAttr := new(os.ProcAttr)
  16. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  17. clusterSize := 4
  18. argGroup, etcds, _ := CreateCluster(clusterSize, procAttr, false)
  19. defer DestroyCluster(etcds)
  20. time.Sleep(time.Second)
  21. c := etcd.NewClient(nil)
  22. c.SyncCluster()
  23. resp, _ := tests.Put("http://localhost:7001/v2/admin/config", "application/json", bytes.NewBufferString(`{"activeSize":4, "syncInterval":1}`))
  24. if !assert.Equal(t, resp.StatusCode, 200) {
  25. t.FailNow()
  26. }
  27. rmReq, _ := http.NewRequest("DELETE", "http://127.0.0.1:7001/remove/node3", nil)
  28. client := &http.Client{}
  29. for i := 0; i < 2; i++ {
  30. for i := 0; i < 2; i++ {
  31. r, _ := tests.Put("http://localhost:7001/v2/admin/config", "application/json", bytes.NewBufferString(`{"activeSize":3}`))
  32. if !assert.Equal(t, r.StatusCode, 200) {
  33. t.FailNow()
  34. }
  35. client.Do(rmReq)
  36. fmt.Println("send remove to node3 and wait for its exiting")
  37. time.Sleep(100 * time.Millisecond)
  38. resp, err := c.Get("_etcd/machines", false, false)
  39. if err != nil {
  40. panic(err)
  41. }
  42. if len(resp.Node.Nodes) != 3 {
  43. t.Fatal("cannot remove peer")
  44. }
  45. etcds[2].Kill()
  46. etcds[2].Wait()
  47. if i == 1 {
  48. // rejoin with log
  49. etcds[2], err = os.StartProcess(EtcdBinPath, argGroup[2], procAttr)
  50. } else {
  51. // rejoin without log
  52. etcds[2], err = os.StartProcess(EtcdBinPath, append(argGroup[2], "-f"), procAttr)
  53. }
  54. if err != nil {
  55. panic(err)
  56. }
  57. r, _ = tests.Put("http://localhost:7001/v2/admin/config", "application/json", bytes.NewBufferString(`{"activeSize":4}`))
  58. if !assert.Equal(t, r.StatusCode, 200) {
  59. t.FailNow()
  60. }
  61. time.Sleep(time.Second + time.Second)
  62. resp, err = c.Get("_etcd/machines", false, false)
  63. if err != nil {
  64. panic(err)
  65. }
  66. if len(resp.Node.Nodes) != 4 {
  67. t.Fatalf("add peer fails #1 (%d != 4)", len(resp.Node.Nodes))
  68. }
  69. }
  70. // first kill the node, then remove it, then add it back
  71. for i := 0; i < 2; i++ {
  72. r, _ := tests.Put("http://localhost:7001/v2/admin/config", "application/json", bytes.NewBufferString(`{"activeSize":3}`))
  73. if !assert.Equal(t, r.StatusCode, 200) {
  74. t.FailNow()
  75. }
  76. etcds[2].Kill()
  77. fmt.Println("kill node3 and wait for its exiting")
  78. etcds[2].Wait()
  79. client.Do(rmReq)
  80. time.Sleep(100 * time.Millisecond)
  81. resp, err := c.Get("_etcd/machines", false, false)
  82. if err != nil {
  83. panic(err)
  84. }
  85. if len(resp.Node.Nodes) != 3 {
  86. t.Fatal("cannot remove peer")
  87. }
  88. if i == 1 {
  89. // rejoin with log
  90. etcds[2], err = os.StartProcess(EtcdBinPath, append(argGroup[2]), procAttr)
  91. } else {
  92. // rejoin without log
  93. etcds[2], err = os.StartProcess(EtcdBinPath, append(argGroup[2], "-f"), procAttr)
  94. }
  95. if err != nil {
  96. panic(err)
  97. }
  98. r, _ = tests.Put("http://localhost:7001/v2/admin/config", "application/json", bytes.NewBufferString(`{"activeSize":4}`))
  99. if !assert.Equal(t, r.StatusCode, 200) {
  100. t.FailNow()
  101. }
  102. time.Sleep(time.Second + time.Second)
  103. resp, err = c.Get("_etcd/machines", false, false)
  104. if err != nil {
  105. panic(err)
  106. }
  107. if len(resp.Node.Nodes) != 4 {
  108. t.Fatalf("add peer fails #2 (%d != 4)", len(resp.Node.Nodes))
  109. }
  110. }
  111. }
  112. }