Add the route ID to uuid (#3372)

* Add the route ID to uuid

Signed-off-by: Yijie Qin <qinyijie@amazon.com>
---------

Signed-off-by: Yijie Qin <qinyijie@amazon.com>
This commit is contained in:
Yijie Qin 2023-09-29 10:07:52 -04:00 committed by GitHub
parent c6be0bcabf
commit 57fdd136ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 0 deletions

View File

@ -180,6 +180,29 @@ func (r *Route) Key() string {
return b.String()
}
// ID returns a unique identifier for the route.
func (r *Route) ID() string {
b := strings.Builder{}
position := -1
if r.parent != nil {
// Find the position in the same level leaf.
for i, cr := range r.parent.Routes {
if cr == r {
position = i
break
}
}
}
b.WriteString(r.Key())
if position > -1 {
b.WriteRune('/')
b.WriteString(fmt.Sprint(position))
}
return b.String()
}
// Walk traverses the route tree in depth-first order.
func (r *Route) Walk(visit func(*Route)) {
visit(r)

View File

@ -853,3 +853,71 @@ routes:
}
}
}
func TestRouteID(t *testing.T) {
in := `
receiver: 'notify-def'
routes:
- matchers: ['{owner="team-A"}', '{level!="critical"}']
receiver: 'notify-D'
group_by: [...]
continue: true
- matchers: ['{owner="team-A"}', '{level!="critical"}']
receiver: 'notify-A'
routes:
- matchers: ['{env="testing"}', '{baz!~".*quux"}']
receiver: 'notify-testing'
group_by: [...]
- match:
env: "production"
receiver: 'notify-productionA'
group_wait: 1m
continue: true
- matchers: [ env=~"produ.*", job=~".*"]
receiver: 'notify-productionB'
group_wait: 30s
group_interval: 5m
repeat_interval: 1h
group_by: ['job']
- match_re:
owner: 'team-(B|C)'
group_by: ['foo', 'bar']
group_wait: 2m
receiver: 'notify-BC'
- matchers: [group_by="role"]
group_by: ['role']
routes:
- matchers: ['{env="testing"}']
receiver: 'notify-testing'
routes:
- matchers: [wait="long"]
group_wait: 2m
`
var ctree config.Route
if err := yaml.UnmarshalStrict([]byte(in), &ctree); err != nil {
t.Fatal(err)
}
tree := NewRoute(&ctree, nil)
expected := []string{
"{}",
"{}/{level!=\"critical\",owner=\"team-A\"}/0",
"{}/{level!=\"critical\",owner=\"team-A\"}/1",
"{}/{level!=\"critical\",owner=\"team-A\"}/{baz!~\".*quux\",env=\"testing\"}/0",
"{}/{level!=\"critical\",owner=\"team-A\"}/{env=\"production\"}/1",
"{}/{level!=\"critical\",owner=\"team-A\"}/{env=~\"produ.*\",job=~\".*\"}/2",
"{}/{owner=~\"^(?:team-(B|C))$\"}/2",
"{}/{group_by=\"role\"}/3",
"{}/{group_by=\"role\"}/{env=\"testing\"}/0",
"{}/{group_by=\"role\"}/{env=\"testing\"}/{wait=\"long\"}/0",
}
var got []string
tree.Walk(func(r *Route) {
got = append(got, r.ID())
})
require.ElementsMatch(t, got, expected)
}