gunk-dev/the-valley

telemetry_test.go

source6a6791929c36main's published tip history
3.1 KiB
1package valley
2
3import (
4 "context"
5 "errors"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10)
11
12// TestFollowAllStreams runs Follow against a fake valley: the follow names
13// no source, so one follow carries every stream; records whose type can move
14// a change wake, others don't; and the next follow resumes from the cursor,
15// one position per stream, that the last one ended with.
16func TestFollowAllStreams(t *testing.T) {
17 dir, bin := t.TempDir(), t.TempDir()
18 const cursor = "integrator=0123456789abcdef:375,report.ci=fedcba9876543210:12,worker=00112233445566ff:75710"
19 script := `#!/bin/sh
20echo "$@" >> ` + filepath.Join(dir, "argv") + `
21cat <<'EOF'
22{"kind":"header","format":"valley-telemetry/v1","op":"follow"}
23{"kind":"record","stream":"worker","seq":75709,"type":"change.seen","data":{}}
24{"kind":"record","stream":"integrator","seq":375,"type":"decision","data":{}}
25{"kind":"record","stream":"report.ci","seq":12,"type":"command","data":{}}
26not a line of the answer
27{"kind":"record","stream":"worker","seq":75710,"type":"evidence.stored","data":{}}
28{"kind":"heartbeat","cursor":"integrator=0123456789abcdef:374"}
29{"kind":"record","stream":"worker","seq":75711,"type":"batch.end","data":{}}
30{"kind":"end","cursor":"` + cursor + `","truncated":false}
31EOF
32`
33 if err := os.WriteFile(filepath.Join(bin, "valley"), []byte(script), 0o755); err != nil {
34 t.Fatal(err)
35 }
36 t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
37
38 wakes := 0
39 got, err := Follow(context.Background(), dir, "", func() { wakes++ })
40 if err != nil {
41 t.Fatalf("Follow = %v", err)
42 }
43 if got != cursor {
44 t.Errorf("Follow returned cursor %q, want the end line's %q", got, cursor)
45 }
46 if wakes != 3 {
47 t.Errorf("Follow woke %d times, want 3: the decision, the evidence and the batch's end", wakes)
48 }
49 if _, err := Follow(context.Background(), dir, got, func() {}); err != nil {
50 t.Fatalf("resumed Follow = %v", err)
51 }
52 argv, _ := os.ReadFile(filepath.Join(dir, "argv"))
53 if want := "telemetry --json follow\ntelemetry --json follow --since " + cursor + "\n"; string(argv) != want {
54 t.Errorf("valley ran:\n%s\nwant:\n%s", argv, want)
55 }
56}
57
58// TestFollowRefusedIsAnError: a host that refuses the follow, as when the
59// principal's follow slots are all taken, fails the follow with valley's
60// reason and no cursor to resume from.
61func TestFollowRefusedIsAnError(t *testing.T) {
62 bin := t.TempDir()
63 script := `#!/bin/sh
64echo '{"kind":"error","error":"the principal already has 4 observe requests open; try again when one ends"}'
65echo "valley: the host refused: the principal already has 4 observe requests open; try again when one ends" >&2
66exit 1
67`
68 if err := os.WriteFile(filepath.Join(bin, "valley"), []byte(script), 0o755); err != nil {
69 t.Fatal(err)
70 }
71 t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
72
73 got, err := Follow(context.Background(), t.TempDir(), "", func() { t.Error("a refused follow woke") })
74 var e *Error
75 if !errors.As(err, &e) || !strings.Contains(e.Reason, "observe requests open") {
76 t.Fatalf("Follow = %q, %v; want valley's refusal", got, err)
77 }
78 if got != "" {
79 t.Errorf("a refused follow returned cursor %q", got)
80 }
81}