From b63094431b8fd5cf13f183c2c715c307fb231f44 Mon Sep 17 00:00:00 2001 From: Andrew Dunham Date: Fri, 9 Dec 2022 13:21:10 -0500 Subject: [PATCH] wgengine/router: fix tests on systems with older Busybox 'ip' binary Adjust the expected system output by removing the unsupported mask component including and after the slash in expected output like: fwmask 0xabc/0xdef This package's tests now pass in an Alpine container when the 'go' and 'iptables' packages are installed (and run as privileged so /dev/net/tun exists). Fixes #5928 Signed-off-by: Andrew Dunham Change-Id: Id1a3896282bfa36b64afaec7a47205e63ad88542 --- wgengine/router/router_linux_test.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/wgengine/router/router_linux_test.go b/wgengine/router/router_linux_test.go index 9534cdfed..b7c1a20c4 100644 --- a/wgengine/router/router_linux_test.go +++ b/wgengine/router/router_linux_test.go @@ -11,8 +11,10 @@ import ( "net/netip" "os" "reflect" + "regexp" "sort" "strings" + "sync" "sync/atomic" "testing" @@ -341,7 +343,7 @@ ip route add throw 192.168.0.0/24 table 52` + basic, t.Fatalf("failed to set router config: %v", err) } got := fake.String() - want := strings.TrimSpace(states[i].want) + want := adjustFwmask(t, strings.TrimSpace(states[i].want)) if diff := cmp.Diff(got, want); diff != "" { t.Fatalf("unexpected OS state (-got+want):\n%s", diff) } @@ -922,3 +924,23 @@ func TestCIDRDiff(t *testing.T) { } } } + +var ( + fwmaskSupported bool + fwmaskSupportedOnce sync.Once + fwmaskAdjustRe = regexp.MustCompile(`(?m)(fwmark 0x[0-9a-f]+)/0x[0-9a-f]+`) +) + +// adjustFwmask removes the "/0xmask" string from fwmask stanzas if the +// installed 'ip' binary does not support that format. +func adjustFwmask(t *testing.T, s string) string { + t.Helper() + fwmaskSupportedOnce.Do(func() { + fwmaskSupported, _ = ipCmdSupportsFwmask() + }) + if fwmaskSupported { + return s + } + + return fwmaskAdjustRe.ReplaceAllString(s, "$1") +}