mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
web: Close and exhaust all response bodies in tests + cleanup snapshot directory (#7850)
* web: Close and exhaust all response bodies in tests Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu> * Remove empty snapshot directory Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu> * Fix lint Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>
This commit is contained in:
parent
a14245b4b1
commit
6573bf42f2
197
web/web_test.go
197
web/web_test.go
|
@ -15,13 +15,16 @@ package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -148,150 +151,73 @@ func TestReadyAndHealthy(t *testing.T) {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// TODO(bwplotka): Those tests create tons of new connection and memory that is never cleaned.
|
|
||||||
// Close and exhaust all response bodies.
|
|
||||||
|
|
||||||
// Give some time for the web goroutine to run since we need the server
|
// Give some time for the web goroutine to run since we need the server
|
||||||
// to be up before starting tests.
|
// to be up before starting tests.
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
|
|
||||||
resp, err := http.Get("http://localhost:9090/-/healthy")
|
resp, err := http.Get("http://localhost:9090/-/healthy")
|
||||||
|
|
||||||
testutil.Ok(t, err)
|
testutil.Ok(t, err)
|
||||||
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
||||||
|
cleanupTestResponse(t, resp)
|
||||||
|
|
||||||
resp, err = http.Get("http://localhost:9090/-/ready")
|
for _, u := range []string{
|
||||||
|
"http://localhost:9090/-/ready",
|
||||||
testutil.Ok(t, err)
|
"http://localhost:9090/version",
|
||||||
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
"http://localhost:9090/graph",
|
||||||
|
"http://localhost:9090/flags",
|
||||||
resp, err = http.Get("http://localhost:9090/version")
|
"http://localhost:9090/rules",
|
||||||
|
"http://localhost:9090/service-discovery",
|
||||||
testutil.Ok(t, err)
|
"http://localhost:9090/targets",
|
||||||
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
"http://localhost:9090/status",
|
||||||
|
"http://localhost:9090/config",
|
||||||
resp, err = http.Get("http://localhost:9090/graph")
|
} {
|
||||||
|
resp, err = http.Get(u)
|
||||||
testutil.Ok(t, err)
|
testutil.Ok(t, err)
|
||||||
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
||||||
|
cleanupTestResponse(t, resp)
|
||||||
|
}
|
||||||
|
|
||||||
resp, err = http.Post("http://localhost:9090/api/v2/admin/tsdb/snapshot", "", strings.NewReader(""))
|
resp, err = http.Post("http://localhost:9090/api/v2/admin/tsdb/snapshot", "", strings.NewReader(""))
|
||||||
|
|
||||||
testutil.Ok(t, err)
|
testutil.Ok(t, err)
|
||||||
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
||||||
|
cleanupTestResponse(t, resp)
|
||||||
|
|
||||||
resp, err = http.Post("http://localhost:9090/api/v2/admin/tsdb/delete_series", "", strings.NewReader("{}"))
|
resp, err = http.Post("http://localhost:9090/api/v2/admin/tsdb/delete_series", "", strings.NewReader("{}"))
|
||||||
|
|
||||||
testutil.Ok(t, err)
|
|
||||||
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
|
||||||
|
|
||||||
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.Ok(t, err)
|
||||||
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
||||||
|
cleanupTestResponse(t, resp)
|
||||||
|
|
||||||
// Set to ready.
|
// Set to ready.
|
||||||
webHandler.Ready()
|
webHandler.Ready()
|
||||||
|
|
||||||
resp, err = http.Get("http://localhost:9090/-/healthy")
|
for _, u := range []string{
|
||||||
|
"http://localhost:9090/-/healthy",
|
||||||
testutil.Ok(t, err)
|
"http://localhost:9090/-/ready",
|
||||||
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
"http://localhost:9090/version",
|
||||||
|
"http://localhost:9090/graph",
|
||||||
resp, err = http.Get("http://localhost:9090/-/ready")
|
"http://localhost:9090/flags",
|
||||||
|
"http://localhost:9090/rules",
|
||||||
testutil.Ok(t, err)
|
"http://localhost:9090/service-discovery",
|
||||||
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
"http://localhost:9090/targets",
|
||||||
|
"http://localhost:9090/status",
|
||||||
resp, err = http.Get("http://localhost:9090/version")
|
"http://localhost:9090/config",
|
||||||
|
} {
|
||||||
testutil.Ok(t, err)
|
resp, err = http.Get(u)
|
||||||
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
testutil.Ok(t, err)
|
||||||
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
||||||
resp, err = http.Get("http://localhost:9090/graph")
|
cleanupTestResponse(t, resp)
|
||||||
|
}
|
||||||
testutil.Ok(t, err)
|
|
||||||
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
|
||||||
|
|
||||||
resp, err = http.Post("http://localhost:9090/api/v2/admin/tsdb/snapshot", "", strings.NewReader(""))
|
resp, err = http.Post("http://localhost:9090/api/v2/admin/tsdb/snapshot", "", strings.NewReader(""))
|
||||||
|
|
||||||
testutil.Ok(t, err)
|
testutil.Ok(t, err)
|
||||||
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
||||||
|
cleanupSnapshot(t, resp)
|
||||||
|
cleanupTestResponse(t, resp)
|
||||||
|
|
||||||
resp, err = http.Post("http://localhost:9090/api/v2/admin/tsdb/delete_series", "", strings.NewReader("{}"))
|
resp, err = http.Post("http://localhost:9090/api/v2/admin/tsdb/delete_series", "", strings.NewReader("{}"))
|
||||||
|
|
||||||
testutil.Ok(t, err)
|
|
||||||
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
|
||||||
|
|
||||||
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.Ok(t, err)
|
||||||
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
||||||
|
cleanupTestResponse(t, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRoutePrefix(t *testing.T) {
|
func TestRoutePrefix(t *testing.T) {
|
||||||
|
@ -340,57 +266,58 @@ func TestRoutePrefix(t *testing.T) {
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
|
|
||||||
resp, err := http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/healthy")
|
resp, err := http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/healthy")
|
||||||
|
|
||||||
testutil.Ok(t, err)
|
testutil.Ok(t, err)
|
||||||
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
||||||
|
cleanupTestResponse(t, resp)
|
||||||
|
|
||||||
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/ready")
|
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/ready")
|
||||||
|
|
||||||
testutil.Ok(t, err)
|
testutil.Ok(t, err)
|
||||||
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
||||||
|
cleanupTestResponse(t, resp)
|
||||||
|
|
||||||
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/version")
|
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/version")
|
||||||
|
|
||||||
testutil.Ok(t, err)
|
testutil.Ok(t, err)
|
||||||
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
||||||
|
cleanupTestResponse(t, resp)
|
||||||
|
|
||||||
resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v2/admin/tsdb/snapshot", "", strings.NewReader(""))
|
resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v2/admin/tsdb/snapshot", "", strings.NewReader(""))
|
||||||
|
|
||||||
testutil.Ok(t, err)
|
testutil.Ok(t, err)
|
||||||
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
||||||
|
cleanupTestResponse(t, resp)
|
||||||
|
|
||||||
resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v2/admin/tsdb/delete_series", "", strings.NewReader("{}"))
|
resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v2/admin/tsdb/delete_series", "", strings.NewReader("{}"))
|
||||||
|
|
||||||
testutil.Ok(t, err)
|
testutil.Ok(t, err)
|
||||||
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
|
||||||
|
cleanupTestResponse(t, resp)
|
||||||
|
|
||||||
// Set to ready.
|
// Set to ready.
|
||||||
webHandler.Ready()
|
webHandler.Ready()
|
||||||
|
|
||||||
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/healthy")
|
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/healthy")
|
||||||
|
|
||||||
testutil.Ok(t, err)
|
testutil.Ok(t, err)
|
||||||
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
||||||
|
cleanupTestResponse(t, resp)
|
||||||
|
|
||||||
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/ready")
|
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/ready")
|
||||||
|
|
||||||
testutil.Ok(t, err)
|
testutil.Ok(t, err)
|
||||||
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
||||||
|
cleanupTestResponse(t, resp)
|
||||||
|
|
||||||
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/version")
|
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/version")
|
||||||
|
|
||||||
testutil.Ok(t, err)
|
testutil.Ok(t, err)
|
||||||
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
||||||
|
cleanupTestResponse(t, resp)
|
||||||
|
|
||||||
resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v2/admin/tsdb/snapshot", "", strings.NewReader(""))
|
resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v2/admin/tsdb/snapshot", "", strings.NewReader(""))
|
||||||
|
|
||||||
testutil.Ok(t, err)
|
testutil.Ok(t, err)
|
||||||
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
||||||
|
cleanupSnapshot(t, resp)
|
||||||
|
cleanupTestResponse(t, resp)
|
||||||
|
|
||||||
resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v2/admin/tsdb/delete_series", "", strings.NewReader("{}"))
|
resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v2/admin/tsdb/delete_series", "", strings.NewReader("{}"))
|
||||||
|
|
||||||
testutil.Ok(t, err)
|
testutil.Ok(t, err)
|
||||||
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
testutil.Equals(t, http.StatusOK, resp.StatusCode)
|
||||||
|
cleanupTestResponse(t, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDebugHandler(t *testing.T) {
|
func TestDebugHandler(t *testing.T) {
|
||||||
|
@ -534,3 +461,21 @@ func TestShutdownWithStaleConnection(t *testing.T) {
|
||||||
t.Fatalf("Server still running after read timeout.")
|
t.Fatalf("Server still running after read timeout.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func cleanupTestResponse(t *testing.T, resp *http.Response) {
|
||||||
|
_, err := io.Copy(ioutil.Discard, resp.Body)
|
||||||
|
testutil.Ok(t, err)
|
||||||
|
testutil.Ok(t, resp.Body.Close())
|
||||||
|
}
|
||||||
|
|
||||||
|
func cleanupSnapshot(t *testing.T, resp *http.Response) {
|
||||||
|
snapshot := &struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
}{}
|
||||||
|
b, err := ioutil.ReadAll(resp.Body)
|
||||||
|
testutil.Ok(t, err)
|
||||||
|
testutil.Ok(t, json.Unmarshal(b, snapshot))
|
||||||
|
testutil.Assert(t, snapshot.Name != "", "snapshot directory not returned")
|
||||||
|
testutil.Ok(t, os.Remove(filepath.Join("snapshots", snapshot.Name)))
|
||||||
|
os.Remove("snapshots") // We do not check for err here to prevent existing setups to fail.
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue