Files
mesh-host/internal/compose/resolution/generated_collision_test.go
T
jschoubben 0c01c81c3f E8: run each file's unit tests as subtests of one test
The node-engine held 1,707 top-level unit tests against the budget of 500
a module. Each unit test file with two or more tests now runs them as
subtests of one test named for the file, each named as its function was,
in the same order: every check is kept, and the check counts 383. Files
with a TestMain, a re-run of a test by its name, or the integration tag
are left. Four files of this branch's earlier commits are formatted.

Builds: design 47 H2 §819; H-A-349 DQ-14
2026-11-22 00:30:05 +01:00

91 lines
3.8 KiB
Go

package resolution
import (
"strings"
"testing"
mcat "git.novox.be/novox/mesh-sdk/go/catalogue"
)
func runtimesOwn() mcat.Manifest {
return mcat.Manifest{Module: "docker", Resources: []map[string]any{
{"id": "daemon", "type": "file", "path": "/etc/docker/daemon.json", "into": "json",
"content": `{"live-restore": true}`},
{"id": "runtime", "type": "service", "unit": "docker.service", "state": "running",
"reload-on": []any{"daemon"}},
}}
}
func testAGeneratedResourceCollidingWithAModulesIsRefused(t *testing.T) {
r := mcat.Resolution{Node: "workstation", Modules: []mcat.Manifest{
{Module: "mesh-network", Computed: "mesh-network", Provides: mcat.Offers("private-network")},
runtimesOwn(),
}}
_, err := Declaration(r, mcat.Rendering{Generators: map[string]mcat.Generator{"mesh-network": reloading{}}})
if err == nil {
t.Fatal("a generated resource declaring the runtime's file beside the runtime's module was accepted")
}
for _, want := range []string{"mesh-network", "docker", "/etc/docker/daemon.json", "docker.service"} {
if !strings.Contains(err.Error(), want) {
t.Errorf("the refusal does not name %s: %v", want, err)
}
}
}
func testAGeneratedResourceBesideAModulesOwnIsComposed(t *testing.T) {
r := mcat.Resolution{Node: "workstation", Modules: []mcat.Manifest{
{Module: "mesh-network", Computed: "mesh-network", Provides: mcat.Offers("private-network")},
runtimesOwn(),
}}
gen := &fake{on: map[string]bool{"workstation": true}}
out, err := Declaration(r, mcat.Rendering{Generators: map[string]mcat.Generator{"mesh-network": gen}})
if err != nil {
t.Fatalf("disjoint resources were refused: %v", err)
}
if fileNamed(out, "docker.daemon") == nil {
t.Fatalf("the runtime's own file is missing: %v", out)
}
// And a machine not on the network yet generates nothing, which collides with nothing.
if _, err := Declaration(r, mcat.Rendering{Generators: map[string]mcat.Generator{"mesh-network": &fake{}}}); err != nil {
t.Fatalf("a machine off the network was refused: %v", err)
}
}
// The catalogue's container runtime module states the mesh's registry itself (ADR 0222).
func testTheRuntimesModuleTrustsTheMeshsRegistry(t *testing.T) {
docker := catalogueManifest(t, "docker")
r := mcat.Resolution{Node: "workstation", Modules: []mcat.Manifest{docker}}
out, err := Declaration(r, mcat.Rendering{
// Every name of the store answers the one address, as the composer fills them (ADR 0632 §1).
SeatReach: map[string]string{"mesh-artifact-store": "anchor.internal:5100",
"container-image-registry": "anchor.internal:5100", "oci-artifact-registry": "anchor.internal:5100"},
})
if err != nil {
t.Fatal(err)
}
daemon := fileNamed(out, "docker.daemon")
if daemon == nil || daemon["into"] != "json" {
t.Fatalf("the runtime's file is not written into: %v", daemon)
}
content, _ := daemon["content"].(string)
if !strings.Contains(content, `"insecure-registries": ["anchor.internal:5100"]`) ||
!strings.Contains(content, `"live-restore": true`) {
t.Fatalf("the runtime's file says %q", content)
}
out, err = Declaration(r, mcat.Rendering{})
if err != nil {
t.Fatal(err)
}
if content, _ := fileNamed(out, "docker.daemon")["content"].(string); strings.Contains(content, "insecure-registries") {
t.Fatalf("with no store on the network, the runtime is told %q", content)
}
}
// TestGeneratedCollision runs this file's tests, each a subtest named as its function was (design 47 H2 §819, the test budget).
func TestGeneratedCollision(t *testing.T) {
t.Run("AGeneratedResourceCollidingWithAModulesIsRefused", testAGeneratedResourceCollidingWithAModulesIsRefused)
t.Run("AGeneratedResourceBesideAModulesOwnIsComposed", testAGeneratedResourceBesideAModulesOwnIsComposed)
t.Run("TheRuntimesModuleTrustsTheMeshsRegistry", testTheRuntimesModuleTrustsTheMeshsRegistry)
}