Skip to content

Commit 9985a71

Browse files
committed
CP-312083: [test] Unit tests for Lldp.parse_neighbors
Cover parsing lldpcli -f json output: a single neighbour, multiple neighbours on one interface (parser returns all), no neighbours, and malformed JSON (empty result).
1 parent ff82426 commit 9985a71

2 files changed

Lines changed: 111 additions & 0 deletions

File tree

ocaml/networkd/test/network_test.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ let () =
1919
@ Test_jsonrpc_client.tests
2020
@ Test_network_device_order_inherited.tests
2121
@ Test_network_device_order.tests
22+
@ Test_lldp.tests
2223
)

ocaml/networkd/test/test_lldp.ml

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
(*
2+
* Copyright (c) Cloud Software Group, Inc.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published
6+
* by the Free Software Foundation; version 2.1 only. with the special
7+
* exception on linking described in file LICENSE.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*)
14+
15+
(* Tests for Lldp.parse_neighbors: parsing the JSON emitted by
16+
[lldpcli -f json show neighbors]. *)
17+
18+
let lldp_rx_testable =
19+
let open Network_stats in
20+
Alcotest.testable
21+
(fun ppf rx ->
22+
Fmt.pf ppf "{system_name=%a; port_id=%a; port_description=%a}"
23+
Fmt.(option string) rx.system_name Fmt.(option string) rx.port_id
24+
Fmt.(option string) rx.port_description
25+
)
26+
( = )
27+
28+
let result_testable = Alcotest.(list (pair string lldp_rx_testable))
29+
30+
let rx ?system_name ?port_id ?port_description () =
31+
Network_stats.{system_name; port_id; port_description}
32+
33+
(* One interface with one neighbour (a Cisco Nexus switch). *)
34+
let single_json =
35+
{|
36+
{ "lldp": { "interface": [
37+
{ "eno8303": {
38+
"via": "LLDP", "rid": "3",
39+
"chassis": { "NKG-ESWA07-2.eng.citrite.net": {
40+
"id": { "type": "mac", "value": "c4:ab:4d:f0:9b:d3" },
41+
"descr": "Cisco Nexus",
42+
"capability": [ { "type": "Bridge", "enabled": true } ]
43+
} },
44+
"port": {
45+
"id": { "type": "ifname", "value": "Ethernet1/28" },
46+
"descr": "nkg-dt16/idrac", "ttl": "120"
47+
}
48+
} }
49+
] } }
50+
|}
51+
52+
(* One interface with two neighbours (wider multicast scope / repeater). *)
53+
let multi_json =
54+
{|
55+
{ "lldp": { "interface": [
56+
{ "eno8303": {
57+
"rid": "1",
58+
"chassis": { "TOR-A-01": { "id": { "type": "mac", "value": "aa" } } },
59+
"port": { "id": { "type": "ifname", "value": "Eth1/8/3" },
60+
"descr": "rack7-a" }
61+
} },
62+
{ "eno8303": {
63+
"rid": "2",
64+
"chassis": { "TOR-B-02": { "id": { "type": "mac", "value": "bb" } } },
65+
"port": { "id": { "type": "ifname", "value": "Eth2/8/3" },
66+
"descr": "rack7-b" }
67+
} }
68+
] } }
69+
|}
70+
71+
let empty_json = {| { "lldp": { "interface": [] } } |}
72+
73+
let test_single () =
74+
Alcotest.check result_testable "single neighbour"
75+
[
76+
( "eno8303"
77+
, rx ~system_name:"NKG-ESWA07-2.eng.citrite.net" ~port_id:"Ethernet1/28"
78+
~port_description:"nkg-dt16/idrac" ()
79+
)
80+
]
81+
(Lldp.parse_neighbors single_json)
82+
83+
let test_multi () =
84+
(* The parser returns all neighbours; picking one is done by the caller. *)
85+
Alcotest.check result_testable "two neighbours on one interface"
86+
[
87+
("eno8303", rx ~system_name:"TOR-A-01" ~port_id:"Eth1/8/3" ~port_description:"rack7-a" ())
88+
; ("eno8303", rx ~system_name:"TOR-B-02" ~port_id:"Eth2/8/3" ~port_description:"rack7-b" ())
89+
]
90+
(Lldp.parse_neighbors multi_json)
91+
92+
let test_empty () =
93+
Alcotest.check result_testable "no neighbours" []
94+
(Lldp.parse_neighbors empty_json)
95+
96+
let test_malformed () =
97+
Alcotest.check result_testable "malformed JSON yields empty" []
98+
(Lldp.parse_neighbors "not json {")
99+
100+
let tests =
101+
[
102+
( "lldp_parse_neighbors"
103+
, [
104+
("single", `Quick, test_single)
105+
; ("multi", `Quick, test_multi)
106+
; ("empty", `Quick, test_empty)
107+
; ("malformed", `Quick, test_malformed)
108+
]
109+
)
110+
]

0 commit comments

Comments
 (0)