2015-06-12 00:42:36 -07: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.
|
|
|
|
|
2020-02-03 08:23:44 -08:00
|
|
|
package parser
|
2015-06-12 00:42:36 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-10-09 17:06:53 -07:00
|
|
|
|
2020-10-29 02:43:23 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-06-12 00:42:36 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestExprString(t *testing.T) {
|
|
|
|
// A list of valid expressions that are expected to be
|
|
|
|
// returned as out when calling String(). If out is empty the output
|
|
|
|
// is expected to equal the input.
|
|
|
|
inputs := []struct {
|
|
|
|
in, out string
|
|
|
|
}{
|
2016-10-19 10:38:26 -07:00
|
|
|
{
|
2018-01-22 02:14:59 -08:00
|
|
|
in: `sum by() (task:errors:rate10s{job="s"})`,
|
2016-10-19 10:38:26 -07:00
|
|
|
out: `sum(task:errors:rate10s{job="s"})`,
|
|
|
|
},
|
2015-06-12 00:42:36 -07:00
|
|
|
{
|
2018-01-22 02:14:59 -08:00
|
|
|
in: `sum by(code) (task:errors:rate10s{job="s"})`,
|
2015-06-12 00:42:36 -07:00
|
|
|
},
|
2018-01-21 13:22:55 -08:00
|
|
|
{
|
2018-01-22 02:14:59 -08:00
|
|
|
in: `sum without() (task:errors:rate10s{job="s"})`,
|
2018-01-21 13:22:55 -08:00
|
|
|
},
|
2016-02-07 10:03:16 -08:00
|
|
|
{
|
2018-01-22 02:14:59 -08:00
|
|
|
in: `sum without(instance) (task:errors:rate10s{job="s"})`,
|
2016-02-07 10:03:16 -08:00
|
|
|
},
|
2016-07-04 05:10:42 -07:00
|
|
|
{
|
|
|
|
in: `topk(5, task:errors:rate10s{job="s"})`,
|
|
|
|
},
|
2016-07-05 09:12:19 -07:00
|
|
|
{
|
|
|
|
in: `count_values("value", task:errors:rate10s{job="s"})`,
|
|
|
|
},
|
2016-10-19 10:38:26 -07:00
|
|
|
{
|
2018-01-22 02:14:59 -08:00
|
|
|
in: `a - on() c`,
|
2016-10-19 10:38:26 -07:00
|
|
|
},
|
2016-04-21 03:45:06 -07:00
|
|
|
{
|
2018-01-22 02:14:59 -08:00
|
|
|
in: `a - on(b) c`,
|
2016-04-21 03:45:06 -07:00
|
|
|
},
|
2016-05-28 10:45:35 -07:00
|
|
|
{
|
2018-01-22 02:14:59 -08:00
|
|
|
in: `a - on(b) group_left(x) c`,
|
2016-05-28 10:45:35 -07:00
|
|
|
},
|
|
|
|
{
|
2018-01-22 02:14:59 -08:00
|
|
|
in: `a - on(b) group_left(x, y) c`,
|
2016-05-28 10:45:35 -07:00
|
|
|
},
|
|
|
|
{
|
2018-01-22 02:14:59 -08:00
|
|
|
in: `a - on(b) group_left c`,
|
|
|
|
out: `a - on(b) group_left() c`,
|
2016-11-04 04:06:07 -07:00
|
|
|
},
|
|
|
|
{
|
2018-01-22 02:14:59 -08:00
|
|
|
in: `a - on(b) group_left() (c)`,
|
2016-05-28 10:45:35 -07:00
|
|
|
},
|
2016-04-21 03:45:06 -07:00
|
|
|
{
|
2018-01-22 02:14:59 -08:00
|
|
|
in: `a - ignoring(b) c`,
|
2016-04-21 03:45:06 -07:00
|
|
|
},
|
2016-10-19 10:38:26 -07:00
|
|
|
{
|
2018-01-22 02:14:59 -08:00
|
|
|
in: `a - ignoring() c`,
|
2016-10-19 10:38:26 -07:00
|
|
|
out: `a - c`,
|
|
|
|
},
|
2015-09-09 16:37:05 -07:00
|
|
|
{
|
2018-01-22 02:14:59 -08:00
|
|
|
in: `up > bool 0`,
|
2015-09-09 16:37:05 -07:00
|
|
|
},
|
2015-11-15 01:26:38 -08:00
|
|
|
{
|
2018-01-22 02:14:59 -08:00
|
|
|
in: `a offset 1m`,
|
2015-11-15 01:26:38 -08:00
|
|
|
},
|
2021-02-23 18:16:28 -08:00
|
|
|
{
|
|
|
|
in: `a offset -7m`,
|
|
|
|
},
|
2015-11-15 01:26:38 -08:00
|
|
|
{
|
2018-01-22 02:14:59 -08:00
|
|
|
in: `a{c="d"}[5m] offset 1m`,
|
2015-11-15 01:26:38 -08:00
|
|
|
},
|
|
|
|
{
|
2018-01-22 02:14:59 -08:00
|
|
|
in: `a[5m] offset 1m`,
|
2015-11-15 01:26:38 -08:00
|
|
|
},
|
2021-02-23 18:16:28 -08:00
|
|
|
{
|
|
|
|
in: `a[12m] offset -3m`,
|
|
|
|
},
|
2019-11-25 22:45:51 -08:00
|
|
|
{
|
|
|
|
in: `a[1h:5m] offset 1m`,
|
|
|
|
},
|
2019-05-10 16:46:15 -07:00
|
|
|
{
|
|
|
|
in: `{__name__="a"}`,
|
|
|
|
},
|
2020-07-24 03:21:42 -07:00
|
|
|
{
|
|
|
|
in: `a{b!="c"}[1m]`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: `a{b=~"c"}[1m]`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: `a{b!~"c"}[1m]`,
|
|
|
|
},
|
2021-02-09 08:03:16 -08:00
|
|
|
{
|
|
|
|
in: `a @ 10`,
|
|
|
|
out: `a @ 10.000`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: `a[1m] @ 10`,
|
|
|
|
out: `a[1m] @ 10.000`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: `a @ start()`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: `a @ end()`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: `a[1m] @ start()`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: `a[1m] @ end()`,
|
|
|
|
},
|
2015-06-12 00:42:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range inputs {
|
|
|
|
expr, err := ParseExpr(test.in)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-10-09 17:06:53 -07:00
|
|
|
|
2015-06-12 00:42:36 -07:00
|
|
|
exp := test.in
|
|
|
|
if test.out != "" {
|
|
|
|
exp = test.out
|
|
|
|
}
|
2019-10-09 17:06:53 -07:00
|
|
|
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Equal(t, exp, expr.String())
|
2015-06-12 00:42:36 -07:00
|
|
|
}
|
|
|
|
}
|