Skip to content

Commit cfc0849

Browse files
committed
fixup! CP-44103: Unit tests for ordering network devices
Signed-off-by: Ming Lu <[email protected]>
1 parent 1dc3a22 commit cfc0849

File tree

1 file changed

+95
-152
lines changed

1 file changed

+95
-152
lines changed

ocaml/networkd/test/test_network_device_order.ml

+95-152
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ let currents =
9494
}
9595
]
9696

97+
let plug dev devices = List.cons dev devices
98+
99+
let unplug mac devices = List.filter (fun dev -> dev.Dev.mac <> mac) devices
100+
97101
let pos_of_mac mac order =
98102
match List.find_opt (fun dev -> dev.mac = mac) order with
99103
| Some {position; _} ->
@@ -108,24 +112,22 @@ let present_of_mac mac order =
108112
| _ ->
109113
failwith "Can't find the device!"
110114

115+
let test_postion_and_present expected_position expected_present mac order =
116+
let name = Format.asprintf "Position assigned for %a" Macaddr.pp mac in
117+
Alcotest.(check int) name expected_position (pos_of_mac mac order) ;
118+
Alcotest.(check bool) name expected_present (present_of_mac mac order)
119+
111120
let test_default () =
112121
let order = sort' ~currents ~rules:[] ~last_order:[] in
113122
Alcotest.(check bool) "is Ok" true (Result.is_ok order) ;
114123
let order = Result.get_ok order in
115124
Alcotest.(check int) "6 devices in the order" 6 (List.length order) ;
116-
Alcotest.(check int) "position assigned" 0 (pos_of_mac mac_addr0 order) ;
117-
Alcotest.(check int) "position assigned" 1 (pos_of_mac mac_addr1 order) ;
118-
Alcotest.(check int) "position assigned" 2 (pos_of_mac mac_addr2 order) ;
119-
Alcotest.(check int) "position assigned" 3 (pos_of_mac mac_addr3 order) ;
120-
Alcotest.(check int) "position assigned" 5 (pos_of_mac mac_addr4 order) ;
121-
Alcotest.(check int) "position assigned" 4 (pos_of_mac mac_addr5 order) ;
122-
123-
Alcotest.(check bool) "present" true (present_of_mac mac_addr0 order) ;
124-
Alcotest.(check bool) "present" true (present_of_mac mac_addr1 order) ;
125-
Alcotest.(check bool) "present" true (present_of_mac mac_addr2 order) ;
126-
Alcotest.(check bool) "present" true (present_of_mac mac_addr3 order) ;
127-
Alcotest.(check bool) "present" true (present_of_mac mac_addr4 order) ;
128-
Alcotest.(check bool) "present" true (present_of_mac mac_addr5 order)
125+
test_postion_and_present 0 true mac_addr0 order ;
126+
test_postion_and_present 1 true mac_addr1 order ;
127+
test_postion_and_present 2 true mac_addr2 order ;
128+
test_postion_and_present 3 true mac_addr3 order ;
129+
test_postion_and_present 5 true mac_addr4 order ;
130+
test_postion_and_present 4 true mac_addr5 order
129131

130132
let test_initial_rules_via_mac () =
131133
let rules =
@@ -143,19 +145,12 @@ let test_initial_rules_via_mac () =
143145
Alcotest.(check bool) "is Ok" true (Result.is_ok order) ;
144146
let order = Result.get_ok order in
145147
Alcotest.(check int) "6 devices in the order" 6 (List.length order) ;
146-
Alcotest.(check int) "position assigned" 5 (pos_of_mac mac_addr0 order) ;
147-
Alcotest.(check int) "position assigned" 4 (pos_of_mac mac_addr1 order) ;
148-
Alcotest.(check int) "position assigned" 3 (pos_of_mac mac_addr2 order) ;
149-
Alcotest.(check int) "position assigned" 2 (pos_of_mac mac_addr3 order) ;
150-
Alcotest.(check int) "position assigned" 1 (pos_of_mac mac_addr4 order) ;
151-
Alcotest.(check int) "position assigned" 0 (pos_of_mac mac_addr5 order) ;
152-
153-
Alcotest.(check bool) "present" true (present_of_mac mac_addr0 order) ;
154-
Alcotest.(check bool) "present" true (present_of_mac mac_addr1 order) ;
155-
Alcotest.(check bool) "present" true (present_of_mac mac_addr2 order) ;
156-
Alcotest.(check bool) "present" true (present_of_mac mac_addr3 order) ;
157-
Alcotest.(check bool) "present" true (present_of_mac mac_addr4 order) ;
158-
Alcotest.(check bool) "present" true (present_of_mac mac_addr5 order)
148+
test_postion_and_present 5 true mac_addr0 order ;
149+
test_postion_and_present 4 true mac_addr1 order ;
150+
test_postion_and_present 3 true mac_addr2 order ;
151+
test_postion_and_present 2 true mac_addr3 order ;
152+
test_postion_and_present 1 true mac_addr4 order ;
153+
test_postion_and_present 0 true mac_addr5 order
159154

