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
67 lines
2.4 KiB
Go
67 lines
2.4 KiB
Go
package outward
|
|
|
|
import (
|
|
"errors"
|
|
"net/netip"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func prefixes(s ...string) []netip.Prefix {
|
|
var out []netip.Prefix
|
|
for _, p := range s {
|
|
out = append(out, netip.MustParsePrefix(p))
|
|
}
|
|
return out
|
|
}
|
|
|
|
// **Each link facing outside says the networks it is attached to** (novox/hq ADR 0340 §7), as the
|
|
// network's address and prefix, never the machine's own address on it — and every one, public and
|
|
// link-local included, because which count is the controller's judgement and said with why.
|
|
func testEachOutwardLinkSaysTheNetworksItIsOn(t *testing.T) {
|
|
addrs := map[string][]netip.Prefix{
|
|
"eth0": prefixes("192.168.1.10/24", "fe80::1/64", "fd12:3456:789a::5/64"),
|
|
"eth1": prefixes("203.0.113.7/24"),
|
|
// Not asked: the bridge is not a link facing outside.
|
|
"docker0": prefixes("172.17.0.1/16"),
|
|
}
|
|
got, err := Networks([]string{"eth1", "eth0"}, func(link string) ([]netip.Prefix, error) { return addrs[link], nil })
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := []Network{
|
|
{Link: "eth0", Network: "192.168.1.0/24"},
|
|
{Link: "eth0", Network: "fd12:3456:789a::/64"},
|
|
{Link: "eth0", Network: "fe80::/64"},
|
|
{Link: "eth1", Network: "203.0.113.0/24"},
|
|
}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("got %+v\nwant %+v", got, want)
|
|
}
|
|
}
|
|
|
|
// A link whose addresses cannot be read fails the whole, so the machine says the fact is unknown rather
|
|
// than reporting part of its networks as all of them.
|
|
func testAnUnreadableLinkIsNotAPartialAccount(t *testing.T) {
|
|
_, err := Networks([]string{"eth0"}, func(string) ([]netip.Prefix, error) { return nil, errors.New("denied") })
|
|
if err == nil {
|
|
t.Fatal("an unreadable link was reported as having no networks")
|
|
}
|
|
}
|
|
|
|
// A link gone between reading the routing table and reading its addresses has no networks, and the
|
|
// rest are still said.
|
|
func testALinkGoneSinceItWasListedHasNoNetworks(t *testing.T) {
|
|
got, err := Networks([]string{"gone0"}, RealAddresses)
|
|
if err != nil || len(got) != 0 {
|
|
t.Fatalf("got %v, %v", got, err)
|
|
}
|
|
}
|
|
|
|
// TestNetworks runs this file's tests, each a subtest named as its function was (design 47 H2 §819, the test budget).
|
|
func TestNetworks(t *testing.T) {
|
|
t.Run("EachOutwardLinkSaysTheNetworksItIsOn", testEachOutwardLinkSaysTheNetworksItIsOn)
|
|
t.Run("AnUnreadableLinkIsNotAPartialAccount", testAnUnreadableLinkIsNotAPartialAccount)
|
|
t.Run("ALinkGoneSinceItWasListedHasNoNetworks", testALinkGoneSinceItWasListedHasNoNetworks)
|
|
}
|