use testutil assertion helpers in web package testsuite

This commit is contained in:
Julien Levesy 2017-10-05 22:40:10 +02:00
parent 332910d02d
commit 26a067b23d

View File

@ -25,6 +25,7 @@ import (
"time"
"github.com/prometheus/prometheus/storage/tsdb"
"github.com/prometheus/prometheus/util/testutil"
libtsdb "github.com/prometheus/tsdb"
)
@ -64,31 +65,28 @@ func TestGlobalURL(t *testing.T) {
},
}
for i, test := range tests {
for _, test := range tests {
inURL, err := url.Parse(test.inURL)
if err != nil {
t.Fatalf("%d. Error parsing input URL: %s", i, err)
}
testutil.Ok(t, err)
globalURL := tmplFuncs("", opts)["globalURL"].(func(u *url.URL) *url.URL)
outURL := globalURL(inURL)
if outURL.String() != test.outURL {
t.Fatalf("%d. got %s, want %s", i, outURL.String(), test.outURL)
}
testutil.Equals(t, test.outURL, outURL.String())
}
}
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)
}
testutil.Ok(t, 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)
}
testutil.Ok(t, err)
opts := &Options{
ListenAddress: ":9090",
@ -116,100 +114,70 @@ func TestReadyAndHealthy(t *testing.T) {
time.Sleep(5 * time.Second)
resp, err := http.Get("http://localhost:9090/-/healthy")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("Path /-/healthy with server unready test, Expected status 200 got: %s", resp.Status)
}
testutil.Ok(t, err)
testutil.Equals(t, http.StatusOK, resp.StatusCode)
resp, err = http.Get("http://localhost:9090/-/ready")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err)
}
if resp.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("Path /-/ready with server unready test, Expected status 503 got: %s", resp.Status)
}
testutil.Ok(t, err)
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
resp, err = http.Get("http://localhost:9090/version")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err)
}
if resp.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("Path /version with server unready test, Expected status 503 got: %s", resp.Status)
}
testutil.Ok(t, err)
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
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)
}
testutil.Ok(t, err)
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
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)
}
testutil.Ok(t, err)
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
// Set to ready.
webHandler.Ready()
resp, err = http.Get("http://localhost:9090/-/healthy")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("Path /-/healthy with server ready test, Expected status 200 got: %s", resp.Status)
}
testutil.Ok(t, err)
testutil.Equals(t, http.StatusOK, resp.StatusCode)
resp, err = http.Get("http://localhost:9090/-/ready")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("Path /-/ready with server ready test, Expected status 200 got: %s", resp.Status)
}
testutil.Ok(t, err)
testutil.Equals(t, http.StatusOK, resp.StatusCode)
resp, err = http.Get("http://localhost:9090/version")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("Path /version with server ready test, Expected status 200 got: %s", resp.Status)
}
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(""))
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)
}
testutil.Ok(t, err)
testutil.Equals(t, http.StatusOK, resp.StatusCode)
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)
}
testutil.Ok(t, err)
testutil.Equals(t, http.StatusOK, resp.StatusCode)
}
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)
}
testutil.Ok(t, 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)
}
testutil.Ok(t, err)
opts := &Options{
ListenAddress: ":9091",
@ -242,87 +210,57 @@ func TestRoutePrefix(t *testing.T) {
time.Sleep(5 * time.Second)
resp, err := http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/healthy")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("Path %s/-/healthy with server unready test, Expected status 200 got: %s", opts.RoutePrefix, resp.Status)
}
testutil.Ok(t, err)
testutil.Equals(t, http.StatusOK, resp.StatusCode)
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/ready")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err)
}
if resp.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("Path %s/-/ready with server unready test, Expected status 503 got: %s", opts.RoutePrefix, resp.Status)
}
testutil.Ok(t, err)
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/version")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err)
}
if resp.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("Path %s/version with server unready test, Expected status 503 got: %s", opts.RoutePrefix, resp.Status)
}
testutil.Ok(t, err)
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
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)
}
testutil.Ok(t, err)
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
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)
}
testutil.Ok(t, err)
testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
// Set to ready.
webHandler.Ready()
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/healthy")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("Path "+opts.RoutePrefix+"/-/healthy with server ready test, Expected status 200 got: %s", resp.Status)
}
testutil.Ok(t, err)
testutil.Equals(t, http.StatusOK, resp.StatusCode)
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/ready")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("Path "+opts.RoutePrefix+"/-/ready with server ready test, Expected status 200 got: %s", resp.Status)
}
testutil.Ok(t, err)
testutil.Equals(t, http.StatusOK, resp.StatusCode)
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/version")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("Path "+opts.RoutePrefix+"/version with server ready test, Expected status 200 got: %s", resp.Status)
}
testutil.Ok(t, err)
testutil.Equals(t, http.StatusOK, resp.StatusCode)
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)
}
testutil.Ok(t, err)
testutil.Equals(t, http.StatusOK, resp.StatusCode)
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)
}
testutil.Ok(t, err)
testutil.Equals(t, http.StatusOK, resp.StatusCode)
}
func TestDebugHandler(t *testing.T) {
@ -347,14 +285,13 @@ func TestDebugHandler(t *testing.T) {
handler.Ready()
w := httptest.NewRecorder()
req, err := http.NewRequest("GET", tc.url, nil)
if err != nil {
t.Fatalf("Unexpected error %s", err)
}
testutil.Ok(t, err)
handler.router.ServeHTTP(w, req)
if w.Code != tc.code {
t.Fatalf("Unexpected status code %d: %s", w.Code, w.Body.String())
}
testutil.Equals(t, tc.code, w.Code)
}
}