160155
let test_initial_rules_via_label () =
161156
let rules =
@@ -173,19 +168,12 @@ let test_initial_rules_via_label () =
173168
Alcotest.(check bool) "is Ok" true (Result.is_ok order) ;
174169
let order = Result.get_ok order in
175170
Alcotest.(check int) "6 devices in the order" 6 (List.length order) ;
176-
Alcotest.(check int) "position assigned" 5 (pos_of_mac mac_addr0 order) ;
177-
Alcotest.(check int) "position assigned" 4 (pos_of_mac mac_addr1 order) ;
178-
Alcotest.(check int) "position assigned" 3 (pos_of_mac mac_addr2 order) ;
179-
Alcotest.(check int) "position assigned" 2 (pos_of_mac mac_addr3 order) ;
180-
Alcotest.(check int) "position assigned" 1 (pos_of_mac mac_addr4 order) ;
181-
Alcotest.(check int) "position assigned" 0 (pos_of_mac mac_addr5 order) ;
182-
183-
Alcotest.(check bool) "present" true (present_of_mac mac_addr0 order) ;
184-
Alcotest.(check bool) "present" true (present_of_mac mac_addr1 order) ;
185-
Alcotest.(check bool) "present" true (present_of_mac mac_addr2 order) ;
186-
Alcotest.(check bool) "present" true (present_of_mac mac_addr3 order) ;
187-
Alcotest.(check bool) "present" true (present_of_mac mac_addr4 order) ;
188-
Alcotest.(check bool) "present" true (present_of_mac mac_addr5 order)
171+
test_postion_and_present 5 true mac_addr0 order ;
172+
test_postion_and_present 4 true mac_addr1 order ;
173+
test_postion_and_present 3 true mac_addr2 order ;
174+
test_postion_and_present 2 true mac_addr3 order ;
175+
test_postion_and_present 1 true mac_addr4 order ;
176+
test_postion_and_present 0 true mac_addr5 order
189177

190178
let test_replacement () =
191179
let mac_addr0' = Macaddr.of_string "fc:f4:bb:e6:d7:b8" |> Result.get_ok in
@@ -210,31 +198,20 @@ let test_replacement () =
210198
}
211199
in
212200
let currents =
213-
currents
214-
|> List.filter (fun dev -> dev.mac <> mac_addr0)
215-
|> List.filter (fun dev -> dev.mac <> mac_addr1)
216-
|> List.cons dev0
217-
|> List.cons dev1
201+
currents |> unplug mac_addr0 |> plug dev0 |> unplug mac_addr1 |> plug dev1
218202
in
219203
let order = sort' ~currents ~rules:[] ~last_order in
220204
Alcotest.(check bool) "is Ok" true (Result.is_ok order) ;
221205
let order = Result.get_ok order in
222206

223207
Alcotest.(check int) "6 devices in the order" 6 (List.length order) ;
224208

