-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinterface_manager_test.go
More file actions
568 lines (454 loc) · 14.8 KB
/
interface_manager_test.go
File metadata and controls
568 lines (454 loc) · 14.8 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
package zeroconf
import (
"net"
"sort"
"sync"
"syscall"
"testing"
"time"
)
// Helper to create mock interfaces
func mockInterface(index int, name string) net.Interface {
return net.Interface{
Index: index,
Name: name,
Flags: net.FlagUp | net.FlagMulticast,
}
}
// ============================================================================
// NewInterfaceManager Tests
// ============================================================================
func TestInterfaceManager_NewDynamicMode(t *testing.T) {
ifaces := []net.Interface{mockInterface(1, "eth0"), mockInterface(2, "wlan0")}
// nil requested = dynamic mode
mgr := NewInterfaceManager(ifaces, nil)
indices := mgr.ActiveIndices()
if len(indices) != 2 {
t.Errorf("expected 2 active indices, got %d", len(indices))
}
}
func TestInterfaceManager_NewExplicitMode(t *testing.T) {
ifaces := []net.Interface{mockInterface(1, "eth0"), mockInterface(2, "wlan0")}
requested := []string{"eth0", "wlan0"}
mgr := NewInterfaceManager(ifaces, requested)
indices := mgr.ActiveIndices()
if len(indices) != 2 {
t.Errorf("expected 2 active indices, got %d", len(indices))
}
}
func TestInterfaceManager_NewEmptyInitial(t *testing.T) {
mgr := NewInterfaceManager(nil, nil)
indices := mgr.ActiveIndices()
if len(indices) != 0 {
t.Errorf("expected 0 active indices, got %d", len(indices))
}
}
// ============================================================================
// ActiveIndices Tests
// ============================================================================
func TestInterfaceManager_ActiveIndices_ReturnsSnapshot(t *testing.T) {
ifaces := []net.Interface{mockInterface(1, "eth0"), mockInterface(5, "wlan0")}
mgr := NewInterfaceManager(ifaces, nil)
indices := mgr.ActiveIndices()
// Should contain both indices
sort.Ints(indices)
if len(indices) != 2 || indices[0] != 1 || indices[1] != 5 {
t.Errorf("expected [1, 5], got %v", indices)
}
}
func TestInterfaceManager_ActiveIndices_ReturnsEmptySliceNotNil(t *testing.T) {
mgr := NewInterfaceManager(nil, nil)
indices := mgr.ActiveIndices()
if indices == nil {
t.Error("expected empty slice, got nil")
}
if len(indices) != 0 {
t.Errorf("expected length 0, got %d", len(indices))
}
}
// ============================================================================
// MarkFailed Tests
// ============================================================================
func TestInterfaceManager_MarkFailed_InterfaceGoneError_RemovesInterface(t *testing.T) {
ifaces := []net.Interface{mockInterface(1, "eth0"), mockInterface(2, "wlan0")}
mgr := NewInterfaceManager(ifaces, nil)
// ENXIO indicates interface is gone
removed := mgr.MarkFailed(1, syscall.ENXIO)
if !removed {
t.Error("expected MarkFailed to return true for ENXIO")
}
indices := mgr.ActiveIndices()
if len(indices) != 1 {
t.Errorf("expected 1 active index after removal, got %d", len(indices))
}
if indices[0] != 2 {
t.Errorf("expected remaining index to be 2, got %d", indices[0])
}
}
func TestInterfaceManager_MarkFailed_TransientError_KeepsInterface(t *testing.T) {
ifaces := []net.Interface{mockInterface(1, "eth0")}
mgr := NewInterfaceManager(ifaces, nil)
// EAGAIN is transient - should not remove
removed := mgr.MarkFailed(1, syscall.EAGAIN)
if removed {
t.Error("expected MarkFailed to return false for transient error")
}
indices := mgr.ActiveIndices()
if len(indices) != 1 {
t.Errorf("expected interface to remain active, got %d active", len(indices))
}
}
func TestInterfaceManager_MarkFailed_Idempotent_SafeWhenAlreadyRemoved(t *testing.T) {
ifaces := []net.Interface{mockInterface(1, "eth0")}
mgr := NewInterfaceManager(ifaces, nil)
// First removal
mgr.MarkFailed(1, syscall.ENXIO)
// Second removal of same index - should not panic
removed := mgr.MarkFailed(1, syscall.ENXIO)
// Returns true because error still indicates "interface gone"
if !removed {
t.Error("expected MarkFailed to return true even when already removed")
}
indices := mgr.ActiveIndices()
if len(indices) != 0 {
t.Errorf("expected 0 active indices, got %d", len(indices))
}
}
func TestInterfaceManager_MarkFailed_UnknownIndex_DoesNotPanic(t *testing.T) {
mgr := NewInterfaceManager(nil, nil)
// Index 999 was never added - should not panic
removed := mgr.MarkFailed(999, syscall.ENXIO)
if !removed {
t.Error("expected true because error indicates interface gone")
}
}
// ============================================================================
// Adaptive Backoff Tests
// ============================================================================
func TestInterfaceManager_AdaptiveBackoff_FirstFailure1s(t *testing.T) {
ifaces := []net.Interface{mockInterface(1, "eth0")}
mgr := NewInterfaceManager(ifaces, nil)
// Fail the interface
mgr.MarkFailed(1, syscall.ENXIO)
// Check backoff is ~1s
mgr.mu.RLock()
state := mgr.failures["eth0"]
mgr.mu.RUnlock()
if state == nil {
t.Fatal("expected failure state to exist")
}
if state.count != 1 {
t.Errorf("expected count 1, got %d", state.count)
}
// retryAt should be ~1s from now
expectedBackoff := backoffFirst
actualBackoff := time.Until(state.retryAt)
if actualBackoff < expectedBackoff-100*time.Millisecond || actualBackoff > expectedBackoff+100*time.Millisecond {
t.Errorf("expected backoff ~%v, got %v", expectedBackoff, actualBackoff)
}
}
func TestInterfaceManager_AdaptiveBackoff_SecondFailure5s(t *testing.T) {
ifaces := []net.Interface{mockInterface(1, "eth0")}
mgr := NewInterfaceManager(ifaces, nil)
// First failure
mgr.MarkFailed(1, syscall.ENXIO)
// Manually re-add and fail again (simulating Sync + re-fail)
mgr.mu.Lock()
mgr.active[1] = "eth0"
mgr.mu.Unlock()
mgr.MarkFailed(1, syscall.ENXIO)
mgr.mu.RLock()
state := mgr.failures["eth0"]
mgr.mu.RUnlock()
if state.count != 2 {
t.Errorf("expected count 2, got %d", state.count)
}
expectedBackoff := backoffSecond
actualBackoff := time.Until(state.retryAt)
if actualBackoff < expectedBackoff-100*time.Millisecond || actualBackoff > expectedBackoff+100*time.Millisecond {
t.Errorf("expected backoff ~%v, got %v", expectedBackoff, actualBackoff)
}
}
func TestInterfaceManager_AdaptiveBackoff_ThirdFailure30s(t *testing.T) {
ifaces := []net.Interface{mockInterface(1, "eth0")}
mgr := NewInterfaceManager(ifaces, nil)
// Three failures
for i := 0; i < 3; i++ {
mgr.mu.Lock()
mgr.active[1] = "eth0"
mgr.mu.Unlock()
mgr.MarkFailed(1, syscall.ENXIO)
}
mgr.mu.RLock()
state := mgr.failures["eth0"]
mgr.mu.RUnlock()
if state.count != 3 {
t.Errorf("expected count 3, got %d", state.count)
}
expectedBackoff := backoffMax
actualBackoff := time.Until(state.retryAt)
if actualBackoff < expectedBackoff-100*time.Millisecond || actualBackoff > expectedBackoff+100*time.Millisecond {
t.Errorf("expected backoff ~%v, got %v", expectedBackoff, actualBackoff)
}
}
// ============================================================================
// Sync Tests
// ============================================================================
func TestInterfaceManager_Sync_DetectsDisappeared(t *testing.T) {
ifaces := []net.Interface{mockInterface(1, "eth0"), mockInterface(2, "wlan0")}
mgr := NewInterfaceManager(ifaces, nil)
// wlan0 disappeared
current := []net.Interface{mockInterface(1, "eth0")}
recovered := mgr.Sync(current)
// Nothing to recover (eth0 was already active)
if len(recovered) != 0 {
t.Errorf("expected 0 recovered, got %d", len(recovered))
}
// wlan0 should be removed
indices := mgr.ActiveIndices()
if len(indices) != 1 || indices[0] != 1 {
t.Errorf("expected [1], got %v", indices)
}
// wlan0 should have failure state
mgr.mu.RLock()
_, hasFailure := mgr.failures["wlan0"]
mgr.mu.RUnlock()
if !hasFailure {
t.Error("expected failure state for wlan0")
}
}
func TestInterfaceManager_Sync_RecoversAfterBackoff(t *testing.T) {
ifaces := []net.Interface{mockInterface(1, "eth0")}
mgr := NewInterfaceManager(ifaces, nil)
// Remove eth0 and set backoff in the past
mgr.mu.Lock()
delete(mgr.active, 1)
mgr.failures["eth0"] = &failureState{
count: 1,
retryAt: time.Now().Add(-1 * time.Second), // Backoff expired
}
mgr.mu.Unlock()
// eth0 reappears
current := []net.Interface{mockInterface(1, "eth0")}
recovered := mgr.Sync(current)
if len(recovered) != 1 {
t.Fatalf("expected 1 recovered, got %d", len(recovered))
}
if recovered[0].Name != "eth0" {
t.Errorf("expected eth0 to be recovered, got %s", recovered[0].Name)
}
}
func TestInterfaceManager_Sync_RespectsBackoffNotExpired(t *testing.T) {
ifaces := []net.Interface{mockInterface(1, "eth0")}
mgr := NewInterfaceManager(ifaces, nil)
// Remove eth0 and set backoff in the future
mgr.mu.Lock()
delete(mgr.active, 1)
mgr.failures["eth0"] = &failureState{
count: 1,
retryAt: time.Now().Add(10 * time.Second), // Backoff NOT expired
}
mgr.mu.Unlock()
current := []net.Interface{mockInterface(1, "eth0")}
recovered := mgr.Sync(current)
// Should NOT recover yet
if len(recovered) != 0 {
t.Errorf("expected 0 recovered (backoff not expired), got %d", len(recovered))
}
}
func TestInterfaceManager_Sync_RespectsExplicitMode(t *testing.T) {
ifaces := []net.Interface{mockInterface(1, "eth0")}
requested := []string{"eth0"} // Only eth0 allowed
mgr := NewInterfaceManager(ifaces, requested)
// New interface wlan0 appears (not in requested list)
current := []net.Interface{mockInterface(1, "eth0"), mockInterface(2, "wlan0")}
recovered := mgr.Sync(current)
// wlan0 should NOT be recovered (not in requested)
for _, iface := range recovered {
if iface.Name == "wlan0" {
t.Error("wlan0 should not be recovered in explicit mode")
}
}
// Only eth0 should be active
indices := mgr.ActiveIndices()
if len(indices) != 1 || indices[0] != 1 {
t.Errorf("expected [1], got %v", indices)
}
}
func TestInterfaceManager_Sync_AcceptsNewInDynamicMode(t *testing.T) {
ifaces := []net.Interface{mockInterface(1, "eth0")}
mgr := NewInterfaceManager(ifaces, nil) // nil = dynamic mode
// New interface wlan0 appears
current := []net.Interface{mockInterface(1, "eth0"), mockInterface(2, "wlan0")}
recovered := mgr.Sync(current)
// wlan0 should be in recovered list
found := false
for _, iface := range recovered {
if iface.Name == "wlan0" {
found = true
break
}
}
if !found {
t.Error("expected wlan0 to be in recovered list (dynamic mode)")
}
}
func TestInterfaceManager_Sync_DetectsIndexChange(t *testing.T) {
// eth0 starts with index 1
ifaces := []net.Interface{mockInterface(1, "eth0")}
mgr := NewInterfaceManager(ifaces, nil)
// eth0 reconnects with index 5 (different index, same name)
current := []net.Interface{mockInterface(5, "eth0")}
recovered := mgr.Sync(current)
// eth0 should be recovered with new index
if len(recovered) != 1 {
t.Fatalf("expected 1 recovered, got %d", len(recovered))
}
if recovered[0].Index != 5 || recovered[0].Name != "eth0" {
t.Errorf("expected {5, eth0}, got {%d, %s}", recovered[0].Index, recovered[0].Name)
}
// Old index 1 should be removed
mgr.mu.RLock()
_, hasOld := mgr.active[1]
mgr.mu.RUnlock()
if hasOld {
t.Error("old index 1 should be removed")
}
}
// ============================================================================
// Activate Tests
// ============================================================================
func TestInterfaceManager_Activate_AddsToActive(t *testing.T) {
mgr := NewInterfaceManager(nil, nil)
iface := mockInterface(3, "eth1")
mgr.Activate(iface)
indices := mgr.ActiveIndices()
if len(indices) != 1 || indices[0] != 3 {
t.Errorf("expected [3], got %v", indices)
}
}
func TestInterfaceManager_Activate_ClearsFailureHistory(t *testing.T) {
mgr := NewInterfaceManager(nil, nil)
// Set up failure state
mgr.mu.Lock()
mgr.failures["eth1"] = &failureState{count: 5, retryAt: time.Now().Add(time.Hour)}
mgr.mu.Unlock()
// Activate should clear it
iface := mockInterface(3, "eth1")
mgr.Activate(iface)
mgr.mu.RLock()
_, hasFailure := mgr.failures["eth1"]
mgr.mu.RUnlock()
if hasFailure {
t.Error("expected failure history to be cleared after Activate")
}
}
func TestInterfaceManager_Activate_HandlesIndexChange(t *testing.T) {
// Start with eth0 at index 1
ifaces := []net.Interface{mockInterface(1, "eth0")}
mgr := NewInterfaceManager(ifaces, nil)
// Activate eth0 with new index 5
mgr.Activate(mockInterface(5, "eth0"))
indices := mgr.ActiveIndices()
sort.Ints(indices)
// Should only have index 5, not both 1 and 5
if len(indices) != 1 || indices[0] != 5 {
t.Errorf("expected [5], got %v", indices)
}
}
// ============================================================================
// SetBackoff Tests
// ============================================================================
func TestInterfaceManager_SetBackoff_SetsFailureState(t *testing.T) {
mgr := NewInterfaceManager(nil, nil)
mgr.SetBackoff("eth0")
mgr.mu.RLock()
state := mgr.failures["eth0"]
mgr.mu.RUnlock()
if state == nil {
t.Fatal("expected failure state to be set")
}
if state.count != 1 {
t.Errorf("expected count 1, got %d", state.count)
}
}
// ============================================================================
// GetActiveInterfaces Tests
// ============================================================================
func TestInterfaceManager_GetActiveInterfaces_ReturnsInterfaces(t *testing.T) {
// This test requires actual system interfaces, so we'll just test the empty case
mgr := NewInterfaceManager(nil, nil)
ifaces := mgr.GetActiveInterfaces()
if ifaces == nil {
t.Error("expected empty slice, got nil")
}
if len(ifaces) != 0 {
t.Errorf("expected 0 interfaces, got %d", len(ifaces))
}
}
// ============================================================================
// Concurrency Tests
// ============================================================================
func TestInterfaceManager_Concurrent_ReadWrite(t *testing.T) {
ifaces := []net.Interface{mockInterface(1, "eth0"), mockInterface(2, "wlan0")}
mgr := NewInterfaceManager(ifaces, nil)
var wg sync.WaitGroup
stop := make(chan struct{})
// Reader goroutine
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-stop:
return
default:
_ = mgr.ActiveIndices()
}
}
}()
// Writer goroutine - MarkFailed
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < 100; i++ {
select {
case <-stop:
return
default:
mgr.MarkFailed(1, syscall.ENXIO)
}
}
}()
// Writer goroutine - Sync
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < 100; i++ {
select {
case <-stop:
return
default:
mgr.Sync(ifaces)
}
}
}()
// Writer goroutine - Activate
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < 100; i++ {
select {
case <-stop:
return
default:
mgr.Activate(mockInterface(3, "eth1"))
}
}
}()
// Let them run for a bit
time.Sleep(50 * time.Millisecond)
close(stop)
wg.Wait()
// If we get here without deadlock or panic, the test passes
}