Skip to content

Commit

Permalink
CA-402921: Add some unit tests for Xapi_vif_helpers
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Hoes <[email protected]>
  • Loading branch information
robhoes committed Dec 18, 2024
1 parent 14406ba commit 8192792
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions ocaml/tests/suite_alcotest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ let () =
; ("Test_pvs_site", Test_pvs_site.test)
; ("Test_pvs_proxy", Test_pvs_proxy.test)
; ("Test_pvs_server", Test_pvs_server.test)
; ("Test_vif_helpers", Test_vif_helpers.test)
; ("Test_vm_memory_constraints", Test_vm_memory_constraints.test)
; ("Test_xapi_xenops", Test_xapi_xenops.test)
; ("Test_network_event_loop", Test_network_event_loop.test)
Expand Down
90 changes: 90 additions & 0 deletions ocaml/tests/test_vif_helpers.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
(*
* Copyright (C) Citrix Systems Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; version 2.1 only. with the special
* exception on linking described in file LICENSE.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*)

module T = Test_common

(* Function under test *)
let create ~__context ~device ~network ~vM ?(mAC = "00:00:00:00:00:00")
?(mTU = 1500L) ?(qos_algorithm_type = "") ?(qos_algorithm_params = [])
?(currently_attached = false) ?(other_config = [])
?(locking_mode = `unlocked) ?(ipv4_allowed = []) ?(ipv6_allowed = [])
?(ipv4_configuration_mode = `None) ?(ipv4_addresses = [])
?(ipv4_gateway = "") ?(ipv6_configuration_mode = `None)
?(ipv6_addresses = []) ?(ipv6_gateway = "") () =
Xapi_vif_helpers.create ~__context ~device ~network ~vM ~mAC ~mTU
~other_config ~qos_algorithm_type ~qos_algorithm_params ~currently_attached
~locking_mode ~ipv4_allowed ~ipv6_allowed ~ipv4_configuration_mode
~ipv4_addresses ~ipv4_gateway ~ipv6_configuration_mode ~ipv6_addresses
~ipv6_gateway

let test_create_ok () =
let __context = T.make_test_database () in
let vM = T.make_vm ~__context () in
let network = T.make_network ~__context () in
let vif = create ~__context ~device:"0" ~network ~vM () in
Alcotest.(check (Alcotest_comparators.ref ()))
"test_create_ok testing get_VM" vM
(Db.VIF.get_VM ~__context ~self:vif)

let test_create_duplicate_device () =
let __context = T.make_test_database () in
let vM = T.make_vm ~__context () in
let network = T.make_network ~__context () in
let _vif0 = T.make_vif ~__context ~device:"0" ~vM ~network () in
Alcotest.check_raises
"test_create_duplicate_device should raise device_already_exists"
Api_errors.(Server_error (device_already_exists, ["0"]))
@@ fun () ->
let _ = create ~__context ~device:"0" ~network ~vM () in
()

let test_create_with_pvs_proxy_ok () =
let __context = T.make_test_database () in
let vM = T.make_vm ~__context () in
let network = T.make_network ~__context () in
let vIF = T.make_vif ~__context ~device:"0" ~vM ~network () in
let _vIF2 = T.make_vif ~__context ~device:"2" ~vM ~network () in
let site = T.make_pvs_site ~__context () in
let _pvs_proxy = T.make_pvs_proxy ~__context ~site ~vIF () in
let vif1 = create ~__context ~device:"1" ~network ~vM () in
Alcotest.(check (Alcotest_comparators.ref ()))
"test_create_with_pvs_proxy_ok testing get_VM" vM
(Db.VIF.get_VM ~__context ~self:vif1)

let test_create_with_pvs_proxy_not_ok () =
let __context = T.make_test_database () in
let vM = T.make_vm ~__context () in
let network = T.make_network ~__context () in
let vIF = T.make_vif ~__context ~device:"1" ~vM ~network () in
let _vIF2 = T.make_vif ~__context ~device:"2" ~vM ~network () in
let site = T.make_pvs_site ~__context () in
let _pvs_proxy = T.make_pvs_proxy ~__context ~site ~vIF () in
Alcotest.check_raises
"test_create_with_pvs_proxy_not_ok should raise \
pvs_proxy_present_on_higher_vif_device"
Api_errors.(Server_error (pvs_proxy_present_on_higher_vif_device, ["1"]))
@@ fun () ->
let _ = create ~__context ~device:"0" ~network ~vM () in
()

let test =
[
("test_create_ok", `Quick, test_create_ok)
; ("test_create_duplicate_device", `Quick, test_create_duplicate_device)
; ("test_create_with_pvs_proxy_ok", `Quick, test_create_with_pvs_proxy_ok)
; ( "test_create_with_pvs_proxy_not_ok"
, `Quick
, test_create_with_pvs_proxy_not_ok
)
]

0 comments on commit 8192792

Please sign in to comment.