cluster_config_test.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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, body["activeSize"], 3)
  23. assert.Equal(t, body["removeDelay"], 60)
  24. }
  25. // Ensure that the cluster configuration can be reloaded.
  26. func TestClusterConfigReload(t *testing.T) {
  27. procAttr := &os.ProcAttr{Files: []*os.File{nil, os.Stdout, os.Stderr}}
  28. argGroup, etcds, err := CreateCluster(3, procAttr, false)
  29. assert.NoError(t, err)
  30. defer DestroyCluster(etcds)
  31. resp, _ := tests.Put("http://localhost:7001/v2/admin/config", "application/json", bytes.NewBufferString(`{"activeSize":3, "removeDelay":60}`))
  32. assert.Equal(t, resp.StatusCode, 200)
  33. time.Sleep(1 * time.Second)
  34. resp, _ = tests.Get("http://localhost:7002/v2/admin/config")
  35. body := tests.ReadBodyJSON(resp)
  36. assert.Equal(t, resp.StatusCode, 200)
  37. assert.Equal(t, body["activeSize"], 3)
  38. assert.Equal(t, body["removeDelay"], 60)
  39. // kill all
  40. DestroyCluster(etcds)
  41. for i := 0; i < 3; i++ {
  42. etcds[i], err = os.StartProcess(EtcdBinPath, argGroup[i], procAttr)
  43. }
  44. time.Sleep(1 * time.Second)
  45. resp, _ = tests.Get("http://localhost:7002/v2/admin/config")
  46. body = tests.ReadBodyJSON(resp)
  47. assert.Equal(t, resp.StatusCode, 200)
  48. assert.Equal(t, body["activeSize"], 3)
  49. assert.Equal(t, body["removeDelay"], 60)
  50. }
  51. // TestGetMachines tests '/v2/admin/machines' sends back messages of all machines.
  52. func TestGetMachines(t *testing.T) {
  53. _, etcds, err := CreateCluster(3, &os.ProcAttr{Files: []*os.File{nil, os.Stdout, os.Stderr}}, false)
  54. assert.NoError(t, err)
  55. defer DestroyCluster(etcds)
  56. time.Sleep(1 * time.Second)
  57. resp, err := tests.Get("http://localhost:7001/v2/admin/machines")
  58. if !assert.Equal(t, err, nil) {
  59. t.FailNow()
  60. }
  61. assert.Equal(t, resp.StatusCode, 200)
  62. machines := make([]map[string]interface{}, 0)
  63. b := tests.ReadBody(resp)
  64. json.Unmarshal(b, &machines)
  65. assert.Equal(t, len(machines), 3)
  66. if machines[0]["state"] != "leader" && machines[1]["state"] != "leader" && machines[2]["state"] != "leader" {
  67. t.Errorf("no leader in the cluster")
  68. }
  69. }