-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathutils_test.go
654 lines (617 loc) · 23.2 KB
/
utils_test.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
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
package utils
import (
"errors"
"fmt"
"net"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/mock"
nl "github.com/vishvananda/netlink"
mocks "github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/utils/mocks"
)
func assertShouldFail(err error, shouldFail bool) {
if shouldFail {
Expect(err).To(HaveOccurred())
} else {
Expect(err).NotTo(HaveOccurred())
}
}
var _ = Describe("In the utils package", func() {
DescribeTable("getting PF PCI address",
func(fs *FakeFilesystem, addr string, expected string, shouldFail bool) {
defer fs.Use()()
actual, err := GetPfAddr(addr)
Expect(actual).To(Equal(expected))
assertShouldFail(err, shouldFail)
},
Entry("address of a PF is not passed when it's a PF", &FakeFilesystem{}, "0000:00:00.0", "", false),
Entry("physfn is not a symlink",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:00:00.0"},
Files: map[string][]byte{"sys/bus/pci/devices/0000:00:00.0/physfn": []byte("invalid content")},
},
"0000:00:00.0", "", true,
),
Entry("getting PF address of a VF",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:00:00.0", "sys/bus/pci/devices/0000:00:00.1"},
Symlinks: map[string]string{"sys/bus/pci/devices/0000:00:00.1/physfn": "../0000:00:00.0"},
},
"0000:00:00.1", "0000:00:00.0", false,
),
)
DescribeTable("checking whether device is a SRIOV PF",
func(fs *FakeFilesystem, addr string, expected bool) {
defer fs.Use()()
actual := IsSriovPF(addr)
Expect(actual).To(Equal(expected))
},
Entry("sriov_totalvfs file exists",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:00:00.0"},
Files: map[string][]byte{"sys/bus/pci/devices/0000:00:00.0/sriov_totalvfs": []byte("0")},
},
"0000:00:00.0", true,
),
Entry("sriov_totalvfs file doesn't exist",
&FakeFilesystem{Dirs: []string{"sys/bus/pci/devices/0000:00:00.0"}}, "0000:00:00.0", false,
),
)
DescribeTable("checking whether device is a SRIOV VF",
func(fs *FakeFilesystem, addr string, expected bool) {
defer fs.Use()()
actual := IsSriovVF(addr)
Expect(actual).To(Equal(expected))
},
Entry("physfn file exists and is a symlink to PF",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:00:00.0", "sys/bus/pci/devices/0000:00:00.1"},
Symlinks: map[string]string{"sys/bus/pci/devices/0000:00:00.1/physfn": "../0000:00:00.0"},
},
"0000:00:00.1", true,
),
Entry("physfn file doesn't exist",
&FakeFilesystem{Dirs: []string{"sys/bus/pci/devices/0000:00:00.1"}}, "0000:00:00.1", false,
),
)
DescribeTable("getting number of configured VFs",
func(fs *FakeFilesystem, pf string, expected int) {
defer fs.Use()()
Expect(GetVFconfigured(pf)).To(Equal(expected))
},
Entry("reading the VF path fails", &FakeFilesystem{}, "0000:00:00.0", 0),
Entry("converting the VFs number to integer fails",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:00:00.1"},
Files: map[string][]byte{"sys/bus/pci/devices/0000:00:00.1/sriov_numvfs": []byte("invalid content")},
},
"0000:00:00.1", 0,
),
Entry("finding positive number of VFs",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:00:00.1"},
Files: map[string][]byte{"sys/bus/pci/devices/0000:00:00.1/sriov_numvfs": []byte("32")},
},
"0000:00:00.1", 32,
),
)
DescribeTable("getting VFs list",
func(fs *FakeFilesystem, pf string, expected []string, shouldFail bool) {
defer fs.Use()()
vfList, err := GetVFList(pf)
assertShouldFail(err, shouldFail)
Expect(vfList).To(Equal(expected))
},
Entry("reading the PF path fails", &FakeFilesystem{}, "0000:00:00.1", []string{}, true),
Entry("VF list is correctly returned",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:01:00.0", "sys/bus/pci/devices/0000:01:10.0"},
Symlinks: map[string]string{"sys/bus/pci/devices/0000:01:00.0/virtfn0": "../0000:01:10.0"},
},
"0000:01:00.0", []string{"0000:01:10.0"}, false,
),
Entry("empty VFs list is returned",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:00:00.3"},
},
"0000:00:00.3", []string{}, false,
),
)
DescribeTable("getting PCI address from VFID",
func(fs *FakeFilesystem, pf string, vf int, expected string, shouldFail bool) {
defer fs.Use()()
pciAddr, err := GetPciAddrFromVFID(pf, vf)
assertShouldFail(err, shouldFail)
Expect(pciAddr).To(Equal(expected))
},
Entry(
"PCI address is successfully returned",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:01:00.0", "sys/bus/pci/devices/0000:01:10.0"},
Symlinks: map[string]string{"sys/bus/pci/devices/0000:01:00.0/virtfn0": "../0000:01:10.0"},
},
"0000:01:00.0", 0, "0000:01:10.0", false,
),
Entry("could not get directory information for the PF", &FakeFilesystem{}, "0000:01:00.0", 0, "", true),
Entry("there's no symbolic link between virtual function and PCI",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:01:00.0"},
Files: map[string][]byte{"sys/bus/pci/devices/0000:01:00.0/virtfn0": []byte("junk")},
},
"0000:01:00.0", 0, "", true,
),
)
DescribeTable("getting SR-IOV VFs capacity for the PF",
func(fs *FakeFilesystem, pf string, expected int) {
defer fs.Use()()
Expect(GetSriovVFcapacity(pf)).To(Equal(expected))
},
Entry("positive number of total VFs is returned",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:01:00.0"},
Files: map[string][]byte{"sys/bus/pci/devices/0000:01:00.0/sriov_totalvfs": []byte("32")},
},
"0000:01:00.0", 32,
),
Entry("total vfs file doesn't exist",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:01:00.0"},
},
"0000:01:00.0", 0,
),
Entry("cannot convert junk from totalvfs to int",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:01:00.0"},
Files: map[string][]byte{"sys/bus/pci/devices/0000:01:00.0/sriov_totalvfs": []byte("junk")},
},
"0000:01:00.0", 0,
),
)
DescribeTable("getting NUMA node of device",
func(fs *FakeFilesystem, pciAddr string, expected int) {
defer fs.Use()()
Expect(GetDevNode(pciAddr)).To(Equal(expected))
},
Entry("reading the device path fails", &FakeFilesystem{}, "0000:00:00.0", -1),
Entry("converting the NUMA node to integer fails",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:00:00.1"},
Files: map[string][]byte{"sys/bus/pci/devices/0000:00:00.1/numa_node": []byte("invalid content")},
},
"0000:00:00.1", -1,
),
Entry("finding positive NUMA node",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:00:00.1"},
Files: map[string][]byte{"sys/bus/pci/devices/0000:00:00.1/numa_node": []byte("1")},
},
"0000:00:00.1", 1,
),
Entry("finding zero NUMA node",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:00:00.1"},
Files: map[string][]byte{"sys/bus/pci/devices/0000:00:00.1/numa_node": []byte("0")},
},
"0000:00:00.1", 0,
),
Entry("finding negative NUMA node",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:00:00.1"},
Files: map[string][]byte{"sys/bus/pci/devices/0000:00:00.1/numa_node": []byte("-1")},
},
"0000:00:00.1", -1,
),
)
DescribeTable("checking that device status is up",
func(fs *FakeFilesystem, dev string, expected bool) {
defer fs.Use()()
Expect(IsNetlinkStatusUp(dev)).To(Equal(expected))
},
Entry("all devices operstates are up should return true",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:01:00.0/net/eth0", "sys/bus/pci/devices/0000:01:00.0/net/eth1"},
Files: map[string][]byte{
"sys/bus/pci/devices/0000:01:00.0/net/eth0/operstate": []byte("up"),
"sys/bus/pci/devices/0000:01:00.0/net/eth1/operstate": []byte("up"),
},
},
"0000:01:00.0", true,
),
Entry("at least one device operstate is down should return false",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:01:00.0/net/eth0", "sys/bus/pci/devices/0000:01:00.0/net/eth1"},
Files: map[string][]byte{
"sys/bus/pci/devices/0000:01:00.0/net/eth0/operstate": []byte("up"),
"sys/bus/pci/devices/0000:01:00.0/net/eth1/operstate": []byte("down"),
},
},
"0000:01:00.0", false,
),
PEntry("when operstate file doesn't exist should return false",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:01:00.0"},
},
"0000:01:00.0", false,
),
)
DescribeTable("checking that device PCI address is valid and device exists",
func(fs *FakeFilesystem, addr, expected string, shouldFail bool) {
defer fs.Use()()
actual, err := ValidPciAddr(addr)
Expect(actual).To(Equal(expected))
assertShouldFail(err, shouldFail)
},
Entry("long id is submitted and device exists",
&FakeFilesystem{Dirs: []string{"sys/bus/pci/devices/0000:01:00.0"}},
"0000:01:00.0", "0000:01:00.0", false,
),
Entry("short id is submitted and device exists",
&FakeFilesystem{Dirs: []string{"sys/bus/pci/devices/0000:01:00.0"}},
"01:00.0", "0000:01:00.0", false,
),
Entry("valid long id is submitted, but device doesn't exist",
&FakeFilesystem{},
"0000:01:00.0", "0000:01:00.0", true,
),
Entry("valid short id is submitted, but device doesn't exist",
&FakeFilesystem{},
"01:00.0", "0000:01:00.0", true,
),
Entry("invalid id is submitted",
&FakeFilesystem{},
"junk", "", true,
),
)
DescribeTable("checking whether SR-IOV is configured",
func(fs *FakeFilesystem, addr string, expected bool) {
defer fs.Use()()
Expect(SriovConfigured(addr)).To(Equal(expected))
},
Entry("SR-IOV is not configured", &FakeFilesystem{}, "0000:01:00.0", false),
Entry("SR-IOV is configured",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:01:00.0"},
Files: map[string][]byte{"sys/bus/pci/devices/0000:01:00.0/sriov_numvfs": []byte("32")},
},
"0000:01:00.0", true,
),
)
DescribeTable("validating resource name",
func(name string, expected bool) {
Expect(ValidResourceName(name)).To(Equal(expected))
},
Entry("resource name is valid", "sriov-net_0", true),
Entry("resource name is invalid", "junk.net.0", false),
)
DescribeTable("getting VFIO device file",
func(fs *FakeFilesystem, device, expected string, shouldFail bool) {
defer fs.Use()()
//TODO: adapt test to running in a virtual environment
actualHost, _, err := GetVFIODeviceFile(device)
Expect(actualHost).To(Equal(expected))
assertShouldFail(err, shouldFail)
},
Entry("could not get directory information for device",
&FakeFilesystem{},
"0000:01:00.0", "", true,
),
Entry("finding iommuDir file fails",
&FakeFilesystem{Dirs: []string{"sys/bus/pci/devices/0000:01:10.0"}},
"0000:01:10.0", "", true,
),
Entry("symlink to iommu group is invalid (normal dir instead of symlink)",
&FakeFilesystem{Dirs: []string{"sys/bus/pci/devices/0000:01:10.0/iommu_group"}},
"0000:01:10.0", "", true,
),
Entry("vfio device file path is returned",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:01:10.0", "sys/kernel/iommu_groups/1"},
Symlinks: map[string]string{"sys/bus/pci/devices/0000:01:10.0/iommu_group": "../../../../kernel/iommu_groups/1"},
},
"0000:01:10.0", "/dev/vfio/1", false,
),
)
DescribeTable("getting UIO device file",
func(fs *FakeFilesystem, device, expected string, shouldFail bool) {
defer fs.Use()()
actual, err := GetUIODeviceFile(device)
Expect(actual).To(Equal(expected))
assertShouldFail(err, shouldFail)
},
Entry("could not get directory information for device",
&FakeFilesystem{},
"0000:01:10.0", "", true,
),
Entry("uio is not a dir",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:01:10.0"},
Files: map[string][]byte{"sys/bus/pci/devices/0000:01:10.0/uio": []byte("junk")},
},
"0000:01:10.0", "", true,
),
Entry("uio device file path is returned",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:01:10.0/uio/uio1"},
},
"0000:01:10.0", "/dev/uio1", false,
),
)
DescribeTable("getting driver name",
func(fs *FakeFilesystem, device, expected string, shouldFail bool) {
defer fs.Use()()
actual, err := GetDriverName(device)
Expect(actual).To(Equal(expected))
assertShouldFail(err, shouldFail)
},
Entry("driver link doesn't exist",
&FakeFilesystem{},
"0000:01:10.0", "", true,
),
Entry("correct driver name is returned",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:01:10.0/", "sys/bus/pci/drivers/fake"},
Symlinks: map[string]string{"sys/bus/pci/devices/0000:01:10.0/driver": "../../../../bus/pci/drivers/fake"},
},
"0000:01:10.0", "fake", false,
),
)
DescribeTable("getting interface names",
func(fs *FakeFilesystem, device string, expected []string, shouldFail bool) {
defer fs.Use()()
actual, err := GetNetNames(device)
Expect(actual).To(ConsistOf(expected))
assertShouldFail(err, shouldFail)
},
Entry("device doesn't exist", &FakeFilesystem{}, "0000:01:10.0", nil, true),
Entry("net is not a directory",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:01:10.0"},
Files: map[string][]byte{"sys/bus/pci/devices/0000:01:10.0/net": []byte("junk")},
},
"0000:01:10.0", nil, true,
),
Entry("single network interface",
&FakeFilesystem{Dirs: []string{"sys/bus/pci/devices/0000:01:10.0/net/fake0"}},
"0000:01:10.0", []string{"fake0"}, false,
),
Entry("multiple network interfaces for single device",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:01:10.0/net/fake0", "sys/bus/pci/devices/0000:01:10.0/net/fake1"},
},
"0000:01:10.0", []string{"fake0", "fake1"}, false,
),
)
DescribeTable("getting PF names legacy",
func(fs *FakeFilesystem, device string, expected string, shouldFail bool) {
fakeNetlinkProvider := mocks.NetlinkProvider{}
fakeNetlinkProvider.
On("GetDevLinkDeviceEswitchAttrs",
mock.AnythingOfType("string")).
Return(&nl.DevlinkDevEswitchAttr{Mode: "legacy"}, nil)
SetNetlinkProviderInst(&fakeNetlinkProvider)
defer fs.Use()()
actual, err := GetPfName(device)
Expect(actual).To(Equal(expected))
assertShouldFail(err, shouldFail)
},
Entry("device doesn't exist", &FakeFilesystem{}, "0000:01:10.0", "", false),
Entry("device is a VF and interface name exists",
&FakeFilesystem{
Dirs: []string{
"sys/bus/pci/devices/0000:01:10.0",
"sys/bus/pci/devices/0000:01:00.0/net/fakePF",
},
Symlinks: map[string]string{
"sys/bus/pci/devices/0000:01:10.0/physfn/": "../0000:01:00.0",
},
}, "0000:01:10.0", "fakePF", false,
),
Entry("device is a VF and interface name does not exist",
&FakeFilesystem{Dirs: []string{"sys/bus/pci/devices/0000:01:10.0/physfn/net/"}},
"0000:01:10.0", "", true,
),
Entry("pf net is not a directory at all",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:01:10.0/physfn"},
Files: map[string][]byte{"sys/bus/pci/devices/0000:01:10.0/physfn/net/": []byte("junk")},
},
"0000:01:10.0", "", true,
),
)
DescribeTable("getting PF names switchdev",
func(fs *FakeFilesystem, device string, expected string, shouldFail bool) {
fakeNetlinkProvider := mocks.NetlinkProvider{}
fakeNetlinkProvider.
On("GetDevLinkDeviceEswitchAttrs", "devlinkDeviceSwitchdev").
Return(&nl.DevlinkDevEswitchAttr{Mode: "switchdev"}, nil).
On("GetDevLinkDeviceEswitchAttrs", "nonDevlinkDevice").
Return(nil, fmt.Errorf("error getting devlink device attributes for net device")).
On("GetDevLinkDeviceEswitchAttrs", "nonSriovDevice").
Return(&nl.DevlinkDevEswitchAttr{Mode: ""}, nil).
On("GetDevLinkDeviceEswitchAttrs", "unknownDevice").
Return(nil, fmt.Errorf("unknown error"))
SetNetlinkProviderInst(&fakeNetlinkProvider)
fakeSriovnetProvider := mocks.SriovnetProvider{}
fakeSriovnetProvider.
On("GetUplinkRepresentor", mock.AnythingOfType("string")).
Return("fakeSwitchdevPF", nil)
SetSriovnetProviderInst(&fakeSriovnetProvider)
defer fs.Use()()
actual, err := GetPfName(device)
Expect(actual).To(Equal(expected))
assertShouldFail(err, shouldFail)
},
Entry("device is a VF and PF is in switchdev mode",
&FakeFilesystem{
Dirs: []string{
"sys/bus/pci/devices/0000:01:10.0",
"sys/bus/pci/devices/devlinkDeviceSwitchdev/net/fakePF",
"sys/bus/pci/devices/devlinkDeviceSwitchdev/net/fakeVF",
},
Symlinks: map[string]string{
"sys/bus/pci/devices/0000:01:10.0/physfn/": "../devlinkDeviceSwitchdev",
},
},
"0000:01:10.0", "fakeSwitchdevPF", false,
),
)
DescribeTable("getting ID of VF",
func(fs *FakeFilesystem, device string, expected int, shouldFail bool) {
defer fs.Use()()
actual, err := GetVFID(device)
Expect(actual).To(Equal(expected))
assertShouldFail(err, shouldFail)
},
Entry("device doesn't exist",
&FakeFilesystem{},
"0000:01:10.0", -1, false),
Entry("device has no link to PF",
&FakeFilesystem{Dirs: []string{"sys/bus/pci/devices/0000:01:10.0"}},
"0000:01:10.0", -1, false),
Entry("PF has no VF links",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:01:10.0/", "sys/bus/pci/devices/0000:01:00.0/"},
Symlinks: map[string]string{"sys/bus/pci/devices/0000:01:10.0/physfn": "../0000:01:00.0"},
},
"0000:01:10.0", -1, false),
Entry("VF not found in PF",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:01:10.0/", "sys/bus/pci/devices/0000:01:00.0/"},
Symlinks: map[string]string{"sys/bus/pci/devices/0000:01:10.0/physfn": "../0000:01:00.0",
"sys/bus/pci/devices/0000:01:00.0/virtfn0": "../0000:01:08.0",
},
},
"0000:01:10.0", -1, false),
Entry("VF found in PF",
&FakeFilesystem{
Dirs: []string{"sys/bus/pci/devices/0000:01:10.0/", "sys/bus/pci/devices/0000:01:00.0/"},
Symlinks: map[string]string{"sys/bus/pci/devices/0000:01:10.0/physfn": "../0000:01:00.0",
"sys/bus/pci/devices/0000:01:00.0/virtfn0": "../0000:01:08.0",
"sys/bus/pci/devices/0000:01:00.0/virtfn1": "../0000:01:09.0",
"sys/bus/pci/devices/0000:01:00.0/virtfn2": "../0000:01:10.0",
},
},
"0000:01:10.0", 2, false),
)
DescribeTable("checking if device has default route",
func(fs *FakeFilesystem, device string, nlRet []nl.Route, nlErr error, expected, shouldFail bool) {
defer fs.Use()()
fakeNetlinkProvider := mocks.NetlinkProvider{}
fakeNetlinkProvider.
On("GetIPv4RouteList", mock.AnythingOfType("string")).
Return(nlRet, nlErr)
SetNetlinkProviderInst(&fakeNetlinkProvider)
actual, err := HasDefaultRoute(device)
Expect(actual).To(Equal(expected))
assertShouldFail(err, shouldFail)
},
Entry("device doesn't exist", &FakeFilesystem{}, "0000:00:00.0", []nl.Route{}, nil, false, true),
Entry("no network interface",
&FakeFilesystem{Dirs: []string{"sys/bus/pci/devices/0000:01:10.0/net"}},
"0000:01:10.0", []nl.Route{}, nil, false, false,
),
Entry("single network interface with no routes",
&FakeFilesystem{Dirs: []string{"sys/bus/pci/devices/0000:01:10.0/net/fake0"}},
"0000:01:10.0", []nl.Route{}, nil, false, false,
),
Entry("multiple network interfaces for single device with no routes",
&FakeFilesystem{
Dirs: []string{
"sys/bus/pci/devices/0000:01:10.0/net/fake0",
"sys/bus/pci/devices/0000:01:10.0/net/fake1",
},
},
"0000:01:10.0", []nl.Route{}, nil, false, false,
),
Entry("single network interface with non-default routes",
&FakeFilesystem{Dirs: []string{"sys/bus/pci/devices/0000:01:10.0/net/fake0"}},
"0000:01:10.0", []nl.Route{{Dst: &net.IPNet{}}, {Dst: &net.IPNet{}}}, nil, false, false,
),
Entry("single network interface with default route",
&FakeFilesystem{Dirs: []string{"sys/bus/pci/devices/0000:01:10.0/net/fake0"}},
"0000:01:10.0", []nl.Route{{Dst: nil}}, nil, true, false,
),
)
DescribeTable("checking vendor name normalization",
func(vendorName, expected string) {
actual := NormalizeVendorName(vendorName)
Expect(actual).To(Equal(expected))
},
Entry("short vendor name", "short_vendor_name", "short_vendor_name"),
Entry("very long vendor name", "veeery_looong_veendor_name",
"veeery_looong_vee..."),
)
DescribeTable("checking product name normalization",
func(productName, expected string) {
actual := NormalizeProductName(productName)
Expect(actual).To(Equal(expected))
},
Entry("short product name", "short_product_name", "short_product_name"),
Entry("very long product name", "veeeeeeery_loooooooong_prooooooooduct_naaaaaaaaaame",
"veeeeeeery_loooooooong_prooooooooduct..."),
)
DescribeTable("checking parsing device ID",
func(deviceID string, expected int, shouldFail bool) {
actual, err := ParseDeviceID(deviceID)
Expect(actual).To(Equal(int64(expected)))
assertShouldFail(err, shouldFail)
},
Entry("parsing correct ID string", "c0fe", 0xc0fe, false),
Entry("parsing empty ID string", "", 0, true),
Entry("parsing incorrect ID string", "not_a_number", 0, true),
)
DescribeTable("checking parsing auxiliary device type",
func(deviceID string, expected string) {
actual := ParseAuxDeviceType(deviceID)
Expect(actual).To(Equal(expected))
},
Entry("wrong deviceID string layout (PCI device string)", "0000:12:34.0", ""),
Entry("wrong deviceID string layout (id not a number)", "driver_name.type.id", ""),
Entry("wrong deviceID string layout (negative id number)", "driver_name.type.-4", ""),
Entry("valid deviceID string", "driver_name.type.123", "type"),
)
Context("GetPfNameFromAuxDev", func() {
var (
mockSriovnet *mocks.SriovnetProvider
)
BeforeEach(func() {
mockSriovnet = mocks.NewSriovnetProvider(GinkgoT())
SetSriovnetProviderInst(mockSriovnet)
})
It("Returns uplink representor netdev name if found", func() {
mockSriovnet.On("GetUplinkRepresentorFromAux", mock.AnythingOfType("string")).Return("enp3s0f0np0", nil)
pfName, err := GetPfNameFromAuxDev("mlx5_core.sf.4")
Expect(err).ToNot(HaveOccurred())
Expect(pfName).To(Equal("enp3s0f0np0"))
})
It("Returns PF netdev name from sysfs if uplink representor not found", func() {
mockSriovnet.On("GetUplinkRepresentorFromAux", mock.AnythingOfType("string")).Return("", errors.New("uplink not found"))
mockSriovnet.On("GetPfPciFromAux", mock.AnythingOfType("string")).Return("0000:03:00.0", nil)
fakeFs := &FakeFilesystem{
Dirs: []string{"/sys/bus/pci/devices/0000:03:00.0/net"},
Files: map[string][]byte{"/sys/bus/pci/devices/0000:03:00.0/net/enp3s0f0np0": nil},
}
defer fakeFs.Use()()
pfName, err := GetPfNameFromAuxDev("mlx5_core.sf.4")
Expect(err).ToNot(HaveOccurred())
Expect(pfName).To(Equal("enp3s0f0np0"))
})
It("Fails if no uplink representor or PF netdev found", func() {
mockSriovnet.On("GetUplinkRepresentorFromAux", mock.AnythingOfType("string")).Return("", errors.New("uplink not found"))
mockSriovnet.On("GetPfPciFromAux", mock.AnythingOfType("string")).Return("0000:03:00.0", nil)
fakeFs := &FakeFilesystem{
Dirs: []string{"/sys/bus/pci/devices/0000:03:00.0"},
}
defer fakeFs.Use()()
_, err := GetPfNameFromAuxDev("mlx5_core.sf.4")
Expect(err).To(HaveOccurred())
})
It("Fails if PF PCI not found", func() {
mockSriovnet.On("GetUplinkRepresentorFromAux", mock.AnythingOfType("string")).Return("", errors.New("uplink not found"))
mockSriovnet.On("GetPfPciFromAux", mock.AnythingOfType("string")).Return("", errors.New("PF PCI address not found"))
_, err := GetPfNameFromAuxDev("mlx5_core.sf.4")
Expect(err).To(HaveOccurred())
})
})
})