package main import ( "bytes" "encoding/json" "net/http/httptest" "os" "os/exec" "path/filepath" "runtime" "strings" "testing" ) func TestDedicatedCommitBundleOverlayAndIsolation(t *testing.T) { if _, e := exec.LookPath("git"); e != nil { t.Skip("Git unavailable") } s := testServer(t) s.workspaceRoot = t.TempDir() original := filepath.Join(s.workspaceRoot, "human") os.MkdirAll(original, 0700) client := t.TempDir() git := func(work string, args ...string) string { t.Helper() out, e := gitCommandFor(work, nil, 60e9, args...) if e != nil { t.Fatalf("git %v: %v", args, e) } return string(bytes.TrimSpace(out)) } commit := func(work string) string { t.Helper() git(work, "add", ".") git(work, "-c", "user.name=Test", "-c", "user.email=test@example.invalid", "commit", "-qm", "snapshot") return git(work, "rev-parse", "HEAD") } git(original, "init", "-q") writeFixture(t, filepath.Join(original, "a.txt"), "human committed\n") humanHead := commit(original) writeFixture(t, filepath.Join(original, "a.txt"), "human uncommitted change\n") git(client, "init", "-q") git(client, "config", "core.autocrlf", "false") wrapperName, wrapper := "gradlew", "#!/bin/sh\nmkdir -p app/build/outputs/apk/debug\nprintf apk > app/build/outputs/apk/debug/real-name.apk\n" if runtime.GOOS == "windows" { wrapperName = "gradlew.bat" wrapper = "@echo off\r\nmkdir app\\build\\outputs\\apk\\debug 2>nul\r\necho apk>app\\build\\outputs\\apk\\debug\\real-name.apk\r\nexit /b 0\r\n" } writeFixture(t, filepath.Join(client, "a.txt"), "commit A\n") writeFixture(t, filepath.Join(client, wrapperName), wrapper) writeFixture(t, filepath.Join(client, "fixture.jks"), "must never be uploaded") a := commit(client) originalIndex, _ := os.ReadFile(filepath.Join(original, ".git", "index")) legacy := id() s.projects[legacy] = &Project{WorkspacePath: original, Files: map[string]File{}, Git: &GitSnapshot{Head: humanHead}} os.MkdirAll(filepath.Join(s.root, "projects", legacy), 0700) if e := s.save(legacy); e != nil { t.Fatal(e) } oldBuild := &Build{ID: id(), Project: legacy, Status: "success", Logs: []string{}, Artifacts: []Artifact{}} s.builds[oldBuild.ID] = oldBuild if e := s.persistBuild(oldBuild); e != nil { t.Fatal(e) } response := call(t, s, "POST", "/projects", WorkspaceRequest{WorkspacePath: original, Dedicated: true, ProjectID: legacy, Git: &GitSnapshot{Head: a}}) if response.Code != 200 { t.Fatal(response.Body.String()) } var created map[string]string json.Unmarshal(response.Body.Bytes(), &created) p := created["id"] if s.projects[p].LegacyProject != legacy { t.Fatal("legacy history link missing") } history := call(t, s, "GET", "/projects/"+p+"/builds", nil) if history.Code != 200 || !strings.Contains(history.Body.String(), oldBuild.ID) { t.Fatal("old build hidden after migration", history.Body.String()) } if s.work(p) == original { t.Fatal("dedicated workspace uses human folder") } sendBundle := func(head, base string) { t.Helper() bundle := filepath.Join(t.TempDir(), "source.bundle") args := []string{"bundle", "create", "--version=3", bundle, "HEAD", "--filter=blob:none"} if base != "" { args = append(args, "^"+base) } git(client, args...) data, e := os.ReadFile(bundle) if e != nil { t.Fatal(e) } request := httptest.NewRequest("POST", "/api/v1/projects/"+p+"/git?head="+head, bytes.NewReader(data)) request.Header.Set("Authorization", "Bearer "+s.token) w := httptest.NewRecorder() s.ServeHTTP(w, request) if w.Code != 200 { t.Fatal(w.Code, w.Body.String()) } } sync := func(head, revision, content string) int { t.Helper() w := call(t, s, "POST", "/projects/"+p+"/sync", SyncRequest{Git: &GitSnapshot{Head: head, Branch: "source-branch"}, Revision: revision, Files: map[string]File{"a.txt": file(content), wrapperName: file(wrapper)}}) if w.Code != 200 && w.Code != 400 && w.Code != 409 { t.Fatal(w.Code, w.Body.String()) } return w.Code } sendBundle(a, "") if objects := git(s.work(p), "cat-file", "--batch-all-objects", "--batch-check=%(objecttype)"); strings.Contains(objects, "blob") { t.Fatal("bundle uploaded file contents including excluded secrets") } if sync(a, "one", "uncommitted overlay A\n") != 200 { t.Fatal("initial overlay failed") } bld := &Build{ID: id(), Project: p, Revision: "one", Task: ":app:assembleDebug", Status: "queued"} s.builds[bld.ID] = bld s.projects[p].Busy = true s.run(bld) if bld.Status != "success" || len(bld.Artifacts) != 1 || bld.Artifacts[0].FileName != "real-name.apk" { t.Fatal("dedicated build did not produce original artifact name", bld.Status, bld.Logs, bld.Artifacts) } writeFixture(t, filepath.Join(s.work(p), ".gradle", "cache-marker"), "keep") writeFixture(t, filepath.Join(client, "a.txt"), "commit B\n") writeFixture(t, filepath.Join(client, "b.txt"), "locally deleted after commit\n") b := commit(client) sendBundle(b, a) if sync(b, "two", "uncommitted overlay B\n") != 200 { t.Fatal("next local commit blocked") } if git(s.work(p), "symbolic-ref", "--short", "HEAD") != "source-branch" { t.Fatal("local branch identity not preserved") } if git(s.work(p), "rev-parse", "HEAD") != b { t.Fatal("Git baseline not advanced") } if data, e := os.ReadFile(filepath.Join(s.work(p), ".gradle", "cache-marker")); e != nil || string(data) != "keep" { t.Fatal("cache removed") } oldBlob := git(client, "rev-parse", a+":a.txt") git(s.work(p), "update-index", "--cacheinfo", "100644,"+oldBlob+",a.txt") if sync(b, "staged-blocked", "overwrite") != 409 { t.Fatal("manual index edit overwritten") } git(s.work(p), "read-tree", b) writeFixture(t, filepath.Join(s.work(p), "a.txt"), "manual remote edit\n") if sync(b, "blocked", "overwrite") != 409 { t.Fatal("manual edit overwritten") } writeFixture(t, filepath.Join(s.work(p), "a.txt"), "uncommitted overlay B\n") writeFixture(t, filepath.Join(client, "a.txt"), "commit C\n") c := commit(client) if sync(c, "missing-bundle", "overlay C\n") != 400 { t.Fatal("unimported commit accepted") } if s.projects[p].Revision != "" || s.projects[p].Pending == nil || s.projects[p].Pending.Files["a.txt"].Data != nil { t.Fatal("failed sync not journaled safely") } state, e := os.ReadFile(filepath.Join(s.root, "projects", p, "state.json")) if e != nil { t.Fatal(e) } var restarted Project json.Unmarshal(state, &restarted) s.projects[p] = &restarted sendBundle(c, b) if sync(c, "three", "overlay C\n") != 200 { t.Fatal("interrupted sync cannot recover") } if data, e := os.ReadFile(filepath.Join(original, "a.txt")); e != nil || string(data) != "human uncommitted change\n" { t.Fatal("human source modified") } afterIndex, _ := os.ReadFile(filepath.Join(original, ".git", "index")) if !bytes.Equal(originalIndex, afterIndex) { t.Fatal("human Git index changed") } if git(original, "rev-parse", "HEAD") != humanHead { t.Fatal("human Git changed") } // 另一个客户端绑定同一来源,也必须拥有独立的工作区。 response = call(t, s, "POST", "/projects", WorkspaceRequest{WorkspacePath: original, Dedicated: true, Git: &GitSnapshot{Head: c}}) json.Unmarshal(response.Body.Bytes(), &created) if response.Code != 200 || created["id"] == p { t.Fatal("clients share workspace") } }