package router import ( "net/http" "net/http/httptest" "path/filepath" "testing" ) func TestTheLeaderEndpointFollowsLeadership(t *testing.T) { rt := newTestRouter(t, baseConfig()) get := func() int { rec := httptest.NewRecorder() rt.Handler().ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/leader", nil)) return rec.Code } // No rival, nothing to follow: the one process leads. if c := get(); c != http.StatusOK { t.Errorf("with no leadership: %d", c) } leading := false rt.SetLeader(func() bool { return leading }) if c := get(); c != http.StatusServiceUnavailable { t.Errorf("a standby: %d", c) } leading = true if c := get(); c != http.StatusOK { t.Errorf("the leader: %d", c) } } func TestTheLeaderEndpointNeedsNoKey(t *testing.T) { // keepalived and load balancers ask it, and they have no key. cfg := baseConfig() cfg.Auth.Enabled = true cfg.Auth.Store = filepath.Join(t.TempDir(), "store.db") rt := newTestRouter(t, cfg) rec := httptest.NewRecorder() rt.Handler().ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/leader", nil)) if rec.Code != http.StatusOK { t.Errorf("/leader without a key: %d", rec.Code) } }