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
2 changes: 2 additions & 0 deletions .travis-opam-coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ sudo apt install -y libpci-dev
opam pin add --no-action xapi .
opam depext --yes xapi
opam install --deps-only xapi
# install the test dependencies
opam install -y alcotest

# build and test xapi with coverage, then submit the coverage information to coveralls

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ clean:
jbuilder clean

test:
jbuilder runtest --no-buffer
jbuilder runtest --no-buffer -j $$(getconf _NPROCESSORS_ONLN)

doc:
jbuilder build ocaml/doc/jsapi.exe ocaml/idl/datamodel_main.exe
Expand Down
12 changes: 10 additions & 2 deletions ocaml/xapi/jbuild
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ let () = Printf.ksprintf Jbuild_plugin.V1.send {|
)

(executables
((names (xapi_main suite quicktest))
(public_names (xapi - quicktestbin))
((names (xapi_main suite suite_alcotest quicktest))
(public_names (xapi - - quicktestbin))
(package xapi)
(flags (:standard -bin-annot %s -warn-error +a-3-4-6-9-27-28-29-52))
(libraries (
alcotest
opasswd
pam
pciutil
Expand Down Expand Up @@ -119,4 +120,11 @@ let () = Printf.ksprintf Jbuild_plugin.V1.send {|
(action (run ${<} -runner sequential -verbose true))
)
)

(alias
((name runtest)
(deps (suite_alcotest.exe (files_recursively_in test_data)))
(action (run ${<}))
)
)
|} (flags rewriters) coverage_rewriter
35 changes: 2 additions & 33 deletions ocaml/xapi/suite.ml
Original file line number Diff line number Diff line change
Expand Up @@ -67,44 +67,13 @@ let base_suite =
Test_vlan.test;
Test_xapi_vbd_helpers.test;
Test_sr_update_vdis.test;
Test_valid_ref_list.test;
Test_network_event_loop.test;
Test_network.test;
Test_pusb.test;
]

let handlers = [
"get_services", Http_svr.FdIO Xapi_services.get_handler;
"post_services", Http_svr.FdIO Xapi_services.post_handler;
"put_services", Http_svr.FdIO Xapi_services.put_handler;
"post_root", Http_svr.BufIO (Api_server.callback false);
"post_json", Http_svr.BufIO (Api_server.callback true);
"post_jsonrpc", Http_svr.BufIO Api_server.jsoncallback;
"post_remote_db_access",
Http_svr.BufIO Db_remote_cache_access_v1.handler;
"post_remote_db_access_v2",
Http_svr.BufIO Db_remote_cache_access_v2.handler;
]

let start_server handlers =
List.iter Xapi_http.add_handler handlers;
Xapi.listen_unix_socket "/tmp/xapi-test/xapi-unit-test-socket"

let harness_init () =
Printexc.record_backtrace true;
Xcp_client.use_switch := false;
Pool_role.set_pool_role_for_test ();
Xapi.register_callback_fns ();
start_server handlers

let harness_destroy () = ()

let () =
Printexc.record_backtrace true;
(* exceeds 4MB limit in Travis *)
Debug.disable ~level:Syslog.Debug "xapi";
Inventory.inventory_filename :=
Filename.concat Test_common.working_area "xcp-inventory";
harness_init ();
ounit2_of_ounit1 base_suite |> OUnit2.run_test_tt_main;
harness_destroy ();
Suite_init.harness_init ();
ounit2_of_ounit1 base_suite |> OUnit2.run_test_tt_main
9 changes: 9 additions & 0 deletions ocaml/xapi/suite_alcotest.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

let () =
Suite_init.harness_init ();
(* Alcotest hides the standard output of successful tests,
so we will probably not exceed the 4MB limit in Traivs *)
Debug.log_to_stdout ();
Alcotest.run "Base suite"
[ "Test_valid_ref_list", Test_valid_ref_list.test
]
26 changes: 26 additions & 0 deletions ocaml/xapi/suite_init.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

let handlers = [
"get_services", Http_svr.FdIO Xapi_services.get_handler;
"post_services", Http_svr.FdIO Xapi_services.post_handler;
"put_services", Http_svr.FdIO Xapi_services.put_handler;
"post_root", Http_svr.BufIO (Api_server.callback false);
"post_json", Http_svr.BufIO (Api_server.callback true);
"post_jsonrpc", Http_svr.BufIO Api_server.jsoncallback;
"post_remote_db_access",
Http_svr.BufIO Db_remote_cache_access_v1.handler;
"post_remote_db_access_v2",
Http_svr.BufIO Db_remote_cache_access_v2.handler;
]

let start_server handlers =
List.iter Xapi_http.add_handler handlers;
Xapi.listen_unix_socket "/tmp/xapi-test/xapi-unit-test-socket"

let harness_init () =
Printexc.record_backtrace true;
Inventory.inventory_filename :=
Filename.concat Test_common.working_area "xcp-inventory";
Xcp_client.use_switch := false;
Pool_role.set_pool_role_for_test ();
Xapi.register_callback_fns ();
start_server handlers
30 changes: 14 additions & 16 deletions ocaml/xapi/test_valid_ref_list.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

let assert_equal l1 l2 = Ounit_comparators.StringList.(assert_equal l1 l2)
let assert_equal = Alcotest.(check (list string)) "same lists"

let with_vm_list f () =
let __context = Mock.make_context_with_new_db "Mock context" in
Expand All @@ -18,9 +18,9 @@ let with_vm_list f () =
let test_exists =
with_vm_list (fun __context l ->
let f vm = Db.VM.get_name_label ~__context ~self:vm = "a" in
OUnit.assert_equal true (Valid_ref_list.exists f l);
Alcotest.(check bool) "true" true (Valid_ref_list.exists f l);
let f vm = Db.VM.get_name_label ~__context ~self:vm = "c" in
OUnit.assert_equal false (Valid_ref_list.exists f l)
Alcotest.(check bool) "false" false (Valid_ref_list.exists f l)
)

let test_valid_ref_filter =
Expand All @@ -40,7 +40,7 @@ let test_valid_ref_filter =
name = "a" || name = "d"
in
assert_equal [vm1; vm4] (Valid_ref_list.filter f l)
| _ -> failwith "The test list should contain 4 VMs"
| _ -> Alcotest.fail "The test list should contain 4 VMs"
)

let test_for_all =
Expand All @@ -49,12 +49,12 @@ let test_for_all =
let name = Db.VM.get_name_label ~__context ~self:vm in
name = "a" || name = "c"
in
OUnit.assert_equal false (Valid_ref_list.for_all f l);
Alcotest.(check bool) "false" false (Valid_ref_list.for_all f l);
let f vm =
let name = Db.VM.get_name_label ~__context ~self:vm in
name = "a" || name = "d"
in
OUnit.assert_equal true (Valid_ref_list.for_all f l)
Alcotest.(check bool) "true" true (Valid_ref_list.for_all f l)
)

