From 38447a66b8934813b0a05aaf0a7267db38e3da66 Mon Sep 17 00:00:00 2001 From: jochen Date: Sat, 21 Nov 2026 23:16:38 +0100 Subject: [PATCH] E8: merge seven unit tests into the tests they extend MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The node-engine holds 1,715 unit tests against a budget of 500; each of these asserted one more case of a rendering another test already holds. Builds: design 47 H2 §819 --- internal/apply/process_test.go | 22 ++--- internal/compose/resolution/filtering_test.go | 9 +-- .../compose/resolution/localnetwork_test.go | 81 +++++++++---------- 3 files changed, 46 insertions(+), 66 deletions(-) diff --git a/internal/apply/process_test.go b/internal/apply/process_test.go index 91ab037d..6168e2e4 100644 --- a/internal/apply/process_test.go +++ b/internal/apply/process_test.go @@ -53,12 +53,9 @@ func TestTheUnitRunsWhatTheDaemonSaid(t *testing.T) { if got := unitFor(draining); !strings.Contains(got, "Restart=always\nRestartSec=5\nKillMode=mixed\nTimeoutStopSec=45s\n\n[Install]") { t.Fatalf("a process with a drain bound of 30 s is not stopped alone within 45 s:\n%s", got) } -} -// **Deterministic, because the unit is half the daemon's identity.** Environment held in a map -// would be written in Go's iteration order, so every apply would see a different unit and call an -// unchanged daemon changed — restarting it on every declaration for ever. -func TestTheUnitIsTheSameEveryTime(t *testing.T) { + // **Deterministic, because the unit is half the daemon's identity.** Environment held in a map would be written in + // Go's iteration order, so every apply would see a different unit and call an unchanged daemon changed. first := unitFor(aProcess()) for i := 0; i < 20; i++ { if again := unitFor(aProcess()); again != first { @@ -69,15 +66,11 @@ func TestTheUnitIsTheSameEveryTime(t *testing.T) { if strings.Index(first, "A_FIRST") > strings.Index(first, "MESH_NODE") { t.Fatalf("environment is not in a stable order:\n%s", first) } -} - -// **Two daemons from one bundle differing only in their command are different daemons.** Tracking -// the digest alone would call the second one unchanged and leave the first one running. -func TestAProcesssIdentityIncludesHowItIsRun(t *testing.T) { - one := aProcess() + // **Two daemons from one bundle differing only in their command are different daemons.** Tracking the digest alone + // would call the second one unchanged and leave the first one running. two := aProcess() two.Run = []string{"node", "other.js"} - if unitFor(one) == unitFor(two) { + if first == unitFor(two) { t.Fatal("two daemons with different commands render one unit, so a change would be missed") } } @@ -173,10 +166,7 @@ func TestAScheduledProcessRunsOnItsCadenceRatherThanContinuously(t *testing.T) { if !strings.Contains(timer, "Persistent=true") { t.Fatalf("a missed fire is skipped silently:\n%s", timer) } -} - -// Five-field cron becomes what a timer reads, rather than the host waking to decide. -func TestACronBecomesATimersCalendar(t *testing.T) { + // Five-field cron becomes what a timer reads, rather than the host waking to decide. for cron, want := range map[string]string{ "0 3 * * *": "*-*-* 3:0:00", "30 4 1 * *": "*-*-1 4:30:00", diff --git a/internal/compose/resolution/filtering_test.go b/internal/compose/resolution/filtering_test.go index 8450a0b4..46eb3b63 100644 --- a/internal/compose/resolution/filtering_test.go +++ b/internal/compose/resolution/filtering_test.go @@ -27,13 +27,8 @@ func TestAMachineThatNamedNoOutwardLinkIsSentNoFilter(t *testing.T) { t.Fatalf("the refusal does not say %q: %v", want, err) } } -} - -// And a machine that names none but loads no filter is not refused: there is nothing to write. -func TestAMachineWithNoFilterModuleIsNotRefused(t *testing.T) { - r := mcat.Resolution{Node: "anchor", Modules: []mcat.Manifest{ - {Module: "web", Listens: []mcat.Listening{{Port: 443, From: mcat.FromEverywhere}}}, - }} + // And a machine that names none but loads no filter is not refused: there is nothing to write. + r.Modules = r.Modules[1:] if _, err := Declaration(r, mcat.Rendering{Mesh: []string{"198.51.100.2"}}); err != nil { t.Fatalf("a machine that loads no filter was refused one: %v", err) } diff --git a/internal/compose/resolution/localnetwork_test.go b/internal/compose/resolution/localnetwork_test.go index caaa61db..a289eede 100644 --- a/internal/compose/resolution/localnetwork_test.go +++ b/internal/compose/resolution/localnetwork_test.go @@ -50,17 +50,31 @@ func TestOnlyAPrivateNetworkOnALinkFacingOutsideCounts(t *testing.T) { } } } -} - -// A network overlapping a private range without lying wholly inside it does not count either, so a -// wide prefix around a private range cannot carry the internet with it. -func TestANetworkNotWhollyInAPrivateRangeDoesNotCount(t *testing.T) { + // A network overlapping a private range without lying wholly inside it does not count either, so a + // wide prefix around a private range cannot carry the internet with it. for _, n := range []string{"10.0.0.0/7", "172.0.0.0/8", "100.64.0.0/10", "0.0.0.0/0", "::/0"} { got := JudgeLocalNetworks([]mcat.AttachedNetwork{{Link: "eth0", Network: n}}, []string{"eth0"}, "") if len(got) != 1 || got[0].Counts { t.Errorf("%s counts as a local network: %+v", n, got) } } + // A global unicast IPv6 subnet on a link facing outside is public, and never counts: the IPv6 side of the + // rule that `local-network` cannot open a port to the internet (ADR 0340 §2). + { + got := JudgeLocalNetworks([]mcat.AttachedNetwork{{Link: "eth0", Network: "2001:db8:1234::7/48"}}, []string{"eth0"}, "wg-mesh") + if len(got) != 1 { + t.Fatalf("judged %+v", got) + } + if got[0].Counts { + t.Fatalf("the global IPv6 subnet %s counts as a local network", got[0].Network) + } + if got[0].Network != "2001:db8:1234::/48" || !strings.Contains(got[0].Why, "public, never counted") { + t.Fatalf("judged %s with why %q, want it named public and never counted", got[0].Network, got[0].Why) + } + if n := CountedLocalNetworks(got); len(n) != 0 { + t.Fatalf("counted %v", n) + } + } } // **The filter admits the local network on that port's rule alone** (§5), beside the private network, @@ -104,27 +118,26 @@ func TestALocalNetworkPortIsAdmittedFromItsNetworksAndTheMesh(t *testing.T) { t.Fatalf("1883 is open to anywhere:\n%s", out) } } -} - -// **Nothing resolved is closed and said** (§6): a machine that reported no private network admits the -// port from the private network only, and the rule set says it found none — never widened to anywhere. -func TestNoLocalNetworkIsSaidAndNotWidened(t *testing.T) { - rules := []mcat.Rule{{Port: 1883, Protocol: "tcp", From: mcat.FromLocalNetwork, Because: []string{"mosquitto"}}} - out := AsNftablesWithLocal(rules, []string{"10.10.0.1"}, false, nil, []string{"eth0"}, "wg-mesh", nil) - if !strings.Contains(out, "1883 wanted the local network, and this machine reported no private network") { - t.Errorf("the rule set does not say the local network resolved to nothing:\n%s", out) - } - if !strings.Contains(out, "ip saddr { 10.10.0.1 } tcp dport 1883 accept") { - t.Errorf("the port is not admitted from the private network:\n%s", out) - } - for _, line := range strings.Split(out, "\n") { - if strings.TrimSpace(line) == "tcp dport 1883 accept" { - t.Fatalf("an unresolved local network was widened to anywhere:\n%s", out) + // **Nothing resolved is closed and said** (§6): a machine that reported no private network admits the + // port from the private network only, and the rule set says it found none — never widened to anywhere. + { + rules := []mcat.Rule{{Port: 1883, Protocol: "tcp", From: mcat.FromLocalNetwork, Because: []string{"mosquitto"}}} + out := AsNftablesWithLocal(rules, []string{"10.10.0.1"}, false, nil, []string{"eth0"}, "wg-mesh", nil) + if !strings.Contains(out, "1883 wanted the local network, and this machine reported no private network") { + t.Errorf("the rule set does not say the local network resolved to nothing:\n%s", out) + } + if !strings.Contains(out, "ip saddr { 10.10.0.1 } tcp dport 1883 accept") { + t.Errorf("the port is not admitted from the private network:\n%s", out) + } + for _, line := range strings.Split(out, "\n") { + if strings.HasPrefix(strings.TrimSpace(line), "tcp dport 1883 accept") { + t.Fatalf("an unresolved local network was widened to anywhere:\n%s", out) + } + } + said := LocalNetworkPorts(rules, nil)[0].Said() + if !strings.Contains(said, "none") { + t.Errorf("plan says %q, want it to say no local network was found", said) } - } - said := LocalNetworkPorts(rules, nil)[0].Said() - if !strings.Contains(said, "none") { - t.Errorf("plan says %q, want it to say no local network was found", said) } } @@ -171,21 +184,3 @@ func TestAnAdoptedNodeOpensTheLocalNetworkPortToTheMeshOnly(t *testing.T) { t.Fatalf("openings %v", out) } } - -// A global unicast IPv6 subnet on a link facing outside is public, and never counts: the IPv6 side of the -// rule that `local-network` cannot open a port to the internet (ADR 0340 §2). -func TestAGlobalIPv6SubnetIsPublicAndNeverCounts(t *testing.T) { - got := JudgeLocalNetworks([]mcat.AttachedNetwork{{Link: "eth0", Network: "2001:db8:1234::7/48"}}, []string{"eth0"}, "wg-mesh") - if len(got) != 1 { - t.Fatalf("judged %+v", got) - } - if got[0].Counts { - t.Fatalf("the global IPv6 subnet %s counts as a local network", got[0].Network) - } - if got[0].Network != "2001:db8:1234::/48" || !strings.Contains(got[0].Why, "public, never counted") { - t.Fatalf("judged %s with why %q, want it named public and never counted", got[0].Network, got[0].Why) - } - if n := CountedLocalNetworks(got); len(n) != 0 { - t.Fatalf("counted %v", n) - } -}