From 81ff8c9c85b58f403b9322839ce79cec164f8d33 Mon Sep 17 00:00:00 2001 From: Gabor Igloi Date: Thu, 11 Jan 2018 15:15:23 +0000 Subject: [PATCH 1/4] Add suite with alcotest And test Valid_ref_list using alcotest. Signed-off-by: Gabor Igloi --- Makefile | 3 ++- ocaml/xapi/jbuild | 12 +++++++++-- ocaml/xapi/suite.ml | 35 ++----------------------------- ocaml/xapi/suite_alcotest.ml | 9 ++++++++ ocaml/xapi/suite_init.ml | 26 +++++++++++++++++++++++ ocaml/xapi/test_valid_ref_list.ml | 30 +++++++++++++------------- xapi.opam | 5 ++++- 7 files changed, 67 insertions(+), 53 deletions(-) create mode 100644 ocaml/xapi/suite_alcotest.ml create mode 100644 ocaml/xapi/suite_init.ml diff --git a/Makefile b/Makefile index 367cc36b265..ca63cb633e2 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,8 @@ clean: jbuilder clean test: - jbuilder runtest --no-buffer + jbuilder runtest --no-buffer -j $$(getconf _NPROCESSORS_ONLN) + jbuilder build @runalcotest -j $$(getconf _NPROCESSORS_ONLN) doc: jbuilder build ocaml/doc/jsapi.exe ocaml/idl/datamodel_main.exe diff --git a/ocaml/xapi/jbuild b/ocaml/xapi/jbuild index 5083aac0619..4136be18bfd 100644 --- a/ocaml/xapi/jbuild +++ b/ocaml/xapi/jbuild @@ -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 @@ -119,4 +120,11 @@ let () = Printf.ksprintf Jbuild_plugin.V1.send {| (action (run ${<} -runner sequential -verbose true)) ) ) + +(alias + ((name runalcotest) + (deps (suite_alcotest.exe (files_recursively_in test_data))) + (action (run ${<})) + ) +) |} (flags rewriters) coverage_rewriter diff --git a/ocaml/xapi/suite.ml b/ocaml/xapi/suite.ml index cec1e003a26..398f77f9559 100644 --- a/ocaml/xapi/suite.ml +++ b/ocaml/xapi/suite.ml @@ -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 diff --git a/ocaml/xapi/suite_alcotest.ml b/ocaml/xapi/suite_alcotest.ml new file mode 100644 index 00000000000..ba4ad36b384 --- /dev/null +++ b/ocaml/xapi/suite_alcotest.ml @@ -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 + ] diff --git a/ocaml/xapi/suite_init.ml b/ocaml/xapi/suite_init.ml new file mode 100644 index 00000000000..8cdcba7649b --- /dev/null +++ b/ocaml/xapi/suite_init.ml @@ -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 diff --git a/ocaml/xapi/test_valid_ref_list.ml b/ocaml/xapi/test_valid_ref_list.ml index 53cd44412ba..9e1eddf849d 100644 --- a/ocaml/xapi/test_valid_ref_list.ml +++ b/ocaml/xapi/test_valid_ref_list.ml @@ -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 @@ -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 = @@ -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 = @@ -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 = @@ -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 = @@ -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 ] diff --git a/xapi.opam b/xapi.opam index 0588d0ec28b..3e153d24dea 100644 --- a/xapi.opam +++ b/xapi.opam @@ -6,7 +6,10 @@ bug-reports: "https://github.com/xapi-project/xen-api/issues" dev-repo: "https://github.com/xapi-project/xen-api.git" build: [[ "jbuilder" "build" "-p" name ]] -build-test: [[ "jbuilder" "runtest" "-p" name "-j" jobs ]] +build-test: [ + [ "jbuilder" "runtest" "-p" name "-j" jobs ] + [ "jbuilder" "build" "@runalcotest" "-j" jobs ] +] depends: [ "jbuilder" {build & >= "1.0+beta11"} From 43eaf1e9227e294c28927204b5dbe971404812cd Mon Sep 17 00:00:00 2001 From: Gabor Igloi Date: Fri, 12 Jan 2018 13:37:08 +0000 Subject: [PATCH 2/4] jbuilder: Use runtest for both test binaries Signed-off-by: Gabor Igloi --- Makefile | 1 - ocaml/xapi/jbuild | 2 +- xapi.opam | 5 +---- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index ca63cb633e2..62b219df76f 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,6 @@ clean: test: jbuilder runtest --no-buffer -j $$(getconf _NPROCESSORS_ONLN) - jbuilder build @runalcotest -j $$(getconf _NPROCESSORS_ONLN) doc: jbuilder build ocaml/doc/jsapi.exe ocaml/idl/datamodel_main.exe diff --git a/ocaml/xapi/jbuild b/ocaml/xapi/jbuild index 4136be18bfd..e43bc157006 100644 --- a/ocaml/xapi/jbuild +++ b/ocaml/xapi/jbuild @@ -122,7 +122,7 @@ let () = Printf.ksprintf Jbuild_plugin.V1.send {| ) (alias - ((name runalcotest) + ((name runtest) (deps (suite_alcotest.exe (files_recursively_in test_data))) (action (run ${<})) ) diff --git a/xapi.opam b/xapi.opam index 3e153d24dea..0588d0ec28b 100644 --- a/xapi.opam +++ b/xapi.opam @@ -6,10 +6,7 @@ bug-reports: "https://github.com/xapi-project/xen-api/issues" dev-repo: "https://github.com/xapi-project/xen-api.git" build: [[ "jbuilder" "build" "-p" name ]] -build-test: [ - [ "jbuilder" "runtest" "-p" name "-j" jobs ] - [ "jbuilder" "build" "@runalcotest" "-j" jobs ] -] +build-test: [[ "jbuilder" "runtest" "-p" name "-j" jobs ]] depends: [ "jbuilder" {build & >= "1.0+beta11"} From e2df08da9f76e99031b2241233dd9dc5fdb9cf76 Mon Sep 17 00:00:00 2001 From: Gabor Igloi Date: Mon, 22 Jan 2018 10:18:03 +0000 Subject: [PATCH 3/4] xapi.opam: add alcotest {test} dependency Signed-off-by: Gabor Igloi --- xapi.opam | 1 + 1 file changed, 1 insertion(+) diff --git a/xapi.opam b/xapi.opam index 0588d0ec28b..7c9c7cce0e5 100644 --- a/xapi.opam +++ b/xapi.opam @@ -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" From 0aa2e3136bdb44ab254141f182d9ccc6d80fd67f Mon Sep 17 00:00:00 2001 From: Gabor Igloi Date: Mon, 22 Jan 2018 12:00:51 +0000 Subject: [PATCH 4/4] .travis-opam-coverage.sh: install alcotest This is a required dependency for testing. We cannot use the "opam install --build-test" flag because it seems that transitively installs the test dependencies of xapi's dependencies, and we don't have all of those in xs-opam apparently. Signed-off-by: Gabor Igloi --- .travis-opam-coverage.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis-opam-coverage.sh b/.travis-opam-coverage.sh index 75647779a3b..4aef4a21496 100644 --- a/.travis-opam-coverage.sh +++ b/.travis-opam-coverage.sh @@ -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