test(meck): add meck unload after meck load

This commit is contained in:
zhanghongtong 2020-12-19 22:55:13 +08:00 committed by Shawn
parent 26758d48d1
commit 3ef08f9ce3
7 changed files with 205 additions and 122 deletions

View File

@ -25,7 +25,7 @@
-include_lib("common_test/include/ct.hrl"). -include_lib("common_test/include/ct.hrl").
-define(LOG_LEVELS,["debug", "error", "info"]). -define(LOG_LEVELS, ["debug", "error", "info"]).
-define(LOG_HANDLER_ID, [file, default]). -define(LOG_HANDLER_ID, [file, default]).
all() -> all() ->
@ -75,7 +75,8 @@ t_app(_Config) ->
emqx_mgmt_auth:del_app(<<"app_id">>), emqx_mgmt_auth:del_app(<<"app_id">>),
%% Use the default application secret %% Use the default application secret
application:set_env(emqx_management, application, [{default_secret, <<"public">>}]), application:set_env(emqx_management, application, [{default_secret, <<"public">>}]),
{ok, AppSecret1} = emqx_mgmt_auth:add_app(<<"app_id">>, <<"app_name">>, <<"app_desc">>, true, undefined), {ok, AppSecret1} = emqx_mgmt_auth:add_app(
<<"app_id">>, <<"app_name">>, <<"app_desc">>, true, undefined),
?assert(emqx_mgmt_auth:is_authorized(<<"app_id">>, AppSecret1)), ?assert(emqx_mgmt_auth:is_authorized(<<"app_id">>, AppSecret1)),
?assertEqual(AppSecret1, emqx_mgmt_auth:get_appsecret(<<"app_id">>)), ?assertEqual(AppSecret1, emqx_mgmt_auth:get_appsecret(<<"app_id">>)),
?assertEqual(AppSecret1, <<"public">>), ?assertEqual(AppSecret1, <<"public">>),
@ -84,7 +85,9 @@ t_app(_Config) ->
emqx_mgmt_auth:del_app(<<"app_id">>), emqx_mgmt_auth:del_app(<<"app_id">>),
application:set_env(emqx_management, application, []), application:set_env(emqx_management, application, []),
%% Specify the application secret %% Specify the application secret
{ok, AppSecret2} = emqx_mgmt_auth:add_app(<<"app_id">>, <<"app_name">>, <<"secret">>, <<"app_desc">>, true, undefined), {ok, AppSecret2} = emqx_mgmt_auth:add_app(
<<"app_id">>, <<"app_name">>, <<"secret">>,
<<"app_desc">>, true, undefined),
?assert(emqx_mgmt_auth:is_authorized(<<"app_id">>, AppSecret2)), ?assert(emqx_mgmt_auth:is_authorized(<<"app_id">>, AppSecret2)),
?assertEqual(AppSecret2, emqx_mgmt_auth:get_appsecret(<<"app_id">>)), ?assertEqual(AppSecret2, emqx_mgmt_auth:get_appsecret(<<"app_id">>)),
?assertEqual({<<"app_id">>, AppSecret2, <<"app_name">>, <<"app_desc">>, true, undefined}, ?assertEqual({<<"app_id">>, AppSecret2, <<"app_name">>, <<"app_desc">>, true, undefined},
@ -93,44 +96,49 @@ t_app(_Config) ->
ok. ok.
t_log_cmd(_) -> t_log_cmd(_) ->
print_mock(), mock_print(),
lists:foreach(fun(Level) -> lists:foreach(fun(Level) ->
emqx_mgmt_cli:log(["primary-level", Level]), emqx_mgmt_cli:log(["primary-level", Level]),
?assertEqual(Level++"\n", emqx_mgmt_cli:log(["primary-level"])) ?assertEqual(Level ++ "\n", emqx_mgmt_cli:log(["primary-level"]))
end, ?LOG_LEVELS), end, ?LOG_LEVELS),
lists:foreach(fun(Level) -> lists:foreach(fun(Level) ->
emqx_mgmt_cli:log(["set-level", Level]), emqx_mgmt_cli:log(["set-level", Level]),
?assertEqual(Level++"\n", emqx_mgmt_cli:log(["primary-level"])) ?assertEqual(Level ++ "\n", emqx_mgmt_cli:log(["primary-level"]))
end, ?LOG_LEVELS), end, ?LOG_LEVELS),
[lists:foreach(fun(Level) -> [lists:foreach(fun(Level) ->
?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(). meck:unload().
t_mgmt_cmd(_) -> t_mgmt_cmd(_) ->
ct:pal("start testing the mgmt command"), % ct:pal("start testing the mgmt command"),
print_mock(), mock_print(),
?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(["lookup", "emqx_appid"]), "Not Found.")), ?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(
?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(["delete", "emqx_appid"]), "ok")), ["lookup", "emqx_appid"]), "Not Found.")),
?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(["insert", "emqx_appid", "emqx_name"]), "AppSecret:")), ?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(
?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(["insert", "emqx_appid", "emqx_name"]), "Error:")), ["insert", "emqx_appid", "emqx_name"]), "AppSecret:")),
?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(["lookup", "emqx_appid"]), "app_id:")), ?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(
?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(["update", "emqx_appid", "ts"]), "update successfully")), ["insert", "emqx_appid", "emqx_name"]), "Error:")),
?assertMatch({match, _}, re:run(emqx_mgmt_cli:mgmt(["delete", "emqx_appid"]), "ok")), ?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(). meck:unload().
t_status_cmd(_) -> t_status_cmd(_) ->
ct:pal("start testing status command"), % ct:pal("start testing status command"),
print_mock(), mock_print(),
?assertMatch({match, _}, re:run(emqx_mgmt_cli:status([]), "is running")), ?assertMatch({match, _}, re:run(emqx_mgmt_cli:status([]), "is running")),
meck:unload(). meck:unload().
t_broker_cmd(_) -> t_broker_cmd(_) ->
ct:pal("start testing the broker command"), % ct:pal("start testing the broker command"),
print_mock(), mock_print(),
?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")),
@ -138,13 +146,13 @@ t_broker_cmd(_) ->
meck:unload(). meck:unload().
t_clients_cmd(_) -> t_clients_cmd(_) ->
ct:pal("start testing the client command"), % ct:pal("start testing the client command"),
print_mock(), mock_print(),
process_flag(trap_exit, true), process_flag(trap_exit, true),
{ok, T} = emqtt:start_link([{host, "localhost"}, {ok, T} = emqtt:start_link([{clientid, <<"client12">>},
{clientid, <<"client12">>},
{username, <<"testuser1">>}, {username, <<"testuser1">>},
{password, <<"pass1">>}]), {password, <<"pass1">>}
]),
{ok, _} = emqtt:connect(T), {ok, _} = emqtt:connect(T),
timer:sleep(300), timer:sleep(300),
emqx_mgmt_cli:clients(["list"]), emqx_mgmt_cli:clients(["list"]),
@ -153,8 +161,9 @@ t_clients_cmd(_) ->
timer:sleep(500), timer:sleep(500),
?assertMatch({match, _}, re:run(emqx_mgmt_cli:clients(["show", "client12"]), "Not Found")), ?assertMatch({match, _}, re:run(emqx_mgmt_cli:clients(["show", "client12"]), "Not Found")),
receive receive
{'EXIT', T, Reason} -> {'EXIT', T, _} ->
ct:pal("Connection closed: ~p~n", [Reason]) ok
% ct:pal("Connection closed: ~p~n", [Reason])
after after
500 -> 500 ->
erlang:error("Client is not kick") erlang:error("Client is not kick")
@ -181,64 +190,82 @@ raw_send_serialize(Packet) ->
emqx_frame:serialize(Packet). emqx_frame:serialize(Packet).
t_vm_cmd(_) -> t_vm_cmd(_) ->
ct:pal("start testing the vm command"), % ct:pal("start testing the vm command"),
print_mock(), mock_print(),
[[?assertMatch({match, _}, re:run(Result, Name)) || Result <- emqx_mgmt_cli:vm([Name])] || Name <- ["load", "memory", "process", "io", "ports"]], [[?assertMatch({match, _}, re:run(Result, Name))
[?assertMatch({match, _}, re:run(Result, "load")) || Result <- emqx_mgmt_cli:vm(["load"])], || Result <- emqx_mgmt_cli:vm([Name])]
[?assertMatch({match, _}, re:run(Result, "memory"))|| Result <- emqx_mgmt_cli:vm(["memory"])], || Name <- ["load", "memory", "process", "io", "ports"]],
[?assertMatch({match, _}, re:run(Result, "process")) || Result <- emqx_mgmt_cli:vm(["process"])], [?assertMatch({match, _}, re:run(Result, "load"))
[?assertMatch({match, _}, re:run(Result, "io")) || Result <- emqx_mgmt_cli:vm(["io"])], || Result <- emqx_mgmt_cli:vm(["load"])],
[?assertMatch({match, _}, re:run(Result, "ports")) || Result <- emqx_mgmt_cli:vm(["ports"])], [?assertMatch({match, _}, re:run(Result, "memory"))
meck:unload(). || 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"])],
unmock_print().
t_trace_cmd(_) -> t_trace_cmd(_) ->
ct:pal("start testing the trace command"), % ct:pal("start testing the trace command"),
print_mock(), mock_print(),
logger:set_primary_config(level, debug), logger:set_primary_config(level, debug),
{ok, T} = emqtt:start_link([{host, "localhost"}, {ok, T} = emqtt:start_link([{clientid, <<"client">>},
{clientid, <<"client">>}, {username, <<"testuser">>},
{username, <<"testuser">>}, {password, <<"pass">>}
{password, <<"pass">>} ]),
]),
emqtt:connect(T), emqtt:connect(T),
emqtt:subscribe(T, <<"a/b/c">>), emqtt:subscribe(T, <<"a/b/c">>),
?assertMatch({match, _}, re:run(emqx_mgmt_cli:trace(["start", "client", "client", "log/clientid_trace.log"]), "successfully")), Trace1 = emqx_mgmt_cli:trace(["start", "client", "client",
?assertMatch({match, _}, re:run(emqx_mgmt_cli:trace(["stop", "client", "client"]), "successfully")), "log/clientid_trace.log"]),
?assertMatch({match, _}, re:run(emqx_mgmt_cli:trace(["start", "client", "client", "log/clientid_trace.log", "error"]), "successfully")), ?assertMatch({match, _}, re:run(Trace1, "successfully")),
?assertMatch({match, _}, re:run(emqx_mgmt_cli:trace(["stop", "client", "client"]), "successfully")), Trace2 = emqx_mgmt_cli:trace(["stop", "client", "client"]),
?assertMatch({match, _}, re:run(emqx_mgmt_cli:trace(["start", "topic", "a/b/c", "log/clientid_trace.log"]), "successfully")), ?assertMatch({match, _}, re:run(Trace2, "successfully")),
?assertMatch({match, _}, re:run(emqx_mgmt_cli:trace(["stop", "topic", "a/b/c"]), "successfully")), Trace3 = emqx_mgmt_cli:trace(["start", "client", "client",
?assertMatch({match, _}, re:run(emqx_mgmt_cli:trace(["start", "topic", "a/b/c", "log/clientid_trace.log", "error"]), "successfully")), "log/clientid_trace.log",
"error"]),
?assertMatch({match, _}, re:run(Trace3, "successfully")),
Trace4 = emqx_mgmt_cli:trace(["stop", "client", "client"]),
?assertMatch({match, _}, re:run(Trace4, "successfully")),
Trace5 = emqx_mgmt_cli:trace(["start", "topic", "a/b/c",
"log/clientid_trace.log"]),
?assertMatch({match, _}, re:run(Trace5, "successfully")),
Trace6 = emqx_mgmt_cli:trace(["stop", "topic", "a/b/c"]),
?assertMatch({match, _}, re:run(Trace6, "successfully")),
Trace7 = emqx_mgmt_cli:trace(["start", "topic", "a/b/c",
"log/clientid_trace.log", "error"]),
?assertMatch({match, _}, re:run(Trace7, "successfully")),
logger:set_primary_config(level, error), logger:set_primary_config(level, error),
meck:unload(). unmock_print().
t_router_cmd(_) -> t_router_cmd(_) ->
ct:pal("start testing the router command"), % ct:pal("start testing the router command"),
print_mock(), mock_print(),
{ok, T} = emqtt:start_link([{host, "localhost"}, {ok, T} = emqtt:start_link([{clientid, <<"client1">>},
{clientid, <<"client1">>}, {username, <<"testuser1">>},
{username, <<"testuser1">>}, {password, <<"pass1">>}
{password, <<"pass1">>}]), ]),
emqtt:connect(T), emqtt:connect(T),
emqtt:subscribe(T, <<"a/b/c">>), emqtt:subscribe(T, <<"a/b/c">>),
{ok, T1} = emqtt:start_link([{host, "localhost"}, {ok, T1} = emqtt:start_link([{clientid, <<"client2">>},
{clientid, <<"client2">>}, {username, <<"testuser2">>},
{username, <<"testuser2">>}, {password, <<"pass2">>}
{password, <<"pass2">>} ]),
]),
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(). unmock_print().
t_subscriptions_cmd(_) -> t_subscriptions_cmd(_) ->
ct:pal("Start testing the subscriptions command"), % ct:pal("Start testing the subscriptions command"),
print_mock(), mock_print(),
{ok, T3} = emqtt:start_link([{host, "localhost"}, {ok, T3} = emqtt:start_link([{clientid, <<"client">>},
{clientid, <<"client">>},
{username, <<"testuser">>}, {username, <<"testuser">>},
{password, <<"pass">>}]), {password, <<"pass">>}
]),
{ok, _} = emqtt:connect(T3), {ok, _} = emqtt:connect(T3),
{ok, _, _} = emqtt:subscribe(T3, <<"b/b/c">>), {ok, _, _} = emqtt:subscribe(T3, <<"b/b/c">>),
timer:sleep(300), timer:sleep(300),
@ -246,56 +273,85 @@ t_subscriptions_cmd(_) ->
|| 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(). unmock_print().
t_listeners_cmd(_) -> t_listeners_cmd(_) ->
print_mock(), mock_print(),
?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(
meck:unload(). emqx_mgmt_cli:listeners(["stop", "wss", "8084"]),
"Stop wss listener on 8084 successfully.\n"
),
unmock_print().
t_plugins_cmd(_) -> t_plugins_cmd(_) ->
print_mock(), mock_print(),
meck:new(emqx_plugins, [non_strict, passthrough]), meck:new(emqx_plugins, [non_strict, passthrough]),
meck:expect(emqx_plugins, load, fun(_) -> ok end), meck:expect(emqx_plugins, load, fun(_) -> ok end),
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_auth_mnesia"]), "Plugin emqx_auth_mnesia unloaded successfully.\n"), ?assertEqual(
?assertEqual(emqx_mgmt_cli:plugins(["load", "emqx_auth_mnesia"]),"Plugin emqx_auth_mnesia loaded successfully.\n"), emqx_mgmt_cli:plugins(["unload", "emqx_auth_mnesia"]),
?assertEqual(emqx_mgmt_cli:plugins(["unload", "emqx_management"]), "Plugin emqx_management can not be unloaded.~n"), "Plugin emqx_auth_mnesia unloaded successfully.\n"
meck:unload(). ),
?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"
),
unmock_print().
t_modules_cmd(_) -> t_modules_cmd(_) ->
print_mock(), mock_print(),
meck:new(emqx_modules, [non_strict, passthrough]), meck:new(emqx_modules, [non_strict, passthrough]),
meck:expect(emqx_modules, load, fun(_) -> ok end), meck:expect(emqx_modules, load, fun(_) -> ok end),
meck:expect(emqx_modules, unload, fun(_) -> ok end), meck:expect(emqx_modules, unload, fun(_) -> ok end),
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"]),
?assertEqual(emqx_mgmt_cli:modules(["unload", "emqx_mod_presence"]), "Module emqx_mod_presence unloaded successfully.\n"), "Module emqx_mod_presence loaded successfully.\n"),
meck:unload(). ?assertEqual(emqx_mgmt_cli:modules(["unload", "emqx_mod_presence"]),
"Module emqx_mod_presence unloaded successfully.\n"),
unmock_print().
t_cli(_) -> t_cli(_) ->
print_mock(), mock_print(),
?assertMatch({match, _}, re:run(emqx_mgmt_cli:status([""]), "status")), ?assertMatch({match, _}, re:run(emqx_mgmt_cli:status([""]), "status")),
[?assertMatch({match, _}, re:run(Value, "broker")) || Value <- emqx_mgmt_cli:broker([""])], [?assertMatch({match, _}, re:run(Value, "broker"))
[?assertMatch({match, _}, re:run(Value, "cluster")) || Value <- emqx_mgmt_cli:cluster([""])], || Value <- emqx_mgmt_cli:broker([""])],
[?assertMatch({match, _}, re:run(Value, "clients")) || Value <- emqx_mgmt_cli:clients([""])], [?assertMatch({match, _}, re:run(Value, "cluster"))
[?assertMatch({match, _}, re:run(Value, "routes")) || Value <- emqx_mgmt_cli:routes([""])], || Value <- emqx_mgmt_cli:cluster([""])],
[?assertMatch({match, _}, re:run(Value, "subscriptions")) || Value <- emqx_mgmt_cli:subscriptions([""])], [?assertMatch({match, _}, re:run(Value, "clients"))
[?assertMatch({match, _}, re:run(Value, "plugins")) || Value <- emqx_mgmt_cli:plugins([""])], || Value <- emqx_mgmt_cli:clients([""])],
[?assertMatch({match, _}, re:run(Value, "listeners")) || Value <- emqx_mgmt_cli:listeners([""])], [?assertMatch({match, _}, re:run(Value, "routes"))
[?assertMatch({match, _}, re:run(Value, "vm")) || Value <- emqx_mgmt_cli:vm([""])], || Value <- emqx_mgmt_cli:routes([""])],
[?assertMatch({match, _}, re:run(Value, "mnesia")) || Value <- emqx_mgmt_cli:mnesia([""])], [?assertMatch({match, _}, re:run(Value, "subscriptions"))
[?assertMatch({match, _}, re:run(Value, "trace")) || Value <- emqx_mgmt_cli:trace([""])], || Value <- emqx_mgmt_cli:subscriptions([""])],
[?assertMatch({match, _}, re:run(Value, "mgmt")) || Value <- emqx_mgmt_cli:mgmt([""])], [?assertMatch({match, _}, re:run(Value, "plugins"))
meck:unload(). || Value <- emqx_mgmt_cli:plugins([""])],
[?assertMatch({match, _}, re:run(Value, "listeners"))
|| Value <- emqx_mgmt_cli:listeners([""])],
[?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([""])],
unmock_print().
print_mock() -> mock_print() ->
catch meck:unload(emqx_ctl),
meck:new(emqx_ctl, [non_strict, passthrough]), meck:new(emqx_ctl, [non_strict, passthrough]),
meck:expect(emqx_ctl, print, fun(Arg) -> emqx_ctl:format(Arg) end), meck:expect(emqx_ctl, print, fun(Arg) -> emqx_ctl:format(Arg) end),
meck:expect(emqx_ctl, print, fun(Msg, Arg) -> emqx_ctl:format(Msg, Arg) end), meck:expect(emqx_ctl, print, fun(Msg, Arg) -> emqx_ctl:format(Msg, Arg) end),
meck:expect(emqx_ctl, usage, fun(Usages) -> emqx_ctl:format_usage(Usages) end), meck:expect(emqx_ctl, usage, fun(Usages) -> emqx_ctl:format_usage(Usages) end),
meck:expect(emqx_ctl, usage, fun(Cmd, Descr) -> emqx_ctl:format_usage(Cmd, Descr) end). meck:expect(emqx_ctl, usage, fun(Cmd, Descr) -> emqx_ctl:format_usage(Cmd, Descr) end).
unmock_print() ->
meck:unload(emqx_ctl).

