Skip to content

Commit

Permalink
fail lint on duplicate rules
Browse files Browse the repository at this point in the history
a little closer

test duplicate rules
  • Loading branch information
paulgmiller authored and jwilder committed Dec 20, 2024
1 parent 512e0cc commit 51d162c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
9 changes: 9 additions & 0 deletions alerter/rules/localStore.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ func FromPath(path, region string) (*fileStore, error) {
}
return nil
})

knownRules := map[string]bool{}
for _, rule := range s.Rules() {
key := rule.Namespace + "/" + rule.Name
if knownRules[key] {
return nil, fmt.Errorf("duplicate rule %s", key)
}
knownRules[key] = true
}
return s, err

}
Expand Down
53 changes: 53 additions & 0 deletions alerter/rules/localStore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,49 @@ spec:
destination: "peoplewhocare"
`

var dupeRule = `apiVersion: adx-mon.azure.com/v1
kind: AlertRule
metadata:
name: alert-rule-one
namespace: namespace-one
spec:
database: SomeDB
interval: 1h
query: |
BadThings
| where stuff > 1
| summarize count() by region
| extend Severity=3
| extend Title = "Bad Things!"
| extend Summary = "Bad Things! Oh no"
| extend CorrelationId = region
| extend TSG="http://gofixit.com"
autoMitigateAfter: 2h
destination: "peoplewhocare"
---
apiVersion: adx-mon.azure.com/v1
kind: AlertRule
metadata:
name: alert-rule-one
namespace: namespace-one
spec:
database: SomeDB
interval: 1h
query: |
BadThings
| where stuff > 1
| summarize count() by region
| extend Severity=3
| extend Title = "Bad Things!"
| extend Summary = "Bad Things! Oh no"
| extend CorrelationId = region
| extend TSG="http://gofixit.com"
autoMitigateAfter: 2h
destination: "peoplewhocare"
`

// has incorrect indentation, which leads to unknown field errors
var invalidRuleExample = `apiVersion: adx-mon.azure.com/v1
kind: AlertRule
Expand Down Expand Up @@ -192,6 +235,11 @@ func TestFromPath(t *testing.T) {
err = os.WriteFile(multiTestfile, []byte(multiRule), 0644)
require.NoError(t, err)

dupeRuleDirectory := t.TempDir()
dupeTestfile := filepath.Join(dupeRuleDirectory, "dupetest.yaml")
err = os.WriteFile(dupeTestfile, []byte(dupeRule), 0644)
require.NoError(t, err)

invalidFileDirectory := t.TempDir()
invalidTestfile := filepath.Join(invalidFileDirectory, "invalid.yaml")
err = os.WriteFile(invalidTestfile, []byte(invalidRuleExample), 0644)
Expand Down Expand Up @@ -259,6 +307,11 @@ func TestFromPath(t *testing.T) {
path: invalidAnnotationFileDirectory,
expectErr: true,
},
{
name: "dupe namespace/name in dir should return error",
path: dupeRuleDirectory,
expectErr: true,
},
}

for _, tc := range testcases {
Expand Down
1 change: 1 addition & 0 deletions alerter/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ func Lint(ctx context.Context, opts *AlerterOpts, path string) error {
if err != nil {
return err
}

logger.Infof("Linting rules from path=%s", path)

lint := NewLinter()
Expand Down

0 comments on commit 51d162c

Please sign in to comment.