From 56eefe34d55e9b2375af606f7e9439573fed8ff1 Mon Sep 17 00:00:00 2001 From: Andrew Mayorov Date: Fri, 16 Feb 2024 13:33:59 +0100 Subject: [PATCH] feat(cth-cluster): use workdir as cwd on each node --- apps/emqx/test/emqx_cth_cluster.erl | 18 ++++++++++------ apps/emqx/test/emqx_cth_peer.erl | 33 ++++++++++------------------- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/apps/emqx/test/emqx_cth_cluster.erl b/apps/emqx/test/emqx_cth_cluster.erl index d02719c7e..28ee1f30f 100644 --- a/apps/emqx/test/emqx_cth_cluster.erl +++ b/apps/emqx/test/emqx_cth_cluster.erl @@ -332,11 +332,12 @@ allocate_listener_ports(Types, Spec) -> start_nodes_init(Specs, Timeout) -> Names = lists:map(fun(#{name := Name}) -> Name end, Specs), - Nodes = start_bare_nodes(Names, Timeout), - lists:foreach(fun node_init/1, Nodes). + _Nodes = start_bare_nodes(Names, Timeout), + lists:foreach(fun node_init/1, Specs). start_bare_nodes(Names) -> start_bare_nodes(Names, ?TIMEOUT_NODE_START_MS). + start_bare_nodes(Names, Timeout) -> Args = erl_flags(), Envs = [], @@ -355,7 +356,7 @@ start_bare_nodes(Names, Timeout) -> Nodes. deadline(Timeout) -> - erlang:monotonic_time() + erlang:convert_time_unit(Timeout, millisecond, nanosecond). + erlang:monotonic_time() + erlang:convert_time_unit(Timeout, millisecond, native). is_overdue(Deadline) -> erlang:monotonic_time() > Deadline. @@ -379,10 +380,15 @@ wait_boot_complete(Waits, Deadline) -> wait_boot_complete(Waits, Deadline) end. -node_init(Node) -> - % Make it possible to call `ct:pal` and friends (if running under rebar3) +node_init(#{name := Node, work_dir := WorkDir}) -> + %% Create exclusive current directory for the node. Some configurations, like plugin + %% installation directory, are the same for the whole cluster, and nodes on the same + %% machine will step on each other's toes... + ok = filelib:ensure_path(WorkDir), + ok = erpc:call(Node, file, set_cwd, [WorkDir]), + %% Make it possible to call `ct:pal` and friends (if running under rebar3) _ = share_load_module(Node, cthr), - % Enable snabbkaffe trace forwarding + %% Enable snabbkaffe trace forwarding ok = snabbkaffe:forward_trace(Node), when_cover_enabled(fun() -> {ok, _} = cover:start([Node]) end), ok. diff --git a/apps/emqx/test/emqx_cth_peer.erl b/apps/emqx/test/emqx_cth_peer.erl index 815b0a712..ca66b42b7 100644 --- a/apps/emqx/test/emqx_cth_peer.erl +++ b/apps/emqx/test/emqx_cth_peer.erl @@ -43,28 +43,17 @@ start_link(Name, Args, Envs, Timeout) when is_atom(Name) -> do_start(Name0, Args, Envs, Timeout, Func) when is_atom(Name0) -> {Name, Host} = parse_node_name(Name0), - %% Create exclusive current directory for the node. Some configurations, like plugin - %% installation directory, are the same for the whole cluster, and nodes on the same - %% machine will step on each other's toes... - {ok, Cwd} = file:get_cwd(), - NodeCwd = filename:join([Cwd, Name]), - ok = filelib:ensure_dir(filename:join([NodeCwd, "dummy"])), - try - file:set_cwd(NodeCwd), - {ok, Pid, Node} = peer:Func(#{ - name => Name, - host => Host, - args => Args, - env => Envs, - wait_boot => Timeout, - longnames => true, - shutdown => {halt, 1000} - }), - true = register(Node, Pid), - {ok, Node} - after - file:set_cwd(Cwd) - end. + {ok, Pid, Node} = peer:Func(#{ + name => Name, + host => Host, + args => Args, + env => Envs, + wait_boot => Timeout, + longnames => true, + shutdown => {halt, 1000} + }), + true = register(Node, Pid), + {ok, Node}. stop(Node) when is_atom(Node) -> Pid = whereis(Node),