Skip to content

Add TestNftabler - #50180

Merged
thaJeztah merged 2 commits into
moby:masterfrom
robmry:test_nftabler
Jun 17, 2025
Merged

thaJeztah merged 2 commits into
moby:masterfrom
robmry:test_nftabler

Conversation

@robmry

@robmry robmry commented Jun 12, 2025 •

Copy link
Copy Markdown
Contributor

- What I did

Add TestNftabler - nftables equivalent to TestIptabler ... it runs the nftabler with fairly exhaustive combinations of options (not all of which make sense, but this checks there's nothing crashy).

There are lots of results files ... and not much in them yet (because the nftabler is just an outline so far). But, for upcoming changes as nftables rules are added, it'll show the effect.

- How I did it

- How to verify it

- Human readable description for the release notes

@robmry robmry added this to the 29.0.0 milestone Jun 12, 2025
@robmry robmry self-assigned this Jun 12, 2025
@robmry robmry added kind/enhancement Enhancements are not bugs or new features but can improve usability or performance. area/networking Networking area/testing area/networking/firewalling Networking labels Jun 12, 2025
@robmry
robmry marked this pull request as ready for review June 17, 2025 10:14
@robmry
robmry requested review from akerouanton and corhere June 17, 2025 10:15

@thaJeztah thaJeztah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

Comment on lines +150 to +151
// Disable undoes Enable. Intended for unit testing.
func Disable() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wish there was a way to share _test.go field between tests. I guess some if testing.Testing() would come closest to prevent it being used for other purposes, but of course would still compile the code.

Well, "meh" it's an internal package, so should not be an issue.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I hadn't thought about test.Testing() ... could do that, but it might lead to unpleasant surprises when a test works but production code does something different?

Exporting the function seemed better than duplicating it in another _test.go, but not really ideal. As you say though, it's an internal package.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, not worth changing; if we'd use the test.Testing() approach, we'd indeed need some error return to make it not ignored silently

@corhere corhere Jun 17, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if !testing.Testing() {
    panic(...)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Heh, yeah, I considered panic, but then thought "not worth it for an internal package".

Comment on lines +23 to +38
const (
ipv4 int64 = iota
ipv6
hairpin
internal
icc
masq
snat
bindLocalhost
wsl2Mirrored
numBoolParams
)
nftables.Enable()
t.Cleanup(func() { nftables.Disable() }) // Cleanup instead of defer, this func returns before the parallel subtests finish.
for i := range 1 << numBoolParams {
p := func(n int64) bool { return (i & (1 << n)) != 0 }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
const (
ipv4 int64 = iota
ipv6
hairpin
internal
icc
masq
snat
bindLocalhost
wsl2Mirrored
numBoolParams
)
nftables.Enable()
t.Cleanup(func() { nftables.Disable() }) // Cleanup instead of defer, this func returns before the parallel subtests finish.
for i := range 1 << numBoolParams {
p := func(n int64) bool { return (i & (1 << n)) != 0 }
const (
ipv4 int64 = 1 << iota
ipv6
hairpin
internal
icc
masq
snat
bindLocalhost
wsl2Mirrored
numBoolParams = iota
)
nftables.Enable()
t.Cleanup(func() { nftables.Disable() }) // Cleanup instead of defer, this func returns before the parallel subtests finish.
for i := range 1 << numBoolParams {
p := func(n int64) bool { return (i & n) == n }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Signed-off-by: Rob Murray <[email protected]>

@thaJeztah thaJeztah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

still LGTM, thanks!

@thaJeztah
thaJeztah merged commit b0d6155 into moby:master Jun 17, 2025
@robmry
robmry deleted the test_nftabler branch July 21, 2025 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/networking/firewalling Networking area/networking Networking area/testing kind/enhancement Enhancements are not bugs or new features but can improve usability or performance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants