package main import ( "bytes" "io" "os" "strings" "testing" "github.com/novox/mesh-controller/internal/inventory" ) // "4 machine(s), all doing what they were told, all heard from, running what the mesh would send // them, and every module current with its source" was true for eleven hours of a mesh in which no // module could reach another (novox/hq 04-ISSUES/145). Every question it answers is about the mesh // and a machine agreeing; none of them dials anything. // printed captures what a function writes to stdout. func printed(t *testing.T, f func() error) string { t.Helper() old := os.Stdout r, w, err := os.Pipe() if err != nil { t.Fatal(err) } os.Stdout = w runErr := f() _ = w.Close() os.Stdout = old var buf bytes.Buffer if _, err := io.Copy(&buf, r); err != nil { t.Fatal(err) } if runErr != nil { t.Fatal(runErr) } return buf.String() } func TestTheAllWellSentenceSaysWhatItDoesNotCover(t *testing.T) { // A mesh with nothing to say. The sentence below was true of a mesh in which no module could // reach another, for eleven hours. got := printed(t, func() error { return printStatus(answers{nodes: []inventory.Node{{Name: "anchor"}, {Name: "laptop"}}}) }) if !strings.Contains(got, "all doing what they were told") { t.Fatalf("a mesh with nothing to say did not print the all-well sentence:\n%s", got) } // And now says what it is not a claim about. for _, want := range []string{"Nothing here dials a provision", "04-ISSUES/145"} { if !strings.Contains(got, want) { t.Fatalf("the all-well sentence does not say %q:\n%s", want, got) } } } func TestAMeshWithSomethingToSayDoesNotPrintTheScopeLine(t *testing.T) { // The scope belongs to the all-well sentence. A mesh with something wrong has specific things to // read, and appending a caveat to those is noise. got := printed(t, func() error { return printStatus(answers{ nodes: []inventory.Node{{Name: "anchor"}}, untaken: map[string]map[string]int{"anchor": {"route-proxy": 3}}, }) }) if strings.Contains(got, "Nothing here dials a provision") { t.Fatalf("a mesh with a held module printed the all-well scope line:\n%s", got) } if strings.Contains(got, "all doing what they were told") { t.Fatalf("a mesh with a held module printed the all-well sentence:\n%s", got) } if !strings.Contains(got, "route-proxy") { t.Fatalf("the held module is not named:\n%s", got) } }