gunk-dev/the-valley

receive_test.go

sourceccb7354855a5main's published tip history
4.9 KiB
1package main
2
3import (
4 "bufio"
5 "io"
6 "net/http"
7 "os"
8 "os/exec"
9 "path/filepath"
10 "strings"
11 "testing"
12 "time"
13
14 "the-valley/web/export/exporttest"
15)
16
17// program is the path of a program of the web UI's that the variable env
18// names: valley-web-receive or valley-web, as the nix check builds them.
19func program(t *testing.T, env string) string {
20 t.Helper()
21 path := os.Getenv(env)
22 if path == "" {
23 if os.Getenv("VALLEY_TEST_TOOLS") == "required" {
24 t.Fatalf("%s names no program, and VALLEY_TEST_TOOLS=required", env)
25 }
26 t.Skipf("%s names no program; the nix check provides one", env)
27 }
28 return path
29}
30
31// listening runs cmd, a server that prints "<name>: listening on ADDR"
32// once it listens, with exporttest.Start, so no process of it outlives
33// the test. It returns the address.
34func listening(t *testing.T, name string, cmd *exec.Cmd) string {
35 t.Helper()
36 stderr, err := cmd.StderrPipe()
37 if err != nil {
38 t.Fatal(err)
39 }
40 if _, err := exporttest.Start(t, cmd); err != nil {
41 t.Fatal(err)
42 }
43 t.Cleanup(func() { cmd.Process.Kill(); cmd.Wait() })
44 lines := make(chan string, 1)
45 go func() {
46 r := bufio.NewReader(stderr)
47 line, _ := r.ReadString('\n')
48 lines <- line
49 io.Copy(io.Discard, r)
50 }()
51 select {
52 case line := <-lines:
53 addr, ok := strings.CutPrefix(strings.TrimSpace(line), name+": listening on ")
54 if !ok {
55 t.Fatalf("%s printed %q", name, line)
56 }
57 return addr
58 case <-time.After(30 * time.Second):
59 t.Fatalf("%s did not say where it listens", name)
60 }
61 return ""
62}
63
64// page is valley-web's status and page for a GET of target.
65func page(t *testing.T, addr, target string) (int, string) {
66 t.Helper()
67 res, err := http.Get("http://" + addr + target)
68 if err != nil {
69 t.Fatal(err)
70 }
71 defer res.Body.Close()
72 body, err := io.ReadAll(res.Body)
73 if err != nil {
74 t.Fatal(err)
75 }
76 return res.StatusCode, string(body)
77}
78
79// The real publisher uploads to the real receiver, a process of its own,
80// and valley-web, another, serves what the receiver activated from its
81// state directory. A pass with another token is refused as
82// unauthenticated, and the edge keeps serving what it has. A pass after
83// anyone's grant is withdrawn takes the project off the edge.
84func TestPublishesToTheRealReceiver(t *testing.T) {
85 v := newValley(t)
86 receive, web := program(t, "VALLEY_WEB_RECEIVE"), program(t, "VALLEY_WEB")
87
88 // The export key, made as a pass makes it, so the receiver can pin its
89 // verifier line from the start.
90 if err := os.MkdirAll(v.state, 0o700); err != nil {
91 t.Fatal(err)
92 }
93 keys := &publisher{cfg: config{state: v.state}, signer: instanceName + "/web-export", stdout: io.Discard}
94 if _, err := keys.loadKey(); err != nil {
95 t.Fatal(err)
96 }
97 dir := t.TempDir()
98 token, wrong := filepath.Join(dir, "token"), filepath.Join(dir, "wrong")
99 v.write(token, "edge-token-0123456789abcdef0123456789\n")
100 v.write(wrong, "another-token-0123456789abcdef0123456\n")
101 state := filepath.Join(dir, "state")
102 receiver := "http://" + listening(t, "valley-web-receive", exec.Command(receive,
103 "--state", state, "--listen", "127.0.0.1:0", "--instance", instanceName, "--destination", destination,
104 "--verifiers", filepath.Join(v.state, verifierFile), "--token-file", token))
105
106 stdout, _ := v.publish("--receiver", receiver, "--receiver-token", token)
107 if !strings.Contains(stdout, "the receiver activated sequence") {
108 t.Errorf("the first pass did not say the receiver activated it:\n%s", stdout)
109 }
110 addr := listening(t, "valley-web", exec.Command(web, "--state", state, "--listen", "127.0.0.1:0"))
111 milestone := "/app/knots/" + v.knots["milestone"]
112 if status, body := page(t, addr, "/-/knots"); status != http.StatusOK || !strings.Contains(body, "Publish the plan") {
113 t.Errorf("valley-web answered /-/knots with %d, and no milestone of app:\n%s", status, body)
114 }
115 if status, _ := page(t, addr, milestone); status != http.StatusOK {
116 t.Errorf("valley-web answered %s with %d, want 200", milestone, status)
117 }
118 if status, _ := page(t, addr, "/site/knots/"+v.knots["site"]); status != http.StatusNotFound {
119 t.Errorf("valley-web answered site's knot with %d, want 404", status)
120 }
121
122 // A comment changes the export, so the pass uploads.
123 v.valley("app", "knot", "comment", v.knots["milestone"], "-m", "The edge is up.")
124 _, _, err := v.tryPublish(nil, "--receiver", receiver, "--receiver-token", wrong)
125 if err == nil || !strings.Contains(err.Error(), "unauthenticated") {
126 t.Errorf("a pass with another token ended with %v, want unauthenticated", err)
127 }
128 if status, body := page(t, addr, milestone); status != http.StatusOK || strings.Contains(body, "The edge is up.") {
129 t.Errorf("after the refused pass, valley-web answered %s with %d, or with the refused comment", milestone, status)
130 }
131
132 v.grant("")
133 v.publish("--receiver", receiver, "--receiver-token", token)
134 if status, _ := page(t, addr, milestone); status != http.StatusNotFound {
135 t.Errorf("valley-web answered %s with %d once anyone's grant was withdrawn, want 404", milestone, status)
136 }
137}