Rewrote tests with testutil for several test files (#4086)

* promql: Rewrote tests with testutil for functions_test

Signed-off-by: Elif T. Kuş <elifkus@gmail.com>

* pkg/relabel: Rewrote tests with testutil for relabel_test

Signed-off-by: Elif T. Kuş <elifkus@gmail.com>

* discovery/consul: Rewrote tests with testutil for consul_test

Signed-off-by: Elif T. Kuş <elifkus@gmail.com>

* scrape: Rewrote tests with testutil for manager_test

Signed-off-by: Elif T. Kuş <elifkus@gmail.com>
This commit is contained in:
Elif T. Kuş 2018-04-27 15:11:16 +03:00 committed by Brian Brazil
parent d79d573f71
commit 57dcdfb15f
4 changed files with 29 additions and 49 deletions

View file

@ -14,18 +14,18 @@
package consul package consul
import ( import (
"context"
"testing" "testing"
"time"
"github.com/stretchr/testify/require"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"context"
"github.com/go-kit/kit/log" "github.com/go-kit/kit/log"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/discovery/targetgroup"
"time" "github.com/prometheus/prometheus/util/testutil"
) )
func TestConfiguredService(t *testing.T) { func TestConfiguredService(t *testing.T) {
@ -127,11 +127,11 @@ func newServer(t *testing.T) (*httptest.Server, *SDConfig) {
w.Header().Add("X-Consul-Index", "1") w.Header().Add("X-Consul-Index", "1")
w.Write([]byte(response)) w.Write([]byte(response))
})) }))
url, err := url.Parse(stub.URL) stuburl, err := url.Parse(stub.URL)
require.NoError(t, err) testutil.Ok(t, err)
config := &SDConfig{ config := &SDConfig{
Server: url.Host, Server: stuburl.Host,
Token: "fake-token", Token: "fake-token",
RefreshInterval: model.Duration(1 * time.Second), RefreshInterval: model.Duration(1 * time.Second),
} }
@ -141,18 +141,18 @@ func newServer(t *testing.T) (*httptest.Server, *SDConfig) {
func newDiscovery(t *testing.T, config *SDConfig) *Discovery { func newDiscovery(t *testing.T, config *SDConfig) *Discovery {
logger := log.NewNopLogger() logger := log.NewNopLogger()
d, err := NewDiscovery(config, logger) d, err := NewDiscovery(config, logger)
require.NoError(t, err) testutil.Ok(t, err)
return d return d
} }
func checkOneTarget(t *testing.T, tg []*targetgroup.Group) { func checkOneTarget(t *testing.T, tg []*targetgroup.Group) {
require.Equal(t, 1, len(tg)) testutil.Equals(t, 1, len(tg))
target := tg[0] target := tg[0]
require.Equal(t, "test-dc", string(target.Labels["__meta_consul_dc"])) testutil.Equals(t, "test-dc", string(target.Labels["__meta_consul_dc"]))
require.Equal(t, target.Source, string(target.Labels["__meta_consul_service"])) testutil.Equals(t, target.Source, string(target.Labels["__meta_consul_service"]))
if target.Source == "test" { if target.Source == "test" {
// test service should have one node. // test service should have one node.
require.NotEmpty(t, target.Targets) testutil.Assert(t, len(target.Targets) > 0, "Test service should have one node")
} }
} }

View file

@ -14,13 +14,13 @@
package relabel package relabel
import ( import (
"reflect"
"testing" "testing"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/util/testutil"
) )
func TestRelabel(t *testing.T) { func TestRelabel(t *testing.T) {
@ -413,11 +413,8 @@ func TestRelabel(t *testing.T) {
}, },
} }
for i, test := range tests { for _, test := range tests {
res := Process(test.input, test.relabel...) res := Process(test.input, test.relabel...)
testutil.Equals(t, test.output, res)
if !reflect.DeepEqual(res, test.output) {
t.Errorf("Test %d: relabel output mismatch: expected %#v, got %#v", i+1, test.output, res)
}
} }
} }

View file

@ -89,31 +89,22 @@ func TestDeriv(t *testing.T) {
engine := NewEngine(nil, nil, 10, 10*time.Second) engine := NewEngine(nil, nil, 10, 10*time.Second)
a, err := storage.Appender() a, err := storage.Appender()
if err != nil { testutil.Ok(t, err)
t.Fatal(err)
}
metric := labels.FromStrings("__name__", "foo") metric := labels.FromStrings("__name__", "foo")
a.Add(metric, 1493712816939, 1.0) a.Add(metric, 1493712816939, 1.0)
a.Add(metric, 1493712846939, 1.0) a.Add(metric, 1493712846939, 1.0)
if err := a.Commit(); err != nil { err = a.Commit()
t.Fatal(err) testutil.Ok(t, err)
}
query, err := engine.NewInstantQuery(storage, "deriv(foo[30m])", timestamp.Time(1493712846939)) query, err := engine.NewInstantQuery(storage, "deriv(foo[30m])", timestamp.Time(1493712846939))
if err != nil { testutil.Ok(t, err)
t.Fatalf("Error parsing query: %s", err)
}
result := query.Exec(context.Background()) result := query.Exec(context.Background())
if result.Err != nil { testutil.Ok(t, result.Err)
t.Fatalf("Error running query: %s", result.Err)
}
vec, _ := result.Vector() vec, _ := result.Vector()
if len(vec) != 1 { testutil.Assert(t, len(vec) == 1, "Expected 1 result, got %d", len(vec))
t.Fatalf("Expected 1 result, got %d", len(vec)) testutil.Assert(t, vec[0].V == 0.0, "Expected 0.0 as value, got %f", vec[0].V)
}
if vec[0].V != 0.0 {
t.Fatalf("Expected 0.0 as value, got %f", vec[0].V)
}
} }

View file

@ -15,7 +15,6 @@ package scrape
import ( import (
"fmt" "fmt"
"reflect"
"testing" "testing"
"time" "time"
@ -23,6 +22,7 @@ import (
"github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/util/testutil"
) )
func mustNewRegexp(s string) config.Regexp { func mustNewRegexp(s string) config.Regexp {
@ -214,22 +214,14 @@ func TestPopulateLabels(t *testing.T) {
err: fmt.Errorf("invalid label value for \"custom\": \"\\xbd\""), err: fmt.Errorf("invalid label value for \"custom\": \"\\xbd\""),
}, },
} }
for i, c := range cases { for _, c := range cases {
in := c.in.Copy() in := c.in.Copy()
res, orig, err := populateLabels(c.in, c.cfg) res, orig, err := populateLabels(c.in, c.cfg)
if !reflect.DeepEqual(err, c.err) { testutil.Equals(t, c.err, err)
t.Fatalf("case %d: wanted %v error, got %v", i, c.err, err) testutil.Equals(t, c.in, in)
} testutil.Equals(t, c.res, res)
if !reflect.DeepEqual(c.in, in) { testutil.Equals(t, c.resOrig, orig)
t.Errorf("case %d: input lset was changed was\n\t%+v\n now\n\t%+v", i, in, c.in)
}
if !reflect.DeepEqual(res, c.res) {
t.Errorf("case %d: expected res\n\t%+v\n got\n\t%+v", i, c.res, res)
}
if !reflect.DeepEqual(orig, c.resOrig) {
t.Errorf("case %d: expected resOrig\n\t%+v\n got\n\t%+v", i, c.resOrig, orig)
}
} }
} }