Improve unit tests for target group

Signed-off-by: Jean-Baptiste Le Duigou <jb.leduigou@gmail.com>
This commit is contained in:
Jean-Baptiste Le Duigou 2019-10-16 18:03:05 +02:00
commit 9372a224b5

View file

@ -17,9 +17,10 @@ import (
"errors" "errors"
"testing" "testing"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/util/testutil"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/util/testutil"
) )
func TestTargetGroupStrictJsonUnmarshal(t *testing.T) { func TestTargetGroupStrictJsonUnmarshal(t *testing.T) {
@ -76,13 +77,13 @@ func TestTargetGroupYamlMarshal(t *testing.T) {
group Group group Group
}{ }{
{ {
//labels should be omitted if empty // labels should be omitted if empty.
group: Group{}, group: Group{},
expectedYaml: "targets: []\n", expectedYaml: "targets: []\n",
expectetedErr: nil, expectetedErr: nil,
}, },
{ {
//targets only exposes addresses // targets only exposes addresses.
group: Group{Targets: []model.LabelSet{ group: Group{Targets: []model.LabelSet{
model.LabelSet{"__address__": "localhost:9090"}, model.LabelSet{"__address__": "localhost:9090"},
model.LabelSet{"__address__": "localhost:9091"}}, model.LabelSet{"__address__": "localhost:9091"}},
@ -106,38 +107,31 @@ func TestTargetGroupYamlUnmarshal(t *testing.T) {
} }
} }
tests := []struct { tests := []struct {
yaml string yaml string
expectedNumberOfTargets int expectedTargets []model.LabelSet
expectedNumberOfLabels int expectedNumberOfLabels int
expectedReply error expectedReply error
}{ }{
{ {
//empty targe group // empty target group.
yaml: "labels:\ntargets:\n", yaml: "labels:\ntargets:\n",
expectedNumberOfTargets: 0, expectedNumberOfLabels: 0,
expectedNumberOfLabels: 0, expectedTargets: []model.LabelSet{},
expectedReply: nil, expectedReply: nil,
}, },
{ {
//brackets syntax // brackets syntax.
yaml: "labels:\n my: label\ntargets:\n ['localhost:9090', 'localhost:9191']", yaml: "labels:\n my: label\ntargets:\n ['localhost:9090', 'localhost:9191']",
expectedNumberOfTargets: 2, expectedNumberOfLabels: 1,
expectedNumberOfLabels: 1, expectedReply: nil,
expectedReply: nil, expectedTargets: []model.LabelSet{
}, model.LabelSet{"__address__": "localhost:9090"},
model.LabelSet{"__address__": "localhost:9191"}}},
{ {
//hyphen syntax // incorrect syntax.
yaml: "targets:\n- localhost:9090\n- localhost:9091\nlabels:\n bar: baz\n foo: bar\n", yaml: "labels:\ntargets:\n 'localhost:9090'",
expectedNumberOfTargets: 2, expectedNumberOfLabels: 0,
expectedNumberOfLabels: 2, expectedReply: &yaml.TypeError{Errors: []string{"line 3: cannot unmarshal !!str `localho...` into []string"}},
expectedReply: nil,
},
{
//incorrect syntax
yaml: "labels:\ntargets:\n 'localhost:9090'",
expectedNumberOfTargets: 0,
expectedNumberOfLabels: 0,
expectedReply: &yaml.TypeError{Errors: []string{"line 3: cannot unmarshal !!str `localho...` into []string"}},
}, },
} }
@ -145,14 +139,14 @@ func TestTargetGroupYamlUnmarshal(t *testing.T) {
tg := Group{} tg := Group{}
actual := tg.UnmarshalYAML(unmarshal([]byte(test.yaml))) actual := tg.UnmarshalYAML(unmarshal([]byte(test.yaml)))
testutil.Equals(t, test.expectedReply, actual) testutil.Equals(t, test.expectedReply, actual)
testutil.Equals(t, test.expectedNumberOfTargets, len(tg.Targets))
testutil.Equals(t, test.expectedNumberOfLabels, len(tg.Labels)) testutil.Equals(t, test.expectedNumberOfLabels, len(tg.Labels))
testutil.Equals(t, test.expectedTargets, tg.Targets)
} }
} }
func TestString(t *testing.T) { func TestString(t *testing.T) {
//String() should return only the source, regardless of other attributes // String() should return only the source, regardless of other attributes.
group1 := group1 :=
Group{Targets: []model.LabelSet{ Group{Targets: []model.LabelSet{
model.LabelSet{"__address__": "localhost:9090"}, model.LabelSet{"__address__": "localhost:9090"},
@ -166,5 +160,4 @@ func TestString(t *testing.T) {
testutil.Equals(t, "<source>", group1.String()) testutil.Equals(t, "<source>", group1.String())
testutil.Equals(t, "<source>", group2.String()) testutil.Equals(t, "<source>", group2.String())
testutil.Equals(t, group1.String(), group2.String()) testutil.Equals(t, group1.String(), group2.String())
} }