Files
mesh-host/internal/reachable/collect_test.go
T
jschoubben 0c01c81c3f E8: run each file's unit tests as subtests of one test
The node-engine held 1,707 top-level unit tests against the budget of 500
a module. Each unit test file with two or more tests now runs them as
subtests of one test named for the file, each named as its function was,
in the same order: every check is kept, and the check counts 383. Files
with a TestMain, a re-run of a test by its name, or the integration tag
are left. Four files of this branch's earlier commits are formatted.

Builds: design 47 H2 §819; H-A-349 DQ-14
2026-11-22 00:30:05 +01:00

109 lines
3.4 KiB
Go

package reachable
import (
"context"
"os"
"strings"
"testing"
)
// Defends novox/hq ADR 0100: converging previews every listening socket and every published
// container port. Fixtures are captured from a real machine.
func fixture(t *testing.T, name string) string {
t.Helper()
raw, err := os.ReadFile("testdata/" + name)
if err != nil {
t.Fatal(err)
}
return string(raw)
}
func find(rs []Reach, protocol, address string, port int) (Reach, bool) {
for _, r := range rs {
if r.Protocol == protocol && r.Address == address && r.Port == port {
return r, true
}
}
return Reach{}, false
}
func testSocketsAreReadWithWhatHoldsThem(t *testing.T) {
got := Sockets(fixture(t, "ss.txt"))
if r, ok := find(got, "tcp", "0.0.0.0", 22); !ok || r.By != "sshd" {
t.Errorf("ssh not read: %+v", r)
}
if r, ok := find(got, "tcp", "::", 445); !ok || r.By != "smbd" {
t.Errorf("an IPv6 wildcard listener not read: %+v", r)
}
if _, ok := find(got, "udp", "fe80::849e:ccff:fea8:24c7", 123); !ok {
t.Error("a link-local address with a scope was not read")
}
if r, ok := find(got, "udp", "127.0.0.1", 53); !ok || r.By != "dnsmasq" {
t.Errorf("a loopback udp socket not read: %+v", r)
}
}
func testPublishedPortsNameTheirContainerAndItsPort(t *testing.T) {
got := Published(fixture(t, "docker-ps.txt"))
if r, ok := find(got, "tcp", "0.0.0.0", 8770); !ok || r.By != "whisper" || r.ContainerPort != 8000 || !r.Published {
t.Errorf("a published port: %+v", r)
}
if r, ok := find(got, "tcp", "0.0.0.0", 9001); !ok || r.ContainerPort != 9001 {
t.Errorf("a published range was not expanded: %+v", r)
}
if r, ok := find(got, "tcp", "127.0.0.1", 15673); !ok || r.ContainerPort != 15672 {
t.Errorf("a loopback-published port: %+v", r)
}
for _, r := range got {
if r.By == "umami_db" {
t.Errorf("an exposed and unpublished port was reported reachable: %+v", r)
}
}
}
func testAPublishedPortIsReportedOnceAsPublished(t *testing.T) {
merged := Merge(Sockets(fixture(t, "ss.txt")), Published(fixture(t, "docker-ps.txt")))
n := 0
for _, r := range merged {
if r.Protocol == "tcp" && r.Address == "0.0.0.0" && r.Port == 8770 {
n++
if !r.Published {
t.Errorf("the runtime's proxy was reported instead of the published port: %+v", r)
}
}
}
if n != 1 {
t.Errorf("port 8770 reported %d times", n)
}
if _, ok := find(merged, "tcp", "0.0.0.0", 22); !ok {
t.Error("a socket was lost in the merge")
}
}
func testCollectAsksSsAndTheRuntime(t *testing.T) {
var asked []string
run := func(_ context.Context, name string, args ...string) (string, error) {
asked = append(asked, name+" "+strings.Join(args, " "))
if name == "ss" {
return fixture(t, "ss.txt"), nil
}
return fixture(t, "docker-ps.txt"), nil
}
got, err := Collect(context.Background(), run)
if err != nil || len(got) == 0 {
t.Fatalf("%v %v", got, err)
}
if len(asked) != 2 {
t.Errorf("asked %v", asked)
}
}
// TestCollect runs this file's tests, each a subtest named as its function was (design 47 H2 §819, the test budget).
func TestCollect(t *testing.T) {
t.Run("SocketsAreReadWithWhatHoldsThem", testSocketsAreReadWithWhatHoldsThem)
t.Run("PublishedPortsNameTheirContainerAndItsPort", testPublishedPortsNameTheirContainerAndItsPort)
t.Run("APublishedPortIsReportedOnceAsPublished", testAPublishedPortIsReportedOnceAsPublished)
t.Run("CollectAsksSsAndTheRuntime", testCollectAsksSsAndTheRuntime)
}