From cc21b315024c6f6c771c6ad8d767a5ad36704e61 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 15 Oct 2019 21:18:08 +0300 Subject: [PATCH] app/victoria-metrics/test: add a test for PopulateTimeTplString --- app/victoria-metrics/test/parser.go | 5 ++-- app/victoria-metrics/test/parser_test.go | 24 +++++++++++++++++++ .../graphite/comparison-not-inf-not-nan.json | 12 ++++++---- .../graphite/not-nan-as-missing-data.json | 12 ++++++---- 4 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 app/victoria-metrics/test/parser_test.go diff --git a/app/victoria-metrics/test/parser.go b/app/victoria-metrics/test/parser.go index 1cecc29f3..953fb6af0 100644 --- a/app/victoria-metrics/test/parser.go +++ b/app/victoria-metrics/test/parser.go @@ -1,5 +1,3 @@ -// +build integration - package test import ( @@ -19,8 +17,9 @@ func PopulateTimeTplString(s string, t time.Time) string { return string(PopulateTimeTpl([]byte(s), t)) } -func PopulateTimeTpl(b []byte, t time.Time) []byte { +func PopulateTimeTpl(b []byte, tGlobal time.Time) []byte { return parseTimeExpRegex.ReplaceAllFunc(b, func(repl []byte) []byte { + t := tGlobal repl = extractRegex.FindSubmatch(repl)[1] parts := strings.SplitN(string(repl), "-", 2) if len(parts) == 2 { diff --git a/app/victoria-metrics/test/parser_test.go b/app/victoria-metrics/test/parser_test.go new file mode 100644 index 000000000..26758088a --- /dev/null +++ b/app/victoria-metrics/test/parser_test.go @@ -0,0 +1,24 @@ +package test + +import ( + "testing" + "time" +) + +func TestPopulateTimeTplString(t *testing.T) { + now, err := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z") + if err != nil { + t.Fatalf("unexpected error when parsing time: %s", err) + } + f := func(s, resultExpected string) { + t.Helper() + result := PopulateTimeTplString(s, now) + if result != resultExpected { + t.Fatalf("unexpected result; got %q; want %q", result, resultExpected) + } + } + f("", "") + f("{TIME_S}", "1136214245") + f("now: {TIME_S}, past 30s: {TIME_MS-30s}, now: {TIME_S}", "now: 1136214245, past 30s: 1136214215000, now: 1136214245") + f("now: {TIME_MS}, past 30m: {TIME_MSZ-30m}, past 2h: {TIME_NS-2h}", "now: 1136214245000, past 30m: 1136212445000, past 2h: 1136207045000000000") +} diff --git a/app/victoria-metrics/testdata/graphite/comparison-not-inf-not-nan.json b/app/victoria-metrics/testdata/graphite/comparison-not-inf-not-nan.json index 4e790bfe3..edea7ab01 100644 --- a/app/victoria-metrics/testdata/graphite/comparison-not-inf-not-nan.json +++ b/app/victoria-metrics/testdata/graphite/comparison-not-inf-not-nan.json @@ -3,12 +3,14 @@ "issue": "https://github.com/VictoriaMetrics/VictoriaMetrics/issues/150", "data": [ "not_nan_not_inf;item=x 1 {TIME_S-1m}", - "not_nan_not_inf;item=x 2 {TIME_S-2m}", - "not_nan_not_inf;item=y 1 {TIME_S-1m}", - "not_nan_not_inf;item=y 2 {TIME_S-2m}"], - "query": ["/api/v1/query_range?query=1/(not_nan_not_inf-1)!=inf!=nan&start={TIME_S-2m}&end={TIME_S}&step=60"], + "not_nan_not_inf;item=x 1 {TIME_S-2m}", + "not_nan_not_inf;item=y 3 {TIME_S-1m}", + "not_nan_not_inf;item=y 1 {TIME_S-2m}"], + "query": ["/api/v1/query_range?query=1/(not_nan_not_inf-1)!=inf!=nan&start={TIME_S-3m}&end={TIME_S}&step=60"], "result_query_range": { "status":"success", "data":{"resultType":"matrix", - "result":[{"metric":{"item":"x"},"values":[["{TIME_S-2m}","1"]]},{"metric":{"item":"y"},"values":[["{TIME_S-2m}","1"]]}]}} + "result":[ + {"metric":{"item":"y"},"values":[["{TIME_S-1m}","0.5"],["{TIME_S}","0.5"]]} + ]}} } diff --git a/app/victoria-metrics/testdata/graphite/not-nan-as-missing-data.json b/app/victoria-metrics/testdata/graphite/not-nan-as-missing-data.json index 9c201743a..fb37f2fc2 100644 --- a/app/victoria-metrics/testdata/graphite/not-nan-as-missing-data.json +++ b/app/victoria-metrics/testdata/graphite/not-nan-as-missing-data.json @@ -2,13 +2,17 @@ "name": "not-nan-as-missing-data", "issue": "https://github.com/VictoriaMetrics/VictoriaMetrics/issues/153", "data": [ - "not_nan_as_missing_data;item=x 1 {TIME_S-1m}", "not_nan_as_missing_data;item=x 2 {TIME_S-2m}", - "not_nan_as_missing_data;item=y 3 {TIME_S-1m}", - "not_nan_as_missing_data;item=y 4 {TIME_S-2m}"], + "not_nan_as_missing_data;item=x 1 {TIME_S-1m}", + "not_nan_as_missing_data;item=y 4 {TIME_S-2m}", + "not_nan_as_missing_data;item=y 3 {TIME_S-1m}" + ], "query": ["/api/v1/query_range?query=not_nan_as_missing_data>1&start={TIME_S-2m}&end={TIME_S}&step=60"], "result_query_range": { "status":"success", "data":{"resultType":"matrix", - "result":[{"metric":{"__name__":"not_nan_as_missing_data","item":"x"},"values":[["{TIME_S-2m}","2"]]},{"metric":{"__name__":"not_nan_as_missing_data","item":"y"},"values":[["{TIME_S-2m}","4"]]}]}} + "result":[ + {"metric":{"__name__":"not_nan_as_missing_data","item":"x"},"values":[["{TIME_S-2m}","2"]]}, + {"metric":{"__name__":"not_nan_as_missing_data","item":"y"},"values":[["{TIME_S-2m}","4"],["{TIME_S-1m}","3"],["{TIME_S}","3"]]} + ]}} }