Skip to content

Commit

Permalink
Allows globs for rules when unit testing (prometheus#5595)
Browse files Browse the repository at this point in the history
* Includes glob support when unit testing rule_files. 

Signed-off-by: Keenan Romain <Keenan.Romain@mailchimp.com>
  • Loading branch information
keenanromain authored and brian-brazil committed Jun 12, 2019
1 parent a0e8d0d commit 55f3a9f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
21 changes: 17 additions & 4 deletions cmd/promtool/unittest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion docs/configuration/unit_testing_rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -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> ]

Expand Down

0 comments on commit 55f3a9f

Please sign in to comment.