Rule.String() method returns .Dst

- Return `to <all | ipNetString>` in string
- Return `all` for `from` if `rule.Src == nil`
- Return `all` for `to` if `rule.Dst == nil`

Add unit test
This commit is contained in:
Quentin McGaw 2021-08-26 06:44:53 -07:00 committed by Alessandro Boch
parent 65349edba8
commit 30ec08bc3d
2 changed files with 57 additions and 1 deletions

13
rule.go
View File

@ -28,7 +28,18 @@ type Rule struct {
}
func (r Rule) String() string {
return fmt.Sprintf("ip rule %d: from %s table %d", r.Priority, r.Src, r.Table)
from := "all"
if r.Src != nil && r.Src.String() != "<nil>" {
from = r.Src.String()
}
to := "all"
if r.Dst != nil && r.Dst.String() != "<nil>" {
to = r.Dst.String()
}
return fmt.Sprintf("ip rule %d: from %s to %s table %d",
r.Priority, from, to, r.Table)
}
// NewRule return empty rules.

View File

@ -1,3 +1,4 @@
//go:build linux
// +build linux
package netlink
@ -352,6 +353,50 @@ func runRuleListFiltered(t *testing.T, family int, srcNet, dstNet *net.IPNet) {
}
}
func TestRuleString(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
r Rule
s string
}{
"empty rule": {
s: "ip rule 0: from all to all table 0",
},
"rule with src and dst equivalent to <nil>": {
r: Rule{
Priority: 100,
Src: &net.IPNet{IP: net.IPv4(10, 0, 0, 0)},
Dst: &net.IPNet{IP: net.IPv4(20, 0, 0, 0)},
Table: 99,
},
s: "ip rule 100: from all to all table 99",
},
"rule with src and dst": {
r: Rule{
Priority: 100,
Src: &net.IPNet{IP: net.IPv4(10, 0, 0, 0), Mask: net.IPv4Mask(255, 255, 255, 0)},
Dst: &net.IPNet{IP: net.IPv4(20, 0, 0, 0), Mask: net.IPv4Mask(255, 255, 255, 0)},
Table: 99,
},
s: "ip rule 100: from 10.0.0.0/24 to 20.0.0.0/24 table 99",
},
}
for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()
s := testCase.r.String()
if s != testCase.s {
t.Errorf("expected %q but got %q", testCase.s, s)
}
})
}
}
func ruleExists(rules []Rule, rule Rule) bool {
for i := range rules {
if ruleEquals(rules[i], rule) {