View File

@ -45,7 +45,7 @@ end_per_suite(_Config) ->
application:stop(emqx). application:stop(emqx).
cli_memory(_) -> cli_memory(_) ->
print_mock(), mock_print(),
Output = emqx_recon_cli:cmd(["memory"]), Output = emqx_recon_cli:cmd(["memory"]),
Zip = lists:zip(Output, [ ?output_patterns("usage/current") Zip = lists:zip(Output, [ ?output_patterns("usage/current")
, ?output_patterns("usage/max") , ?output_patterns("usage/max")
@ -58,10 +58,11 @@ cli_memory(_) ->
]), ]),
%ct:pal("=======~p", [Zip]), %ct:pal("=======~p", [Zip]),
[?assertMatch({match, _}, re:run(Line, Match, [{capture,all,list}])) [?assertMatch({match, _}, re:run(Line, Match, [{capture,all,list}]))
|| {Line, Match} <- Zip]. || {Line, Match} <- Zip],
unmock_print().
cli_allocated(_) -> cli_allocated(_) ->
print_mock(), mock_print(),
Output = emqx_recon_cli:cmd(["allocated"]), Output = emqx_recon_cli:cmd(["allocated"]),
Zip = lists:zip(Output, [ ?output_patterns("binary_alloc/current") Zip = lists:zip(Output, [ ?output_patterns("binary_alloc/current")
, ?output_patterns("driver_alloc/current") , ?output_patterns("driver_alloc/current")
@ -84,13 +85,15 @@ cli_allocated(_) ->
]), ]),
ct:pal("=======~p", [Zip]), ct:pal("=======~p", [Zip]),
[?assertMatch({match, _}, re:run(Line, Match, [{capture,all,list}])) [?assertMatch({match, _}, re:run(Line, Match, [{capture,all,list}]))
|| {Line, Match} <- Zip]. || {Line, Match} <- Zip],
unmock_print().
cli_bin_leak(_) -> cli_bin_leak(_) ->
print_mock(), mock_print(),
Output = emqx_recon_cli:cmd(["bin_leak"]), Output = emqx_recon_cli:cmd(["bin_leak"]),
[?assertMatch({match, _}, re:run(Line, "current_function", [{capture,all,list}])) [?assertMatch({match, _}, re:run(Line, "current_function", [{capture,all,list}]))
|| Line <- Output]. || Line <- Output],
unmock_print().
cli_node_stats(_) -> cli_node_stats(_) ->
emqx_recon_cli:cmd(["node_stats"]). emqx_recon_cli:cmd(["node_stats"]).
@ -109,9 +112,13 @@ start_apps(App, DataDir) ->
[application:set_env(App, Par, Value) || {Par, Value} <- Vals], [application:set_env(App, Par, Value) || {Par, Value} <- Vals],
application:ensure_all_started(App). application:ensure_all_started(App).
print_mock() -> mock_print() ->
catch meck:unload(emqx_ctl),
meck:new(emqx_ctl, [non_strict, passthrough]), meck:new(emqx_ctl, [non_strict, passthrough]),
meck:expect(emqx_ctl, print, fun(Arg) -> emqx_ctl:format(Arg) end), meck:expect(emqx_ctl, print, fun(Arg) -> emqx_ctl:format(Arg) end),
meck:expect(emqx_ctl, print, fun(Msg, Arg) -> emqx_ctl:format(Msg, Arg) end), meck:expect(emqx_ctl, print, fun(Msg, Arg) -> emqx_ctl:format(Msg, Arg) end),
meck:expect(emqx_ctl, usage, fun(Usages) -> emqx_ctl:format_usage(Usages) end). meck:expect(emqx_ctl, usage, fun(Usages) -> emqx_ctl:format_usage(Usages) end).
unmock_print() ->
meck:unload().

