diff --git a/ocaml/tests/suite.ml b/ocaml/tests/suite.ml index a6972bbd28e..f087a1b42b5 100644 --- a/ocaml/tests/suite.ml +++ b/ocaml/tests/suite.ml @@ -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; diff --git a/ocaml/tests/suite_alcotest.ml b/ocaml/tests/suite_alcotest.ml index 4b873e9b44c..59bdfd88c0e 100644 --- a/ocaml/tests/suite_alcotest.ml +++ b/ocaml/tests/suite_alcotest.ml @@ -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 ] diff --git a/ocaml/tests/test_agility.ml b/ocaml/tests/test_agility.ml index 64f702eaac7..58d4cc22fda 100644 --- a/ocaml/tests/test_agility.ml +++ b/ocaml/tests/test_agility.ml @@ -12,7 +12,6 @@ * GNU Lesser General Public License for more details. *) -open OUnit open Test_common let test_vm_agility_with_vgpu () = @@ -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; ] diff --git a/ocaml/tests/test_basic.ml b/ocaml/tests/test_basic.ml deleted file mode 100644 index 7f31db61977..00000000000 --- a/ocaml/tests/test_basic.ml +++ /dev/null @@ -1,45 +0,0 @@ -(* - * Copyright (C) 2006-2012 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. - *) - -open OUnit -open Test_common - -let test_always_pass () = assert_equal 1 1 -let test_always_fail () = skip "This will fail" ; assert_equal 1 0 - -let test_mock_db () = - let __context = Mock.make_context_with_new_db "Mock context" in - let blob_ref = Ref.make () in - Db.Blob.create __context blob_ref - (Uuid.to_string (Uuid.make_uuid ())) - "BLOB" "" 5L true (Stdext.Date.of_float 0.0) "" ; - ignore (Db.Blob.get_record ~__context ~self:blob_ref) ; - ignore (Db.VM.get_all_records ~__context) ; - let blob_name = Db.Blob.get_name_label ~__context ~self:blob_ref in - assert_equal blob_name "BLOB" - -let test_assert_licensed_storage_motion () = skip "TODO" ; - let __context = Mock.make_context_with_new_db "Mock context" in - let licensed = try Xapi_vm_migrate.assert_licensed_storage_motion ~__context; true - with _ -> false in - assert_bool "Not licensed for SXM" licensed - -let test = - "test_basic" >::: - [ - "test_always_pass" >:: test_always_pass ; - "test_always_fail" >:: test_always_fail ; - "test_mock_db" >:: test_mock_db ; - "test_assert_licensed_storage_motion" >:: test_assert_licensed_storage_motion ; - ] diff --git a/ocaml/tests/test_daemon_manager.ml b/ocaml/tests/test_daemon_manager.ml index bdf3d228206..e6919c096f9 100644 --- a/ocaml/tests/test_daemon_manager.ml +++ b/ocaml/tests/test_daemon_manager.ml @@ -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. *) @@ -63,14 +61,17 @@ 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. *) @@ -78,25 +79,22 @@ 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 = @@ -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 () = @@ -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 () = @@ -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; ]