Skip to content

Commit 1b920ee

Browse files
aledbfclaude
andcommitted
test(lifecycle): hold the hook error, shell and env probe edits
Without Docker no unit test refused edits to HookError's message, the shell server's -1 exit code on a failed exec, the hook output logging, the sbin PATH filter, the env cache read and parseEnvOutput's continuation lines. Test each through the package's fake executors. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent cf5e536 commit 1b920ee

3 files changed

Lines changed: 115 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package lifecycle
2+
3+
import (
4+
"testing"
5+
6+
"github.com/devcontainers/cli/internal/imagemeta"
7+
"github.com/devcontainers/cli/internal/log"
8+
)
9+
10+
// A failed hook's error names its phase, and the parallel command that failed
11+
// when it is one of an object's commands (TS runLifecycleCommand's description
12+
// names it too: "<name> of <hook> from ... failed.").
13+
func TestRunHooksErrorNamesTheFailedCommand(t *testing.T) {
14+
for want, command := range map[string]any{
15+
"postCreateCommand failed: command failed: bad": "bad",
16+
"postCreateCommand failed: b: command failed: bad": map[string]any{"a": "good", "b": "bad"},
17+
} {
18+
merged := &imagemeta.MergedConfig{PostCreateCommands: []any{command}}
19+
err := RunHooks(log.Null, &mockExecutor{failOn: "bad"}, merged, RunOptions{})
20+
if err == nil || err.Error() != want {
21+
t.Errorf("RunHooks(%v) = %v, want %q", command, err, want)
22+
}
23+
}
24+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package lifecycle
2+
3+
import (
4+
"errors"
5+
"maps"
6+
"testing"
7+
)
8+
9+
// A non-root user's merged PATH leaves out every base entry under an sbin
10+
// directory, not only one ending in it: TS mergePaths tests /\/sbin(\/|$)/.
11+
func TestMergePathsSkipsEntriesUnderSbinForNonRoot(t *testing.T) {
12+
if got := mergePaths("/usr/bin", "/opt/sbin/tools:/opt/sbinx:/usr/bin", false); got != "/opt/sbinx:/usr/bin" {
13+
t.Errorf("mergePaths = %q, want /opt/sbinx:/usr/bin", got)
14+
}
15+
}
16+
17+
// The env cache is used only when it was read: an exec error, a non-zero exit
18+
// or no output is a miss even when stdout holds JSON.
19+
func TestReadEnvCacheMissesUnlessTheReadSucceeded(t *testing.T) {
20+
for name, reply := range map[string]struct {
21+
code int
22+
err error
23+
}{
24+
"exec error": {0, errors.New("gone")},
25+
"non-zero exit": {1, nil},
26+
"error and exit": {1, errors.New("gone")},
27+
} {
28+
fs := &fakeShell{handler: func(string) (string, int, error) { return `{"A":"1"}`, reply.code, reply.err }}
29+
if got := readEnvCache(fs, ProbeLoginShell, "/tmp/sess"); got != nil {
30+
t.Errorf("%s: readEnvCache = %v, want a miss", name, got)
31+
}
32+
}
33+
}
34+
35+
// parseEnvOutput reads one-letter names, appends a line without '=' to the
36+
// value before it, and drops lines before the first variable.
37+
func TestParseEnvOutputContinuationsAndShortNames(t *testing.T) {
38+
got := parseEnvOutput("stray line\nA=1\nB=x\ny\nC=3")
39+
want := map[string]string{"A": "1", "B": "x\ny", "C": "3"}
40+
if !maps.Equal(got, want) {
41+
t.Errorf("parseEnvOutput = %q, want %q", got, want)
42+
}
43+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package lifecycle
2+
3+
import (
4+
"errors"
5+
"slices"
6+
"testing"
7+
)
8+
9+
// A command the container could not run at all has no exit code: Exec and
10+
// ExecWithStderr report -1 with the error, as os.ProcessState.ExitCode does for
11+
// a process that did not exit.
12+
func TestShellServerReportsNoExitCodeWhenTheExecFails(t *testing.T) {
13+
boom := errors.New("no such container")
14+
s := NewShellServer(t.Context(), &fakeExecutor{fn: func(string) (string, string, int, error) {
15+
return "out", "err", 7, boom
16+
}}, "cid", "")
17+
if out, code, err := s.Exec("true"); !errors.Is(err, boom) || out != "" || code != -1 {
18+
t.Errorf("Exec = (%q, %d, %v), want (\"\", -1, %v)", out, code, err, boom)
19+
}
20+
if out, errText, code, err := s.ExecWithStderr("true"); !errors.Is(err, boom) || out != "" || errText != "" || code != -1 {
21+
t.Errorf("ExecWithStderr = (%q, %q, %d, %v), want (\"\", \"\", -1, %v)", out, errText, code, err, boom)
22+
}
23+
}
24+
25+
// A hook's stdout and its stderr reach the log when there is any (TS streams
26+
// lifecycle-hook output); empty stdout and blank stderr write nothing.
27+
func TestShellExecutorLogsOnlyOutputThereIs(t *testing.T) {
28+
for _, tc := range []struct {
29+
stdout, stderr string
30+
want []string
31+
}{
32+
{"", " \n", nil},
33+
{"out\n", "", []string{"out\n"}},
34+
{"", "warn\n", []string{"warn\n"}},
35+
{"out\n", "warn\n", []string{"out\n", "warn\n"}},
36+
} {
37+
logger := &captureLog{}
38+
e := &ShellExecutor{Server: NewShellServer(t.Context(), &fakeExecutor{fn: func(string) (string, string, int, error) {
39+
return tc.stdout, tc.stderr, 0, nil
40+
}}, "cid", ""), Log: logger}
41+
if err := e.Exec("hook"); err != nil {
42+
t.Fatal(err)
43+
}
44+
if !slices.Equal(logger.lines, tc.want) {
45+
t.Errorf("stdout %q, stderr %q: logged %q, want %q", tc.stdout, tc.stderr, logger.lines, tc.want)
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)