test(management): refine the tests

This commit is contained in:
JianBo He 2020-12-11 18:15:44 +08:00 committed by JianBo He
parent 5d98944c94
commit bddacf5284
4 changed files with 83 additions and 74 deletions

View File

@ -174,18 +174,15 @@ do_import(Filename) ->
case lists:member(Version, ?VERSIONS) of case lists:member(Version, ?VERSIONS) of
true -> true ->
try try
%emqx_mgmt:import_confs(maps:get(<<"configs">>, Data, []), maps:get(<<"listeners_state">>, Data, [])),
emqx_mgmt:import_resources(maps:get(<<"resources">>, Data, [])), emqx_mgmt:import_resources(maps:get(<<"resources">>, Data, [])),
emqx_mgmt:import_rules(maps:get(<<"rules">>, Data, [])), emqx_mgmt:import_rules(maps:get(<<"rules">>, Data, [])),
emqx_mgmt:import_blacklist(maps:get(<<"blacklist">>, Data, [])), emqx_mgmt:import_blacklist(maps:get(<<"blacklist">>, Data, [])),
emqx_mgmt:import_applications(maps:get(<<"apps">>, Data, [])), emqx_mgmt:import_applications(maps:get(<<"apps">>, Data, [])),
emqx_mgmt:import_users(maps:get(<<"users">>, Data, [])), emqx_mgmt:import_users(maps:get(<<"users">>, Data, [])),
%emqx_mgmt:import_modules(maps:get(<<"modules">>, Data, [])),
_ = emqx_mgmt:import_auth_clientid(maps:get(<<"auth_clientid">>, Data, [])), _ = emqx_mgmt:import_auth_clientid(maps:get(<<"auth_clientid">>, Data, [])),
_ = emqx_mgmt:import_auth_username(maps:get(<<"auth_username">>, Data, [])), _ = emqx_mgmt:import_auth_username(maps:get(<<"auth_username">>, Data, [])),
_ = emqx_mgmt:import_auth_mnesia(maps:get(<<"auth_mnesia">>, Data, []), Version), _ = emqx_mgmt:import_auth_mnesia(maps:get(<<"auth_mnesia">>, Data, []), Version),
_ = emqx_mgmt:import_acl_mnesia(maps:get(<<"acl_mnesia">>, Data, []), Version), _ = emqx_mgmt:import_acl_mnesia(maps:get(<<"acl_mnesia">>, Data, []), Version),
%_ = emqx_mgmt:import_schemas(maps:get(<<"schemas">>, Data, [])),
logger:debug("The emqx data has been imported successfully"), logger:debug("The emqx data has been imported successfully"),
ok ok
catch Class:Reason:Stack -> catch Class:Reason:Stack ->

View File

