Skip to content

Commit

Permalink
pkg/rule: support identical rule filenames in different directories
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Pasquier <spasquie@redhat.com>
  • Loading branch information
simonpasquier committed Nov 26, 2019
1 parent 825f119 commit 1ad5a18
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
3 changes: 2 additions & 1 deletion pkg/rule/rule.go
@@ -1,6 +1,7 @@
package thanosrule

import (
"crypto/sha256"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -187,7 +188,7 @@ func (m *Manager) Update(evalInterval time.Duration, files []string) error {
continue
}

newFn := filepath.Join(m.workDir, filepath.Base(fn)+"."+s.String())
newFn := filepath.Join(m.workDir, fmt.Sprintf("%s.%x.%s", filepath.Base(fn), sha256.Sum256([]byte(fn)), s.String()))
if err := ioutil.WriteFile(newFn, b, os.ModePerm); err != nil {
errs = append(errs, errors.Wrap(err, newFn))
continue
Expand Down
39 changes: 27 additions & 12 deletions pkg/rule/rule_test.go
Expand Up @@ -3,7 +3,6 @@ package thanosrule
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"sort"
"strings"
Expand All @@ -22,39 +21,41 @@ func TestUpdate(t *testing.T) {
dir, err := ioutil.TempDir("", "test_rule_rule_groups")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(dir)) }()
err = os.MkdirAll(filepath.Join(dir, "subdir"), 0775)
testutil.Ok(t, err)

testutil.Ok(t, ioutil.WriteFile(path.Join(dir, "no_strategy.yaml"), []byte(`
testutil.Ok(t, ioutil.WriteFile(filepath.Join(dir, "no_strategy.yaml"), []byte(`
groups:
- name: "something1"
rules:
- alert: "some"
expr: "up"
`), os.ModePerm))
testutil.Ok(t, ioutil.WriteFile(path.Join(dir, "abort.yaml"), []byte(`
testutil.Ok(t, ioutil.WriteFile(filepath.Join(dir, "abort.yaml"), []byte(`
groups:
- name: "something2"
partial_response_strategy: "abort"
rules:
- alert: "some"
expr: "up"
`), os.ModePerm))
testutil.Ok(t, ioutil.WriteFile(path.Join(dir, "warn.yaml"), []byte(`
testutil.Ok(t, ioutil.WriteFile(filepath.Join(dir, "warn.yaml"), []byte(`
groups:
- name: "something3"
partial_response_strategy: "warn"
rules:
- alert: "some"
expr: "up"
`), os.ModePerm))
testutil.Ok(t, ioutil.WriteFile(path.Join(dir, "wrong.yaml"), []byte(`
testutil.Ok(t, ioutil.WriteFile(filepath.Join(dir, "wrong.yaml"), []byte(`
groups:
- name: "something4"
partial_response_strategy: "afafsdgsdgs" # Err 1
rules:
- alert: "some"
expr: "up"
`), os.ModePerm))
testutil.Ok(t, ioutil.WriteFile(path.Join(dir, "combined.yaml"), []byte(`
testutil.Ok(t, ioutil.WriteFile(filepath.Join(dir, "combined.yaml"), []byte(`
groups:
- name: "something5"
partial_response_strategy: "warn"
Expand All @@ -71,6 +72,14 @@ groups:
- alert: "some"
expr: "up"
`), os.ModePerm))
// Same filename as the first rule file but different path.
testutil.Ok(t, ioutil.WriteFile(filepath.Join(dir, "subdir", "no_strategy.yaml"), []byte(`
groups:
- name: "something8"
rules:
- alert: "some"
expr: "up"
`), os.ModePerm))

opts := rules.ManagerOptions{
Logger: log.NewLogfmtLogger(os.Stderr),
Expand All @@ -80,12 +89,13 @@ groups:
m.SetRuleManager(storepb.PartialResponseStrategy_WARN, rules.NewManager(&opts))

err = m.Update(10*time.Second, []string{
path.Join(dir, "no_strategy.yaml"),
path.Join(dir, "abort.yaml"),
path.Join(dir, "warn.yaml"),
path.Join(dir, "wrong.yaml"),
path.Join(dir, "combined.yaml"),
path.Join(dir, "non_existing.yaml"),
filepath.Join(dir, "no_strategy.yaml"),
filepath.Join(dir, "abort.yaml"),
filepath.Join(dir, "warn.yaml"),
filepath.Join(dir, "wrong.yaml"),
filepath.Join(dir, "combined.yaml"),
filepath.Join(dir, "non_existing.yaml"),
filepath.Join(dir, "subdir", "no_strategy.yaml"),
})

testutil.NotOk(t, err)
Expand Down Expand Up @@ -132,6 +142,11 @@ groups:
file: filepath.Join(dir, "combined.yaml"),
strategy: storepb.PartialResponseStrategy_ABORT,
},
{
name: "something8",
file: filepath.Join(dir, "subdir", "no_strategy.yaml"),
strategy: storepb.PartialResponseStrategy_ABORT,
},
}
testutil.Equals(t, len(exp), len(g))
for i := range exp {
Expand Down

0 comments on commit 1ad5a18

Please sign in to comment.