2017-04-20 00:24:14 +03:00
|
|
|
// Copyright 2017 The Prometheus Authors
|
2015-08-19 16:33:59 +03:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-04-25 00:05:17 +03:00
|
|
|
// +build go1.7
|
|
|
|
|
2017-04-20 00:24:14 +03:00
|
|
|
package v1
|
2015-08-19 16:33:59 +03:00
|
|
|
|
|
|
|
import (
|
2017-04-20 15:57:46 +03:00
|
|
|
"context"
|
2015-08-19 16:33:59 +03:00
|
|
|
"encoding/json"
|
2018-10-25 20:44:21 +03:00
|
|
|
"errors"
|
2015-08-19 16:33:59 +03:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"reflect"
|
2017-04-01 01:42:19 +03:00
|
|
|
"strings"
|
2015-08-19 16:33:59 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-04-20 00:24:14 +03:00
|
|
|
"github.com/prometheus/common/model"
|
2015-08-19 16:33:59 +03:00
|
|
|
)
|
|
|
|
|
2017-04-20 00:24:14 +03:00
|
|
|
type apiTest struct {
|
2018-10-25 20:44:21 +03:00
|
|
|
do func() (interface{}, error)
|
|
|
|
inErr error
|
|
|
|
inStatusCode int
|
|
|
|
inRes interface{}
|
2017-04-20 00:24:14 +03:00
|
|
|
|
|
|
|
reqPath string
|
|
|
|
reqParam url.Values
|
|
|
|
reqMethod string
|
|
|
|
res interface{}
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
type apiTestClient struct {
|
|
|
|
*testing.T
|
|
|
|
curTest apiTest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *apiTestClient) URL(ep string, args map[string]string) *url.URL {
|
|
|
|
path := ep
|
|
|
|
for k, v := range args {
|
|
|
|
path = strings.Replace(path, ":"+k, v, -1)
|
2015-08-19 16:33:59 +03:00
|
|
|
}
|
2017-04-20 00:24:14 +03:00
|
|
|
u := &url.URL{
|
|
|
|
Host: "test:9090",
|
|
|
|
Path: path,
|
|
|
|
}
|
|
|
|
return u
|
2015-08-19 16:33:59 +03:00
|
|
|
}
|
|
|
|
|
2017-04-20 00:24:14 +03:00
|
|
|
func (c *apiTestClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, error) {
|
|
|
|
|
|
|
|
test := c.curTest
|
|
|
|
|
|
|
|
if req.URL.Path != test.reqPath {
|
|
|
|
c.Errorf("unexpected request path: want %s, got %s", test.reqPath, req.URL.Path)
|
|
|
|
}
|
|
|
|
if req.Method != test.reqMethod {
|
|
|
|
c.Errorf("unexpected request method: want %s, got %s", test.reqMethod, req.Method)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(test.inRes)
|
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &http.Response{}
|
2018-10-25 20:44:21 +03:00
|
|
|
if test.inStatusCode != 0 {
|
|
|
|
resp.StatusCode = test.inStatusCode
|
|
|
|
} else if test.inErr != nil {
|
2017-04-20 00:24:14 +03:00
|
|
|
resp.StatusCode = statusAPIError
|
|
|
|
} else {
|
|
|
|
resp.StatusCode = http.StatusOK
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, b, test.inErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPIs(t *testing.T) {
|
|
|
|
|
|
|
|
testTime := time.Now()
|
|
|
|
|
|
|
|
client := &apiTestClient{T: t}
|
|
|
|
|
2018-04-06 02:47:48 +03:00
|
|
|
promAPI := &httpAPI{
|
2017-04-20 00:24:14 +03:00
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:21:25 +03:00
|
|
|
doAlertManagers := func() func() (interface{}, error) {
|
2017-04-20 00:24:14 +03:00
|
|
|
return func() (interface{}, error) {
|
2018-04-10 17:21:25 +03:00
|
|
|
return promAPI.AlertManagers(context.Background())
|
2017-04-20 00:24:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:21:25 +03:00
|
|
|
doCleanTombstones := func() func() (interface{}, error) {
|
2017-04-20 00:24:14 +03:00
|
|
|
return func() (interface{}, error) {
|
2018-04-10 17:21:25 +03:00
|
|
|
return nil, promAPI.CleanTombstones(context.Background())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
doConfig := func() func() (interface{}, error) {
|
|
|
|
return func() (interface{}, error) {
|
|
|
|
return promAPI.Config(context.Background())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
doDeleteSeries := func(matcher string, startTime time.Time, endTime time.Time) func() (interface{}, error) {
|
|
|
|
return func() (interface{}, error) {
|
|
|
|
return nil, promAPI.DeleteSeries(context.Background(), []string{matcher}, startTime, endTime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
doFlags := func() func() (interface{}, error) {
|
|
|
|
return func() (interface{}, error) {
|
|
|
|
return promAPI.Flags(context.Background())
|
2017-04-20 00:24:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
doLabelValues := func(label string) func() (interface{}, error) {
|
|
|
|
return func() (interface{}, error) {
|
2018-04-06 02:47:48 +03:00
|
|
|
return promAPI.LabelValues(context.Background(), label)
|
2017-04-20 00:24:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:21:25 +03:00
|
|
|
doQuery := func(q string, ts time.Time) func() (interface{}, error) {
|
2017-11-24 15:12:53 +03:00
|
|
|
return func() (interface{}, error) {
|
2018-04-10 17:21:25 +03:00
|
|
|
return promAPI.Query(context.Background(), q, ts)
|
2018-04-06 02:47:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:21:25 +03:00
|
|
|
doQueryRange := func(q string, rng Range) func() (interface{}, error) {
|
2018-04-06 02:47:48 +03:00
|
|
|
return func() (interface{}, error) {
|
2018-04-10 17:21:25 +03:00
|
|
|
return promAPI.QueryRange(context.Background(), q, rng)
|
2018-04-06 02:47:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:21:25 +03:00
|
|
|
doSeries := func(matcher string, startTime time.Time, endTime time.Time) func() (interface{}, error) {
|
2018-04-06 02:47:48 +03:00
|
|
|
return func() (interface{}, error) {
|
2018-04-10 17:21:25 +03:00
|
|
|
return promAPI.Series(context.Background(), []string{matcher}, startTime, endTime)
|
2018-04-06 02:47:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:21:25 +03:00
|
|
|
doSnapshot := func(skipHead bool) func() (interface{}, error) {
|
2018-04-06 02:47:48 +03:00
|
|
|
return func() (interface{}, error) {
|
2018-04-10 17:21:25 +03:00
|
|
|
return promAPI.Snapshot(context.Background(), skipHead)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
doTargets := func() func() (interface{}, error) {
|
|
|
|
return func() (interface{}, error) {
|
|
|
|
return promAPI.Targets(context.Background())
|
2017-11-24 15:12:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 00:24:14 +03:00
|
|
|
queryTests := []apiTest{
|
2015-08-19 16:33:59 +03:00
|
|
|
{
|
2017-04-20 00:24:14 +03:00
|
|
|
do: doQuery("2", testTime),
|
|
|
|
inRes: &queryResult{
|
|
|
|
Type: model.ValScalar,
|
|
|
|
Result: &model.Scalar{
|
|
|
|
Value: 2,
|
|
|
|
Timestamp: model.TimeFromUnix(testTime.Unix()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
reqMethod: "GET",
|
|
|
|
reqPath: "/api/v1/query",
|
|
|
|
reqParam: url.Values{
|
|
|
|
"query": []string{"2"},
|
|
|
|
"time": []string{testTime.Format(time.RFC3339Nano)},
|
|
|
|
},
|
|
|
|
res: &model.Scalar{
|
|
|
|
Value: 2,
|
|
|
|
Timestamp: model.TimeFromUnix(testTime.Unix()),
|
|
|
|
},
|
2015-08-19 16:33:59 +03:00
|
|
|
},
|
|
|
|
{
|
2017-04-20 00:24:14 +03:00
|
|
|
do: doQuery("2", testTime),
|
|
|
|
inErr: fmt.Errorf("some error"),
|
|
|
|
|
|
|
|
reqMethod: "GET",
|
|
|
|
reqPath: "/api/v1/query",
|
|
|
|
reqParam: url.Values{
|
|
|
|
"query": []string{"2"},
|
|
|
|
"time": []string{testTime.Format(time.RFC3339Nano)},
|
2015-08-19 16:33:59 +03:00
|
|
|
},
|
2017-04-20 00:24:14 +03:00
|
|
|
err: fmt.Errorf("some error"),
|
2015-08-19 16:33:59 +03:00
|
|
|
},
|
2018-10-25 20:44:21 +03:00
|
|
|
{
|
|
|
|
do: doQuery("2", testTime),
|
|
|
|
inRes: "some body",
|
|
|
|
inStatusCode: 500,
|
|
|
|
inErr: &Error{
|
|
|
|
Type: ErrServer,
|
|
|
|
Msg: "server error: 500",
|
|
|
|
Detail: "some body",
|
|
|
|
},
|
|
|
|
|
|
|
|
reqMethod: "GET",
|
|
|
|
reqPath: "/api/v1/query",
|
|
|
|
reqParam: url.Values{
|
|
|
|
"query": []string{"2"},
|
|
|
|
"time": []string{testTime.Format(time.RFC3339Nano)},
|
|
|
|
},
|
|
|
|
err: errors.New("server_error: server error: 500"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
do: doQuery("2", testTime),
|
|
|
|
inRes: "some body",
|
|
|
|
inStatusCode: 404,
|
|
|
|
inErr: &Error{
|
|
|
|
Type: ErrClient,
|
|
|
|
Msg: "client error: 404",
|
|
|
|
Detail: "some body",
|
|
|
|
},
|
|
|
|
|
|
|
|
reqMethod: "GET",
|
|
|
|
reqPath: "/api/v1/query",
|
|
|
|
reqParam: url.Values{
|
|
|
|
"query": []string{"2"},
|
|
|
|
"time": []string{testTime.Format(time.RFC3339Nano)},
|
|
|
|
},
|
|
|
|
err: errors.New("client_error: client error: 404"),
|
|
|
|
},
|
2017-04-20 00:24:14 +03:00
|
|
|
|
2015-08-19 16:33:59 +03:00
|
|
|
{
|
2017-04-20 00:24:14 +03:00
|
|
|
do: doQueryRange("2", Range{
|
|
|
|
Start: testTime.Add(-time.Minute),
|
|
|
|
End: testTime,
|
|
|
|
Step: time.Minute,
|
|
|
|
}),
|
|
|
|
inErr: fmt.Errorf("some error"),
|
|
|
|
|
|
|
|
reqMethod: "GET",
|
|
|
|
reqPath: "/api/v1/query_range",
|
|
|
|
reqParam: url.Values{
|
|
|
|
"query": []string{"2"},
|
|
|
|
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)},
|
|
|
|
"end": []string{testTime.Format(time.RFC3339Nano)},
|
|
|
|
"step": []string{time.Minute.String()},
|
2015-08-19 16:33:59 +03:00
|
|
|
},
|
2017-04-20 00:24:14 +03:00
|
|
|
err: fmt.Errorf("some error"),
|
2015-08-19 16:33:59 +03:00
|
|
|
},
|
2017-04-20 00:24:14 +03:00
|
|
|
|
2015-08-19 16:33:59 +03:00
|
|
|
{
|
2017-04-20 00:24:14 +03:00
|
|
|
do: doLabelValues("mylabel"),
|
|
|
|
inRes: []string{"val1", "val2"},
|
|
|
|
reqMethod: "GET",
|
|
|
|
reqPath: "/api/v1/label/mylabel/values",
|
|
|
|
res: model.LabelValues{"val1", "val2"},
|
2015-08-19 16:33:59 +03:00
|
|
|
},
|
2017-04-20 00:24:14 +03:00
|
|
|
|
2015-08-19 16:33:59 +03:00
|
|
|
{
|
2017-04-20 00:24:14 +03:00
|
|
|
do: doLabelValues("mylabel"),
|
|
|
|
inErr: fmt.Errorf("some error"),
|
|
|
|
reqMethod: "GET",
|
|
|
|
reqPath: "/api/v1/label/mylabel/values",
|
|
|
|
err: fmt.Errorf("some error"),
|
2015-08-19 16:33:59 +03:00
|
|
|
},
|
2017-11-24 15:12:53 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
do: doSeries("up", testTime.Add(-time.Minute), testTime),
|
|
|
|
inRes: []map[string]string{
|
|
|
|
{
|
|
|
|
"__name__": "up",
|
|
|
|
"job": "prometheus",
|
|
|
|
"instance": "localhost:9090"},
|
|
|
|
},
|
|
|
|
reqMethod: "GET",
|
|
|
|
reqPath: "/api/v1/series",
|
|
|
|
reqParam: url.Values{
|
|
|
|
"match": []string{"up"},
|
|
|
|
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)},
|
|
|
|
"end": []string{testTime.Format(time.RFC3339Nano)},
|
|
|
|
},
|
|
|
|
res: []model.LabelSet{
|
|
|
|
model.LabelSet{
|
|
|
|
"__name__": "up",
|
|
|
|
"job": "prometheus",
|
|
|
|
"instance": "localhost:9090",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
do: doSeries("up", testTime.Add(-time.Minute), testTime),
|
|
|
|
inErr: fmt.Errorf("some error"),
|
|
|
|
reqMethod: "GET",
|
|
|
|
reqPath: "/api/v1/series",
|
|
|
|
reqParam: url.Values{
|
|
|
|
"match": []string{"up"},
|
|
|
|
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)},
|
|
|
|
"end": []string{testTime.Format(time.RFC3339Nano)},
|
|
|
|
},
|
|
|
|
err: fmt.Errorf("some error"),
|
|
|
|
},
|
2018-04-06 02:47:48 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
do: doSnapshot(true),
|
|
|
|
inRes: map[string]string{
|
|
|
|
"name": "20171210T211224Z-2be650b6d019eb54",
|
|
|
|
},
|
|
|
|
reqMethod: "POST",
|
|
|
|
reqPath: "/api/v1/admin/tsdb/snapshot",
|
|
|
|
reqParam: url.Values{
|
|
|
|
"skip_head": []string{"true"},
|
|
|
|
},
|
|
|
|
res: SnapshotResult{
|
|
|
|
Name: "20171210T211224Z-2be650b6d019eb54",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
do: doSnapshot(true),
|
|
|
|
inErr: fmt.Errorf("some error"),
|
|
|
|
reqMethod: "POST",
|
|
|
|
reqPath: "/api/v1/admin/tsdb/snapshot",
|
|
|
|
err: fmt.Errorf("some error"),
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
do: doCleanTombstones(),
|
|
|
|
reqMethod: "POST",
|
|
|
|
reqPath: "/api/v1/admin/tsdb/clean_tombstones",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
do: doCleanTombstones(),
|
|
|
|
inErr: fmt.Errorf("some error"),
|
|
|
|
reqMethod: "POST",
|
|
|
|
reqPath: "/api/v1/admin/tsdb/clean_tombstones",
|
|
|
|
err: fmt.Errorf("some error"),
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
do: doDeleteSeries("up", testTime.Add(-time.Minute), testTime),
|
|
|
|
inRes: []map[string]string{
|
|
|
|
{
|
|
|
|
"__name__": "up",
|
|
|
|
"job": "prometheus",
|
|
|
|
"instance": "localhost:9090"},
|
|
|
|
},
|
|
|
|
reqMethod: "POST",
|
|
|
|
reqPath: "/api/v1/admin/tsdb/delete_series",
|
|
|
|
reqParam: url.Values{
|
|
|
|
"match": []string{"up"},
|
|
|
|
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)},
|
|
|
|
"end": []string{testTime.Format(time.RFC3339Nano)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
do: doDeleteSeries("up", testTime.Add(-time.Minute), testTime),
|
|
|
|
inErr: fmt.Errorf("some error"),
|
|
|
|
reqMethod: "POST",
|
|
|
|
reqPath: "/api/v1/admin/tsdb/delete_series",
|
|
|
|
reqParam: url.Values{
|
|
|
|
"match": []string{"up"},
|
|
|
|
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)},
|
|
|
|
"end": []string{testTime.Format(time.RFC3339Nano)},
|
|
|
|
},
|
|
|
|
err: fmt.Errorf("some error"),
|
|
|
|
},
|
2018-04-10 17:21:25 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
do: doConfig(),
|
|
|
|
reqMethod: "GET",
|
|
|
|
reqPath: "/api/v1/status/config",
|
|
|
|
inRes: map[string]string{
|
|
|
|
"yaml": "<content of the loaded config file in YAML>",
|
|
|
|
},
|
|
|
|
res: ConfigResult{
|
|
|
|
YAML: "<content of the loaded config file in YAML>",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
do: doConfig(),
|
|
|
|
reqMethod: "GET",
|
|
|
|
reqPath: "/api/v1/status/config",
|
|
|
|
inErr: fmt.Errorf("some error"),
|
|
|
|
err: fmt.Errorf("some error"),
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
do: doFlags(),
|
|
|
|
reqMethod: "GET",
|
|
|
|
reqPath: "/api/v1/status/flags",
|
|
|
|
inRes: map[string]string{
|
|
|
|
"alertmanager.notification-queue-capacity": "10000",
|
|
|
|
"alertmanager.timeout": "10s",
|
|
|
|
"log.level": "info",
|
|
|
|
"query.lookback-delta": "5m",
|
|
|
|
"query.max-concurrency": "20",
|
|
|
|
},
|
|
|
|
res: FlagsResult{
|
2018-04-11 17:38:10 +03:00
|
|
|
"alertmanager.notification-queue-capacity": "10000",
|
|
|
|
"alertmanager.timeout": "10s",
|
|
|
|
"log.level": "info",
|
|
|
|
"query.lookback-delta": "5m",
|
|
|
|
"query.max-concurrency": "20",
|
2018-04-10 17:21:25 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
do: doFlags(),
|
|
|
|
reqMethod: "GET",
|
|
|
|
reqPath: "/api/v1/status/flags",
|
|
|
|
inErr: fmt.Errorf("some error"),
|
|
|
|
err: fmt.Errorf("some error"),
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
do: doAlertManagers(),
|
|
|
|
reqMethod: "GET",
|
|
|
|
reqPath: "/api/v1/alertmanagers",
|
|
|
|
inRes: map[string]interface{}{
|
|
|
|
"activeAlertManagers": []map[string]string{
|
|
|
|
{
|
2018-04-11 17:38:10 +03:00
|
|
|
"url": "http://127.0.0.1:9091/api/v1/alerts",
|
2018-04-10 17:21:25 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"droppedAlertManagers": []map[string]string{
|
|
|
|
{
|
2018-04-11 17:38:10 +03:00
|
|
|
"url": "http://127.0.0.1:9092/api/v1/alerts",
|
2018-04-10 17:21:25 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: AlertManagersResult{
|
|
|
|
Active: []AlertManager{
|
|
|
|
{
|
2018-04-11 17:38:10 +03:00
|
|
|
URL: "http://127.0.0.1:9091/api/v1/alerts",
|
2018-04-10 17:21:25 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Dropped: []AlertManager{
|
|
|
|
{
|
2018-04-11 17:38:10 +03:00
|
|
|
URL: "http://127.0.0.1:9092/api/v1/alerts",
|
2018-04-10 17:21:25 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
do: doAlertManagers(),
|
|
|
|
reqMethod: "GET",
|
|
|
|
reqPath: "/api/v1/alertmanagers",
|
|
|
|
inErr: fmt.Errorf("some error"),
|
|
|
|
err: fmt.Errorf("some error"),
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
do: doTargets(),
|
|
|
|
reqMethod: "GET",
|
|
|
|
reqPath: "/api/v1/targets",
|
|
|
|
inRes: map[string]interface{}{
|
|
|
|
"activeTargets": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"discoveredLabels": map[string]string{
|
|
|
|
"__address__": "127.0.0.1:9090",
|
|
|
|
"__metrics_path__": "/metrics",
|
|
|
|
"__scheme__": "http",
|
|
|
|
"job": "prometheus",
|
|
|
|
},
|
|
|
|
"labels": map[string]string{
|
|
|
|
"instance": "127.0.0.1:9090",
|
|
|
|
"job": "prometheus",
|
|
|
|
},
|
2018-04-11 17:38:10 +03:00
|
|
|
"scrapeUrl": "http://127.0.0.1:9090",
|
2018-04-10 17:21:25 +03:00
|
|
|
"lastError": "error while scraping target",
|
2018-04-11 05:16:08 +03:00
|
|
|
"lastScrape": testTime.UTC().Format(time.RFC3339Nano),
|
2018-04-10 17:21:25 +03:00
|
|
|
"health": "up",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"droppedTargets": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"discoveredLabels": map[string]string{
|
|
|
|
"__address__": "127.0.0.1:9100",
|
|
|
|
"__metrics_path__": "/metrics",
|
|
|
|
"__scheme__": "http",
|
|
|
|
"job": "node",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
res: TargetsResult{
|
|
|
|
Active: []ActiveTarget{
|
|
|
|
{
|
|
|
|
DiscoveredLabels: model.LabelSet{
|
|
|
|
"__address__": "127.0.0.1:9090",
|
|
|
|
"__metrics_path__": "/metrics",
|
|
|
|
"__scheme__": "http",
|
|
|
|
"job": "prometheus",
|
|
|
|
},
|
|
|
|
Labels: model.LabelSet{
|
|
|
|
"instance": "127.0.0.1:9090",
|
|
|
|
"job": "prometheus",
|
|
|
|
},
|
2018-04-11 17:38:10 +03:00
|
|
|
ScrapeURL: "http://127.0.0.1:9090",
|
2018-04-11 04:37:17 +03:00
|
|
|
LastError: "error while scraping target",
|
2018-04-11 05:16:08 +03:00
|
|
|
LastScrape: testTime.UTC(),
|
2018-04-11 17:38:10 +03:00
|
|
|
Health: HealthGood,
|
2018-04-10 17:21:25 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Dropped: []DroppedTarget{
|
|
|
|
{
|
|
|
|
DiscoveredLabels: model.LabelSet{
|
|
|
|
"__address__": "127.0.0.1:9100",
|
|
|
|
"__metrics_path__": "/metrics",
|
|
|
|
"__scheme__": "http",
|
|
|
|
"job": "node",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
do: doTargets(),
|
|
|
|
reqMethod: "GET",
|
|
|
|
reqPath: "/api/v1/targets",
|
|
|
|
inErr: fmt.Errorf("some error"),
|
|
|
|
err: fmt.Errorf("some error"),
|
|
|
|
},
|
2015-08-19 16:33:59 +03:00
|
|
|
}
|
|
|
|
|
2017-04-20 00:24:14 +03:00
|
|
|
var tests []apiTest
|
|
|
|
tests = append(tests, queryTests...)
|
|
|
|
|
2018-10-25 20:44:21 +03:00
|
|
|
for i, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
|
|
|
client.curTest = test
|
|
|
|
|
|
|
|
res, err := test.do()
|
|
|
|
|
|
|
|
if test.err != nil {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected error %q but got none", test.err)
|
|
|
|
}
|
|
|
|
if err.Error() != test.err.Error() {
|
|
|
|
t.Errorf("unexpected error: want %s, got %s", test.err, err)
|
|
|
|
}
|
|
|
|
if apiErr, ok := err.(*Error); ok {
|
|
|
|
if apiErr.Detail != test.inRes {
|
|
|
|
t.Errorf("%q should be %q", apiErr.Detail, test.inRes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2017-04-20 00:24:14 +03:00
|
|
|
}
|
2018-10-25 20:44:21 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
2017-04-20 00:24:14 +03:00
|
|
|
}
|
2015-08-19 16:33:59 +03:00
|
|
|
|
2018-10-25 20:44:21 +03:00
|
|
|
if !reflect.DeepEqual(res, test.res) {
|
|
|
|
t.Errorf("unexpected result: want %v, got %v", test.res, res)
|
|
|
|
}
|
|
|
|
})
|
2015-08-19 16:33:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type testClient struct {
|
|
|
|
*testing.T
|
|
|
|
|
|
|
|
ch chan apiClientTest
|
|
|
|
req *http.Request
|
|
|
|
}
|
|
|
|
|
|
|
|
type apiClientTest struct {
|
2018-10-25 20:44:21 +03:00
|
|
|
code int
|
|
|
|
response interface{}
|
|
|
|
expectedBody string
|
|
|
|
expectedErr *Error
|
2015-08-19 16:33:59 +03:00
|
|
|
}
|
|
|
|
|
2017-04-20 00:24:14 +03:00
|
|
|
func (c *testClient) URL(ep string, args map[string]string) *url.URL {
|
2015-08-19 16:33:59 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-20 00:24:14 +03:00
|
|
|
func (c *testClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, error) {
|
2015-08-19 16:33:59 +03:00
|
|
|
if ctx == nil {
|
|
|
|
c.Fatalf("context was not passed down")
|
|
|
|
}
|
|
|
|
if req != c.req {
|
|
|
|
c.Fatalf("request was not passed down")
|
|
|
|
}
|
|
|
|
|
|
|
|
test := <-c.ch
|
|
|
|
|
|
|
|
var b []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
switch v := test.response.(type) {
|
|
|
|
case string:
|
|
|
|
b = []byte(v)
|
|
|
|
default:
|
|
|
|
b, err = json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := &http.Response{
|
|
|
|
StatusCode: test.code,
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPIClientDo(t *testing.T) {
|
|
|
|
tests := []apiClientTest{
|
|
|
|
{
|
2018-10-25 20:44:21 +03:00
|
|
|
code: statusAPIError,
|
2015-08-19 16:33:59 +03:00
|
|
|
response: &apiResponse{
|
|
|
|
Status: "error",
|
|
|
|
Data: json.RawMessage(`null`),
|
|
|
|
ErrorType: ErrBadData,
|
|
|
|
Error: "failed",
|
|
|
|
},
|
2018-10-25 20:44:21 +03:00
|
|
|
expectedErr: &Error{
|
2015-08-19 16:33:59 +03:00
|
|
|
Type: ErrBadData,
|
|
|
|
Msg: "failed",
|
|
|
|
},
|
2018-10-25 20:44:21 +03:00
|
|
|
expectedBody: `null`,
|
2015-08-19 16:33:59 +03:00
|
|
|
},
|
|
|
|
{
|
2018-10-25 20:44:21 +03:00
|
|
|
code: statusAPIError,
|
2015-08-19 16:33:59 +03:00
|
|
|
response: &apiResponse{
|
|
|
|
Status: "error",
|
|
|
|
Data: json.RawMessage(`"test"`),
|
|
|
|
ErrorType: ErrTimeout,
|
|
|
|
Error: "timed out",
|
|
|
|
},
|
2018-10-25 20:44:21 +03:00
|
|
|
expectedErr: &Error{
|
2015-08-19 16:33:59 +03:00
|
|
|
Type: ErrTimeout,
|
|
|
|
Msg: "timed out",
|
|
|
|
},
|
2018-10-25 20:44:21 +03:00
|
|
|
expectedBody: `test`,
|
2015-08-19 16:33:59 +03:00
|
|
|
},
|
|
|
|
{
|
2018-10-25 20:44:21 +03:00
|
|
|
code: http.StatusInternalServerError,
|
|
|
|
response: "500 error details",
|
|
|
|
expectedErr: &Error{
|
|
|
|
Type: ErrServer,
|
|
|
|
Msg: "server error: 500",
|
|
|
|
Detail: "500 error details",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: http.StatusNotFound,
|
|
|
|
response: "404 error details",
|
|
|
|
expectedErr: &Error{
|
|
|
|
Type: ErrClient,
|
|
|
|
Msg: "client error: 404",
|
|
|
|
Detail: "404 error details",
|
2018-05-31 17:15:36 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-10-25 20:44:21 +03:00
|
|
|
code: http.StatusBadRequest,
|
2018-05-31 17:15:36 +03:00
|
|
|
response: &apiResponse{
|
|
|
|
Status: "error",
|
2018-06-01 19:52:44 +03:00
|
|
|
Data: json.RawMessage(`null`),
|
2018-05-31 17:15:36 +03:00
|
|
|
ErrorType: ErrBadData,
|
|
|
|
Error: "end timestamp must not be before start time",
|
|
|
|
},
|
2018-10-25 20:44:21 +03:00
|
|
|
expectedErr: &Error{
|
2018-05-31 17:15:36 +03:00
|
|
|
Type: ErrBadData,
|
|
|
|
Msg: "end timestamp must not be before start time",
|
2015-08-19 16:33:59 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-10-25 20:44:21 +03:00
|
|
|
code: statusAPIError,
|
2015-08-19 16:33:59 +03:00
|
|
|
response: "bad json",
|
2018-10-25 20:44:21 +03:00
|
|
|
expectedErr: &Error{
|
2015-08-19 16:33:59 +03:00
|
|
|
Type: ErrBadResponse,
|
|
|
|
Msg: "invalid character 'b' looking for beginning of value",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-10-25 20:44:21 +03:00
|
|
|
code: statusAPIError,
|
2015-08-19 16:33:59 +03:00
|
|
|
response: &apiResponse{
|
|
|
|
Status: "success",
|
|
|
|
Data: json.RawMessage(`"test"`),
|
|
|
|
},
|
2018-10-25 20:44:21 +03:00
|
|
|
expectedErr: &Error{
|
2015-08-19 16:33:59 +03:00
|
|
|
Type: ErrBadResponse,
|
|
|
|
Msg: "inconsistent body for response code",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-10-25 20:44:21 +03:00
|
|
|
code: statusAPIError,
|
2015-08-19 16:33:59 +03:00
|
|
|
response: &apiResponse{
|
|
|
|
Status: "success",
|
|
|
|
Data: json.RawMessage(`"test"`),
|
|
|
|
ErrorType: ErrTimeout,
|
|
|
|
Error: "timed out",
|
|
|
|
},
|
2018-10-25 20:44:21 +03:00
|
|
|
expectedErr: &Error{
|
2015-08-19 16:33:59 +03:00
|
|
|
Type: ErrBadResponse,
|
|
|
|
Msg: "inconsistent body for response code",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-10-25 20:44:21 +03:00
|
|
|
code: http.StatusOK,
|
2015-08-19 16:33:59 +03:00
|
|
|
response: &apiResponse{
|
|
|
|
Status: "error",
|
|
|
|
Data: json.RawMessage(`"test"`),
|
|
|
|
ErrorType: ErrTimeout,
|
|
|
|
Error: "timed out",
|
|
|
|
},
|
2018-10-25 20:44:21 +03:00
|
|
|
expectedErr: &Error{
|
2015-08-19 16:33:59 +03:00
|
|
|
Type: ErrBadResponse,
|
|
|
|
Msg: "inconsistent body for response code",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tc := &testClient{
|
|
|
|
T: t,
|
|
|
|
ch: make(chan apiClientTest, 1),
|
|
|
|
req: &http.Request{},
|
|
|
|
}
|
|
|
|
client := &apiClient{tc}
|
|
|
|
|
2018-10-25 20:44:21 +03:00
|
|
|
for i, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
|
|
|
|
|
|
|
tc.ch <- test
|
|
|
|
|
|
|
|
_, body, err := client.Do(context.Background(), tc.req)
|
|
|
|
|
|
|
|
if test.expectedErr != nil {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected error %q but got none", test.expectedErr)
|
|
|
|
}
|
|
|
|
if test.expectedErr.Error() != err.Error() {
|
|
|
|
t.Errorf("unexpected error: want %q, got %q", test.expectedErr, err)
|
|
|
|
}
|
|
|
|
if test.expectedErr.Detail != "" {
|
|
|
|
apiErr := err.(*Error)
|
|
|
|
if apiErr.Detail != test.expectedErr.Detail {
|
|
|
|
t.Errorf("unexpected error details: want %q, got %q", test.expectedErr.Detail, apiErr.Detail)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2015-08-19 16:33:59 +03:00
|
|
|
}
|
2018-10-25 20:44:21 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpeceted error %s", err)
|
2015-08-19 16:33:59 +03:00
|
|
|
}
|
|
|
|
|
2018-10-25 20:44:21 +03:00
|
|
|
want, got := test.expectedBody, string(body)
|
|
|
|
if want != got {
|
|
|
|
t.Errorf("unexpected body: want %q, got %q", want, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2015-08-19 16:33:59 +03:00
|
|
|
}
|
|
|
|
}
|