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{`refused`, `failed`} { 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: ``, At: time.Now()}}, })) if strings.Contains(rendered, "