Improve test by asserting on whole Target Group object

Signed-off-by: Jean-Baptiste Le Duigou <jb.leduigou@gmail.com>
This commit is contained in:
Jean-Baptiste Le Duigou 2019-10-17 21:25:38 +02:00
parent 3309ffa482
commit 0939d566f3

View file

@ -25,31 +25,29 @@ import (
func TestTargetGroupStrictJsonUnmarshal(t *testing.T) { func TestTargetGroupStrictJsonUnmarshal(t *testing.T) {
tests := []struct { tests := []struct {
json string json string
expectedReply error expectedReply error
expectedTargets []model.LabelSet expectedGroup Group
}{ }{
{ {
json: ` {"labels": {},"targets": []}`, json: ` {"labels": {},"targets": []}`,
expectedReply: nil, expectedReply: nil,
expectedTargets: []model.LabelSet{}, expectedGroup: Group{Targets: []model.LabelSet{}, Labels: model.LabelSet{}},
}, },
{ {
json: ` {"labels": {},"targets": ["localhost:9090","localhost:9091"]}`, json: ` {"labels": {"my":"label"},"targets": ["localhost:9090","localhost:9091"]}`,
expectedReply: nil, expectedReply: nil,
expectedTargets: []model.LabelSet{ expectedGroup: Group{Targets: []model.LabelSet{
model.LabelSet{"__address__": "localhost:9090"}, model.LabelSet{"__address__": "localhost:9090"},
model.LabelSet{"__address__": "localhost:9091"}}, model.LabelSet{"__address__": "localhost:9091"}}, Labels: model.LabelSet{"my": "label"}},
}, },
{ {
json: ` {"label": {},"targets": []}`, json: ` {"label": {},"targets": []}`,
expectedReply: errors.New("json: unknown field \"label\""), expectedReply: errors.New("json: unknown field \"label\""),
expectedTargets: nil,
}, },
{ {
json: ` {"labels": {},"target": []}`, json: ` {"labels": {},"target": []}`,
expectedReply: errors.New("json: unknown field \"target\""), expectedReply: errors.New("json: unknown field \"target\""),
expectedTargets: nil,
}, },
} }
@ -57,7 +55,7 @@ func TestTargetGroupStrictJsonUnmarshal(t *testing.T) {
tg := Group{} tg := Group{}
actual := tg.UnmarshalJSON([]byte(test.json)) actual := tg.UnmarshalJSON([]byte(test.json))
testutil.Equals(t, test.expectedReply, actual) testutil.Equals(t, test.expectedReply, actual)
testutil.Equals(t, test.expectedTargets, tg.Targets) testutil.Equals(t, test.expectedGroup, tg)
} }
} }
@ -107,31 +105,28 @@ func TestTargetGroupYamlUnmarshal(t *testing.T) {
} }
} }
tests := []struct { tests := []struct {
yaml string yaml string
expectedTargets []model.LabelSet expectedGroup Group
expectedNumberOfLabels int expectedReply error
expectedReply error
}{ }{
{ {
// empty target group. // empty target group.
yaml: "labels:\ntargets:\n", yaml: "labels:\ntargets:\n",
expectedNumberOfLabels: 0, expectedGroup: Group{Targets: []model.LabelSet{}},
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']",
expectedNumberOfLabels: 1, expectedReply: nil,
expectedReply: nil, expectedGroup: Group{Targets: []model.LabelSet{
expectedTargets: []model.LabelSet{
model.LabelSet{"__address__": "localhost:9090"}, model.LabelSet{"__address__": "localhost:9090"},
model.LabelSet{"__address__": "localhost:9191"}}}, model.LabelSet{"__address__": "localhost:9191"}}, Labels: model.LabelSet{"my": "label"}},
},
{ {
// incorrect syntax. // incorrect syntax.
yaml: "labels:\ntargets:\n 'localhost:9090'", yaml: "labels:\ntargets:\n 'localhost:9090'",
expectedNumberOfLabels: 0, expectedReply: &yaml.TypeError{Errors: []string{"line 3: cannot unmarshal !!str `localho...` into []string"}},
expectedReply: &yaml.TypeError{Errors: []string{"line 3: cannot unmarshal !!str `localho...` into []string"}},
}, },
} }
@ -139,8 +134,7 @@ 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.expectedNumberOfLabels, len(tg.Labels)) testutil.Equals(t, test.expectedGroup, tg)
testutil.Equals(t, test.expectedTargets, tg.Targets)
} }
} }