package runner
import (
"context"
"encoding/json"
"net/http/httptest"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
"testing"
"time"
)
// A JOB'S LOG OPENS WITH THE ENVIRONMENT ITS STEPS SEE: user, umask,
// directory, PATH, variable NAMES (never values) and tool versions,
// between two delimiters, before the first step. A probe that fails or a
// missing tool never fails the job.
func TestAJobLogOpensWithItsEnvironment(t *testing.T) {
// A PATH holding only what the report needs, so tools are absent
// except a fake go whose version probe fails.
bin := t.TempDir()
for _, n := range []string{"sh", "env", "grep", "cut", "sort", "id", "head"} {
p, err := exec.LookPath(n)
if err != nil {
t.Skipf("no %s: %v", n, err)
}
if err := os.Symlink(p, filepath.Join(bin, n)); err != nil {
t.Fatal(err)
}
}
if err := os.WriteFile(filepath.Join(bin, "go"), []byte("#!/bin/sh\nexit 1\n"), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", bin)
u, err := user.Current()
if err != nil {
t.Fatal(err)
}
g, gerr := user.LookupGroupId(u.Gid)
job := Job{Env: map[string]string{"VISIBLE_NAME": "recognisable-value-7731"},
Steps: []JobStep{{Run: "echo STEP-OUTPUT"}}}
raw, _ := json.Marshal(job)
run := func(jobUsers []string, self string) *fakeBridge {
p := task("e", "c1")
p.Metadata = map[string]string{JobKey: string(raw)}
b := &fakeBridge{queue: map[string][]polled{"t-job": {p}}, resolved: map[string]map[string]any{}, logs: map[string]string{},
token: func(string) (map[string]string, string) {
return map[string]string{"QUARRY_USER": "run-abc", "QUARRY_TOKEN": "qrt_abc_t0ken-value"}, ""
}}
srv := httptest.NewServer(b.handler())
defer srv.Close()
r, err := New(Config{
Bridge: srv.URL, Token: "WT", Tasks: []string{"t-job"}, MaxTasks: 1,
WorkDir: t.TempDir(), Checkout: appCheckout{}, Heartbeat: 50 * time.Millisecond, Logf: t.Logf,
JobUsers: jobUsers, Self: self,
})
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() { r.Run(ctx); close(done) }()
for deadline := time.Now().Add(20 * time.Second); time.Now().Before(deadline); time.Sleep(20 * time.Millisecond) {
b.mu.Lock()
n := len(b.resolved)
b.mu.Unlock()
if n == 1 {
break
}
}
cancel()
<-done
return b
}
check := func(label string, b *fakeBridge, wantMasked bool) {
b.mu.Lock()
defer b.mu.Unlock()
log := b.logs["e"]
if got := b.resolved["e"]; got["action"] != "complete" {
t.Fatalf("FAIL: %s: job resolved %v, log %q", label, got, log)
}
begin, end, step := strings.Index(log, EnvBegin), strings.Index(log, EnvEnd), strings.Index(log, "STEP-OUTPUT")
if begin < 0 || end < begin || step < end {
t.Errorf("FAIL: %s: log %q; want the delimited block before the first step's output", label, log)
return
}
block := log[begin:end]
if !strings.Contains(block, "VISIBLE_NAME") {
t.Errorf("FAIL: %s: block %q lacks the variable name", label, block)
}
if strings.Contains(log, "recognisable-value-7731") || strings.Contains(log, "t0ken-value") {
t.Errorf("FAIL: %s: a variable's value reached the log %q", label, log)
}
if !strings.Contains(block, u.Username) {
t.Errorf("FAIL: %s: block %q lacks the user %s", label, block, u.Username)
}
if wantMasked && !strings.Contains(block, "QUARRY_TOKEN (masked)") {
t.Errorf("FAIL: %s: block %q does not mark QUARRY_TOKEN masked", label, block)
}
if !strings.Contains(block, "zig: absent") || strings.Contains(block, "go: absent") {
t.Errorf("FAIL: %s: block %q; want zig absent and the failing go probe present", label, block)
}
// The steps' open-file limit is in the block: a job run under a
// launchd agent can get a smaller one than a terminal does.
if out, err := exec.Command("sh", "-c", "ulimit -n").Output(); err == nil {
if n := strings.TrimSpace(string(out)); n != "" && !strings.Contains(block, "open files "+n) {
t.Errorf("FAIL: %s: block %q lacks the open-file limit %s", label, block, n)
}
}
}
check("as itself", run(nil, ""), false)
if gerr == nil {
// The job user path: a fake sudo runs the command as the current
// user, so this proves the report goes through the starter.
sudo := t.TempDir()
fake := "#!/bin/sh\n[ \"$6\" = job-reclaim ] && exit 0\nshift 4\nexec env QUARRY_TEST_JOBENV=1 \"$@\"\n"
if err := os.WriteFile(filepath.Join(sudo, "sudo"), []byte(fake), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", sudo+string(os.PathListSeparator)+bin)
self, err := os.Executable()
if err != nil {
t.Fatal(err)
}
check("with job users", run([]string{g.Name}, self), true)
}
if !t.Failed() {
t.Log("ok")
}
}