2017-06-14 01:04:13 -07:00
|
|
|
// Copyright 2017 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.
|
|
|
|
|
2017-06-07 07:58:15 -07:00
|
|
|
package rulefmt
|
|
|
|
|
2017-06-14 00:51:32 -07:00
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2018-09-13 05:55:58 -07:00
|
|
|
|
2020-10-29 02:43:23 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-06-14 00:51:32 -07:00
|
|
|
)
|
2017-06-07 07:58:15 -07:00
|
|
|
|
|
|
|
func TestParseFileSuccess(t *testing.T) {
|
2021-09-13 12:19:20 -07:00
|
|
|
_, errs := ParseFile("testdata/test.yaml")
|
|
|
|
require.Empty(t, errs, "unexpected errors parsing file")
|
2017-06-07 07:58:15 -07:00
|
|
|
}
|
2017-06-14 00:51:32 -07:00
|
|
|
|
|
|
|
func TestParseFileFailure(t *testing.T) {
|
|
|
|
table := []struct {
|
|
|
|
filename string
|
|
|
|
errMsg string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
filename: "duplicate_grp.bad.yaml",
|
|
|
|
errMsg: "groupname: \"yolo\" is repeated in the same file",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
filename: "bad_expr.bad.yaml",
|
|
|
|
errMsg: "parse error",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
filename: "record_and_alert.bad.yaml",
|
|
|
|
errMsg: "only one of 'record' and 'alert' must be set",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
filename: "no_rec_alert.bad.yaml",
|
|
|
|
errMsg: "one of 'record' or 'alert' must be set",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
filename: "noexpr.bad.yaml",
|
|
|
|
errMsg: "field 'expr' must be set in rule",
|
|
|
|
},
|
2017-06-14 02:37:54 -07:00
|
|
|
{
|
|
|
|
filename: "bad_lname.bad.yaml",
|
|
|
|
errMsg: "invalid label name",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
filename: "bad_annotation.bad.yaml",
|
|
|
|
errMsg: "invalid annotation name",
|
|
|
|
},
|
2017-10-17 02:22:59 -07:00
|
|
|
{
|
|
|
|
filename: "invalid_record_name.bad.yaml",
|
|
|
|
errMsg: "invalid recording rule name",
|
|
|
|
},
|
2020-08-09 06:42:25 -07:00
|
|
|
{
|
|
|
|
filename: "bad_field.bad.yaml",
|
|
|
|
errMsg: "field annotation not found",
|
|
|
|
},
|
2020-08-12 14:37:31 -07:00
|
|
|
{
|
|
|
|
filename: "invalid_label_name.bad.yaml",
|
|
|
|
errMsg: "invalid label name",
|
|
|
|
},
|
2017-06-14 00:51:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range table {
|
|
|
|
_, errs := ParseFile(filepath.Join("testdata", c.filename))
|
2021-09-13 12:19:20 -07:00
|
|
|
require.NotNil(t, errs, "Expected error parsing %s but got none", c.filename)
|
|
|
|
require.Error(t, errs[0], c.errMsg, "Expected error for %s.", c.filename)
|
2017-06-14 00:51:32 -07:00
|
|
|
}
|
|
|
|
}
|
2018-09-13 05:55:58 -07:00
|
|
|
|
|
|
|
func TestTemplateParsing(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
ruleString string
|
|
|
|
shouldPass bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
ruleString: `
|
|
|
|
groups:
|
|
|
|
- name: example
|
|
|
|
rules:
|
|
|
|
- alert: InstanceDown
|
|
|
|
expr: up == 0
|
|
|
|
for: 5m
|
|
|
|
labels:
|
|
|
|
severity: "page"
|
|
|
|
annotations:
|
|
|
|
summary: "Instance {{ $labels.instance }} down"
|
|
|
|
`,
|
|
|
|
shouldPass: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// `$label` instead of `$labels`.
|
|
|
|
ruleString: `
|
|
|
|
groups:
|
|
|
|
- name: example
|
|
|
|
rules:
|
|
|
|
- alert: InstanceDown
|
|
|
|
expr: up == 0
|
|
|
|
for: 5m
|
|
|
|
labels:
|
|
|
|
severity: "page"
|
|
|
|
annotations:
|
|
|
|
summary: "Instance {{ $label.instance }} down"
|
|
|
|
`,
|
|
|
|
shouldPass: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// `$this_is_wrong`.
|
|
|
|
ruleString: `
|
|
|
|
groups:
|
|
|
|
- name: example
|
|
|
|
rules:
|
|
|
|
- alert: InstanceDown
|
|
|
|
expr: up == 0
|
|
|
|
for: 5m
|
|
|
|
labels:
|
|
|
|
severity: "{{$this_is_wrong}}"
|
|
|
|
annotations:
|
|
|
|
summary: "Instance {{ $labels.instance }} down"
|
|
|
|
`,
|
|
|
|
shouldPass: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// `$labels.quantile * 100`.
|
|
|
|
ruleString: `
|
|
|
|
groups:
|
|
|
|
- name: example
|
|
|
|
rules:
|
|
|
|
- alert: InstanceDown
|
|
|
|
expr: up == 0
|
|
|
|
for: 5m
|
|
|
|
labels:
|
|
|
|
severity: "page"
|
|
|
|
annotations:
|
|
|
|
summary: "Instance {{ $labels.instance }} down"
|
|
|
|
description: "{{$labels.quantile * 100}}"
|
|
|
|
`,
|
|
|
|
shouldPass: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tst := range tests {
|
|
|
|
rgs, errs := Parse([]byte(tst.ruleString))
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NotNil(t, rgs, "Rule parsing, rule=\n"+tst.ruleString)
|
2018-09-13 05:55:58 -07:00
|
|
|
passed := (tst.shouldPass && len(errs) == 0) || (!tst.shouldPass && len(errs) > 0)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.True(t, passed, "Rule validation failed, rule=\n"+tst.ruleString)
|
2018-09-13 05:55:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|