From 711a82c5fd156a4f9c095b2ce6a670d21e8d5191 Mon Sep 17 00:00:00 2001 From: Gabriel Buica Date: Tue, 6 Feb 2024 13:24:57 +0000 Subject: [PATCH] CP-46157: Add unit test for `observed_components_of` Adds a unit test for `observed_components_of`. Tests over a list of inputs fot `observed_components_of_inputs` while changing the value of `Xapi_globs.observer_experimental_components` with: `no_exp_comp` -- empty `StringSet`; `smapi_exp_comp` -- singleton `StringSet` that represents smapi. Signed-off-by: Gabriel Buica --- ocaml/tests/test_observer.ml | 55 ++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/ocaml/tests/test_observer.ml b/ocaml/tests/test_observer.ml index 69c2f754c93..165bc4afd1b 100644 --- a/ocaml/tests/test_observer.ml +++ b/ocaml/tests/test_observer.ml @@ -16,6 +16,19 @@ open Tracing module D = Debug.Make (struct let name = "test_observer" end) open D +module ComponentSet = Set.Make (Xapi_observer_components) + +let component_set_to_string cs = + cs + |> ComponentSet.to_seq + |> Seq.map Xapi_observer_components.to_string + |> List.of_seq + |> String.concat "," + +let component_set_testable = + let pp = Fmt.of_to_string component_set_to_string in + + Alcotest.testable pp ComponentSet.equal let () = Printexc.record_backtrace true @@ -515,6 +528,47 @@ let test_attribute_validation () = List.iter test_good_attribute good_attributes ; List.iter test_bad_attribute bad_attributes +let test_observed_components_of () = + let open Xapi_globs in + let open Xapi_observer_components in + let original_value = !observer_experimental_components in + let remove comp = List.filter (fun c -> comp <> c) in + let inputs = [[]; [SMApi]; [Xapi; SMApi]; all] in + + let expected_components_given_config_value = + [ + ( "No experimental component is expected" + , StringSet.empty + , [remove Xapi_clusterd all; [SMApi]; [Xapi; SMApi]; all] + ) + ; ( "SMapi is experimental component" + , StringSet.singleton Constants.observer_component_smapi + , [ + all |> remove Xapi_clusterd |> remove SMApi + ; [] + ; [Xapi] + ; remove SMApi all + ] + ) + ] + in + + let test_exp_comp (msg, v, expected_list) = + Xapi_globs.observer_experimental_components := v ; + let observed_components = List.map observed_components_of inputs in + List.iter2 + (fun expected received -> + Alcotest.(check component_set_testable) + msg + (ComponentSet.of_list expected) + (ComponentSet.of_list received) + ) + expected_list observed_components + in + + List.iter test_exp_comp expected_components_given_config_value ; + observer_experimental_components := original_value + let test = [ ( "test_observer_create_and_destroy" @@ -528,6 +582,7 @@ let test = ; ("test_hashtbl_leaks", `Quick, test_hashtbl_leaks) ; ("test_tracing_exn_backtraces", `Quick, test_tracing_exn_backtraces) ; ("test_attribute_validation", `Quick, test_attribute_validation) + ; ("test_observed_components_of", `Quick, test_observed_components_of) ] let () =