From dde5154d00239c8aa76bf8bb72b0717a9792a8e3 Mon Sep 17 00:00:00 2001 From: Nelson Vides Date: Thu, 13 Jun 2024 12:44:23 +0200 Subject: [PATCH] Rework integration test helper --- .../extra_code_paths/path1/dummy_helper.erl | 109 +++++++++++------- 1 file changed, 65 insertions(+), 44 deletions(-) diff --git a/integration_test/extra_code_paths/path1/dummy_helper.erl b/integration_test/extra_code_paths/path1/dummy_helper.erl index 6a3e82ac..89c55ede 100644 --- a/integration_test/extra_code_paths/path1/dummy_helper.erl +++ b/integration_test/extra_code_paths/path1/dummy_helper.erl @@ -1,7 +1,12 @@ -module(dummy_helper). --required_variable(#{name=>dummy_var, description=>"dummy_var", - default_value=>default_value}). +-include_lib("stdlib/include/assert.hrl"). + +-required_variable(#{name => dummy_var, + description => "dummy_var", + default_value => default_value}). + +-define(comment(Input), io_lib:format("Condition failed with last input ~p", Input)). %% amoc_dist testing function -export([test_amoc_dist/0]). @@ -11,65 +16,81 @@ test_amoc_dist() -> Master = amoc_cluster:master_node(), Slaves = amoc_cluster:slave_nodes(), %% check the status of the nodes - disabled = rpc:call(Master, amoc_controller, get_status, []), - [{running, #{scenario := dummy_scenario}} = rpc:call(Node, amoc_controller, get_status, []) - || Node <- Slaves], - %% check user ids - {N1, Nodes1, Ids1, Max1} = get_users_info(Slaves), - true = N1 > 0, - N1 = Max1, - Ids1 = lists:seq(1, N1), + ?assertEqual(disabled, get_status(Master)), + [ ?assertMatch({running, #{scenario := dummy_scenario}}, get_status(Node)) || Node <- Slaves], + %% check user ids, users have all been started at the first two nodes + Res1 = {N1, Max1, Nodes1, Ids1} = get_users_info(Slaves), + ?assert(N1 > 0), + ?assertEqual(N1, Max1, ?comment(Res1)), + ?assertEqual(Ids1, lists:seq(1, N1), ?comment(Res1)), [AddedNode] = Slaves -- Nodes1, %% add 20 users - {ok, _} = rpc:call(Master, amoc_dist, add, [20]), - timer:sleep(3000), - {N2, Nodes2, Ids2, Max2} = get_users_info(Slaves), - N2 = Max2, - Ids2 = lists:seq(1, N2), - [AddedNode] = Nodes2 -- Nodes1, - N2 = N1 + 20, + add_and_wait(Master, 20), + Res2 = {N2, Max2, Nodes2, Ids2} = get_users_info(Slaves), + ?assertEqual(N2, Max2, ?comment(Res2)), + ?assertEqual(Ids2, lists:seq(1, N2), ?comment(Res2)), + ?assertEqual([AddedNode], Nodes2 -- Nodes1, ?comment(Res2)), + ?assertEqual(N2, N1 + 20, ?comment(Res2)), %% remove 10 users - {ok, _} = rpc:call(Master, amoc_dist, remove, [10, true]), - timer:sleep(3000), - {N3, Nodes3, _Ids3, Max3} = get_users_info(Slaves), - Nodes2 = Nodes3, - Max3 = Max2, - N2 = N3 + 10, + remove_and_wait(Master, 10), + Res3 = {N3, Max3, Nodes3, _Ids3} = get_users_info(Slaves), + ?assertEqual(N2, N3 + 10, ?comment(Res3)), + ?assertEqual(Max2, Max3, ?comment(Res3)), + ?assertEqual(Nodes2, Nodes3, ?comment(Res3)), %% try to remove N3 users - {ok, Ret} = rpc:call(Master, amoc_dist, remove, [N3, true]), + Ret = remove_and_wait(Master, N3), RemovedN = lists:sum([N || {_, N} <- Ret]), - timer:sleep(3000), - {N4, Nodes4, Ids4, _Max4} = get_users_info(Slaves), - Nodes1 = Nodes4, - N3 = N4 + RemovedN, - true = RemovedN < N3, + Res4 = {N4, _Max4, Nodes4, Ids4} = get_users_info(Slaves), + ?assertEqual(N3, N4 + RemovedN, ?comment(Res4)), + ?assertEqual(Nodes1, Nodes4, ?comment(Res4)), + ?assert(RemovedN < N3), %% add 20 users - {ok, _} = rpc:call(Master, amoc_dist, add, [20]), - timer:sleep(3000), - {N5, Nodes5, Ids5, Max5} = get_users_info(Slaves), - Nodes2 = Nodes5, - Max5 = Max2 + 20, - N5 = N4 + 20, - true = Ids5 -- Ids4 =:= lists:seq(Max2 + 1, Max5), + add_and_wait(Master, 20), + Res5 = {N5, Max5, Nodes5, Ids5} = get_users_info(Slaves), + ?assertEqual(Nodes2, Nodes5, ?comment(Res5)), + ?assertEqual(Max5, Max2 + 20, ?comment(Res5)), + ?assertEqual(N5, N4 + 20, ?comment(Res5)), + ?assertEqual(Ids5 -- Ids4, lists:seq(Max2 + 1, Max5), ?comment(Res5)), %% terminate scenario - {ok,_} = rpc:call(Master, amoc_dist, stop, []), - timer:sleep(3000), - [{finished, dummy_scenario} = rpc:call(Node, amoc_controller, get_status, []) - || Node <- Slaves], + stop(Master), + [ ?assertEqual({finished, dummy_scenario}, get_status(Node)) || Node <- Slaves], %% return expected value amoc_dist_works_as_expected catch C:E:S -> - {error, {C, E, S}} + {C, E, S} end. +-spec get_users_info([node()]) -> + {NoRunningUsers :: non_neg_integer(), + MaxId :: non_neg_integer, + [node()], + [amoc_scenario:user_id()]}. get_users_info(SlaveNodes) -> Users = [{Node, Id} || - Node <- SlaveNodes, - {_Pid, Id} <- rpc:call(Node, amoc_users_sup, get_all_children, [])], + Node <- SlaveNodes, + {_Pid, Id} <- rpc:call(Node, amoc_users_sup, get_all_children, [])], Ids = lists:usort([Id || {_, Id} <- Users]), Nodes = lists:usort([Node || {Node, _} <- Users]), N = length(Ids), N = length(Users), MaxId = lists:max(Ids), - {N, Nodes, Ids, MaxId}. + {N, MaxId, Nodes, Ids}. + +add_and_wait(Master, Num) -> + {ok, Ret} = rpc:call(Master, amoc_dist, add, [Num]), + timer:sleep(3000), + Ret. + +remove_and_wait(Master, Num) -> + {ok, Ret} = rpc:call(Master, amoc_dist, remove, [Num, true]), + timer:sleep(3000), + Ret. + +stop(Master) -> + {ok, Ret} = rpc:call(Master, amoc_dist, stop, []), + timer:sleep(3000), + Ret. + +get_status(Node) -> + rpc:call(Node, amoc_controller, get_status, []).