Merge pull request #3245 from jlevesy/use-require-web

Imports benbjohnson/testing and use it in web package
This commit is contained in:
Fabian Reinartz 2017-10-10 10:58:27 +02:00 committed by GitHub
commit 269b5cf936
2 changed files with 140 additions and 145 deletions

58
util/testutil/testing.go Normal file
View File

@ -0,0 +1,58 @@
// The MIT License (MIT)
// Copyright (c) 2014 Ben Johnson
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package testutil
import (
"fmt"
"path/filepath"
"reflect"
"runtime"
"testing"
)
// Assert fails the test if the condition is false.
func Assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
if !condition {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
tb.FailNow()
}
}
// Ok fails the test if an err is not nil.
func Ok(tb testing.TB, err error) {
if err != nil {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error())
tb.FailNow()
}
}
// Equals fails the test if exp is not equal to act.
func Equals(tb testing.TB, exp, act interface{}) {
if !reflect.DeepEqual(exp, act) {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)
tb.FailNow()
}
}

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)
}
}