Skip to content

Commit

Permalink
Rewrote tests for relabel and template (prometheus#3754)
Browse files Browse the repository at this point in the history
* relabel: use testutil for testing

* template: use testutil for testing
  • Loading branch information
elifkus authored and brian-brazil committed Mar 29, 2018
1 parent 184b6e3 commit daebf68
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
9 changes: 3 additions & 6 deletions relabel/relabel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
package relabel

import (
"reflect"
"testing"

"github.com/prometheus/common/model"

"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/util/testutil"
)

func TestRelabel(t *testing.T) {
Expand Down Expand Up @@ -412,11 +412,8 @@ func TestRelabel(t *testing.T) {
},
}

for i, test := range tests {
for _, test := range tests {
res := Process(test.input, test.relabel...)

if !reflect.DeepEqual(res, test.output) {
t.Errorf("Test %d: relabel output mismatch: expected %#v, got %#v", i+1, test.output, res)
}
testutil.Equals(t, res, test.output)
}
}
22 changes: 8 additions & 14 deletions template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/util/testutil"
)

type testTemplatesScenario struct {
Expand Down Expand Up @@ -256,7 +257,7 @@ func TestTemplateExpansion(t *testing.T) {
panic(err)
}

for i, s := range scenarios {
for _, s := range scenarios {
queryFunc := func(_ context.Context, _ string, _ time.Time) (promql.Vector, error) {
return s.queryResult, nil
}
Expand All @@ -269,21 +270,14 @@ func TestTemplateExpansion(t *testing.T) {
result, err = expander.Expand()
}
if s.shouldFail {
if err == nil {
t.Fatalf("%d. Error not returned from %v", i, s.text)
}
if err.Error() != s.errorMsg {
t.Fatalf("%d. Error message returned is wrong:\n returned: %v\n expected: %v", i, err.Error(), s.errorMsg)
}
testutil.NotOk(t, err, "%v", s.text)
continue
}
if err != nil {
t.Fatalf("%d. Error returned from %v: %v", i, s.text, err)
continue
}
if result != s.output {
t.Fatalf("%d. Error in result from %v: Expected '%v' Got '%v'", i, s.text, s.output, result)
continue

testutil.Ok(t, err)

if err == nil {
testutil.Equals(t, result, s.output)
}
}
}

0 comments on commit daebf68

Please sign in to comment.