View File

@ -468,7 +468,7 @@ t_show_resource_type_api(_Config) ->
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
t_rules_cli(_Config) -> t_rules_cli(_Config) ->
print_mock(), mock_print(),
RCreate = emqx_rule_engine_cli:rules(["create", RCreate = emqx_rule_engine_cli:rules(["create",
"select * from \"t1\" where topic='t1'", "select * from \"t1\" where topic='t1'",
"[{\"name\":\"inspect\", \"params\": {\"arg1\": 1}}]", "[{\"name\":\"inspect\", \"params\": {\"arg1\": 1}}]",
@ -500,10 +500,11 @@ t_rules_cli(_Config) ->
RShow2 = emqx_rule_engine_cli:rules(["show", RuleId]), RShow2 = emqx_rule_engine_cli:rules(["show", RuleId]),
?assertMatch({match, _}, re:run(RShow2, "Cannot found")), ?assertMatch({match, _}, re:run(RShow2, "Cannot found")),
%ct:pal("RShow2 : ~p", [RShow2]), %ct:pal("RShow2 : ~p", [RShow2]),
unmock_print(),
ok. ok.
t_actions_cli(_Config) -> t_actions_cli(_Config) ->
print_mock(), mock_print(),
RList = emqx_rule_engine_cli:actions(["list"]), RList = emqx_rule_engine_cli:actions(["list"]),
?assertMatch({match, _}, re:run(RList, "inspect")), ?assertMatch({match, _}, re:run(RList, "inspect")),
%ct:pal("RList : ~p", [RList]), %ct:pal("RList : ~p", [RList]),
@ -511,10 +512,11 @@ t_actions_cli(_Config) ->
RShow = emqx_rule_engine_cli:actions(["show", "inspect"]), RShow = emqx_rule_engine_cli:actions(["show", "inspect"]),
?assertMatch({match, _}, re:run(RShow, "inspect")), ?assertMatch({match, _}, re:run(RShow, "inspect")),
%ct:pal("RShow : ~p", [RShow]), %ct:pal("RShow : ~p", [RShow]),
unmock_print(),
ok. ok.
t_resources_cli(_Config) -> t_resources_cli(_Config) ->
print_mock(), mock_print(),
RCreate = emqx_rule_engine_cli:resources(["create", "built_in", "{\"a\" : 1}", "-d", "test resource"]), RCreate = emqx_rule_engine_cli:resources(["create", "built_in", "{\"a\" : 1}", "-d", "test resource"]),
ResId = re:replace(re:replace(RCreate, "Resource\s", "", [{return, list}]), "\screated\n", "", [{return, list}]), ResId = re:replace(re:replace(RCreate, "Resource\s", "", [{return, list}]), "\screated\n", "", [{return, list}]),
@ -536,10 +538,11 @@ t_resources_cli(_Config) ->
RShow2 = emqx_rule_engine_cli:resources(["show", ResId]), RShow2 = emqx_rule_engine_cli:resources(["show", ResId]),
?assertMatch({match, _}, re:run(RShow2, "Cannot found")), ?assertMatch({match, _}, re:run(RShow2, "Cannot found")),
%ct:pal("RShow2 : ~p", [RShow2]), %ct:pal("RShow2 : ~p", [RShow2]),
unmock_print(),
ok. ok.
t_resource_types_cli(_Config) -> t_resource_types_cli(_Config) ->
print_mock(), mock_print(),
RList = emqx_rule_engine_cli:resource_types(["list"]), RList = emqx_rule_engine_cli:resource_types(["list"]),
?assertMatch({match, _}, re:run(RList, "built_in")), ?assertMatch({match, _}, re:run(RList, "built_in")),
%ct:pal("RList : ~p", [RList]), %ct:pal("RList : ~p", [RList]),
@ -547,6 +550,7 @@ t_resource_types_cli(_Config) ->
RShow = emqx_rule_engine_cli:resource_types(["show", "inspect"]), RShow = emqx_rule_engine_cli:resource_types(["show", "inspect"]),
?assertMatch({match, _}, re:run(RShow, "inspect")), ?assertMatch({match, _}, re:run(RShow, "inspect")),
%ct:pal("RShow : ~p", [RShow]), %ct:pal("RShow : ~p", [RShow]),
unmock_print(),
ok. ok.
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
@ -562,7 +566,7 @@ t_topic_func(_Config) ->
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
t_add_get_remove_rule(_Config) -> t_add_get_remove_rule(_Config) ->
print_mock(), mock_print(),
RuleId0 = <<"rule-debug-0">>, RuleId0 = <<"rule-debug-0">>,
ok = emqx_rule_registry:add_rule(make_simple_rule(RuleId0)), ok = emqx_rule_registry:add_rule(make_simple_rule(RuleId0)),
?assertMatch({ok, #rule{id = RuleId0}}, emqx_rule_registry:get_rule(RuleId0)), ?assertMatch({ok, #rule{id = RuleId0}}, emqx_rule_registry:get_rule(RuleId0)),
@ -575,6 +579,7 @@ t_add_get_remove_rule(_Config) ->
?assertMatch({ok, #rule{id = RuleId1}}, emqx_rule_registry:get_rule(RuleId1)), ?assertMatch({ok, #rule{id = RuleId1}}, emqx_rule_registry:get_rule(RuleId1)),
ok = emqx_rule_registry:remove_rule(Rule1), ok = emqx_rule_registry:remove_rule(Rule1),
?assertEqual(not_found, emqx_rule_registry:get_rule(RuleId1)), ?assertEqual(not_found, emqx_rule_registry:get_rule(RuleId1)),
unmock_print(),
ok. ok.
t_add_get_remove_rules(_Config) -> t_add_get_remove_rules(_Config) ->
@ -2435,7 +2440,7 @@ set_special_configs(emqx_rule_engine) ->
set_special_configs(_App) -> set_special_configs(_App) ->
ok. ok.
print_mock() -> mock_print() ->
catch meck:unload(emqx_ctl), catch meck:unload(emqx_ctl),
meck:new(emqx_ctl, [non_strict, passthrough]), meck:new(emqx_ctl, [non_strict, passthrough]),
meck:expect(emqx_ctl, print, fun(Arg) -> emqx_ctl:format(Arg) end), meck:expect(emqx_ctl, print, fun(Arg) -> emqx_ctl:format(Arg) end),
@ -2443,6 +2448,9 @@ print_mock() ->
meck:expect(emqx_ctl, usage, fun(Usages) -> emqx_ctl:format_usage(Usages) end), meck:expect(emqx_ctl, usage, fun(Usages) -> emqx_ctl:format_usage(Usages) end),
meck:expect(emqx_ctl, usage, fun(Cmd, Descr) -> emqx_ctl:format_usage(Cmd, Descr) end). meck:expect(emqx_ctl, usage, fun(Cmd, Descr) -> emqx_ctl:format_usage(Cmd, Descr) end).
unmock_print() ->
meck:unload(emqx_ctl).
t_load_providers(_) -> t_load_providers(_) ->
error('TODO'). error('TODO').

View File

@ -1,26 +1,35 @@
%% -*-: erlang -*-
[ [
{ {
elvis, elvis,
[ [
{config, {config,
[ [
{dirs => ["apps"], #{dirs => ["apps/**/src", "src"],
filter => "*.erl", filter => "*.erl",
ruleset => erl_files, ruleset => erl_files
# rules => [ %rules => [
# {elvis_style, max_module_length, #{}}, % {elvis_style, max_module_length, #{}},
# {elvis_style, no_common_caveats_call, #{}} % {elvis_style, no_common_caveats_call, #{}}
# ] % ]
}, },
{dirs => ["."], #{dirs => ["apps/**/test", "test"],
filter => "*.erl",
rules => [
{elvis_text_style, line_length, #{ limit => 100
, skip_comments => false }},
{elvis_style, dont_repeat_yourself, #{ min_complexity => 100 }}
]
},
#{dirs => ["."],
filter => "Makefile", filter => "Makefile",
ruleset => makefiles ruleset => makefiles
}, },
{dirs => ["."], #{dirs => ["."],
filter => "rebar.config", filter => "rebar.config",
ruleset => rebar_config ruleset => rebar_config
}, },
{dirs => ["."], #{dirs => ["."],
filter => "elvis.config", filter => "elvis.config",
ruleset => elvis_config ruleset => elvis_config
} }

View File

@ -23,7 +23,8 @@ config() ->
]. ].
plugins() -> plugins() ->
[ {relup_helper,{git,"https://github.com/emqx/relup_helper", {branch,"master"}}} [ {relup_helper,{git,"https://github.com/emqx/relup_helper", {branch,"master"}}},
{er_coap_client, {git, "https://github.com/emqx/er_coap_client", {tag, "v1.0"}}}
]. ].
test_deps() -> test_deps() ->

View File

@ -31,7 +31,7 @@ for file in $(git_diff); do
# not .erl file # not .erl file
continue continue
fi fi
if ! ./elvis rock "$file"; then if ! ./elvis rock "$file" -c elvis.config; then
bad_file_count=$(( bad_file_count + 1)) bad_file_count=$(( bad_file_count + 1))
fi fi
done done

View File

@ -106,11 +106,13 @@ with_ctl_server(Fun) ->
mock_print() -> mock_print() ->
%% proxy usage/1,2 and print/1,2 to format_xx/1,2 funcs %% proxy usage/1,2 and print/1,2 to format_xx/1,2 funcs
catch meck:unload(emqx_ctl),
meck:new(emqx_ctl, [non_strict, passthrough]), meck:new(emqx_ctl, [non_strict, passthrough]),
meck:expect(emqx_ctl, print, fun(Arg) -> emqx_ctl:format(Arg) end), meck:expect(emqx_ctl, print, fun(Arg) -> emqx_ctl:format(Arg) end),
meck:expect(emqx_ctl, print, fun(Msg, Arg) -> emqx_ctl:format(Msg, Arg) end), meck:expect(emqx_ctl, print, fun(Msg, Arg) -> emqx_ctl:format(Msg, Arg) end),
meck:expect(emqx_ctl, usage, fun(Usages) -> emqx_ctl:format_usage(Usages) end), meck:expect(emqx_ctl, usage, fun(Usages) -> emqx_ctl:format_usage(Usages) end),
meck:expect(emqx_ctl, usage, fun(CmdParams, CmdDescr) -> emqx_ctl:format_usage(CmdParams, CmdDescr) end). meck:expect(emqx_ctl, usage, fun(CmdParams, CmdDescr) ->
emqx_ctl:format_usage(CmdParams, CmdDescr) end).
unmock_print() -> unmock_print() ->
meck:unload(emqx_ctl). meck:unload(emqx_ctl).