188 lines
11 KiB
Go
188 lines
11 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
builds "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"
|
|
sdkstep "git.novox.be/novox/mesh-delivery/sdk/buildstep"
|
|
"git.novox.be/novox/mesh-sdk/go/forgekey"
|
|
)
|
|
|
|
// Issue 1585 S15 (novox/hq ADR 0573 §2, §4): the asked version of a mesh package is written into the directory's modfile
|
|
// before anything is downloaded, whatever go.mod names, with its checksums beside it, and named in the fetch's answer: a
|
|
// trunk commit Go resolves is required at its Go version; a version go.mod names that is newer is built instead, said; a
|
|
// group's head Go cannot fetch is placed from the forge's pull request heads; a go.mod that does not require the package
|
|
// takes nothing.
|
|
func TestAnAskedVersionIsWrittenIntoTheModfileWhateverGoModNames(t *testing.T) {
|
|
const sdk = "git.novox.be/novox/mesh-sdk/go"
|
|
trunk := strings.Repeat("a", 40)
|
|
offHead := strings.Repeat("b", 40) // a commit no pull request's head carries
|
|
for _, c := range []struct {
|
|
name, asked, answer string
|
|
failing bool
|
|
built, said, edit string
|
|
failsWith string // a download failure that is not an unknown revision: the step fails with go's own words
|
|
forgeDown bool // the forge's listing of pull request heads fails too
|
|
timeouts, downloads int // downloads that time out before go answers; the downloads asked in all (0: not counted)
|
|
}{
|
|
{name: "a trunk commit newer than go.mod's", asked: trunk,
|
|
answer: `{"Path":"` + sdk + `","Version":"v0.1.15-0.20261101000000-aaaaaaaaaaaa","Sum":"h1:zip=","GoModSum":"h1:mod="}`,
|
|
built: "v0.1.15-0.20261101000000-aaaaaaaaaaaa", edit: "-require=" + sdk + "@v0.1.15-0.20261101000000-aaaaaaaaaaaa"},
|
|
{name: "go.mod names a newer one", asked: trunk,
|
|
answer: `{"Path":"` + sdk + `","Version":"v0.1.15-0.20261001000000-aaaaaaaaaaaa","Sum":"h1:zip="}`,
|
|
built: "v0.1.15-0.20261022120000-" + pinShort, said: "newer than the asked"},
|
|
{name: "a group's head Go cannot fetch", asked: commitSHA, failing: true,
|
|
answer: `{"Path":"` + sdk + `","Error":"invalid version: unknown revision ` + pinShort + `"}`,
|
|
built: commitSHA, said: "placed from the pull request's head"},
|
|
// Whether a commit is a pull request's head is the forge's fact, never read from go's words (issue 1126 review 2):
|
|
// a head is placed whatever go said (the group's check has no name lookup, so go fails on the network first), and
|
|
// go's words only label a commit on no head. The wording: "unknown revision" is cmd/go's
|
|
// (modfetch/coderepo_test.go); the lookup and connection texts are those of issue 1748 and the check of 12:42.
|
|
{name: "a head go cannot even look up is placed", asked: commitSHA, failing: true,
|
|
answer: `{"Path":"` + sdk + `","Error":"git.novox.be/novox/mesh-sdk/go@` + commitSHA + `: dial tcp: lookup git.novox.be on [::1]:53: connection refused"}`,
|
|
built: commitSHA, said: "placed from the pull request's head"},
|
|
{name: "a lookup that times out is no pin", asked: offHead, failing: true, failsWith: "i/o timeout",
|
|
answer: `{"Path":"` + sdk + `","Error":"git.novox.be/novox/mesh-sdk/go@` + offHead + `: dial tcp: lookup git.novox.be on 10.10.0.3:53: read udp 10.10.0.3:42292->10.10.0.3:53: i/o timeout"}`},
|
|
// The forge's own listing failing is a failure that may be retaken too, with its own cause (review 3).
|
|
{name: "a forge that cannot be listed is retaken", asked: commitSHA, failing: true, forgeDown: true,
|
|
failsWith: "unable to access the forge",
|
|
answer: `{"Path":"` + sdk + `","Error":"invalid version: unknown revision ` + pinShort + `"}`},
|
|
// ADR 0607 §3, §5 (design 51 *Tagged versions*): a group's tagged version is fetched by that version and built
|
|
// whatever go.mod names; one the Go seat lacks fails naming it, never looked for among the pull request heads.
|
|
{name: "a group's tagged version", asked: "v0.1.15-feat-x.2",
|
|
answer: `{"Path":"` + sdk + `","Version":"v0.1.15-feat-x.2","Sum":"h1:zip=","GoModSum":"h1:mod="}`,
|
|
built: "v0.1.15-feat-x.2", edit: "-require=" + sdk + "@v0.1.15-feat-x.2"},
|
|
{name: "a group's tagged version older than go.mod's", asked: "v0.1.14-feat-x.2",
|
|
answer: `{"Path":"` + sdk + `","Version":"v0.1.14-feat-x.2","Sum":"h1:zip=","GoModSum":"h1:mod="}`,
|
|
built: "v0.1.14-feat-x.2", said: "the group's tagged version", edit: "-require=" + sdk + "@v0.1.14-feat-x.2"},
|
|
{name: "a tagged version the Go seat lacks", asked: "v0.1.15-feat-x.3", failing: true, failsWith: "the group's tagged version v0.1.15-feat-x.3",
|
|
answer: `{"Path":"` + sdk + `","Error":"` + sdk + `@v0.1.15-feat-x.3: reading https://seat/go/@v/v0.1.15-feat-x.3.info: 404 Not Found"}`},
|
|
// Issue 1966: a network failure is taken again a bounded number of times; another failure is not.
|
|
{name: "a timeout twice is taken again", asked: trunk, timeouts: 2, downloads: 3,
|
|
answer: `{"Path":"` + sdk + `","Version":"v0.1.15-0.20261101000000-aaaaaaaaaaaa","Sum":"h1:zip=","GoModSum":"h1:mod="}`,
|
|
built: "v0.1.15-0.20261101000000-aaaaaaaaaaaa", edit: "-require=" + sdk + "@v0.1.15-0.20261101000000-aaaaaaaaaaaa"},
|
|
{name: "a timeout that stays fails after three downloads", asked: offHead, failing: true, failsWith: "taken 3 times", downloads: 3,
|
|
answer: `{"Path":"` + sdk + `","Error":"lookup git.novox.be on 10.10.0.3:53: i/o timeout"}`},
|
|
{name: "an unknown revision is not taken again", asked: offHead, failing: true, failsWith: "on no pull request head", downloads: 1,
|
|
answer: `{"Path":"` + sdk + `","Error":"invalid version: unknown revision ` + pinShort + `"}`},
|
|
{name: "a refused connection is no pin", asked: offHead, failing: true, failsWith: "connection refused",
|
|
answer: `{"Path":"` + sdk + `","Error":"git ls-remote -q origin in /tmp/gopath: dial tcp 10.10.0.1:443: connect: connection refused"}`},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
h, states := newPinHarness(t, pinnedGoMod, false, pullList)
|
|
h.f.gomods = "go.mod\nother/go.mod\n"
|
|
var paused []time.Duration
|
|
h.m.Pause = func(d time.Duration) { paused = append(paused, d) }
|
|
downloads := 0
|
|
base := h.m.Run
|
|
h.m.Run = func(ctx context.Context, env []string, name string, args ...string) forgekey.Ran {
|
|
line := name + " " + strings.Join(args, " ")
|
|
if name == "git" && strings.Contains(line, " checkout ") && !strings.Contains(line, "pinned") {
|
|
r := base(ctx, env, name, args...)
|
|
for i, a := range args {
|
|
if a == "-C" { // other/ uses no mesh package
|
|
os.WriteFile(filepath.Join(args[i+1], "other", "go.mod"), []byte("module fixture/other\n\ngo 1.26\n"), 0o644)
|
|
}
|
|
}
|
|
return r
|
|
}
|
|
if name == "go" && strings.Contains(line, "/other mod ") {
|
|
return forgekey.Ran{} // other/ requires nothing Go cannot fetch
|
|
}
|
|
if name == "git" && strings.Contains(line, "ls-remote") && c.forgeDown {
|
|
return forgekey.Ran{Status: 128, Stderr: "fatal: unable to access the forge: Could not resolve host\n"}
|
|
}
|
|
if name == "go" && strings.Contains(line, " mod download -json "+sdk+"@") {
|
|
h.mu.Lock()
|
|
h.cmds = append(h.cmds, cmdRecord{env: env, line: line})
|
|
h.mu.Unlock()
|
|
downloads++
|
|
if downloads <= c.timeouts {
|
|
return forgekey.Ran{Status: 1, Stdout: `{"Path":"` + sdk + `","Error":"lookup git.novox.be on 10.10.0.3:53: i/o timeout"}`}
|
|
}
|
|
if c.failing {
|
|
return forgekey.Ran{Status: 1, Stdout: c.answer}
|
|
}
|
|
return forgekey.Ran{Stdout: c.answer}
|
|
}
|
|
return base(ctx, env, name, args...)
|
|
}
|
|
args := map[string]any{"repository": "novox/mesh-delivery", "commit": commitSHA, "packages": map[string]any{sdk: c.asked}}
|
|
if _, err := fetchTool(t, h.m)(args); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
st := states.waitEnd(t)
|
|
if c.downloads != 0 && (downloads != c.downloads || len(paused) != c.downloads-1) {
|
|
t.Errorf("go downloaded %d times with %d waits, want %d downloads", downloads, len(paused), c.downloads)
|
|
}
|
|
if c.failsWith == "on no pull request head" { // not a network failure: the forge's heads said, no retaking
|
|
if st.State != sdkstep.StepFailed || !strings.Contains(st.Reason, c.failsWith) {
|
|
t.Errorf("the unknown revision ended %s: %q", st.State, st.Reason)
|
|
}
|
|
return
|
|
}
|
|
if c.failsWith != "" {
|
|
if st.State != sdkstep.StepFailed || !strings.Contains(st.Reason, c.failsWith) || strings.Contains(st.Reason, "no pull request head") ||
|
|
!strings.Contains(st.Reason, "may be retaken") || builds.FailedUntilChanged(st.Reason) {
|
|
t.Errorf("the download failure ended %s: %q", st.State, st.Reason)
|
|
}
|
|
return
|
|
}
|
|
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)
|
|
}
|
|
if len(res.Packages) != 1 {
|
|
t.Fatalf("packages = %+v, want one: the directory requiring mesh-sdk", res.Packages)
|
|
}
|
|
got := res.Packages[0]
|
|
if got.Dir != "." || got.Package != sdk || got.Asked != c.asked || got.Built != c.built || !strings.Contains(got.Said, c.said) {
|
|
t.Errorf("taken = %+v, want built %s saying %q", got, c.built, c.said)
|
|
}
|
|
if h.ran(" mod edit ") != (c.edit != "") || (c.edit != "" && !h.ran(c.edit)) {
|
|
t.Errorf("go mod edit %q ran: %v", c.edit, h.ran(" mod edit "))
|
|
}
|
|
modfile, ok := h.m.pinModfile(filepath.Join(h.m.Work, res.Workspace, "tree"))
|
|
if !ok {
|
|
t.Fatal("the directory was handed no modfile")
|
|
}
|
|
sums, _ := os.ReadFile(filepath.Join(filepath.Dir(modfile), "go.sum"))
|
|
if wantSum := c.edit != ""; strings.Contains(string(sums), "h1:zip=") != wantSum || strings.Contains(string(sums), "/go.mod h1:mod=") != wantSum {
|
|
t.Errorf("the checksums beside the modfile:\n%s", sums)
|
|
}
|
|
body, _ := os.ReadFile(modfile)
|
|
if placed := strings.Contains(string(body), "replace "+sdk+" => "); placed != c.failing {
|
|
t.Errorf("the modfile replaces mesh-sdk by a placed head: %v, want %v:\n%s", placed, c.failing, body)
|
|
}
|
|
if _, other := h.m.pinModfile(filepath.Join(h.m.Work, res.Workspace, "tree", "other")); other {
|
|
t.Error("a go.mod that requires no asked package was handed a modfile")
|
|
}
|
|
})
|
|
}
|
|
// The order of Go versions: a pseudo-version by its time, a release after its pre-releases, a non-version first.
|
|
for _, v := range []struct {
|
|
a, b string
|
|
want int
|
|
}{
|
|
{"v0.1.15-0.20261101000000-aaaaaaaaaaaa", "v0.1.15-0.20261022120000-0123456789ab", 1},
|
|
{"v0.1.14-0.20261031084708-acbf80ad910a", "v0.1.14-0.20261101005726-af4e4e60a3a0", -1},
|
|
{"v0.1.7", "v0.1.14-0.20261101005726-af4e4e60a3a0", -1},
|
|
{"v0.1.14", "v0.1.14-0.20261101005726-af4e4e60a3a0", 1},
|
|
{"v0.2.0", "v0.2.0", 0},
|
|
{"main", "v0.0.1", -1},
|
|
} {
|
|
if got := compareVersions(v.a, v.b); (got > 0) != (v.want > 0) || (got < 0) != (v.want < 0) {
|
|
t.Errorf("compareVersions(%s, %s) = %d, want the sign of %d", v.a, v.b, got, v.want)
|
|
}
|
|
}
|
|
}
|