cluster_config_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 TestClusterConfig(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, "promoteDelay":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["promoteDelay"], 60)
  24. }
  25. // TestGetMachines tests '/v2/admin/machines' sends back messages of all machines.
  26. func TestGetMachines(t *testing.T) {
  27. _, etcds, err := CreateCluster(3, &os.ProcAttr{Files: []*os.File{nil, os.Stdout, os.Stderr}}, false)
  28. assert.NoError(t, err)
  29. defer DestroyCluster(etcds)
  30. time.Sleep(1 * time.Second)
  31. resp, err := tests.Get("http://localhost:7001/v2/admin/machines")
  32. if !assert.Equal(t, err, nil) {
  33. t.FailNow()
  34. }
  35. assert.Equal(t, resp.StatusCode, 200)
  36. machines := make([]map[string]interface{}, 0)
  37. b := tests.ReadBody(resp)
  38. json.Unmarshal(b, &machines)
  39. assert.Equal(t, len(machines), 3)
  40. if machines[0]["state"] != "leader" && machines[1]["state"] != "leader" && machines[2]["state"] != "leader" {
  41. t.Errorf("no leader in the cluster")
  42. }
  43. }