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:
		| @@ -33,6 +33,15 @@ import ( | |||||||
|  |  | ||||||
| 	apievents "github.com/containerd/containerd/api/events" | 	apievents "github.com/containerd/containerd/api/events" | ||||||
| 	"github.com/containerd/containerd/api/types/runc/options" | 	"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/client" | ||||||
| 	"github.com/containerd/containerd/v2/core/containers" | 	"github.com/containerd/containerd/v2/core/containers" | ||||||
| 	"github.com/containerd/containerd/v2/core/images" | 	"github.com/containerd/containerd/v2/core/images" | ||||||
| @@ -42,14 +51,6 @@ import ( | |||||||
| 	"github.com/containerd/containerd/v2/pkg/oci" | 	"github.com/containerd/containerd/v2/pkg/oci" | ||||||
| 	gogotypes "github.com/containerd/containerd/v2/pkg/protobuf/types" | 	gogotypes "github.com/containerd/containerd/v2/pkg/protobuf/types" | ||||||
| 	"github.com/containerd/containerd/v2/plugins" | 	"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 { | func empty() cio.Creator { | ||||||
| @@ -571,6 +572,8 @@ func TestContainerPids(t *testing.T) { | |||||||
| 		t.Errorf("invalid task pid %d", taskPid) | 		t.Errorf("invalid task pid %d", taskPid) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	tryUntil := time.Now().Add(time.Second) | ||||||
|  | 	checkPids := func() bool { | ||||||
| 		processes, err := task.Pids(ctx) | 		processes, err := task.Pids(ctx) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			t.Fatal(err) | 			t.Fatal(err) | ||||||
| @@ -595,6 +598,14 @@ func TestContainerPids(t *testing.T) { | |||||||
| 		} else { | 		} else { | ||||||
| 			// 2 processes, 1 for sh and one for sleep | 			// 2 processes, 1 for sh and one for sleep | ||||||
| 			if l != 2 { | 			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) | 				t.Errorf("expected 2 process but received %d", l) | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| @@ -609,6 +620,13 @@ func TestContainerPids(t *testing.T) { | |||||||
| 		if !found { | 		if !found { | ||||||
| 			t.Errorf("pid %d must be in %+v", taskPid, processes) | 			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 { | 	if err := task.Kill(ctx, syscall.SIGKILL); err != nil { | ||||||
| 		select { | 		select { | ||||||
| 		case s := <-statusC: | 		case s := <-statusC: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Derek McGowan
					Derek McGowan