test: add test coverage for emqx_observer_cli

This commit is contained in:
Zaiming (Stone) Shi 2022-04-04 11:34:27 +02:00
parent 8f202b6fed
commit c24d61efcd
2 changed files with 67 additions and 5 deletions

View File

@ -35,17 +35,23 @@ disable() ->
cmd(["status"]) -> cmd(["status"]) ->
observer_cli:start(); observer_cli:start();
cmd(["bin_leak"]) -> cmd(["bin_leak"]) ->
[emqx_ctl:print("~p~n", [Row]) || Row <- recon:bin_leak(100)]; lists:foreach(
fun(Row) -> emqx_ctl:print("~p~n", [Row]) end,
recon:bin_leak(100)
);
cmd(["load", Mod]) -> cmd(["load", Mod]) ->
Module = list_to_existing_atom(Mod), Module = list_to_existing_atom(Mod),
Nodes = nodes(), Nodes = nodes(),
Res = remote_load(Nodes, Module), Res = remote_load(Nodes, Module),
emqx_ctl:print("Loaded ~p module on ~p on ~n", [Mod, Nodes, Res]); emqx_ctl:print("Loaded ~p module on ~p: ~p~n", [Module, Nodes, Res]);
cmd(_) -> cmd(_) ->
emqx_ctl:usage([ emqx_ctl:usage([
{"observer status", "observer_cli:start()"}, {"observer status", "Start observer in the current console"},
{"observer bin_leak", "recon:bin_leak(100)"}, {"observer bin_leak",
{"observer load Mod", "recon:remote_load(Mod) to all nodes"} "Force all processes to perform garbage collection "
"and prints the top-100 processes that freed the "
"biggest amount of binaries, potentially highlighting leaks."},
{"observer load Mod", "Ensure a module is loaded in all EMQX nodes in the cluster"}
]). ]).
%% recon:remote_load/1 has a bug, when nodes() returns [], it is %% recon:remote_load/1 has a bug, when nodes() returns [], it is

View File

@ -0,0 +1,56 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2022 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------
-module(emqx_observer_cli_tests).
-include_lib("eunit/include/eunit.hrl").
start_observer_cli_test() ->
meck:new(observer_cli, [passthrough, no_history, no_link, no_passthrough_cover]),
meck:expect(observer_cli, start, fun() -> ok end),
try
ok = emqx_observer_cli:cmd(["status"])
after
meck:unload(observer_cli)
end.
bin_leak_test() ->
ok = emqx_observer_cli:cmd(["bin_leak"]).
load_observer_cli_test() ->
ok = emqx_observer_cli:cmd(["load", "lists"]).
unknown_command_test() ->
meck_emqx_ctl(),
try
ok = emqx_observer_cli:cmd(dummy),
receive
{usage, [_ | _]} -> ok
end
after
unmeck_emqx_ctl()
end.
meck_emqx_ctl() ->
Pid = self(),
meck:new(emqx_ctl, [passthrough, no_history, no_link, no_passthrough_cover]),
meck:expect(emqx_ctl, usage, fun(Tuples) ->
Pid ! {usage, Tuples},
ok
end).
unmeck_emqx_ctl() ->
meck:unload(emqx_ctl).