diff --git a/test/e2e/batch_test.go b/test/e2e/batch_test.go index 1ddac2e2090..c97f2cf8ee1 100644 --- a/test/e2e/batch_test.go +++ b/test/e2e/batch_test.go @@ -18,8 +18,12 @@ import ( "fmt" "strings" "testing" + "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "k8s.io/apimachinery/pkg/util/sets" + "k8s.io/apimachinery/pkg/util/wait" ) // TestBatchCreatePods verifies there is no FD leak after batched Pod creation. @@ -39,17 +43,19 @@ func TestBatchCreatePods(t *testing.T) { podName, err := data.getAntreaPodOnNode(node1) assert.NoError(t, err) - getFDs := func() string { + getFDs := func() sets.Set[string] { // In case that antrea-agent is not running as Pid 1 in future. cmds := []string{"pgrep", "-o", "antrea-agent"} pid, _, err := data.RunCommandFromPod(antreaNamespace, podName, "antrea-agent", cmds) - assert.NoError(t, err) + require.NoError(t, err) // Ignore the difference of modification time by specifying "--time-style +". cmds = []string{"ls", "-l", "--time-style", "+", fmt.Sprintf("/proc/%s/fd/", strings.TrimSpace(pid))} stdout, _, err := data.RunCommandFromPod(antreaNamespace, podName, "antrea-agent", cmds) - assert.NoError(t, err) - return stdout + require.NoError(t, err) + + fds := strings.Split(stdout, "\n") + return sets.New(fds...) } oldFDs := getFDs() @@ -57,6 +63,14 @@ func TestBatchCreatePods(t *testing.T) { _, _, cleanupFn := createTestBusyboxPods(t, data, batchNum, data.testNamespace, node1) defer cleanupFn() - newFDs := getFDs() - assert.Equal(t, oldFDs, newFDs, "FDs were changed after batched Pod creation") + // It is possible for new FDs to be allocated temporarily by the process, for different + // reasons (health probes, CNI invocations, ...). In that case, the new set of FDs can + // contain additional entries compared to the old set of FDs. However, eventually, getFDs() + // should return a subset of oldFDs. + // Most of the time, wait.PollImmediate will return immediately, after the first call to the + // condition function. + assert.NoError(t, wait.PollImmediate(100*time.Millisecond, 2*time.Second, func() (bool, error) { + newFDs := getFDs() + return oldFDs.IsSuperset(newFDs), nil + }), "Batched Pod creation allocated new FDs") }