From 000a918f3880d8abdb3e775fffd8b8e6b4158f08 Mon Sep 17 00:00:00 2001 From: Hui Wang Date: Fri, 29 Nov 2024 17:29:04 +0800 Subject: [PATCH] app/vmalert-tool: print an error message if no rule group is found under 'rule_files' Both vmalert and vmalert-tool support multiple `rule_files` and use directory as a file, so it's ok if some files don't contain any rule group. But vmalert-tool should warn the user if no rule group is found in any of the `rule_files`. Related issue: https://github.com/VictoriaMetrics/VictoriaMetrics/issues/7663 --- .../testdata/failed-test-with-missing-rulefile.yaml | 4 ++++ app/vmalert-tool/unittest/unittest.go | 9 ++++++--- app/vmalert-tool/unittest/unittest_test.go | 3 ++- docs/changelog/CHANGELOG.md | 1 + 4 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 app/vmalert-tool/unittest/testdata/failed-test-with-missing-rulefile.yaml diff --git a/app/vmalert-tool/unittest/testdata/failed-test-with-missing-rulefile.yaml b/app/vmalert-tool/unittest/testdata/failed-test-with-missing-rulefile.yaml new file mode 100644 index 000000000..e39e04194 --- /dev/null +++ b/app/vmalert-tool/unittest/testdata/failed-test-with-missing-rulefile.yaml @@ -0,0 +1,4 @@ +rule_files: + - non-existing-file.yaml + +tests: [] diff --git a/app/vmalert-tool/unittest/unittest.go b/app/vmalert-tool/unittest/unittest.go index a4d4140ac..c4f0d3233 100644 --- a/app/vmalert-tool/unittest/unittest.go +++ b/app/vmalert-tool/unittest/unittest.go @@ -96,8 +96,8 @@ func UnitTest(files []string, disableGroupLabel bool, externalLabels []string, e var failed bool for fileName, file := range testfiles { if err := ruleUnitTest(fileName, file, labels); err != nil { - fmt.Println(" FAILED") - fmt.Printf("\nfailed to run unit test for file %q: \n%v", fileName, err) + fmt.Println("FAILED") + fmt.Printf("failed to run unit test for file %q: \n%v", fileName, err) failed = true } else { fmt.Println(" SUCCESS") @@ -108,7 +108,7 @@ func UnitTest(files []string, disableGroupLabel bool, externalLabels []string, e } func ruleUnitTest(filename string, content []byte, externalLabels map[string]string) []error { - fmt.Println("\nUnit Testing: ", filename) + fmt.Println("\n\nUnit Testing: ", filename) var unitTestInp unitTestFile if err := yaml.UnmarshalStrict(content, &unitTestInp); err != nil { return []error{fmt.Errorf("failed to unmarshal file: %w", err)} @@ -138,6 +138,9 @@ func ruleUnitTest(filename string, content []byte, externalLabels map[string]str if err != nil { return []error{fmt.Errorf("failed to parse `rule_files`: %w", err)} } + if len(testGroups) == 0 { + return []error{fmt.Errorf("found no rule group in %v", unitTestInp.RuleFiles)} + } var errs []error for _, t := range unitTestInp.Tests { diff --git a/app/vmalert-tool/unittest/unittest_test.go b/app/vmalert-tool/unittest/unittest_test.go index 2abd55c4f..3f1ba35b6 100644 --- a/app/vmalert-tool/unittest/unittest_test.go +++ b/app/vmalert-tool/unittest/unittest_test.go @@ -24,7 +24,8 @@ func TestUnitTest_Failure(t *testing.T) { } } - // failing test + f([]string{"./testdata/failed-test-with-missing-rulefile.yaml"}) + f([]string{"./testdata/failed-test.yaml"}) } diff --git a/docs/changelog/CHANGELOG.md b/docs/changelog/CHANGELOG.md index 4e7346b11..5de570473 100644 --- a/docs/changelog/CHANGELOG.md +++ b/docs/changelog/CHANGELOG.md @@ -37,6 +37,7 @@ See also [LTS releases](https://docs.victoriametrics.com/lts-releases/). * BUGFIX: [vmui](https://docs.victoriametrics.com/#vmui): fix for `showLegend` and `alias` flags in predefined panels. [See this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/7565) * BUGFIX: [vmalert](https://docs.victoriametrics.com/vmalert): improve the correctness of alert [state restoration](https://docs.victoriametrics.com/vmalert/#alerts-state-on-restarts). Previously, it could result in false-positive alerts if alert was resolved shortly before vmalert restart. * BUGFIX: [vmalert-tool](https://docs.victoriametrics.com/vmalert-tool/): exit immediately with error message if no test file is found under specified `-files`. +* BUGFIX: [vmalert-tool](https://docs.victoriametrics.com/vmalert-tool/): print an error message if no rule group is found in `rule_files`. * BUGFIX: `vmselect` in [VictoriaMetrics cluster](https://docs.victoriametrics.com/cluster-victoriametrics/): Properly handle [multitenant](https://docs.victoriametrics.com/cluster-victoriametrics/#multitenancy-via-labels) query request errors and correctly perform search for available tenants. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/7549) for details. This is follow-up after [v1.106.1](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.106.1) release changes. * BUGFIX: `vmselect` in [VictoriaMetrics cluster](https://docs.victoriametrics.com/cluster-victoriametrics/): properly set `multitenant` cache expiration duration with `search.tenantCacheExpireDuration`. Previously flag always used default value.