@ -53,16 +53,16 @@ groups() ->
]}]. ]}].
apps() -> apps() ->
[emqx, emqx_management, emqx_reloader]. [emqx, emqx_management, emqx_auth_mnesia].
init_per_suite(Config) -> init_per_suite(Config) ->
ekka_mnesia:start(), ekka_mnesia:start(),
emqx_mgmt_auth:mnesia(boot), emqx_mgmt_auth:mnesia(boot),
emqx_ct_helpers:start_apps([emqx_management, emqx_reloader]), emqx_ct_helpers:start_apps([emqx_management, emqx_auth_mnesia]),
Config. Config.
end_per_suite(_Config) -> end_per_suite(_Config) ->
emqx_ct_helpers:stop_apps([emqx_management, emqx_reloader, emqx]). emqx_ct_helpers:stop_apps([emqx_management, emqx_auth_mnesia]).
t_app(_Config) -> t_app(_Config) ->
{ok, AppSecret} = emqx_mgmt_auth:add_app(<<"app_id">>, <<"app_name">>), {ok, AppSecret} = emqx_mgmt_auth:add_app(<<"app_id">>, <<"app_name">>),
@ -106,7 +106,8 @@ t_log_cmd(_) ->
?assertEqual(Level++"\n", emqx_mgmt_cli:log(["handlers", "set-level", ?assertEqual(Level++"\n", emqx_mgmt_cli:log(["handlers", "set-level",
atom_to_list(Id), Level])) atom_to_list(Id), Level]))
end, ?LOG_LEVELS) end, ?LOG_LEVELS)
|| #{id := Id} <- emqx_logger:get_log_handlers()]. || #{id := Id} <- emqx_logger:get_log_handlers()],
meck:unload().
t_mgmt_cmd(_) -> t_mgmt_cmd(_) ->
ct:pal("start testing the mgmt command"), ct:pal("start testing the mgmt command"),
@ -118,12 +119,14 @@ t_mgmt_cmd(_) ->
?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(["lookup", "emqx_appid"]), "app_id:")), ?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(["lookup", "emqx_appid"]), "app_id:")),
?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(["update", "emqx_appid", "ts"]), "update successfully")), ?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(["update", "emqx_appid", "ts"]), "update successfully")),
?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(["delete", "emqx_appid"]), "ok")), ?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(["delete", "emqx_appid"]), "ok")),
ok = emqx_mgmt_cli:mgmt(["list"]). ok = emqx_mgmt_cli:mgmt(["list"]),
meck:unload().
t_status_cmd(_) -> t_status_cmd(_) ->
ct:pal("start testing status command"), ct:pal("start testing status command"),
print_mock(), print_mock(),
?assertMatch({match, _}, re:run(emqx_mgmt_cli:status([]), "is running")). ?assertMatch({match, _}, re:run(emqx_mgmt_cli:status([]), "is running")),
meck:unload().
t_broker_cmd(_) -> t_broker_cmd(_) ->
ct:pal("start testing the broker command"), ct:pal("start testing the broker command"),
@ -131,7 +134,8 @@ t_broker_cmd(_) ->
?assertMatch({match, _}, re:run(emqx_mgmt_cli:broker([]), "sysdescr")), ?assertMatch({match, _}, re:run(emqx_mgmt_cli:broker([]), "sysdescr")),
?assertMatch({match, _}, re:run(emqx_mgmt_cli:broker(["stats"]), "subscriptions.shared")), ?assertMatch({match, _}, re:run(emqx_mgmt_cli:broker(["stats"]), "subscriptions.shared")),
?assertMatch({match, _}, re:run(emqx_mgmt_cli:broker(["metrics"]), "bytes.sent")), ?assertMatch({match, _}, re:run(emqx_mgmt_cli:broker(["metrics"]), "bytes.sent")),
?assertMatch({match, _}, re:run(emqx_mgmt_cli:broker([undefined]), "broker")). ?assertMatch({match, _}, re:run(emqx_mgmt_cli:broker([undefined]), "broker")),
meck:unload().
t_clients_cmd(_) -> t_clients_cmd(_) ->
ct:pal("start testing the client command"), ct:pal("start testing the client command"),
@ -164,7 +168,8 @@ t_clients_cmd(_) ->
{binary, Bin} = rfc6455_client:recv(WS), {binary, Bin} = rfc6455_client:recv(WS),
{ok, Connack, <<>>, _} = raw_recv_pase(Bin), {ok, Connack, <<>>, _} = raw_recv_pase(Bin),
timer:sleep(300), timer:sleep(300),
?assertMatch({match, _}, re:run(emqx_mgmt_cli:clients(["show", "client13"]), "client13")). ?assertMatch({match, _}, re:run(emqx_mgmt_cli:clients(["show", "client13"]), "client13")),
meck:unload().
% emqx_mgmt_cli:clients(["kick", "client13"]), % emqx_mgmt_cli:clients(["kick", "client13"]),
% timer:sleep(500), % timer:sleep(500),
% ?assertMatch({match, _}, re:run(emqx_mgmt_cli:clients(["show", "client13"]), "Not Found")). % ?assertMatch({match, _}, re:run(emqx_mgmt_cli:clients(["show", "client13"]), "Not Found")).
@ -183,7 +188,8 @@ t_vm_cmd(_) ->
[?assertMatch({match, _}, re:run(Result, "memory"))|| Result <- emqx_mgmt_cli:vm(["memory"])], [?assertMatch({match, _}, re:run(Result, "memory"))|| Result <- emqx_mgmt_cli:vm(["memory"])],
[?assertMatch({match, _}, re:run(Result, "process")) || Result <- emqx_mgmt_cli:vm(["process"])], [?assertMatch({match, _}, re:run(Result, "process")) || Result <- emqx_mgmt_cli:vm(["process"])],
[?assertMatch({match, _}, re:run(Result, "io")) || Result <- emqx_mgmt_cli:vm(["io"])], [?assertMatch({match, _}, re:run(Result, "io")) || Result <- emqx_mgmt_cli:vm(["io"])],
[?assertMatch({match, _}, re:run(Result, "ports")) || Result <- emqx_mgmt_cli:vm(["ports"])]. [?assertMatch({match, _}, re:run(Result, "ports")) || Result <- emqx_mgmt_cli:vm(["ports"])],
meck:unload().
t_trace_cmd(_) -> t_trace_cmd(_) ->
ct:pal("start testing the trace command"), ct:pal("start testing the trace command"),
@ -203,7 +209,8 @@ t_trace_cmd(_) ->
?assertMatch({match, _}, re:run(emqx_mgmt_cli:trace(["start", "topic", "a/b/c", "log/clientid_trace.log"]), "successfully")), ?assertMatch({match, _}, re:run(emqx_mgmt_cli:trace(["start", "topic", "a/b/c", "log/clientid_trace.log"]), "successfully")),
?assertMatch({match, _}, re:run(emqx_mgmt_cli:trace(["stop", "topic", "a/b/c"]), "successfully")), ?assertMatch({match, _}, re:run(emqx_mgmt_cli:trace(["stop", "topic", "a/b/c"]), "successfully")),
?assertMatch({match, _}, re:run(emqx_mgmt_cli:trace(["start", "topic", "a/b/c", "log/clientid_trace.log", "error"]), "successfully")), ?assertMatch({match, _}, re:run(emqx_mgmt_cli:trace(["start", "topic", "a/b/c", "log/clientid_trace.log", "error"]), "successfully")),
logger:set_primary_config(level, error). logger:set_primary_config(level, error),
meck:unload().
t_router_cmd(_) -> t_router_cmd(_) ->
ct:pal("start testing the router command"), ct:pal("start testing the router command"),
@ -222,7 +229,8 @@ t_router_cmd(_) ->
emqtt:connect(T1), emqtt:connect(T1),
emqtt:subscribe(T1, <<"a/b/c/d">>), emqtt:subscribe(T1, <<"a/b/c/d">>),
?assertMatch({match, _}, re:run(emqx_mgmt_cli:routes(["list"]), "a/b/c | a/b/c")), ?assertMatch({match, _}, re:run(emqx_mgmt_cli:routes(["list"]), "a/b/c | a/b/c")),
?assertMatch({match, _}, re:run(emqx_mgmt_cli:routes(["show", "a/b/c"]), "a/b/c")). ?assertMatch({match, _}, re:run(emqx_mgmt_cli:routes(["show", "a/b/c"]), "a/b/c")),
meck:unload().
t_subscriptions_cmd(_) -> t_subscriptions_cmd(_) ->
ct:pal("Start testing the subscriptions command"), ct:pal("Start testing the subscriptions command"),
@ -237,12 +245,14 @@ t_subscriptions_cmd(_) ->
[?assertMatch({match, _} , re:run(Result, "b/b/c")) [?assertMatch({match, _} , re:run(Result, "b/b/c"))
|| Result <- emqx_mgmt_cli:subscriptions(["show", <<"client">>])], || Result <- emqx_mgmt_cli:subscriptions(["show", <<"client">>])],
?assertEqual(emqx_mgmt_cli:subscriptions(["add", "client", "b/b/c", "0"]), "ok~n"), ?assertEqual(emqx_mgmt_cli:subscriptions(["add", "client", "b/b/c", "0"]), "ok~n"),
?assertEqual(emqx_mgmt_cli:subscriptions(["del", "client", "b/b/c"]), "ok~n"). ?assertEqual(emqx_mgmt_cli:subscriptions(["del", "client", "b/b/c"]), "ok~n"),
meck:unload().
t_listeners_cmd(_) -> t_listeners_cmd(_) ->
print_mock(), print_mock(),
?assertEqual(emqx_mgmt_cli:listeners([]), ok), ?assertEqual(emqx_mgmt_cli:listeners([]), ok),
?assertEqual(emqx_mgmt_cli:listeners(["stop", "wss", "8084"]), "Stop wss listener on 8084 successfully.\n"). ?assertEqual(emqx_mgmt_cli:listeners(["stop", "wss", "8084"]), "Stop wss listener on 8084 successfully.\n"),
meck:unload().
t_plugins_cmd(_) -> t_plugins_cmd(_) ->
print_mock(), print_mock(),
@ -251,9 +261,10 @@ t_plugins_cmd(_) ->
meck:expect(emqx_plugins, unload, fun(_) -> ok end), meck:expect(emqx_plugins, unload, fun(_) -> ok end),
meck:expect(emqx_plugins, reload, fun(_) -> ok end), meck:expect(emqx_plugins, reload, fun(_) -> ok end),
?assertEqual(emqx_mgmt_cli:plugins(["list"]), ok), ?assertEqual(emqx_mgmt_cli:plugins(["list"]), ok),
?assertEqual(emqx_mgmt_cli:plugins(["unload", "emqx_reloader"]), "Plugin emqx_reloader unloaded successfully.\n"), ?assertEqual(emqx_mgmt_cli:plugins(["unload", "emqx_auth_mnesia"]), "Plugin emqx_auth_mnesia unloaded successfully.\n"),
?assertEqual(emqx_mgmt_cli:plugins(["load", "emqx_reloader"]),"Plugin emqx_reloader loaded successfully.\n"), ?assertEqual(emqx_mgmt_cli:plugins(["load", "emqx_auth_mnesia"]),"Plugin emqx_auth_mnesia loaded successfully.\n"),
?assertEqual(emqx_mgmt_cli:plugins(["unload", "emqx_management"]), "Plugin emqx_management can not be unloaded.~n"). ?assertEqual(emqx_mgmt_cli:plugins(["unload", "emqx_management"]), "Plugin emqx_management can not be unloaded.~n"),
meck:unload().
t_modules_cmd(_) -> t_modules_cmd(_) ->
print_mock(), print_mock(),
@ -263,7 +274,8 @@ t_modules_cmd(_) ->
meck:expect(emqx_modules, reload, fun(_) -> ok end), meck:expect(emqx_modules, reload, fun(_) -> ok end),
?assertEqual(emqx_mgmt_cli:modules(["list"]), ok), ?assertEqual(emqx_mgmt_cli:modules(["list"]), ok),
?assertEqual(emqx_mgmt_cli:modules(["load", "emqx_mod_presence"]),"Module emqx_mod_presence loaded successfully.\n"), ?assertEqual(emqx_mgmt_cli:modules(["load", "emqx_mod_presence"]),"Module emqx_mod_presence loaded successfully.\n"),
?assertEqual(emqx_mgmt_cli:modules(["unload", "emqx_mod_presence"]), "Module emqx_mod_presence unloaded successfully.\n"). ?assertEqual(emqx_mgmt_cli:modules(["unload", "emqx_mod_presence"]), "Module emqx_mod_presence unloaded successfully.\n"),
meck:unload().
t_cli(_) -> t_cli(_) ->
print_mock(), print_mock(),
@ -278,7 +290,8 @@ t_cli(_) ->
[?assertMatch({match, _}, re:run(Value, "vm")) || Value <- emqx_mgmt_cli:vm([""])], [?assertMatch({match, _}, re:run(Value, "vm")) || Value <- emqx_mgmt_cli:vm([""])],
[?assertMatch({match, _}, re:run(Value, "mnesia")) || Value <- emqx_mgmt_cli:mnesia([""])], [?assertMatch({match, _}, re:run(Value, "mnesia")) || Value <- emqx_mgmt_cli:mnesia([""])],
[?assertMatch({match, _}, re:run(Value, "trace")) || Value <- emqx_mgmt_cli:trace([""])], [?assertMatch({match, _}, re:run(Value, "trace")) || Value <- emqx_mgmt_cli:trace([""])],
[?assertMatch({match, _}, re:run(Value, "mgmt")) || Value <- emqx_mgmt_cli:mgmt([""])]. [?assertMatch({match, _}, re:run(Value, "mgmt")) || Value <- emqx_mgmt_cli:mgmt([""])],
meck:unload().
print_mock() -> print_mock() ->
meck:new(emqx_ctl, [non_strict, passthrough]), meck:new(emqx_ctl, [non_strict, passthrough]),

