package main import ( "encoding/json" "net/http" "net/http/httptest" "os" "path/filepath" "strings" "testing" "time" ) func TestIdempotentBuildCanBeFoundAfterLostResponse(t *testing.T) { s := testServer(t) p := project(t, s) wrapper := "#!/bin/sh\nexit 1\n" name := "gradlew" if strings.Contains(os.Getenv("OS"), "Windows") { name = "gradlew.bat" wrapper = "@exit /b 1\r\n" } call(t, s, "POST", "/projects/"+p+"/sync", SyncRequest{Revision: "rev", Files: map[string]File{name: file(wrapper)}}) body := map[string]string{"revision": "rev", "task": ":app:assembleDebug", "clientRequestId": "persisted-before-submit"} first := call(t, s, "POST", "/projects/"+p+"/builds", body) if first.Code != 200 { t.Fatal(first.Body.String()) } var b Build json.Unmarshal(first.Body.Bytes(), &b) again := call(t, s, "POST", "/projects/"+p+"/builds", body) var repeated Build json.Unmarshal(again.Body.Bytes(), &repeated) if repeated.ID != b.ID { t.Fatal("duplicate build submitted") } found := call(t, s, "GET", "/projects/"+p+"/builds?clientRequestId=persisted-before-submit", nil) if !strings.Contains(found.Body.String(), b.ID) { t.Fatal("lost response cannot be recovered") } body["revision"] = "different" if call(t, s, "POST", "/projects/"+p+"/builds", body).Code != 409 { t.Fatal("idempotency key accepted different source") } deadline := time.Now().Add(5 * time.Second) for { var state Build json.Unmarshal(call(t, s, "GET", "/builds/"+b.ID, nil).Body.Bytes(), &state) if state.Status != "queued" && state.Status != "running" { break } if time.Now().After(deadline) { t.Fatal("test build stuck") } time.Sleep(20 * time.Millisecond) } data, e := os.ReadFile(filepath.Join(s.root, "artifacts", b.ID, "build.json")) if e != nil { t.Fatal(e) } var stored Build if json.Unmarshal(data, &stored) != nil || stored.RequestID != "persisted-before-submit" { t.Fatal("submission not durable") } } func TestInterruptedBuildRecoverableButWorkspaceLocked(t *testing.T) { s := testServer(t) p := project(t, s) b := &Build{ID: id(), Project: p, Status: "running", RequestID: "recover-me", Logs: []string{"previous log"}} s.builds[b.ID] = b s.persistBuild(b) os.WriteFile(filepath.Join(s.root, "projects", p, "active-build"), []byte(b.ID), 0600) s.recoverBuilds() if b.Status != "interrupted" || !s.projects[p].Busy { t.Fatal("unsafe recovery state") } if w := call(t, s, "GET", "/builds/"+b.ID, nil); w.Code != 200 { t.Fatal(w.Code) } if w := call(t, s, "POST", "/projects/"+p+"/sync", SyncRequest{Revision: "new", Files: map[string]File{}}); w.Code != 409 { t.Fatal("locked workspace accepted sync") } } func TestExactInstalledGradleReusedAndDifferentVersionRejected(t *testing.T) { s := testServer(t) p := project(t, s) work := s.work(p) os.MkdirAll(filepath.Join(work, "gradle", "wrapper"), 0700) os.WriteFile(filepath.Join(work, "gradle", "wrapper", "gradle-wrapper.properties"), []byte("distributionUrl=https\\://services.gradle.org/distributions/gradle-8.13-bin.zip\n"), 0600) home := filepath.Join(s.root, "Gradle home 中文") os.MkdirAll(filepath.Join(home, "lib"), 0700) os.MkdirAll(filepath.Join(home, "bin"), 0700) os.WriteFile(filepath.Join(home, "lib", "gradle-launcher-8.13.jar"), nil, 0600) for _, n := range []string{"gradle", "gradle.bat"} { os.WriteFile(filepath.Join(home, "bin", n), nil, 0700) } cfg := BuildEnvironment{GradleHome: home, GradleUserHome: filepath.Join(s.root, "cache")} _, info, e := s.prepareCommand(work, ":app:assembleDebug", cfg) if e != nil || info["execution"] != "installed-gradle" { t.Fatalf("%v %v", e, info) } os.Rename(filepath.Join(home, "lib", "gradle-launcher-8.13.jar"), filepath.Join(home, "lib", "gradle-launcher-8.12.jar")) _, info, e = s.prepareCommand(work, ":app:assembleDebug", cfg) if e != nil || info["execution"] != "project-wrapper" { t.Fatal("wrong Gradle version reused") } } func TestProxyAppliedToGradleAndProgressPersists(t *testing.T) { proxyHit := make(chan bool, 1) proxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { select { case proxyHit <- true: default: } http.Error(w, "test proxy denies external network", 403) })) defer proxy.Close() s := testServer(t) p := project(t, s) work := s.work(p) os.MkdirAll(filepath.Join(work, "gradle", "wrapper"), 0700) os.WriteFile(filepath.Join(work, "gradle", "wrapper", "gradle-wrapper.properties"), []byte("distributionUrl=https\\://services.gradle.org/distributions/gradle-8.13-bin.zip\n"), 0600) cfg := BuildEnvironment{GradleUserHome: filepath.Join(s.root, "cache"), Proxy: proxy.URL} raw, target := distributionTarget(work, cfg) if raw == "" { t.Fatal("distribution not detected") } os.MkdirAll(filepath.Dir(target), 0700) os.WriteFile(target, make([]byte, 128*1024), 0600) b := &Build{ID: id(), Project: p, Status: "running", Environment: map[string]string{"execution": "project-wrapper"}} stop := make(chan struct{}) done := make(chan struct{}) go func() { s.monitorDistribution(b, work, cfg, stop); close(done) }() select { case <-proxyHit: case <-time.After(4 * time.Second): t.Fatal("system/configured proxy not used") } time.Sleep(1500 * time.Millisecond) close(stop) <-done s.mu.Lock() progress := b.Progress s.mu.Unlock() if progress.Current != 128*1024 || progress.Path != target { t.Fatalf("missing live progress: %+v", progress) } if _, e := os.Stat(filepath.Join(s.root, "artifacts", b.ID, "build.json")); e != nil { t.Fatal("progress not persisted") } if !strings.Contains(strings.Join(cfg.environ(), "\n"), "-Dhttps.proxyHost=127.0.0.1") { t.Fatal("proxy not forwarded to Gradle JVM") } }