@@ -15,7 +15,8 @@ import (
1515)
1616
1717type FakeIPTablesProvider struct {
18- iptables * fakes.IPTablesMock
18+ iptables * fakes.IPTablesMock
19+ iptablesLegacy * fakes.IPTablesLegacyMock
1920}
2021
2122func (c * FakeIPTablesProvider ) GetIPTables () (iptablesClient , error ) {
@@ -26,6 +27,13 @@ func (c *FakeIPTablesProvider) GetIPTables() (iptablesClient, error) {
2627 return c .iptables , nil
2728}
2829
30+ func (c * FakeIPTablesProvider ) GetIPTablesLegacy () iptablesLegacyClient {
31+ if c .iptablesLegacy == nil {
32+ c .iptablesLegacy = & fakes.IPTablesLegacyMock {}
33+ }
34+ return c .iptablesLegacy
35+ }
36+
2937func TestAddSNATRules (t * testing.T ) {
3038 type chainExpectation struct {
3139 table string
@@ -70,8 +78,8 @@ func TestAddSNATRules(t *testing.T) {
7078 chain : SWIFTPOSTROUTING ,
7179 expected : []string {
7280 "-N SWIFT-POSTROUTING" ,
73- "-A SWIFT-POSTROUTING -m addrtype ! --dst-type local -s 240.1.2.0/24 -d " + networkutils .AzureDNS + " -p udp --dport " + strconv .Itoa (iptables .DNSPort ) + " -j SNAT --to 240.1.2.1 " ,
74- "-A SWIFT-POSTROUTING -m addrtype ! --dst-type local -s 240.1.2.0/24 -d " + networkutils .AzureDNS + " -p tcp --dport " + strconv .Itoa (iptables .DNSPort ) + " -j SNAT --to 240.1.2.1 " ,
81+ "-A SWIFT-POSTROUTING -m addrtype ! --dst-type local -s 240.1.2.0/24 -d " + networkutils .AzureDNS + " -p udp --dport " + strconv .Itoa (iptables .DNSPort ) + " -j SNAT --to 10.0.0.4 " ,
82+ "-A SWIFT-POSTROUTING -m addrtype ! --dst-type local -s 240.1.2.0/24 -d " + networkutils .AzureDNS + " -p tcp --dport " + strconv .Itoa (iptables .DNSPort ) + " -j SNAT --to 10.0.0.4 " ,
7583 "-A SWIFT-POSTROUTING -m addrtype ! --dst-type local -s 240.1.2.0/24 -d " + networkutils .AzureIMDS + " -p tcp --dport " + strconv .Itoa (iptables .HTTPPort ) + " -j SNAT --to 10.0.0.4" ,
7684 },
7785 },
@@ -140,8 +148,8 @@ func TestAddSNATRules(t *testing.T) {
140148 chain : SWIFTPOSTROUTING ,
141149 expected : []string {
142150 "-N SWIFT-POSTROUTING" ,
143- "-A SWIFT-POSTROUTING -m addrtype ! --dst-type local -s 240.1.2.0/24 -d " + networkutils .AzureDNS + " -p udp --dport " + strconv .Itoa (iptables .DNSPort ) + " -j SNAT --to 240.1.2.1 " ,
144- "-A SWIFT-POSTROUTING -m addrtype ! --dst-type local -s 240.1.2.0/24 -d " + networkutils .AzureDNS + " -p tcp --dport " + strconv .Itoa (iptables .DNSPort ) + " -j SNAT --to 240.1.2.1 " ,
151+ "-A SWIFT-POSTROUTING -m addrtype ! --dst-type local -s 240.1.2.0/24 -d " + networkutils .AzureDNS + " -p udp --dport " + strconv .Itoa (iptables .DNSPort ) + " -j SNAT --to 10.0.0.4 " ,
152+ "-A SWIFT-POSTROUTING -m addrtype ! --dst-type local -s 240.1.2.0/24 -d " + networkutils .AzureDNS + " -p tcp --dport " + strconv .Itoa (iptables .DNSPort ) + " -j SNAT --to 10.0.0.4 " ,
145153 "-A SWIFT-POSTROUTING -m addrtype ! --dst-type local -s 240.1.2.0/24 -d " + networkutils .AzureIMDS + " -p tcp --dport " + strconv .Itoa (iptables .HTTPPort ) + " -j SNAT --to 10.0.0.4" ,
146154 },
147155 },
@@ -201,15 +209,15 @@ func TestAddSNATRules(t *testing.T) {
201209 chain : SWIFTPOSTROUTING ,
202210 rule : []string {
203211 "-m" , "addrtype" , "!" , "--dst-type" , "local" , "-s" , "240.1.2.0/24" , "-d" , networkutils .AzureDNS ,
204- "-p" , "udp" , "--dport" , strconv .Itoa (iptables .DNSPort ), "-j" , "SNAT" , "--to" , "240.1.2.1 " ,
212+ "-p" , "udp" , "--dport" , strconv .Itoa (iptables .DNSPort ), "-j" , "SNAT" , "--to" , "10.0.0.4 " ,
205213 },
206214 },
207215 {
208216 table : iptables .Nat ,
209217 chain : SWIFTPOSTROUTING ,
210218 rule : []string {
211219 "-m" , "addrtype" , "!" , "--dst-type" , "local" , "-s" , "240.1.2.0/24" , "-d" , networkutils .AzureDNS ,
212- "-p" , "tcp" , "--dport" , strconv .Itoa (iptables .DNSPort ), "-j" , "SNAT" , "--to" , "240.1.2.1 " ,
220+ "-p" , "tcp" , "--dport" , strconv .Itoa (iptables .DNSPort ), "-j" , "SNAT" , "--to" , "10.0.0.4 " ,
213221 },
214222 },
215223 {
@@ -235,8 +243,8 @@ func TestAddSNATRules(t *testing.T) {
235243 chain : SWIFTPOSTROUTING ,
236244 expected : []string {
237245 "-N SWIFT-POSTROUTING" ,
238- "-A SWIFT-POSTROUTING -m addrtype ! --dst-type local -s 240.1.2.0/24 -d " + networkutils .AzureDNS + " -p udp --dport " + strconv .Itoa (iptables .DNSPort ) + " -j SNAT --to 240.1.2.1 " ,
239- "-A SWIFT-POSTROUTING -m addrtype ! --dst-type local -s 240.1.2.0/24 -d " + networkutils .AzureDNS + " -p tcp --dport " + strconv .Itoa (iptables .DNSPort ) + " -j SNAT --to 240.1.2.1 " ,
246+ "-A SWIFT-POSTROUTING -m addrtype ! --dst-type local -s 240.1.2.0/24 -d " + networkutils .AzureDNS + " -p udp --dport " + strconv .Itoa (iptables .DNSPort ) + " -j SNAT --to 10.0.0.4 " ,
247+ "-A SWIFT-POSTROUTING -m addrtype ! --dst-type local -s 240.1.2.0/24 -d " + networkutils .AzureDNS + " -p tcp --dport " + strconv .Itoa (iptables .DNSPort ) + " -j SNAT --to 10.0.0.4 " ,
240248 "-A SWIFT-POSTROUTING -m addrtype ! --dst-type local -s 240.1.2.0/24 -d " + networkutils .AzureIMDS + " -p tcp --dport " + strconv .Itoa (iptables .HTTPPort ) + " -j SNAT --to 10.0.0.4" ,
241249 },
242250 },
@@ -307,8 +315,10 @@ func TestAddSNATRules(t *testing.T) {
307315 t .Run (tt .name , func (t * testing.T ) {
308316 service := getTestService (cns .KubernetesCRD )
309317 ipt := fakes .NewIPTablesMock ()
318+ iptl := & fakes.IPTablesLegacyMock {}
310319 service .iptables = & FakeIPTablesProvider {
311- iptables : ipt ,
320+ iptables : ipt ,
321+ iptablesLegacy : iptl ,
312322 }
313323
314324 // setup pre-existing rules
@@ -360,6 +370,12 @@ func TestAddSNATRules(t *testing.T) {
360370 if actualClearChainCalls != tt .expectedClearChainCalls {
361371 t .Fatalf ("ClearChain call count mismatch: got %d, expected %d" , actualClearChainCalls , tt .expectedClearChainCalls )
362372 }
373+
374+ // verify we delete legacy swift postrouting jump
375+ actualLegacyDeleteCalls := iptl .DeleteCallCount ()
376+ if actualLegacyDeleteCalls != 1 {
377+ t .Fatalf ("Delete call count mismatch: got %d, expected 1" , actualLegacyDeleteCalls )
378+ }
363379 })
364380 }
365381}
0 commit comments