|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package container |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "strconv" |
| 23 | + "testing" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/containerd/nerdctl/mod/tigron/expect" |
| 27 | + "github.com/containerd/nerdctl/mod/tigron/require" |
| 28 | + "github.com/containerd/nerdctl/mod/tigron/test" |
| 29 | + |
| 30 | + "github.com/containerd/nerdctl/v2/pkg/rootlessutil" |
| 31 | + "github.com/containerd/nerdctl/v2/pkg/testutil" |
| 32 | + "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest" |
| 33 | +) |
| 34 | + |
| 35 | +// iptablesCheckCommand is the shell command to check iptables rules |
| 36 | +const iptablesCheckCommand = "iptables -t nat -S && iptables -t filter -S && iptables -t mangle -S" |
| 37 | + |
| 38 | +// testContainerRmIptablesExecutor is a common executor function for testing iptables rules cleanup |
| 39 | +func testContainerRmIptablesExecutor(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 40 | + t := helpers.T() |
| 41 | + |
| 42 | + // Get the actual container ID from the container name and store it in a label |
| 43 | + containerID := data.Labels().Get("containerID") |
| 44 | + if containerID == "" { |
| 45 | + container := nerdtest.InspectContainer(helpers, data.Identifier()) |
| 46 | + containerID = container.ID |
| 47 | + data.Labels().Set("containerID", containerID) |
| 48 | + } |
| 49 | + |
| 50 | + // Remove the container |
| 51 | + helpers.Ensure("rm", "-f", containerID) |
| 52 | + |
| 53 | + time.Sleep(1 * time.Second) |
| 54 | + |
| 55 | + // Create a TestableCommand using helpers.Custom |
| 56 | + if rootlessutil.IsRootless() { |
| 57 | + // In rootless mode, we need to enter the rootlesskit network namespace |
| 58 | + stateDir, err := rootlessutil.RootlessKitStateDir() |
| 59 | + if err != nil { |
| 60 | + t.Fatalf("Failed to get rootlesskit state dir: %v", err) |
| 61 | + } |
| 62 | + |
| 63 | + childPid, err := rootlessutil.RootlessKitChildPid(stateDir) |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("Failed to get rootlesskit child pid: %v", err) |
| 66 | + } |
| 67 | + |
| 68 | + // Construct the path to the network namespace |
| 69 | + uid := os.Getuid() |
| 70 | + netnsPath := fmt.Sprintf("/run/user/%d/containerd-rootless/netns", uid) |
| 71 | + |
| 72 | + if netns, err := rootlessutil.DetachedNetNS(); err != nil { |
| 73 | + t.Fatalf("Failed to get detached network namespace: %v", err) |
| 74 | + } else { |
| 75 | + if netns != "" { |
| 76 | + // First enter the user and mount namespace, then the network namespace |
| 77 | + return helpers.Custom("nsenter", "-t", strconv.Itoa(childPid), "-U", "-m", "--preserve-credentials", |
| 78 | + "--", "sh", "-c", fmt.Sprintf("nsenter --net=%s sh -c '%s'", netnsPath, iptablesCheckCommand)) |
| 79 | + } |
| 80 | + // Enter into RootlessKit namespace using containerd-rootless-setuptool.sh |
| 81 | + return helpers.Custom("containerd-rootless-setuptool.sh", "nsenter", "--", "sh", "-c", iptablesCheckCommand) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // In non-rootless mode, check iptables rules directly on the host |
| 86 | + return helpers.Custom("sh", "-c", iptablesCheckCommand) |
| 87 | +} |
| 88 | + |
| 89 | +// TestContainerRmIptables tests that iptables rules are cleared after container deletion |
| 90 | +func TestContainerRmIptables(t *testing.T) { |
| 91 | + testCase := nerdtest.Setup() |
| 92 | + |
| 93 | + // Require iptables and containerd-rootless-setuptool.sh commands to be available |
| 94 | + testCase.Require = require.All( |
| 95 | + require.Binary("iptables"), |
| 96 | + require.Binary("containerd-rootless-setuptool.sh"), |
| 97 | + require.Not(require.Windows), |
| 98 | + require.Not(nerdtest.Docker), |
| 99 | + ) |
| 100 | + |
| 101 | + testCase.SubTests = []*test.Case{ |
| 102 | + { |
| 103 | + Description: "Test iptables rules are cleared after container deletion", |
| 104 | + Setup: func(data test.Data, helpers test.Helpers) { |
| 105 | + // Create a container with port mapping to ensure iptables rules are created |
| 106 | + helpers.Ensure("run", "-d", "--name", data.Identifier(), "-p", "8080:80", testutil.NginxAlpineImage) |
| 107 | + nerdtest.EnsureContainerStarted(helpers, identifier) |
| 108 | + }, |
| 109 | + Cleanup: func(data test.Data, helpers test.Helpers) { |
| 110 | + // Make sure container is removed even if test fails |
| 111 | + helpers.Anyhow("rm", "-f", data.Identifier()) |
| 112 | + }, |
| 113 | + Command: testContainerRmIptablesExecutor, |
| 114 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 115 | + // Get the container ID from the label |
| 116 | + containerID := data.Labels().Get("containerID") |
| 117 | + return &test.Expected{ |
| 118 | + ExitCode: expect.ExitCodeSuccess, |
| 119 | + // Verify that the iptables output does not contain the container ID |
| 120 | + Output: expect.DoesNotContain(containerID), |
| 121 | + } |
| 122 | + }, |
| 123 | + }, |
| 124 | + } |
| 125 | + |
| 126 | + testCase.Run(t) |
| 127 | +} |
0 commit comments