From 15de05d55ee3c2a8debf0cc0981322f5b04ae445 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Le Duigou Date: Mon, 14 Oct 2019 21:00:11 +0200 Subject: [PATCH 1/4] adding unit test for target group Signed-off-by: Jean-Baptiste Le Duigou --- discovery/targetgroup/targetgroup_test.go | 43 +++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/discovery/targetgroup/targetgroup_test.go b/discovery/targetgroup/targetgroup_test.go index 0087b272fd..ceb6bf9b94 100644 --- a/discovery/targetgroup/targetgroup_test.go +++ b/discovery/targetgroup/targetgroup_test.go @@ -18,6 +18,7 @@ import ( "testing" "github.com/prometheus/prometheus/util/testutil" + "gopkg.in/yaml.v2" ) func TestTargetGroupStrictJsonUnmarshal(t *testing.T) { @@ -46,3 +47,45 @@ func TestTargetGroupStrictJsonUnmarshal(t *testing.T) { } } + +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 + expectedNumberOfTargets int + expectedNumberOfLabels int + expectedReply error + }{ + { + yaml: "labels:\ntargets:\n", + expectedNumberOfTargets: 0, + expectedNumberOfLabels: 0, + expectedReply: nil, + }, + { + yaml: "labels:\n my: label\ntargets:\n ['localhost:9090', 'localhost:9191']", + expectedNumberOfTargets: 2, + expectedNumberOfLabels: 1, + expectedReply: nil, + }, + { + yaml: "labels:\ntargets:\n 'localhost:9090'", + expectedNumberOfTargets: 0, + expectedNumberOfLabels: 0, + 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.expectedNumberOfTargets, len(tg.Targets)) + testutil.Equals(t, test.expectedNumberOfLabels, len(tg.Labels)) + } + +} From 1f9eb09e8e8f5d7e4dc96adc7ac344c998d4bb63 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Le Duigou Date: Tue, 15 Oct 2019 21:24:02 +0200 Subject: [PATCH 2/4] Improve unit tests for target group Signed-off-by: Jean-Baptiste Le Duigou --- MAINTAINERS.md | 2 + Makefile | 5 ++ discovery/targetgroup/targetgroup_test.go | 89 +++++++++++++++++++++-- tsdb/MAINTAINERS.md | 4 - 4 files changed, 91 insertions(+), 9 deletions(-) delete mode 100644 tsdb/MAINTAINERS.md diff --git a/MAINTAINERS.md b/MAINTAINERS.md index e00da702c0..a61d396a62 100644 --- a/MAINTAINERS.md +++ b/MAINTAINERS.md @@ -3,4 +3,6 @@ Maintainers of this repository with their focus areas: * Brian Brazil @brian-brazil: Console templates; semantics of PromQL, service discovery, and relabeling. * Fabian Reinartz @fabxc: PromQL parsing and evaluation; implementation of retrieval, alert notification, and service discovery. * Julius Volz @juliusv: Remote storage integrations; web UI. +* Krasi Georgiev @krasi-georgiev: TSDB - the storage engine. +* Ganesh Vernekar @codesome: TSDB - the storage engine. diff --git a/Makefile b/Makefile index 11d4fa508e..b029fa67a8 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/discovery/targetgroup/targetgroup_test.go b/discovery/targetgroup/targetgroup_test.go index ceb6bf9b94..18b4efe141 100644 --- a/discovery/targetgroup/targetgroup_test.go +++ b/discovery/targetgroup/targetgroup_test.go @@ -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: "", + Labels: model.LabelSet{"foo": "bar", "bar": "baz"}} + group2 := + Group{Targets: []model.LabelSet{}, + Source: "", + Labels: model.LabelSet{}} + testutil.Equals(t, "", group1.String()) + testutil.Equals(t, "", group2.String()) + testutil.Equals(t, group1.String(), group2.String()) + +} diff --git a/tsdb/MAINTAINERS.md b/tsdb/MAINTAINERS.md deleted file mode 100644 index dcb57a80df..0000000000 --- a/tsdb/MAINTAINERS.md +++ /dev/null @@ -1,4 +0,0 @@ -Maintainers of this repository: - -* Krasi Georgiev @krasi-georgiev -* Goutham Veeramachaneni @gouthamve \ No newline at end of file From 3309ffa482d9dc2c2230baaf545dbf36917213a0 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Le Duigou Date: Thu, 17 Oct 2019 21:12:39 +0200 Subject: [PATCH 3/4] Fix imports Signed-off-by: Jean-Baptiste Le Duigou --- discovery/targetgroup/targetgroup_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/discovery/targetgroup/targetgroup_test.go b/discovery/targetgroup/targetgroup_test.go index 482c2693b3..102b7f66e4 100644 --- a/discovery/targetgroup/targetgroup_test.go +++ b/discovery/targetgroup/targetgroup_test.go @@ -17,9 +17,9 @@ import ( "errors" "testing" - "gopkg.in/yaml.v2" "github.com/prometheus/common/model" - + "gopkg.in/yaml.v2" + "github.com/prometheus/prometheus/util/testutil" ) From 0939d566f320be41041f365c1f37eb3950fe6605 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Le Duigou Date: Thu, 17 Oct 2019 21:25:38 +0200 Subject: [PATCH 4/4] Improve test by asserting on whole Target Group object Signed-off-by: Jean-Baptiste Le Duigou --- discovery/targetgroup/targetgroup_test.go | 56 ++++++++++------------- 1 file changed, 25 insertions(+), 31 deletions(-) diff --git a/discovery/targetgroup/targetgroup_test.go b/discovery/targetgroup/targetgroup_test.go index 102b7f66e4..c4a588e0ff 100644 --- a/discovery/targetgroup/targetgroup_test.go +++ b/discovery/targetgroup/targetgroup_test.go @@ -25,31 +25,29 @@ import ( func TestTargetGroupStrictJsonUnmarshal(t *testing.T) { tests := []struct { - json string - expectedReply error - expectedTargets []model.LabelSet + json string + expectedReply error + expectedGroup Group }{ { json: ` {"labels": {},"targets": []}`, - expectedReply: nil, - expectedTargets: []model.LabelSet{}, + expectedReply: nil, + 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, + expectedReply: errors.New("json: unknown field \"label\""), }, { json: ` {"labels": {},"target": []}`, - expectedReply: errors.New("json: unknown field \"target\""), - expectedTargets: nil, + expectedReply: errors.New("json: unknown field \"target\""), }, } @@ -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) } } @@ -107,31 +105,28 @@ func TestTargetGroupYamlUnmarshal(t *testing.T) { } } tests := []struct { - yaml string - expectedTargets []model.LabelSet - expectedNumberOfLabels int - expectedReply error + yaml string + expectedGroup Group + expectedReply error }{ { // empty target group. - yaml: "labels:\ntargets:\n", - expectedNumberOfLabels: 0, - expectedTargets: []model.LabelSet{}, - expectedReply: nil, + yaml: "labels:\ntargets:\n", + 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{ + 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"}}}, + 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"}}, + yaml: "labels:\ntargets:\n 'localhost:9090'", + 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) } }