emqx_gateway_cli_SUITE.erl 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2021-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_gateway_cli_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("eunit/include/eunit.hrl").
  20. -define(GP(S), begin
  21. S,
  22. receive
  23. {fmt, P} -> P;
  24. O -> O
  25. end
  26. end).
  27. %% this parses to #{}, will not cause config cleanup
  28. %% so we will need call emqx_config:erase
  29. -define(CONF_DEFAULT, <<
  30. "\n"
  31. "gateway {}\n"
  32. >>).
  33. %% The config with json format for mqtt-sn gateway
  34. -define(CONF_MQTTSN,
  35. "\n"
  36. "{\"idle_timeout\": \"30s\",\n"
  37. " \"enable_stats\": true,\n"
  38. " \"mountpoint\": \"mqttsn/\",\n"
  39. " \"gateway_id\": 1,\n"
  40. " \"broadcast\": true,\n"
  41. " \"enable_qos3\": true,\n"
  42. " \"predefined\": [{\"id\": 1001, \"topic\": \"pred/a\"}],\n"
  43. " \"listeners\":\n"
  44. " [{\"type\": \"udp\",\n"
  45. " \"name\": \"ct\",\n"
  46. " \"bind\": \"1884\"\n"
  47. " }]\n"
  48. "}\n"
  49. ).
  50. -import(emqx_gateway_test_utils, [sn_client_connect/1, sn_client_disconnect/1]).
  51. %%--------------------------------------------------------------------
  52. %% Setup
  53. %%--------------------------------------------------------------------
  54. all() -> emqx_common_test_helpers:all(?MODULE).
  55. init_per_suite(Conf) ->
  56. emqx_config:erase(gateway),
  57. emqx_gateway_test_utils:load_all_gateway_apps(),
  58. emqx_common_test_helpers:load_config(emqx_gateway_schema, ?CONF_DEFAULT),
  59. emqx_mgmt_api_test_util:init_suite([emqx_conf, emqx_auth, emqx_gateway]),
  60. Conf.
  61. end_per_suite(Conf) ->
  62. emqx_mgmt_api_test_util:end_suite([emqx_gateway, emqx_auth, emqx_conf]),
  63. Conf.
  64. init_per_testcase(_, Conf) ->
  65. Self = self(),
  66. ok = meck:new(emqx_ctl, [passthrough, no_history, no_link]),
  67. ok = meck:expect(
  68. emqx_ctl,
  69. usage,
  70. fun(L) -> emqx_ctl:format_usage(L) end
  71. ),
  72. ok = meck:expect(
  73. emqx_ctl,
  74. print,
  75. fun(Fmt) ->
  76. Self ! {fmt, emqx_ctl:format(Fmt, [])}
  77. end
  78. ),
  79. ok = meck:expect(
  80. emqx_ctl,
  81. print,
  82. fun(Fmt, Args) ->
  83. Self ! {fmt, emqx_ctl:format(Fmt, Args)}
  84. end
  85. ),
  86. Conf.
  87. end_per_testcase(_, _) ->
  88. meck:unload([emqx_ctl]),
  89. ok.
  90. %%--------------------------------------------------------------------
  91. %% Cases
  92. %%--------------------------------------------------------------------
  93. %% TODO:
  94. t_load_unload(_) ->
  95. ok.
  96. t_gateway_registry_usage(_) ->
  97. ?assertEqual(
  98. ["gateway-registry list # List all registered gateways\n"],
  99. emqx_gateway_cli:'gateway-registry'(usage)
  100. ).
  101. t_gateway_registry_list(_) ->
  102. emqx_gateway_cli:'gateway-registry'(["list"]),
  103. %% TODO: assert it.
  104. _ = acc_print().
  105. t_gateway_usage(_) ->
  106. ?assertEqual(
  107. [
  108. "gateway list # List all gateway\n",
  109. "gateway lookup <Name> # Lookup a gateway detailed information\n",
  110. "gateway load <Name> <JsonConf> # Load a gateway with config\n",
  111. "gateway unload <Name> # Unload the gateway\n",
  112. "gateway stop <Name> # Stop the gateway\n",
  113. "gateway start <Name> # Start the gateway\n"
  114. ],
  115. emqx_gateway_cli:gateway(usage)
  116. ).
  117. t_gateway_list(_) ->
  118. emqx_gateway_cli:gateway(["list"]),
  119. %% TODO: assert it.
  120. _ = acc_print(),
  121. emqx_gateway_cli:gateway(["load", "mqttsn", ?CONF_MQTTSN]),
  122. ?assertEqual("ok\n", acc_print()),
  123. emqx_gateway_cli:gateway(["list"]),
  124. %% TODO: assert it.
  125. _ = acc_print(),
  126. emqx_gateway_cli:gateway(["unload", "mqttsn"]),
  127. ?assertEqual("ok\n", acc_print()).
  128. t_gateway_load_unload_lookup(_) ->
  129. emqx_gateway_cli:gateway(["lookup", "mqttsn"]),
  130. ?assertEqual("undefined\n", acc_print()),
  131. emqx_gateway_cli:gateway(["load", "mqttsn", ?CONF_MQTTSN]),
  132. ?assertEqual("ok\n", acc_print()),
  133. %% TODO: bad config name, format???
  134. emqx_gateway_cli:gateway(["lookup", "mqttsn"]),
  135. %% TODO: assert it. for example:
  136. %% name: mqttsn
  137. %% status: running
  138. %% created_at: 2022-01-05T14:40:20.039+08:00
  139. %% started_at: 2022-01-05T14:42:37.894+08:00
  140. %% config: #{broadcast => false,enable => true,enable_qos3 => true,
  141. %% enable_stats => true,gateway_id => 1,idle_timeout => 30000,
  142. %% mountpoint => <<>>,predefined => []}
  143. _ = acc_print(),
  144. emqx_gateway_cli:gateway(["load", "mqttsn", "{}"]),
  145. ?assertEqual(
  146. "Error: The mqttsn gateway already loaded\n",
  147. acc_print()
  148. ),
  149. emqx_gateway_cli:gateway(["load", "bad-gw-name", "{}"]),
  150. %% TODO: assert it. for example:
  151. %% Error: Illegal gateway name
  152. _ = acc_print(),
  153. emqx_gateway_cli:gateway(["unload", "mqttsn"]),
  154. ?assertEqual("ok\n", acc_print()),
  155. %% Always return ok, even the gateway has unloaded
  156. emqx_gateway_cli:gateway(["unload", "mqttsn"]),
  157. ?assertEqual("ok\n", acc_print()),
  158. emqx_gateway_cli:gateway(["lookup", "mqttsn"]),
  159. ?assertEqual("undefined\n", acc_print()).
  160. t_gateway_start_stop(_) ->
  161. emqx_gateway_cli:gateway(["load", "mqttsn", ?CONF_MQTTSN]),
  162. ?assertEqual("ok\n", acc_print()),
  163. emqx_gateway_cli:gateway(["stop", "mqttsn"]),
  164. ?assertEqual("ok\n", acc_print()),
  165. %% duplicated stop gateway, return ok
  166. emqx_gateway_cli:gateway(["stop", "mqttsn"]),
  167. ?assertEqual("ok\n", acc_print()),
  168. emqx_gateway_cli:gateway(["start", "mqttsn"]),
  169. ?assertEqual("ok\n", acc_print()),
  170. %% duplicated start gateway, return ok
  171. emqx_gateway_cli:gateway(["start", "mqttsn"]),
  172. ?assertEqual("ok\n", acc_print()),
  173. emqx_gateway_cli:gateway(["unload", "mqttsn"]),
  174. ?assertEqual("ok\n", acc_print()).
  175. t_gateway_clients_usage(_) ->
  176. ?assertEqual(
  177. [
  178. "gateway-clients list <Name> "
  179. "# List all clients for a gateway\n",
  180. "gateway-clients lookup <Name> <ClientId> "
  181. "# Lookup the Client Info for specified client\n",
  182. "gateway-clients kick <Name> <ClientId> "
  183. "# Kick out a client\n"
  184. ],
  185. emqx_gateway_cli:'gateway-clients'(usage)
  186. ).
  187. t_gateway_clients(_) ->
  188. emqx_gateway_cli:gateway(["load", "mqttsn", ?CONF_MQTTSN]),
  189. ?assertEqual("ok\n", acc_print()),
  190. Socket = sn_client_connect(<<"client1">>),
  191. _ = emqx_gateway_cli:'gateway-clients'(["list", "mqttsn"]),
  192. ClientDesc1 = acc_print(),
  193. _ = emqx_gateway_cli:'gateway-clients'(["lookup", "mqttsn", "client1"]),
  194. ClientDesc2 = acc_print(),
  195. ?assertEqual(ClientDesc1, ClientDesc2),
  196. sn_client_disconnect(Socket),
  197. timer:sleep(500),
  198. _ = emqx_gateway_cli:'gateway-clients'(["lookup", "mqttsn", "bad-client"]),
  199. ?assertEqual("Not Found.\n", acc_print()),
  200. _ = emqx_gateway_cli:'gateway-clients'(["lookup", "bad-gw", "bad-client"]),
  201. ?assertEqual("Bad Gateway Name.\n", acc_print()),
  202. _ = emqx_gateway_cli:'gateway-clients'(["list", "mqttsn"]),
  203. %% no print for empty client list
  204. _ = emqx_gateway_cli:'gateway-clients'(["list", "bad-gw"]),
  205. ?assertEqual("Bad Gateway Name.\n", acc_print()),
  206. emqx_gateway_cli:gateway(["unload", "mqttsn"]),
  207. ?assertEqual("ok\n", acc_print()).
  208. t_gateway_clients_kick(_) ->
  209. emqx_gateway_cli:gateway(["load", "mqttsn", ?CONF_MQTTSN]),
  210. ?assertEqual("ok\n", acc_print()),
  211. Socket = sn_client_connect(<<"client1">>),
  212. _ = emqx_gateway_cli:'gateway-clients'(["list", "mqttsn"]),
  213. _ = acc_print(),
  214. _ = emqx_gateway_cli:'gateway-clients'(["kick", "mqttsn", "bad-client"]),
  215. ?assertEqual("Not Found.\n", acc_print()),
  216. _ = emqx_gateway_cli:'gateway-clients'(["kick", "mqttsn", "client1"]),
  217. ?assertEqual("ok\n", acc_print()),
  218. sn_client_disconnect(Socket),
  219. emqx_gateway_cli:gateway(["unload", "mqttsn"]),
  220. ?assertEqual("ok\n", acc_print()).
  221. t_gateway_metrcis_usage(_) ->
  222. ?assertEqual(
  223. [
  224. "gateway-metrics <Name> "
  225. "# List all metrics for a gateway\n"
  226. ],
  227. emqx_gateway_cli:'gateway-metrics'(usage)
  228. ).
  229. t_gateway_metrcis(_) ->
  230. ok.
  231. acc_print() ->
  232. lists:concat(lists:reverse(acc_print([]))).
  233. acc_print(Acc) ->
  234. receive
  235. {fmt, S} -> acc_print([S | Acc])
  236. after 200 ->
  237. Acc
  238. end.