Skip to content

integration: Add tests for port mappings#48545

Merged
thaJeztah merged 3 commits intomoby:masterfrom
akerouanton:integration-networking-port-mapper
Oct 7, 2024
Merged

integration: Add tests for port mappings#48545
thaJeztah merged 3 commits intomoby:masterfrom
akerouanton:integration-networking-port-mapper

Conversation

@akerouanton
Copy link
Member

@akerouanton akerouanton commented Sep 24, 2024

- What I did

Introduce new integration tests that ensure port mappings are correctly accessible from the host, and from a remote host. This is another attempt at upstreaming some of the tests introduced in:

These tests should help ensure nothing breaks when we'll modify iptables rules, or when we'll add nftables support.

- How I did it

I added a new L3Segment struct that simulates (using netns) a LAN segment where arbitrary Hosts live. That's what makes TestAccessPublishedPortFromRemoteHost possible.

That specific test simulates two hosts: one is the 'current' netns where the daemon-under-test and the tests themselves are executed. The other is the 'remote host'. Each host gets a pair of veth. A third network namespace is used to isolate the bridge from any network programming the engine does.

To create all these objects (netns, veth, etc...), simple iproute2 invocations are made. The previous attempt was using netlink calls, which was an horrendously bad idea since there was no way to recreate the same env for manual debugging. All iproute2 invocations are logged, so now it's just a matter of copy / pasting logs into a shell script, tidying them up a bit, and then running the script.

The L3Segment struct has one important method: AddHost.

And Host has two important methods:

  • Run, which runs a command in the host's netns. That command is logged too.
  • Do, which runs a callback in the host's netns.

Both Hosts and L3Segment can take any number of IP addrs, making it easy to test v4/v6-only, as well as, dual-stack scenarios in one go.

This should be enough to cover all use-cases.

- How to verify it

CI is green.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
@akerouanton akerouanton added this to the 28.0.0 milestone Sep 24, 2024
@akerouanton akerouanton self-assigned this Sep 24, 2024
@akerouanton akerouanton changed the title Integration networking port mapper Add integration tests for port mappings Sep 24, 2024
serverID := container.Run(ctx, t, c,
container.WithName(sanitizeCtrName(t.Name()+"-server")),
container.WithExposedPorts("80/tcp"),
container.WithPortMap(nat.PortMap{"80/tcp": {{HostPort: serverPort}}}),
Copy link
Contributor

@robmry robmry Sep 24, 2024

Choose a reason for hiding this comment

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

As the mapping doesn't include a HostIP, the mapping should work via any host address (apart from --userland-proxy=false --ipv6=false, which will be IPv4 only).

If that's the intention, perhaps it'd be worth checking access via each of the host addresses in each test... dropping the last component of the test-config so that, for each combo of userland-proxy and IPv6-enabled, there's an http request to each of eth0 and lo's v4 and v6 addresses.

But it might also be worth testing that, with a host address in the port mapping, it's not possible to access the http server via other host addresses?

@akerouanton akerouanton removed the request for review from corhere September 24, 2024 21:27
@akerouanton akerouanton force-pushed the integration-networking-port-mapper branch 3 times, most recently from 2bb254f to dfafb97 Compare September 25, 2024 14:14
@akerouanton akerouanton marked this pull request as draft September 25, 2024 17:32
@akerouanton akerouanton force-pushed the integration-networking-port-mapper branch 15 times, most recently from d1b45d4 to 09df741 Compare September 27, 2024 14:05
@akerouanton akerouanton force-pushed the integration-networking-port-mapper branch 7 times, most recently from f885f18 to d3e2a59 Compare September 30, 2024 13:36
@akerouanton akerouanton marked this pull request as ready for review September 30, 2024 14:34
@akerouanton akerouanton changed the title Add integration tests for port mappings integration: Add tests for port mappings Sep 30, 2024
Copy link
Member

@neersighted neersighted left a comment

Choose a reason for hiding this comment

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

LGTM overall with the exception of @thaJeztah's concern about sysctls, which he beat me to.

@akerouanton akerouanton force-pushed the integration-networking-port-mapper branch from d3e2a59 to ef52cdd Compare October 4, 2024 16:46
Signed-off-by: Albin Kerouanton <albinker@gmail.com>
Along with this new test, a new struct `L3Segment` is introduced to
simulate hosts connected on a same switched network. This struct will
let us test various scenarios where published ports and containers
should or should not be accessible from remote hosts.

The new test introduced, `TestAccessPublishedPortFromRemoteHost`, skips
link-local address as port publishing doesn't work on those addresses
currently. This will be fixed in a future commit.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
@akerouanton akerouanton force-pushed the integration-networking-port-mapper branch from ef52cdd to 5875b6e Compare October 4, 2024 18:09
Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

LGTM

@thaJeztah thaJeztah merged commit 2d049d1 into moby:master Oct 7, 2024
@akerouanton akerouanton deleted the integration-networking-port-mapper branch October 7, 2024 13:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants