test(mgmt): update and simplify testsuites setup
This commit is contained in:
parent
cffb52ab28
commit
6514659733
|
@ -37,13 +37,27 @@ end_per_suite(_) ->
|
||||||
%% cases
|
%% cases
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
t_cluster_query(_Config) ->
|
t_cluster_query(Config) ->
|
||||||
net_kernel:start(['master@127.0.0.1', longnames]),
|
net_kernel:start(['master@127.0.0.1', longnames]),
|
||||||
ct:timetrap({seconds, 120}),
|
ct:timetrap({seconds, 120}),
|
||||||
snabbkaffe:fix_ct_logging(),
|
snabbkaffe:fix_ct_logging(),
|
||||||
[{Name, Opts}, {Name1, Opts1}] = cluster_specs(),
|
ListenerConf = fun(Port) ->
|
||||||
Node1 = emqx_common_test_helpers:start_peer(Name, Opts),
|
io_lib:format(
|
||||||
Node2 = emqx_common_test_helpers:start_peer(Name1, Opts1),
|
"\n listeners.tcp.default.bind = ~p"
|
||||||
|
"\n listeners.ssl.default.enable = false"
|
||||||
|
"\n listeners.ws.default.enable = false"
|
||||||
|
"\n listeners.wss.default.enable = false",
|
||||||
|
[Port]
|
||||||
|
)
|
||||||
|
end,
|
||||||
|
Nodes =
|
||||||
|
[Node1, Node2] = emqx_cth_cluster:start(
|
||||||
|
[
|
||||||
|
{corenode1, #{role => core, apps => [{emqx, ListenerConf(2883)}, emqx_management]}},
|
||||||
|
{corenode2, #{role => core, apps => [{emqx, ListenerConf(3883)}, emqx_management]}}
|
||||||
|
],
|
||||||
|
#{work_dir => emqx_cth_suite:work_dir(?FUNCTION_NAME, Config)}
|
||||||
|
),
|
||||||
try
|
try
|
||||||
process_flag(trap_exit, true),
|
process_flag(trap_exit, true),
|
||||||
ClientLs1 = [start_emqtt_client(Node1, I, 2883) || I <- lists:seq(1, 10)],
|
ClientLs1 = [start_emqtt_client(Node1, I, 2883) || I <- lists:seq(1, 10)],
|
||||||
|
@ -168,13 +182,19 @@ t_cluster_query(_Config) ->
|
||||||
_ = lists:foreach(fun(C) -> emqtt:disconnect(C) end, ClientLs1),
|
_ = lists:foreach(fun(C) -> emqtt:disconnect(C) end, ClientLs1),
|
||||||
_ = lists:foreach(fun(C) -> emqtt:disconnect(C) end, ClientLs2)
|
_ = lists:foreach(fun(C) -> emqtt:disconnect(C) end, ClientLs2)
|
||||||
after
|
after
|
||||||
emqx_common_test_helpers:stop_peer(Node1),
|
emqx_cth_cluster:stop(Nodes)
|
||||||
emqx_common_test_helpers:stop_peer(Node2)
|
end.
|
||||||
end,
|
|
||||||
ok.
|
|
||||||
|
|
||||||
t_bad_rpc(_) ->
|
t_bad_rpc(Config) ->
|
||||||
emqx_mgmt_api_test_util:init_suite(),
|
Apps = emqx_cth_suite:start(
|
||||||
|
[
|
||||||
|
emqx,
|
||||||
|
emqx_management,
|
||||||
|
{emqx_dashboard, "dashboard.listeners.http { enable = true, bind = 18083 }"}
|
||||||
|
],
|
||||||
|
#{work_dir => emqx_cth_suite:work_dir(?FUNCTION_NAME, Config)}
|
||||||
|
),
|
||||||
|
{ok, _} = emqx_common_test_http:create_default_app(),
|
||||||
process_flag(trap_exit, true),
|
process_flag(trap_exit, true),
|
||||||
ClientLs1 = [start_emqtt_client(node(), I, 1883) || I <- lists:seq(1, 10)],
|
ClientLs1 = [start_emqtt_client(node(), I, 1883) || I <- lists:seq(1, 10)],
|
||||||
Path = emqx_mgmt_api_test_util:api_path(["clients?limit=2&page=2"]),
|
Path = emqx_mgmt_api_test_util:api_path(["clients?limit=2&page=2"]),
|
||||||
|
@ -187,35 +207,13 @@ t_bad_rpc(_) ->
|
||||||
after
|
after
|
||||||
_ = lists:foreach(fun(C) -> emqtt:disconnect(C) end, ClientLs1),
|
_ = lists:foreach(fun(C) -> emqtt:disconnect(C) end, ClientLs1),
|
||||||
meck:unload(emqx),
|
meck:unload(emqx),
|
||||||
emqx_mgmt_api_test_util:end_suite()
|
emqx_cth_suite:stop(Apps)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% helpers
|
%% helpers
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
cluster_specs() ->
|
|
||||||
Specs =
|
|
||||||
%% default listeners port
|
|
||||||
[
|
|
||||||
{core, corenode1, #{listener_ports => [{tcp, 2883}]}},
|
|
||||||
{core, corenode2, #{listener_ports => [{tcp, 3883}]}}
|
|
||||||
],
|
|
||||||
CommOpts =
|
|
||||||
[
|
|
||||||
{env, [{emqx, boot_modules, all}]},
|
|
||||||
{apps, []},
|
|
||||||
{conf, [
|
|
||||||
{[listeners, ssl, default, enable], false},
|
|
||||||
{[listeners, ws, default, enable], false},
|
|
||||||
{[listeners, wss, default, enable], false}
|
|
||||||
]}
|
|
||||||
],
|
|
||||||
emqx_common_test_helpers:emqx_cluster(
|
|
||||||
Specs,
|
|
||||||
CommOpts
|
|
||||||
).
|
|
||||||
|
|
||||||
start_emqtt_client(Node0, N, Port) ->
|
start_emqtt_client(Node0, N, Port) ->
|
||||||
Node = atom_to_binary(Node0),
|
Node = atom_to_binary(Node0),
|
||||||
ClientId = iolist_to_binary([Node, "-", integer_to_binary(N)]),
|
ClientId = iolist_to_binary([Node, "-", integer_to_binary(N)]),
|
||||||
|
|
|
@ -27,14 +27,19 @@
|
||||||
all() ->
|
all() ->
|
||||||
[
|
[
|
||||||
{group, with_defaults_in_file},
|
{group, with_defaults_in_file},
|
||||||
{group, without_defaults_in_file}
|
{group, without_defaults_in_file},
|
||||||
|
{group, max_connections}
|
||||||
].
|
].
|
||||||
|
|
||||||
groups() ->
|
groups() ->
|
||||||
AllTests = emqx_common_test_helpers:all(?MODULE),
|
AllTests = emqx_common_test_helpers:all(?MODULE),
|
||||||
|
MaxConnTests = [
|
||||||
|
t_max_connection_default
|
||||||
|
],
|
||||||
[
|
[
|
||||||
{with_defaults_in_file, AllTests},
|
{with_defaults_in_file, AllTests -- MaxConnTests},
|
||||||
{without_defaults_in_file, AllTests}
|
{without_defaults_in_file, AllTests -- MaxConnTests},
|
||||||
|
{max_connections, MaxConnTests}
|
||||||
].
|
].
|
||||||
|
|
||||||
init_per_suite(Config) ->
|
init_per_suite(Config) ->
|
||||||
|
@ -44,29 +49,39 @@ end_per_suite(_Config) ->
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
init_per_group(without_defaults_in_file, Config) ->
|
init_per_group(without_defaults_in_file, Config) ->
|
||||||
emqx_mgmt_api_test_util:init_suite([emqx_conf]),
|
init_group_apps(#{}, Config);
|
||||||
Config;
|
|
||||||
init_per_group(with_defaults_in_file, Config) ->
|
init_per_group(with_defaults_in_file, Config) ->
|
||||||
%% we have to materialize the config file with default values for this test group
|
%% we have to materialize the config file with default values for this test group
|
||||||
%% because we want to test the deletion of non-existing listener
|
%% because we want to test the deletion of non-existing listener
|
||||||
%% if there is no config file, the such deletion would result in a deletion
|
%% if there is no config file, the such deletion would result in a deletion
|
||||||
%% of the default listener.
|
%% of the default listener.
|
||||||
Name = atom_to_list(?MODULE) ++ "-default-listeners",
|
PrivDir = ?config(priv_dir, Config),
|
||||||
TmpConfFullPath = inject_tmp_config_content(Name, default_listeners_hocon_text()),
|
FileName = filename:join([PrivDir, "etc", atom_to_list(?MODULE) ++ "-default-listeners"]),
|
||||||
emqx_mgmt_api_test_util:init_suite([emqx_conf]),
|
ok = filelib:ensure_dir(FileName),
|
||||||
[{injected_conf_file, TmpConfFullPath} | Config].
|
ok = file:write_file(FileName, default_listeners_hocon_text()),
|
||||||
|
init_group_apps("include \"" ++ FileName ++ "\"", Config);
|
||||||
|
init_per_group(max_connections, Config) ->
|
||||||
|
init_group_apps(
|
||||||
|
io_lib:format("listeners.tcp.max_connection_test {bind = \"0.0.0.0:~p\"}", [?PORT]),
|
||||||
|
Config
|
||||||
|
).
|
||||||
|
|
||||||
end_per_group(Group, Config) ->
|
init_group_apps(Config, CTConfig) ->
|
||||||
emqx_conf:tombstone([listeners, tcp, new], #{override_to => cluster}),
|
Apps = emqx_cth_suite:start(
|
||||||
emqx_conf:tombstone([listeners, tcp, new1], #{override_to => local}),
|
[
|
||||||
case Group =:= with_defaults_in_file of
|
{emqx_conf, Config},
|
||||||
true ->
|
emqx_management,
|
||||||
{_, File} = lists:keyfind(injected_conf_file, 1, Config),
|
{emqx_dashboard, "dashboard.listeners.http { enable = true, bind = 18083 }"}
|
||||||
ok = file:delete(File);
|
],
|
||||||
false ->
|
#{
|
||||||
ok
|
work_dir => emqx_cth_suite:work_dir(CTConfig)
|
||||||
end,
|
}
|
||||||
emqx_mgmt_api_test_util:end_suite([emqx_conf]).
|
),
|
||||||
|
{ok, _} = emqx_common_test_http:create_default_app(),
|
||||||
|
[{suite_apps, Apps} | CTConfig].
|
||||||
|
|
||||||
|
end_per_group(_Group, Config) ->
|
||||||
|
ok = emqx_cth_suite:stop(?config(suite_apps, Config)).
|
||||||
|
|
||||||
init_per_testcase(Case, Config) ->
|
init_per_testcase(Case, Config) ->
|
||||||
try
|
try
|
||||||
|
@ -84,16 +99,6 @@ end_per_testcase(Case, Config) ->
|
||||||
ok
|
ok
|
||||||
end.
|
end.
|
||||||
|
|
||||||
t_max_connection_default({init, Config}) ->
|
|
||||||
emqx_mgmt_api_test_util:end_suite([emqx_conf]),
|
|
||||||
Port = integer_to_binary(?PORT),
|
|
||||||
Bin = <<"listeners.tcp.max_connection_test {bind = \"0.0.0.0:", Port/binary, "\"}">>,
|
|
||||||
TmpConfName = atom_to_list(?FUNCTION_NAME) ++ ".conf",
|
|
||||||
TmpConfFullPath = inject_tmp_config_content(TmpConfName, Bin),
|
|
||||||
emqx_mgmt_api_test_util:init_suite([emqx_conf]),
|
|
||||||
[{tmp_config_file, TmpConfFullPath} | Config];
|
|
||||||
t_max_connection_default({'end', Config}) ->
|
|
||||||
ok = file:delete(proplists:get_value(tmp_config_file, Config));
|
|
||||||
t_max_connection_default(Config) when is_list(Config) ->
|
t_max_connection_default(Config) when is_list(Config) ->
|
||||||
#{<<"listeners">> := Listeners} = emqx_mgmt_api_listeners:do_list_listeners(),
|
#{<<"listeners">> := Listeners} = emqx_mgmt_api_listeners:do_list_listeners(),
|
||||||
Target = lists:filter(
|
Target = lists:filter(
|
||||||
|
@ -189,13 +194,19 @@ t_wss_crud_listeners_by_id(Config) when is_list(Config) ->
|
||||||
crud_listeners_by_id(ListenerId, NewListenerId, MinListenerId, BadId, Type, 34000).
|
crud_listeners_by_id(ListenerId, NewListenerId, MinListenerId, BadId, Type, 34000).
|
||||||
|
|
||||||
t_api_listeners_list_not_ready(Config) when is_list(Config) ->
|
t_api_listeners_list_not_ready(Config) when is_list(Config) ->
|
||||||
net_kernel:start(['listeners@127.0.0.1', longnames]),
|
|
||||||
ct:timetrap({seconds, 120}),
|
ct:timetrap({seconds, 120}),
|
||||||
snabbkaffe:fix_ct_logging(),
|
Apps = [
|
||||||
Cluster = [{Name, Opts}, {Name1, Opts1}] = cluster([core, core]),
|
{emqx, #{after_start => fun() -> emqx_app:set_config_loader(emqx) end}},
|
||||||
ct:pal("Starting ~p", [Cluster]),
|
{emqx_conf, #{}}
|
||||||
Node1 = emqx_common_test_helpers:start_peer(Name, Opts),
|
],
|
||||||
Node2 = emqx_common_test_helpers:start_peer(Name1, Opts1),
|
Nodes =
|
||||||
|
[Node1, Node2] = emqx_cth_cluster:start(
|
||||||
|
[
|
||||||
|
{t_api_listeners_list_not_ready1, #{role => core, apps => Apps}},
|
||||||
|
{t_api_listeners_list_not_ready2, #{role => core, apps => Apps}}
|
||||||
|
],
|
||||||
|
#{work_dir => emqx_cth_suite:work_dir(?FUNCTION_NAME, Config)}
|
||||||
|
),
|
||||||
try
|
try
|
||||||
L1 = get_tcp_listeners(Node1),
|
L1 = get_tcp_listeners(Node1),
|
||||||
|
|
||||||
|
@ -214,8 +225,7 @@ t_api_listeners_list_not_ready(Config) when is_list(Config) ->
|
||||||
?assert(length(L1) > length(L2), Comment),
|
?assert(length(L1) > length(L2), Comment),
|
||||||
?assertEqual(length(L2), length(L3), Comment)
|
?assertEqual(length(L2), length(L3), Comment)
|
||||||
after
|
after
|
||||||
emqx_common_test_helpers:stop_peer(Node1),
|
emqx_cth_cluster:stop(Nodes)
|
||||||
emqx_common_test_helpers:stop_peer(Node2)
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
t_clear_certs(Config) when is_list(Config) ->
|
t_clear_certs(Config) when is_list(Config) ->
|
||||||
|
@ -296,25 +306,6 @@ assert_config_load_not_done(Node) ->
|
||||||
Prio = rpc:call(Node, emqx_app, get_config_loader, []),
|
Prio = rpc:call(Node, emqx_app, get_config_loader, []),
|
||||||
?assertEqual(emqx, Prio, #{node => Node}).
|
?assertEqual(emqx, Prio, #{node => Node}).
|
||||||
|
|
||||||
cluster(Specs) ->
|
|
||||||
Env = [
|
|
||||||
{emqx, boot_modules, []}
|
|
||||||
],
|
|
||||||
emqx_common_test_helpers:emqx_cluster(Specs, [
|
|
||||||
{env, Env},
|
|
||||||
{apps, [emqx_conf]},
|
|
||||||
{load_schema, false},
|
|
||||||
{env_handler, fun
|
|
||||||
(emqx) ->
|
|
||||||
application:set_env(emqx, boot_modules, []),
|
|
||||||
%% test init_config not ready.
|
|
||||||
emqx_app:set_config_loader(emqx),
|
|
||||||
ok;
|
|
||||||
(_) ->
|
|
||||||
ok
|
|
||||||
end}
|
|
||||||
]).
|
|
||||||
|
|
||||||
crud_listeners_by_id(ListenerId, NewListenerId, MinListenerId, BadId, Type, PortBase) ->
|
crud_listeners_by_id(ListenerId, NewListenerId, MinListenerId, BadId, Type, PortBase) ->
|
||||||
OriginPath = emqx_mgmt_api_test_util:api_path(["listeners", ListenerId]),
|
OriginPath = emqx_mgmt_api_test_util:api_path(["listeners", ListenerId]),
|
||||||
NewPath = emqx_mgmt_api_test_util:api_path(["listeners", NewListenerId]),
|
NewPath = emqx_mgmt_api_test_util:api_path(["listeners", NewListenerId]),
|
||||||
|
@ -529,15 +520,3 @@ default_listeners_hocon_text() ->
|
||||||
Listeners = hocon_tconf:make_serializable(Sc, #{}, #{}),
|
Listeners = hocon_tconf:make_serializable(Sc, #{}, #{}),
|
||||||
Config = #{<<"listeners">> => Listeners},
|
Config = #{<<"listeners">> => Listeners},
|
||||||
hocon_pp:do(Config, #{}).
|
hocon_pp:do(Config, #{}).
|
||||||
|
|
||||||
%% inject a 'include' at the end of emqx.conf.all
|
|
||||||
%% the 'include' can be kept after test,
|
|
||||||
%% as long as the file has been deleted it is a no-op
|
|
||||||
inject_tmp_config_content(TmpFile, Content) ->
|
|
||||||
Etc = filename:join(["etc", "emqx.conf.all"]),
|
|
||||||
Inc = filename:join(["etc", TmpFile]),
|
|
||||||
ConfFile = emqx_common_test_helpers:app_path(emqx_conf, Etc),
|
|
||||||
TmpFileFullPath = emqx_common_test_helpers:app_path(emqx_conf, Inc),
|
|
||||||
ok = file:write_file(TmpFileFullPath, Content),
|
|
||||||
ok = file:write_file(ConfFile, ["\ninclude \"", TmpFileFullPath, "\"\n"], [append]),
|
|
||||||
TmpFileFullPath.
|
|
||||||
|
|
|
@ -19,16 +19,25 @@
|
||||||
-compile(nowarn_export_all).
|
-compile(nowarn_export_all).
|
||||||
|
|
||||||
-include_lib("eunit/include/eunit.hrl").
|
-include_lib("eunit/include/eunit.hrl").
|
||||||
|
-include_lib("common_test/include/ct.hrl").
|
||||||
|
|
||||||
all() ->
|
all() ->
|
||||||
emqx_common_test_helpers:all(?MODULE).
|
emqx_common_test_helpers:all(?MODULE).
|
||||||
|
|
||||||
init_per_suite(Config) ->
|
init_per_suite(Config) ->
|
||||||
emqx_mgmt_api_test_util:init_suite([emqx_conf, emqx_management]),
|
Apps = emqx_cth_suite:start(
|
||||||
Config.
|
[
|
||||||
|
emqx_conf,
|
||||||
|
emqx_management,
|
||||||
|
{emqx_dashboard, "dashboard.listeners.http { enable = true, bind = 18083 }"}
|
||||||
|
],
|
||||||
|
#{work_dir => emqx_cth_suite:work_dir(Config)}
|
||||||
|
),
|
||||||
|
{ok, _} = emqx_common_test_http:create_default_app(),
|
||||||
|
[{suite_apps, Apps} | Config].
|
||||||
|
|
||||||
end_per_suite(_) ->
|
end_per_suite(Config) ->
|
||||||
emqx_mgmt_api_test_util:end_suite([emqx_management, emqx_conf]).
|
ok = emqx_cth_suite:stop(?config(suite_apps, Config)).
|
||||||
|
|
||||||
init_per_testcase(t_log_path, Config) ->
|
init_per_testcase(t_log_path, Config) ->
|
||||||
emqx_config_logger:add_handler(),
|
emqx_config_logger:add_handler(),
|
||||||
|
@ -121,16 +130,17 @@ t_node_metrics_api(_) ->
|
||||||
emqx_mgmt_api_test_util:request_api(get, BadNodePath)
|
emqx_mgmt_api_test_util:request_api(get, BadNodePath)
|
||||||
).
|
).
|
||||||
|
|
||||||
t_multiple_nodes_api(_) ->
|
t_multiple_nodes_api(Config) ->
|
||||||
net_kernel:start(['node_api@127.0.0.1', longnames]),
|
|
||||||
ct:timetrap({seconds, 120}),
|
ct:timetrap({seconds, 120}),
|
||||||
snabbkaffe:fix_ct_logging(),
|
snabbkaffe:fix_ct_logging(),
|
||||||
Seq1 = list_to_atom(atom_to_list(?MODULE) ++ "1"),
|
Nodes =
|
||||||
Seq2 = list_to_atom(atom_to_list(?MODULE) ++ "2"),
|
[Node1, Node2] = emqx_cth_cluster:start(
|
||||||
Cluster = [{Name, Opts}, {Name1, Opts1}] = cluster([{core, Seq1}, {core, Seq2}]),
|
[
|
||||||
ct:pal("Starting ~p", [Cluster]),
|
{t_multiple_nodes_api1, #{role => core, apps => [emqx_conf, emqx_management]}},
|
||||||
Node1 = emqx_common_test_helpers:start_peer(Name, Opts),
|
{t_multiple_nodes_api2, #{role => core, apps => [emqx_conf, emqx_management]}}
|
||||||
Node2 = emqx_common_test_helpers:start_peer(Name1, Opts1),
|
],
|
||||||
|
#{work_dir => emqx_cth_suite:work_dir(?FUNCTION_NAME, Config)}
|
||||||
|
),
|
||||||
try
|
try
|
||||||
{200, NodesList} = rpc:call(Node1, emqx_mgmt_api_nodes, nodes, [get, #{}]),
|
{200, NodesList} = rpc:call(Node1, emqx_mgmt_api_nodes, nodes, [get, #{}]),
|
||||||
All = [Node1, Node2],
|
All = [Node1, Node2],
|
||||||
|
@ -148,22 +158,6 @@ t_multiple_nodes_api(_) ->
|
||||||
]),
|
]),
|
||||||
?assertMatch(#{node := Node1}, Node11)
|
?assertMatch(#{node := Node1}, Node11)
|
||||||
after
|
after
|
||||||
emqx_common_test_helpers:stop_peer(Node1),
|
emqx_cth_cluster:stop(Nodes)
|
||||||
emqx_common_test_helpers:stop_peer(Node2)
|
|
||||||
end,
|
end,
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
cluster(Specs) ->
|
|
||||||
Env = [{emqx, boot_modules, []}],
|
|
||||||
emqx_common_test_helpers:emqx_cluster(Specs, [
|
|
||||||
{env, Env},
|
|
||||||
{apps, [emqx_conf, emqx_management]},
|
|
||||||
{load_schema, false},
|
|
||||||
{env_handler, fun
|
|
||||||
(emqx) ->
|
|
||||||
application:set_env(emqx, boot_modules, []),
|
|
||||||
ok;
|
|
||||||
(_) ->
|
|
||||||
ok
|
|
||||||
end}
|
|
||||||
]).
|
|
||||||
|
|
Loading…
Reference in New Issue