Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions ocaml/tests/suite.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ open OUnit
let base_suite =
"base_suite" >:::
[
Test_basic.test;
Test_agility.test;
Test_helpers.test;
Test_datamodel_utils.test;
Test_daemon_manager.test;
Test_http.test;
Test_pool_db_backup.test;
Test_xapi_db_upgrade.test;
Expand Down
4 changes: 3 additions & 1 deletion ocaml/tests/suite_alcotest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ let () =
; "Test_host", Test_host.test
; "Test_vdi_cbt", Test_vdi_cbt.test
; "Test_db_lowlevel", Test_db_lowlevel.test
; "Test_vlan", Test_vlan.test;
; "Test_vlan", Test_vlan.test
; "Test_agility", Test_agility.test
; "Test_daemon_manager", Test_daemon_manager.test
]
9 changes: 3 additions & 6 deletions ocaml/tests/test_agility.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* GNU Lesser General Public License for more details.
*)

open OUnit
open Test_common

let test_vm_agility_with_vgpu () =
Expand All @@ -22,13 +21,11 @@ let test_vm_agility_with_vgpu () =
Agility.vm_assert_agile ~__context ~self:vm;
(* Create a VGPU - VM should no longer be agile. *)
let (_: API.ref_VGPU) = make_vgpu ~__context ~vM:vm () in
assert_raises_api_error
~args:[Ref.string_of vm]
Api_errors.vm_has_vgpu
Alcotest.check_raises "VM should no longer be agile"
Api_errors.(Server_error (vm_has_vgpu, [Ref.string_of vm]))
(fun () -> Agility.vm_assert_agile ~__context ~self:vm)

let test =
"test_agility" >:::
[
"test_vm_agility_with_vgpu" >:: test_vm_agility_with_vgpu;
"test_vm_agility_with_vgpu", `Quick, test_vm_agility_with_vgpu;
]
45 changes: 0 additions & 45 deletions ocaml/tests/test_basic.ml

This file was deleted.

53 changes: 24 additions & 29 deletions ocaml/tests/test_daemon_manager.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
* GNU Lesser General Public License for more details.
*)

open OUnit

type stop_failure = {
error: exn;
(** The exception thrown when trying to stop the daemon. *)
Expand Down Expand Up @@ -63,40 +61,40 @@ end

module Mock_manager = Daemon_manager.Make(Mock_daemon)

let check_times_called ~start ~stop =
Alcotest.(check int) "times_called_start" !Mock_daemon.times_called_start start;
Alcotest.(check int) "time_until_stopped" !Mock_daemon.times_called_stop stop

(* Test that the daemon is restarted, and that the return value of the function
passed to with_daemon_stopped is propagated. *)
let test_basic_operation () =
Mock_daemon.reset ~is_running:true;
let result = Mock_manager.with_daemon_stopped (fun () -> 123) in
assert_equal result 123;
assert_equal !Mock_daemon.times_called_start 1;
assert_equal !Mock_daemon.times_called_stop 1
Alcotest.(check int) "result" result 123;
check_times_called ~start:1 ~stop:1

(* Two sequential calls to with_daemon_stopped should restart the daemon
twice. *)
let test_two_restarts () =
Mock_daemon.reset ~is_running:true;
Mock_manager.with_daemon_stopped (fun () -> ());
Mock_manager.with_daemon_stopped (fun () -> ());
assert_equal !Mock_daemon.times_called_start 2;
assert_equal !Mock_daemon.times_called_stop 2
check_times_called ~start:2 ~stop:2

(* Test that if the daemon is stopped, calling with_daemon_stopped does not
attempt to stop or start it. *)
let test_already_stopped () =
Mock_daemon.reset ~is_running:false;
let result = Mock_manager.with_daemon_stopped (fun () -> 123) in
assert_equal result 123;
assert_equal !Mock_daemon.times_called_start 0;
assert_equal !Mock_daemon.times_called_stop 0
Alcotest.(check int) "result" result 123;
check_times_called ~start:0 ~stop:0

(* Test that an exception is propagated by with_daemon_stopped. *)
let test_exception () =
Mock_daemon.reset ~is_running:true;
assert_raises (Failure "fail")
Alcotest.check_raises "exception is propagated by with_daemon_stopped"
(Failure "fail")
(fun () -> Mock_manager.with_daemon_stopped (fun () -> failwith "fail"));
assert_equal !Mock_daemon.times_called_start 1;
assert_equal !Mock_daemon.times_called_stop 1
check_times_called ~start:1 ~stop:1

let spawn_threads_and_wait task count =
let rec spawn_threads task count acc =
Expand All @@ -117,8 +115,7 @@ let test_threads () =
Mock_manager.with_daemon_stopped (fun () -> Thread.delay 5.0)
in
spawn_threads_and_wait delay_thread 5;
assert_equal !Mock_daemon.times_called_start 1;
assert_equal !Mock_daemon.times_called_stop 1
check_times_called ~start:1 ~stop:1

(* The daemon initially fails to stop, but it stops within the timeout. *)
let test_timeout_succeed () =
Expand All @@ -128,8 +125,7 @@ let test_timeout_succeed () =
time_until_stopped = 2.0;
};
Mock_manager.with_daemon_stopped ~timeout:5.0 (fun () -> ());
assert_equal !Mock_daemon.times_called_start 1;
assert_equal !Mock_daemon.times_called_stop 1
check_times_called ~start:1 ~stop:1

(* The daemon does not stop within the timeout, so the exception is raised. *)
let test_timeout_fail () =
Expand All @@ -138,19 +134,18 @@ let test_timeout_fail () =
error = Failure "stop failed";
time_until_stopped = 5.0;
};
assert_raises (Failure "stop failed")
Alcotest.check_raises "does not stop within timeout"
(Failure "stop failed")
(fun () -> Mock_manager.with_daemon_stopped ~timeout:2.0 (fun () -> ()));
assert_equal !Mock_daemon.times_called_start 0;
assert_equal !Mock_daemon.times_called_stop 1
check_times_called ~start:0 ~stop:1

let test =
"daemon_manager" >:::
[
"test_basic_operation" >:: test_basic_operation;
"test_two_restarts" >:: test_two_restarts;
"test_already_stopped" >:: test_already_stopped;
"test_exception" >:: test_exception;
"test_threads" >:: test_threads;
"test_timeout_succeed" >:: test_timeout_succeed;
"test_timeout_fail" >:: test_timeout_fail;
"test_basic_operation", `Quick, test_basic_operation;
"test_two_restarts", `Quick, test_two_restarts;
"test_already_stopped", `Quick, test_already_stopped;
"test_exception", `Quick, test_exception;
"test_threads", `Slow, test_threads;
"test_timeout_succeed", `Slow, test_timeout_succeed;
"test_timeout_fail", `Slow, test_timeout_fail;
]