Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkg/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
}
}
Expand Down
35 changes: 35 additions & 0 deletions pkg/network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package network

import (
"regexp"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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)
})
}
}