mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-25 13:44:05 -08:00
91d76c8023
* spelling: alertmanager Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: attributes Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: autocomplete Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: bootstrap Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: caught Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: chunkenc Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: compaction Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: corrupted Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: deletable Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: expected Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: fine-grained Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: initialized Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: iteration Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: javascript Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: multiple Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: number Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: overlapping Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: possible Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: postings Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: procedure Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: programmatic Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: queuing Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: querier Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: repairing Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: received Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: reproducible Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: retention Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: sample Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: segements Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: semantic Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: software [LICENSE] Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: staging Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: timestamp Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: unfortunately Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: uvarint Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: subsequently Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> * spelling: ressamples Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
158 lines
4.4 KiB
Go
158 lines
4.4 KiB
Go
// Copyright 2018 The Prometheus Authors
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package targetgroup
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/prometheus/common/model"
|
|
"gopkg.in/yaml.v2"
|
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
|
)
|
|
|
|
func TestTargetGroupStrictJsonUnmarshal(t *testing.T) {
|
|
tests := []struct {
|
|
json string
|
|
expectedReply error
|
|
expectedGroup Group
|
|
}{
|
|
{
|
|
json: ` {"labels": {},"targets": []}`,
|
|
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": []}`,
|
|
expectedReply: errors.New("json: unknown field \"label\""),
|
|
},
|
|
{
|
|
json: ` {"labels": {},"target": []}`,
|
|
expectedReply: errors.New("json: unknown field \"target\""),
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
tg := Group{}
|
|
actual := tg.UnmarshalJSON([]byte(test.json))
|
|
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
|
|
expectedErr error
|
|
group Group
|
|
}{
|
|
{
|
|
// labels should be omitted if empty.
|
|
group: Group{},
|
|
expectedYaml: "targets: []\n",
|
|
expectedErr: 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",
|
|
expectedErr: nil,
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
actual, err := test.group.MarshalYAML()
|
|
testutil.Equals(t, test.expectedErr, err)
|
|
testutil.Equals(t, test.expectedYaml, string(marshal(actual)))
|
|
}
|
|
}
|
|
|
|
func TestTargetGroupYamlUnmarshal(t *testing.T) {
|
|
unmarshal := func(d []byte) func(interface{}) error {
|
|
return func(o interface{}) error {
|
|
return yaml.Unmarshal(d, o)
|
|
}
|
|
}
|
|
tests := []struct {
|
|
yaml string
|
|
expectedGroup Group
|
|
expectedReply error
|
|
}{
|
|
{
|
|
// empty target group.
|
|
yaml: "labels:\ntargets:\n",
|
|
expectedGroup: Group{Targets: []model.LabelSet{}},
|
|
expectedReply: nil,
|
|
},
|
|
{
|
|
// brackets syntax.
|
|
yaml: "labels:\n my: label\ntargets:\n ['localhost:9090', 'localhost:9191']",
|
|
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'",
|
|
expectedReply: &yaml.TypeError{Errors: []string{"line 3: cannot unmarshal !!str `localho...` into []string"}},
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
tg := Group{}
|
|
actual := tg.UnmarshalYAML(unmarshal([]byte(test.yaml)))
|
|
testutil.Equals(t, test.expectedReply, actual)
|
|
testutil.Equals(t, test.expectedGroup, tg)
|
|
}
|
|
|
|
}
|
|
|
|
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())
|
|
}
|