-
Notifications
You must be signed in to change notification settings - Fork 158
/
mock.go
326 lines (304 loc) · 7.48 KB
/
mock.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright (c) 2022 Zededa, Inc.
// SPDX-License-Identifier: Apache-2.0
package netmonitor
import (
"bytes"
"context"
"fmt"
"net"
"reflect"
"sync"
"github.com/lf-edge/eve/pkg/pillar/base"
)
// MockNetworkMonitor is used for unit testing.
type MockNetworkMonitor struct {
sync.Mutex
Log *base.LogObject
MainRT int // inject syscall.RT_TABLE_MAIN for Linux network stack
eventSubs []subscriber
interfaces map[int]MockInterface // key = ifIndex
routes []Route
}
// MockInterface : a simulated network interface and its state.
type MockInterface struct {
Attrs IfAttrs
IPAddrs []*net.IPNet
HwAddr net.HardwareAddr
DNS DNSInfo
DHCP DHCPInfo
}
// AddOrUpdateInterface : allows to simulate an event of interface being added
// or updated.
func (m *MockNetworkMonitor) AddOrUpdateInterface(mockIf MockInterface) {
m.Lock()
defer m.Unlock()
if m.interfaces == nil {
m.interfaces = make(map[int]MockInterface)
}
ifIndex := mockIf.Attrs.IfIndex
prev, existed := m.interfaces[ifIndex]
m.interfaces[ifIndex] = mockIf
// Interface add/update event.
if !existed || !reflect.DeepEqual(mockIf.Attrs, prev.Attrs) {
m.publishEvent(IfChange{
Attrs: mockIf.Attrs,
Added: !existed,
})
}
// Interface IP address events.
for _, addr := range mockIf.IPAddrs {
var found bool
for _, prevAddr := range prev.IPAddrs {
if addr.IP.Equal(prevAddr.IP) &&
bytes.Equal(addr.Mask, prevAddr.Mask) {
found = true
break
}
}
if !found {
m.publishEvent(AddrChange{
IfIndex: ifIndex,
IfAddress: addr,
Deleted: false, // Added
})
}
}
for _, prevAddr := range prev.IPAddrs {
var found bool
for _, addr := range mockIf.IPAddrs {
if addr.IP.Equal(prevAddr.IP) &&
bytes.Equal(addr.Mask, prevAddr.Mask) {
found = true
break
}
}
if !found {
m.publishEvent(AddrChange{
IfIndex: ifIndex,
IfAddress: prevAddr,
Deleted: true,
})
}
}
// Interface DNS change event.
if !existed || !reflect.DeepEqual(mockIf.DNS, prev.DNS) {
m.publishEvent(DNSInfoChange{
IfIndex: ifIndex,
Info: mockIf.DNS,
})
}
}
// DelInterface : allows to simulate an event of removed interface.
func (m *MockNetworkMonitor) DelInterface(ifName string) {
m.Lock()
defer m.Unlock()
ifIndex, exists := m.getIfIndex(ifName)
if !exists {
if m.Log != nil {
m.Log.Fatal("failed to delete mock interface")
}
return
}
mockIf := m.interfaces[ifIndex]
delete(m.interfaces, ifIndex)
for _, ipAddr := range mockIf.IPAddrs {
m.publishEvent(AddrChange{
IfIndex: ifIndex,
IfAddress: ipAddr,
Deleted: true,
})
}
m.publishEvent(DNSInfoChange{
IfIndex: ifIndex,
Info: DNSInfo{},
})
m.publishEvent(IfChange{
Attrs: mockIf.Attrs,
Deleted: true,
})
}
// UpdateRoutes : allows to update the set of mock routes.
func (m *MockNetworkMonitor) UpdateRoutes(routes []Route) {
m.Lock()
defer m.Unlock()
prev := m.routes
m.routes = routes
// Publish events for new routes.
for _, route := range m.routes {
var found bool
for _, prevRoute := range prev {
if reflect.DeepEqual(route, prevRoute) {
found = true
break
}
}
if !found {
m.publishEvent(RouteChange{
Route: route,
Deleted: false,
})
}
}
// Publish events for removed routes.
for _, prevRoute := range prev {
var found bool
for _, route := range m.routes {
if reflect.DeepEqual(prevRoute, route) {
found = true
break
}
}
if !found {
m.publishEvent(RouteChange{
Route: prevRoute,
Deleted: true,
})
}
}
}
// ListInterfaces returns all mocked interfaces.
func (m *MockNetworkMonitor) ListInterfaces() (ifNames []string, err error) {
m.Lock()
defer m.Unlock()
for _, mockIf := range m.interfaces {
ifNames = append(ifNames, mockIf.Attrs.IfName)
}
return ifNames, nil
}
// GetInterfaceIndex returns index of the mock interface.
func (m *MockNetworkMonitor) GetInterfaceIndex(ifName string) (
ifIndex int, exists bool, err error) {
m.Lock()
defer m.Unlock()
ifIndex, exists = m.getIfIndex(ifName)
return ifIndex, exists, nil
}
// This method is run with the monitor in the locked state.
func (m *MockNetworkMonitor) getIfIndex(ifName string) (
ifIndex int, exists bool) {
for ifIndex, mockIf := range m.interfaces {
if mockIf.Attrs.IfName == ifName {
return ifIndex, true
}
}
return -1, false
}
func (m *MockNetworkMonitor) ifNotFoundErr(ifIndex int) error {
return fmt.Errorf("interface with index %d does not exist", ifIndex)
}
// GetInterfaceAttrs returns attributes of the mock interface.
func (m *MockNetworkMonitor) GetInterfaceAttrs(ifIndex int) (IfAttrs, error) {
m.Lock()
defer m.Unlock()
mockIf, exists := m.interfaces[ifIndex]
if !exists {
return IfAttrs{}, m.ifNotFoundErr(ifIndex)
}
return mockIf.Attrs, nil
}
// GetInterfaceAddrs returns addresses assigned to the mock interface.
func (m *MockNetworkMonitor) GetInterfaceAddrs(ifIndex int) (
[]*net.IPNet, net.HardwareAddr, error) {
m.Lock()
defer m.Unlock()
mockIf, exists := m.interfaces[ifIndex]
if !exists {
return nil, nil, m.ifNotFoundErr(ifIndex)
}
return mockIf.IPAddrs, mockIf.HwAddr, nil
}
// GetInterfaceDNSInfo returns DNS info associated with the mock interface.
func (m *MockNetworkMonitor) GetInterfaceDNSInfo(ifIndex int) (DNSInfo, error) {
m.Lock()
defer m.Unlock()
mockIf, exists := m.interfaces[ifIndex]
if !exists {
return DNSInfo{}, m.ifNotFoundErr(ifIndex)
}
return mockIf.DNS, nil
}
// GetInterfaceDHCPInfo returns DHCP info associated with the mock interface.
func (m *MockNetworkMonitor) GetInterfaceDHCPInfo(ifIndex int) (DHCPInfo, error) {
m.Lock()
defer m.Unlock()
mockIf, exists := m.interfaces[ifIndex]
if !exists {
return DHCPInfo{}, m.ifNotFoundErr(ifIndex)
}
return mockIf.DHCP, nil
}
// GetInterfaceDefaultGWs returns default gateways associated with the mock interface.
func (m *MockNetworkMonitor) GetInterfaceDefaultGWs(ifIndex int) (gws []net.IP, err error) {
m.Lock()
defer m.Unlock()
for _, route := range m.routes {
if route.Table != m.MainRT {
continue
}
if route.IfIndex != ifIndex {
continue
}
if route.Dst != nil {
continue
}
gws = append(gws, route.Gw)
}
return gws, nil
}
// ListRoutes lists all mock routes.
func (m *MockNetworkMonitor) ListRoutes(filters RouteFilters) (rts []Route, err error) {
m.Lock()
defer m.Unlock()
for _, route := range m.routes {
if filters.FilterByIf {
if route.IfIndex != filters.IfIndex {
continue
}
}
if filters.FilterByTable {
if route.Table != filters.Table {
continue
}
}
rts = append(rts, route)
}
return rts, nil
}
// ClearCache does nothing.
func (m *MockNetworkMonitor) ClearCache() {}
// WatchEvents allows to watch for event created artificially using
// AddOrUpdateInterface, DelInterface and UpdateRoutes.
func (m *MockNetworkMonitor) WatchEvents(ctx context.Context, subName string) <-chan Event {
m.Lock()
defer m.Unlock()
sub := subscriber{
name: subName,
events: make(chan Event, eventChanBufSize),
done: ctx.Done(),
}
m.eventSubs = append(m.eventSubs, sub)
return sub.events
}
// This method is run with the monitor in the locked state.
func (m *MockNetworkMonitor) publishEvent(ev Event) {
var activeSubs []subscriber
for _, sub := range m.eventSubs {
select {
case <-sub.done:
// unsubscribe
continue
default:
// continue subscription
}
select {
case sub.events <- ev:
default:
if m.Log != nil {
m.Log.Warnf("failed to deliver event %+v to subscriber %s",
ev, sub.name)
}
}
activeSubs = append(activeSubs, sub)
}
m.eventSubs = activeSubs
}