test: deflake runsc/boot:boot_test

Due to Go's "zero value" semantics, raw FD values not explicitly specified in
boot.Args default to 0 (stdin). In loader_test.go:TestHostnetWithRawSockets,
which expects a call to boot.New() to fail, this causes the construction of a
fd.FD for Args.ExecFD = 0 which is later closed unpredictably by finalizer.

PiperOrigin-RevId: 644199565
This commit is contained in:
Jamie Liu
2024-06-17 18:39:00 -07:00
committed by gVisor bot
parent e8ca88e167
commit 4b965591e9
+12 -8
View File
@@ -116,19 +116,20 @@ func createLoader(conf *config.Config, spec *specs.Spec) (*Loader, func(), error
sock := fmt.Sprintf("\x00loader-test.%010d", rand.Int())
fd, err := server.CreateSocket(sock)
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to create socket: %w", err)
}
sandEnd, cleanup, err := startGofer(spec.Root.Path, conf)
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to start gofer: %w", err)
}
// Loader takes ownership of stdio.
var stdio []int
for _, f := range []*os.File{os.Stdin, os.Stdout, os.Stderr} {
newFd, err := unix.Dup(int(f.Fd()))
fd := int(f.Fd())
newFd, err := unix.Dup(fd)
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to dup FD %d: %w", fd, err)
}
stdio = append(stdio, newFd)
}
@@ -148,7 +149,7 @@ func createLoader(conf *config.Config, spec *specs.Spec) (*Loader, func(), error
l, err := New(args)
if err != nil {
cleanup()
return nil, nil, err
return nil, nil, fmt.Errorf("boot.New: %w", err)
}
return l, cleanup, nil
}
@@ -273,9 +274,12 @@ func TestHostnetWithRawSockets(t *testing.T) {
// Creating loader should fail.
l, err := New(Args{
ID: "should-fail",
Spec: testSpec(),
Conf: conf,
ID: "should-fail",
Spec: testSpec(),
Conf: conf,
DevGoferFD: -1,
PodInitConfigFD: -1,
ExecFD: -1,
})
if err == nil {
l.Destroy()