Merge pull request #3286 from prometheus/dev-2.0

Dev 2.0
pull/3317/head
Fabian Reinartz 2017-10-12 16:19:10 +02:00 committed by GitHub
commit 53875da7fb
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

@ -26,6 +26,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"
) )
@ -65,31 +66,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",
@ -117,100 +115,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",
@ -243,87 +211,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) {
@ -348,14 +286,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)
}
} }
} }