mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
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:
parent
a0e8d0d98d
commit
55f3a9fe4a
|
@ -70,7 +70,9 @@ func ruleUnitTest(filename string) []error {
|
||||||
if err := yaml.UnmarshalStrict(b, &unitTestInp); err != nil {
|
if err := yaml.UnmarshalStrict(b, &unitTestInp); err != nil {
|
||||||
return []error{err}
|
return []error{err}
|
||||||
}
|
}
|
||||||
resolveFilepaths(filepath.Dir(filename), &unitTestInp)
|
if err := resolveAndGlobFilepaths(filepath.Dir(filename), &unitTestInp); err != nil {
|
||||||
|
return []error{err}
|
||||||
|
}
|
||||||
|
|
||||||
if unitTestInp.EvaluationInterval == 0 {
|
if unitTestInp.EvaluationInterval == 0 {
|
||||||
unitTestInp.EvaluationInterval = 1 * time.Minute
|
unitTestInp.EvaluationInterval = 1 * time.Minute
|
||||||
|
@ -128,14 +130,25 @@ func (utf *unitTestFile) maxEvalTime() time.Duration {
|
||||||
return maxd
|
return maxd
|
||||||
}
|
}
|
||||||
|
|
||||||
// resolveFilepaths joins all relative paths in a configuration
|
// resolveAndGlobFilepaths joins all relative paths in a configuration
|
||||||
// with a given base directory.
|
// with a given base directory and replaces all globs with matching files.
|
||||||
func resolveFilepaths(baseDir string, utf *unitTestFile) {
|
func resolveAndGlobFilepaths(baseDir string, utf *unitTestFile) error {
|
||||||
for i, rf := range utf.RuleFiles {
|
for i, rf := range utf.RuleFiles {
|
||||||
if rf != "" && !filepath.IsAbs(rf) {
|
if rf != "" && !filepath.IsAbs(rf) {
|
||||||
utf.RuleFiles[i] = filepath.Join(baseDir, 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.
|
// testGroup is a group of input series and tests associated with it.
|
||||||
|
|
|
@ -18,7 +18,7 @@ You can use `promtool` to test your rules.
|
||||||
## Test file format
|
## Test file format
|
||||||
|
|
||||||
```yaml
|
```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:
|
rule_files:
|
||||||
[ - <file_name> ]
|
[ - <file_name> ]
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue