package main import ( "strings" "testing" "github.com/novox/mesh-controller/internal/catalogue" ) // `plan` tells a person what a module would open and why, from the same `why` every listens // entry already carries for the firewall (novox/hq ADR 0007) — so deciding whether to assign a // module does not need reading its manifest first. func TestListensLinesShowWhatAModuleWouldOpenAndWhy(t *testing.T) { m := catalogue.Manifest{Module: "minio", Listens: []catalogue.Listening{ {Port: 9000, From: catalogue.FromMesh, Why: "the S3 endpoint"}, {Port: 9001, From: catalogue.FromMesh}, }} got := listensLines(m) if len(got) != 2 { t.Fatalf("two listens entries, got %d: %v", len(got), got) } if !strings.Contains(got[0], "9000/tcp") || !strings.Contains(got[0], "the S3 endpoint") { t.Errorf("the port and its why did not both appear: %q", got[0]) } if strings.Contains(got[1], "—") { t.Errorf("a listens entry with no why should not print a dash: %q", got[1]) } if !strings.Contains(got[1], "9001/tcp") { t.Errorf("the port still appears without a why: %q", got[1]) } } func TestListensLinesAreEmptyForAModuleWithNothingToListenOn(t *testing.T) { if got := listensLines(catalogue.Manifest{Module: "board"}); len(got) != 0 { t.Errorf("a module with no listens should print nothing, got %v", got) } } // `plan --show` says when a file is written into rather than over (novox/hq issue 128), or the // mesh's region of a hosts file reads as the whole file. func TestAFileWrittenIntoIsShownAsSuch(t *testing.T) { region := shownAs(map[string]any{ "id": "mesh-wireguard.fact-node-names", "path": "/etc/hosts", "into": "block"}) if region != "mesh-wireguard.fact-node-names /etc/hosts (written into, block)" { t.Errorf("the region is shown as %q", region) } whole := shownAs(map[string]any{"id": "dnsmasq.fact-node-zones", "path": "/etc/mesh-resolver/nodes.conf"}) if strings.Contains(whole, "written into") { t.Errorf("a whole file is shown as written into: %q", whole) } }