Allows globs for rules when unit testing (#5595)

* Includes glob support when unit testing rule_files. 

Signed-off-by: Keenan Romain <Keenan.Romain@mailchimp.com>
This commit is contained in:
Keenan Romain 2019-06-12 06:31:07 -04:00 committed by Brian Brazil
parent a0e8d0d98d
commit 55f3a9fe4a
2 changed files with 18 additions and 5 deletions

View File

@ -70,7 +70,9 @@ func ruleUnitTest(filename string) []error {
if err := yaml.UnmarshalStrict(b, &unitTestInp); err != nil {
return []error{err}
}
resolveFilepaths(filepath.Dir(filename), &unitTestInp)
if err := resolveAndGlobFilepaths(filepath.Dir(filename), &unitTestInp); err != nil {
return []error{err}
}
if unitTestInp.EvaluationInterval == 0 {
unitTestInp.EvaluationInterval = 1 * time.Minute
@ -128,14 +130,25 @@ func (utf *unitTestFile) maxEvalTime() time.Duration {
return maxd
}
// resolveFilepaths joins all relative paths in a configuration
// with a given base directory.
func resolveFilepaths(baseDir string, utf *unitTestFile) {
// resolveAndGlobFilepaths joins all relative paths in a configuration
// with a given base directory and replaces all globs with matching files.
func resolveAndGlobFilepaths(baseDir string, utf *unitTestFile) error {
for i, rf := range utf.RuleFiles {
if rf != "" && !filepath.IsAbs(rf) {
utf.RuleFiles[i] = filepath.Join(baseDir, rf)
}
}
var globbedFiles []string
for _, rf := range utf.RuleFiles {
m, err := filepath.Glob(rf)
if err != nil {
return err
}
globbedFiles = append(globbedFiles, m...)
}
utf.RuleFiles = globbedFiles
return nil
}
// testGroup is a group of input series and tests associated with it.

View File

@ -18,7 +18,7 @@ You can use `promtool` to test your rules.
## Test file format
```yaml
# This is a list of rule files to consider for testing.
# This is a list of rule files to consider for testing. Globs are supported.
rule_files:
[ - <file_name> ]