1515package ip
1616
1717import (
18+ "net"
1819 "strings"
1920 "testing"
2021
@@ -31,43 +32,48 @@ func Test_setupIPMasqNFTables(t *testing.T) {
3132 network string
3233 ifname string
3334 containerID string
34- addr string
35+ addrs [] string
3536 }{
3637 {
3738 network : "unit-test" ,
3839 ifname : "eth0" ,
3940 containerID : "one" ,
40- addr : "192.168.1.1/24" ,
41+ addrs : [] string { "192.168.1.1/24" } ,
4142 },
4243 {
4344 network : "unit-test" ,
4445 ifname : "eth0" ,
4546 containerID : "two" ,
46- addr : "192.168.1.2/24" ,
47+ addrs : [] string { "192.168.1.2/24" , "2001:db8::2/64" } ,
4748 },
4849 {
4950 network : "unit-test" ,
5051 ifname : "eth0" ,
5152 containerID : "three" ,
52- addr : "192.168.99.5/24" ,
53+ addrs : [] string { "192.168.99.5/24" } ,
5354 },
5455 {
5556 network : "alternate" ,
5657 ifname : "net1" ,
5758 containerID : "three" ,
58- addr : "10.0.0.5/24" ,
59+ addrs : [] string { "10.0.0.5/24" , "2001:db8::5/64" } ,
5960 },
6061 }
6162
6263 for _ , c := range containers {
63- addr , err := netlink .ParseAddr (c .addr )
64- if err != nil {
65- t .Fatalf ("failed to parse test addr: %v" , err )
64+ ipns := []* net.IPNet {}
65+ for _ , addr := range c .addrs {
66+ nladdr , err := netlink .ParseAddr (addr )
67+ if err != nil {
68+ t .Fatalf ("failed to parse test addr: %v" , err )
69+ }
70+ ipns = append (ipns , nladdr .IPNet )
6671 }
67- err = setupIPMasqNFTablesWithInterface (nft , addr . IPNet , c .network , c .ifname , c .containerID )
72+ err : = setupIPMasqNFTablesWithInterface (nft , ipns , c .network , c .ifname , c .containerID )
6873 if err != nil {
6974 t .Fatalf ("error from setupIPMasqNFTables: %v" , err )
7075 }
76+
7177 }
7278
7379 expected := strings .TrimSpace (`
@@ -76,8 +82,10 @@ add chain inet cni_plugins_masquerade masq_checks { comment "Masquerade traffic
7682add chain inet cni_plugins_masquerade postrouting { type nat hook postrouting priority 100 ; }
7783add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.1.1 ip daddr != 192.168.1.0/24 masquerade comment "6fd94d501e58f0aa-287fc69eff0574a2, net: unit-test, if: eth0, id: one"
7884add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.1.2 ip daddr != 192.168.1.0/24 masquerade comment "6fd94d501e58f0aa-d750b2c8f0f25d5f, net: unit-test, if: eth0, id: two"
85+ add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::2 ip6 daddr != 2001:db8::/64 masquerade comment "6fd94d501e58f0aa-d750b2c8f0f25d5f, net: unit-test, if: eth0, id: two"
7986add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.99.5 ip daddr != 192.168.99.0/24 masquerade comment "6fd94d501e58f0aa-a4d4adb82b669cfe, net: unit-test, if: eth0, id: three"
8087add rule inet cni_plugins_masquerade masq_checks ip saddr == 10.0.0.5 ip daddr != 10.0.0.0/24 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
88+ add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::5 ip6 daddr != 2001:db8::/64 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
8189add rule inet cni_plugins_masquerade postrouting ip daddr == 224.0.0.0/4 return
8290add rule inet cni_plugins_masquerade postrouting ip6 daddr == ff00::/8 return
8391add rule inet cni_plugins_masquerade postrouting goto masq_checks
@@ -88,22 +96,18 @@ add rule inet cni_plugins_masquerade postrouting goto masq_checks
8896 }
8997
9098 // Add a new container reusing "one"'s address, before deleting "one"
91- addr , err := netlink .ParseAddr (containers [0 ].addr )
99+ c := containers [0 ]
100+ addr , err := netlink .ParseAddr (c .addrs [0 ])
92101 if err != nil {
93102 t .Fatalf ("failed to parse test addr: %v" , err )
94103 }
95- err = setupIPMasqNFTablesWithInterface (nft , addr .IPNet , "unit-test" , "eth0" , "four" )
104+ err = setupIPMasqNFTablesWithInterface (nft , [] * net. IPNet { addr .IPNet } , "unit-test" , "eth0" , "four" )
96105 if err != nil {
97106 t .Fatalf ("error from setupIPMasqNFTables: %v" , err )
98107 }
99108
100109 // Remove "one"
101- c := containers [0 ]
102- addr , err = netlink .ParseAddr (c .addr )
103- if err != nil {
104- t .Fatalf ("failed to parse test addr: %v" , err )
105- }
106- err = teardownIPMasqNFTablesWithInterface (nft , addr .IPNet , c .network , c .ifname , c .containerID )
110+ err = teardownIPMasqNFTablesWithInterface (nft , []* net.IPNet {addr .IPNet }, c .network , c .ifname , c .containerID )
107111 if err != nil {
108112 t .Fatalf ("error from teardownIPMasqNFTables: %v" , err )
109113 }
@@ -114,8 +118,10 @@ add table inet cni_plugins_masquerade { comment "Masquerading for plugins from g
114118add chain inet cni_plugins_masquerade masq_checks { comment "Masquerade traffic from certain IPs to any (non-multicast) IP outside their subnet" ; }
115119add chain inet cni_plugins_masquerade postrouting { type nat hook postrouting priority 100 ; }
116120add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.1.2 ip daddr != 192.168.1.0/24 masquerade comment "6fd94d501e58f0aa-d750b2c8f0f25d5f, net: unit-test, if: eth0, id: two"
121+ add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::2 ip6 daddr != 2001:db8::/64 masquerade comment "6fd94d501e58f0aa-d750b2c8f0f25d5f, net: unit-test, if: eth0, id: two"
117122add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.99.5 ip daddr != 192.168.99.0/24 masquerade comment "6fd94d501e58f0aa-a4d4adb82b669cfe, net: unit-test, if: eth0, id: three"
118123add rule inet cni_plugins_masquerade masq_checks ip saddr == 10.0.0.5 ip daddr != 10.0.0.0/24 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
124+ add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::5 ip6 daddr != 2001:db8::/64 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
119125add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.1.1 ip daddr != 192.168.1.0/24 masquerade comment "6fd94d501e58f0aa-e766de567ef6c543, net: unit-test, if: eth0, id: four"
120126add rule inet cni_plugins_masquerade postrouting ip daddr == 224.0.0.0/4 return
121127add rule inet cni_plugins_masquerade postrouting ip6 daddr == ff00::/8 return
@@ -150,8 +156,10 @@ add table inet cni_plugins_masquerade { comment "Masquerading for plugins from g
150156add chain inet cni_plugins_masquerade masq_checks { comment "Masquerade traffic from certain IPs to any (non-multicast) IP outside their subnet" ; }
151157add chain inet cni_plugins_masquerade postrouting { type nat hook postrouting priority 100 ; }
152158add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.1.2 ip daddr != 192.168.1.0/24 masquerade comment "6fd94d501e58f0aa-d750b2c8f0f25d5f, net: unit-test, if: eth0, id: two"
159+ add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::2 ip6 daddr != 2001:db8::/64 masquerade comment "6fd94d501e58f0aa-d750b2c8f0f25d5f, net: unit-test, if: eth0, id: two"
153160add rule inet cni_plugins_masquerade masq_checks ip saddr == 192.168.99.5 ip daddr != 192.168.99.0/24 masquerade comment "6fd94d501e58f0aa-a4d4adb82b669cfe, net: unit-test, if: eth0, id: three"
154161add rule inet cni_plugins_masquerade masq_checks ip saddr == 10.0.0.5 ip daddr != 10.0.0.0/24 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
162+ add rule inet cni_plugins_masquerade masq_checks ip6 saddr == 2001:db8::5 ip6 daddr != 2001:db8::/64 masquerade comment "82783ef24bdc7036-acb19d111858e348, net: alternate, if: net1, id: three"
155163add rule inet cni_plugins_masquerade postrouting ip daddr == 224.0.0.0/4 return
156164add rule inet cni_plugins_masquerade postrouting ip6 daddr == ff00::/8 return
157165add rule inet cni_plugins_masquerade postrouting goto masq_checks
0 commit comments