cluster_config_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package test
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "os"
  6. "testing"
  7. "time"
  8. "github.com/coreos/etcd/tests"
  9. "github.com/coreos/etcd/third_party/github.com/stretchr/testify/assert"
  10. )
  11. // Ensure that the cluster configuration can be updated.
  12. func TestClusterConfigSet(t *testing.T) {
  13. _, etcds, err := CreateCluster(3, &os.ProcAttr{Files: []*os.File{nil, os.Stdout, os.Stderr}}, false)
  14. assert.NoError(t, err)
  15. defer DestroyCluster(etcds)
  16. resp, _ := tests.Put("http://localhost:7001/v2/admin/config", "application/json", bytes.NewBufferString(`{"activeSize":3, "removeDelay":60}`))
  17. assert.Equal(t, resp.StatusCode, 200)
  18. time.Sleep(1 * time.Second)
  19. resp, _ = tests.Get("http://localhost:7002/v2/admin/config")
  20. body := tests.ReadBodyJSON(resp)
  21. assert.Equal(t, resp.StatusCode, 200)
  22. assert.Equal(t, resp.Header.Get("Content-Type"), "application/json")
  23. assert.Equal(t, body["activeSize"], 3)
  24. assert.Equal(t, body["removeDelay"], 60)
  25. }
  26. // Ensure that the cluster configuration can be reloaded.
  27. func TestClusterConfigReload(t *testing.T) {
  28. procAttr := &os.ProcAttr{Files: []*os.File{nil, os.Stdout, os.Stderr}}
  29. argGroup, etcds, err := CreateCluster(3, procAttr, false)
  30. assert.NoError(t, err)
  31. defer DestroyCluster(etcds)
  32. resp, _ := tests.Put("http://localhost:7001/v2/admin/config", "application/json", bytes.NewBufferString(`{"activeSize":3, "removeDelay":60}`))
  33. assert.Equal(t, resp.StatusCode, 200)
  34. time.Sleep(1 * time.Second)
  35. resp, _ = tests.Get("http://localhost:7002/v2/admin/config")
  36. body := tests.ReadBodyJSON(resp)
  37. assert.Equal(t, resp.StatusCode, 200)
  38. assert.Equal(t, resp.Header.Get("Content-Type"), "application/json")
  39. assert.Equal(t, body["activeSize"], 3)
  40. assert.Equal(t, body["removeDelay"], 60)
  41. // kill all
  42. DestroyCluster(etcds)
  43. for i := 0; i < 3; i++ {
  44. etcds[i], err = os.StartProcess(EtcdBinPath, argGroup[i], procAttr)
  45. }
  46. time.Sleep(1 * time.Second)
  47. resp, _ = tests.Get("http://localhost:7002/v2/admin/config")
  48. body = tests.ReadBodyJSON(resp)
  49. assert.Equal(t, resp.StatusCode, 200)
  50. assert.Equal(t, resp.Header.Get("Content-Type"), "application/json")
  51. assert.Equal(t, body["activeSize"], 3)
  52. assert.Equal(t, body["removeDelay"], 60)
  53. }
  54. // TestGetMachines tests '/v2/admin/machines' sends back messages of all machines.
  55. func TestGetMachines(t *testing.T) {
  56. _, etcds, err := CreateCluster(3, &os.ProcAttr{Files: []*os.File{nil, os.Stdout, os.Stderr}}, false)
  57. assert.NoError(t, err)
  58. defer DestroyCluster(etcds)
  59. time.Sleep(1 * time.Second)
  60. resp, err := tests.Get("http://localhost:7001/v2/admin/machines")
  61. if !assert.Equal(t, err, nil) {
  62. t.FailNow()
  63. }
  64. assert.Equal(t, resp.StatusCode, 200)
  65. assert.Equal(t, resp.Header.Get("Content-Type"), "application/json")
  66. machines := make([]map[string]interface{}, 0)
  67. b := tests.ReadBody(resp)
  68. json.Unmarshal(b, &machines)
  69. assert.Equal(t, len(machines), 3)
  70. if machines[0]["state"] != "leader" && machines[1]["state"] != "leader" && machines[2]["state"] != "leader" {
  71. t.Errorf("no leader in the cluster")
  72. }
  73. }