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
286 lines
11 KiB
Go
286 lines
11 KiB
Go
package resolution
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
mcat "git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
)
|
|
|
|
func placedModule() mcat.Manifest {
|
|
return mcat.Manifest{
|
|
Module: "photos",
|
|
Resources: []map[string]any{
|
|
{"id": "data", "type": "directory", "mode": "0700"},
|
|
{"id": "server-env", "type": "file", "path": "${dir:data}/server.env",
|
|
"content": "STORE=${dir:data}/objects\n"},
|
|
{"id": "server", "type": "container", "name": "photos-server",
|
|
"volumes": []any{"${dir:data}:/data"},
|
|
"env": map[string]any{"DATA": "${dir:data}/objects"},
|
|
"env-file": []any{"${dir:data}/server.env"}},
|
|
},
|
|
}
|
|
}
|
|
|
|
func testAPathlessDirectoryResolvesUnderTheNodesRoot(t *testing.T) {
|
|
m := placedModule()
|
|
dirs := dirsFor(m, mcat.Rendering{})
|
|
if dirs["data"] != "/var/lib/photos/data" {
|
|
t.Fatalf("the default root is /var/lib and the shape is <root>/<module>/<id>; got %q", dirs["data"])
|
|
}
|
|
dirs = dirsFor(m, mcat.Rendering{DataRoot: "/tank/nox/"})
|
|
if dirs["data"] != "/tank/nox/photos/data" {
|
|
t.Fatalf("a node's own root is honoured, trailing slash and all; got %q", dirs["data"])
|
|
}
|
|
}
|
|
|
|
func testDirReferencesBecomeThePlaceInEveryField(t *testing.T) {
|
|
m := placedModule()
|
|
dirs := dirsFor(m, mcat.Rendering{})
|
|
|
|
directory := shallowCopy(m.Resources[0])
|
|
if err := dirInto(directory, dirs, m.Module); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if directory["path"] != "/var/lib/photos/data" {
|
|
t.Fatalf("a pathless directory receives its resolved path; got %v", directory["path"])
|
|
}
|
|
|
|
file := shallowCopy(m.Resources[1])
|
|
if err := dirInto(file, dirs, m.Module); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if file["path"] != "/var/lib/photos/data/server.env" {
|
|
t.Fatalf("a file's path names the place; got %v", file["path"])
|
|
}
|
|
if file["content"] != "STORE=/var/lib/photos/data/objects\n" {
|
|
t.Fatalf("a file's content names the place; got %v", file["content"])
|
|
}
|
|
|
|
container := shallowCopy(m.Resources[2])
|
|
if err := dirInto(container, dirs, m.Module); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if container["volumes"].([]any)[0] != "/var/lib/photos/data:/data" {
|
|
t.Fatalf("a mount names the place; got %v", container["volumes"])
|
|
}
|
|
if container["env"].(map[string]any)["DATA"] != "/var/lib/photos/data/objects" {
|
|
t.Fatalf("an environment value names the place; got %v", container["env"])
|
|
}
|
|
if container["env-file"].([]any)[0] != "/var/lib/photos/data/server.env" {
|
|
t.Fatalf("an env-file names the place; got %v", container["env-file"])
|
|
}
|
|
}
|
|
|
|
func testAStatedPathIsThePlacementAndStillAnswersByName(t *testing.T) {
|
|
m := placedModule()
|
|
// The adopted-machine case: data that must sit where the predecessor already put it.
|
|
m.Resources[0]["path"] = "/services/mssql/data/"
|
|
dirs := dirsFor(m, mcat.Rendering{})
|
|
if dirs["data"] != "/services/mssql/data" {
|
|
t.Fatalf("a stated path wins over the root, trimmed; got %q", dirs["data"])
|
|
}
|
|
container := shallowCopy(m.Resources[2])
|
|
if err := dirInto(container, dirs, m.Module); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if container["volumes"].([]any)[0] != "/services/mssql/data:/data" {
|
|
t.Fatalf("references follow the placement; got %v", container["volumes"])
|
|
}
|
|
}
|
|
|
|
func testFillingForOneNodeLeaksIntoNoOther(t *testing.T) {
|
|
m := placedModule()
|
|
first := shallowCopy(m.Resources[2])
|
|
if err := dirInto(first, dirsFor(m, mcat.Rendering{DataRoot: "/first"}), m.Module); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
second := shallowCopy(m.Resources[2])
|
|
if err := dirInto(second, dirsFor(m, mcat.Rendering{DataRoot: "/second"}), m.Module); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := second["volumes"].([]any)[0]; got != "/second/photos/data:/data" {
|
|
t.Fatalf("the second composition must see the manifest, not the first fill; got %v", got)
|
|
}
|
|
if m.Resources[2]["volumes"].([]any)[0] != "${dir:data}:/data" {
|
|
t.Fatalf("the manifest itself stays a template; got %v", m.Resources[2]["volumes"])
|
|
}
|
|
}
|
|
|
|
func testAReferenceToNoDirectoryRefusesAtCompositionToo(t *testing.T) {
|
|
m := placedModule()
|
|
container := shallowCopy(m.Resources[2])
|
|
container["env"] = map[string]any{"DATA": "${dir:date}"}
|
|
err := dirInto(container, dirsFor(m, mcat.Rendering{}), m.Module)
|
|
if err == nil || !strings.Contains(err.Error(), `"date"`) || !strings.Contains(err.Error(), `"data"`) {
|
|
t.Fatalf("the refusal names the mistake and what exists; got %v", err)
|
|
}
|
|
}
|
|
|
|
func testTheAssignmentsOwnRootIsAPlace(t *testing.T) {
|
|
m := mcat.Manifest{Module: "mailu", Resources: []map[string]any{
|
|
{"id": "state", "type": "directory", "place": ".", "mode": "0700"},
|
|
{"id": "data-mail", "type": "directory"},
|
|
}}
|
|
dirs := dirsFor(m, mcat.Rendering{})
|
|
if dirs["state"] != "/var/lib/mailu" {
|
|
t.Fatalf("place %q is the assignment's root; got %q", ".", dirs["state"])
|
|
}
|
|
if dirs["data-mail"] != "/var/lib/mailu/data-mail" {
|
|
t.Fatalf("everything else sits beneath it; got %q", dirs["data-mail"])
|
|
}
|
|
root := shallowCopy(m.Resources[0])
|
|
if err := dirInto(root, dirs, m.Module); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if root["path"] != "/var/lib/mailu" {
|
|
t.Fatalf("the root receives its path; got %v", root["path"])
|
|
}
|
|
if _, still := root["place"]; still {
|
|
t.Fatal("place must never reach the host, which parses strictly")
|
|
}
|
|
}
|
|
|
|
func testTheManifestsMapsArePlaced(t *testing.T) {
|
|
m := mcat.Manifest{
|
|
Module: "photos",
|
|
Resources: []map[string]any{
|
|
{"id": "state", "type": "directory", "place": "."},
|
|
},
|
|
Binds: map[string]string{"route": "${dir:state}/route.json"},
|
|
Secrets: map[string]string{"mongodb-database": "${dir:state}/database.secret"},
|
|
OwnSecrets: mcat.OwnSecrets{"admin-key": {Path: "${dir:state}/admin-key.secret"}},
|
|
Receives: map[string]string{"route": "${dir:state}/grants/mesh.json"},
|
|
}
|
|
placed, err := placedManifest(m, mcat.Rendering{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if placed.Binds["route"] != "/var/lib/photos/route.json" {
|
|
t.Fatalf("binds are placed; got %v", placed.Binds)
|
|
}
|
|
if placed.Secrets["mongodb-database"] != "/var/lib/photos/database.secret" {
|
|
t.Fatalf("secrets are placed; got %v", placed.Secrets)
|
|
}
|
|
if placed.OwnSecrets["admin-key"].Path != "/var/lib/photos/admin-key.secret" {
|
|
t.Fatalf("own-secrets are placed; got %v", placed.OwnSecrets)
|
|
}
|
|
if placed.Receives["route"] != "/var/lib/photos/grants/mesh.json" {
|
|
t.Fatalf("receives are placed; got %v", placed.Receives)
|
|
}
|
|
if m.Binds["route"] != "${dir:state}/route.json" {
|
|
t.Fatalf("the manifest itself stays a template; got %v", m.Binds)
|
|
}
|
|
}
|
|
|
|
func testTheMeshsDirectoryForAModuleIsAPlace(t *testing.T) {
|
|
// novox/hq issue 174. What the mesh writes for a module — its bus credential, its bindings —
|
|
// is the mesh's plumbing under <root>/mesh/<module>, and the definition names it by id.
|
|
m := mcat.Manifest{Module: "umami",
|
|
Resources: []map[string]any{
|
|
{"id": "mesh-state", "type": "directory", "place": "mesh"},
|
|
{"id": "state", "type": "directory", "place": "."},
|
|
{"id": "server", "type": "container", "image": "x@sha256:aa",
|
|
"volumes": []any{"${dir:mesh-state}/broker:/run/secrets/broker:ro"}},
|
|
},
|
|
OwnSecrets: mcat.OwnSecrets{"broker": {Path: "${dir:mesh-state}/broker"}},
|
|
Binds: map[string]string{"route": "${dir:state}/route.json"},
|
|
}
|
|
dirs := dirsFor(m, mcat.Rendering{})
|
|
if dirs["mesh-state"] != "/var/lib/mesh/umami" || dirs["state"] != "/var/lib/umami" {
|
|
t.Fatalf("the mesh's directory sits beside the module's, not in it; got %v", dirs)
|
|
}
|
|
dirs = dirsFor(m, mcat.Rendering{DataRoot: "/srv"})
|
|
if dirs["mesh-state"] != "/srv/mesh/umami" {
|
|
t.Fatalf("a node's root moves the mesh's files with the module's; got %v", dirs)
|
|
}
|
|
placed, err := placedManifest(m, mcat.Rendering{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if placed.OwnSecrets["broker"].Path != "/var/lib/mesh/umami/broker" {
|
|
t.Fatalf("own-secrets are placed under the mesh's directory; got %v", placed.OwnSecrets)
|
|
}
|
|
container := shallowCopy(m.Resources[2])
|
|
if err := dirInto(container, dirsFor(m, mcat.Rendering{}), m.Module); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if container["volumes"].([]any)[0] != "/var/lib/mesh/umami/broker:/run/secrets/broker:ro" {
|
|
t.Fatalf("the mount's host side is placed; got %v", container["volumes"])
|
|
}
|
|
}
|
|
|
|
func testTwoModulesPlacedRootsAreNoCollision(t *testing.T) {
|
|
a := mcat.Manifest{Module: "gitea", Resources: []map[string]any{
|
|
{"id": "state", "type": "directory", "place": "."},
|
|
{"id": "env", "type": "file", "path": "${dir:state}/server.env", "content": ""},
|
|
}}
|
|
b := mcat.Manifest{Module: "nextcloud", Resources: []map[string]any{
|
|
{"id": "state", "type": "directory", "place": "."},
|
|
{"id": "env", "type": "file", "path": "${dir:state}/server.env", "content": ""},
|
|
}}
|
|
if got := checkResources([]mcat.Manifest{a, b}); len(got) != 0 {
|
|
t.Fatalf("alike templates are different places; got %v", got)
|
|
}
|
|
// And the real collision is still caught: a module stating another's placed root.
|
|
c := mcat.Manifest{Module: "squatter", Resources: []map[string]any{
|
|
{"id": "nest", "type": "directory", "path": "/var/lib/gitea"},
|
|
}}
|
|
got := checkResources([]mcat.Manifest{a, c})
|
|
if len(got) != 1 || !strings.Contains(got[0], `"/var/lib/gitea"`) {
|
|
t.Fatalf("a stated path on a placed root collides; got %v", got)
|
|
}
|
|
}
|
|
|
|
func shallowCopy(resource map[string]any) map[string]any {
|
|
copied := map[string]any{}
|
|
for k, v := range resource {
|
|
copied[k] = v
|
|
}
|
|
return copied
|
|
}
|
|
|
|
func testADirectoryBeneathAPlacedOneIsPlacedWithIt(t *testing.T) {
|
|
// An adopted layout keeps a subdirectory the predecessor made under the mesh's directory
|
|
// (issue 174: a forge's runtime state, a manager's output). Stated as beneath the placed one,
|
|
// it moves with it — a node's root moves both, and the definition names no host path.
|
|
m := mcat.Manifest{Module: "gitea", Resources: []map[string]any{
|
|
{"id": "mesh-state", "type": "directory", "place": "mesh"},
|
|
{"id": "runtime-state", "type": "directory", "path": "${dir:mesh-state}/state"},
|
|
{"id": "server", "type": "container", "image": "x@sha256:aa",
|
|
"volumes": []any{"${dir:runtime-state}:/data"}},
|
|
}}
|
|
dirs := dirsFor(m, mcat.Rendering{DataRoot: "/srv"})
|
|
if dirs["runtime-state"] != "/srv/mesh/gitea/state" {
|
|
t.Fatalf("the subdirectory follows the placed one; got %v", dirs)
|
|
}
|
|
sub := shallowCopy(m.Resources[1])
|
|
if err := dirInto(sub, dirs, m.Module); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if sub["path"] != "/srv/mesh/gitea/state" {
|
|
t.Fatalf("the directory resource itself is resolved; got %v", sub["path"])
|
|
}
|
|
container := shallowCopy(m.Resources[2])
|
|
if err := dirInto(container, dirs, m.Module); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if container["volumes"].([]any)[0] != "/srv/mesh/gitea/state:/data" {
|
|
t.Fatalf("a reference to the subdirectory resolves whole; got %v", container["volumes"])
|
|
}
|
|
}
|
|
|
|
// TestDirInto runs this file's tests, each a subtest named as its function was (design 47 H2 §819, the test budget).
|
|
func TestDirInto(t *testing.T) {
|
|
t.Run("APathlessDirectoryResolvesUnderTheNodesRoot", testAPathlessDirectoryResolvesUnderTheNodesRoot)
|
|
t.Run("DirReferencesBecomeThePlaceInEveryField", testDirReferencesBecomeThePlaceInEveryField)
|
|
t.Run("AStatedPathIsThePlacementAndStillAnswersByName", testAStatedPathIsThePlacementAndStillAnswersByName)
|
|
t.Run("FillingForOneNodeLeaksIntoNoOther", testFillingForOneNodeLeaksIntoNoOther)
|
|
t.Run("AReferenceToNoDirectoryRefusesAtCompositionToo", testAReferenceToNoDirectoryRefusesAtCompositionToo)
|
|
t.Run("TheAssignmentsOwnRootIsAPlace", testTheAssignmentsOwnRootIsAPlace)
|
|
t.Run("TheManifestsMapsArePlaced", testTheManifestsMapsArePlaced)
|
|
t.Run("TheMeshsDirectoryForAModuleIsAPlace", testTheMeshsDirectoryForAModuleIsAPlace)
|
|
t.Run("TwoModulesPlacedRootsAreNoCollision", testTwoModulesPlacedRootsAreNoCollision)
|
|
t.Run("ADirectoryBeneathAPlacedOneIsPlacedWithIt", testADirectoryBeneathAPlacedOneIsPlacedWithIt)
|
|
}
|