View File

@ -23,8 +23,7 @@
-include_lib("emqx/include/emqx.hrl"). -include_lib("emqx/include/emqx.hrl").
-include_lib("emqx/include/emqx_mqtt.hrl"). -include_lib("emqx/include/emqx_mqtt.hrl").
-include_lib("emqx_management/include/emqx_mgmt.hrl").
-include("emqx_mgmt.hrl").
-define(CONTENT_TYPE, "application/x-www-form-urlencoded"). -define(CONTENT_TYPE, "application/x-www-form-urlencoded").
@ -57,13 +56,13 @@ groups() ->
data]}]. data]}].
init_per_suite(Config) -> init_per_suite(Config) ->
emqx_ct_helpers:start_apps([emqx, emqx_management, emqx_reloader]), emqx_ct_helpers:start_apps([emqx, emqx_management, emqx_auth_mnesia]),
ekka_mnesia:start(), ekka_mnesia:start(),
emqx_mgmt_auth:mnesia(boot), emqx_mgmt_auth:mnesia(boot),
Config. Config.
end_per_suite(_Config) -> end_per_suite(_Config) ->
emqx_ct_helpers:stop_apps([emqx_reloader, emqx_management, emqx]), emqx_ct_helpers:stop_apps([emqx_auth_mnesia, emqx_management, emqx]),
ekka_mnesia:ensure_stopped(). ekka_mnesia:ensure_stopped().
init_per_testcase(data, Config) -> init_per_testcase(data, Config) ->
@ -154,11 +153,11 @@ apps(_) ->
auth_header_(), #{<<"app_id">> => AppId, auth_header_(), #{<<"app_id">> => AppId,
<<"name">> => <<"test">>, <<"name">> => <<"test">>,
<<"status">> => true}), <<"status">> => true}),
?assertEqual(?ERROR2, get(<<"code">>, Error1)), ?assertMatch(<<"undefined">>, get(<<"message">>, Error1)),
meck:expect(emqx_mgmt_auth, del_app, 1, fun(_) -> {error, undefined} end), meck:expect(emqx_mgmt_auth, del_app, 1, fun(_) -> {error, undefined} end),
{ok, Error2} = request_api(delete, api_path(["apps", binary_to_list(AppId)]), auth_header_()), {ok, Error2} = request_api(delete, api_path(["apps", binary_to_list(AppId)]), auth_header_()),
?assertEqual(?ERROR2, get(<<"code">>, Error2)), ?assertMatch(<<"undefined">>, get(<<"message">>, Error2)),
meck:unload(emqx_mgmt_auth), meck:unload(emqx_mgmt_auth),
{ok, NoApp} = request_api(get, api_path(["apps", binary_to_list(AppId)]), auth_header_()), {ok, NoApp} = request_api(get, api_path(["apps", binary_to_list(AppId)]), auth_header_()),
@ -329,54 +328,54 @@ nodes(_) ->
plugins(_) -> plugins(_) ->
{ok, Plugins1} = request_api(get, api_path(["plugins"]), auth_header_()), {ok, Plugins1} = request_api(get, api_path(["plugins"]), auth_header_()),
[Plugins11] = filter(get(<<"data">>, Plugins1), <<"node">>, atom_to_binary(node(), utf8)), [Plugins11] = filter(get(<<"data">>, Plugins1), <<"node">>, atom_to_binary(node(), utf8)),
[Plugin1] = filter(maps:get(<<"plugins">>, Plugins11), <<"name">>, <<"emqx_reloader">>), [Plugin1] = filter(maps:get(<<"plugins">>, Plugins11), <<"name">>, <<"emqx_auth_mnesia">>),
?assertEqual(<<"emqx_reloader">>, maps:get(<<"name">>, Plugin1)), ?assertEqual(<<"emqx_auth_mnesia">>, maps:get(<<"name">>, Plugin1)),
?assertEqual(true, maps:get(<<"active">>, Plugin1)), ?assertEqual(true, maps:get(<<"active">>, Plugin1)),
{ok, _} = request_api(put, {ok, _} = request_api(put,
api_path(["plugins", api_path(["plugins",
atom_to_list(emqx_reloader), atom_to_list(emqx_auth_mnesia),
"unload"]), "unload"]),
auth_header_()), auth_header_()),
{ok, Error1} = request_api(put, {ok, Error1} = request_api(put,
api_path(["plugins", api_path(["plugins",
atom_to_list(emqx_reloader), atom_to_list(emqx_auth_mnesia),
"unload"]), "unload"]),
auth_header_()), auth_header_()),
?assertEqual(<<"not_started">>, get(<<"message">>, Error1)), ?assertEqual(<<"not_started">>, get(<<"message">>, Error1)),
{ok, Plugins2} = request_api(get, {ok, Plugins2} = request_api(get,
api_path(["nodes", atom_to_list(node()), "plugins"]), api_path(["nodes", atom_to_list(node()), "plugins"]),
auth_header_()), auth_header_()),
[Plugin2] = filter(get(<<"data">>, Plugins2), <<"name">>, <<"emqx_reloader">>), [Plugin2] = filter(get(<<"data">>, Plugins2), <<"name">>, <<"emqx_auth_mnesia">>),
?assertEqual(<<"emqx_reloader">>, maps:get(<<"name">>, Plugin2)), ?assertEqual(<<"emqx_auth_mnesia">>, maps:get(<<"name">>, Plugin2)),
?assertEqual(false, maps:get(<<"active">>, Plugin2)), ?assertEqual(false, maps:get(<<"active">>, Plugin2)),
{ok, _} = request_api(put, {ok, _} = request_api(put,
api_path(["nodes", api_path(["nodes",
atom_to_list(node()), atom_to_list(node()),
"plugins", "plugins",
atom_to_list(emqx_reloader), atom_to_list(emqx_auth_mnesia),
"load"]), "load"]),
auth_header_()), auth_header_()),
{ok, Plugins3} = request_api(get, {ok, Plugins3} = request_api(get,
api_path(["nodes", atom_to_list(node()), "plugins"]), api_path(["nodes", atom_to_list(node()), "plugins"]),
auth_header_()), auth_header_()),
[Plugin3] = filter(get(<<"data">>, Plugins3), <<"name">>, <<"emqx_reloader">>), [Plugin3] = filter(get(<<"data">>, Plugins3), <<"name">>, <<"emqx_auth_mnesia">>),
?assertEqual(<<"emqx_reloader">>, maps:get(<<"name">>, Plugin3)), ?assertEqual(<<"emqx_auth_mnesia">>, maps:get(<<"name">>, Plugin3)),
?assertEqual(false, maps:get(<<"active">>, Plugin3)), ?assertEqual(false, maps:get(<<"active">>, Plugin3)),
{ok, _} = request_api(put, {ok, _} = request_api(put,
api_path(["nodes", api_path(["nodes",
atom_to_list(node()), atom_to_list(node()),
"plugins", "plugins",
atom_to_list(emqx_reloader), atom_to_list(emqx_auth_mnesia),
"unload"]), "unload"]),
auth_header_()), auth_header_()),
{ok, Error2} = request_api(put, {ok, Error2} = request_api(put,
api_path(["nodes", api_path(["nodes",
atom_to_list(node()), atom_to_list(node()),
"plugins", "plugins",
atom_to_list(emqx_reloader), atom_to_list(emqx_auth_mnesia),
"unload"]), "unload"]),
auth_header_()), auth_header_()),
?assertEqual(<<"not_started">>, get(<<"message">>, Error2)). ?assertEqual(<<"not_started">>, get(<<"message">>, Error2)).