|
| 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 | + "strconv" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/containerd/nerdctl/mod/tigron/expect" |
| 26 | + "github.com/containerd/nerdctl/mod/tigron/require" |
| 27 | + "github.com/containerd/nerdctl/mod/tigron/test" |
| 28 | + |
| 29 | + "github.com/containerd/nerdctl/v2/pkg/rootlessutil" |
| 30 | + "github.com/containerd/nerdctl/v2/pkg/testutil" |
| 31 | + "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest" |
| 32 | + "github.com/containerd/nerdctl/v2/pkg/testutil/portlock" |
| 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 container ID from the label |
| 43 | + containerID := data.Labels().Get("containerID") |
| 44 | + |
| 45 | + // Remove the container |
| 46 | + helpers.Ensure("rm", "-f", containerID) |
| 47 | + |
| 48 | + time.Sleep(1 * time.Second) |
| 49 | + |
| 50 | + // Create a TestableCommand using helpers.Custom |
| 51 | + if rootlessutil.IsRootless() { |
| 52 | + // In rootless mode, we need to enter the rootlesskit network namespace |
| 53 | + if netns, err := rootlessutil.DetachedNetNS(); err != nil { |
| 54 | + t.Fatalf("Failed to get detached network namespace: %v", err) |
| 55 | + } else { |
| 56 | + if netns != "" { |
| 57 | + // Use containerd-rootless-setuptool.sh to enter the RootlessKit namespace |
| 58 | + return helpers.Custom("containerd-rootless-setuptool.sh", "nsenter", "--", "nsenter", "--net="+netns, "sh", "-ec", iptablesCheckCommand) |
| 59 | + } |
| 60 | + // Enter into :RootlessKit namespace using containerd-rootless-setuptool.sh |
| 61 | + return helpers.Custom("containerd-rootless-setuptool.sh", "nsenter", "--", "sh", "-ec", iptablesCheckCommand) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // In non-rootless mode, check iptables rules directly on the host |
| 66 | + return helpers.Custom("sh", "-ec", iptablesCheckCommand) |
| 67 | +} |
| 68 | + |
| 69 | +// TestContainerRmIptables tests that iptables rules are cleared after container deletion |
| 70 | +func TestContainerRmIptables(t *testing.T) { |
| 71 | + testCase := nerdtest.Setup() |
| 72 | + |
| 73 | + // Require iptables and containerd-rootless-setuptool.sh commands to be available |
| 74 | + testCase.Require = require.All( |
| 75 | + require.Binary("iptables"), |
| 76 | + require.Binary("containerd-rootless-setuptool.sh"), |
| 77 | + require.Not(require.Windows), |
| 78 | + require.Not(nerdtest.Docker), |
| 79 | + ) |
| 80 | + |
| 81 | + testCase.SubTests = []*test.Case{ |
| 82 | + { |
| 83 | + Description: "Test iptables rules are cleared after container deletion", |
| 84 | + Setup: func(data test.Data, helpers test.Helpers) { |
| 85 | + // Get a free port using portlock |
| 86 | + port, err := portlock.Acquire(0) |
| 87 | + if err != nil { |
| 88 | + helpers.T().Fatalf("Failed to acquire port: %v", err) |
| 89 | + } |
| 90 | + data.Labels().Set("port", strconv.Itoa(port)) |
| 91 | + |
| 92 | + // Create a container with port mapping to ensure iptables rules are created |
| 93 | + containerID := helpers.Capture("run", "-d", "--name", data.Identifier(), "-p", fmt.Sprintf("%d:80", port), testutil.NginxAlpineImage) |
| 94 | + data.Labels().Set("containerID", containerID) |
| 95 | + nerdtest.EnsureContainerStarted(helpers, data.Identifier()) |
| 96 | + }, |
| 97 | + Cleanup: func(data test.Data, helpers test.Helpers) { |
| 98 | + // Make sure container is removed even if test fails |
| 99 | + helpers.Anyhow("rm", "-f", data.Identifier()) |
| 100 | + |
| 101 | + // Release the acquired port |
| 102 | + if portStr := data.Labels().Get("port"); portStr != "" { |
| 103 | + port, _ := strconv.Atoi(portStr) |
| 104 | + _ = portlock.Release(port) |
| 105 | + } |
| 106 | + }, |
| 107 | + Command: testContainerRmIptablesExecutor, |
| 108 | + Expected: func(data test.Data, helpers test.Helpers) *test.Expected { |
| 109 | + // Get the container ID from the label |
| 110 | + containerID := data.Labels().Get("containerID") |
| 111 | + return &test.Expected{ |
| 112 | + ExitCode: expect.ExitCodeSuccess, |
| 113 | + // Verify that the iptables output does not contain the container ID |
| 114 | + Output: expect.DoesNotContain(containerID), |
| 115 | + } |
| 116 | + }, |
| 117 | + }, |
| 118 | + } |
| 119 | + |
| 120 | + testCase.Run(t) |
| 121 | +} |
0 commit comments