2015-03-30 16:12:51 +00:00
|
|
|
// Copyright 2015 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 promql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-10-10 00:06:53 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
2015-03-30 16:12:51 +00:00
|
|
|
)
|
|
|
|
|
2019-01-16 21:35:45 +00:00
|
|
|
type testCase struct {
|
2015-05-11 12:04:53 +00:00
|
|
|
input string
|
|
|
|
expected []item
|
|
|
|
fail bool
|
|
|
|
seriesDesc bool // Whether to lex a series description.
|
2019-01-16 21:35:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
name string
|
|
|
|
tests []testCase
|
2015-03-30 16:12:51 +00:00
|
|
|
}{
|
|
|
|
{
|
2019-01-16 21:35:45 +00:00
|
|
|
name: "common",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: ",",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemComma, 0, ","}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "()",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemLeftParen, 0, `(`}, {ItemRightParen, 1, `)`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "{}",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemLeftBrace, 0, `{`}, {ItemRightBrace, 1, `}`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "[5m]",
|
|
|
|
expected: []item{
|
2019-03-14 19:53:55 +00:00
|
|
|
{ItemLeftBracket, 0, `[`},
|
|
|
|
{ItemDuration, 1, `5m`},
|
|
|
|
{ItemRightBracket, 3, `]`},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
input: "\r\n\r",
|
|
|
|
expected: []item{},
|
|
|
|
},
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-01-16 21:35:45 +00:00
|
|
|
name: "numbers",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: "1",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemNumber, 0, "1"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "4.23",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemNumber, 0, "4.23"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: ".3",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemNumber, 0, ".3"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "5.",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemNumber, 0, "5."}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "NaN",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemNumber, 0, "NaN"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "nAN",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemNumber, 0, "nAN"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "NaN 123",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemNumber, 0, "NaN"}, {ItemNumber, 4, "123"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "NaN123",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemIdentifier, 0, "NaN123"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "iNf",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemNumber, 0, "iNf"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "Inf",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemNumber, 0, "Inf"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "+Inf",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemADD, 0, "+"}, {ItemNumber, 1, "Inf"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "+Inf 123",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemADD, 0, "+"}, {ItemNumber, 1, "Inf"}, {ItemNumber, 5, "123"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "-Inf",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemSUB, 0, "-"}, {ItemNumber, 1, "Inf"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "Infoo",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemIdentifier, 0, "Infoo"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "-Infoo",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemSUB, 0, "-"}, {ItemIdentifier, 1, "Infoo"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "-Inf 123",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemSUB, 0, "-"}, {ItemNumber, 1, "Inf"}, {ItemNumber, 5, "123"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "0x123",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemNumber, 0, "0x123"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
2016-03-01 22:23:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-01-16 21:35:45 +00:00
|
|
|
name: "strings",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: "\"test\\tsequence\"",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemString, 0, `"test\tsequence"`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "\"test\\\\.expression\"",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemString, 0, `"test\\.expression"`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "\"test\\.expression\"",
|
|
|
|
expected: []item{
|
2019-03-14 19:53:55 +00:00
|
|
|
{ItemError, 0, "unknown escape sequence U+002E '.'"},
|
|
|
|
{ItemString, 0, `"test\.expression"`},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "`test\\.expression`",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemString, 0, "`test\\.expression`"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// See https://github.com/prometheus/prometheus/issues/939.
|
|
|
|
input: ".٩",
|
|
|
|
fail: true,
|
|
|
|
},
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-01-16 21:35:45 +00:00
|
|
|
name: "durations",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: "5s",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemDuration, 0, "5s"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "123m",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemDuration, 0, "123m"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "1h",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemDuration, 0, "1h"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "3w",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemDuration, 0, "3w"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "1y",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemDuration, 0, "1y"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
},
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
{
|
2019-01-16 21:35:45 +00:00
|
|
|
name: "identifiers",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: "abc",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemIdentifier, 0, "abc"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "a:bc",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemMetricIdentifier, 0, "a:bc"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "abc d",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemIdentifier, 0, "abc"}, {ItemIdentifier, 4, "d"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: ":bc",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemMetricIdentifier, 0, ":bc"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "0a:bc",
|
|
|
|
fail: true,
|
|
|
|
},
|
2015-05-08 14:43:02 +00:00
|
|
|
},
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
{
|
2019-01-16 21:35:45 +00:00
|
|
|
name: "comments",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: "# some comment",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemComment, 0, "# some comment"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "5 # 1+1\n5",
|
|
|
|
expected: []item{
|
2019-03-14 19:53:55 +00:00
|
|
|
{ItemNumber, 0, "5"},
|
|
|
|
{ItemComment, 2, "# 1+1"},
|
|
|
|
{ItemNumber, 8, "5"},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
|
|
|
{
|
2019-01-16 21:35:45 +00:00
|
|
|
name: "operators",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: `=`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemAssign, 0, `=`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
// Inside braces equality is a single '=' character.
|
|
|
|
input: `{=}`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemLeftBrace, 0, `{`}, {ItemEQL, 1, `=`}, {ItemRightBrace, 2, `}`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: `==`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemEQL, 0, `==`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: `!=`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemNEQ, 0, `!=`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: `<`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemLSS, 0, `<`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: `>`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemGTR, 0, `>`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: `>=`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemGTE, 0, `>=`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: `<=`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemLTE, 0, `<=`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: `+`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemADD, 0, `+`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: `-`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemSUB, 0, `-`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: `*`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemMUL, 0, `*`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: `/`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemDIV, 0, `/`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: `^`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemPOW, 0, `^`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: `%`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemMOD, 0, `%`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: `AND`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemLAND, 0, `AND`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: `or`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemLOR, 0, `or`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: `unless`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemLUnless, 0, `unless`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
},
|
2015-03-30 16:12:51 +00:00
|
|
|
},
|
2017-06-16 14:19:24 +00:00
|
|
|
{
|
2019-01-16 21:35:45 +00:00
|
|
|
name: "aggregators",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: `sum`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemSum, 0, `sum`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: `AVG`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemAvg, 0, `AVG`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: `MAX`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemMax, 0, `MAX`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: `min`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemMin, 0, `min`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: `count`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemCount, 0, `count`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: `stdvar`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemStdvar, 0, `stdvar`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: `stddev`,
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemStddev, 0, `stddev`}},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
},
|
2017-06-16 14:19:24 +00:00
|
|
|
},
|
|
|
|
{
|
2019-01-16 21:35:45 +00:00
|
|
|
name: "keywords",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: "offset",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemOffset, 0, "offset"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "by",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemBy, 0, "by"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "without",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemWithout, 0, "without"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "on",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemOn, 0, "on"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "ignoring",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemIgnoring, 0, "ignoring"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "group_left",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemGroupLeft, 0, "group_left"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "group_right",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemGroupRight, 0, "group_right"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
}, {
|
|
|
|
input: "bool",
|
2019-03-14 19:53:55 +00:00
|
|
|
expected: []item{{ItemBool, 0, "bool"}},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
},
|
2017-06-16 14:19:24 +00:00
|
|
|
},
|
2015-05-12 08:39:10 +00:00
|
|
|
{
|
2019-01-16 21:35:45 +00:00
|
|
|
name: "selectors",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: `台北`,
|
|
|
|
fail: true,
|
|
|
|
}, {
|
|
|
|
input: `{台北='a'}`,
|
|
|
|
fail: true,
|
|
|
|
}, {
|
|
|
|
input: `{0a='a'}`,
|
|
|
|
fail: true,
|
|
|
|
}, {
|
|
|
|
input: `{foo='bar'}`,
|
|
|
|
expected: []item{
|
2019-03-14 19:53:55 +00:00
|
|
|
{ItemLeftBrace, 0, `{`},
|
|
|
|
{ItemIdentifier, 1, `foo`},
|
|
|
|
{ItemEQL, 4, `=`},
|
|
|
|
{ItemString, 5, `'bar'`},
|
|
|
|
{ItemRightBrace, 10, `}`},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
input: `{foo="bar"}`,
|
|
|
|
expected: []item{
|
2019-03-14 19:53:55 +00:00
|
|
|
{ItemLeftBrace, 0, `{`},
|
|
|
|
{ItemIdentifier, 1, `foo`},
|
|
|
|
{ItemEQL, 4, `=`},
|
|
|
|
{ItemString, 5, `"bar"`},
|
|
|
|
{ItemRightBrace, 10, `}`},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
input: `{foo="bar\"bar"}`,
|
|
|
|
expected: []item{
|
2019-03-14 19:53:55 +00:00
|
|
|
{ItemLeftBrace, 0, `{`},
|
|
|
|
{ItemIdentifier, 1, `foo`},
|
|
|
|
{ItemEQL, 4, `=`},
|
|
|
|
{ItemString, 5, `"bar\"bar"`},
|
|
|
|
{ItemRightBrace, 15, `}`},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
input: `{NaN != "bar" }`,
|
|
|
|
expected: []item{
|
2019-03-14 19:53:55 +00:00
|
|
|
{ItemLeftBrace, 0, `{`},
|
|
|
|
{ItemIdentifier, 1, `NaN`},
|
|
|
|
{ItemNEQ, 5, `!=`},
|
|
|
|
{ItemString, 8, `"bar"`},
|
|
|
|
{ItemRightBrace, 14, `}`},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
input: `{alert=~"bar" }`,
|
|
|
|
expected: []item{
|
2019-03-14 19:53:55 +00:00
|
|
|
{ItemLeftBrace, 0, `{`},
|
|
|
|
{ItemIdentifier, 1, `alert`},
|
|
|
|
{ItemEQLRegex, 6, `=~`},
|
|
|
|
{ItemString, 8, `"bar"`},
|
|
|
|
{ItemRightBrace, 14, `}`},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
input: `{on!~"bar"}`,
|
|
|
|
expected: []item{
|
2019-03-14 19:53:55 +00:00
|
|
|
{ItemLeftBrace, 0, `{`},
|
|
|
|
{ItemIdentifier, 1, `on`},
|
|
|
|
{ItemNEQRegex, 3, `!~`},
|
|
|
|
{ItemString, 5, `"bar"`},
|
|
|
|
{ItemRightBrace, 10, `}`},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
input: `{alert!#"bar"}`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: `{foo:a="bar"}`, fail: true,
|
|
|
|
},
|
2015-05-12 08:39:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-01-16 21:35:45 +00:00
|
|
|
name: "common errors",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: `=~`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: `!~`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: `!(`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: "1a", fail: true,
|
|
|
|
},
|
2015-05-12 08:39:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-01-16 21:35:45 +00:00
|
|
|
name: "mismatched parentheses",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: `(`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: `())`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: `(()`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: `{`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: `}`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: "{{", fail: true,
|
|
|
|
}, {
|
|
|
|
input: "{{}}", fail: true,
|
|
|
|
}, {
|
|
|
|
input: `[`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: `[[`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: `[]]`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: `[[]]`, fail: true,
|
|
|
|
}, {
|
|
|
|
input: `]`, fail: true,
|
|
|
|
},
|
2015-05-12 08:39:10 +00:00
|
|
|
},
|
|
|
|
},
|
2018-12-22 13:47:13 +00:00
|
|
|
{
|
2019-01-16 21:35:45 +00:00
|
|
|
name: "encoding issues",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: "\"\xff\"", fail: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "`\xff`", fail: true,
|
|
|
|
},
|
2018-12-22 13:47:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-01-16 21:35:45 +00:00
|
|
|
name: "series descriptions",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: `{} _ 1 x .3`,
|
|
|
|
expected: []item{
|
2019-03-14 19:53:55 +00:00
|
|
|
{ItemLeftBrace, 0, `{`},
|
|
|
|
{ItemRightBrace, 1, `}`},
|
|
|
|
{ItemSpace, 2, ` `},
|
|
|
|
{ItemBlank, 3, `_`},
|
|
|
|
{ItemSpace, 4, ` `},
|
|
|
|
{ItemNumber, 5, `1`},
|
|
|
|
{ItemSpace, 6, ` `},
|
|
|
|
{ItemTimes, 7, `x`},
|
|
|
|
{ItemSpace, 8, ` `},
|
|
|
|
{ItemNumber, 9, `.3`},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
seriesDesc: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `metric +Inf Inf NaN`,
|
|
|
|
expected: []item{
|
2019-03-14 19:53:55 +00:00
|
|
|
{ItemIdentifier, 0, `metric`},
|
|
|
|
{ItemSpace, 6, ` `},
|
|
|
|
{ItemADD, 7, `+`},
|
|
|
|
{ItemNumber, 8, `Inf`},
|
|
|
|
{ItemSpace, 11, ` `},
|
|
|
|
{ItemNumber, 12, `Inf`},
|
|
|
|
{ItemSpace, 15, ` `},
|
|
|
|
{ItemNumber, 16, `NaN`},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
seriesDesc: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `metric 1+1x4`,
|
|
|
|
expected: []item{
|
2019-03-14 19:53:55 +00:00
|
|
|
{ItemIdentifier, 0, `metric`},
|
|
|
|
{ItemSpace, 6, ` `},
|
|
|
|
{ItemNumber, 7, `1`},
|
|
|
|
{ItemADD, 8, `+`},
|
|
|
|
{ItemNumber, 9, `1`},
|
|
|
|
{ItemTimes, 10, `x`},
|
|
|
|
{ItemNumber, 11, `4`},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
seriesDesc: true,
|
|
|
|
},
|
2018-12-22 13:47:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-01-16 21:35:45 +00:00
|
|
|
name: "subqueries",
|
|
|
|
tests: []testCase{
|
|
|
|
{
|
|
|
|
input: `test_name{on!~"bar"}[4m:4s]`,
|
|
|
|
expected: []item{
|
2019-03-14 19:53:55 +00:00
|
|
|
{ItemIdentifier, 0, `test_name`},
|
|
|
|
{ItemLeftBrace, 9, `{`},
|
|
|
|
{ItemIdentifier, 10, `on`},
|
|
|
|
{ItemNEQRegex, 12, `!~`},
|
|
|
|
{ItemString, 14, `"bar"`},
|
|
|
|
{ItemRightBrace, 19, `}`},
|
|
|
|
{ItemLeftBracket, 20, `[`},
|
|
|
|
{ItemDuration, 21, `4m`},
|
|
|
|
{ItemColon, 23, `:`},
|
|
|
|
{ItemDuration, 24, `4s`},
|
|
|
|
{ItemRightBracket, 26, `]`},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `test:name{on!~"bar"}[4m:4s]`,
|
|
|
|
expected: []item{
|
2019-03-14 19:53:55 +00:00
|
|
|
{ItemMetricIdentifier, 0, `test:name`},
|
|
|
|
{ItemLeftBrace, 9, `{`},
|
|
|
|
{ItemIdentifier, 10, `on`},
|
|
|
|
{ItemNEQRegex, 12, `!~`},
|
|
|
|
{ItemString, 14, `"bar"`},
|
|
|
|
{ItemRightBrace, 19, `}`},
|
|
|
|
{ItemLeftBracket, 20, `[`},
|
|
|
|
{ItemDuration, 21, `4m`},
|
|
|
|
{ItemColon, 23, `:`},
|
|
|
|
{ItemDuration, 24, `4s`},
|
|
|
|
{ItemRightBracket, 26, `]`},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
input: `test:name{on!~"b:ar"}[4m:4s]`,
|
|
|
|
expected: []item{
|
2019-03-14 19:53:55 +00:00
|
|
|
{ItemMetricIdentifier, 0, `test:name`},
|
|
|
|
{ItemLeftBrace, 9, `{`},
|
|
|
|
{ItemIdentifier, 10, `on`},
|
|
|
|
{ItemNEQRegex, 12, `!~`},
|
|
|
|
{ItemString, 14, `"b:ar"`},
|
|
|
|
{ItemRightBrace, 20, `}`},
|
|
|
|
{ItemLeftBracket, 21, `[`},
|
|
|
|
{ItemDuration, 22, `4m`},
|
|
|
|
{ItemColon, 24, `:`},
|
|
|
|
{ItemDuration, 25, `4s`},
|
|
|
|
{ItemRightBracket, 27, `]`},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
input: `test:name{on!~"b:ar"}[4m:]`,
|
|
|
|
expected: []item{
|
2019-03-14 19:53:55 +00:00
|
|
|
{ItemMetricIdentifier, 0, `test:name`},
|
|
|
|
{ItemLeftBrace, 9, `{`},
|
|
|
|
{ItemIdentifier, 10, `on`},
|
|
|
|
{ItemNEQRegex, 12, `!~`},
|
|
|
|
{ItemString, 14, `"b:ar"`},
|
|
|
|
{ItemRightBrace, 20, `}`},
|
|
|
|
{ItemLeftBracket, 21, `[`},
|
|
|
|
{ItemDuration, 22, `4m`},
|
|
|
|
{ItemColon, 24, `:`},
|
|
|
|
{ItemRightBracket, 25, `]`},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
}, { // Nested Subquery.
|
|
|
|
input: `min_over_time(rate(foo{bar="baz"}[2s])[5m:])[4m:3s]`,
|
|
|
|
expected: []item{
|
|
|
|
|
2019-03-14 19:53:55 +00:00
|
|
|
{ItemIdentifier, 0, `min_over_time`},
|
|
|
|
{ItemLeftParen, 13, `(`},
|
|
|
|
{ItemIdentifier, 14, `rate`},
|
|
|
|
{ItemLeftParen, 18, `(`},
|
|
|
|
{ItemIdentifier, 19, `foo`},
|
|
|
|
{ItemLeftBrace, 22, `{`},
|
|
|
|
{ItemIdentifier, 23, `bar`},
|
|
|
|
{ItemEQL, 26, `=`},
|
|
|
|
{ItemString, 27, `"baz"`},
|
|
|
|
{ItemRightBrace, 32, `}`},
|
|
|
|
{ItemLeftBracket, 33, `[`},
|
|
|
|
{ItemDuration, 34, `2s`},
|
|
|
|
{ItemRightBracket, 36, `]`},
|
|
|
|
{ItemRightParen, 37, `)`},
|
|
|
|
{ItemLeftBracket, 38, `[`},
|
|
|
|
{ItemDuration, 39, `5m`},
|
|
|
|
{ItemColon, 41, `:`},
|
|
|
|
{ItemRightBracket, 42, `]`},
|
|
|
|
{ItemRightParen, 43, `)`},
|
|
|
|
{ItemLeftBracket, 44, `[`},
|
|
|
|
{ItemDuration, 45, `4m`},
|
|
|
|
{ItemColon, 47, `:`},
|
|
|
|
{ItemDuration, 48, `3s`},
|
|
|
|
{ItemRightBracket, 50, `]`},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
// Subquery with offset.
|
|
|
|
{
|
|
|
|
input: `test:name{on!~"b:ar"}[4m:4s] offset 10m`,
|
|
|
|
expected: []item{
|
2019-03-14 19:53:55 +00:00
|
|
|
{ItemMetricIdentifier, 0, `test:name`},
|
|
|
|
{ItemLeftBrace, 9, `{`},
|
|
|
|
{ItemIdentifier, 10, `on`},
|
|
|
|
{ItemNEQRegex, 12, `!~`},
|
|
|
|
{ItemString, 14, `"b:ar"`},
|
|
|
|
{ItemRightBrace, 20, `}`},
|
|
|
|
{ItemLeftBracket, 21, `[`},
|
|
|
|
{ItemDuration, 22, `4m`},
|
|
|
|
{ItemColon, 24, `:`},
|
|
|
|
{ItemDuration, 25, `4s`},
|
|
|
|
{ItemRightBracket, 27, `]`},
|
|
|
|
{ItemOffset, 29, "offset"},
|
|
|
|
{ItemDuration, 36, "10m"},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
}, {
|
|
|
|
input: `min_over_time(rate(foo{bar="baz"}[2s])[5m:] offset 6m)[4m:3s]`,
|
|
|
|
expected: []item{
|
2018-12-22 13:47:13 +00:00
|
|
|
|
2019-03-14 19:53:55 +00:00
|
|
|
{ItemIdentifier, 0, `min_over_time`},
|
|
|
|
{ItemLeftParen, 13, `(`},
|
|
|
|
{ItemIdentifier, 14, `rate`},
|
|
|
|
{ItemLeftParen, 18, `(`},
|
|
|
|
{ItemIdentifier, 19, `foo`},
|
|
|
|
{ItemLeftBrace, 22, `{`},
|
|
|
|
{ItemIdentifier, 23, `bar`},
|
|
|
|
{ItemEQL, 26, `=`},
|
|
|
|
{ItemString, 27, `"baz"`},
|
|
|
|
{ItemRightBrace, 32, `}`},
|
|
|
|
{ItemLeftBracket, 33, `[`},
|
|
|
|
{ItemDuration, 34, `2s`},
|
|
|
|
{ItemRightBracket, 36, `]`},
|
|
|
|
{ItemRightParen, 37, `)`},
|
|
|
|
{ItemLeftBracket, 38, `[`},
|
|
|
|
{ItemDuration, 39, `5m`},
|
|
|
|
{ItemColon, 41, `:`},
|
|
|
|
{ItemRightBracket, 42, `]`},
|
|
|
|
{ItemOffset, 44, `offset`},
|
|
|
|
{ItemDuration, 51, `6m`},
|
|
|
|
{ItemRightParen, 53, `)`},
|
|
|
|
{ItemLeftBracket, 54, `[`},
|
|
|
|
{ItemDuration, 55, `4m`},
|
|
|
|
{ItemColon, 57, `:`},
|
|
|
|
{ItemDuration, 58, `3s`},
|
|
|
|
{ItemRightBracket, 60, `]`},
|
2019-01-16 21:35:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `test:name{o:n!~"bar"}[4m:4s]`,
|
|
|
|
fail: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `test:name{on!~"bar"}[4m:4s:4h]`,
|
|
|
|
fail: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `test:name{on!~"bar"}[4m:4s:]`,
|
|
|
|
fail: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `test:name{on!~"bar"}[4m::]`,
|
|
|
|
fail: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: `test:name{on!~"bar"}[:4s]`,
|
|
|
|
fail: true,
|
|
|
|
},
|
2018-12-22 13:47:13 +00:00
|
|
|
},
|
|
|
|
},
|
2015-03-30 16:12:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestLexer tests basic functionality of the lexer. More elaborate tests are implemented
|
|
|
|
// for the parser to avoid duplicated effort.
|
|
|
|
func TestLexer(t *testing.T) {
|
2019-01-16 21:35:45 +00:00
|
|
|
for _, typ := range tests {
|
|
|
|
t.Run(typ.name, func(t *testing.T) {
|
|
|
|
for i, test := range typ.tests {
|
|
|
|
l := &lexer{
|
|
|
|
input: test.input,
|
|
|
|
items: make(chan item),
|
|
|
|
seriesDesc: test.seriesDesc,
|
|
|
|
}
|
|
|
|
go l.run()
|
2016-08-29 07:20:43 +00:00
|
|
|
|
2019-01-16 21:35:45 +00:00
|
|
|
out := []item{}
|
|
|
|
for it := range l.items {
|
|
|
|
out = append(out, it)
|
|
|
|
}
|
2015-03-30 16:12:51 +00:00
|
|
|
|
2019-01-16 21:35:45 +00:00
|
|
|
lastItem := out[len(out)-1]
|
|
|
|
if test.fail {
|
2019-03-14 19:53:55 +00:00
|
|
|
if lastItem.typ != ItemError {
|
2019-01-16 21:35:45 +00:00
|
|
|
t.Logf("%d: input %q", i, test.input)
|
|
|
|
t.Fatalf("expected lexing error but did not fail")
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2019-03-14 19:53:55 +00:00
|
|
|
if lastItem.typ == ItemError {
|
2019-01-16 21:35:45 +00:00
|
|
|
t.Logf("%d: input %q", i, test.input)
|
|
|
|
t.Fatalf("unexpected lexing error at position %d: %s", lastItem.pos, lastItem)
|
|
|
|
}
|
2015-03-30 16:12:51 +00:00
|
|
|
|
2019-10-10 00:06:53 +00:00
|
|
|
eofItem := item{ItemEOF, Pos(len(test.input)), ""}
|
|
|
|
testutil.Equals(t, lastItem, eofItem, "%d: input %q", i, test.input)
|
|
|
|
|
2019-01-16 21:35:45 +00:00
|
|
|
out = out[:len(out)-1]
|
2019-10-10 00:06:53 +00:00
|
|
|
testutil.Equals(t, out, test.expected, "%d: input %q", i, test.input)
|
2019-01-16 21:35:45 +00:00
|
|
|
}
|
|
|
|
})
|
2015-03-30 16:12:51 +00:00
|
|
|
}
|
|
|
|
}
|