gunk-dev/the-valley

self_update_test.go

sourced9c8073fd613main's published tip history
20.2 KiB
1//go:build e2e
2
3package e2e
4
5import (
6 "bytes"
7 "encoding/json"
8 "fmt"
9 "net"
10 "os"
11 "os/exec"
12 "path/filepath"
13 "strings"
14 "testing"
15 "time"
16
17 "the-valley/flow/internal/nix"
18 "the-valley/flow/internal/run"
19 "the-valley/flow/internal/selfupdate"
20)
21
22// selfUpdateEnv is a harness whose host config turns self-update on against
23// the sandbox's bare origin, with real git and a stub nix: nix eval says
24// whether the flake offers packages.<system>.klaus (it does unless the
25// noklaus file exists and is empty or names the repository), nix build makes a fake store path whose bin/klaus
26// reports the requested commit, and the profile is a file holding the klaus
27// element's store path.
28type selfUpdateEnv struct {
29 *Harness
30 store string // the stub's store
31 profile string // the stub profile's klaus element
32}
33
34func newSelfUpdateEnv(t *testing.T) *selfUpdateEnv {
35 h := NewHarness(t)
36 e := &selfUpdateEnv{Harness: h, store: filepath.Join(h.E2EDir, "store"), profile: filepath.Join(h.E2EDir, "profile.klaus")}
37 e.writeHostConfig(map[string]any{"self_update": map[string]any{"repo": h.OriginDir, "branch": "main"}})
38 h.WriteStub("nix", fmt.Sprintf(`#!/usr/bin/env bash
39dir=%q store=%q profile=%q
40{ printf 'nix'; for a in "$@"; do printf ' %%s' "$a"; done; printf '\n'; } >> "$dir/nix.argv"
41case "$1 $2" in
42"eval --json")
43 case "$4" in
44 *'builtins.hasAttr "klaus"'*'.packages.'*)
45 if [ -e "$dir/noklaus" ]; then no=$(cat "$dir/noklaus"); case "$4" in *"$no"*) echo false; exit 0;; esac; fi
46 echo true;;
47 *) echo "nix stub: unexpected eval $4" >&2; exit 2;;
48 esac;;
49"build --no-link")
50 : > "$dir/build.started"
51 while [ -e "$dir/build.hold" ]; do sleep 0.05; done
52 [ -e "$dir/build.fail" ] && { echo "error: builder for klaus failed with exit code 1" >&2; exit 1; }
53 ref="${@: -1}"; rev="${ref##*rev=}"; rev="${rev%%%%#*}"; short="${rev:0:7}"
54 out="$store/$rev-klaus-0.4.1-$short"
55 mkdir -p "$out/bin"
56 if [ -e "$dir/status.fail" ]; then status='echo "error: cannot read run state" >&2; exit 1'; else status='echo "No runs found."'; fi
57 printf '#!/bin/sh\ncase "$1" in\n--version) echo "klaus version 0.4.1-%%s";;\nstatus) %%s;;\nesac\n' "$short" "$status" > "$out/bin/klaus"
58 chmod +x "$out/bin/klaus"
59 echo "$out";;
60"profile list")
61 k=$(cat "$profile" 2>/dev/null)
62 if [ -n "$k" ]; then printf '{"elements":{"htop":{"storePaths":["/nix/store/h-htop-3"]},"klaus":{"storePaths":["%%s"]}},"version":3}\n' "$k"
63 else printf '{"elements":{"htop":{"storePaths":["/nix/store/h-htop-3"]}},"version":3}\n'; fi;;
64"profile remove") echo "remove $3" >> "$dir/profile.log"; : > "$profile";;
65"profile add") echo "add $3" >> "$dir/profile.log"; printf '%%s' "$3" > "$profile";;
66*) echo "nix stub: unexpected $*" >&2; exit 2;;
67esac
68`, h.E2EDir, e.store, e.profile))
69 return e
70}
71
72func (e *selfUpdateEnv) writeHostConfig(cfg map[string]any) {
73 e.t.Helper()
74 dir := filepath.Join(e.Home, ".klaus")
75 data, _ := json.Marshal(cfg)
76 if err := os.MkdirAll(dir, 0o755); err != nil {
77 e.t.Fatal(err)
78 }
79 if err := os.WriteFile(filepath.Join(dir, "config.json"), data, 0o644); err != nil {
80 e.t.Fatal(err)
81 }
82}
83
84// advance commits to main and pushes it to the origin, returning the new tip.
85func (e *selfUpdateEnv) advance(msg string) string {
86 e.t.Helper()
87 e.git(e.RepoDir, "commit", "-q", "--allow-empty", "-m", msg)
88 e.git(e.RepoDir, "push", "-q", "origin", "main")
89 return e.GitOutput(e.RepoDir, "rev-parse", "HEAD")
90}
91
92func (e *selfUpdateEnv) tip() string { return e.GitOutput(e.RepoDir, "rev-parse", "HEAD") }
93
94func (e *selfUpdateEnv) out(sha string) string {
95 return filepath.Join(e.store, sha+"-klaus-0.4.1-"+sha[:7])
96}
97
98func (e *selfUpdateEnv) state() selfupdate.State {
99 e.t.Helper()
100 st, err := selfupdate.Load(filepath.Join(e.Home, ".local", "state", "klaus"))
101 if err != nil {
102 e.t.Fatal(err)
103 }
104 return st
105}
106
107func (e *selfUpdateEnv) builds() int {
108 data, _ := os.ReadFile(filepath.Join(e.E2EDir, "nix.argv"))
109 return strings.Count(string(data), "nix build ")
110}
111
112func (e *selfUpdateEnv) profileKlaus() string {
113 data, _ := os.ReadFile(e.profile)
114 return string(data)
115}
116
117func (e *selfUpdateEnv) profileLog() string {
118 data, _ := os.ReadFile(filepath.Join(e.E2EDir, "profile.log"))
119 return string(data)
120}
121
122func (e *selfUpdateEnv) flag(name string, on bool) {
123 e.t.Helper()
124 p := filepath.Join(e.E2EDir, name)
125 if on {
126 if err := os.WriteFile(p, nil, 0o644); err != nil {
127 e.t.Fatal(err)
128 }
129 return
130 }
131 _ = os.Remove(p)
132}
133
134func (e *selfUpdateEnv) selfUpdate(wantExit int, args ...string) RunResult {
135 e.t.Helper()
136 res := e.RunKlausIn(e.E2EDir, append([]string{"self-update"}, args...)...)
137 if res.ExitCode != wantExit {
138 e.t.Fatalf("klaus self-update %v exited %d, want %d\nstdout:\n%s\nstderr:\n%s", args, res.ExitCode, wantExit, res.Stdout, res.Stderr)
139 }
140 return res
141}
142
143// TestSelfUpdateSelectsTheKlausPackage: with the host config and the state
144// an earlier run left, self-update builds and installs the flake's
145// packages.<system>.klaus, never its default package. Once the configured
146// repository's flake offers no klaus package, self-update refuses before
147// anything changes.
148func TestSelfUpdateSelectsTheKlausPackage(t *testing.T) {
149 t.Parallel()
150 e := newSelfUpdateEnv(t)
151 first := e.tip()
152 e.selfUpdate(0)
153 argv, _ := os.ReadFile(filepath.Join(e.E2EDir, "nix.argv"))
154 if !strings.Contains(string(argv), "rev="+first+"#packages."+nix.System()+".klaus\n") || strings.Contains(string(argv), ".default") {
155 t.Fatalf("self-update should build packages.%s.klaus and nothing else:\n%s", nix.System(), argv)
156 }
157 if got := e.profileKlaus(); got != e.out(first) {
158 t.Fatalf("profile klaus = %q, want %q", got, e.out(first))
159 }
160
161 e.flag("noklaus", true)
162 e.advance("second")
163 e.assertRefused()
164}
165
166// TestSelfUpdateRefusesAFailedTipWithoutThePackage: from an install and a
167// failure the state file remembers, a flake that offers no klaus package is
168// refused, not reported as the remembered failure. Pointed at a repository
169// whose flake offers it, the next run installs from there.
170func TestSelfUpdateRefusesAFailedTipWithoutThePackage(t *testing.T) {
171 t.Parallel()
172 e := newSelfUpdateEnv(t)
173 first := e.tip()
174 e.selfUpdate(0)
175 e.flag("build.fail", true)
176 second := e.advance("second")
177 e.selfUpdate(1)
178 e.flag("build.fail", false)
179 if st := e.state(); st.Current == nil || st.Current.Commit != first || st.Failed == nil || st.Failed.Commit != second {
180 t.Fatalf("the state should remember the install of %s and the failure of %s: %+v", first[:7], second[:7], st)
181 }
182
183 if err := os.WriteFile(filepath.Join(e.E2EDir, "noklaus"), []byte(e.OriginDir), 0o644); err != nil {
184 t.Fatal(err)
185 }
186 if res := e.assertRefused(); strings.Contains(res.Stdout+res.Stderr, "skipping") {
187 t.Errorf("the remembered failure was reported instead of the refusal:\n%s%s", res.Stdout, res.Stderr)
188 }
189
190 other := filepath.Join(e.E2EDir, "flake-with-klaus.git")
191 e.git(e.E2EDir, "clone", "-q", "--bare", e.OriginDir, other)
192 e.git(e.RepoDir, "commit", "-q", "--allow-empty", "-m", "third")
193 e.git(e.RepoDir, "push", "-q", other, "HEAD:main")
194 third := e.tip()
195 e.writeHostConfig(map[string]any{"self_update": map[string]any{"repo": other, "branch": "main"}})
196 e.selfUpdate(0)
197 if got := e.profileKlaus(); got != e.out(third) {
198 t.Errorf("profile klaus = %q, want %q", got, e.out(third))
199 }
200 if st := e.state(); st.Current == nil || st.Current.Commit != third || st.Failed != nil || st.Previous == nil || st.Previous.Commit != first {
201 t.Errorf("after the install from %s, state: %+v", other, st)
202 }
203}
204
205// assertRefused runs self-update against a flake that offers no klaus
206// package and checks that it changed nothing: it exits non-zero, builds
207// nothing, and leaves the profile and the state file as they were, byte
208// for byte.
209func (e *selfUpdateEnv) assertRefused() RunResult {
210 e.t.Helper()
211 statePath := selfupdate.StatePath(filepath.Join(e.Home, ".local", "state", "klaus"))
212 stateBefore, _ := os.ReadFile(statePath)
213 profileBefore, logBefore, buildsBefore := e.profileKlaus(), e.profileLog(), e.builds()
214 res := e.selfUpdate(1)
215 if n := e.builds(); n != buildsBefore {
216 e.t.Errorf("%d builds, want %d: a refused repository must not build", n, buildsBefore)
217 }
218 if got, _ := os.ReadFile(statePath); !bytes.Equal(got, stateBefore) {
219 e.t.Errorf("the refusal changed the state file:\nbefore %s\nafter %s", stateBefore, got)
220 }
221 if e.profileKlaus() != profileBefore || e.profileLog() != logBefore {
222 e.t.Errorf("the refusal changed the profile: klaus %q, log:\n%s", e.profileKlaus(), e.profileLog())
223 }
224 return res
225}
226
227func TestSelfUpdateRefusesWithoutConfig(t *testing.T) {
228 t.Parallel()
229 e := newSelfUpdateEnv(t)
230 e.writeHostConfig(map[string]any{})
231 res := e.selfUpdate(1)
232 if !strings.Contains(res.Stderr, "self-update is off") || !strings.Contains(res.Stderr, `"self_update"`) {
233 t.Errorf("want a refusal that shows how to enable self_update, got:\n%s", res.Stderr)
234 }
235 if e.builds() != 0 {
236 t.Error("built without config")
237 }
238}
239
240// TestSelfUpdateRefusesExtRepo: an ext:: repo, whose command git would run
241// here since git config allows the transport, is refused and runs nothing.
242func TestSelfUpdateRefusesExtRepo(t *testing.T) {
243 t.Parallel()
244 e := newSelfUpdateEnv(t)
245 marker := filepath.Join(e.E2EDir, "pwned")
246 e.WriteStub("pwn", fmt.Sprintf("#!/bin/sh\ntouch %q\n", marker))
247 f, err := os.OpenFile(filepath.Join(e.Home, ".gitconfig"), os.O_APPEND|os.O_WRONLY, 0)
248 if err != nil {
249 t.Fatal(err)
250 }
251 fmt.Fprint(f, "[protocol \"ext\"]\n\tallow = always\n")
252 f.Close()
253
254 for _, repo := range []string{"ext::pwn", "ext::sh -c pwn"} {
255 e.writeHostConfig(map[string]any{"self_update": map[string]any{"repo": repo}})
256 res := e.selfUpdate(1)
257 if !strings.Contains(res.Stderr, fmt.Sprintf("self_update.repo %q is not a repo self-update accepts", repo)) {
258 t.Errorf("repo %q: want a refusal naming it, got:\n%s", repo, res.Stderr)
259 }
260 if _, err := os.Stat(marker); err == nil {
261 t.Fatalf("repo %q: the ext:: command ran", repo)
262 }
263 }
264 if e.builds() != 0 {
265 t.Error("built from a refused repo")
266 }
267}
268
269// TestSelfUpdateInstallsMovesAndRollsBack: install the tip, stay put while
270// it is installed, install the next tip when main moves, and roll back and
271// forward again.
272func TestSelfUpdateInstallsMovesAndRollsBack(t *testing.T) {
273 t.Parallel()
274 e := newSelfUpdateEnv(t)
275 first := e.tip()
276
277 res := e.selfUpdate(0)
278 for _, want := range []string{
279 "main is at " + first[:7],
280 "building git+file://" + e.OriginDir + "?ref=refs/heads/main&rev=" + first + "#packages.",
281 "built " + e.out(first),
282 "smoke test passed: klaus version 0.4.1-" + first[:7] + "; klaus status exits 0",
283 "installed " + first[:7] + " into the nix profile, replacing nothing",
284 } {
285 if !strings.Contains(res.Stdout, want) {
286 t.Errorf("first run: missing %q in:\n%s", want, res.Stdout)
287 }
288 }
289 if got := e.profileKlaus(); got != e.out(first) {
290 t.Fatalf("profile klaus = %q, want %q", got, e.out(first))
291 }
292 if st := e.state(); st.Current == nil || st.Current.Commit != first || st.Previous != nil {
293 t.Fatalf("state after first install: %+v", st)
294 }
295
296 // Up to date: no build, and nothing said unless -v.
297 if res := e.selfUpdate(0); res.Stdout != "" {
298 t.Errorf("up-to-date run printed:\n%s", res.Stdout)
299 }
300 if res := e.selfUpdate(0, "-v"); !strings.Contains(res.Stdout, "main is at "+first[:7]+": klaus is up to date") {
301 t.Errorf("-v up-to-date run printed:\n%s", res.Stdout)
302 }
303 if n := e.builds(); n != 1 {
304 t.Fatalf("%d builds, want 1: up-to-date runs must not build", n)
305 }
306
307 second := e.advance("second")
308 res = e.selfUpdate(0)
309 if !strings.Contains(res.Stdout, "installed "+second[:7]+" into the nix profile, replacing "+first[:7]) {
310 t.Errorf("second run:\n%s", res.Stdout)
311 }
312 if got := e.profileKlaus(); got != e.out(second) {
313 t.Fatalf("profile klaus = %q, want %q", got, e.out(second))
314 }
315 st := e.state()
316 if st.Current == nil || st.Current.Commit != second || st.Current.StorePath != e.out(second) ||
317 st.Previous == nil || st.Previous.Commit != first || st.Previous.StorePath != e.out(first) {
318 t.Fatalf("state after the branch moved: current %+v previous %+v", st.Current, st.Previous)
319 }
320 if log := e.profileLog(); log != "add "+e.out(first)+"\nremove klaus\nadd "+e.out(second)+"\n" {
321 t.Errorf("profile operations:\n%s", log)
322 }
323
324 // A klaus removed from the profile by hand is reinstalled, and the
325 // rollback target stays.
326 if err := os.WriteFile(e.profile, nil, 0o644); err != nil {
327 t.Fatal(err)
328 }
329 if res := e.selfUpdate(0); !strings.Contains(res.Stdout, "the nix profile's klaus is no longer "+second[:7]+"; reinstalling it") {
330 t.Errorf("run after the profile lost klaus:\n%s", res.Stdout)
331 }
332 if got := e.profileKlaus(); got != e.out(second) {
333 t.Fatalf("profile klaus = %q, want it reinstalled: %q", got, e.out(second))
334 }
335 if st := e.state(); st.Current.Commit != second || st.Previous == nil || st.Previous.Commit != first {
336 t.Fatalf("state after the reinstall: current %+v previous %+v; want the rollback target kept", st.Current, st.Previous)
337 }
338
339 res = e.selfUpdate(0, "--rollback")
340 if !strings.Contains(res.Stdout, "rolled back to "+first[:7]) || !strings.Contains(res.Stdout, "--rollback again returns to "+second[:7]) {
341 t.Errorf("rollback:\n%s", res.Stdout)
342 }
343 if got := e.profileKlaus(); got != e.out(first) {
344 t.Errorf("after rollback profile klaus = %q, want %q", got, e.out(first))
345 }
346 if st := e.state(); st.Current.Commit != first || st.Previous.Commit != second {
347 t.Errorf("state after rollback: current %+v previous %+v", st.Current, st.Previous)
348 }
349 e.selfUpdate(0, "--rollback")
350 if got := e.profileKlaus(); got != e.out(second) {
351 t.Errorf("after a second rollback profile klaus = %q, want %q", got, e.out(second))
352 }
353 if st := e.state(); st.Current.Commit != second || st.Previous.Commit != first {
354 t.Errorf("state after the second rollback: current %+v previous %+v", st.Current, st.Previous)
355 }
356}
357
358// TestSelfUpdateSkipsAFailedCommit: a commit that fails its smoke test
359// installs nothing and is skipped until the branch moves or --retry.
360func TestSelfUpdateSkipsAFailedCommit(t *testing.T) {
361 t.Parallel()
362 e := newSelfUpdateEnv(t)
363 bad := e.tip()
364 e.flag("status.fail", true)
365
366 res := e.selfUpdate(1)
367 if !strings.Contains(res.Stderr, "smoke test: klaus status") || !strings.Contains(res.Stdout, bad[:7]+" failed; nothing installed") {
368 t.Errorf("failed run:\nstdout:\n%s\nstderr:\n%s", res.Stdout, res.Stderr)
369 }
370 if e.profileLog() != "" {
371 t.Fatalf("a failed build touched the profile:\n%s", e.profileLog())
372 }
373 if st := e.state(); st.Failed == nil || st.Failed.Commit != bad || !strings.Contains(st.Failed.Error, "cannot read run state") || st.Current != nil {
374 t.Fatalf("state after the failure: %+v", st)
375 }
376
377 res = e.selfUpdate(0)
378 if !strings.Contains(res.Stdout, "skipping "+bad[:7]+": it failed at") || !strings.Contains(res.Stdout, "cannot read run state") {
379 t.Errorf("rerun should skip the failed commit, naming the failure:\n%s", res.Stdout)
380 }
381 if n := e.builds(); n != 1 {
382 t.Fatalf("%d builds, want 1: a known failure must not rebuild", n)
383 }
384
385 // --retry builds it again; it still fails.
386 e.selfUpdate(1, "--retry")
387 if n := e.builds(); n != 2 {
388 t.Fatalf("%d builds after --retry, want 2", n)
389 }
390
391 // The branch moves to a fix: it builds and installs, and the failure clears.
392 e.flag("status.fail", false)
393 fixed := e.advance("fix")
394 e.selfUpdate(0)
395 if got := e.profileKlaus(); got != e.out(fixed) {
396 t.Errorf("profile klaus = %q, want the fix %q", got, e.out(fixed))
397 }
398 if st := e.state(); st.Failed != nil || st.Current.Commit != fixed {
399 t.Errorf("state after the fix: %+v", st)
400 }
401}
402
403func TestSelfUpdatePin(t *testing.T) {
404 t.Parallel()
405 e := newSelfUpdateEnv(t)
406 first := e.tip()
407 e.selfUpdate(0)
408
409 if res := e.selfUpdate(0, "--pin"); !strings.Contains(res.Stdout, "pinned at "+first[:7]) {
410 t.Errorf("--pin:\n%s", res.Stdout)
411 }
412 e.advance("second")
413 if res := e.selfUpdate(0); !strings.Contains(res.Stdout, "pinned at "+first[:7]+": leaving it alone until klaus self-update --unpin") {
414 t.Errorf("pinned run:\n%s", res.Stdout)
415 }
416 if n := e.builds(); n != 1 {
417 t.Fatalf("%d builds, want 1: a pinned run must not build", n)
418 }
419
420 e.selfUpdate(0, "--unpin")
421 e.selfUpdate(0)
422 if got := e.profileKlaus(); got != e.out(e.tip()) {
423 t.Errorf("after --unpin profile klaus = %q, want %q", got, e.out(e.tip()))
424 }
425}
426
427// TestSelfUpdateConcurrentRuns: a run that starts while another builds
428// exits 0 without touching anything.
429func TestSelfUpdateConcurrentRuns(t *testing.T) {
430 t.Parallel()
431 e := newSelfUpdateEnv(t)
432 e.flag("build.hold", true)
433
434 first := exec.Command(klausBinary, "self-update")
435 first.Dir, first.Env = e.E2EDir, e.env()
436 var out bytes.Buffer
437 first.Stdout, first.Stderr = &out, &out
438 if err := first.Start(); err != nil {
439 t.Fatal(err)
440 }
441 started := filepath.Join(e.E2EDir, "build.started")
442 for deadline := time.Now().Add(20 * time.Second); ; time.Sleep(50 * time.Millisecond) {
443 if _, err := os.Stat(started); err == nil {
444 break
445 }
446 if time.Now().After(deadline) {
447 t.Fatalf("first run never started building:\n%s", out.String())
448 }
449 }
450
451 res := e.selfUpdate(0)
452 if strings.TrimSpace(res.Stdout) != "another self-update is in progress; leaving it to finish" {
453 t.Errorf("second run:\n%s", res.Stdout)
454 }
455
456 e.flag("build.hold", false)
457 if err := first.Wait(); err != nil {
458 t.Fatalf("first run: %v\n%s", err, out.String())
459 }
460 if n := e.builds(); n != 1 {
461 t.Errorf("%d builds, want 1", n)
462 }
463 if got := e.profileKlaus(); got != e.out(e.tip()) {
464 t.Errorf("profile klaus = %q, want %q", got, e.out(e.tip()))
465 }
466}
467
468// TestDashboardRestartsOntoNewKlaus runs the real dashboard in a tmux pane,
469// with a nix profile's bin ahead of the klaus it runs on PATH. When a new
470// klaus appears there, the dashboard shuts down, releasing its webhook
471// port, and execs it with the same arguments and environment.
472func TestDashboardRestartsOntoNewKlaus(t *testing.T) {
473 t.Parallel()
474 h := NewHarness(t)
475 ln, err := net.Listen("tcp", "127.0.0.1:0")
476 if err != nil {
477 t.Fatal(err)
478 }
479 port := ln.Addr().(*net.TCPAddr).Port
480 ln.Close()
481 h.AmendRepoConfig(map[string]any{"webhook": map[string]any{"port": port}})
482 repo, merged := "acme/widget", time.Now().Add(-time.Minute).Format(time.RFC3339)
483 h.SeedState(&run.State{ID: "20261014-1200-aaaa0001", Type: "launch", Prompt: "seeded", TargetRepo: &repo,
484 CreatedAt: merged, MergedAt: &merged})
485
486 profileBin := filepath.Join(h.E2EDir, "profile", "bin")
487 if err := os.MkdirAll(profileBin, 0o755); err != nil {
488 t.Fatal(err)
489 }
490 pane := strings.TrimSpace(h.tmux("new-session", "-d", "-s", "dash", "-x", "120", "-y", "20", "-c", h.RepoDir, "-P", "-F", "#{pane_id}",
491 fmt.Sprintf("env KLAUS_SESSION_ID=%s PATH=%s:$PATH %s dashboard", h.SessionID, profileBin, filepath.Join(h.BinDir, "klaus"))))
492 screen := func(timeout time.Duration, want string) string {
493 t.Helper()
494 var last string
495 for deadline := time.Now().Add(timeout); time.Now().Before(deadline); time.Sleep(200 * time.Millisecond) {
496 if last = h.tmux("capture-pane", "-p", "-t", pane); strings.Contains(last, want) {
497 return last
498 }
499 }
500 t.Fatalf("screen never showed %q; last screen:\n%s", want, last)
501 return ""
502 }
503 screen(20*time.Second, "webhook: listening on 127.0.0.1:")
504 if l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port)); err == nil {
505 l.Close()
506 t.Fatal("the dashboard is not holding its webhook port")
507 }
508
509 // A new klaus lands in the profile, as self-update installs it.
510 store := filepath.Join(h.E2EDir, "store", "new-klaus", "bin")
511 if err := os.MkdirAll(store, 0o755); err != nil {
512 t.Fatal(err)
513 }
514 script := fmt.Sprintf(`#!/bin/sh
515[ "$1" = --version ] && { echo "klaus version 9.9.9-feedbee"; exit 0; }
516dir=%q
517printf '%%s\n' "$@" > "$dir/new.argv"
518printf '%%s' "$KLAUS_SESSION_ID" > "$dir/new.session"
519echo "new klaus running: $*"
520exec sleep 600
521`, h.E2EDir)
522 if err := os.WriteFile(filepath.Join(store, "klaus"), []byte(script), 0o755); err != nil {
523 t.Fatal(err)
524 }
525 if err := os.Symlink(filepath.Join(store, "klaus"), filepath.Join(profileBin, "klaus")); err != nil {
526 t.Fatal(err)
527 }
528
529 // The check runs every 30s.
530 screen(60*time.Second, "new klaus running: dashboard")
531 argv, _ := os.ReadFile(filepath.Join(h.E2EDir, "new.argv"))
532 if string(argv) != "dashboard\n" {
533 t.Errorf("new klaus argv = %q, want the dashboard's", argv)
534 }
535 if sid, _ := os.ReadFile(filepath.Join(h.E2EDir, "new.session")); string(sid) != h.SessionID {
536 t.Errorf("new klaus KLAUS_SESSION_ID = %q, want %q", sid, h.SessionID)
537 }
538 if l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port)); err != nil {
539 t.Errorf("webhook port still held after the restart: %v", err)
540 } else {
541 l.Close()
542 }
543}