remove_node_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package test
  2. import (
  3. "net/http"
  4. "os"
  5. "testing"
  6. "time"
  7. "github.com/coreos/go-etcd/etcd"
  8. )
  9. // remove the node and node rejoin with previous log
  10. func TestRemoveNode(t *testing.T) {
  11. procAttr := new(os.ProcAttr)
  12. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  13. clusterSize := 3
  14. argGroup, etcds, _ := CreateCluster(clusterSize, procAttr, false)
  15. defer DestroyCluster(etcds)
  16. time.Sleep(time.Second)
  17. c := etcd.NewClient(nil)
  18. c.SyncCluster()
  19. rmReq, _ := http.NewRequest("DELETE", "http://127.0.0.1:7001/remove/node3", nil)
  20. client := &http.Client{}
  21. for i := 0; i < 2; i++ {
  22. for i := 0; i < 2; i++ {
  23. client.Do(rmReq)
  24. etcds[2].Wait()
  25. resp, err := c.Get("_etcd/machines")
  26. if err != nil {
  27. panic(err)
  28. }
  29. if len(resp) != 2 {
  30. t.Fatal("cannot remove machine")
  31. }
  32. if i == 1 {
  33. // rejoin with log
  34. etcds[2], err = os.StartProcess(EtcdBinPath, argGroup[2], procAttr)
  35. } else {
  36. // rejoin without log
  37. etcds[2], err = os.StartProcess(EtcdBinPath, append(argGroup[2], "-f"), procAttr)
  38. }
  39. if err != nil {
  40. panic(err)
  41. }
  42. time.Sleep(time.Second)
  43. resp, err = c.Get("_etcd/machines")
  44. if err != nil {
  45. panic(err)
  46. }
  47. if len(resp) != 3 {
  48. t.Fatalf("add machine fails #1 (%d != 3)", len(resp))
  49. }
  50. }
  51. // first kill the node, then remove it, then add it back
  52. for i := 0; i < 2; i++ {
  53. etcds[2].Kill()
  54. etcds[2].Wait()
  55. client.Do(rmReq)
  56. resp, err := c.Get("_etcd/machines")
  57. if err != nil {
  58. panic(err)
  59. }
  60. if len(resp) != 2 {
  61. t.Fatal("cannot remove machine")
  62. }
  63. if i == 1 {
  64. // rejoin with log
  65. etcds[2], err = os.StartProcess(EtcdBinPath, append(argGroup[2]), procAttr)
  66. } else {
  67. // rejoin without log
  68. etcds[2], err = os.StartProcess(EtcdBinPath, append(argGroup[2], "-f"), procAttr)
  69. }
  70. if err != nil {
  71. panic(err)
  72. }
  73. time.Sleep(time.Second)
  74. resp, err = c.Get("_etcd/machines")
  75. if err != nil {
  76. panic(err)
  77. }
  78. if len(resp) != 3 {
  79. t.Fatalf("add machine fails #2 (%d != 3)", len(resp))
  80. }
  81. }
  82. }
  83. }