Make TestContainerPids more resilient

ListPids may not pick up the sh subprocess yet when it is first run. To
make this test more resilient, retry fetching the processes if only a
single pid is found for a short time.

Signed-off-by: Derek McGowan <derek@mcg.dev>
This commit is contained in:
Derek McGowan 2024-11-02 23:46:55 -07:00
parent ada2fa16d6
commit bddeba8250
No known key found for this signature in database
GPG Key ID: F58C5D0A4405ACDB

View File

@ -33,6 +33,15 @@ import (
apievents "github.com/containerd/containerd/api/events"
"github.com/containerd/containerd/api/types/runc/options"
"github.com/containerd/continuity/fs"
"github.com/containerd/errdefs"
"github.com/containerd/go-runc"
"github.com/containerd/log/logtest"
"github.com/containerd/platforms"
"github.com/containerd/typeurl/v2"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/require"
. "github.com/containerd/containerd/v2/client"
"github.com/containerd/containerd/v2/core/containers"
"github.com/containerd/containerd/v2/core/images"
@ -42,14 +51,6 @@ import (
"github.com/containerd/containerd/v2/pkg/oci"
gogotypes "github.com/containerd/containerd/v2/pkg/protobuf/types"
"github.com/containerd/containerd/v2/plugins"
"github.com/containerd/continuity/fs"
"github.com/containerd/errdefs"
"github.com/containerd/go-runc"
"github.com/containerd/log/logtest"
"github.com/containerd/platforms"
"github.com/containerd/typeurl/v2"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/require"
)
func empty() cio.Creator {
@ -571,6 +572,8 @@ func TestContainerPids(t *testing.T) {
t.Errorf("invalid task pid %d", taskPid)
}
tryUntil := time.Now().Add(time.Second)
checkPids := func() bool {
processes, err := task.Pids(ctx)
if err != nil {
t.Fatal(err)
@ -595,6 +598,14 @@ func TestContainerPids(t *testing.T) {
} else {
// 2 processes, 1 for sh and one for sleep
if l != 2 {
if l == 1 && time.Now().Before(tryUntil) {
// The subcommand may not have been started when the
// pids are requested. Retrying is a simple way to
// handle the race under normal conditions. A better
// but more complex solution would be first waiting
// for output from the subprocess to be seen.
return true
}
t.Errorf("expected 2 process but received %d", l)
}
}
@ -609,6 +620,13 @@ func TestContainerPids(t *testing.T) {
if !found {
t.Errorf("pid %d must be in %+v", taskPid, processes)
}
return false
}
for checkPids() {
time.Sleep(5 * time.Millisecond)
}
if err := task.Kill(ctx, syscall.SIGKILL); err != nil {
select {
case s := <-statusC: