| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "io" |
| 6 | "net/http" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "strings" |
| 10 | "syscall" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "the-valley/webui/export" |
| 15 | "the-valley/webui/export/exporttest" |
| 16 | ) |
| 17 | |
| 18 | // The test binary is valley-web when VALLEY_WEB_TEST_MAIN is set, so the |
| 19 | // tests run the real program as a process of its own. |
| 20 | func TestMain(m *testing.M) { |
| 21 | if os.Getenv("VALLEY_WEB_TEST_MAIN") == "1" { |
| 22 | main() |
| 23 | os.Exit(0) |
| 24 | } |
| 25 | os.Exit(m.Run()) |
| 26 | } |
| 27 | |
| 28 | func valleyWeb(args ...string) *exec.Cmd { |
| 29 | cmd := exec.Command(os.Args[0], args...) |
| 30 | cmd.Env = append(os.Environ(), "VALLEY_WEB_TEST_MAIN=1") |
| 31 | return cmd |
| 32 | } |
| 33 | |
| 34 | // valley-web serves a state directory the fixture's export was uploaded |
| 35 | // to, on the address it prints, and stops cleanly on SIGTERM. |
| 36 | func TestServesTheStateDirectory(t *testing.T) { |
| 37 | f := exporttest.New(t) |
| 38 | state := t.TempDir() |
| 39 | store, err := export.OpenStore(state, nil) |
| 40 | if err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | f.Upload(t, store) |
| 44 | |
| 45 | cmd := valleyWeb("--state", state, "--listen", "127.0.0.1:0") |
| 46 | stderr, err := cmd.StderrPipe() |
| 47 | if err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | if err := cmd.Start(); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | t.Cleanup(func() { cmd.Process.Kill(); cmd.Wait() }) |
| 54 | lines := make(chan string, 1) |
| 55 | go func() { |
| 56 | r := bufio.NewReader(stderr) |
| 57 | line, _ := r.ReadString('\n') |
| 58 | lines <- line |
| 59 | io.Copy(io.Discard, r) |
| 60 | }() |
| 61 | var addr string |
| 62 | select { |
| 63 | case line := <-lines: |
| 64 | var ok bool |
| 65 | if addr, ok = strings.CutPrefix(strings.TrimSpace(line), "valley-web: listening on "); !ok { |
| 66 | t.Fatalf("valley-web printed %q", line) |
| 67 | } |
| 68 | case <-time.After(30 * time.Second): |
| 69 | t.Fatal("valley-web did not say where it listens") |
| 70 | } |
| 71 | |
| 72 | for target, status := range map[string]int{ |
| 73 | "/": http.StatusOK, |
| 74 | "/knots/app/oc-a000002": http.StatusOK, |
| 75 | "/source/app/tree/" + f.Export.Projects[0].SourceTip + "/": http.StatusOK, |
| 76 | "/source/site/": http.StatusNotFound, |
| 77 | } { |
| 78 | res, err := http.Get("http://" + addr + target) |
| 79 | if err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | body, _ := io.ReadAll(res.Body) |
| 83 | res.Body.Close() |
| 84 | if res.StatusCode != status { |
| 85 | t.Errorf("GET %s answered %d, not %d:\n%s", target, res.StatusCode, status, body) |
| 86 | } |
| 87 | if !strings.HasPrefix(res.Header.Get("Content-Security-Policy"), "default-src 'self';") { |
| 88 | t.Errorf("GET %s answered with no policy", target) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if err := cmd.Process.Signal(syscall.SIGTERM); err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | if err := cmd.Wait(); err != nil { |
| 96 | t.Errorf("valley-web stopped with %v", err) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // A command line it cannot run with gets the usage and exit status 2; |
| 101 | // --help gets the usage alone. |
| 102 | func TestUsage(t *testing.T) { |
| 103 | state := t.TempDir() |
| 104 | for _, args := range [][]string{ |
| 105 | {}, |
| 106 | {"--state", state}, |
| 107 | {"--listen", "127.0.0.1:0"}, |
| 108 | {"--state", state, "--listen", "127.0.0.1:0", "extra"}, |
| 109 | {"--no-such-flag"}, |
| 110 | } { |
| 111 | out, err := valleyWeb(args...).CombinedOutput() |
| 112 | exit, ok := err.(*exec.ExitError) |
| 113 | if !ok || exit.ExitCode() != 2 || !strings.Contains(string(out), "usage: valley-web --state DIR --listen ADDR") { |
| 114 | t.Errorf("valley-web %q gave %v:\n%s", args, err, out) |
| 115 | } |
| 116 | } |
| 117 | out, err := valleyWeb("--help").CombinedOutput() |
| 118 | if err != nil || !strings.HasPrefix(string(out), "usage: valley-web") { |
| 119 | t.Errorf("valley-web --help gave %v:\n%s", err, out) |
| 120 | } |
| 121 | } |