node-engine: a module's composition is applied for its node, so its processes get MESH_NODE (design 47 G A-342)

declarationOf never copied the composition's Node into the declaration's For, so no process
placed by a composition got MESH_NODE. Compositions of two nodes, or a composition naming no node,
are refused.

Builds: design 47 G A-342
This commit is contained in:
2026-11-25 14:47:20 +01:00
parent d27af8347a
commit 32c57111d6
2 changed files with 74 additions and 1 deletions
+60 -1
View File
@@ -1,10 +1,22 @@
package main
import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"encoding/json"
engcomp "github.com/novox/mesh-host/internal/compositions"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"git.novox.be/novox/mesh-sdk/go/declaration"
"github.com/novox/mesh-host/internal/apply"
engcomp "github.com/novox/mesh-host/internal/compositions"
"github.com/novox/mesh-host/internal/link"
)
// A composition that names a file the provisioner fills without saying it is filled would have this node-engine write
@@ -29,4 +41,51 @@ func TestAFileItCannotProduceIsNeverWrittenEmpty(t *testing.T) {
if d, _ = declarationOf(plain); refuseEmptyWrites(d) != nil {
t.Fatalf("a file with its content was refused: %v", refuseEmptyWrites(d))
}
// Design 47 G A-342, through the module's apply (applyComposition and declarationOf, not Apply by hand): a process a
// composition places runs with its node's name as MESH_NODE; a composition naming no node, or compositions of two, are
// refused and nothing is applied.
t.Run("AProcessAComposedModulePlacesRunsWithItsNodesName", func(t *testing.T) {
var raw bytes.Buffer
zipped := gzip.NewWriter(&raw)
w := tar.NewWriter(zipped)
_ = w.WriteHeader(&tar.Header{Name: "main.js", Mode: 0o644, Size: 1, Typeflag: tar.TypeReg})
_, _ = w.Write([]byte("1"))
_ = w.Close()
_ = zipped.Close()
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) { _, _ = rw.Write(raw.Bytes()) }))
t.Cleanup(server.Close)
proc := json.RawMessage(`{"id":"signin.runtime","type":"process","name":"signin","source":"` + server.URL + `/a.tar.gz","digest":"` +
declaration.DigestOf(raw.Bytes()) + `","run":["node","main.js"],"env":{"A":"1"}}`)
units := t.TempDir()
t.Cleanup(apply.ForTests(units, t.TempDir()))
opts := options{state: filepath.Join(t.TempDir(), "state.json"), out: &strings.Builder{},
runner: func(context.Context, string, ...string) (string, error) { return "", nil }}
bin := t.TempDir()
if err := os.WriteFile(filepath.Join(bin, "pacman"), []byte("#!/bin/sh\nexit 0\n"), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
wasBuilt := builtFor
builtFor = "arch"
t.Cleanup(func() { builtFor = wasBuilt })
say := link.Announce(func(string) {})
apply1 := func(c engcomp.Composed) string {
return applyComposition(context.Background(), opts, c, nil, nil, nil, nil, say).Detail
}
if why := apply1(engcomp.Composed{Module: "signin", Resources: []json.RawMessage{proc}}); !strings.Contains(why, "names no node") {
t.Errorf("a composition with no node was not refused: %q", why)
}
if _, err := declarationOf(engcomp.Composed{Node: "ace"}, engcomp.Composed{Node: "g14"}); err == nil {
t.Error("compositions of two nodes were not refused")
}
res := applyComposition(context.Background(), opts, engcomp.Composed{Node: "ace", Module: "signin", Resources: []json.RawMessage{proc}},
nil, nil, nil, nil, say)
if res.Detail != "" {
t.Fatalf("the apply failed: %s", res.Detail)
}
if b, err := os.ReadFile(filepath.Join(units, "signin.service")); err != nil || !strings.Contains(string(b), `Environment="MESH_NODE=ace"`) {
t.Errorf("the unit does not carry the node's name (%v):\n%s", err, b)
}
})
}
+14
View File
@@ -301,6 +301,16 @@ func convergeDirectories(statePath string, runs compositions.Composed) []string
// ("2 resources declare X") on every node. The node's own copy is left out, and which were left out is said once.
func declarationOf(cs ...compositions.Composed) (*declaration.Declaration, error) {
d := &declaration.Declaration{Version: declaration.Version}
// The declaration is for the node its compositions are of (design 47 G A-342: every process runs with it as MESH_NODE).
// Compositions of one node carry one name; two names, or a name beside an empty one, refuse the declaration whole.
for _, c := range cs {
if c.Node != cs[0].Node {
return nil, fmt.Errorf("the compositions are of different nodes (%q and %q)", cs[0].Node, c.Node)
}
}
if len(cs) > 0 {
d.For = cs[0].Node
}
var modules []compositions.Composed
for _, c := range cs {
if c.Module != "" {
@@ -920,6 +930,10 @@ func applyComposition(ctx context.Context, opts options, c compositions.Composed
if err != nil {
return failedWith(err.Error())
}
if d.For == "" {
// A process placed without its node's name would crash-loop on it (design 47 G A-342): refused, never applied.
return failedWith(fmt.Sprintf("the composition of %s names no node", name))
}
if err := refuseEmptyWrites(d); err != nil {
return failedWith(err.Error())
}