-
Notifications
You must be signed in to change notification settings - Fork 229
/
mocknetio.go
57 lines (45 loc) · 1.25 KB
/
mocknetio.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package netio
import (
"errors"
"fmt"
"net"
)
type getInterfaceValidationFn func(name string) (*net.Interface, error)
type MockNetIO struct {
fail bool
failAttempt int
numTimesCalled int
getInterfaceFn getInterfaceValidationFn
}
// ErrMockNetIOFail - mock netio error
var ErrMockNetIOFail = errors.New("netio fail")
func NewMockNetIO(fail bool, failAttempt int) *MockNetIO {
return &MockNetIO{
fail: fail,
failAttempt: failAttempt,
}
}
func (netshim *MockNetIO) SetGetInterfaceValidatonFn(fn getInterfaceValidationFn) {
netshim.getInterfaceFn = fn
}
func (netshim *MockNetIO) GetNetworkInterfaceByName(name string) (*net.Interface, error) {
netshim.numTimesCalled++
if netshim.fail && netshim.failAttempt == netshim.numTimesCalled {
return nil, fmt.Errorf("%w:%s", ErrMockNetIOFail, name)
}
if netshim.getInterfaceFn != nil {
return netshim.getInterfaceFn(name)
}
hwAddr, _ := net.ParseMAC("ab:cd:ef:12:34:56")
return &net.Interface{
//nolint:gomnd // Dummy MTU
MTU: 1000,
Name: name,
HardwareAddr: hwAddr,
//nolint:gomnd // Dummy interface index
Index: 2,
}, nil
}
func (netshim *MockNetIO) GetNetworkInterfaceAddrs(iface *net.Interface) ([]net.Addr, error) {
return []net.Addr{}, nil
}