gunk-dev/the-valley

receive_test.go

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