let test_map =
Expand Down Expand Up @@ -88,7 +88,7 @@ let test_filter_map =
if n = "a" || n = "d" then Some n else None
in
assert_equal ["a"; "d"] (Valid_ref_list.filter_map f l)
| _ -> failwith "The test list should contain 4 VMs"
| _ -> Alcotest.fail "The test list should contain 4 VMs"
)

let test_iter =
Expand All @@ -102,13 +102,11 @@ let test_iter =
)

let test =
let ((>:::), (>::)) = OUnit.((>:::), (>::)) in
"test_valid_ref_list" >:::
[ "test_map" >:: test_map
; "test_exists" >:: test_exists
; "test_filter" >:: test_valid_ref_filter
; "test_for_all" >:: test_for_all
; "test_flat_map" >:: test_flat_map
; "test_filter_map" >:: test_filter_map
; "test_iter" >:: test_iter
[ "test_map", `Quick, test_map
; "test_exists", `Quick, test_exists
; "test_filter", `Quick, test_valid_ref_filter
; "test_for_all", `Quick, test_for_all
; "test_flat_map", `Quick, test_flat_map
; "test_filter_map", `Quick, test_filter_map
; "test_iter", `Quick, test_iter
]
1 change: 1 addition & 0 deletions xapi.opam
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ build-test: [[ "jbuilder" "runtest" "-p" name "-j" jobs ]]

depends: [
"jbuilder" {build & >= "1.0+beta11"}
"alcotest" {test}
"cdrom"
"fd-send-recv"
"mtime"
Expand Down