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

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