in tooling/prometheus-rules/main.go [199:247]
func runTests(inputRules []alertingRuleFile) error {
dir, err := os.MkdirTemp("/tmp", "prom-rule-test")
if err != nil {
return fmt.Errorf("error creating tempdir %v", err)
}
defer func() {
os.RemoveAll(dir)
}()
logrus.Debugf("Created tempdir %s", dir)
for _, irf := range inputRules {
if irf.testFileBaseName == "" {
continue
}
ruleGroups, err := yaml.Marshal(irf.rules.Spec)
if err != nil {
return fmt.Errorf("error Marshalling rule groups %v", err)
}
tmpFile := fmt.Sprintf("%s%s%s", dir, string(os.PathSeparator), irf.fileBaseName)
err = os.WriteFile(tmpFile, ruleGroups, 0644)
if err != nil {
return fmt.Errorf("error writing rule groups file %v", err)
}
fileNameParts := strings.Split(irf.fileBaseName, ".")
if len(fileNameParts) != 2 {
return fmt.Errorf("missing filename extension or using '.' in filename")
}
testFile := filepath.Join(dir, irf.testFileBaseName)
err = os.WriteFile(testFile, irf.testFileContent, 0644)
if err != nil {
return fmt.Errorf("error writing rule groups test file %v", err)
}
logrus.Debugf("running test %s", irf.testFileBaseName)
cmd := exec.Command("promtool", "test", "rules", testFile)
output, err := cmd.CombinedOutput()
if err != nil {
logrus.Error(string(output))
return fmt.Errorf("error running promtool %v", err)
}
}
return nil
}