@@ -24,27 +24,65 @@ func TestParseIPTableRules(t *testing.T) {
2424 testCases := []struct {
2525 name string
2626 rules []string
27- want []uint64
27+ want []PortRule
2828 }{
2929 {
3030 name : "Empty input" ,
3131 rules : []string {},
32- want : []uint64 {},
32+ want : []PortRule {},
3333 },
3434 {
3535 name : "Single rule with single port" ,
3636 rules : []string {
3737 "-A CNI-HOSTPORT-DNAT -p tcp -m comment --comment \" dnat name: \" bridge\" id: \" some-id\" \" -m multiport --dports 8080 -j CNI-DN-some-hash" ,
3838 },
39- want : []uint64 { 8080 },
39+ want : []PortRule {{ IP : "" , Port : 8080 } },
4040 },
4141 {
4242 name : "Multiple rules with multiple ports" ,
4343 rules : []string {
4444 "-A CNI-HOSTPORT-DNAT -p tcp -m comment --comment \" dnat name: \" bridge\" id: \" some-id\" \" -m multiport --dports 8080 -j CNI-DN-some-hash" ,
4545 "-A CNI-HOSTPORT-DNAT -p tcp -m comment --comment \" dnat name: \" bridge\" id: \" some-id\" \" -m multiport --dports 9090 -j CNI-DN-some-hash" ,
4646 },
47- want : []uint64 {8080 , 9090 },
47+ want : []PortRule {
48+ {IP : "" , Port : 8080 },
49+ {IP : "" , Port : 9090 },
50+ },
51+ },
52+ {
53+ name : "Single rule with comma-separated ports" ,
54+ rules : []string {
55+ "-A CNI-HOSTPORT-DNAT -p tcp -m comment --comment \" dnat name: \" bridge\" id: \" some-id\" \" -m multiport --dports 8080,9090 -j CNI-DN-some-hash" ,
56+ },
57+ want : []PortRule {
58+ {IP : "" , Port : 8080 },
59+ {IP : "" , Port : 9090 },
60+ },
61+ },
62+ {
63+ name : "Sub-chain DNAT rule with destination IP" ,
64+ rules : []string {
65+ "-A CNI-DN-some-hash -d 192.168.1.141/32 -p tcp -m tcp --dport 80 -j DNAT --to-destination 10.4.0.2:80" ,
66+ },
67+ want : []PortRule {{IP : "192.168.1.141" , Port : 80 }},
68+ },
69+ {
70+ name : "Multiple sub-chain rules with different IPs same port" ,
71+ rules : []string {
72+ "-A CNI-DN-hash1 -d 192.168.1.141/32 -p tcp -m tcp --dport 80 -j DNAT --to-destination 10.4.0.2:80" ,
73+ "-A CNI-DN-hash2 -d 192.168.1.142/32 -p tcp -m tcp --dport 80 -j DNAT --to-destination 10.4.0.3:80" ,
74+ },
75+ want : []PortRule {
76+ {IP : "192.168.1.141" , Port : 80 },
77+ {IP : "192.168.1.142" , Port : 80 },
78+ },
79+ },
80+ {
81+ name : "Sub-chain rule without CIDR suffix" ,
82+ rules : []string {
83+ "-A CNI-DN-hash1 -d 10.0.0.1 -p tcp -m tcp --dport 443 -j DNAT --to-destination 10.4.0.2:443" ,
84+ },
85+ want : []PortRule {{IP : "10.0.0.1" , Port : 443 }},
4886 },
4987 }
5088
@@ -58,12 +96,12 @@ func TestParseIPTableRules(t *testing.T) {
5896 }
5997}
6098
61- func equal (a , b []uint64 ) bool {
99+ func equal (a , b []PortRule ) bool {
62100 if len (a ) != len (b ) {
63101 return false
64102 }
65- for i , v := range a {
66- if v != b [i ] {
103+ for i := range a {
104+ if a [ i ] != b [i ] {
67105 return false
68106 }
69107 }
0 commit comments