207 lines
7.5 KiB
Go
207 lines
7.5 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-catalog/modules/mesh-build-go/buildstep"
|
|
sdkstep "git.novox.be/novox/mesh-delivery/sdk/buildstep"
|
|
)
|
|
|
|
func fetchOnce(t *testing.T, m *Module, states *keptStates, args map[string]any) FetchResult {
|
|
t.Helper()
|
|
if _, err := fetchTool(t, m)(args); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
st := states.waitEnd(t)
|
|
if st.State != sdkstep.StepPassed {
|
|
t.Fatalf("the step ended %s: %s", st.State, st.Reason)
|
|
}
|
|
var res FetchResult
|
|
if err := json.Unmarshal(st.Result, &res); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return res
|
|
}
|
|
|
|
// Issue 890/AC4 (ADR 0536 §2): two fetches of one repository on one module bring the same mirror to their commit, and
|
|
// each workspace's alternates file names the mirror's objects.
|
|
func TestTwoFetchesOfARepositoryUseOneMirrorAndReferenceIt(t *testing.T) {
|
|
f := &fakeStep{gomods: "go.mod\n"}
|
|
m, states := fetchModule(t, f)
|
|
mirror := filepath.Join(m.Cache, "mirror", "novox", "mesh-sdk.git")
|
|
var workspaces []string
|
|
for i := 0; i < 2; i++ {
|
|
workspaces = append(workspaces, fetchOnce(t, m, states, fetchArgs).Workspace)
|
|
}
|
|
updates := 0
|
|
for _, l := range f.lines() {
|
|
if strings.Contains(l, "-C "+mirror+" fetch --no-tags origin "+commitSHA) {
|
|
updates++
|
|
}
|
|
if strings.Contains(l, "mirror") && !strings.Contains(l, mirror) {
|
|
t.Errorf("a command names another mirror: %s", l)
|
|
}
|
|
}
|
|
if updates != 2 {
|
|
t.Errorf("the mirror %s was updated %d times, want 2:\n%s", mirror, updates, strings.Join(f.lines(), "\n"))
|
|
}
|
|
// The fake git writes nothing, so the alternates file the SDK wrote stands until the fetch dissociates: it is gone
|
|
// at the end and git repack -a -d ran in each tree.
|
|
for _, ws := range workspaces {
|
|
tree := filepath.Join(m.Work, ws, "tree")
|
|
if _, err := os.Stat(filepath.Join(tree, ".git", "objects", "info", "alternates")); err == nil {
|
|
t.Errorf("the workspace %s still borrows from the mirror", ws)
|
|
}
|
|
if !strings.Contains(strings.Join(f.lines(), "\n"), "-C "+tree+" repack -a -d") {
|
|
t.Errorf("git repack -a -d did not run in %s", tree)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A sibling is placed through its own repository's mirror.
|
|
func TestASiblingIsPlacedThroughItsOwnMirror(t *testing.T) {
|
|
f := &fakeStep{gomods: "go.mod\n"}
|
|
m, states := fetchModule(t, f)
|
|
args := map[string]any{"repository": "novox/mesh-controller", "commit": commitSHA, "beside": []any{
|
|
map[string]any{"repository": "novox/mesh-sdk", "commit": commitSHA}}}
|
|
ws := fetchOnce(t, m, states, args).Workspace
|
|
for _, repo := range []string{"mesh-controller", "mesh-sdk"} {
|
|
mirror := filepath.Join(m.Cache, "mirror", "novox", repo+".git")
|
|
if !strings.Contains(strings.Join(f.lines(), "\n"), "-C "+mirror+" fetch --no-tags origin "+commitSHA) {
|
|
t.Errorf("the mirror of %s was not updated", repo)
|
|
}
|
|
}
|
|
if _, err := os.Stat(filepath.Join(m.Work, ws, "beside", "mesh-sdk", ".git", "objects", "info", "alternates")); err == nil {
|
|
t.Errorf("the sibling still borrows from the mirror")
|
|
}
|
|
}
|
|
|
|
// Two checks at once on one node update one mirror one after the other.
|
|
func TestTwoUpdatesOfOneMirrorNeverRunAtOnce(t *testing.T) {
|
|
var mu sync.Mutex
|
|
inside, most := 0, 0
|
|
f := &fakeStep{gomods: "go.mod\n"}
|
|
f.rule = func(line string) (r runAnswer, ok bool) {
|
|
if !strings.Contains(line, "fetch --no-tags origin") || strings.Contains(line, "--depth") {
|
|
return r, false
|
|
}
|
|
mu.Lock()
|
|
inside++
|
|
if inside > most {
|
|
most = inside
|
|
}
|
|
mu.Unlock()
|
|
sleepABit()
|
|
mu.Lock()
|
|
inside--
|
|
mu.Unlock()
|
|
return r, true
|
|
}
|
|
m, states := fetchModule(t, f)
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < 2; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
fetchTool(t, m)(fetchArgs)
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
states.waitEnd(t)
|
|
states.waitEnd(t)
|
|
if most != 1 {
|
|
t.Errorf("%d updates of the mirror ran at once", most)
|
|
}
|
|
}
|
|
|
|
// A mirror that cannot be updated does not fail the fetch: the fetch goes on without it, and the line saying so carries
|
|
// git's own words. The forge's refusal of a repository its account cannot read came out as "git -C: error:" (ace,
|
|
// 2026-10-26: git writes a bare "error:" line before the forge's message).
|
|
func TestAMirrorThatCannotBeUpdatedLetsTheFetchGoOnWithoutIt(t *testing.T) {
|
|
f := &fakeStep{gomods: "go.mod\n"}
|
|
f.rule = func(line string) (r runAnswer, ok bool) {
|
|
if strings.Contains(line, "mirror") && strings.Contains(line, " fetch ") {
|
|
return runAnswer{Status: 128, Stderr: "error: \nerror: User: 22:nox-builder with Key: 30:mesh:ace.mesh-build-go is not authorized to read jschoubben/car-hunter.\nerror: \nfatal: Could not read from remote repository.\n"}, true
|
|
}
|
|
return r, false
|
|
}
|
|
m, states := fetchModule(t, f)
|
|
var mu sync.Mutex
|
|
var said []string
|
|
m.Say = func(format string, a ...any) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
said = append(said, fmt.Sprintf(format, a...))
|
|
}
|
|
ws := fetchOnce(t, m, states, fetchArgs).Workspace
|
|
if _, err := os.Stat(filepath.Join(m.Work, ws, "tree", ".git", "objects", "info", "alternates")); err == nil {
|
|
t.Errorf("the workspace borrows from a mirror that is not there")
|
|
}
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
all := strings.Join(said, "\n")
|
|
if !strings.Contains(all, "is not used: git fetch: error: User: 22:nox-builder with Key: 30:mesh:ace.mesh-build-go is not authorized to read jschoubben/car-hunter.; fatal: Could not read from remote repository.") {
|
|
t.Errorf("the mirror's line lost git's words:\n%s", all)
|
|
}
|
|
}
|
|
|
|
func sleepABit() { time.Sleep(30 * time.Millisecond) }
|
|
|
|
// The workspace is self-contained after the fetch: no alternates file, and git finds the commit with the mirror gone,
|
|
// as a step shut off with the workspace alone needs. Real git, a local repository as the forge.
|
|
func TestTheWorkspaceIsSelfContainedAfterAFetchThroughTheMirror(t *testing.T) {
|
|
if _, err := exec.LookPath("git"); err != nil {
|
|
t.Skip("no git")
|
|
}
|
|
forge := t.TempDir()
|
|
src := filepath.Join(forge, "novox", "mesh-sdk.git")
|
|
if err := os.MkdirAll(src, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
git := func(dir string, args ...string) string {
|
|
cmd := exec.Command("git", append([]string{"-C", dir, "-c", "user.name=t", "-c", "user.email=t@example.org", "-c", "commit.gpgsign=false"}, args...)...)
|
|
cmd.Env = append(os.Environ(), "GIT_CONFIG_GLOBAL=/dev/null")
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("git %v: %v %s", args, err, out)
|
|
}
|
|
return strings.TrimSpace(string(out))
|
|
}
|
|
git(src, "init", "-q")
|
|
if err := os.WriteFile(filepath.Join(src, "f.txt"), []byte("one\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
git(src, "add", ".")
|
|
git(src, "commit", "-q", "-m", "one")
|
|
commit := git(src, "rev-parse", "HEAD")
|
|
|
|
states := newStates()
|
|
m := moduleWith(states, "go1.27.1")
|
|
m.Home, m.Work, m.Cache = t.TempDir(), t.TempDir(), t.TempDir()
|
|
m.Run = StepRunner(m.Home)
|
|
m.RemoteBase = "file://" + forge + "/"
|
|
m.FetchOptions = []buildstep.Option{buildstep.AllowFileRemote()}
|
|
ws := fetchOnce(t, m, states, map[string]any{"repository": "novox/mesh-sdk", "commit": commit}).Workspace
|
|
tree := filepath.Join(m.Work, ws, "tree")
|
|
if _, err := os.Stat(filepath.Join(tree, ".git", "objects", "info", "alternates")); err == nil {
|
|
t.Errorf("the workspace still has an alternates file")
|
|
}
|
|
if err := os.RemoveAll(filepath.Join(m.Cache, "mirror")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if out, err := exec.Command("git", "-C", tree, "cat-file", "-e", commit+"^{commit}").CombinedOutput(); err != nil {
|
|
t.Errorf("git does not find the commit with the mirror gone: %v %s", err, out)
|
|
}
|
|
if out, err := exec.Command("git", "-C", tree, "fsck", "--connectivity-only").CombinedOutput(); err != nil {
|
|
t.Errorf("the workspace is not whole: %v %s", err, out)
|
|
}
|
|
}
|