|
| 1 | +// Copyright 2026 The gVisor Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package proc |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "os/exec" |
| 20 | + "sync" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/containerd/console" |
| 24 | + "github.com/containerd/containerd/v2/pkg/stdio" |
| 25 | + runc "github.com/containerd/go-runc" |
| 26 | + "gvisor.dev/gvisor/pkg/shim/v1/runsccmd" |
| 27 | +) |
| 28 | + |
| 29 | +// fakePlatform is a no-op stdio.Platform. |
| 30 | +type fakePlatform struct{} |
| 31 | + |
| 32 | +func (fakePlatform) CopyConsole(ctx context.Context, con console.Console, id, stdin, stdout, stderr string, wg *sync.WaitGroup) (console.Console, error) { |
| 33 | + return con, nil |
| 34 | +} |
| 35 | + |
| 36 | +func (fakePlatform) ShutdownConsole(context.Context, console.Console) error { return nil } |
| 37 | + |
| 38 | +func (fakePlatform) Close() error { return nil } |
| 39 | + |
| 40 | +// closerIO records whether it was closed. The embedded interface is nil; only |
| 41 | +// the methods below are called. |
| 42 | +type closerIO struct { |
| 43 | + runc.IO |
| 44 | + closed bool |
| 45 | +} |
| 46 | + |
| 47 | +func (c *closerIO) Close() error { |
| 48 | + c.closed = true |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +func (c *closerIO) Set(*exec.Cmd) {} |
| 53 | + |
| 54 | +// TestCreatedStateStartFailureWithoutIO verifies that a failed start does not |
| 55 | +// panic when p.io is nil, as Init.Create leaves it for a terminal. |
| 56 | +func TestCreatedStateStartFailureWithoutIO(t *testing.T) { |
| 57 | + p := New("id", &runsccmd.Runsc{Command: "/nonexistent-runsc"}, stdio.Stdio{Terminal: true}) |
| 58 | + p.Platform = fakePlatform{} |
| 59 | + p.Sandbox = false |
| 60 | + |
| 61 | + if err := p.Start(t.Context()); err == nil { |
| 62 | + t.Fatal("Start() succeeded, want error") |
| 63 | + } |
| 64 | + if _, ok := p.initState.(*stoppedState); !ok { |
| 65 | + t.Errorf("initState = %T, want *stoppedState", p.initState) |
| 66 | + } |
| 67 | + if got := p.ExitStatus(); got != internalErrorCode { |
| 68 | + t.Errorf("ExitStatus() = %d, want %d", got, internalErrorCode) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// TestCreatedStateStartFailureClosesIO verifies that the failure path still |
| 73 | +// closes the IO when it exists. |
| 74 | +func TestCreatedStateStartFailureClosesIO(t *testing.T) { |
| 75 | + p := New("id", &runsccmd.Runsc{Command: "/nonexistent-runsc"}, stdio.Stdio{}) |
| 76 | + p.Platform = fakePlatform{} |
| 77 | + p.Sandbox = false |
| 78 | + cio := &closerIO{} |
| 79 | + p.io = cio |
| 80 | + |
| 81 | + if err := p.Start(t.Context()); err == nil { |
| 82 | + t.Fatal("Start() succeeded, want error") |
| 83 | + } |
| 84 | + if !cio.closed { |
| 85 | + t.Error("process IO was not closed on start failure") |
| 86 | + } |
| 87 | + if _, ok := p.initState.(*stoppedState); !ok { |
| 88 | + t.Errorf("initState = %T, want *stoppedState", p.initState) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// TestCreatedStateStartFailureSandbox verifies that a sandbox is left in the |
| 93 | +// created state on start failure, so that it can still be deleted. |
| 94 | +func TestCreatedStateStartFailureSandbox(t *testing.T) { |
| 95 | + p := New("id", &runsccmd.Runsc{Command: "/nonexistent-runsc"}, stdio.Stdio{Terminal: true}) |
| 96 | + p.Platform = fakePlatform{} |
| 97 | + p.Sandbox = true |
| 98 | + |
| 99 | + if err := p.Start(t.Context()); err == nil { |
| 100 | + t.Fatal("Start() succeeded, want error") |
| 101 | + } |
| 102 | + if _, ok := p.initState.(*createdState); !ok { |
| 103 | + t.Errorf("initState = %T, want *createdState", p.initState) |
| 104 | + } |
| 105 | +} |
0 commit comments