Files
mesh-catalog/modules/mesh-build-go/cmd/mesh-build-go/compose_integration_test.go
T

248 lines
9.2 KiB
Go

//go:build integration
package main
import (
"context"
"encoding/json"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
"git.novox.be/novox/mesh-catalog/modules/mesh-build-go/buildstep"
catalogclient "git.novox.be/novox/mesh-delivery/contracts/go/clients/meshcatalogclient"
sdkstep "git.novox.be/novox/mesh-delivery/sdk/buildstep"
"git.novox.be/novox/mesh-delivery/sdk/composer"
"git.novox.be/novox/mesh-sdk/go/forgekey"
"git.novox.be/novox/mesh-sdk/go/testbus"
)
// nodeEngineTree is the node-engine's repository as the checker places it beside this check (MESH_CHECK_BESIDE, the
// repository mesh-host), and with it every Go module of the other repositories placed there (mesh-sdk, mesh-contracts,
// mesh-delivery, mesh-networking, the catalogue's trunk) and of the catalogue under check, first, as candidates: the workspace lays those the
// node-engine's composer requires at the zero version, transitively (workspaceOf), so they resolve as the group lays
// them, wherever the composer lives. The test
// builds the node-engine's real composer command from that tree; it does not stand one in. With no beside directory it
// fails, and with the node-engine beside but no composer command in it it fails: a composer test that could not build its
// composer proves nothing. With a beside directory that holds no node-engine at all it skips aloud (NOT JUDGED), since a
// checker from before this release places none for the catalogue.
func nodeEngineTree(t *testing.T) (tree string, modules []string) {
t.Helper()
beside := os.Getenv("MESH_CHECK_BESIDE")
if beside == "" {
t.Fatal("NOT CHECKED: MESH_CHECK_BESIDE is not set, so the node-engine's repository (mesh-host) is not beside this check")
}
tree = filepath.Join(beside, "mesh-host")
if !dirExists(tree) {
// A checker from before novox/hq ADR 0593 places only mesh-sdk beside a catalogue check, so the node-engine's
// repository is absent, not wrong: said aloud, since nothing here could build a composer to judge.
t.Skipf("NOT JUDGED: the node-engine's repository (mesh-host) is not beside this check in MESH_CHECK_BESIDE=%s; the running checker places none for the catalogue, so the composer is judged only once it does", beside)
}
if _, err := os.Stat(filepath.Join(tree, "cmd", "mesh-compose")); err != nil {
t.Fatalf("NOT CHECKED: the node-engine's composer command is not beside this check in MESH_CHECK_BESIDE=%s: %v", beside, err)
}
entries, err := os.ReadDir(beside)
if err != nil {
t.Fatal(err)
}
var roots []string
for _, e := range entries {
if e.Name() == "mesh-host" || !dirExists(filepath.Join(beside, e.Name())) {
continue
}
root, err := filepath.EvalSymlinks(filepath.Join(beside, e.Name()))
if err != nil {
t.Fatal(err)
}
roots = append(roots, root)
}
// The catalogue under check, first: beside lies its trunk (issue 1295), whose modules a head's must win over.
catalogue, err := filepath.Abs(filepath.Join("..", "..", "..", ".."))
if err != nil {
t.Fatal(err)
}
for _, root := range append([]string{catalogue}, roots...) {
filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error {
if err != nil {
return nil
}
if d.IsDir() && (d.Name() == ".git" || d.Name() == "testdata" || d.Name() == "node_modules") {
return filepath.SkipDir
}
if !d.IsDir() && d.Name() == "go.mod" {
modules = append(modules, filepath.Dir(p))
}
return nil
})
}
return tree, modules
}
func dirExists(p string) bool { st, err := os.Stat(p); return err == nil && st.IsDir() }
// copyTree copies the files of a source tree into dst, leaving out its .git.
func copyHostTree(t *testing.T, src, dst string) {
t.Helper()
src, err := filepath.EvalSymlinks(src)
if err != nil {
t.Fatal(err)
}
err = filepath.WalkDir(src, func(p string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
rel, _ := filepath.Rel(src, p)
if d.IsDir() {
if d.Name() == ".git" {
return filepath.SkipDir
}
return os.MkdirAll(filepath.Join(dst, rel), 0o700)
}
if !d.Type().IsRegular() {
return nil
}
raw, err := os.ReadFile(p)
if err != nil {
return err
}
return os.WriteFile(filepath.Join(dst, rel), raw, 0o600)
})
if err != nil {
t.Fatal(err)
}
}
// goEnvOf is one value of this machine's go env.
func goEnvOf(t *testing.T, name string) string {
t.Helper()
out, err := exec.Command("go", "env", name).Output()
if err != nil {
t.Fatalf("go env %s: %v", name, err)
}
return strings.TrimSpace(string(out))
}
// 1622/AC1, AC3 against the real bus (ADR 0593 §7, C9): a compose ask put on the Go build seat's check stream is pulled by
// the module started whole on that bus, which fetches the commit (the node-engine's repository beside the check, its Go module at the root), builds the composer command the ask names there with nothing fetched, fills the
// request's manifests from the indexer's state and runs the composer; the step key <node>.<ask id> passes with the
// composer's verdict. A second ask of the same commit runs the kept composer: fetched and built once.
func TestAComposeAskIsBuiltOnceAndAnsweredOnTheBus(t *testing.T) {
nc, err := nats.Connect(testbus.URL(t))
if err != nil {
t.Fatal(err)
}
defer nc.Close()
js, err := jetstream.New(nc)
if err != nil {
t.Fatal(err)
}
ctx, stop := context.WithCancel(context.Background())
defer stop()
if err := buildstep.DeclareAskWorkers(ctx, js, sdkstep.AskGo, 2*time.Minute); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = js.DeleteStream(context.Background(), "SEAT_NODE_BUILD_GO") })
states := newStates()
m := moduleWith(states, "go1.26.0")
m.Work, m.Cache, m.Inside = t.TempDir(), t.TempDir(), true
m.Capacity = func() int { return 1 } // its own slot: no settings file, and a node with no capacity set pulls nothing (ADR 0567 §15)
m.Run = StepRunner(t.TempDir())
m.GoModCache = goEnvOf(t, "GOMODCACHE") // the module cache this module's own build filled, read and not fetched
run, gocache := m.Run, goEnvOf(t, "GOCACHE") // this machine's build cache, so the composer's packages compile once
hostTree, beside := nodeEngineTree(t)
workFile := ""
m.Run = func(ctx context.Context, env []string, name string, args ...string) forgekey.Ran {
env = append(env, "GOCACHE="+gocache)
if workFile != "" {
env = append(env, "GOWORK="+workFile, "GOFLAGS=-mod=readonly") // a workspace takes no -mod=mod
}
return run(ctx, env, name, args...)
}
read, digests := indexerHolds(t, `{"module":"demo","version":"1"}`)
m.ReadManifest = read
fetches := 0
steps := fakeSteps(new([]string))
for i := range steps {
if steps[i].Name == "build_go_fetch" {
steps[i].Run = func(ctx context.Context, args map[string]any) (any, error) {
fetches++
ws := sdkstep.NewID()
tree := filepath.Join(m.Work, ws, "tree")
if err := os.MkdirAll(tree, 0o700); err != nil {
return nil, err
}
copyHostTree(t, hostTree, tree)
if len(beside) > 0 { // the group's other repositories, laid beside the node-engine's tree as one workspace
work, err := workspaceOf(tree, beside)
if err != nil {
return nil, err
}
if err := os.WriteFile(filepath.Join(m.Work, ws, "go.work"), []byte(work), 0o600); err != nil {
return nil, err
}
workFile = filepath.Join(m.Work, ws, "go.work")
// Nothing is downloaded here: this level has no network. The check's own fetch downloaded this workspace's
// graph (downloadBeside), and a module it lacks fails the build naming it.
}
return map[string]any{"workspace": ws}, nil
}
}
}
p := m.Puller(steps, &buildstep.JetStreamAsks{JS: js, Language: sdkstep.AskGo})
p.PullWait = 200 * time.Millisecond
go p.Pull(ctx)
ask := func() sdkstep.Step {
a := composeAskOf(t, digests[0])
a.Compose.Fetch.Repository, a.Compose.Dir, a.Compose.Package = "novox/mesh-host", catalogclient.RootPath, "github.com/novox/mesh-host/cmd/mesh-compose"
raw, err := sdkstep.EncodeStepAsk(a)
if err != nil {
t.Fatal(err)
}
subject, _ := sdkstep.AskSubject(sdkstep.AskGo, sdkstep.AskCheck)
if _, err := js.Publish(ctx, subject, raw); err != nil {
t.Fatal(err)
}
key, _ := sdkstep.AskStepKey("ace", a.ID)
for deadline := time.Now().Add(90 * time.Second); time.Now().Before(deadline); time.Sleep(50 * time.Millisecond) {
states.mu.Lock()
v := states.steps[key]
states.mu.Unlock()
if len(v) > 0 && v[len(v)-1].State != sdkstep.StepRunning {
return v[len(v)-1]
}
}
t.Fatalf("the compose ask %s never ended", a.ID)
return sdkstep.Step{}
}
for n, wantBuilt := range []bool{true, false} {
step := ask()
if step.State != sdkstep.StepPassed {
t.Fatalf("ask %d: %s: %s", n+1, step.State, step.Reason)
}
var ans sdkstep.ComposeAnswer
var v composer.Verdict
if err := json.Unmarshal(step.Result, &ans); err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(ans.Verdict, &v); err != nil {
t.Fatal(err)
}
if ans.Built != wantBuilt || !strings.HasPrefix(ans.Key, "composer-") || v.V != composer.Version || len(v.Cases) != 1 || v.Cases[0].Name != "change" {
t.Fatalf("ask %d: answer built %v key %s, verdict %+v; want built %v and the verdict of the case \"change\"", n+1, ans.Built, ans.Key, v, wantBuilt)
}
}
if fetches != 1 {
t.Errorf("two asks of one commit fetched %d times, want once", fetches)
}
}