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

@ -600,52 +600,52 @@ delete_banned(Who) ->
export_rules() ->
lists:map(fun({_, RuleId, _, RawSQL, _, _, _, _, _, _, Actions, Enabled, Desc}) ->
[{id, RuleId},
{rawsql, RawSQL},
{actions, actions_to_prop_list(Actions)},
{enabled, Enabled},
{description, Desc}]
end, emqx_rule_registry:get_rules()).
[{id, RuleId},
{rawsql, RawSQL},
{actions, actions_to_prop_list(Actions)},
{enabled, Enabled},
{description, Desc}]
end, emqx_rule_registry:get_rules()).
export_resources() ->
lists:map(fun({_, Id, Type, Config, CreatedAt, Desc}) ->
NCreatedAt = case CreatedAt of
undefined -> null;
_ -> CreatedAt
end,
[{id, Id},
{type, Type},
{config, maps:to_list(Config)},
{created_at, NCreatedAt},
{description, Desc}]
end, emqx_rule_registry:get_resources()).
NCreatedAt = case CreatedAt of
undefined -> null;
_ -> CreatedAt
end,
[{id, Id},
{type, Type},
{config, maps:to_list(Config)},
{created_at, NCreatedAt},
{description, Desc}]
end, emqx_rule_registry:get_resources()).
export_blacklist() ->
lists:map(fun(#banned{who = Who, by = By, reason = Reason, at = At, until = Until}) ->
NWho = case Who of
{peerhost, Peerhost} -> {peerhost, inet:ntoa(Peerhost)};
_ -> Who
end,
[{who, [NWho]}, {by, By}, {reason, Reason}, {at, At}, {until, Until}]
end, ets:tab2list(emqx_banned)).
NWho = case Who of
{peerhost, Peerhost} -> {peerhost, inet:ntoa(Peerhost)};
_ -> Who
end,
[{who, [NWho]}, {by, By}, {reason, Reason}, {at, At}, {until, Until}]
end, ets:tab2list(emqx_banned)).
export_applications() ->
lists:map(fun({_, AppID, AppSecret, Name, Desc, Status, Expired}) ->
[{id, AppID}, {secret, AppSecret}, {name, Name}, {desc, Desc}, {status, Status}, {expired, Expired}]
end, ets:tab2list(mqtt_app)).
[{id, AppID}, {secret, AppSecret}, {name, Name}, {desc, Desc}, {status, Status}, {expired, Expired}]
end, ets:tab2list(mqtt_app)).
export_users() ->
lists:map(fun({_, Username, Password, Tags}) ->
[{username, Username}, {password, base64:encode(Password)}, {tags, Tags}]
end, ets:tab2list(mqtt_admin)).
[{username, Username}, {password, base64:encode(Password)}, {tags, Tags}]
end, ets:tab2list(mqtt_admin)).
export_auth_mnesia() ->
case ets:info(emqx_user) of
undefined -> [];
_ ->
lists:map(fun({_, {Type, Login}, Password, CreatedAt}) ->
[{login, Login}, {type, Type}, {password, base64:encode(Password)}, {created_at, CreatedAt}]
end, ets:tab2list(emqx_user))
[{login, Login}, {type, Type}, {password, base64:encode(Password)}, {created_at, CreatedAt}]
end, ets:tab2list(emqx_user))
end.
export_acl_mnesia() ->
@ -653,14 +653,14 @@ export_acl_mnesia() ->
undefined -> [];
_ ->
lists:map(fun({_, Filter, Action, Access, CreatedAt}) ->
Filter1 = case Filter of
{{Type, TypeValue}, Topic} ->
[{type, Type}, {type_value, TypeValue}, {topic, Topic}];
{Type, Topic} ->
[{type, Type}, {topic, Topic}]
end,
Filter1 ++ [{action, Action}, {access, Access}, {created_at, CreatedAt}]
end, ets:tab2list(emqx_acl))
Filter1 = case Filter of
{{Type, TypeValue}, Topic} ->
[{type, Type}, {type_value, TypeValue}, {topic, Topic}];
{Type, Topic} ->
[{type, Type}, {topic, Topic}]
end,
Filter1 ++ [{action, Action}, {access, Access}, {created_at, CreatedAt}]
end, ets:tab2list(emqx_acl))
end.
import_rules(Rules) ->

View File

@ -174,18 +174,15 @@ do_import(Filename) ->
case lists:member(Version, ?VERSIONS) of
true ->
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_rules(maps:get(<<"rules">>, Data, [])),
emqx_mgmt:import_blacklist(maps:get(<<"blacklist">>, Data, [])),
emqx_mgmt:import_applications(maps:get(<<"apps">>, 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_username(maps:get(<<"auth_username">>, Data, [])),
_ = 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_schemas(maps:get(<<"schemas">>, Data, [])),
logger:debug("The emqx data has been imported successfully"),
ok
catch Class:Reason:Stack ->

View File

@ -53,16 +53,16 @@ groups() ->
]}].
apps() ->
[emqx, emqx_management, emqx_reloader].
[emqx, emqx_management, emqx_auth_mnesia].
init_per_suite(Config) ->
ekka_mnesia:start(),
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.
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) ->
{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",
atom_to_list(Id), Level]))
end, ?LOG_LEVELS)
|| #{id := Id} <- emqx_logger:get_log_handlers()].
|| #{id := Id} <- emqx_logger:get_log_handlers()],
meck:unload().
t_mgmt_cmd(_) ->
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(["update", "emqx_appid", "ts"]), "update successfully")),
?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(_) ->
ct:pal("start testing status command"),
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(_) ->
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(["stats"]), "subscriptions.shared")),
?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(_) ->
ct:pal("start testing the client command"),
@ -164,7 +168,8 @@ t_clients_cmd(_) ->
{binary, Bin} = rfc6455_client:recv(WS),
{ok, Connack, <<>>, _} = raw_recv_pase(Bin),
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"]),
% timer:sleep(500),
% ?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, "process")) || Result <- emqx_mgmt_cli:vm(["process"])],
[?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(_) ->
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(["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")),
logger:set_primary_config(level, error).
logger:set_primary_config(level, error),
meck:unload().
t_router_cmd(_) ->
ct:pal("start testing the router command"),
@ -222,7 +229,8 @@ t_router_cmd(_) ->
emqtt:connect(T1),
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(["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(_) ->
ct:pal("Start testing the subscriptions command"),
@ -237,12 +245,14 @@ t_subscriptions_cmd(_) ->
[?assertMatch({match, _} , re:run(Result, "b/b/c"))
|| 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(["del", "client", "b/b/c"]), "ok~n").
?assertEqual(emqx_mgmt_cli:subscriptions(["del", "client", "b/b/c"]), "ok~n"),
meck:unload().
t_listeners_cmd(_) ->
print_mock(),
?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(_) ->
print_mock(),
@ -251,9 +261,10 @@ t_plugins_cmd(_) ->
meck:expect(emqx_plugins, unload, fun(_) -> ok end),
meck:expect(emqx_plugins, reload, fun(_) -> ok end),
?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(["load", "emqx_reloader"]),"Plugin emqx_reloader 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_auth_mnesia"]), "Plugin emqx_auth_mnesia unloaded 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"),
meck:unload().
t_modules_cmd(_) ->
print_mock(),
@ -263,7 +274,8 @@ t_modules_cmd(_) ->
meck:expect(emqx_modules, reload, fun(_) -> ok end),
?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(["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(_) ->
print_mock(),
@ -278,7 +290,8 @@ t_cli(_) ->
[?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, "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() ->
meck:new(emqx_ctl, [non_strict, passthrough]),

View File

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