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" "time"
"github.com/prometheus/prometheus/storage/tsdb" "github.com/prometheus/prometheus/storage/tsdb"
"github.com/prometheus/prometheus/util/testutil"
libtsdb "github.com/prometheus/tsdb" 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) 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) globalURL := tmplFuncs("", opts)["globalURL"].(func(u *url.URL) *url.URL)
outURL := globalURL(inURL) outURL := globalURL(inURL)
if outURL.String() != test.outURL { testutil.Equals(t, test.outURL, outURL.String())
t.Fatalf("%d. got %s, want %s", i, outURL.String(), test.outURL)
}
} }
} }
func TestReadyAndHealthy(t *testing.T) { func TestReadyAndHealthy(t *testing.T) {
t.Parallel() t.Parallel()
dbDir, err := ioutil.TempDir("", "tsdb-ready") 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) defer os.RemoveAll(dbDir)
db, err := libtsdb.Open(dbDir, nil, nil, nil) 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{ opts := &Options{
ListenAddress: ":9090", ListenAddress: ":9090",
@ -116,100 +114,70 @@ func TestReadyAndHealthy(t *testing.T) {
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")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err) testutil.Ok(t, err)
} testutil.Equals(t, http.StatusOK, resp.StatusCode)
if resp.StatusCode != http.StatusOK {
t.Fatalf("Path /-/healthy with server unready test, Expected status 200 got: %s", resp.Status)
}
resp, err = http.Get("http://localhost:9090/-/ready") resp, err = http.Get("http://localhost:9090/-/ready")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err) testutil.Ok(t, err)
} testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
if resp.StatusCode != http.StatusServiceUnavailable {
t.Fatalf("Path /-/ready with server unready test, Expected status 503 got: %s", resp.Status)
}
resp, err = http.Get("http://localhost:9090/version") resp, err = http.Get("http://localhost:9090/version")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err) testutil.Ok(t, err)
} testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
if resp.StatusCode != http.StatusServiceUnavailable {
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("")) resp, err = http.Post("http://localhost:9090/api/v2/admin/tsdb/snapshot", "", strings.NewReader(""))
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err) testutil.Ok(t, err)
} testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
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("{}")) 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) testutil.Ok(t, err)
} testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
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. // Set to ready.
webHandler.Ready() webHandler.Ready()
resp, err = http.Get("http://localhost:9090/-/healthy") resp, err = http.Get("http://localhost:9090/-/healthy")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err) testutil.Ok(t, err)
} testutil.Equals(t, http.StatusOK, resp.StatusCode)
if resp.StatusCode != http.StatusOK {
t.Fatalf("Path /-/healthy with server ready test, Expected status 200 got: %s", resp.Status)
}
resp, err = http.Get("http://localhost:9090/-/ready") resp, err = http.Get("http://localhost:9090/-/ready")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err) testutil.Ok(t, err)
} testutil.Equals(t, http.StatusOK, resp.StatusCode)
if resp.StatusCode != http.StatusOK {
t.Fatalf("Path /-/ready with server ready test, Expected status 200 got: %s", resp.Status)
}
resp, err = http.Get("http://localhost:9090/version") resp, err = http.Get("http://localhost:9090/version")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err) testutil.Ok(t, err)
} testutil.Equals(t, http.StatusOK, resp.StatusCode)
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("")) resp, err = http.Post("http://localhost:9090/api/v2/admin/tsdb/snapshot", "", strings.NewReader(""))
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err) testutil.Ok(t, err)
} testutil.Equals(t, http.StatusOK, resp.StatusCode)
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("{}")) 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) testutil.Ok(t, err)
} testutil.Equals(t, http.StatusOK, resp.StatusCode)
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) { func TestRoutePrefix(t *testing.T) {
t.Parallel() t.Parallel()
dbDir, err := ioutil.TempDir("", "tsdb-ready") 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) defer os.RemoveAll(dbDir)
db, err := libtsdb.Open(dbDir, nil, nil, nil) 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{ opts := &Options{
ListenAddress: ":9091", ListenAddress: ":9091",
@ -242,87 +210,57 @@ 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")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err) testutil.Ok(t, err)
} testutil.Equals(t, http.StatusOK, resp.StatusCode)
if resp.StatusCode != http.StatusOK {
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") resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/ready")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err) testutil.Ok(t, err)
} testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
if resp.StatusCode != http.StatusServiceUnavailable {
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") resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/version")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err) testutil.Ok(t, err)
} testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
if resp.StatusCode != http.StatusServiceUnavailable {
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("")) 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) testutil.Ok(t, err)
} testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
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("{}")) 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) testutil.Ok(t, err)
} testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode)
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. // 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")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err) testutil.Ok(t, err)
} testutil.Equals(t, http.StatusOK, resp.StatusCode)
if resp.StatusCode != http.StatusOK {
t.Fatalf("Path "+opts.RoutePrefix+"/-/healthy with server ready test, Expected status 200 got: %s", resp.Status)
}
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/ready") resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/ready")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err) testutil.Ok(t, err)
} testutil.Equals(t, http.StatusOK, resp.StatusCode)
if resp.StatusCode != http.StatusOK {
t.Fatalf("Path "+opts.RoutePrefix+"/-/ready with server ready test, Expected status 200 got: %s", resp.Status)
}
resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/version") resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/version")
if err != nil {
t.Fatalf("Unexpected HTTP error %s", err) testutil.Ok(t, err)
} testutil.Equals(t, http.StatusOK, resp.StatusCode)
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("")) 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) testutil.Ok(t, err)
} testutil.Equals(t, http.StatusOK, resp.StatusCode)
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("{}")) 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) testutil.Ok(t, err)
} testutil.Equals(t, http.StatusOK, resp.StatusCode)
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) { func TestDebugHandler(t *testing.T) {
@ -347,14 +285,13 @@ func TestDebugHandler(t *testing.T) {
handler.Ready() handler.Ready()
w := httptest.NewRecorder() w := httptest.NewRecorder()
req, err := http.NewRequest("GET", tc.url, nil) 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) 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)
}
} }
} }