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
196 lines
7.8 KiB
Go
196 lines
7.8 KiB
Go
package resolution
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
mcat "git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
)
|
|
|
|
type fake struct {
|
|
on map[string]bool
|
|
asked []string
|
|
err error
|
|
}
|
|
|
|
func (f *fake) Resources(node string) ([]map[string]any, bool, error) {
|
|
f.asked = append(f.asked, node)
|
|
if f.err != nil {
|
|
return nil, false, f.err
|
|
}
|
|
if !f.on[node] {
|
|
return nil, false, nil
|
|
}
|
|
return []map[string]any{{"id": "peers", "type": "file", "path": "/etc/x.conf",
|
|
"merge": mcat.MergeJSON, "content": `{"peer":"` + node + `"}`}}, true, nil
|
|
}
|
|
|
|
func computedShelf() map[string]mcat.Manifest {
|
|
return shelf(mcat.Manifest{Module: "mesh-network", Computed: "mesh-network",
|
|
Provides: mcat.Offers("private-network")})
|
|
}
|
|
|
|
func testAComputedModuleIsAskedAboutTheNodeItIsFor(t *testing.T) {
|
|
// The generator gets a node name, not a plan. Everything it needs is the whole mesh, which
|
|
// it was built with — this is the one thing a node could never work out for itself.
|
|
got, err := Resolve(computedShelf(), []string{"mesh-network"}, workstation(), mcat.World{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
gen := &fake{on: map[string]bool{"workstation": true}}
|
|
out, err := Declaration(got, mcat.Rendering{Generators: map[string]mcat.Generator{"mesh-network": gen}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(gen.asked) != 1 || gen.asked[0] != "workstation" {
|
|
t.Fatalf("asked about %v, wanted workstation once", gen.asked)
|
|
}
|
|
if len(out) != 1 || !strings.Contains(out[0]["content"].(string), `"peer": "workstation"`) {
|
|
t.Fatalf("got %v", out)
|
|
}
|
|
}
|
|
|
|
func testAMachineNobodyGaveItGetsNothing(t *testing.T) {
|
|
// The whole point of making the network a module. Before this, every machine with an address
|
|
// was on the private network and there was no way to say one should stay off.
|
|
got, err := Resolve(computedShelf(), nil, workstation(), mcat.World{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
gen := &fake{on: map[string]bool{"workstation": true}}
|
|
out, err := Declaration(got, mcat.Rendering{Generators: map[string]mcat.Generator{"mesh-network": gen}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(out) != 0 {
|
|
t.Fatalf("a machine that was assigned nothing got %d resource(s)", len(out))
|
|
}
|
|
if len(gen.asked) != 0 {
|
|
t.Fatalf("the generator was asked about %v, and nobody had asked for it", gen.asked)
|
|
}
|
|
}
|
|
|
|
func testAssignedAndNotYetPartOfItIsNotAFailure(t *testing.T) {
|
|
// A machine given the network module before it has a place on it. Brief and ordinary — the
|
|
// answer is "nothing yet", and treating it as an error would make an ordering a fault.
|
|
got, _ := Resolve(computedShelf(), []string{"mesh-network"}, workstation(), mcat.World{})
|
|
gen := &fake{on: map[string]bool{}}
|
|
out, err := Declaration(got, mcat.Rendering{Generators: map[string]mcat.Generator{"mesh-network": gen}})
|
|
if err != nil {
|
|
t.Fatalf("refused a node that is not on the network yet: %v", err)
|
|
}
|
|
if len(out) != 0 {
|
|
t.Fatalf("got %v", out)
|
|
}
|
|
}
|
|
|
|
func testAGeneratorThisControlPlaneDoesNotHaveIsRefused(t *testing.T) {
|
|
// Sending a machine a module with no files would look like it worked. Named in the message,
|
|
// because the only fix is a control plane that has it.
|
|
got, _ := Resolve(shelf(mcat.Manifest{Module: "weather", Computed: "the-weather"}), []string{"weather"}, workstation(), mcat.World{})
|
|
|
|
_, err := Declaration(got, mcat.Rendering{Generators: map[string]mcat.Generator{"mesh-network": &fake{}}})
|
|
if err == nil {
|
|
t.Fatal("a module computed by nothing was accepted")
|
|
}
|
|
if !strings.Contains(err.Error(), "the-weather") {
|
|
t.Fatalf("the refusal does not name what is missing: %v", err)
|
|
}
|
|
}
|
|
|
|
func testSettingsApplyToAComputedModuleToo(t *testing.T) {
|
|
// Being computed is about where the files come from, not about whether they are configurable.
|
|
// A line drawn there would be arbitrary and nobody could predict it.
|
|
got, _ := Resolve(computedShelf(), []string{"mesh-network"}, workstation(), mcat.World{})
|
|
gen := &fake{on: map[string]bool{"workstation": true}}
|
|
out, err := Declaration(got, mcat.Rendering{
|
|
Generators: map[string]mcat.Generator{"mesh-network": gen},
|
|
Settings: mcat.SettingsBy{"mesh-network": {{From: "the mesh",
|
|
Values: map[string]any{"keepalive": 25}}}},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
content := out[0]["content"].(string)
|
|
if !strings.Contains(content, `"keepalive": 25`) {
|
|
t.Fatalf("the setting did not reach a computed file: %s", content)
|
|
}
|
|
if !strings.Contains(content, `"peer": "workstation"`) {
|
|
t.Fatalf("the setting replaced what the generator computed: %s", content)
|
|
}
|
|
}
|
|
|
|
func testTwoWaysToBeOnAPrivateNetworkRefuseAndNameBoth(t *testing.T) {
|
|
// WireGuard is one way. The refusing rule is what makes a second one safe to add: assigning
|
|
// both is caught rather than producing a machine on two networks that each half-work.
|
|
_, err := Resolve(shelf(mcat.Manifest{Module: "mesh-network", Computed: "mesh-network",
|
|
Provides: mcat.Offers("private-network")}, mcat.Manifest{Module: "tailscale", Provides: mcat.Offers("private-network")}, mcat.Manifest{Module: "backups", Requires: []string{"private-network"}},
|
|
), []string{"backups"}, workstation(), mcat.World{})
|
|
|
|
if err == nil {
|
|
t.Fatal("two answers to one requirement were taken silently")
|
|
}
|
|
for _, want := range []string{"mesh-network", "tailscale", "private-network"} {
|
|
if !strings.Contains(err.Error(), want) {
|
|
t.Fatalf("the refusal does not name %s: %v", want, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// reloading answers the runtime's trust as the networking module once did (novox/hq ADR 0102; it no
|
|
// longer does, ADR 0222 — and beside the runtime's module it is refused, generated_collision_test): a file
|
|
// written into, and the runtime reloaded on it.
|
|
type reloading struct{}
|
|
|
|
func (reloading) Resources(node string) ([]map[string]any, bool, error) {
|
|
return []map[string]any{
|
|
{"id": "registry-trust", "type": "file", "path": "/etc/docker/daemon.json",
|
|
"merge": mcat.MergeJSON, "into": "json", "content": `{"insecure-registries":["r:5000"]}`},
|
|
{"id": "registry-trust-reload", "type": "service", "unit": "docker.service",
|
|
"state": "running", "reload-on": []string{"registry-trust"}},
|
|
}, true, nil
|
|
}
|
|
|
|
func testWhatAServiceIsReloadedOnIsNamedAsTheHostWillSeeIt(t *testing.T) {
|
|
// Ids are prefixed with their module on the way out. An unprefixed reload-on would name a
|
|
// resource the host never sees, and the runtime would never be reloaded for its trust.
|
|
got, err := Resolve(computedShelf(), []string{"mesh-network"}, workstation(), mcat.World{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out, err := Declaration(got, mcat.Rendering{Generators: map[string]mcat.Generator{"mesh-network": reloading{}}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var file, service map[string]any
|
|
for _, r := range out {
|
|
switch r["type"] {
|
|
case "file":
|
|
file = r
|
|
case "service":
|
|
service = r
|
|
}
|
|
}
|
|
if file == nil || service == nil {
|
|
t.Fatalf("got %v", out)
|
|
}
|
|
if file["into"] != "json" {
|
|
t.Errorf("into did not reach the host: %v", file)
|
|
}
|
|
if want := "[" + file["id"].(string) + "]"; fmt.Sprint(service["reload-on"]) != want {
|
|
t.Errorf("reload-on names %v, the file is %v", service["reload-on"], file["id"])
|
|
}
|
|
}
|
|
|
|
// TestComputed runs this file's tests, each a subtest named as its function was (design 47 H2 §819, the test budget).
|
|
func TestComputed(t *testing.T) {
|
|
t.Run("AComputedModuleIsAskedAboutTheNodeItIsFor", testAComputedModuleIsAskedAboutTheNodeItIsFor)
|
|
t.Run("AMachineNobodyGaveItGetsNothing", testAMachineNobodyGaveItGetsNothing)
|
|
t.Run("AssignedAndNotYetPartOfItIsNotAFailure", testAssignedAndNotYetPartOfItIsNotAFailure)
|
|
t.Run("AGeneratorThisControlPlaneDoesNotHaveIsRefused", testAGeneratorThisControlPlaneDoesNotHaveIsRefused)
|
|
t.Run("SettingsApplyToAComputedModuleToo", testSettingsApplyToAComputedModuleToo)
|
|
t.Run("TwoWaysToBeOnAPrivateNetworkRefuseAndNameBoth", testTwoWaysToBeOnAPrivateNetworkRefuseAndNameBoth)
|
|
t.Run("WhatAServiceIsReloadedOnIsNamedAsTheHostWillSeeIt", testWhatAServiceIsReloadedOnIsNamedAsTheHostWillSeeIt)
|
|
}
|