A pipeline's test step places no repository beside the tree and a module's tests read nothing outside their module (lead answer A-474), so the test no longer needs MESH_CHECK_BESIDE or the node-engine's tree: it fetches a small program of the composer's contract (testdata/composer) and still proves the build once, the kept composer on the second ask, and the verdict by case. Builds: design 47 G lead answer A-474 (principles 10 and 9)
170 lines
5.8 KiB
Go
170 lines
5.8 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"
|
|
|
|
catalogclient "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
|
|
sdkstep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/composer"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun"
|
|
"git.novox.be/novox/mesh-sdk/go/forgekey"
|
|
"git.novox.be/novox/mesh-sdk/go/testbus"
|
|
)
|
|
|
|
// copyHostTree 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 (a stand-in composer of this module's testdata, 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 := steprun.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
|
|
// The composer is a stand-in of this module's own testdata (design 47 G A-474: a module's tests read nothing outside it, and a
|
|
// pipeline's step places no other repository beside the tree): a program of the composer's contract that imports nothing.
|
|
hostTree := filepath.Join("testdata", "composer")
|
|
m.Run = func(ctx context.Context, env []string, name string, args ...string) forgekey.Ran {
|
|
return run(ctx, append(env, "GOCACHE="+gocache), name, args...)
|
|
}
|
|
read, digests := catalogHolds(t, `{"module":"demo","version":"1"}`)
|
|
m.ReadEntry = 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)
|
|
return map[string]any{"workspace": ws}, nil
|
|
}
|
|
}
|
|
}
|
|
p := m.Puller(steps, &steprun.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-catalog", catalogclient.RootPath, "standin/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)
|
|
}
|
|
}
|