|
| 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/exec" |
| 22 | + "strings" |
| 23 | + "testing" |
| 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 | +) |
| 33 | + |
| 34 | +// TestContainerRmIptables tests that iptables rules are cleared after container deletion |
| 35 | +func TestContainerRmIptables(t *testing.T) { |
| 36 | + // Check if iptables command exists |
| 37 | + if _, err := exec.LookPath("iptables"); err != nil { |
| 38 | + t.Skip("iptables command not found, skipping test") |
| 39 | + } |
| 40 | + |
| 41 | + testCase := nerdtest.Setup() |
| 42 | + |
| 43 | + // This test requires Linux |
| 44 | + testCase.Require = require.Not(require.Windows) |
| 45 | + |
| 46 | + testCase.SubTests = []*test.Case{ |
| 47 | + { |
| 48 | + Description: "Test iptables rules are cleared after container deletion", |
| 49 | + Setup: func(data test.Data, helpers test.Helpers) { |
| 50 | + // Create a container with port mapping to ensure iptables rules are created |
| 51 | + helpers.Ensure("run", "-d", "--name", data.Identifier(), "-p", "8080:80", testutil.NginxAlpineImage) |
| 52 | + }, |
| 53 | + Cleanup: func(data test.Data, helpers test.Helpers) { |
| 54 | + // Make sure container is removed even if test fails |
| 55 | + helpers.Anyhow("rm", "-f", data.Identifier()) |
| 56 | + }, |
| 57 | + Command: func(data test.Data, helpers test.Helpers) test.TestableCommand { |
| 58 | + containerID := data.Identifier() |
| 59 | + |
| 60 | + // Check if iptables rules exist for the container in all tables |
| 61 | + tables := []string{"nat", "filter", "mangle"} |
| 62 | + rulesFound := false |
| 63 | + |
| 64 | + for _, table := range tables { |
| 65 | + iptablesCmd := exec.Command("iptables", "-t", table, "-S") |
| 66 | + output, err := iptablesCmd.CombinedOutput() |
| 67 | + if err != nil { |
| 68 | + t.Logf("Warning: Failed to list iptables rules for table %s: %v", table, err) |
| 69 | + continue |
| 70 | + } |
| 71 | + |
| 72 | + // Check if rules exist for the container |
| 73 | + iptablesOutput := string(output) |
| 74 | + if strings.Contains(iptablesOutput, containerID) { |
| 75 | + rulesFound = true |
| 76 | + t.Logf("Found iptables rules for container %s in table %s", containerID, table) |
| 77 | + break |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if !rulesFound { |
| 82 | + t.Fatalf("Expected iptables rules for container %s, but none found", containerID) |
| 83 | + } |
| 84 | + |
| 85 | + // Remove the container |
| 86 | + helpers.Ensure("rm", "-f", containerID) |
| 87 | + |
| 88 | + // Check all tables to ensure rules are cleared |
| 89 | + cmd := helpers.Command("run", "--rm", testutil.AlpineImage, "sh", "-c", |
| 90 | + fmt.Sprintf(` |
| 91 | + for table in nat filter mangle; do |
| 92 | + if iptables -t $table -S | grep -q %s; then |
| 93 | + echo "iptables rules still exist in table $table" |
| 94 | + exit 1 |
| 95 | + fi |
| 96 | + done |
| 97 | + echo "iptables rules cleared" |
| 98 | + `, containerID)) |
| 99 | + |
| 100 | + return cmd |
| 101 | + }, |
| 102 | + Expected: test.Expects(0, nil, expect.Contains("iptables rules cleared")), |
| 103 | + }, |
| 104 | + } |
| 105 | + |
| 106 | + testCase.Run(t) |
| 107 | +} |
0 commit comments