diff --git a/pkg/network/network.go b/pkg/network/network.go index cbc73c707..d3daefd64 100644 --- a/pkg/network/network.go +++ b/pkg/network/network.go @@ -67,9 +67,10 @@ func getTapIndex() (int, error) { if err != nil { return 0, err } + tapRe := regexp.MustCompile(`^tap\d+(_urunc)?$`) tapCount := 0 for _, iface := range ifaces { - if strings.Contains(iface.Name, "tap") { + if tapRe.MatchString(iface.Name) { tapCount++ } } diff --git a/pkg/network/network_test.go b/pkg/network/network_test.go index da8c1d3d3..a0b12ccc2 100644 --- a/pkg/network/network_test.go +++ b/pkg/network/network_test.go @@ -15,6 +15,7 @@ package network import ( + "regexp" "testing" "github.com/stretchr/testify/assert" @@ -56,3 +57,37 @@ func TestNewNetworkManager(t *testing.T) { }) } } + +func TestGetTapIndex(t *testing.T) { + tapCount, err := getTapIndex() + assert.NoError(t, err, "getTapIndex() should not error") + assert.GreaterOrEqual(t, tapCount, 0, "tapCount should be >= 0") +} + +func TestTapDeviceRegexMatching(t *testing.T) { + tapRe := regexp.MustCompile(`^tap\d+(_urunc)?$`) + + tests := []struct { + ifaceName string + shouldMatch bool + }{ + {"tap0_urunc", true}, + {"tap1_urunc", true}, + {"tap0", true}, + {"tap12", true}, + {"vtap0", false}, + {"cni-tap0", false}, + {"bootstrap0", false}, + {"stape0", false}, + {"tap-master", false}, + {"eth0", false}, + {"lo", false}, + } + + for _, tt := range tests { + t.Run(tt.ifaceName, func(t *testing.T) { + matched := tapRe.MatchString(tt.ifaceName) + assert.Equal(t, tt.shouldMatch, matched, "interface name %s match expectation failed", tt.ifaceName) + }) + } +}