emqx_ctl_SUITE.erl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2019-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_ctl_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("eunit/include/eunit.hrl").
  20. -include_lib("common_test/include/ct.hrl").
  21. all() -> [t_reg_unreg_command, t_run_commands, t_print, t_usage, t_unexpected].
  22. init_per_suite(Config) ->
  23. application:stop(emqx_ctl),
  24. Config.
  25. end_per_suite(_Config) ->
  26. ok.
  27. %%--------------------------------------------------------------------
  28. %% Test cases
  29. %%--------------------------------------------------------------------
  30. t_reg_unreg_command(_) ->
  31. with_ctl_server(
  32. fun(_CtlSrv) ->
  33. emqx_ctl:register_command(cmd1, {?MODULE, cmd1_fun}),
  34. emqx_ctl:register_command(cmd2, {?MODULE, cmd2_fun}),
  35. ?assertEqual({?MODULE, cmd1_fun}, lookup_command(cmd1)),
  36. ?assertEqual({?MODULE, cmd2_fun}, lookup_command(cmd2)),
  37. ?assertEqual(
  38. [{cmd1, ?MODULE, cmd1_fun}, {cmd2, ?MODULE, cmd2_fun}],
  39. emqx_ctl:get_commands()
  40. ),
  41. emqx_ctl:unregister_command(cmd1),
  42. emqx_ctl:unregister_command(cmd2),
  43. ct:sleep(100),
  44. ?assertEqual({error, cmd_not_found}, lookup_command(cmd1)),
  45. ?assertEqual({error, cmd_not_found}, lookup_command(cmd2)),
  46. ?assertEqual([], emqx_ctl:get_commands())
  47. end
  48. ).
  49. t_run_commands(_) ->
  50. with_ctl_server(
  51. fun(_CtlSrv) ->
  52. ?assertEqual({error, cmd_not_found}, emqx_ctl:run_command(["cmd", "arg"])),
  53. emqx_ctl:register_command(cmd1, {?MODULE, cmd1_fun}),
  54. emqx_ctl:register_command(cmd2, {?MODULE, cmd2_fun}),
  55. ok = emqx_ctl:run_command(["cmd1", "arg"]),
  56. {error, badarg} = emqx_ctl:run_command(["cmd1", "badarg"]),
  57. ok = emqx_ctl:run_command(["cmd2", "arg1", "arg2"]),
  58. {error, badarg} = emqx_ctl:run_command(["cmd2", "arg1", "badarg"])
  59. end
  60. ).
  61. t_print(_) ->
  62. ok = emqx_ctl:print("help"),
  63. ok = emqx_ctl:print("~ts", [help]),
  64. ok = emqx_ctl:print("~ts", [<<"~!@#$%^&*()">>]),
  65. % - check the output of the usage
  66. mock_print(),
  67. ?assertEqual("help", emqx_ctl:print("help")),
  68. ?assertEqual("help", emqx_ctl:print("~ts", [help])),
  69. ?assertEqual("~!@#$%^&*()", emqx_ctl:print("~ts", [<<"~!@#$%^&*()">>])),
  70. unmock_print().
  71. t_eval_erl(_) ->
  72. mock_print(),
  73. Expected = atom_to_list(node()) ++ "\n",
  74. ?assertEqual(Expected, emqx_ctl:run_command(["eval_erl", "node()"])),
  75. unmock_print().
  76. t_usage(_) ->
  77. CmdParams1 = "emqx_cmd_1 param1 param2",
  78. CmdDescr1 = "emqx_cmd_1 is a test command means nothing",
  79. % - usage/1,2 should return ok
  80. ok = emqx_ctl:usage([{CmdParams1, CmdDescr1}, {CmdParams1, CmdDescr1}]),
  81. ok = emqx_ctl:usage(CmdParams1, CmdDescr1).
  82. t_unexpected(_) ->
  83. with_ctl_server(
  84. fun(CtlSrv) ->
  85. ignored = gen_server:call(CtlSrv, unexpected_call),
  86. ok = gen_server:cast(CtlSrv, unexpected_cast),
  87. CtlSrv ! unexpected_info,
  88. ?assert(is_process_alive(CtlSrv))
  89. end
  90. ).
  91. %%--------------------------------------------------------------------
  92. %% Cmds for test
  93. %%--------------------------------------------------------------------
  94. cmd1_fun(["arg"]) -> ok;
  95. cmd1_fun(["badarg"]) -> error(badarg).
  96. cmd2_fun(["arg1", "arg2"]) -> ok;
  97. cmd2_fun(["arg1", "badarg"]) -> error(badarg).
  98. with_ctl_server(Fun) ->
  99. ok = emqx_ctl:stop(),
  100. {ok, Pid} = emqx_ctl:start_link(),
  101. try
  102. _ = Fun(Pid),
  103. ok
  104. after
  105. ok = emqx_ctl:stop()
  106. end.
  107. mock_print() ->
  108. %% proxy usage/1,2 and print/1,2 to format_xx/1,2 funcs
  109. catch meck:unload(emqx_ctl),
  110. meck:new(emqx_ctl, [non_strict, passthrough]),
  111. meck:expect(emqx_ctl, print, fun(Arg) -> emqx_ctl:format(Arg, []) end),
  112. meck:expect(emqx_ctl, print, fun(Msg, Arg) -> emqx_ctl:format(Msg, Arg) end),
  113. meck:expect(emqx_ctl, usage, fun(Usages) -> emqx_ctl:format_usage(Usages) end),
  114. meck:expect(emqx_ctl, usage, fun(CmdParams, CmdDescr) ->
  115. emqx_ctl:format_usage(CmdParams, CmdDescr)
  116. end).
  117. unmock_print() ->
  118. meck:unload(emqx_ctl).
  119. lookup_command(Cmd) ->
  120. case emqx_ctl:lookup_command(Cmd) of
  121. {ok, {Mod, Fun}} -> {Mod, Fun};
  122. Error -> Error
  123. end.