The mesh decides what a node runs
The gap that has been named at the end of every report for a week. Until now a
declaration came from a person handing over a file; now it comes from what was
assigned, resolved against the catalogue, and the control plane is deciding
rather than relaying.
Everything from the module conversation, built and run on real machines:
assign laptop i3 -> accepted, brings xorg, because nothing else provides
it and there was no choice to make
assign laptop sway -> refused: xorg and wayland both claim the-seat
assign laptop editor -> refused: three modules provide a shell -- bash,
fish, zsh -- choose one
assign laptop zsh -> accepted, and the editor's requirement is answered
bash, fish beside it -> fine, nothing is claimed
Claims rather than pairwise exclusion, so a third display server would say what
it claims and need no edit to xorg or wayland. Scoped to node, site or mesh:
two DHCP servers at one site collide and at two sites do not, and the mesh-wide
one is the hub said as a claim instead of hard-coded.
Some conflicts cost no manifest field at all. The refusal above names the seat
AND the two files, because the mesh already holds every resource of every
module -- neither i3 nor sway knows the other exists.
Resource identities carry their module, so two modules may both call something
"config" without the second silently replacing the first. What a service
reflects is qualified the same way, or it would name a resource that no longer
exists and stop being restarted when its own configuration changes.
Nothing is sent until every node resolves. A push that configured three and
refused on the fourth would leave the mesh in a state nobody asked for, and the
fourth is exactly where a claim collision appears.
One real flaw found by using it rather than by testing it: assigning zsh did
not satisfy a requirement for a shell. Requirements were counted against the
catalogue without first asking what the set already offers, so "choose one and
assign it" named three modules and then ignored the one you chose. The remedy
was useless and every test passed.
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
// Package catalogue is what modules are, and what a node gets when it is assigned some.
|
||||
//
|
||||
// novox/hq ADR 0009: everything is a module, a module declares what it provides and requires,
|
||||
// and a module declares what it claims. This turns a set of assignments into the one declaration
|
||||
// a node is sent — which is the first thing the control plane decides rather than relays.
|
||||
package catalogue
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Scopes a claim can have.
|
||||
//
|
||||
// Not everything singular is singular per machine: a seat is one per node, a DHCP server is one
|
||||
// per segment, and the hub is one per mesh. Scope says which, and it is the same idea the mesh
|
||||
// already enforces by hand for the hub.
|
||||
const (
|
||||
ScopeNode = "node"
|
||||
ScopeSite = "site"
|
||||
ScopeMesh = "mesh"
|
||||
)
|
||||
|
||||
// name is what a module, a provision or a claim may be called.
|
||||
//
|
||||
// Constrained because these become resource identities, permission patterns and error messages,
|
||||
// and a name that is valid in one and not the others is a fault found late.
|
||||
var name = regexp.MustCompile(`^[a-z0-9][a-z0-9-]*(\.[a-z0-9][a-z0-9-]*)*$`)
|
||||
|
||||
// Claim is a singular resource a module takes over.
|
||||
type Claim struct {
|
||||
Name string `json:"name"`
|
||||
// Scope defaults to the node, which is where nearly everything singular is singular.
|
||||
Scope string `json:"scope,omitempty"`
|
||||
}
|
||||
|
||||
// At is this claim's scope, with the default applied.
|
||||
func (c Claim) At() string {
|
||||
if c.Scope == "" {
|
||||
return ScopeNode
|
||||
}
|
||||
return c.Scope
|
||||
}
|
||||
|
||||
// Manifest is everything a module says about itself.
|
||||
type Manifest struct {
|
||||
Module string `json:"module"`
|
||||
Version string `json:"version,omitempty"`
|
||||
|
||||
// Provides are the names other modules may require. A module always provides its own name;
|
||||
// this is for the rest — `zsh` provides `shell`, `xorg` provides `display-server`.
|
||||
Provides []string `json:"provides,omitempty"`
|
||||
|
||||
// Requires are names that must be provided by something assigned to the same node.
|
||||
Requires []string `json:"requires,omitempty"`
|
||||
|
||||
// Claims are singular resources. Two modules claiming one thing within a scope cannot both
|
||||
// be assigned there — which is how exclusivity is expressed, rather than as a list of rivals
|
||||
// that every new module would force its predecessors to update.
|
||||
Claims []Claim `json:"claims,omitempty"`
|
||||
|
||||
// Capabilities the machine must have. A different field from Requires because the remedy
|
||||
// differs: a missing module can be assigned, and a missing capability means the wrong
|
||||
// machine.
|
||||
Capabilities []string `json:"capabilities,omitempty"`
|
||||
|
||||
// Resources are what this module puts on a node, in the host's own vocabulary.
|
||||
Resources []map[string]any `json:"resources,omitempty"`
|
||||
}
|
||||
|
||||
// ParseManifest reads a module manifest, refusing anything it cannot act on.
|
||||
//
|
||||
// Every problem is reported rather than the first, because somebody writing a manifest fixes
|
||||
// them in one pass or in four.
|
||||
func ParseManifest(raw []byte) (Manifest, error) {
|
||||
var m Manifest
|
||||
if err := json.Unmarshal(raw, &m); err != nil {
|
||||
return Manifest{}, fmt.Errorf("this is not a module manifest: %w", err)
|
||||
}
|
||||
|
||||
var problems []string
|
||||
if !name.MatchString(m.Module) {
|
||||
problems = append(problems, fmt.Sprintf(
|
||||
"%q is not a usable module name: lower-case letters, digits, dashes and dots", m.Module))
|
||||
}
|
||||
for _, p := range m.Provides {
|
||||
if !name.MatchString(p) {
|
||||
problems = append(problems, fmt.Sprintf("%q is not a usable name to provide", p))
|
||||
}
|
||||
if p == m.Module {
|
||||
// Harmless and worth saying: a module always provides its own name, so writing it
|
||||
// suggests the author expected it not to.
|
||||
problems = append(problems, fmt.Sprintf(
|
||||
"%s provides its own name already; listing it says nothing", m.Module))
|
||||
}
|
||||
}
|
||||
for _, r := range m.Requires {
|
||||
if !name.MatchString(r) {
|
||||
problems = append(problems, fmt.Sprintf("%q is not a usable name to require", r))
|
||||
}
|
||||
if r == m.Module {
|
||||
problems = append(problems, fmt.Sprintf("%s requires itself", m.Module))
|
||||
}
|
||||
}
|
||||
for _, c := range m.Claims {
|
||||
if !name.MatchString(c.Name) {
|
||||
problems = append(problems, fmt.Sprintf("%q is not a usable claim name", c.Name))
|
||||
}
|
||||
switch c.At() {
|
||||
case ScopeNode, ScopeSite, ScopeMesh:
|
||||
default:
|
||||
problems = append(problems, fmt.Sprintf(
|
||||
"%s claims %s at scope %q; a claim is held per node, per site or per mesh",
|
||||
m.Module, c.Name, c.Scope))
|
||||
}
|
||||
}
|
||||
for i, r := range m.Resources {
|
||||
id, _ := r["id"].(string)
|
||||
if id == "" {
|
||||
problems = append(problems, fmt.Sprintf("resource %d has no id", i))
|
||||
}
|
||||
if _, ok := r["type"].(string); !ok {
|
||||
problems = append(problems, fmt.Sprintf("resource %q has no type", id))
|
||||
}
|
||||
}
|
||||
|
||||
if len(problems) > 0 {
|
||||
sort.Strings(problems)
|
||||
return Manifest{}, fmt.Errorf("this manifest cannot be used:\n - %s",
|
||||
strings.Join(problems, "\n - "))
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// Offers is everything this module can satisfy: its own name, and what it provides.
|
||||
func (m Manifest) Offers() []string {
|
||||
out := append([]string{m.Module}, m.Provides...)
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,302 @@
|
||||
package catalogue
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Resolving is turning "these modules are assigned here" into "this is what the node runs".
|
||||
//
|
||||
// It refuses rather than guesses, everywhere. novox/hq ADR 0009: a requirement with several
|
||||
// answers is refused and named, because counting candidates has no surprising behaviour and a
|
||||
// solver that picks has to be understood before its answer can be trusted.
|
||||
|
||||
// Node is what resolution needs to know about the machine.
|
||||
type Node struct {
|
||||
Name string
|
||||
Site string
|
||||
// Capabilities the machine actually has, as its profile reported them. Only the present ones
|
||||
// — a capability that was looked for and not found is the same as one nobody looked for, as
|
||||
// far as deciding what may run here goes.
|
||||
Capabilities map[string]bool
|
||||
}
|
||||
|
||||
// Held is a claim somebody already has, used for the scopes wider than one node.
|
||||
type Held struct {
|
||||
Claim string
|
||||
Scope string
|
||||
Node string
|
||||
Module string
|
||||
Site string
|
||||
}
|
||||
|
||||
// Resolution is what a node should run, and why.
|
||||
type Resolution struct {
|
||||
// Modules in the order they were resolved: assigned first, then what they pulled in.
|
||||
Modules []Manifest
|
||||
// Because says why each module is here — assigned, or required by something.
|
||||
Because map[string]string
|
||||
// Claims is what this node's set holds, so wider scopes can be checked against it.
|
||||
Claims []Held
|
||||
}
|
||||
|
||||
// Refusal is why a set of assignments cannot become a declaration.
|
||||
//
|
||||
// Every reason at once rather than the first, and each says what to do about it. A person
|
||||
// resolving these fixes them in one pass or in four.
|
||||
type Refusal struct{ Problems []string }
|
||||
|
||||
func (r *Refusal) Error() string {
|
||||
return "these assignments cannot be applied:\n - " + strings.Join(r.Problems, "\n - ")
|
||||
}
|
||||
|
||||
// ErrAmbiguous is returned inside a Refusal when a requirement has more than one answer.
|
||||
var ErrAmbiguous = errors.New("more than one module provides that")
|
||||
|
||||
// Resolve works out everything a node runs, from what was assigned to it.
|
||||
//
|
||||
// The catalogue is every module the mesh knows about; assigned is what a person put on this node.
|
||||
// What comes back is the closure — assigned modules plus everything they require — or a refusal
|
||||
// naming every reason it could not be closed.
|
||||
func Resolve(catalogue map[string]Manifest, assigned []string, node Node, elsewhere []Held) (Resolution, error) {
|
||||
var problems []string
|
||||
|
||||
// What each name can be satisfied by. Built once from the whole catalogue, because "how many
|
||||
// modules provide this" is the question the whole rule turns on.
|
||||
offers := map[string][]string{}
|
||||
for _, m := range catalogue {
|
||||
for _, o := range m.Offers() {
|
||||
offers[o] = append(offers[o], m.Module)
|
||||
}
|
||||
}
|
||||
for k := range offers {
|
||||
sort.Strings(offers[k])
|
||||
}
|
||||
|
||||
chosen := map[string]bool{}
|
||||
because := map[string]string{}
|
||||
var order []string
|
||||
|
||||
// What the set already offers, which is the first thing a requirement is checked against.
|
||||
//
|
||||
// Without this, assigning zsh does not satisfy something that requires a shell: the
|
||||
// requirement is counted against the catalogue, three modules provide it, and the answer is
|
||||
// still "choose one" after somebody has chosen one. That makes the remedy useless, and it is
|
||||
// how this read when first used.
|
||||
satisfied := map[string]bool{}
|
||||
|
||||
// Everything a person assigned goes in first. Those are choices already made, and a
|
||||
// requirement one of them answers is not a choice to put back to anybody.
|
||||
queue := append([]string{}, assigned...)
|
||||
for _, a := range assigned {
|
||||
because[a] = "assigned"
|
||||
if m, known := catalogue[a]; known {
|
||||
for _, o := range m.Offers() {
|
||||
satisfied[o] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for len(queue) > 0 {
|
||||
want := queue[0]
|
||||
queue = queue[1:]
|
||||
if chosen[want] {
|
||||
continue
|
||||
}
|
||||
// Already answered by something in the set. This is the case that makes assigning zsh do
|
||||
// what a person meant by it.
|
||||
if satisfied[want] && !isModule(catalogue, want) {
|
||||
continue
|
||||
}
|
||||
|
||||
candidates := offers[want]
|
||||
switch len(candidates) {
|
||||
case 0:
|
||||
problems = append(problems, fmt.Sprintf(
|
||||
"nothing provides %q, wanted by %s", want, because[want]))
|
||||
continue
|
||||
case 1:
|
||||
// No choice to make, so none is made. This is the case that lets `install i3` bring
|
||||
// in xorg without anybody being asked anything.
|
||||
default:
|
||||
problems = append(problems, fmt.Sprintf(
|
||||
"%q is wanted by %s and %d modules provide it — choose one and assign it: %s",
|
||||
want, because[want], len(candidates), strings.Join(candidates, ", ")))
|
||||
continue
|
||||
}
|
||||
|
||||
m := catalogue[candidates[0]]
|
||||
if chosen[m.Module] {
|
||||
continue
|
||||
}
|
||||
chosen[m.Module] = true
|
||||
order = append(order, m.Module)
|
||||
for _, o := range m.Offers() {
|
||||
satisfied[o] = true
|
||||
}
|
||||
if _, ok := because[m.Module]; !ok {
|
||||
because[m.Module] = fmt.Sprintf("required by %s", because[want])
|
||||
}
|
||||
|
||||
for _, r := range m.Requires {
|
||||
if _, ok := because[r]; !ok {
|
||||
because[r] = m.Module
|
||||
}
|
||||
queue = append(queue, r)
|
||||
}
|
||||
}
|
||||
|
||||
resolution := Resolution{Because: because}
|
||||
for _, n := range order {
|
||||
resolution.Modules = append(resolution.Modules, catalogue[n])
|
||||
}
|
||||
|
||||
problems = append(problems, checkCapabilities(resolution.Modules, node)...)
|
||||
claims, claimProblems := checkClaims(resolution.Modules, node, elsewhere)
|
||||
problems = append(problems, claimProblems...)
|
||||
problems = append(problems, checkResources(resolution.Modules)...)
|
||||
resolution.Claims = claims
|
||||
|
||||
if len(problems) > 0 {
|
||||
sort.Strings(problems)
|
||||
return Resolution{}, &Refusal{Problems: problems}
|
||||
}
|
||||
return resolution, nil
|
||||
}
|
||||
|
||||
// isModule reports whether a name is a module in its own right rather than only something
|
||||
// modules provide.
|
||||
//
|
||||
// A requirement naming a module is not satisfied by something else providing that name: `i3`
|
||||
// requires `xorg` and means xorg, not "anything calling itself a display server".
|
||||
func isModule(catalogue map[string]Manifest, want string) bool {
|
||||
_, ok := catalogue[want]
|
||||
return ok
|
||||
}
|
||||
|
||||
// checkCapabilities refuses a module the machine cannot run.
|
||||
//
|
||||
// Said as a fact about the machine rather than about the module, because that is what it is and
|
||||
// because nothing can be installed to change it.
|
||||
func checkCapabilities(modules []Manifest, node Node) []string {
|
||||
var problems []string
|
||||
for _, m := range modules {
|
||||
for _, c := range m.Capabilities {
|
||||
if !node.Capabilities[c] {
|
||||
problems = append(problems, fmt.Sprintf(
|
||||
"%s needs the capability %q and %s does not have it — this is the wrong "+
|
||||
"machine, not a missing module", m.Module, c, node.Name))
|
||||
}
|
||||
}
|
||||
}
|
||||
return problems
|
||||
}
|
||||
|
||||
// checkClaims refuses two modules holding one singular thing.
|
||||
//
|
||||
// Within this node's own set, and against what is already held elsewhere for the wider scopes. A
|
||||
// claim at mesh scope is the same idea as the mesh's one hub, said once instead of hard-coded.
|
||||
func checkClaims(modules []Manifest, node Node, elsewhere []Held) ([]Held, []string) {
|
||||
var problems []string
|
||||
var held []Held
|
||||
|
||||
byScope := map[string]map[string]string{} // scope → claim → module
|
||||
for _, m := range modules {
|
||||
for _, c := range m.Claims {
|
||||
scope := c.At()
|
||||
if byScope[scope] == nil {
|
||||
byScope[scope] = map[string]string{}
|
||||
}
|
||||
if other, taken := byScope[scope][c.Name]; taken {
|
||||
problems = append(problems, fmt.Sprintf(
|
||||
"%s and %s both claim %q, and only one thing may hold it per %s",
|
||||
other, m.Module, c.Name, scope))
|
||||
continue
|
||||
}
|
||||
byScope[scope][c.Name] = m.Module
|
||||
held = append(held, Held{Claim: c.Name, Scope: scope, Node: node.Name,
|
||||
Module: m.Module, Site: node.Site})
|
||||
}
|
||||
}
|
||||
|
||||
// And against the rest of the mesh, for the scopes that reach past this machine.
|
||||
for _, h := range held {
|
||||
for _, e := range elsewhere {
|
||||
if e.Node == node.Name || e.Claim != h.Claim || e.Scope != h.Scope {
|
||||
continue
|
||||
}
|
||||
switch h.Scope {
|
||||
case ScopeMesh:
|
||||
problems = append(problems, fmt.Sprintf(
|
||||
"%s on %s claims %q, which %s on %s already holds — one per mesh",
|
||||
h.Module, node.Name, h.Claim, e.Module, e.Node))
|
||||
case ScopeSite:
|
||||
if node.Site != "" && node.Site == e.Site {
|
||||
problems = append(problems, fmt.Sprintf(
|
||||
"%s on %s claims %q, which %s on %s already holds at %s — one per site",
|
||||
h.Module, node.Name, h.Claim, e.Module, e.Node, node.Site))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return held, problems
|
||||
}
|
||||
|
||||
// checkResources refuses two modules writing the same thing.
|
||||
//
|
||||
// This costs no manifest field: the mesh already holds every resource of every module, so two
|
||||
// declaring one path or one unit are visible without either having to know about the other. A
|
||||
// declared claim is only for the abstract conflicts nothing in the resources reveals.
|
||||
func checkResources(modules []Manifest) []string {
|
||||
var problems []string
|
||||
owner := map[string]string{}
|
||||
|
||||
for _, m := range modules {
|
||||
for _, r := range m.Resources {
|
||||
for _, field := range []string{"path", "unit", "name", "package"} {
|
||||
value, ok := r[field].(string)
|
||||
if !ok || value == "" {
|
||||
continue
|
||||
}
|
||||
key := field + " " + value
|
||||
if other, taken := owner[key]; taken && other != m.Module {
|
||||
problems = append(problems, fmt.Sprintf(
|
||||
"%s and %s both declare the %s %q", other, m.Module, field, value))
|
||||
}
|
||||
owner[key] = m.Module
|
||||
}
|
||||
}
|
||||
}
|
||||
return problems
|
||||
}
|
||||
|
||||
// Declaration is everything the resolved modules put on the node, as the host reads it.
|
||||
//
|
||||
// Resource identities are prefixed with the module they came from. Two modules may reasonably
|
||||
// both call something "config", and without this the second would silently replace the first —
|
||||
// the node applying one of them and reporting success.
|
||||
func (r Resolution) Declaration() []map[string]any {
|
||||
var out []map[string]any
|
||||
for _, m := range r.Modules {
|
||||
for _, resource := range m.Resources {
|
||||
copied := map[string]any{}
|
||||
for k, v := range resource {
|
||||
copied[k] = v
|
||||
}
|
||||
copied["id"] = m.Module + "." + fmt.Sprint(resource["id"])
|
||||
// A service saying what it reflects names resources within its own module, so those
|
||||
// are prefixed too or they would point at nothing.
|
||||
if reflects, ok := resource["restart-on"].([]any); ok {
|
||||
var renamed []any
|
||||
for _, id := range reflects {
|
||||
renamed = append(renamed, m.Module+"."+fmt.Sprint(id))
|
||||
}
|
||||
copied["restart-on"] = renamed
|
||||
}
|
||||
out = append(out, copied)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,319 @@
|
||||
package catalogue
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func mod(name string, provides, requires, capabilities []string, claims ...Claim) Manifest {
|
||||
return Manifest{Module: name, Provides: provides, Requires: requires,
|
||||
Capabilities: capabilities, Claims: claims}
|
||||
}
|
||||
|
||||
func shelf(ms ...Manifest) map[string]Manifest {
|
||||
out := map[string]Manifest{}
|
||||
for _, m := range ms {
|
||||
out[m.Module] = m
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func workstation() Node {
|
||||
return Node{Name: "workstation", Site: "house",
|
||||
Capabilities: map[string]bool{"seat": true, "container-runtime": true}}
|
||||
}
|
||||
|
||||
func names(r Resolution) []string {
|
||||
var out []string
|
||||
for _, m := range r.Modules {
|
||||
out = append(out, m.Module)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestARequirementWithOneAnswerIsTakenSilently(t *testing.T) {
|
||||
// `install i3` should bring in xorg without asking anybody anything, because there was no
|
||||
// choice to make. This is what keeps the refusing rule from being tiresome.
|
||||
got, err := Resolve(shelf(
|
||||
mod("i3", nil, []string{"xorg"}, []string{"seat"}),
|
||||
mod("xorg", []string{"display-server"}, nil, []string{"seat"}, Claim{Name: "the-seat"}),
|
||||
), []string{"i3"}, workstation(), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got.Modules) != 2 {
|
||||
t.Fatalf("resolved %v; i3 should have brought xorg with it", names(got))
|
||||
}
|
||||
if got.Because["xorg"] == "assigned" {
|
||||
t.Error("xorg is recorded as assigned; it was required")
|
||||
}
|
||||
}
|
||||
|
||||
func TestARequirementWithSeveralAnswersIsRefusedAndNamed(t *testing.T) {
|
||||
// The mesh does not pick. A default would be a choice made for somebody who finds out later,
|
||||
// and naming the candidates is the whole remedy.
|
||||
_, err := Resolve(shelf(
|
||||
mod("editor", nil, []string{"shell"}, nil),
|
||||
mod("bash", []string{"shell"}, nil, nil),
|
||||
mod("zsh", []string{"shell"}, nil, nil),
|
||||
mod("fish", []string{"shell"}, nil, nil),
|
||||
), []string{"editor"}, workstation(), nil)
|
||||
if err == nil {
|
||||
t.Fatal("a requirement with three answers was resolved without asking")
|
||||
}
|
||||
for _, want := range []string{"bash", "fish", "zsh", "choose one"} {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("the refusal does not mention %q: %v", want, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestARequirementWithNoAnswerIsRefused(t *testing.T) {
|
||||
_, err := Resolve(shelf(mod("i3", nil, []string{"xorg"}, nil)),
|
||||
[]string{"i3"}, workstation(), nil)
|
||||
if err == nil || !strings.Contains(err.Error(), "nothing provides") {
|
||||
t.Fatalf("a requirement nothing satisfies gave %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeveralModulesMayProvideTheSameThingAndCoexist(t *testing.T) {
|
||||
// Shells. Nothing is claimed, so any number may be assigned — which is the case that made
|
||||
// "flavor" look necessary and turns out to need nothing at all.
|
||||
got, err := Resolve(shelf(
|
||||
mod("bash", []string{"shell"}, nil, nil),
|
||||
mod("zsh", []string{"shell"}, nil, nil),
|
||||
mod("fish", []string{"shell"}, nil, nil),
|
||||
), []string{"bash", "zsh", "fish"}, workstation(), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("three shells could not coexist: %v", err)
|
||||
}
|
||||
if len(got.Modules) != 3 {
|
||||
t.Errorf("resolved %v", names(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestTwoModulesClaimingOneThingAreRefused(t *testing.T) {
|
||||
// xorg and wayland. Neither knows the other exists — the refusal comes from both claiming
|
||||
// the seat, which is what lets a third display server be added without editing either.
|
||||
_, err := Resolve(shelf(
|
||||
mod("xorg", []string{"display-server"}, nil, nil, Claim{Name: "the-seat"}),
|
||||
mod("wayland", []string{"display-server"}, nil, nil, Claim{Name: "the-seat"}),
|
||||
), []string{"xorg", "wayland"}, workstation(), nil)
|
||||
if err == nil {
|
||||
t.Fatal("two modules claiming the seat were both assigned")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "the-seat") || !strings.Contains(err.Error(), "per node") {
|
||||
t.Errorf("the refusal does not say what was claimed or how widely: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAThirdModuleNeedsNoChangeToTheOthers(t *testing.T) {
|
||||
// The property claims exist for. A third display server says what it claims and nothing else
|
||||
// in the catalogue is touched — where pairwise exclusion would need xorg and wayland edited
|
||||
// to know about it, and the edits would grow as the square of the count.
|
||||
catalogue := shelf(
|
||||
mod("xorg", []string{"display-server"}, nil, nil, Claim{Name: "the-seat"}),
|
||||
mod("wayland", []string{"display-server"}, nil, nil, Claim{Name: "the-seat"}),
|
||||
mod("mir", []string{"display-server"}, nil, nil, Claim{Name: "the-seat"}),
|
||||
)
|
||||
for _, pair := range [][]string{{"xorg", "mir"}, {"wayland", "mir"}} {
|
||||
if _, err := Resolve(catalogue, pair, workstation(), nil); err == nil {
|
||||
t.Errorf("%v were both assigned", pair)
|
||||
}
|
||||
}
|
||||
if _, err := Resolve(catalogue, []string{"mir"}, workstation(), nil); err != nil {
|
||||
t.Errorf("the newcomer alone was refused: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAMissingCapabilityIsSaidToBeTheWrongMachine(t *testing.T) {
|
||||
// The remedy differs from a missing module and the message has to say which. Nothing can be
|
||||
// installed to give a server a seat.
|
||||
server := Node{Name: "server", Capabilities: map[string]bool{"container-runtime": true}}
|
||||
_, err := Resolve(shelf(mod("xorg", nil, nil, []string{"seat"}, Claim{Name: "the-seat"})),
|
||||
[]string{"xorg"}, server, nil)
|
||||
if err == nil {
|
||||
t.Fatal("a display server was assigned to a machine with no seat")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "wrong machine") {
|
||||
t.Errorf("the refusal reads like a missing module: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAMeshWideClaimIsHeldByOneNode(t *testing.T) {
|
||||
// The hub, said as a claim rather than hard-coded. Another node already holds it, so this one
|
||||
// cannot.
|
||||
_, err := Resolve(shelf(mod("hub", nil, nil, nil, Claim{Name: "the-hub", Scope: ScopeMesh})),
|
||||
[]string{"hub"}, workstation(),
|
||||
[]Held{{Claim: "the-hub", Scope: ScopeMesh, Node: "anchor", Module: "hub"}})
|
||||
if err == nil {
|
||||
t.Fatal("two nodes both hold a mesh-wide claim")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "anchor") || !strings.Contains(err.Error(), "one per mesh") {
|
||||
t.Errorf("the refusal does not say who holds it: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestASiteClaimOnlyCollidesWithinThatSite(t *testing.T) {
|
||||
// A DHCP server per segment. Two of them is a fault at one site and perfectly ordinary
|
||||
// across two, and treating site as mesh would forbid the ordinary case.
|
||||
catalogue := shelf(mod("dhcp", nil, nil, nil, Claim{Name: "dhcp", Scope: ScopeSite}))
|
||||
|
||||
elsewhere := []Held{{Claim: "dhcp", Scope: ScopeSite, Node: "other", Module: "dhcp", Site: "house"}}
|
||||
if _, err := Resolve(catalogue, []string{"dhcp"}, workstation(), elsewhere); err == nil {
|
||||
t.Error("two DHCP servers at one site were allowed")
|
||||
}
|
||||
|
||||
faraway := []Held{{Claim: "dhcp", Scope: ScopeSite, Node: "other", Module: "dhcp", Site: "office"}}
|
||||
if _, err := Resolve(catalogue, []string{"dhcp"}, workstation(), faraway); err != nil {
|
||||
t.Errorf("a DHCP server at another site was treated as a collision: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTwoModulesWritingOneFileAreRefusedWithoutAnyClaim(t *testing.T) {
|
||||
// This conflict costs no manifest field: the mesh already holds every resource of every
|
||||
// module, so two declaring one path are visible without either knowing the other exists.
|
||||
a := mod("a", nil, nil, nil)
|
||||
a.Resources = []map[string]any{{"id": "conf", "type": "file", "path": "/etc/thing.conf"}}
|
||||
b := mod("b", nil, nil, nil)
|
||||
b.Resources = []map[string]any{{"id": "conf", "type": "file", "path": "/etc/thing.conf"}}
|
||||
|
||||
_, err := Resolve(shelf(a, b), []string{"a", "b"}, workstation(), nil)
|
||||
if err == nil {
|
||||
t.Fatal("two modules writing the same file were both assigned")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "/etc/thing.conf") {
|
||||
t.Errorf("the refusal does not name the file: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResourceIdentitiesCarryTheirModule(t *testing.T) {
|
||||
// Two modules may reasonably both call something "config". Without the prefix the second
|
||||
// would silently replace the first, and the node would apply one of them and report success.
|
||||
a := mod("a", nil, nil, nil)
|
||||
a.Resources = []map[string]any{{"id": "config", "type": "file", "path": "/etc/a"}}
|
||||
b := mod("b", nil, nil, nil)
|
||||
b.Resources = []map[string]any{{"id": "config", "type": "file", "path": "/etc/b"}}
|
||||
|
||||
got, err := Resolve(shelf(a, b), []string{"a", "b"}, workstation(), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, r := range got.Declaration() {
|
||||
id := r["id"].(string)
|
||||
if seen[id] {
|
||||
t.Errorf("two resources share the identity %q", id)
|
||||
}
|
||||
seen[id] = true
|
||||
}
|
||||
if !seen["a.config"] || !seen["b.config"] {
|
||||
t.Errorf("identities are not qualified by module: %v", seen)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWhatAServiceReflectsIsQualifiedToo(t *testing.T) {
|
||||
// Otherwise it names a resource that no longer exists under that identity, and the service
|
||||
// quietly stops being restarted when its own configuration changes.
|
||||
m := mod("thing", nil, nil, nil)
|
||||
m.Resources = []map[string]any{
|
||||
{"id": "conf", "type": "file", "path": "/etc/thing.conf"},
|
||||
{"id": "svc", "type": "service", "unit": "thing.service", "state": "running",
|
||||
"restart-on": []any{"conf"}},
|
||||
}
|
||||
got, err := Resolve(shelf(m), []string{"thing"}, workstation(), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, r := range got.Declaration() {
|
||||
if r["id"] == "thing.svc" {
|
||||
if got := fmt.Sprint(r["restart-on"]); got != "[thing.conf]" {
|
||||
t.Errorf("a service reflects %s, which is not a resource in the declaration", got)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Error("the service is missing from the declaration")
|
||||
}
|
||||
|
||||
func TestEveryReasonIsGivenAtOnce(t *testing.T) {
|
||||
// Somebody resolving these fixes them in one pass or in four.
|
||||
server := Node{Name: "server", Capabilities: map[string]bool{}}
|
||||
_, err := Resolve(shelf(
|
||||
mod("xorg", nil, nil, []string{"seat"}, Claim{Name: "the-seat"}),
|
||||
mod("wayland", nil, nil, []string{"seat"}, Claim{Name: "the-seat"}),
|
||||
), []string{"xorg", "wayland"}, server, nil)
|
||||
if err == nil {
|
||||
t.Fatal("expected refusals")
|
||||
}
|
||||
if strings.Count(err.Error(), "\n - ") < 3 {
|
||||
t.Errorf("only some problems were reported:\n%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestACycleStopsRatherThanRunsAway(t *testing.T) {
|
||||
// Two modules requiring each other is a mistake somebody makes, and it must produce an answer
|
||||
// rather than a stack overflow.
|
||||
got, err := Resolve(shelf(
|
||||
mod("a", nil, []string{"b"}, nil),
|
||||
mod("b", nil, []string{"a"}, nil),
|
||||
), []string{"a"}, workstation(), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got.Modules) != 2 {
|
||||
t.Errorf("a cycle resolved to %v", names(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestChoosingOneSatisfiesTheRequirement(t *testing.T) {
|
||||
// The other half of refusing. "Choose one and assign it" has to actually work, or the remedy
|
||||
// names three modules and then ignores the one you pick — which is how this read the first
|
||||
// time it was used on a real mesh.
|
||||
got, err := Resolve(shelf(
|
||||
mod("editor", nil, []string{"shell"}, nil),
|
||||
mod("bash", []string{"shell"}, nil, nil),
|
||||
mod("zsh", []string{"shell"}, nil, nil),
|
||||
mod("fish", []string{"shell"}, nil, nil),
|
||||
), []string{"editor", "zsh"}, workstation(), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("choosing a shell did not satisfy the requirement for one: %v", err)
|
||||
}
|
||||
if len(got.Modules) != 2 {
|
||||
t.Errorf("resolved %v; only the chosen shell should have come in", names(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestChoosingSeveralIsStillFine(t *testing.T) {
|
||||
// And the choice is not exclusive. Nothing is claimed, so a person may have all three and
|
||||
// the requirement is answered by whichever they picked.
|
||||
got, err := Resolve(shelf(
|
||||
mod("editor", nil, []string{"shell"}, nil),
|
||||
mod("bash", []string{"shell"}, nil, nil),
|
||||
mod("zsh", []string{"shell"}, nil, nil),
|
||||
mod("fish", []string{"shell"}, nil, nil),
|
||||
), []string{"editor", "zsh", "bash", "fish"}, workstation(), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got.Modules) != 4 {
|
||||
t.Errorf("resolved %v", names(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestARequirementNamingAModuleMeansThatModule(t *testing.T) {
|
||||
// i3 requires xorg and means xorg, not "anything calling itself a display server". Otherwise
|
||||
// assigning wayland would silently satisfy i3 and the machine would come up with a window
|
||||
// manager talking to nothing.
|
||||
_, err := Resolve(shelf(
|
||||
mod("i3", nil, []string{"xorg"}, nil),
|
||||
mod("xorg", []string{"display-server"}, nil, nil),
|
||||
mod("wayland", []string{"display-server", "xorg"}, nil, nil),
|
||||
), []string{"i3", "wayland"}, workstation(), nil)
|
||||
// wayland claiming to provide "xorg" is a manifest saying something untrue; what matters is
|
||||
// that a real xorg module still wins when it exists, and that the answer is not silent.
|
||||
if err != nil && !strings.Contains(err.Error(), "xorg") {
|
||||
t.Errorf("unexpected refusal: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user