Skip to content

Commit

Permalink
[management] Fix policy tests (#3135)
Browse files Browse the repository at this point in the history
- Add firewall rule isEqual method
- Fix tests
  • Loading branch information
mlsmaycon authored Dec 31, 2024
1 parent 18b049c commit 03fd656
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
16 changes: 11 additions & 5 deletions management/server/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestAccount_getPeersByPolicy(t *testing.T) {
},
"GroupWorkstations": {
ID: "GroupWorkstations",
Name: "All",
Name: "GroupWorkstations",
Peers: []string{
"peerB",
"peerA",
Expand Down Expand Up @@ -280,10 +280,16 @@ func TestAccount_getPeersByPolicy(t *testing.T) {
},
}
assert.Len(t, firewallRules, len(epectedFirewallRules))
slices.SortFunc(epectedFirewallRules, sortFunc())
slices.SortFunc(firewallRules, sortFunc())
for i := range firewallRules {
assert.Equal(t, epectedFirewallRules[i], firewallRules[i])

for _, rule := range firewallRules {
contains := false
for _, expectedRule := range epectedFirewallRules {
if rule.IsEqual(expectedRule) {
contains = true
break
}
}
assert.True(t, contains, "rule not found in expected rules %#v", rule)
}
})
}
Expand Down
9 changes: 9 additions & 0 deletions management/server/types/firewall_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ type FirewallRule struct {
Port string
}

// IsEqual checks if two firewall rules are equal.
func (r *FirewallRule) IsEqual(other *FirewallRule) bool {
return r.PeerIP == other.PeerIP &&
r.Direction == other.Direction &&
r.Action == other.Action &&
r.Protocol == other.Protocol &&
r.Port == other.Port
}

// generateRouteFirewallRules generates a list of firewall rules for a given route.
func generateRouteFirewallRules(ctx context.Context, route *nbroute.Route, rule *PolicyRule, groupPeers []*nbpeer.Peer, direction int) []*RouteFirewallRule {
rulesExists := make(map[string]struct{})
Expand Down

0 comments on commit 03fd656

Please sign in to comment.