mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-26 22:19:40 -08:00
Adding unit test for target group (#6141)
* adding unit test for target group Signed-off-by: Jean-Baptiste Le Duigou <jb.leduigou@gmail.com> * Improve unit tests for target group Signed-off-by: Jean-Baptiste Le Duigou <jb.leduigou@gmail.com> * Fix imports Signed-off-by: Jean-Baptiste Le Duigou <jb.leduigou@gmail.com> * 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:
commit
3acc3e856c
|
@ -17,18 +17,29 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/prometheus/prometheus/util/testutil"
|
"github.com/prometheus/common/model"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
|
"github.com/prometheus/prometheus/util/testutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTargetGroupStrictJsonUnmarshal(t *testing.T) {
|
func TestTargetGroupStrictJsonUnmarshal(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
json string
|
json string
|
||||||
expectedReply error
|
expectedReply error
|
||||||
|
expectedGroup Group
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
json: ` {"labels": {},"targets": []}`,
|
json: ` {"labels": {},"targets": []}`,
|
||||||
expectedReply: nil,
|
expectedReply: nil,
|
||||||
|
expectedGroup: Group{Targets: []model.LabelSet{}, Labels: model.LabelSet{}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
json: ` {"labels": {"my":"label"},"targets": ["localhost:9090","localhost:9091"]}`,
|
||||||
|
expectedReply: nil,
|
||||||
|
expectedGroup: Group{Targets: []model.LabelSet{
|
||||||
|
model.LabelSet{"__address__": "localhost:9090"},
|
||||||
|
model.LabelSet{"__address__": "localhost:9091"}}, Labels: model.LabelSet{"my": "label"}},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
json: ` {"label": {},"targets": []}`,
|
json: ` {"label": {},"targets": []}`,
|
||||||
|
@ -39,15 +50,54 @@ func TestTargetGroupStrictJsonUnmarshal(t *testing.T) {
|
||||||
expectedReply: errors.New("json: unknown field \"target\""),
|
expectedReply: errors.New("json: unknown field \"target\""),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
tg := Group{}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
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.expectedGroup, tg)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTargetGroupYamlMarshal(t *testing.T) {
|
||||||
|
marshal := func(g interface{}) []byte {
|
||||||
|
d, err := yaml.Marshal(g)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
expectedYaml string
|
||||||
|
expectetedErr error
|
||||||
|
group Group
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
// labels should be omitted if empty.
|
||||||
|
group: Group{},
|
||||||
|
expectedYaml: "targets: []\n",
|
||||||
|
expectetedErr: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// targets only exposes addresses.
|
||||||
|
group: Group{Targets: []model.LabelSet{
|
||||||
|
model.LabelSet{"__address__": "localhost:9090"},
|
||||||
|
model.LabelSet{"__address__": "localhost:9091"}},
|
||||||
|
Labels: model.LabelSet{"foo": "bar", "bar": "baz"}},
|
||||||
|
expectedYaml: "targets:\n- localhost:9090\n- localhost:9091\nlabels:\n bar: baz\n foo: bar\n",
|
||||||
|
expectetedErr: nil,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
actual, err := test.group.MarshalYAML()
|
||||||
|
testutil.Equals(t, test.expectetedErr, err)
|
||||||
|
testutil.Equals(t, test.expectedYaml, string(marshal(actual)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestTargetGroupYamlUnmarshal(t *testing.T) {
|
func TestTargetGroupYamlUnmarshal(t *testing.T) {
|
||||||
unmarshal := func(d []byte) func(interface{}) error {
|
unmarshal := func(d []byte) func(interface{}) error {
|
||||||
return func(o interface{}) error {
|
return func(o interface{}) error {
|
||||||
|
@ -56,26 +106,26 @@ func TestTargetGroupYamlUnmarshal(t *testing.T) {
|
||||||
}
|
}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
yaml string
|
yaml string
|
||||||
expectedNumberOfTargets int
|
expectedGroup Group
|
||||||
expectedNumberOfLabels int
|
|
||||||
expectedReply error
|
expectedReply error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
|
// empty target group.
|
||||||
yaml: "labels:\ntargets:\n",
|
yaml: "labels:\ntargets:\n",
|
||||||
expectedNumberOfTargets: 0,
|
expectedGroup: Group{Targets: []model.LabelSet{}},
|
||||||
expectedNumberOfLabels: 0,
|
|
||||||
expectedReply: nil,
|
expectedReply: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
// 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,
|
|
||||||
expectedReply: nil,
|
expectedReply: nil,
|
||||||
|
expectedGroup: Group{Targets: []model.LabelSet{
|
||||||
|
model.LabelSet{"__address__": "localhost:9090"},
|
||||||
|
model.LabelSet{"__address__": "localhost:9191"}}, Labels: model.LabelSet{"my": "label"}},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
// incorrect syntax.
|
||||||
yaml: "labels:\ntargets:\n 'localhost:9090'",
|
yaml: "labels:\ntargets:\n 'localhost:9090'",
|
||||||
expectedNumberOfTargets: 0,
|
|
||||||
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"}},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -84,8 +134,24 @@ 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.expectedGroup, tg)
|
||||||
testutil.Equals(t, test.expectedNumberOfLabels, len(tg.Labels))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestString(t *testing.T) {
|
||||||
|
// String() should return only the source, regardless of other attributes.
|
||||||
|
group1 :=
|
||||||
|
Group{Targets: []model.LabelSet{
|
||||||
|
model.LabelSet{"__address__": "localhost:9090"},
|
||||||
|
model.LabelSet{"__address__": "localhost:9091"}},
|
||||||
|
Source: "<source>",
|
||||||
|
Labels: model.LabelSet{"foo": "bar", "bar": "baz"}}
|
||||||
|
group2 :=
|
||||||
|
Group{Targets: []model.LabelSet{},
|
||||||
|
Source: "<source>",
|
||||||
|
Labels: model.LabelSet{}}
|
||||||
|
testutil.Equals(t, "<source>", group1.String())
|
||||||
|
testutil.Equals(t, "<source>", group2.String())
|
||||||
|
testutil.Equals(t, group1.String(), group2.String())
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue