-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_network_collector.py
More file actions
1943 lines (1575 loc) · 68 KB
/
test_network_collector.py
File metadata and controls
1943 lines (1575 loc) · 68 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
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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###############################################################################
#
# MIT License
#
# Copyright (c) 2025 Advanced Micro Devices, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
###############################################################################
from unittest.mock import MagicMock
import pytest
from nodescraper.enums.executionstatus import ExecutionStatus
from nodescraper.enums.systeminteraction import SystemInteractionLevel
from nodescraper.models.systeminfo import OSFamily
from nodescraper.plugins.inband.network.network_collector import NetworkCollector
from nodescraper.plugins.inband.network.networkdata import (
BroadcomNicDevice,
BroadcomNicQos,
EthtoolInfo,
IpAddress,
Neighbor,
NetworkDataModel,
NetworkInterface,
PensandoNicCard,
PensandoNicDcqcn,
PensandoNicEnvironment,
PensandoNicPcieAts,
PensandoNicPort,
PensandoNicQos,
PensandoNicQosScheduling,
Route,
RoutingRule,
)
@pytest.fixture
def collector(system_info, conn_mock):
return NetworkCollector(
system_info=system_info,
system_interaction_level=SystemInteractionLevel.PASSIVE,
connection=conn_mock,
)
# Sample command outputs for testing (mock data)
IP_ADDR_OUTPUT = """1: lo: <LOOPBACK,UP,LOWER_UP> mtu 12345 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 5678 qdisc mq state UP group default qlen 1000
link/ether aa:bb:cc:dd:ee:ff brd ff:ff:ff:ff:ff:ff
inet 1.123.123.100/24 brd 1.123.123.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::aabb:ccff/64 scope link
valid_lft forever preferred_lft forever"""
IP_ROUTE_OUTPUT = """default via 2.123.123.1 dev eth0 proto static metric 100
2.123.123.0/24 dev eth0 proto kernel scope link src 2.123.123.100 metric 100
7.8.0.0/16 dev docker0 proto kernel scope link src 7.8.0.1 linkdown"""
IP_RULE_OUTPUT = """0: from all lookup local
89145: from all lookup main
56789: from all lookup default"""
IP_NEIGHBOR_OUTPUT = """50.50.1.50 dev eth0 lladdr 11:22:33:44:55:66 STALE
50.50.1.1 dev eth0 lladdr 99:88:77:66:55:44 REACHABLE"""
ETHTOOL_OUTPUT = """Settings for ethmock123:
Supported ports: [ TP ]
Supported link modes: 10mockbaseT/Half
123mockbaseT/Half
1234mockbaseT/Full
Supported pause frame use: Symmetric
Supports auto-negotiation: Yes
Supported FEC modes: Not reported
Advertised link modes: 10mockbaseT/Half 10mockbaseT/Full
167mockbaseT/Half 167mockbaseT/Full
1345mockbaseT/Full
Advertised pause frame use: Symmetric
Advertised auto-negotiation: Yes
Advertised FEC modes: Xyz ABCfec
Speed: 1000mockMb/s
Duplex: Full
Port: MockedTwisted Pair
PHYAD: 1
Transceiver: internal
Auto-negotiation: on
MDI-X: on (auto)
Supports Wake-on: qwerty
Wake-on: g
Current message level: 0x123123
Link detected: yes"""
ETHTOOL_NO_LINK_OUTPUT = """Settings for ethmock1:
Supported ports: [ FIBRE ]
Supported link modes: 11122mockbaseT/Full
Speed: Unknown!
Duplex: Unknown!
Port: FIBRE
Auto-negotiation: off
Link detected: no"""
def test_parse_ip_addr_loopback(collector):
"""Test parsing loopback interface from ip addr output"""
interfaces = collector._parse_ip_addr(IP_ADDR_OUTPUT)
# Find loopback interface
lo = next((i for i in interfaces if i.name == "lo"), None)
assert lo is not None
assert lo.index == 1
assert lo.state == "UNKNOWN"
assert lo.mtu == 12345
assert lo.qdisc == "noqueue"
assert lo.mac_address == "00:00:00:00:00:00"
assert "LOOPBACK" in lo.flags
assert "UP" in lo.flags
# Check addresses
assert len(lo.addresses) == 2
ipv4 = next((a for a in lo.addresses if a.family == "inet"), None)
assert ipv4 is not None
assert ipv4.address == "127.0.0.1"
assert ipv4.prefix_len == 8
assert ipv4.scope == "host"
def test_parse_ip_addr_ethernet(collector):
"""Test parsing ethernet interface from ip addr output"""
interfaces = collector._parse_ip_addr(IP_ADDR_OUTPUT)
# Find ethernet interface
eth = next((i for i in interfaces if i.name == "eth0"), None)
assert eth is not None
assert eth.index == 2
assert eth.state == "UP"
assert eth.mtu == 5678
assert eth.qdisc == "mq"
assert eth.mac_address == "aa:bb:cc:dd:ee:ff"
assert "BROADCAST" in eth.flags
assert "MULTICAST" in eth.flags
# Check IPv4 address
ipv4 = next((a for a in eth.addresses if a.family == "inet"), None)
assert ipv4 is not None
assert ipv4.address == "1.123.123.100"
assert ipv4.prefix_len == 24
assert ipv4.broadcast == "1.123.123.255"
assert ipv4.scope == "global"
def test_parse_ip_route_default(collector):
"""Test parsing default route"""
routes = collector._parse_ip_route(IP_ROUTE_OUTPUT)
# Find default route
default_route = next((r for r in routes if r.destination == "default"), None)
assert default_route is not None
assert default_route.gateway == "2.123.123.1"
assert default_route.device == "eth0"
assert default_route.protocol == "static"
assert default_route.metric == 100
def test_parse_ip_route_network(collector):
"""Test parsing network route with source"""
routes = collector._parse_ip_route(IP_ROUTE_OUTPUT)
# Find network route
net_route = next((r for r in routes if r.destination == "2.123.123.0/24"), None)
assert net_route is not None
assert net_route.gateway is None # Direct route, no gateway
assert net_route.device == "eth0"
assert net_route.protocol == "kernel"
assert net_route.scope == "link"
assert net_route.source == "2.123.123.100"
assert net_route.metric == 100
def test_parse_ip_route_docker(collector):
"""Test parsing docker bridge route"""
routes = collector._parse_ip_route(IP_ROUTE_OUTPUT)
# Find docker route
docker_route = next((r for r in routes if r.destination == "7.8.0.0/16"), None)
assert docker_route is not None
assert docker_route.gateway is None
assert docker_route.device == "docker0"
assert docker_route.protocol == "kernel"
assert docker_route.scope == "link"
assert docker_route.source == "7.8.0.1"
def test_parse_ip_rule_basic(collector):
"""Test parsing routing rules"""
rules = collector._parse_ip_rule(IP_RULE_OUTPUT)
assert len(rules) == 3
# Check local rule
local_rule = next((r for r in rules if r.priority == 0), None)
assert local_rule is not None
assert local_rule.source is None # "from all"
assert local_rule.destination is None
assert local_rule.table == "local"
assert local_rule.action == "lookup"
# Check main rule
main_rule = next((r for r in rules if r.priority == 89145), None)
assert main_rule is not None
assert main_rule.table == "main"
# Check default rule
default_rule = next((r for r in rules if r.priority == 56789), None)
assert default_rule is not None
assert default_rule.table == "default"
def test_parse_ip_rule_complex(collector):
"""Test parsing complex routing rule with all fields"""
complex_rule_output = (
"100: from 192.168.1.0/24 to 10.0.0.0/8 iif eth0 oif eth1 fwmark 0x10 lookup custom_table"
)
rules = collector._parse_ip_rule(complex_rule_output)
assert len(rules) == 1
rule = rules[0]
assert rule.priority == 100
assert rule.source == "192.168.1.0/24"
assert rule.destination == "10.0.0.0/8"
assert rule.iif == "eth0"
assert rule.oif == "eth1"
assert rule.fwmark == "0x10"
assert rule.table == "custom_table"
assert rule.action == "lookup"
def test_parse_ip_neighbor_reachable(collector):
"""Test parsing neighbor entries"""
neighbors = collector._parse_ip_neighbor(IP_NEIGHBOR_OUTPUT)
# Check REACHABLE neighbor
reachable = next((n for n in neighbors if n.state == "REACHABLE"), None)
assert reachable is not None
assert reachable.ip_address == "50.50.1.1"
assert reachable.device == "eth0"
assert reachable.mac_address == "99:88:77:66:55:44"
assert reachable.state == "REACHABLE"
def test_parse_ip_neighbor_stale(collector):
"""Test parsing STALE neighbor entry"""
neighbors = collector._parse_ip_neighbor(IP_NEIGHBOR_OUTPUT)
# Check STALE neighbor
stale = next((n for n in neighbors if n.state == "STALE"), None)
assert stale is not None
assert stale.ip_address == "50.50.1.50"
assert stale.device == "eth0"
assert stale.mac_address == "11:22:33:44:55:66"
assert stale.state == "STALE"
def test_parse_ip_neighbor_with_flags(collector):
"""Test parsing neighbor with flags"""
neighbor_with_flags = "10.0.0.1 dev eth0 lladdr aa:bb:cc:dd:ee:ff REACHABLE router proxy"
neighbors = collector._parse_ip_neighbor(neighbor_with_flags)
assert len(neighbors) == 1
neighbor = neighbors[0]
assert neighbor.ip_address == "10.0.0.1"
assert neighbor.mac_address == "aa:bb:cc:dd:ee:ff"
assert neighbor.state == "REACHABLE"
assert "router" in neighbor.flags
assert "proxy" in neighbor.flags
def test_collect_data_success(collector, conn_mock):
"""Test successful collection of all network data"""
collector.system_info.os_family = OSFamily.LINUX
# Mock successful command execution
def run_sut_cmd_side_effect(cmd, **kwargs):
if "addr show" in cmd:
return MagicMock(exit_code=0, stdout=IP_ADDR_OUTPUT, command=cmd)
elif "route show" in cmd:
return MagicMock(exit_code=0, stdout=IP_ROUTE_OUTPUT, command=cmd)
elif "rule show" in cmd:
return MagicMock(exit_code=0, stdout=IP_RULE_OUTPUT, command=cmd)
elif "neighbor show" in cmd:
return MagicMock(exit_code=0, stdout=IP_NEIGHBOR_OUTPUT, command=cmd)
elif "ethtool" in cmd:
# Fail ethtool commands (simulating no sudo or not supported)
return MagicMock(exit_code=1, stdout="", command=cmd)
elif "lldpcli" in cmd or "lldpctl" in cmd:
# LLDP commands fail (not available)
return MagicMock(exit_code=1, stdout="", command=cmd)
elif "niccli" in cmd:
# Broadcom NIC commands fail (not available)
return MagicMock(exit_code=1, stdout="", command=cmd)
elif "nicctl" in cmd:
# Pensando NIC commands fail (not available)
return MagicMock(exit_code=1, stdout="", command=cmd)
return MagicMock(exit_code=1, stdout="", command=cmd)
collector._run_sut_cmd = MagicMock(side_effect=run_sut_cmd_side_effect)
result, data = collector.collect_data()
assert result.status == ExecutionStatus.OK
assert data is not None
assert isinstance(data, NetworkDataModel)
assert len(data.interfaces) == 2
assert len(data.routes) == 3
assert len(data.rules) == 3
assert len(data.neighbors) == 2
# Since nicctl commands fail in this test, we expect the failure message
assert "Network data collection failed" in result.message
def test_collect_data_addr_failure(collector, conn_mock):
"""Test collection when ip addr command fails"""
collector.system_info.os_family = OSFamily.LINUX
# Mock failed addr command but successful others
def run_sut_cmd_side_effect(cmd, **kwargs):
if "addr show" in cmd:
return MagicMock(exit_code=1, command=cmd)
elif "route show" in cmd:
return MagicMock(exit_code=0, stdout=IP_ROUTE_OUTPUT, command=cmd)
elif "rule show" in cmd:
return MagicMock(exit_code=0, stdout=IP_RULE_OUTPUT, command=cmd)
elif "neighbor show" in cmd:
return MagicMock(exit_code=0, stdout=IP_NEIGHBOR_OUTPUT, command=cmd)
elif "ethtool" in cmd:
return MagicMock(exit_code=1, command=cmd)
elif "lldpcli" in cmd or "lldpctl" in cmd:
# LLDP commands fail (not available)
return MagicMock(exit_code=1, command=cmd)
elif "niccli" in cmd:
# Broadcom NIC commands fail (not available)
return MagicMock(exit_code=1, command=cmd)
elif "nicctl" in cmd:
# Pensando NIC commands fail (not available)
return MagicMock(exit_code=1, command=cmd)
return MagicMock(exit_code=1, command=cmd)
collector._run_sut_cmd = MagicMock(side_effect=run_sut_cmd_side_effect)
result, data = collector.collect_data()
# Should still return data from successful commands
assert result.status == ExecutionStatus.OK
assert data is not None
assert len(data.interfaces) == 0 # Failed
assert len(data.routes) == 3 # Success
assert len(data.rules) == 3 # Success
assert len(data.neighbors) == 2 # Success
assert len(data.ethtool_info) == 0 # No interfaces, so no ethtool data
assert len(result.events) > 0
def test_collect_data_all_failures(collector, conn_mock):
"""Test collection when all commands fail"""
collector.system_info.os_family = OSFamily.LINUX
# Mock all commands failing (including ethtool, LLDP, Broadcom, Pensando)
def run_sut_cmd_side_effect(cmd, **kwargs):
return MagicMock(exit_code=1, command=cmd)
collector._run_sut_cmd = MagicMock(side_effect=run_sut_cmd_side_effect)
result, data = collector.collect_data()
assert result.status == ExecutionStatus.OK
assert data is not None
assert len(data.interfaces) == 0
assert len(data.routes) == 0
assert len(data.rules) == 0
assert len(data.neighbors) == 0
assert len(result.events) > 0
def test_parse_empty_output(collector):
"""Test parsing empty command output"""
interfaces = collector._parse_ip_addr("")
routes = collector._parse_ip_route("")
rules = collector._parse_ip_rule("")
neighbors = collector._parse_ip_neighbor("")
assert len(interfaces) == 0
assert len(routes) == 0
assert len(rules) == 0
assert len(neighbors) == 0
def test_parse_malformed_output(collector):
"""Test parsing malformed output gracefully"""
malformed = "this is not valid ip output\nsome random text\n123 456"
# Should not crash, just return empty or skip bad lines
interfaces = collector._parse_ip_addr(malformed)
routes = collector._parse_ip_route(malformed)
neighbors = collector._parse_ip_neighbor(malformed)
# Parser should handle gracefully
assert isinstance(interfaces, list)
assert isinstance(routes, list)
assert isinstance(neighbors, list)
def test_parse_ip_addr_ipv6_only(collector):
"""Test parsing interface with only IPv6 address"""
ipv6_only = """3: eth1: <BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether aa:bb:cc:dd:ee:ff brd ff:ff:ff:ff:ff:ff
inet6 fe80::a8bb:ccff:fedd:eeff/64 scope link
valid_lft forever preferred_lft forever"""
interfaces = collector._parse_ip_addr(ipv6_only)
assert len(interfaces) == 1
eth1 = interfaces[0]
assert eth1.name == "eth1"
assert len(eth1.addresses) == 1
assert eth1.addresses[0].family == "inet6"
assert eth1.addresses[0].address == "fe80::a8bb:ccff:fedd:eeff"
assert eth1.addresses[0].prefix_len == 64
def test_parse_ip_rule_with_action(collector):
"""Test parsing rule with unreachable action"""
rule_with_action = "200: from 10.0.0.5 unreachable"
rules = collector._parse_ip_rule(rule_with_action)
assert len(rules) == 1
rule = rules[0]
assert rule.priority == 200
assert rule.source == "10.0.0.5"
assert rule.action == "unreachable"
assert rule.table is None
def test_parse_ethtool_basic(collector):
"""Test parsing basic ethtool output"""
ethtool_info = collector._parse_ethtool("ethmock123", ETHTOOL_OUTPUT)
assert ethtool_info.interface == "ethmock123"
assert ethtool_info.speed == "1000mockMb/s"
assert ethtool_info.duplex == "Full"
assert ethtool_info.port == "MockedTwisted Pair"
assert ethtool_info.auto_negotiation == "on"
assert ethtool_info.link_detected == "yes"
assert "Speed" in ethtool_info.settings
assert ethtool_info.settings["Speed"] == "1000mockMb/s"
assert ethtool_info.settings["PHYAD"] == "1"
assert ethtool_info.raw_output == ETHTOOL_OUTPUT
def test_parse_ethtool_supported_link_modes(collector):
"""Test parsing supported link modes from ethtool output"""
ethtool_info = collector._parse_ethtool("ethmock123", ETHTOOL_OUTPUT)
# Check supported link modes are stored in settings dict
# Note: The current implementation stores link modes in settings dict,
# not in the supported_link_modes list
assert "Supported link modes" in ethtool_info.settings
assert "10mockbaseT/Half" in ethtool_info.settings["Supported link modes"]
def test_parse_ethtool_advertised_link_modes(collector):
"""Test parsing advertised link modes from ethtool output"""
ethtool_info = collector._parse_ethtool("ethmock123", ETHTOOL_OUTPUT)
# Check advertised link modes are stored in settings dict
# Note: The current implementation stores link modes in settings dict,
# not in the advertised_link_modes list
assert "Advertised link modes" in ethtool_info.settings
assert "10mockbaseT/Half" in ethtool_info.settings["Advertised link modes"]
assert "10mockbaseT/Full" in ethtool_info.settings["Advertised link modes"]
def test_parse_ethtool_no_link(collector):
"""Test parsing ethtool output when link is down"""
ethtool_info = collector._parse_ethtool("ethmock1", ETHTOOL_NO_LINK_OUTPUT)
assert ethtool_info.interface == "ethmock1"
assert ethtool_info.speed == "Unknown!"
assert ethtool_info.duplex == "Unknown!"
assert ethtool_info.port == "FIBRE"
assert ethtool_info.auto_negotiation == "off"
assert ethtool_info.link_detected == "no"
# Check supported link modes are stored in settings dict
assert "Supported link modes" in ethtool_info.settings
assert "11122mockbaseT/Full" in ethtool_info.settings["Supported link modes"]
def test_parse_ethtool_empty_output(collector):
"""Test parsing empty ethtool output"""
ethtool_info = collector._parse_ethtool("eth0", "")
assert ethtool_info.interface == "eth0"
assert ethtool_info.speed is None
assert ethtool_info.duplex is None
assert ethtool_info.link_detected is None
assert len(ethtool_info.settings) == 0
assert len(ethtool_info.supported_link_modes) == 0
assert len(ethtool_info.advertised_link_modes) == 0
def test_network_data_model_creation(collector):
"""Test creating NetworkDataModel with all components"""
interface = NetworkInterface(
name="ethmock123",
index=1,
state="UP",
mtu=5678,
addresses=[IpAddress(address="1.123.123.100", prefix_len=24, family="inet")],
)
route = Route(destination="default", gateway="2.123.123.1", device="ethmock123")
rule = RoutingRule(priority=100, source="1.123.123.0/24", table="main")
neighbor = Neighbor(
ip_address="50.50.1.1",
device="ethmock123",
mac_address="11:22:33:44:55:66",
state="REACHABLE",
)
ethtool_info = EthtoolInfo(
interface="ethmock123", raw_output=ETHTOOL_OUTPUT, speed="1000mockMb/s", duplex="Full"
)
data = NetworkDataModel(
interfaces=[interface],
routes=[route],
rules=[rule],
neighbors=[neighbor],
ethtool_info={"ethmock123": ethtool_info},
)
assert len(data.interfaces) == 1
assert len(data.routes) == 1
assert len(data.rules) == 1
assert len(data.neighbors) == 1
assert len(data.ethtool_info) == 1
assert data.interfaces[0].name == "ethmock123"
assert data.ethtool_info["ethmock123"].speed == "1000mockMb/s"
# Sample Broadcom NIC command outputs for testing
NICCLI_LISTDEV_OUTPUT = """
1 ) Broadcom BCM57608 1x400G QSFP-DD PCIe Ethernet NIC (Adp#1 Port#1)
Device Interface Name : abcd1p1
MAC Address : 81:82:83:84:85:88
PCI Address : 0000:22:00.0
"""
NICCLI_QOS_OUTPUT = """
IEEE 8021QAZ ETS Configuration TLV:
PRIO_MAP: 0:0 1:0 2:0 3:1 4:0 5:0 6:0 7:2
TC Bandwidth: 50% 50% 0%
TSA_MAP: 0:ets 1:ets 2:strict
IEEE 8021QAZ PFC TLV:
PFC enabled: 3
IEEE 8021QAZ APP TLV:
APP#0:
Priority: 7
Sel: 5
DSCP: 48
APP#1:
Priority: 3
Sel: 5
DSCP: 26
APP#2:
Priority: 3
Sel: 3
UDP or DCCP: 4791
TC Rate Limit: 100% 100% 100% 0% 0% 0% 0% 0%
"""
NICCLI_QOS_MINIMAL_OUTPUT = """IEEE 8021QAZ ETS Configuration TLV:
PRIO_MAP: 0:0 1:1
TC Bandwidth: 50% 50%
TSA_MAP: 0:ets 1:strict
IEEE 8021QAZ PFC TLV:
PFC enabled: 1
TC Rate Limit: 100% 100%
"""
# Sample Pensando NIC command outputs for testing
NICCTL_SHOW_CARD_OUTPUT = """
---------------------------------------------------------------------------------------------
Id PCIe BDF ASIC F/W partition Serial number
---------------------------------------------------------------------------------------------
1111111-4c32-3533-3330-12345000000 0000:06:00.0 test1 A ABC1234
2222222-4c32-3533-3731-78901500000 0000:16:00.0 test2 A DEF5678
"""
NICCTL_SHOW_DCQCN_OUTPUT = """
NIC : 1111111-4c32-3533-3330-12345000000 (0000:06:00.0)
------------------------------------------------------------------------------------------
Lif id : 1111111-4c32-3533-3330-12345000000
ROCE device : sample
DCQCN profile id : 1
Status : Disabled
******************************************************************************************
"""
NICCTL_SHOW_ENVIRONMENT_OUTPUT = """
NIC : 1111111-4c32-3533-3330-12345000000 (0000:06:00.0)
Power(W):
Total power drawn (pin) : 29.437
Core power (pout1) : 12.375
ARM power (pout2) : 0.788
Temperature(C):
Local board temperature : 44.12
Die temperature : 45.59
Voltage(mV):
Input voltage : 12078
Core voltage : 725
Frequency(MHz):
Core frequency : 1100
CPU frequency : 1500
P4 stage frequency : 1500
-------------------------------------------------------------------------------------
"""
NICCTL_SHOW_PCIE_ATS_OUTPUT = """
NIC : 1111111-4c32-3533-3330-12345000000 (0000:06:00.0) : Disabled
"""
NICCTL_SHOW_PORT_OUTPUT = """
NIC : 1111111-4c32-3533-3330-12345000000 (0000:06:00.0)
Port : 555555a-6c40-4242-4242-000011010000 (eth1/1)
Spec:
Ifindex : 0x11010000
Type : ETH
speed : 400G
Admin state : UP
FEC type : RS
Pause type : PFC
Number of lanes : 4
MTU : 9216
TX pause : enabled
RX pause : enabled
Auto negotiation : disabled
Status:
Physical port : 1
Operational status : DOWN
Link FSM state : SIGNAL_DETECT
FEC type : RS
Cable type : Copper
Number of lanes : 4
speed : 400G
Auto negotiation : disabled
MAC ID : 0
MAC channel : 0
MAC address : 04:90:81:4a:6c:40
Transceiver type : QSFP_CMIS
Transceiver state : SPROM-READ
Transceiver PID : QSFP-400G-CR4
-------------------------------------------------------------------------------------
"""
NICCTL_SHOW_QOS_OUTPUT = """
NIC : 1111111-4c32-3533-3330-12345000000 (0000:06:00.0)
Port : 0490814a-6c40-4242-4242-000011010000
Classification type : DSCP
DSCP-to-priority :
DSCP bitmap : 0xffffffffffffffff ==> priority : 0
DSCP : 0-63 ==> priority : 0
PFC :
PFC priority bitmap : 0x0
PFC no-drop priorities :
Scheduling :
--------------------------------------------
Priority Scheduling Bandwidth Rate-limit
Type (in %age) (in Gbps)
--------------------------------------------
0 DWRR 0 N/A
"""
def test_parse_niccli_listdev_device(collector):
"""Test parsing Broadcom NIC device from niccli --list_devices output"""
devices = collector._parse_niccli_listdev(NICCLI_LISTDEV_OUTPUT)
assert len(devices) == 1
# Check device
device1 = devices[0]
assert device1.device_num == 1
assert device1.model == "Broadcom BCM57608 1x400G QSFP-DD PCIe Ethernet NIC"
assert device1.adapter_port == "Adp#1 Port#1"
assert device1.interface_name == "abcd1p1"
assert device1.mac_address == "81:82:83:84:85:88"
assert device1.pci_address == "0000:22:00.0"
def test_parse_niccli_listdev_empty_output(collector):
"""Test parsing empty niccli --list_devices output"""
devices = collector._parse_niccli_listdev("")
assert len(devices) == 0
def test_parse_niccli_listdev_malformed_output(collector):
"""Test parsing malformed niccli --list_devices output gracefully"""
malformed = """some random text
not a valid device line
123 invalid format
"""
devices = collector._parse_niccli_listdev(malformed)
# Should handle gracefully, return empty list or skip invalid lines
assert isinstance(devices, list)
def test_parse_niccli_qos_complete(collector):
"""Test parsing complete Broadcom NIC QoS output with all fields"""
qos = collector._parse_niccli_qos(1, NICCLI_QOS_OUTPUT)
assert qos.device_num == 1
assert qos.raw_output == NICCLI_QOS_OUTPUT
# Check PRIO_MAP
assert len(qos.prio_map) == 8
assert qos.prio_map[0] == 0
assert qos.prio_map[1] == 0
assert qos.prio_map[3] == 1
assert qos.prio_map[7] == 2
# Check TC Bandwidth
assert len(qos.tc_bandwidth) == 3
assert qos.tc_bandwidth[0] == 50
assert qos.tc_bandwidth[1] == 50
assert qos.tc_bandwidth[2] == 0
# Check TSA_MAP
assert len(qos.tsa_map) == 3
assert qos.tsa_map[0] == "ets"
assert qos.tsa_map[1] == "ets"
assert qos.tsa_map[2] == "strict"
# Check PFC enabled
assert qos.pfc_enabled == 3
# Check APP entries
assert len(qos.app_entries) == 3
# Check APP#0
app0 = qos.app_entries[0]
assert app0.priority == 7
assert app0.sel == 5
assert app0.dscp == 48
assert app0.protocol is None
assert app0.port is None
# Check APP#1
app1 = qos.app_entries[1]
assert app1.priority == 3
assert app1.sel == 5
assert app1.dscp == 26
# Check APP#2 (with protocol and port)
app2 = qos.app_entries[2]
assert app2.priority == 3
assert app2.sel == 3
assert app2.dscp is None
assert app2.protocol == "UDP or DCCP"
assert app2.port == 4791
# Check TC Rate Limit
assert len(qos.tc_rate_limit) == 8
assert qos.tc_rate_limit[0] == 100
assert qos.tc_rate_limit[1] == 100
assert qos.tc_rate_limit[2] == 100
assert qos.tc_rate_limit[3] == 0
assert qos.tc_rate_limit[7] == 0
def test_parse_niccli_qos_empty_output(collector):
"""Test parsing empty QoS output"""
qos = collector._parse_niccli_qos(1, "")
assert qos.device_num == 1
assert qos.raw_output == ""
assert len(qos.prio_map) == 0
assert len(qos.tc_bandwidth) == 0
assert len(qos.tsa_map) == 0
assert qos.pfc_enabled is None
assert len(qos.app_entries) == 0
assert len(qos.tc_rate_limit) == 0
def test_parse_niccli_qos_multiple_app_protocols(collector):
"""Test parsing QoS with APP entries having different protocols"""
qos_multi_protocol = """IEEE 8021QAZ ETS Configuration TLV:
PRIO_MAP: 0:0
TC Bandwidth: 100%
TSA_MAP: 0:ets
IEEE 8021QAZ PFC TLV:
PFC enabled: 0
IEEE 8021QAZ APP TLV:
APP#0:
Priority: 5
Sel: 3
TCP: 8080
APP#1:
Priority: 6
Sel: 3
UDP: 9000
TC Rate Limit: 100%
"""
qos = collector._parse_niccli_qos(3, qos_multi_protocol)
assert len(qos.app_entries) == 2
# Check TCP entry
app0 = qos.app_entries[0]
assert app0.priority == 5
assert app0.sel == 3
assert app0.protocol == "TCP"
assert app0.port == 8080
# Check UDP entry
app1 = qos.app_entries[1]
assert app1.priority == 6
assert app1.sel == 3
assert app1.protocol == "UDP"
assert app1.port == 9000
def test_parse_niccli_qos_malformed_values(collector):
"""Test parsing QoS output with malformed values gracefully"""
malformed = """IEEE 8021QAZ ETS Configuration TLV:
PRIO_MAP: 0:invalid 1:1 bad:data
TC Bandwidth: 50% invalid 50%
TSA_MAP: 0:ets bad:value 1:strict
IEEE 8021QAZ PFC TLV:
PFC enabled: not_a_number
TC Rate Limit: 100% bad% 100%
"""
qos = collector._parse_niccli_qos(1, malformed)
# Should skip invalid entries but parse valid ones
assert qos.device_num == 1
# Should have parsed valid prio_map entry (1:1)
assert 1 in qos.prio_map
assert qos.prio_map[1] == 1
# Should have parsed valid bandwidth entries
assert 50 in qos.tc_bandwidth
# Should have parsed valid tsa_map entries
assert qos.tsa_map.get(0) == "ets"
assert qos.tsa_map.get(1) == "strict"
# PFC should be None due to invalid number
assert qos.pfc_enabled is None
def test_network_data_model_with_broadcom_nic(collector):
"""Test creating NetworkDataModel with Broadcom NIC data"""
device = BroadcomNicDevice(
device_num=1,
model="Broadcom BCM57608 1x400G QSFP-DD PCIe Ethernet NIC",
adapter_port="Adp#1 Port#1",
interface_name="benic1p1",
mac_address="8C:84:74:37:C3:70",
pci_address="0000:06:00.0",
)
qos = BroadcomNicQos(
device_num=1,
raw_output="test output",
prio_map={0: 0, 1: 1},
tc_bandwidth=[50, 50],
tsa_map={0: "ets", 1: "strict"},
pfc_enabled=3,
tc_rate_limit=[100, 100],
)
data = NetworkDataModel(
interfaces=[],
routes=[],
rules=[],
neighbors=[],
ethtool_info={},
broadcom_nic_devices=[device],
broadcom_nic_qos={1: qos},
)
assert len(data.broadcom_nic_devices) == 1
assert len(data.broadcom_nic_qos) == 1
assert data.broadcom_nic_devices[0].device_num == 1
assert data.broadcom_nic_devices[0].interface_name == "benic1p1"
assert data.broadcom_nic_qos[1].device_num == 1
assert data.broadcom_nic_qos[1].pfc_enabled == 3
def test_parse_nicctl_show_card_multiple_cards(collector):
"""Test parsing multiple Pensando NIC cards from nicctl show card output"""
cards = collector._parse_nicctl_card(NICCTL_SHOW_CARD_OUTPUT)
assert len(cards) == 2
# Check first card
card1 = cards[0]
assert card1.id == "1111111-4c32-3533-3330-12345000000"
assert card1.pcie_bdf == "0000:06:00.0"
assert card1.asic == "test1"
assert card1.fw_partition == "A"
assert card1.serial_number == "ABC1234"
# Check second card
card2 = cards[1]
assert card2.id == "2222222-4c32-3533-3731-78901500000"
assert card2.pcie_bdf == "0000:16:00.0"
assert card2.asic == "test2"
assert card2.fw_partition == "A"
assert card2.serial_number == "DEF5678"
def test_parse_nicctl_show_card_empty_output(collector):
"""Test parsing empty nicctl show card output"""
cards = collector._parse_nicctl_card("")
assert len(cards) == 0
def test_parse_nicctl_show_card_partial_fields(collector):
"""Test parsing nicctl show card output with partial fields"""
partial_output = """
---------------------------------------------------------------------------------------------
Id PCIe BDF ASIC F/W partition Serial number
---------------------------------------------------------------------------------------------
42424650-4c32-3533-3330-323934000000 0000:06:00.0
42424650-4c32-3533-3731-304535000000 0000:16:00.0 salina
"""
cards = collector._parse_nicctl_card(partial_output)
assert len(cards) == 2
# First card with only ID and PCIe BDF
card1 = cards[0]
assert card1.id == "42424650-4c32-3533-3330-323934000000"
assert card1.pcie_bdf == "0000:06:00.0"
assert card1.asic is None
assert card1.fw_partition is None
assert card1.serial_number is None
# Second card with ID, PCIe BDF, and ASIC
card2 = cards[1]
assert card2.id == "42424650-4c32-3533-3731-304535000000"
assert card2.pcie_bdf == "0000:16:00.0"
assert card2.asic == "salina"
assert card2.fw_partition is None
assert card2.serial_number is None