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 <andrew@du.nham.ca>
Change-Id: Id1a3896282bfa36b64afaec7a47205e63ad88542
pull/6690/head
Andrew Dunham 2 years ago
parent eb1adf629f
commit b63094431b

@ -11,8 +11,10 @@ import (
"net/netip" "net/netip"
"os" "os"
"reflect" "reflect"
"regexp"
"sort" "sort"
"strings" "strings"
"sync"
"sync/atomic" "sync/atomic"
"testing" "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) t.Fatalf("failed to set router config: %v", err)
} }
got := fake.String() got := fake.String()
want := strings.TrimSpace(states[i].want) want := adjustFwmask(t, strings.TrimSpace(states[i].want))
if diff := cmp.Diff(got, want); diff != "" { if diff := cmp.Diff(got, want); diff != "" {
t.Fatalf("unexpected OS state (-got+want):\n%s", 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")
}

Loading…
Cancel
Save