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
89 lines
3.7 KiB
Go
89 lines
3.7 KiB
Go
package resolution
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
mcat "git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
)
|
|
|
|
// **A process's environment is composed as a container's is** (novox/hq to-be 38 WP4c).
|
|
//
|
|
// A module's code moving out of its container becomes a process on the machine, and what its
|
|
// container's environment asked for — the port this machine gave the module, the place it put the
|
|
// module's directory — it still has to be told. Filled for a container and not for a process, the
|
|
// literal `${port:8080}` reached the process as its environment and was read as a port; the modules
|
|
// that moved first wrote their run-once steps an env file instead.
|
|
func processModule(env map[string]any) mcat.Manifest {
|
|
return mcat.Manifest{
|
|
Module: "showcase",
|
|
Listens: []mcat.Listening{{Port: 8080, From: mcat.FromMesh}},
|
|
Resources: []map[string]any{
|
|
{"id": "data", "type": "directory", "mode": "0700"},
|
|
{"id": "setup", "type": "process", "name": "showcase-setup", "run-once": true,
|
|
"run": []any{"/usr/bin/showcase", "setup"}, "env": env},
|
|
},
|
|
}
|
|
}
|
|
|
|
func testAProcessIsToldItsPortAndItsPlaceInItsEnvironment(t *testing.T) {
|
|
env := map[string]any{
|
|
"SHOWCASE_URL": "http://127.0.0.1:${port:8080}",
|
|
"SHOWCASE_DATA": "${dir:data}/objects",
|
|
"SHOWCASE_DB": "127.0.0.1:${seat:mesh-store:5432}",
|
|
"GREETING": "hello",
|
|
}
|
|
out, err := Declaration(mcat.Resolution{Node: "anchor", Modules: []mcat.Manifest{processModule(env)}}, mcat.Rendering{
|
|
Ports: map[string]map[int]int{"showcase": {8080: 21000}},
|
|
Seats: map[string]map[int]int{"mesh-store": {5432: 6852}},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("a process asking for its port and its place does not compose: %v", err)
|
|
}
|
|
setup := fileNamed(out, "showcase.setup")
|
|
if setup == nil {
|
|
t.Fatalf("the process is not in the declaration: %v", out)
|
|
}
|
|
got, _ := setup["env"].(map[string]any)
|
|
for key, want := range map[string]string{
|
|
"SHOWCASE_URL": "http://127.0.0.1:21000",
|
|
"SHOWCASE_DATA": "/var/lib/showcase/data/objects",
|
|
"SHOWCASE_DB": "127.0.0.1:6852",
|
|
"GREETING": "hello",
|
|
} {
|
|
if got[key] != want {
|
|
t.Errorf("the process is told %s=%v, want %q", key, got[key], want)
|
|
}
|
|
}
|
|
if env["SHOWCASE_URL"] != "http://127.0.0.1:${port:8080}" {
|
|
t.Fatalf("composing for one machine edited the module's own manifest: %v", env)
|
|
}
|
|
}
|
|
|
|
// An unknown reference in a process's environment is refused as a container's is, naming the
|
|
// process and the variable — left alone, it would reach the machine as a literal.
|
|
func testAProcessAskingAboutAnUndeclaredPortIsRefused(t *testing.T) {
|
|
env := map[string]any{"SHOWCASE_URL": "http://127.0.0.1:${port:9999}"}
|
|
_, err := Declaration(mcat.Resolution{Node: "anchor", Modules: []mcat.Manifest{processModule(env)}}, mcat.Rendering{})
|
|
if err == nil {
|
|
t.Fatal("a process was told a port its module never said it listens on")
|
|
}
|
|
for _, said := range []string{"showcase-setup", "SHOWCASE_URL", "${port:9999}", "8080"} {
|
|
if !strings.Contains(err.Error(), said) {
|
|
t.Errorf("the refusal does not say %q: %v", said, err)
|
|
}
|
|
}
|
|
|
|
env = map[string]any{"SHOWCASE_DATA": "${dir:date}/objects"}
|
|
if _, err := Declaration((mcat.Resolution{Node: "anchor", Modules: []mcat.Manifest{processModule(env)}}), mcat.Rendering{}); err == nil ||
|
|
!strings.Contains(err.Error(), "${dir:date}") {
|
|
t.Fatalf("a process naming no directory of its module was not refused: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestProcessEnv runs this file's tests, each a subtest named as its function was (design 47 H2 §819, the test budget).
|
|
func TestProcessEnv(t *testing.T) {
|
|
t.Run("AProcessIsToldItsPortAndItsPlaceInItsEnvironment", testAProcessIsToldItsPortAndItsPlaceInItsEnvironment)
|
|
t.Run("AProcessAskingAboutAnUndeclaredPortIsRefused", testAProcessAskingAboutAnUndeclaredPortIsRefused)
|
|
}
|