1// SPDX-License-Identifier: MIT
2
3package guest
4
5import (
6 "context"
7 "net"
8 "os/exec"
9 "os/user"
10 "path/filepath"
11 "strings"
12 "sync"
13 "testing"
14 "time"
15
16 "bnema.dev/ermite-runner/internal/execstream"
17 "bnema.dev/ermite-runner/internal/execstream/execpb"
18)
19
20// runJobSession starts Exec on a unix socket and drives it with a job.
21func runJobSession(t *testing.T, yaml string, cancelOn string, tweak func(*ExecConfig)) (steps []string, logs []*execpb.Log, res *execpb.JobResult, exit *execpb.Exit) {
22 t.Helper()
23 sock := filepath.Join(t.TempDir(), "s")
24 ln, err := net.Listen("unix", sock)
25 if err != nil {
26 t.Skip(err)
27 }
28 defer func() { _ = ln.Close() }()
29 home := t.TempDir()
30 t.Setenv("HOME", home)
31 t.Setenv("RUNNER_TOOL_CACHE", filepath.Join(home, "toolcache"))
32 t.Setenv("ERMITE_CONNECT", "unix:"+sock)
33 done := make(chan struct{})
34 go func() {
35 defer close(done)
36 cfg := ExecConfig{Target: Target{"unix", sock}, Sandbox: "container", Version: "test", RunTimeout: 5 * time.Second,
37 TermAfter: 200 * time.Millisecond, KillAfter: 200 * time.Millisecond, WaitDelay: 300 * time.Millisecond}
38 if tweak != nil {
39 tweak(&cfg)
40 }
41 _, _ = Exec(context.Background(), cfg)
42 }()
43 conn, err := ln.Accept()
44 if err != nil {
45 t.Fatal(err)
46 }
47 defer func() { _ = conn.Close() }()
48 // With cancelOn, the job is cancelled when it logs that line (the script
49 // prints it once it is ready to be interrupted): no timer to guess.
50 var cancel chan *execpb.Cancel
51 if cancelOn != "" {
52 cancel = make(chan *execpb.Cancel, 1)
53 }
54 var cancelOnce sync.Once
55 var mu sync.Mutex
56 _, exit, err = execstream.DriveJob(conn, &execpb.Run{Job: &execpb.JobSpec{
57 WorkflowPath: ".github/workflows/ci.yml", WorkflowYaml: yaml, JobKey: "j", Repo: "hello", RunnerName: "r", RunnerVersion: "v",
58 Contexts: `{"github":{"repository":"octo/hello","event_name":"push","job":"j"}}`, Matrix: "{}",
59 }}, cancel, execstream.JobHandler{
60 StepStarted: func(s *execpb.StepStarted) {
61 mu.Lock()
62 steps = append(steps, s.Name)
63 mu.Unlock()
64 },
65 Log: func(l *execpb.Log) {
66 logs = append(logs, l)
67 if cancelOn != "" && l.Text == cancelOn {
68 cancelOnce.Do(func() { cancel <- &execpb.Cancel{Reason: "test", Kind: execpb.CancelKind_CANCEL_KIND_USER} })
69 }
70 },
71 JobResult: func(r *execpb.JobResult) { res = r },
72 })
73 if err != nil {
74 t.Fatal(err)
75 }
76 <-done
77 return
78}
79
80func requireBash(t *testing.T) {
81 t.Helper()
82 if _, err := lookPath("bash"); err != nil {
83 t.Skip("bash not installed")
84 }
85}
86
87func TestExecRunsAJob(t *testing.T) {
88 requireBash(t)
89 steps, logs, res, exit := runJobSession(t, "on: push\njobs:\n j:\n runs-on: x\n outputs:\n o: ${{ steps.s.outputs.v }}\n steps:\n - id: s\n run: |\n echo hello-from-job\n echo v=42 >> \"$GITHUB_OUTPUT\"\n", "", nil)
90 if res == nil || res.Conclusion != execpb.StepConclusion_STEP_CONCLUSION_SUCCESS || res.Outputs["o"] != "42" || exit.Code != 0 {
91 t.Fatalf("res=%v exit=%v", res, exit)
92 }
93 if strings.Join(steps, "|") != "Set up job|Run echo hello-from-job|Complete job" {
94 t.Fatalf("steps %v", steps)
95 }
96 found := false
97 for _, l := range logs {
98 if l.Text == "hello-from-job" && l.Step == 2 && l.UnixNano > 0 && !l.Continued {
99 found = true
100 }
101 }
102 if !found {
103 t.Fatalf("log line missing: %d lines", len(logs))
104 }
105}
106
107func TestExecJobFailureExitsNonZero(t *testing.T) {
108 requireBash(t)
109 _, _, res, exit := runJobSession(t, "on: push\njobs:\n j:\n runs-on: x\n steps:\n - run: exit 4\n", "", nil)
110 if res.Conclusion != execpb.StepConclusion_STEP_CONCLUSION_FAILURE || exit.Code == 0 {
111 t.Fatalf("res=%v exit=%v", res, exit)
112 }
113}
114
115func TestExecJobLongLinesAreContinued(t *testing.T) {
116 requireBash(t)
117 _, logs, res, _ := runJobSession(t, "on: push\njobs:\n j:\n runs-on: x\n steps:\n - run: head -c 150000 /dev/zero | tr '\\0' y; echo\n", "", nil)
118 if res.Conclusion != execpb.StepConclusion_STEP_CONCLUSION_SUCCESS {
119 t.Fatalf("%v", res)
120 }
121 var total int
122 var pieces int
123 for _, l := range logs {
124 if strings.HasPrefix(l.Text, "yyyy") {
125 pieces++
126 total += len(l.Text)
127 if len(l.Text) > execstream.MaxLogLine || (pieces > 1) != l.Continued {
128 t.Fatalf("piece %d: len %d continued %v", pieces, len(l.Text), l.Continued)
129 }
130 }
131 }
132 if pieces != 3 || total != 150000 {
133 t.Fatalf("pieces=%d total=%d", pieces, total)
134 }
135}
136
137func TestExecJobCancel(t *testing.T) {
138 requireBash(t)
139 start := time.Now()
140 _, _, res, _ := runJobSession(t, "on: push\njobs:\n j:\n runs-on: x\n steps:\n - run: |\n echo ready\n sleep 30\n", "ready", nil)
141 if res.Conclusion != execpb.StepConclusion_STEP_CONCLUSION_CANCELLED || res.RunnerShutdown {
142 t.Fatalf("%v", res)
143 }
144 if time.Since(start) > 8*time.Second {
145 t.Fatal("cancel too slow")
146 }
147}
148
149func TestExecJobHostGoneIsAShutdown(t *testing.T) {
150 requireBash(t)
151 sock := filepath.Join(t.TempDir(), "s")
152 ln, err := net.Listen("unix", sock)
153 if err != nil {
154 t.Skip(err)
155 }
156 defer func() { _ = ln.Close() }()
157 t.Setenv("HOME", t.TempDir())
158 t.Setenv("RUNNER_TOOL_CACHE", filepath.Join(t.TempDir(), "toolcache"))
159 done := make(chan int, 1)
160 go func() {
161 c, _ := Exec(context.Background(), ExecConfig{Target: Target{"unix", sock}, RunTimeout: 5 * time.Second,
162 TermAfter: 100 * time.Millisecond, KillAfter: 100 * time.Millisecond, WaitDelay: 200 * time.Millisecond})
163 done <- c
164 }()
165 conn, _ := ln.Accept()
166 c := execstream.NewConn(conn)
167 if _, err := c.Recv(); err != nil {
168 t.Fatal(err)
169 }
170 _ = c.Send(&execpb.Frame{Body: &execpb.Frame_Run{Run: &execpb.Run{Job: &execpb.JobSpec{
171 WorkflowYaml: "on: push\njobs:\n j:\n runs-on: x\n steps:\n - run: echo running; sleep 60\n", JobKey: "j", Repo: "hello",
172 Contexts: `{"github":{"repository":"o/hello"}}`,
173 }}}})
174 // Close the stream once the step is running (its first line arrived).
175 for {
176 f, err := c.Recv()
177 if err != nil {
178 t.Fatal(err)
179 }
180 if l := f.GetLog(); l != nil && l.Text == "running" {
181 break
182 }
183 }
184 _ = conn.Close()
185 select {
186 case <-done:
187 case <-time.After(10 * time.Second):
188 t.Fatal("the executor kept running without a host")
189 }
190}
191
192func TestStepEnv(t *testing.T) {
193 got := stepEnv([]string{"PATH=/bin", "HOME=/root", "ERMITE_CONNECT=x", "USER=root", "RUNNER_TOOL_CACHE=/x", "FOO=1"}, StepUser{Name: "runner", Home: "/home/runner"})
194 want := "PATH=/bin FOO=1 HOME=/home/runner USER=runner LOGNAME=runner"
195 if strings.Join(got, " ") != want {
196 t.Fatalf("%v", got)
197 }
198}
199
200func lookPath(name string) (string, error) { return exec.LookPath(name) }
201
202func TestMissingStepUserFailsTheJobUnlessTestOptionSet(t *testing.T) {
203 none := func(string) (*user.User, error) { return nil, user.UnknownUserError("runner") }
204 // As root with no such user: refuse.
205 if _, err := lookupStepUser(0, none, "runner", false); err == nil || !strings.Contains(err.Error(), `no user "runner"`) || !strings.Contains(err.Error(), "refusing to run them as root") {
206 t.Fatalf("%v", err)
207 }
208 // The test option allows root.
209 if su, err := lookupStepUser(0, none, "runner", true); err != nil || su.Name != "root" || su.Credential != nil {
210 t.Fatalf("%+v %v", su, err)
211 }
212 // Not root: the steps run as the current user, whatever the name.
213 if su, err := lookupStepUser(1000, none, "runner", false); err != nil || su.Credential != nil {
214 t.Fatalf("%+v %v", su, err)
215 }
216 // As root with the user: switch to it.
217 found := func(string) (*user.User, error) {
218 return &user.User{Uid: "1001", Gid: "1001", Username: "runner", HomeDir: "/home/runner"}, nil
219 }
220 su, err := lookupStepUser(0, found, "runner", false)
221 if err != nil || su.Credential == nil || su.Credential.Uid != 1001 || su.Credential.Gid != 1001 || su.Home != "/home/runner" {
222 t.Fatalf("%+v %v", su, err)
223 }
224}
225
226// The failure is reported like a failed "Set up job".
227func TestFailSetupReportsAFailedFirstStep(t *testing.T) {
228 host, guest := net.Pipe()
229 got := make(chan []string, 1)
230 go func() {
231 var frames []string
232 c := execstream.NewConn(host)
233 for {
234 f, err := c.Recv()
235 if err != nil {
236 got <- frames
237 return
238 }
239 switch b := f.Body.(type) {
240 case *execpb.Frame_StepStarted:
241 frames = append(frames, "start:"+b.StepStarted.Name)
242 case *execpb.Frame_Log:
243 frames = append(frames, "log:"+b.Log.Text)
244 case *execpb.Frame_StepCompleted:
245 frames = append(frames, "done:"+b.StepCompleted.Conclusion.String())
246 case *execpb.Frame_JobResult:
247 frames = append(frames, "result:"+b.JobResult.Conclusion.String())
248 }
249 }
250 }()
251 if code := failSetup(execstream.NewConn(guest), "no user"); code != 1 {
252 t.Fatal(code)
253 }
254 _ = guest.Close()
255 want := "start:Set up job|log:##[error]no user|done:STEP_CONCLUSION_FAILURE|result:STEP_CONCLUSION_FAILURE"
256 if g := strings.Join(<-got, "|"); g != want {
257 t.Fatalf("%s", g)
258 }
259}