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-15 21:24:02 +02:00
parent 15de05d55e
commit 1f9eb09e8e
4 changed files with 91 additions and 9 deletions

View file

@ -3,4 +3,6 @@ Maintainers of this repository with their focus areas:
* Brian Brazil <brian.brazil@robustperception.io> @brian-brazil: Console templates; semantics of PromQL, service discovery, and relabeling.
* Fabian Reinartz <freinartz@google.com> @fabxc: PromQL parsing and evaluation; implementation of retrieval, alert notification, and service discovery.
* Julius Volz <julius.volz@gmail.com> @juliusv: Remote storage integrations; web UI.
* Krasi Georgiev <kgeorgie@redhat.com> @krasi-georgiev: TSDB - the storage engine.
* Ganesh Vernekar <cs15btech11018@iith.ac.in> @codesome: TSDB - the storage engine.

View file

@ -21,6 +21,9 @@ TSDB_BENCHMARK_NUM_METRICS ?= 1000
TSDB_BENCHMARK_DATASET ?= "$(TSDB_PROJECT_DIR)/testdata/20kseries.json"
TSDB_BENCHMARK_OUTPUT_DIR ?= "$(TSDB_CLI_DIR)/benchout"
.PHONY: all
all: common-all check_assets
include Makefile.common
DOCKER_IMAGE_NAME ?= prometheus
@ -39,9 +42,11 @@ check_assets: assets
exit 1; \
fi
.PHONY: build_tsdb
build_tsdb:
GO111MODULE=$(GO111MODULE) $(GO) build -o $(TSDB_BIN) $(TSDB_CLI_DIR)
.PHONY: bench_tsdb
bench_tsdb: build_tsdb
@echo ">> running benchmark, writing result to $(TSDB_BENCHMARK_OUTPUT_DIR)"
@$(TSDB_BIN) bench write --metrics=$(TSDB_BENCHMARK_NUM_METRICS) --out=$(TSDB_BENCHMARK_OUTPUT_DIR) $(TSDB_BENCHMARK_DATASET)

View file

@ -17,37 +17,88 @@ import (
"errors"
"testing"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/util/testutil"
"gopkg.in/yaml.v2"
)
func TestTargetGroupStrictJsonUnmarshal(t *testing.T) {
tests := []struct {
json string
expectedReply error
json string
expectedReply error
expectedTargets []model.LabelSet
}{
{
json: ` {"labels": {},"targets": []}`,
expectedReply: nil,
expectedTargets: []model.LabelSet{},
},
{
json: ` {"labels": {},"targets": ["localhost:9090","localhost:9091"]}`,
expectedReply: nil,
expectedTargets: []model.LabelSet{
model.LabelSet{"__address__": "localhost:9090"},
model.LabelSet{"__address__": "localhost:9091"}},
},
{
json: ` {"label": {},"targets": []}`,
expectedReply: errors.New("json: unknown field \"label\""),
expectedReply: errors.New("json: unknown field \"label\""),
expectedTargets: nil,
},
{
json: ` {"labels": {},"target": []}`,
expectedReply: errors.New("json: unknown field \"target\""),
expectedReply: errors.New("json: unknown field \"target\""),
expectedTargets: nil,
},
}
tg := Group{}
for _, test := range tests {
tg := Group{}
actual := tg.UnmarshalJSON([]byte(test.json))
testutil.Equals(t, test.expectedReply, actual)
testutil.Equals(t, test.expectedTargets, tg.Targets)
}
}
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) {
unmarshal := func(d []byte) func(interface{}) error {
return func(o interface{}) error {
@ -61,18 +112,28 @@ func TestTargetGroupYamlUnmarshal(t *testing.T) {
expectedReply error
}{
{
//empty targe group
yaml: "labels:\ntargets:\n",
expectedNumberOfTargets: 0,
expectedNumberOfLabels: 0,
expectedReply: nil,
},
{
//brackets syntax
yaml: "labels:\n my: label\ntargets:\n ['localhost:9090', 'localhost:9191']",
expectedNumberOfTargets: 2,
expectedNumberOfLabels: 1,
expectedReply: nil,
},
{
//hyphen syntax
yaml: "targets:\n- localhost:9090\n- localhost:9091\nlabels:\n bar: baz\n foo: bar\n",
expectedNumberOfTargets: 2,
expectedNumberOfLabels: 2,
expectedReply: nil,
},
{
//incorrect syntax
yaml: "labels:\ntargets:\n 'localhost:9090'",
expectedNumberOfTargets: 0,
expectedNumberOfLabels: 0,
@ -89,3 +150,21 @@ func TestTargetGroupYamlUnmarshal(t *testing.T) {
}
}
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())
}

View file

@ -1,4 +0,0 @@
Maintainers of this repository:
* Krasi Georgiev <kgeorgie@redhat.com> @krasi-georgiev
* Goutham Veeramachaneni <gouthamve@gmail.com> @gouthamve