Merge pull request #3240 from Gouthamve/fix-admin-api

web/api: Fix typo which broke the 2.0 admin APIs.
This commit is contained in:
Fabian Reinartz 2017-10-05 14:33:12 +02:00 committed by GitHub
commit 7a3261aa99
2 changed files with 104 additions and 5 deletions

View File

@ -358,6 +358,11 @@ func (h *Handler) testReady(f http.HandlerFunc) http.HandlerFunc {
}
}
// Checks if server is ready, calls f if it is, returns 503 if it is not.
func (h *Handler) testReadyHandler(f http.Handler) http.HandlerFunc {
return h.testReady(f.ServeHTTP)
}
// Quit returns the receive-only quit channel.
func (h *Handler) Quit() <-chan struct{} {
return h.quitCh
@ -404,6 +409,8 @@ func (h *Handler) Run(ctx context.Context) error {
return err
}
hhFunc := h.testReadyHandler(hh)
operationName := nethttp.OperationNameFunc(func(r *http.Request) string {
return fmt.Sprintf("%s %s", r.Method, r.URL.Path)
})
@ -420,10 +427,10 @@ func (h *Handler) Run(ctx context.Context) error {
mux.Handle(apiPath+"/v1/", http.StripPrefix(apiPath+"/v1", av1))
mux.Handle(apiPath, http.StripPrefix(apiPath,
mux.Handle(apiPath+"/", http.StripPrefix(apiPath,
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
setCORS(w)
hh.ServeHTTP(w, r)
hhFunc(w, r)
}),
))

View File

@ -15,13 +15,17 @@ package web
import (
"context"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strings"
"testing"
"time"
"github.com/prometheus/prometheus/storage/tsdb"
libtsdb "github.com/prometheus/tsdb"
)
func TestGlobalURL(t *testing.T) {
@ -76,6 +80,16 @@ func TestGlobalURL(t *testing.T) {
func TestReadyAndHealthy(t *testing.T) {
t.Parallel()
dbDir, err := ioutil.TempDir("", "tsdb-ready")
if err != nil {
t.Fatalf("Unexpected error creating a tmpDir: %s", err)
}
defer os.RemoveAll(dbDir)
db, err := libtsdb.Open(dbDir, nil, nil, nil)
if err != nil {
t.Fatalf("Unexpected error opening empty dir: %s", err)
}
opts := &Options{
ListenAddress: ":9090",
ReadTimeout: 30 * time.Second,
@ -88,6 +102,8 @@ func TestReadyAndHealthy(t *testing.T) {
Notifier: nil,
RoutePrefix: "/",
MetricsPath: "/metrics/",
EnableAdminAPI: true,
TSDB: func() *libtsdb.DB { return db },
}
opts.Flags = map[string]string{}
@ -123,6 +139,22 @@ func TestReadyAndHealthy(t *testing.T) {
t.Fatalf("Path /version with server unready test, Expected status 503 got: %s", resp.Status)
}
resp, err = http.Post("http://localhost:9090/api/v2/admin/tsdb/snapshot", "", strings.NewReader(""))
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err)
}
if resp.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("Path /api/v2/admin/tsdb/snapshot with server unready test, Expected status 503 got: %s", resp.Status)
}
resp, err = http.Post("http://localhost:9090/api/v2/admin/tsdb/delete_series", "", strings.NewReader("{}"))
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err)
}
if resp.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("Path /api/v2/admin/tsdb/delete_series with server unready test, Expected status 503 got: %s", resp.Status)
}
// Set to ready.
webHandler.Ready()
@ -149,10 +181,36 @@ func TestReadyAndHealthy(t *testing.T) {
if resp.StatusCode != http.StatusOK {
t.Fatalf("Path /version with server ready test, Expected status 200 got: %s", resp.Status)
}
resp, err = http.Post("http://localhost:9090/api/v2/admin/tsdb/snapshot", "", strings.NewReader(""))
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("Path /api/v2/admin/tsdb/snapshot with server unready test, Expected status 503 got: %s", resp.Status)
}
resp, err = http.Post("http://localhost:9090/api/v2/admin/tsdb/delete_series", "", strings.NewReader("{}"))
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("Path /api/v2/admin/tsdb/delete_series with server unready test, Expected status 503 got: %s", resp.Status)
}
}
func TestRoutePrefix(t *testing.T) {
t.Parallel()
dbDir, err := ioutil.TempDir("", "tsdb-ready")
if err != nil {
t.Fatalf("Unexpected error creating a tmpDir: %s", err)
}
defer os.RemoveAll(dbDir)
db, err := libtsdb.Open(dbDir, nil, nil, nil)
if err != nil {
t.Fatalf("Unexpected error opening empty dir: %s", err)
}
opts := &Options{
ListenAddress: ":9091",
ReadTimeout: 30 * time.Second,
@ -165,6 +223,8 @@ func TestRoutePrefix(t *testing.T) {
Notifier: nil,
RoutePrefix: "/prometheus",
MetricsPath: "/prometheus/metrics",
EnableAdminAPI: true,
TSDB: func() *libtsdb.DB { return db },
}
opts.Flags = map[string]string{}
@ -186,7 +246,7 @@ func TestRoutePrefix(t *testing.T) {
t.Fatalf("Unexpected HTTP error %s", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("Path "+opts.RoutePrefix+"/-/healthy with server unready test, Expected status 200 got: %s", resp.Status)
t.Fatalf("Path %s/-/healthy with server unready test, Expected status 200 got: %s", opts.RoutePrefix, resp.Status)
}
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/ready")
@ -194,7 +254,7 @@ func TestRoutePrefix(t *testing.T) {
t.Fatalf("Unexpected HTTP error %s", err)
}
if resp.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("Path "+opts.RoutePrefix+"/-/ready with server unready test, Expected status 503 got: %s", resp.Status)
t.Fatalf("Path %s/-/ready with server unready test, Expected status 503 got: %s", opts.RoutePrefix, resp.Status)
}
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/version")
@ -202,7 +262,23 @@ func TestRoutePrefix(t *testing.T) {
t.Fatalf("Unexpected HTTP error %s", err)
}
if resp.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("Path "+opts.RoutePrefix+"/version with server unready test, Expected status 503 got: %s", resp.Status)
t.Fatalf("Path %s/version with server unready test, Expected status 503 got: %s", opts.RoutePrefix, resp.Status)
}
resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v2/admin/tsdb/snapshot", "", strings.NewReader(""))
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err)
}
if resp.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("Path %s/api/v2/admin/tsdb/snapshot with server unready test, Expected status 503 got: %s", opts.RoutePrefix, resp.Status)
}
resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v2/admin/tsdb/delete_series", "", strings.NewReader("{}"))
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err)
}
if resp.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("Path %s/api/v2/admin/tsdb/delete_series with server unready test, Expected status 503 got: %s", opts.RoutePrefix, resp.Status)
}
// Set to ready.
@ -231,6 +307,22 @@ func TestRoutePrefix(t *testing.T) {
if resp.StatusCode != http.StatusOK {
t.Fatalf("Path "+opts.RoutePrefix+"/version with server ready test, Expected status 200 got: %s", resp.Status)
}
resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v2/admin/tsdb/snapshot", "", strings.NewReader(""))
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("Path %s/api/v2/admin/tsdb/snapshot with server unready test, Expected status 503 got: %s", opts.RoutePrefix, resp.Status)
}
resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v2/admin/tsdb/delete_series", "", strings.NewReader("{}"))
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("Path %s/api/v2/admin/tsdb/delete_series with server unready test, Expected status 503 got: %s", opts.RoutePrefix, resp.Status)
}
}
func TestDebugHandler(t *testing.T) {