Files
RemoteCompilation/server/main_test.go
T

189 lines
6.0 KiB
Go

package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
)
func testServer(t *testing.T) *Server {
t.Helper()
return &Server{root: t.TempDir(), token: "test-token-abcdefghijklmnopqrstuvwxyz", tasks: map[string]bool{":app:assembleDebug": true}, projects: map[string]*Project{}, builds: map[string]*Build{}}
}
func TestRunningCancellation(t *testing.T) {
s := testServer(t)
p := project(t, s)
name, wrapper := "gradlew", "#!/bin/sh\nsleep 20\n"
if runtime.GOOS == "windows" {
name = "gradlew.bat"
wrapper = "@echo off\r\nping 127.0.0.1 -n 20 >nul\r\n"
}
os.WriteFile(filepath.Join(s.work(p), name), []byte(wrapper), 0600)
b := &Build{ID: id(), Project: p, Revision: "r", Task: ":app:assembleDebug", Status: "queued", Logs: []string{}}
s.builds[b.ID] = b
s.projects[p].Busy = true
done := make(chan struct{})
go func() { s.run(b); close(done) }()
deadline := time.Now().Add(5 * time.Second)
for {
s.mu.Lock()
started := b.cmd != nil && b.cmd.Process != nil
s.mu.Unlock()
if started {
break
}
if time.Now().After(deadline) {
t.Fatal("executor failed to start")
}
time.Sleep(10 * time.Millisecond)
}
if w := call(t, s, "DELETE", "/builds/"+b.ID, nil); w.Code != 200 {
t.Fatal(w.Body.String())
}
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("cancel did not stop process tree")
}
if b.Status != "cancelled" || s.projects[p].Busy {
t.Fatal("incorrect cancellation state")
}
}
func TestSSEReplay(t *testing.T) {
s := testServer(t)
b := &Build{ID: id(), Status: "success", Logs: []string{"first", "second"}}
s.builds[b.ID] = b
r := httptest.NewRequest("GET", "/api/v1/builds/"+b.ID+"/logs", nil)
r.Header.Set("Authorization", "Bearer "+s.token)
r.Header.Set("Last-Event-ID", "1")
w := httptest.NewRecorder()
s.ServeHTTP(w, r)
if strings.Contains(w.Body.String(), "first") || !strings.Contains(w.Body.String(), "second") {
t.Fatal("SSE replay offset ignored")
}
}
func call(t *testing.T, s *Server, method, path string, body any) *httptest.ResponseRecorder {
t.Helper()
b, _ := json.Marshal(body)
r := httptest.NewRequest(method, "/api/v1"+path, bytes.NewReader(b))
r.Header.Set("Authorization", "Bearer "+s.token)
w := httptest.NewRecorder()
s.ServeHTTP(w, r)
return w
}
func project(t *testing.T, s *Server) string {
w := call(t, s, "POST", "/projects", nil)
if w.Code != 200 {
t.Fatal(w.Body.String())
}
var v map[string]string
json.Unmarshal(w.Body.Bytes(), &v)
return v["id"]
}
func file(data string) File {
encoded := base64.StdEncoding.EncodeToString([]byte(data))
return File{Hash: digest([]byte(data)), Data: &encoded}
}
func TestPaths(t *testing.T) {
for _, p := range []string{"../x", "/x", "C:/x", "a\\b", "a/../x", "a//b", "CON.txt", "a./x", ".git/config", "app/build/x"} {
if validPath(p) {
t.Errorf("accepted %q", p)
}
}
if !validPath("app/src/中文 a.kt") {
t.Fatal("valid unicode path rejected")
}
}
func TestAuthentication(t *testing.T) {
s := testServer(t)
w := httptest.NewRecorder()
s.ServeHTTP(w, httptest.NewRequest("GET", "/api/v1/health", nil))
if w.Code != 401 {
t.Fatal(w.Code)
}
}
func TestSyncAndRevision(t *testing.T) {
s := testServer(t)
p := project(t, s)
path := "/projects/" + p + "/sync"
req := SyncRequest{"one", map[string]File{"a.txt": file("hello"), "b.txt": file("remove me")}}
if w := call(t, s, "POST", path, req); w.Code != 200 {
t.Fatal(w.Body.String())
}
bad := SyncRequest{"bad", map[string]File{"a.txt": {Hash: "wrong"}}}
if w := call(t, s, "POST", path, bad); w.Code != 400 {
t.Fatal(w.Code)
}
if s.projects[p].Revision != "one" {
t.Fatal("invalid sync changed revision")
}
req = SyncRequest{"two", map[string]File{"a.txt": {Hash: digest([]byte("hello"))}}}
if w := call(t, s, "POST", path, req); w.Code != 200 {
t.Fatal(w.Body.String())
}
if _, e := os.Stat(filepath.Join(s.work(p), "b.txt")); !os.IsNotExist(e) {
t.Fatal("deleted source retained")
}
s.projects[p].Busy = true
if w := call(t, s, "POST", path, req); w.Code != 409 {
t.Fatal("sync allowed while building")
}
s.projects[p].Busy = false
if w := call(t, s, "POST", "/projects/"+p+"/builds", map[string]string{"revision": "one", "task": ":app:assembleDebug"}); w.Code != 409 {
t.Fatal("stale revision accepted")
}
if w := call(t, s, "POST", "/projects/"+p+"/builds", map[string]string{"revision": "two", "task": "evil & command"}); w.Code != 400 {
t.Fatal("unapproved command accepted")
}
}
func TestBuildArtifactAndFailure(t *testing.T) {
s := testServer(t)
p := project(t, s)
wrapper := "#!/bin/sh\nmkdir -p app/build/outputs/apk/debug\nprintf fresh > app/build/outputs/apk/debug/app-debug.apk\n"
name := "gradlew"
if runtime.GOOS == "windows" {
name = "gradlew.bat"
wrapper = "@echo off\r\nmkdir app\\build\\outputs\\apk\\debug 2>nul\r\necho fresh>app\\build\\outputs\\apk\\debug\\app-debug.apk\r\nexit /b 0\r\n"
}
req := SyncRequest{"one", map[string]File{name: file(wrapper)}}
if w := call(t, s, "POST", "/projects/"+p+"/sync", req); w.Code != 200 {
t.Fatal(w.Body.String())
}
b := &Build{ID: id(), Project: p, Revision: "one", Task: ":app:assembleDebug", Status: "queued"}
s.builds[b.ID] = b
s.projects[p].Busy = true
s.run(b)
if b.Status != "success" || len(b.Artifacts) != 1 {
t.Fatalf("%s %v", b.Status, b.Logs)
}
a := b.Artifacts[0]
w := call(t, s, "GET", "/builds/"+b.ID+"/artifacts/"+a.Name, nil)
if w.Code != 200 || digest(w.Body.Bytes()) != a.Hash {
t.Fatal("artifact mismatch")
}
if runtime.GOOS == "windows" {
wrapper = "@exit /b 1\r\n"
} else {
wrapper = "#!/bin/sh\nexit 1\n"
}
os.WriteFile(filepath.Join(s.work(p), name), []byte(wrapper), 0600)
failed := &Build{ID: id(), Project: p, Revision: "one", Task: ":app:assembleDebug", Status: "queued"}
s.run(failed)
if failed.Status != "failed" || len(failed.Artifacts) != 0 {
t.Fatal("failed build returned artifacts")
}
cancelled := &Build{ID: id(), Project: p, Status: "queued", cancelled: true}
s.run(cancelled)
if cancelled.Status != "cancelled" || s.projects[p].Busy {
t.Fatal("queued cancellation failed")
}
}