2016-04-13 14:08:22 +00:00
|
|
|
// Copyright 2016 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-07-03 12:47:52 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2017-08-10 14:28:49 +00:00
|
|
|
"context"
|
2018-01-13 10:37:38 +00:00
|
|
|
"fmt"
|
2017-10-05 05:38:07 +00:00
|
|
|
"io/ioutil"
|
2017-07-26 00:47:45 +00:00
|
|
|
"net/http"
|
2017-09-01 11:12:51 +00:00
|
|
|
"net/http/httptest"
|
2015-07-03 12:47:52 +00:00
|
|
|
"net/url"
|
2017-10-05 05:38:07 +00:00
|
|
|
"os"
|
2019-06-19 15:56:04 +00:00
|
|
|
"strconv"
|
2017-10-05 05:38:07 +00:00
|
|
|
"strings"
|
2015-07-03 12:47:52 +00:00
|
|
|
"testing"
|
2017-07-26 00:47:45 +00:00
|
|
|
"time"
|
2017-09-18 10:32:17 +00:00
|
|
|
|
2019-06-24 13:48:15 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2019-06-19 15:56:04 +00:00
|
|
|
prom_testutil "github.com/prometheus/client_golang/prometheus/testutil"
|
|
|
|
|
2019-02-08 10:17:47 +00:00
|
|
|
"github.com/prometheus/prometheus/config"
|
|
|
|
"github.com/prometheus/prometheus/notifier"
|
|
|
|
"github.com/prometheus/prometheus/rules"
|
|
|
|
"github.com/prometheus/prometheus/scrape"
|
2017-09-18 10:32:17 +00:00
|
|
|
"github.com/prometheus/prometheus/storage/tsdb"
|
2019-08-13 08:34:14 +00:00
|
|
|
libtsdb "github.com/prometheus/prometheus/tsdb"
|
2017-10-05 20:40:10 +00:00
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
2015-07-03 12:47:52 +00:00
|
|
|
)
|
|
|
|
|
2017-12-20 18:21:10 +00:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
// On linux with a global proxy the tests will fail as the go client(http,grpc) tries to connect through the proxy.
|
|
|
|
os.Setenv("no_proxy", "localhost,127.0.0.1,0.0.0.0,:")
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
2015-07-03 12:47:52 +00:00
|
|
|
func TestGlobalURL(t *testing.T) {
|
|
|
|
opts := &Options{
|
|
|
|
ListenAddress: ":9090",
|
|
|
|
ExternalURL: &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "externalhost:80",
|
|
|
|
Path: "/path/prefix",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
inURL string
|
|
|
|
outURL string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// Nothing should change if the input URL is not on localhost, even if the port is our listening port.
|
|
|
|
inURL: "http://somehost:9090/metrics",
|
|
|
|
outURL: "http://somehost:9090/metrics",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Port and host should change if target is on localhost and port is our listening port.
|
|
|
|
inURL: "http://localhost:9090/metrics",
|
|
|
|
outURL: "https://externalhost:80/metrics",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Only the host should change if the port is not our listening port, but the host is localhost.
|
|
|
|
inURL: "http://localhost:8000/metrics",
|
|
|
|
outURL: "http://externalhost:8000/metrics",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Alternative localhost representations should also work.
|
|
|
|
inURL: "http://127.0.0.1:9090/metrics",
|
|
|
|
outURL: "https://externalhost:80/metrics",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-10-05 20:40:10 +00:00
|
|
|
for _, test := range tests {
|
2015-07-03 12:47:52 +00:00
|
|
|
inURL, err := url.Parse(test.inURL)
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
2015-07-03 12:47:52 +00:00
|
|
|
globalURL := tmplFuncs("", opts)["globalURL"].(func(u *url.URL) *url.URL)
|
|
|
|
outURL := globalURL(inURL)
|
|
|
|
|
2017-10-05 20:40:10 +00:00
|
|
|
testutil.Equals(t, test.outURL, outURL.String())
|
2015-07-03 12:47:52 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-26 00:47:45 +00:00
|
|
|
|
|
|
|
func TestReadyAndHealthy(t *testing.T) {
|
2017-09-06 12:41:07 +00:00
|
|
|
t.Parallel()
|
2017-10-05 05:38:07 +00:00
|
|
|
dbDir, err := ioutil.TempDir("", "tsdb-ready")
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
2017-10-05 05:38:07 +00:00
|
|
|
defer os.RemoveAll(dbDir)
|
|
|
|
db, err := libtsdb.Open(dbDir, nil, nil, nil)
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
2017-10-05 05:38:07 +00:00
|
|
|
|
2017-07-26 00:47:45 +00:00
|
|
|
opts := &Options{
|
|
|
|
ListenAddress: ":9090",
|
|
|
|
ReadTimeout: 30 * time.Second,
|
|
|
|
MaxConnections: 512,
|
|
|
|
Context: nil,
|
2017-09-18 10:32:17 +00:00
|
|
|
Storage: &tsdb.ReadyStorage{},
|
2017-07-26 00:47:45 +00:00
|
|
|
QueryEngine: nil,
|
2019-02-08 10:17:47 +00:00
|
|
|
ScrapeManager: &scrape.Manager{},
|
|
|
|
RuleManager: &rules.Manager{},
|
2017-07-26 00:47:45 +00:00
|
|
|
Notifier: nil,
|
|
|
|
RoutePrefix: "/",
|
2017-10-05 05:38:07 +00:00
|
|
|
EnableAdminAPI: true,
|
|
|
|
TSDB: func() *libtsdb.DB { return db },
|
2018-09-19 07:20:53 +00:00
|
|
|
ExternalURL: &url.URL{
|
|
|
|
Scheme: "http",
|
|
|
|
Host: "localhost:9090",
|
|
|
|
Path: "/",
|
|
|
|
},
|
2019-06-24 13:48:15 +00:00
|
|
|
Version: &PrometheusVersion{},
|
|
|
|
Gatherer: prometheus.DefaultGatherer,
|
2017-07-26 00:47:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
opts.Flags = map[string]string{}
|
|
|
|
|
2017-08-11 18:45:52 +00:00
|
|
|
webHandler := New(nil, opts)
|
2019-02-08 10:17:47 +00:00
|
|
|
|
|
|
|
webHandler.config = &config.Config{}
|
|
|
|
webHandler.notifier = ¬ifier.Manager{}
|
|
|
|
|
2017-12-14 13:45:36 +00:00
|
|
|
go func() {
|
|
|
|
err := webHandler.Run(context.Background())
|
|
|
|
if err != nil {
|
2018-01-13 10:37:38 +00:00
|
|
|
panic(fmt.Sprintf("Can't start web handler:%s", err))
|
2017-12-14 13:45:36 +00:00
|
|
|
}
|
|
|
|
}()
|
2017-07-26 00:47:45 +00:00
|
|
|
|
|
|
|
// Give some time for the web goroutine to run since we need the server
|
|
|
|
// to be up before starting tests.
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
|
|
|
resp, err := http.Get("http://localhost:9090/-/healthy")
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
2017-07-26 00:47:45 +00:00
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:9090/-/ready")
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
2017-07-26 00:47:45 +00:00
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:9090/version")
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
2017-07-26 00:47:45 +00:00
|
|
|
|
2018-09-19 07:20:53 +00:00
|
|
|
resp, err = http.Get("http://localhost:9090/graph")
|
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
|
|
|
|
2017-10-05 05:38:07 +00:00
|
|
|
resp, err = http.Post("http://localhost:9090/api/v2/admin/tsdb/snapshot", "", strings.NewReader(""))
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
2017-10-05 05:38:07 +00:00
|
|
|
|
|
|
|
resp, err = http.Post("http://localhost:9090/api/v2/admin/tsdb/delete_series", "", strings.NewReader("{}"))
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
2017-10-05 05:38:07 +00:00
|
|
|
|
2019-02-08 10:17:47 +00:00
|
|
|
resp, err = http.Get("http://localhost:9090/graph")
|
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:9090/alerts")
|
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:9090/flags")
|
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:9090/rules")
|
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:9090/service-discovery")
|
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:9090/targets")
|
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:9090/config")
|
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:9090/status")
|
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
|
|
|
|
2017-07-26 00:47:45 +00:00
|
|
|
// Set to ready.
|
|
|
|
webHandler.Ready()
|
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:9090/-/healthy")
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
2017-07-26 00:47:45 +00:00
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:9090/-/ready")
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
2017-07-26 00:47:45 +00:00
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:9090/version")
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
2017-10-05 05:38:07 +00:00
|
|
|
|
2018-09-19 07:20:53 +00:00
|
|
|
resp, err = http.Get("http://localhost:9090/graph")
|
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
2017-10-05 05:38:07 +00:00
|
|
|
resp, err = http.Post("http://localhost:9090/api/v2/admin/tsdb/snapshot", "", strings.NewReader(""))
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
2017-10-05 05:38:07 +00:00
|
|
|
|
|
|
|
resp, err = http.Post("http://localhost:9090/api/v2/admin/tsdb/delete_series", "", strings.NewReader("{}"))
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
2019-02-08 10:17:47 +00:00
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:9090/alerts")
|
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:9090/flags")
|
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:9090/rules")
|
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:9090/service-discovery")
|
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:9090/targets")
|
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:9090/config")
|
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:9090/status")
|
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
2017-09-06 12:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRoutePrefix(t *testing.T) {
|
|
|
|
t.Parallel()
|
2017-10-05 05:38:07 +00:00
|
|
|
dbDir, err := ioutil.TempDir("", "tsdb-ready")
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
2017-10-05 05:38:07 +00:00
|
|
|
defer os.RemoveAll(dbDir)
|
2017-10-05 20:40:10 +00:00
|
|
|
|
2017-10-05 05:38:07 +00:00
|
|
|
db, err := libtsdb.Open(dbDir, nil, nil, nil)
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
2017-10-05 05:38:07 +00:00
|
|
|
|
2017-09-06 12:41:07 +00:00
|
|
|
opts := &Options{
|
|
|
|
ListenAddress: ":9091",
|
|
|
|
ReadTimeout: 30 * time.Second,
|
|
|
|
MaxConnections: 512,
|
|
|
|
Context: nil,
|
2017-09-18 10:32:17 +00:00
|
|
|
Storage: &tsdb.ReadyStorage{},
|
2017-09-06 12:41:07 +00:00
|
|
|
QueryEngine: nil,
|
2017-11-29 22:52:38 +00:00
|
|
|
ScrapeManager: nil,
|
2017-09-06 12:41:07 +00:00
|
|
|
RuleManager: nil,
|
|
|
|
Notifier: nil,
|
|
|
|
RoutePrefix: "/prometheus",
|
2017-10-05 05:38:07 +00:00
|
|
|
EnableAdminAPI: true,
|
|
|
|
TSDB: func() *libtsdb.DB { return db },
|
2017-09-06 12:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
opts.Flags = map[string]string{}
|
|
|
|
|
|
|
|
webHandler := New(nil, opts)
|
|
|
|
go func() {
|
|
|
|
err := webHandler.Run(context.Background())
|
|
|
|
if err != nil {
|
2018-01-13 10:37:38 +00:00
|
|
|
panic(fmt.Sprintf("Can't start web handler:%s", err))
|
2017-09-06 12:41:07 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Give some time for the web goroutine to run since we need the server
|
|
|
|
// to be up before starting tests.
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
|
|
|
resp, err := http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/healthy")
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
2017-09-06 12:41:07 +00:00
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/ready")
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
2017-09-06 12:41:07 +00:00
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/version")
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
2017-10-05 05:38:07 +00:00
|
|
|
|
|
|
|
resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v2/admin/tsdb/snapshot", "", strings.NewReader(""))
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
2017-10-05 05:38:07 +00:00
|
|
|
|
|
|
|
resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v2/admin/tsdb/delete_series", "", strings.NewReader("{}"))
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
2017-09-06 12:41:07 +00:00
|
|
|
|
|
|
|
// Set to ready.
|
|
|
|
webHandler.Ready()
|
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/healthy")
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
2017-07-26 00:47:45 +00:00
|
|
|
|
2017-09-06 12:41:07 +00:00
|
|
|
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/ready")
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
2017-09-06 12:41:07 +00:00
|
|
|
|
|
|
|
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/version")
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
2017-10-05 05:38:07 +00:00
|
|
|
|
|
|
|
resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v2/admin/tsdb/snapshot", "", strings.NewReader(""))
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
2017-10-05 05:38:07 +00:00
|
|
|
|
|
|
|
resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v2/admin/tsdb/delete_series", "", strings.NewReader("{}"))
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
2017-07-26 00:47:45 +00:00
|
|
|
}
|
2018-11-15 13:22:16 +00:00
|
|
|
|
2017-09-01 11:12:51 +00:00
|
|
|
func TestDebugHandler(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
prefix, url string
|
|
|
|
code int
|
|
|
|
}{
|
|
|
|
{"/", "/debug/pprof/cmdline", 200},
|
|
|
|
{"/foo", "/foo/debug/pprof/cmdline", 200},
|
|
|
|
|
|
|
|
{"/", "/debug/pprof/goroutine", 200},
|
|
|
|
{"/foo", "/foo/debug/pprof/goroutine", 200},
|
|
|
|
|
|
|
|
{"/", "/debug/pprof/foo", 404},
|
|
|
|
{"/foo", "/bar/debug/pprof/goroutine", 404},
|
|
|
|
} {
|
|
|
|
opts := &Options{
|
|
|
|
RoutePrefix: tc.prefix,
|
|
|
|
}
|
2017-10-05 10:16:15 +00:00
|
|
|
handler := New(nil, opts)
|
2017-09-01 11:12:51 +00:00
|
|
|
handler.Ready()
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
2017-10-05 20:40:10 +00:00
|
|
|
|
2017-09-01 11:12:51 +00:00
|
|
|
req, err := http.NewRequest("GET", tc.url, nil)
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Ok(t, err)
|
2017-09-01 11:12:51 +00:00
|
|
|
|
|
|
|
handler.router.ServeHTTP(w, req)
|
2017-10-05 20:40:10 +00:00
|
|
|
|
|
|
|
testutil.Equals(t, tc.code, w.Code)
|
2017-09-01 11:12:51 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-19 15:56:04 +00:00
|
|
|
|
|
|
|
func TestHTTPMetrics(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
handler := New(nil, &Options{RoutePrefix: "/"})
|
|
|
|
getReady := func() int {
|
|
|
|
t.Helper()
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/-/ready", nil)
|
|
|
|
testutil.Ok(t, err)
|
|
|
|
|
|
|
|
handler.router.ServeHTTP(w, req)
|
|
|
|
return w.Code
|
|
|
|
}
|
|
|
|
|
|
|
|
code := getReady()
|
|
|
|
testutil.Equals(t, http.StatusServiceUnavailable, code)
|
2019-06-24 13:48:15 +00:00
|
|
|
counter := handler.metrics.requestCounter
|
|
|
|
testutil.Equals(t, 1, int(prom_testutil.ToFloat64(counter.WithLabelValues("/-/ready", strconv.Itoa(http.StatusServiceUnavailable)))))
|
2019-06-19 15:56:04 +00:00
|
|
|
|
|
|
|
handler.Ready()
|
|
|
|
for range [2]int{} {
|
|
|
|
code = getReady()
|
|
|
|
testutil.Equals(t, http.StatusOK, code)
|
|
|
|
}
|
2019-06-24 13:48:15 +00:00
|
|
|
testutil.Equals(t, 2, int(prom_testutil.ToFloat64(counter.WithLabelValues("/-/ready", strconv.Itoa(http.StatusOK)))))
|
|
|
|
testutil.Equals(t, 1, int(prom_testutil.ToFloat64(counter.WithLabelValues("/-/ready", strconv.Itoa(http.StatusServiceUnavailable)))))
|
2019-06-19 15:56:04 +00:00
|
|
|
}
|