File view with blame information shown in the left gutter beside each line.

barerepo / server / internal/httpd/install_test.go
79 lines · 2.8kb · master
log files threads runs releases config jump to file t
133728e barerepo 1mo
1
package httpd
133728e barerepo 1mo
2
133728e barerepo 1mo
3
import (
133728e barerepo 1mo
4
"os/exec"
133728e barerepo 1mo
5
"strings"
133728e barerepo 1mo
6
"testing"
133728e barerepo 1mo
7
)
133728e barerepo 1mo
8
133728e barerepo 1mo
9
// Chapter 15's whole flow is one line, and the line pointed at a 404 until this existed.
133728e barerepo 1mo
10
func TestRunnerScriptIsRealShellAndCarriesTheToken(t *testing.T) {
133728e barerepo 1mo
11
script := runnerScript("https://barerepo.example/")
133728e barerepo 1mo
12
133728e barerepo 1mo
13
// The trailing slash must not survive into the urls the script builds.
133728e barerepo 1mo
14
if strings.Contains(script, "barerepo.example//") {
133728e barerepo 1mo
15
t.Error("the external url's trailing slash reached a request path")
133728e barerepo 1mo
16
}
133728e barerepo 1mo
17
for _, want := range []string{
133728e barerepo 1mo
18
// The token arrives as $1, because the setup page says sh -s <token>.
133728e barerepo 1mo
19
`TOKEN="${1:-}"`,
133728e barerepo 1mo
20
`SERVER="https://barerepo.example"`,
133728e barerepo 1mo
21
"$SERVER/runner/binary?token=$TOKEN",
133728e barerepo 1mo
22
`exec "$DIR/barerepo-runner" "$TOKEN" --server "$SERVER"`,
133728e barerepo 1mo
23
// Chapter 15: no second step, so the script attaches rather than telling you to.
133728e barerepo 1mo
24
"\"$TOKEN\"",
133728e barerepo 1mo
25
} {
133728e barerepo 1mo
26
if !strings.Contains(script, want) {
133728e barerepo 1mo
27
t.Errorf("the install script is missing %q", want)
133728e barerepo 1mo
28
}
133728e barerepo 1mo
29
}
133728e barerepo 1mo
30
133728e barerepo 1mo
31
// A script served to be piped into sh has to parse in sh.
133728e barerepo 1mo
32
cmd := exec.Command("sh", "-n")
133728e barerepo 1mo
33
cmd.Stdin = strings.NewReader(script)
133728e barerepo 1mo
34
if out, err := cmd.CombinedOutput(); err != nil {
133728e barerepo 1mo
35
t.Fatalf("sh -n rejected the install script: %v\n%s", err, out)
133728e barerepo 1mo
36
}
133728e barerepo 1mo
37
133728e barerepo 1mo
38
// With no token it must say the line to paste, not download anything.
133728e barerepo 1mo
39
cmd = exec.Command("sh", "-s")
133728e barerepo 1mo
40
cmd.Stdin = strings.NewReader(script)
133728e barerepo 1mo
41
out, err := cmd.CombinedOutput()
133728e barerepo 1mo
42
if err == nil {
133728e barerepo 1mo
43
t.Error("the script ran with no token instead of refusing")
133728e barerepo 1mo
44
}
133728e barerepo 1mo
45
if !strings.Contains(string(out), "usage: curl -sL https://barerepo.example/runner.sh") {
133728e barerepo 1mo
46
t.Errorf("the refusal does not show the line to paste: %s", out)
133728e barerepo 1mo
47
}
133728e barerepo 1mo
48
}
133728e barerepo 1mo
49
133728e barerepo 1mo
50
// The server can only hand out the binary it is running, and a wrong-arch binary is worse than none.
133728e barerepo 1mo
51
func TestRunnerScriptRefusesAnotherPlatform(t *testing.T) {
133728e barerepo 1mo
52
script := runnerScript("https://barerepo.example")
133728e barerepo 1mo
53
if !strings.Contains(script, `HAVE="`+platform()+`"`) {
133728e barerepo 1mo
54
t.Error("the script does not name the platform this server can serve")
133728e barerepo 1mo
55
}
133728e barerepo 1mo
56
if !strings.Contains(script, `if [ "$WANT" != "$HAVE" ]`) {
133728e barerepo 1mo
57
t.Error("the script does not compare the asking platform with the served one")
133728e barerepo 1mo
58
}
133728e barerepo 1mo
59
// It has to leave the reader a way through rather than only an error.
133728e barerepo 1mo
60
for _, want := range []string{"go build -o barerepo-runner ./cmd/barerepo-runner", "$TOKEN --server $SERVER"} {
133728e barerepo 1mo
61
if !strings.Contains(script, want) {
133728e barerepo 1mo
62
t.Errorf("the mismatch message does not tell the reader what to run instead: %q", want)
133728e barerepo 1mo
63
}
133728e barerepo 1mo
64
}
133728e barerepo 1mo
65
}
133728e barerepo 1mo
66
133728e barerepo 1mo
67
// Chapter 15 shows three platforms at once, so the windows half is not allowed to be a stub.
133728e barerepo 1mo
68
func TestRunnerPS1CarriesTheTokenAndTheServer(t *testing.T) {
133728e barerepo 1mo
69
ps := runnerPS1("https://barerepo.example/")
133728e barerepo 1mo
70
for _, want := range []string{
133728e barerepo 1mo
71
`$server = "https://barerepo.example"`,
133728e barerepo 1mo
72
"$server/runner/binary?token=$Token",
133728e barerepo 1mo
73
"$Token --server $server",
133728e barerepo 1mo
74
} {
133728e barerepo 1mo
75
if !strings.Contains(ps, want) {
133728e barerepo 1mo
76
t.Errorf("the powershell install is missing %q", want)
133728e barerepo 1mo
77
}
133728e barerepo 1mo
78
}
133728e barerepo 1mo
79
}
history · rawbarerepo 0.1.0