225-
Alcotest.(check int) "position assigned" 0 (pos_of_mac mac_addr0' order) ;
226-
Alcotest.(check int) "position assigned" 1 (pos_of_mac mac_addr1' order) ;
227-
Alcotest.(check int) "position assigned" 2 (pos_of_mac mac_addr2 order) ;
228-
Alcotest.(check int) "position assigned" 3 (pos_of_mac mac_addr3 order) ;
229-
Alcotest.(check int) "position assigned" 4 (pos_of_mac mac_addr4 order) ;
230-
Alcotest.(check int) "position assigned" 5 (pos_of_mac mac_addr5 order) ;
231-
232-
Alcotest.(check bool) "present" true (present_of_mac mac_addr0' order) ;
233-
Alcotest.(check bool) "present" true (present_of_mac mac_addr1' order) ;
234-
Alcotest.(check bool) "present" true (present_of_mac mac_addr2 order) ;
235-
Alcotest.(check bool) "present" true (present_of_mac mac_addr3 order) ;
236-
Alcotest.(check bool) "present" true (present_of_mac mac_addr4 order) ;
237-
Alcotest.(check bool) "present" true (present_of_mac mac_addr5 order)
209+
test_postion_and_present 0 true mac_addr0' order ;
210+
test_postion_and_present 1 true mac_addr1' order ;
211+
test_postion_and_present 2 true mac_addr2 order ;
212+
test_postion_and_present 3 true mac_addr3 order ;
213+
test_postion_and_present 4 true mac_addr4 order ;
214+
test_postion_and_present 5 true mac_addr5 order
238215

239216
let test_adding () =
240217
let pci_addr6 = Pciaddr.of_string "0000:06:0f.0" |> Result.get_ok in
@@ -261,55 +238,34 @@ let test_adding () =
261238
}
262239
in
263240
(* Add two devices *)
264-
let currents = List.rev_append currents [dev6; dev7] in
241+
let currents = currents |> plug dev6 |> plug dev7 in
265242
let order = sort' ~currents ~rules:[] ~last_order in
266243
Alcotest.(check bool) "is Ok" true (Result.is_ok order) ;
267244
let order = Result.get_ok order in
268245
Alcotest.(check int) "8 devices in the order" 8 (List.length order) ;
269-
Alcotest.(check int) "position assigned" 0 (pos_of_mac mac_addr0 order) ;
270-
Alcotest.(check int) "position assigned" 1 (pos_of_mac mac_addr1 order) ;
271-
Alcotest.(check int) "position assigned" 2 (pos_of_mac mac_addr2 order) ;
272-
Alcotest.(check int) "position assigned" 3 (pos_of_mac mac_addr3 order) ;
273-
Alcotest.(check int) "position assigned" 4 (pos_of_mac mac_addr4 order) ;
274-
Alcotest.(check int) "position assigned" 5 (pos_of_mac mac_addr5 order) ;
275-
Alcotest.(check int) "position assigned" 6 (pos_of_mac mac_addr7 order) ;
276-
Alcotest.(check int) "position assigned" 7 (pos_of_mac mac_addr6 order) ;
277-
278-
Alcotest.(check bool) "present" true (present_of_mac mac_addr0 order) ;
279-
Alcotest.(check bool) "present" true (present_of_mac mac_addr1 order) ;
280-
Alcotest.(check bool) "present" true (present_of_mac mac_addr2 order) ;
281-
Alcotest.(check bool) "present" true (present_of_mac mac_addr3 order) ;
282-
Alcotest.(check bool) "present" true (present_of_mac mac_addr4 order) ;
283-
Alcotest.(check bool) "present" true (present_of_mac mac_addr5 order) ;
284-
Alcotest.(check bool) "present" true (present_of_mac mac_addr6 order) ;
285-
Alcotest.(check bool) "present" true (present_of_mac mac_addr7 order)
246+
test_postion_and_present 0 true mac_addr0 order ;
247+
test_postion_and_present 1 true mac_addr1 order ;
248+
test_postion_and_present 2 true mac_addr2 order ;
249+
test_postion_and_present 3 true mac_addr3 order ;
250+
test_postion_and_present 4 true mac_addr4 order ;
251+
test_postion_and_present 5 true mac_addr5 order ;
252+
test_postion_and_present 6 true mac_addr7 order ;
253+
test_postion_and_present 7 true mac_addr6 order
286254

287255
let test_removing () =
288256
(* Remove two devices *)
289-
let currents =
290-
let open Dev in
291-
currents
292-
|> List.filter (fun dev -> dev.mac <> mac_addr0)
293-
|> List.filter (fun dev -> dev.mac <> mac_addr1)
294-
in
257+
let currents = currents |> unplug mac_addr0 |> unplug mac_addr1 in
295258
let order = sort' ~currents ~rules:[] ~last_order in
296259
Alcotest.(check bool) "is Ok" true (Result.is_ok order) ;
297260
let order = Result.get_ok order in
298261

299262
Alcotest.(check int) "6 devices in the order" 6 (List.length order) ;
300-
Alcotest.(check int) "position assigned" 0 (pos_of_mac mac_addr0 order) ;
301-
Alcotest.(check int) "position assigned" 1 (pos_of_mac mac_addr1 order) ;
302-
Alcotest.(check int) "position assigned" 2 (pos_of_mac mac_addr2 order) ;
303-
Alcotest.(check int) "position assigned" 3 (pos_of_mac mac_addr3 order) ;
304-
Alcotest.(check int) "position assigned" 4 (pos_of_mac mac_addr4 order) ;
305-
Alcotest.(check int) "position assigned" 5 (pos_of_mac mac_addr5 order) ;
306-
307-
Alcotest.(check bool) "present" false (present_of_mac mac_addr0 order) ;
308-
Alcotest.(check bool) "present" false (present_of_mac mac_addr1 order) ;
309-
Alcotest.(check bool) "present" true (present_of_mac mac_addr2 order) ;
310-
Alcotest.(check bool) "present" true (present_of_mac mac_addr3 order) ;
311-
Alcotest.(check bool) "present" true (present_of_mac mac_addr4 order) ;
312-
Alcotest.(check bool) "present" true (present_of_mac mac_addr5 order)
263+
test_postion_and_present 0 false mac_addr0 order ;
264+
test_postion_and_present 1 false mac_addr1 order ;
265+
test_postion_and_present 2 true mac_addr2 order ;
266+
test_postion_and_present 3 true mac_addr3 order ;
267+
test_postion_and_present 4 true mac_addr4 order ;
268+
test_postion_and_present 5 true mac_addr5 order
313269

314270
let test_replug () =
315271
let dev0 =
@@ -330,19 +286,12 @@ let test_replug () =
330286
Alcotest.(check bool) "is Ok" true (Result.is_ok order) ;
331287
let order = Result.get_ok order in
332288
Alcotest.(check int) "6 devices in the order" 6 (List.length order) ;
333-
Alcotest.(check int) "position assigned" 0 (pos_of_mac mac_addr0 order) ;
334-
Alcotest.(check int) "position assigned" 1 (pos_of_mac mac_addr1 order) ;
335-
Alcotest.(check int) "position assigned" 2 (pos_of_mac mac_addr2 order) ;
336-
Alcotest.(check int) "position assigned" 3 (pos_of_mac mac_addr3 order) ;
337-
Alcotest.(check int) "position assigned" 4 (pos_of_mac mac_addr4 order) ;
338-
Alcotest.(check int) "position assigned" 5 (pos_of_mac mac_addr5 order) ;
339-
340-
Alcotest.(check bool) "present" true (present_of_mac mac_addr0 order) ;
341-
Alcotest.(check bool) "present" true (present_of_mac mac_addr1 order) ;
342-
Alcotest.(check bool) "present" true (present_of_mac mac_addr2 order) ;
343-
Alcotest.(check bool) "present" true (present_of_mac mac_addr3 order) ;
344-
Alcotest.(check bool) "present" true (present_of_mac mac_addr4 order) ;
345-
Alcotest.(check bool) "present" true (present_of_mac mac_addr5 order)
289+
test_postion_and_present 0 true mac_addr0 order ;
290+
test_postion_and_present 1 true mac_addr1 order ;
291+
test_postion_and_present 2 true mac_addr2 order ;
292+
test_postion_and_present 3 true mac_addr3 order ;
293+
test_postion_and_present 4 true mac_addr4 order ;
294+
test_postion_and_present 5 true mac_addr5 order
346295

347296
let test_multi_nic_inplace_reorder () =
348297
let mac_addr4' = Macaddr.of_string "01:02:c9:ed:fd:f0" |> Result.get_ok in
@@ -378,19 +327,12 @@ let test_multi_nic_inplace_reorder () =
378327
Alcotest.(check bool) "is Ok" true (Result.is_ok order) ;
379328
let order = Result.get_ok order in
380329
Alcotest.(check int) "6 devices in the order" 6 (List.length order) ;
381-
Alcotest.(check int) "position assigned" 0 (pos_of_mac mac_addr0 order) ;
382-
Alcotest.(check int) "position assigned" 1 (pos_of_mac mac_addr1 order) ;
383-
Alcotest.(check int) "position assigned" 2 (pos_of_mac mac_addr2 order) ;
384-
Alcotest.(check int) "position assigned" 3 (pos_of_mac mac_addr3 order) ;
385-
Alcotest.(check int) "position assigned" 4 (pos_of_mac mac_addr4' order) ;
386-
Alcotest.(check int) "position assigned" 5 (pos_of_mac mac_addr5' order) ;
387-
388-
Alcotest.(check bool) "present" true (present_of_mac mac_addr0 order) ;
389-
Alcotest.(check bool) "present" true (present_of_mac mac_addr1 order) ;
390-
Alcotest.(check bool) "present" true (present_of_mac mac_addr2 order) ;
391-
Alcotest.(check bool) "present" true (present_of_mac mac_addr3 order) ;
392-
Alcotest.(check bool) "present" true (present_of_mac mac_addr4' order) ;
393-
Alcotest.(check bool) "present" true (present_of_mac mac_addr5' order)
330+
test_postion_and_present 0 true mac_addr0 order ;
331+
test_postion_and_present 1 true mac_addr1 order ;
332+
test_postion_and_present 2 true mac_addr2 order ;
333+
test_postion_and_present 3 true mac_addr3 order ;
334+
test_postion_and_present 4 true mac_addr4' order ;
335+
test_postion_and_present 5 true mac_addr5' order
394336

395337
let test_multi_nic_new_devices () =
396338
let mac_addr6 = Macaddr.of_string "01:02:c9:ed:fd:f0" |> Result.get_ok in
@@ -415,29 +357,21 @@ let test_multi_nic_new_devices () =
415357
; multi_nic= true
416358
}
417359
in
418-
(* New devices are reported on the same PCI address. *)
419-
let currents = currents |> List.cons dev6 |> List.cons dev7 in
360+
(* New devices are reported on the same PCI address.
361+
It's equivalent to plugging new devices but locate at the same PCI address. *)
362+
let currents = currents |> plug dev6 |> plug dev7 in
420363
let order = sort' ~currents ~rules:[] ~last_order in
421364
Alcotest.(check bool) "is Ok" true (Result.is_ok order) ;
422365
let order = Result.get_ok order in
423366
Alcotest.(check int) "8 devices in the order" 8 (List.length order) ;
424-
Alcotest.(check int) "position assigned" 0 (pos_of_mac mac_addr0 order) ;
425-
Alcotest.(check int) "position assigned" 1 (pos_of_mac mac_addr1 order) ;
426-
Alcotest.(check int) "position assigned" 2 (pos_of_mac mac_addr2 order) ;
427-
Alcotest.(check int) "position assigned" 3 (pos_of_mac mac_addr3 order) ;
428-
Alcotest.(check int) "position assigned" 4 (pos_of_mac mac_addr4 order) ;
429-
Alcotest.(check int) "position assigned" 5 (pos_of_mac mac_addr5 order) ;
430-
Alcotest.(check int) "position assigned" 6 (pos_of_mac mac_addr6 order) ;
431-
Alcotest.(check int) "position assigned" 7 (pos_of_mac mac_addr7 order) ;
432-
433-
Alcotest.(check bool) "present" true (present_of_mac mac_addr0 order) ;
434-
Alcotest.(check bool) "present" true (present_of_mac mac_addr1 order) ;
435-
Alcotest.(check bool) "present" true (present_of_mac mac_addr2 order) ;
436-
Alcotest.(check bool) "present" true (present_of_mac mac_addr3 order) ;
437-
Alcotest.(check bool) "present" true (present_of_mac mac_addr4 order) ;
438-
Alcotest.(check bool) "present" true (present_of_mac mac_addr5 order) ;
439-
Alcotest.(check bool) "present" true (present_of_mac mac_addr6 order) ;
440-
Alcotest.(check bool) "present" true (present_of_mac mac_addr7 order)
367+
test_postion_and_present 0 true mac_addr0 order ;
368+
test_postion_and_present 1 true mac_addr1 order ;
369+
test_postion_and_present 2 true mac_addr2 order ;
370+
test_postion_and_present 3 true mac_addr3 order ;
371+
test_postion_and_present 4 true mac_addr4 order ;
372+
test_postion_and_present 5 true mac_addr5 order ;
373+
test_postion_and_present 6 true mac_addr6 order ;
374+
test_postion_and_present 7 true mac_addr7 order
441375

442376
let test_pci_changes () =
443377
let move_bus_by_1 pci_addr = Xcp_pci.{pci_addr with bus= pci_addr.bus + 1} in
@@ -448,19 +382,27 @@ let test_pci_changes () =
448382
Alcotest.(check bool) "is Ok" true (Result.is_ok order) ;
449383
let order = Result.get_ok order in
450384
Alcotest.(check int) "6 devices in the order" 6 (List.length order) ;
451-
Alcotest.(check int) "position assigned" 0 (pos_of_mac mac_addr0 order) ;
452-
Alcotest.(check int) "position assigned" 1 (pos_of_mac mac_addr1 order) ;
453-
Alcotest.(check int) "position assigned" 2 (pos_of_mac mac_addr2 order) ;
454-
Alcotest.(check int) "position assigned" 3 (pos_of_mac mac_addr3 order) ;
455-
Alcotest.(check int) "position assigned" 4 (pos_of_mac mac_addr4 order) ;
456-
Alcotest.(check int) "position assigned" 5 (pos_of_mac mac_addr5 order) ;
457-
458-
Alcotest.(check bool) "present" true (present_of_mac mac_addr0 order) ;
459-
Alcotest.(check bool) "present" true (present_of_mac mac_addr1 order) ;
460-
Alcotest.(check bool) "present" true (present_of_mac mac_addr2 order) ;
461-
Alcotest.(check bool) "present" true (present_of_mac mac_addr3 order) ;
462-
Alcotest.(check bool) "present" true (present_of_mac mac_addr4 order) ;
463-
Alcotest.(check bool) "present" true (present_of_mac mac_addr5 order)
385+
test_postion_and_present 0 true mac_addr0 order ;
386+
test_postion_and_present 1 true mac_addr1 order ;
387+
test_postion_and_present 2 true mac_addr2 order ;
388+
test_postion_and_present 3 true mac_addr3 order ;
389+
test_postion_and_present 4 true mac_addr4 order ;
390+
test_postion_and_present 5 true mac_addr5 order
391+
392+
let test_pci_addr_compare () =
393+
let addr0 = Pciaddr.of_string "0000:01:0e.0" |> Result.get_ok in
394+
let addr1 = Pciaddr.of_string "0000:01:0e.0" |> Result.get_ok in
395+
let addr2 = Pciaddr.of_string "0000:01:0e.2" |> Result.get_ok in
396+
let addr3 = Pciaddr.of_string "0000:01:0e.3" |> Result.get_ok in
397+
let addr4 = Pciaddr.of_string "0000:01:0f.0" |> Result.get_ok in
398+
let addr5 = Pciaddr.of_string "0000:02:0f.0" |> Result.get_ok in
399+
let addr6 = Pciaddr.of_string "0001:02:0f.0" |> Result.get_ok in
400+
Alcotest.(check bool) "equal" true (Pciaddr.compare addr0 addr1 = 0) ;
401+
Alcotest.(check bool) "less than" true (Pciaddr.compare addr0 addr2 < 0) ;
402+
Alcotest.(check bool) "greater than" true (Pciaddr.compare addr3 addr2 > 0) ;
403+
Alcotest.(check bool) "greater than" true (Pciaddr.compare addr4 addr3 > 0) ;
404+
Alcotest.(check bool) "greater than" true (Pciaddr.compare addr5 addr4 > 0) ;
405+
Alcotest.(check bool) "less than" true (Pciaddr.compare addr6 addr5 > 0)
464406

465407
let tests =
466408
[
@@ -479,6 +421,7 @@ let tests =
479421
)
480422
; ("test_multi_nic_new_devices", `Quick, test_multi_nic_new_devices)
481423
; ("test_pci_changes", `Quick, test_pci_changes)
424+
; ("test_pci_addr_compare", `Quick, test_pci_addr_compare)
482425
]
483426
)
484427
]

0 commit comments

Comments
 (0)