novox/hq 03-DESIGN/01-to-be/11-a-board.md, built. The board being replaced is one service reading every context's database directly — ADR 0008 violated by the one component with a reason to violate it. The cost is not hypothetical: a boundary nothing may cross can move, and one thing crossing it is enough to freeze it. A board that reads the provisioning tables breaks when provisioning changes them, and the change then gets weighed against the board. So the three questions are read once, by one function, for all three ways of saying them — a person's status, its JSON, and this page. Three implementations of "which machine is not doing what it was told" would be three chances to disagree. Refused and failed stay distinct all the way to the page: refused means the machine is exactly as it was and what is wrong is in what was sent; failed means it is in a state nobody declared. Different places to fix, so one word for both would send half the readers to the wrong one. It stores nothing, changes nothing, and every action it might offer already exists as a command. A board that cannot reach the mesh says so rather than rendering an empty page — an empty page says "nothing is wrong" in the one situation where nobody can know that. One test earns its place twice: a machine's own words are the whole reason the page is useful and the one thing on it nobody in this repository wrote, so they are shown and are not markup.
122 lines
4.8 KiB
Go
122 lines
4.8 KiB
Go
package main
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/novox/mesh-control/internal/inventory"
|
|
)
|
|
|
|
// Refused and failed stay distinct all the way to the page.
|
|
//
|
|
// **Refused means the machine is exactly as it was and what is wrong is in what was sent; failed
|
|
// means it is in a state nobody declared and what is wrong is on the machine.** They are fixed in
|
|
// different places, so a page saying "error" for both sends half its readers to the wrong one.
|
|
func TestRefusedAndFailedReachThePageAsDifferentThings(t *testing.T) {
|
|
rendered := render(t, viewOf(answers{
|
|
nodes: []inventory.Node{{Name: "anchor"}, {Name: "laptop"}},
|
|
wrong: []inventory.Doing{
|
|
{Node: "anchor", Outcome: "refused", Refused: "not a declaration this host speaks",
|
|
At: time.Now()},
|
|
{Node: "laptop", Outcome: "failed", At: time.Now(),
|
|
Failed: []inventory.FailedResource{{ID: "web.container", Error: "no such image"}}},
|
|
},
|
|
}))
|
|
// The rendered outcome, not the word anywhere on the page: both words appear in the
|
|
// stylesheet, so a plain Contains passes whatever the machine actually said. It did.
|
|
for _, want := range []string{`<span class="outcome refused">refused</span>`,
|
|
`<span class="outcome failed">failed</span>`} {
|
|
if !strings.Contains(rendered, want) {
|
|
t.Fatalf("the page does not distinguish the two — missing %s:\n%s", want, rendered)
|
|
}
|
|
}
|
|
// And the host's own words, which say exactly what it could not accept.
|
|
for _, want := range []string{"not a declaration this host speaks", "web.container", "no such image"} {
|
|
if !strings.Contains(rendered, want) {
|
|
t.Fatalf("the page does not say %q, so a reader must go and ask:\n%s", want, rendered)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Nothing wrong is said, not left blank. An empty page and a well mesh must not look alike.
|
|
func TestAWellMeshSaysSoRatherThanShowingNothing(t *testing.T) {
|
|
rendered := render(t, viewOf(answers{nodes: []inventory.Node{{Name: "anchor"}}}))
|
|
for _, want := range []string{
|
|
"Every machine is doing what it was told",
|
|
"Every machine has been heard from",
|
|
"Every module is what its source last had",
|
|
} {
|
|
if !strings.Contains(rendered, want) {
|
|
t.Fatalf("a well mesh does not say %q:\n%s", want, rendered)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A board that cannot reach the mesh must not render an empty page: that says "nothing is wrong"
|
|
// in the one situation where nobody can know it.
|
|
func TestABoardThatCannotReadTheMeshSaysSo(t *testing.T) {
|
|
rendered := render(t, view{Unreachable: "the store did not answer"})
|
|
if !strings.Contains(rendered, "the store did not answer") {
|
|
t.Fatalf("the reason is not on the page:\n%s", rendered)
|
|
}
|
|
if strings.Contains(rendered, "Every machine is doing what it was told") {
|
|
t.Fatal("a board that could not read the mesh reported that the mesh is well")
|
|
}
|
|
}
|
|
|
|
// Quiet is not broken, and the page says which it is.
|
|
func TestQuietIsNotReportedAsBroken(t *testing.T) {
|
|
rendered := render(t, viewOf(answers{
|
|
nodes: []inventory.Node{{Name: "laptop"}},
|
|
quiet: []inventory.Node{{Name: "laptop"}},
|
|
}))
|
|
if !strings.Contains(rendered, "not the same as tried and could not") {
|
|
t.Fatalf("the page does not separate quiet from broken:\n%s", rendered)
|
|
}
|
|
if !strings.Contains(rendered, "Every machine is doing what it was told") {
|
|
t.Fatalf("a quiet machine was counted as broken:\n%s", rendered)
|
|
}
|
|
}
|
|
|
|
// The page renders what a machine said, and a hostile string in it is not markup.
|
|
//
|
|
// What is on this page comes from machines, and a machine's own words are the whole reason the
|
|
// page is useful. They are also the one thing here that nobody in this repository wrote.
|
|
func TestWhatAMachineSaidIsNotMarkup(t *testing.T) {
|
|
rendered := render(t, viewOf(answers{
|
|
nodes: []inventory.Node{{Name: "anchor"}},
|
|
wrong: []inventory.Doing{{Node: "anchor", Outcome: "refused",
|
|
Refused: `<script>alert("from the machine")</script>`, At: time.Now()}},
|
|
}))
|
|
if strings.Contains(rendered, "<script>alert") {
|
|
t.Fatalf("a machine's words were rendered as markup:\n%s", rendered)
|
|
}
|
|
if !strings.Contains(rendered, "<script>") {
|
|
t.Fatalf("the words were not shown at all, so the reader cannot see what it said:\n%s", rendered)
|
|
}
|
|
}
|
|
|
|
// A module behind its source names the machines running the old one — the part with consequences.
|
|
func TestBeingBehindNamesTheMachinesRunningTheOldOne(t *testing.T) {
|
|
rendered := render(t, viewOf(answers{
|
|
nodes: []inventory.Node{{Name: "laptop"}},
|
|
behind: map[string][]string{"web": {"laptop"}},
|
|
sources: map[string]inventory.Source{"web": {BuiltFrom: "aaaaaaaaaa", Head: "bbbbbbbbbb"}},
|
|
}))
|
|
for _, want := range []string{"web", "aaaaaaaa", "bbbbbbbb", "running on laptop"} {
|
|
if !strings.Contains(rendered, want) {
|
|
t.Fatalf("the page does not say %q:\n%s", want, rendered)
|
|
}
|
|
}
|
|
}
|
|
|
|
func render(t *testing.T, v view) string {
|
|
t.Helper()
|
|
var out strings.Builder
|
|
if err := page.Execute(&out, v); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return out.String()
|
|
}
|