Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions felix/fv/nat_outgoing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,47 @@ var _ = infrastructure.DatastoreDescribe("NATOutgoing rule rendering test", []ap
}
})
})

var _ = infrastructure.DatastoreDescribe("NATPortRange rendering test", []apiconfig.DatastoreType{apiconfig.EtcdV3, apiconfig.Kubernetes}, func(getInfra infrastructure.InfraFactory) {
var (
infra infrastructure.DatastoreInfra
tc infrastructure.TopologyContainers
client client.Interface
)

BeforeEach(func() {
var err error
infra = getInfra()

opts := infrastructure.DefaultTopologyOptions()
opts.IPIPMode = api.IPIPModeNever
opts.EnableIPv6 = true

opts.ExtraEnvVars = map[string]string{
"FELIX_NATPortRange": "32768:65535",
}
tc, client = infrastructure.StartSingleNodeTopology(opts, infra)

ctx := context.Background()
ippool := api.NewIPPool()
ippool.Name = "nat-pool"
ippool.Spec.CIDR = "10.244.255.0/24"
ippool.Spec.NATOutgoing = true
ippool, err = client.IPPools().Create(ctx, ippool, options.SetOptions{})
Expect(err).NotTo(HaveOccurred())
})

It("should have expected rendering", func() {
if NFTMode() {
Eventually(func() string {
output, _ := tc.Felixes[0].ExecOutput("nft", "list", "chain", "ip", "calico", "nat-cali-nat-outgoing")
return output
}, 5*time.Second, 100*time.Millisecond).Should(ContainSubstring("32768-65535"))
} else {
Eventually(func() string {
output, _ := tc.Felixes[0].ExecOutput("iptables-save", "-t", "nat")
return output
}, 5*time.Second, 100*time.Millisecond).Should(ContainSubstring("32768-65535"))
}
})
})
2 changes: 2 additions & 0 deletions felix/iptables/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ var _ = DescribeTable("Actions",
Entry("SNATAction", environment.Features{}, SNATAction{ToAddr: "10.0.0.1"}, "--jump SNAT --to-source 10.0.0.1"),
Entry("SNATAction fully random", environment.Features{SNATFullyRandom: true}, SNATAction{ToAddr: "10.0.0.1"}, "--jump SNAT --to-source 10.0.0.1 --random-fully"),
Entry("MasqAction", environment.Features{}, MasqAction{}, "--jump MASQUERADE"),
Entry("MasqAction", environment.Features{}, MasqAction{ToPorts: "32768-65535"}, "--jump MASQUERADE --to-ports 32768-65535"),
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test entry name "MasqAction" is duplicated on lines 39, 40, and 41. While this follows an existing pattern in this file, Ginkgo test entries should have unique names for better test identification and reporting. Consider using more descriptive names like "MasqAction with ToPorts", "MasqAction fully random", etc.

Copilot uses AI. Check for mistakes.
Entry("MasqAction", environment.Features{MASQFullyRandom: true}, MasqAction{}, "--jump MASQUERADE --random-fully"),
Comment on lines +40 to 41
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MasqAction implementation supports both ToPorts and MASQFullyRandom features simultaneously (line 313 in actions.go appends fullyRand even when ToPorts is set), but there is no test case covering this combination. Consider adding a test case like: Entry("MasqAction with ToPorts and fully random", environment.Features{MASQFullyRandom: true}, MasqAction{ToPorts: "32768-65535"}, "--jump MASQUERADE --to-ports 32768-65535 --random-fully")

Copilot uses AI. Check for mistakes.
Entry("MasqAction", environment.Features{MASQFullyRandom: true}, MasqAction{ToPorts: "32768-65535"}, "--jump MASQUERADE --to-ports 32768-65535 --random-fully"),
Entry("ClearMarkAction", environment.Features{}, ClearMarkAction{Mark: 0x1000}, "--jump MARK --set-mark 0/0x1000"),
Entry("SetMarkAction", environment.Features{}, SetMarkAction{Mark: 0x1000}, "--jump MARK --set-mark 0x1000/0x1000"),
Entry("SetMaskedMarkAction", environment.Features{}, SetMaskedMarkAction{
Expand Down
2 changes: 1 addition & 1 deletion felix/nftables/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func (g MasqAction) ToFragment(features *environment.Features) string {
}
if g.ToPorts != "" {
// e.g., masquerade to :1024-65535
return fmt.Sprintf("masquerade to %s"+fullyRand, g.ToPorts)
return fmt.Sprintf("masquerade to :%s"+fullyRand, g.ToPorts)
}
return "masquerade" + fullyRand
}
Expand Down
2 changes: 2 additions & 0 deletions felix/nftables/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ var _ = DescribeTable("Actions",
Entry("SNATAction", environment.Features{}, SNATAction{ToAddr: "10.0.0.1"}, "snat to 10.0.0.1"),
Entry("SNATAction fully random", environment.Features{SNATFullyRandom: true}, SNATAction{ToAddr: "10.0.0.1"}, "snat to 10.0.0.1 fully-random"),
Entry("MasqAction", environment.Features{}, MasqAction{}, "masquerade"),
Entry("MasqAction", environment.Features{}, MasqAction{ToPorts: "32768-65535"}, "masquerade to :32768-65535"),
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test entry name "MasqAction" is duplicated on lines 39, 40, and 41. While this follows an existing pattern in this file, Ginkgo test entries should have unique names for better test identification and reporting. Consider using more descriptive names like "MasqAction with ToPorts", "MasqAction fully random", etc.

Copilot uses AI. Check for mistakes.
Entry("MasqAction", environment.Features{MASQFullyRandom: true}, MasqAction{}, "masquerade fully-random"),
Comment on lines +40 to 41
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MasqAction implementation supports both ToPorts and MASQFullyRandom features simultaneously (line 332 in actions.go appends fullyRand even when ToPorts is set), but there is no test case covering this combination. Consider adding a test case like: Entry("MasqAction with ToPorts and fully random", environment.Features{MASQFullyRandom: true}, MasqAction{ToPorts: "32768-65535"}, "masquerade to :32768-65535 fully-random")

Copilot uses AI. Check for mistakes.
Entry("MasqAction", environment.Features{MASQFullyRandom: true}, MasqAction{ToPorts: "32768-65535"}, "masquerade to :32768-65535 fully-random"),
Entry("ClearMarkAction", environment.Features{}, ClearMarkAction{Mark: 0x1000}, "meta mark set mark & 0xffffefff"),
Entry("SetMarkAction", environment.Features{}, SetMarkAction{Mark: 0x1000}, "meta mark set mark or 0x1000"),
Entry("SetMaskedMarkAction", environment.Features{}, SetMaskedMarkAction{Mark: 0x1000, Mask: 0xf000}, "meta mark set mark & 0xffff0fff ^ 0x1000"),
Expand Down