internal_version_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package test
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/http/httptest"
  6. "net/url"
  7. "os"
  8. "sync"
  9. "testing"
  10. "time"
  11. )
  12. // Ensure that etcd does not come up if the internal raft versions do not match.
  13. func TestInternalVersion(t *testing.T) {
  14. var mu sync.Mutex
  15. checkedVersion := false
  16. testMux := http.NewServeMux()
  17. testMux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
  18. fmt.Fprintln(w, "This is not a version number")
  19. mu.Lock()
  20. defer mu.Unlock()
  21. checkedVersion = true
  22. })
  23. testMux.HandleFunc("/join", func(w http.ResponseWriter, r *http.Request) {
  24. t.Fatal("should not attempt to join!")
  25. })
  26. ts := httptest.NewServer(testMux)
  27. defer ts.Close()
  28. fakeURL, _ := url.Parse(ts.URL)
  29. procAttr := new(os.ProcAttr)
  30. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  31. args := []string{"etcd", "-name=node1", "-f", "-data-dir=/tmp/node1", "-peers=" + fakeURL.Host}
  32. process, err := os.StartProcess(EtcdBinPath, args, procAttr)
  33. if err != nil {
  34. t.Fatal("start process failed:" + err.Error())
  35. return
  36. }
  37. time.Sleep(time.Second)
  38. process.Kill()
  39. _, err = http.Get("http://127.0.0.1:4001")
  40. if err == nil {
  41. t.Fatal("etcd node should not be up")
  42. return
  43. }
  44. mu.Lock()
  45. defer mu.Unlock()
  46. if checkedVersion == false {
  47. t.Fatal("etcd did not check the version")
  48. return
  49. }
  50. }