emqx_mgmt_api_plugins_SUITE.erl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-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_mgmt_api_plugins_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. -define(EMQX_PLUGIN_TEMPLATE_NAME, "my_emqx_plugin").
  22. -define(EMQX_PLUGIN_TEMPLATE_VSN, "5.1.0").
  23. -define(PACKAGE_SUFFIX, ".tar.gz").
  24. -define(CLUSTER_API_SERVER(PORT), ("http://127.0.0.1:" ++ (integer_to_list(PORT)))).
  25. -import(emqx_common_test_helpers, [on_exit/1]).
  26. all() ->
  27. emqx_common_test_helpers:all(?MODULE).
  28. init_per_suite(Config) ->
  29. WorkDir = proplists:get_value(data_dir, Config),
  30. ok = filelib:ensure_dir(WorkDir),
  31. DemoShDir1 = string:replace(WorkDir, "emqx_mgmt_api_plugins", "emqx_plugins"),
  32. DemoShDir = lists:flatten(string:replace(DemoShDir1, "emqx_management", "emqx_plugins")),
  33. OrigInstallDir = emqx_plugins:get_config_interal(install_dir, undefined),
  34. ok = filelib:ensure_dir(DemoShDir),
  35. emqx_mgmt_api_test_util:init_suite([emqx_conf, emqx_plugins]),
  36. emqx_plugins:put_config_internal(install_dir, DemoShDir),
  37. [{demo_sh_dir, DemoShDir}, {orig_install_dir, OrigInstallDir} | Config].
  38. end_per_suite(Config) ->
  39. emqx_common_test_helpers:boot_modules(all),
  40. %% restore config
  41. case proplists:get_value(orig_install_dir, Config) of
  42. undefined -> ok;
  43. OrigInstallDir -> emqx_plugins:put_config_internal(install_dir, OrigInstallDir)
  44. end,
  45. emqx_mgmt_api_test_util:end_suite([emqx_plugins, emqx_conf]),
  46. ok.
  47. init_per_testcase(t_cluster_update_order = TestCase, Config0) ->
  48. Config = [{api_port, 18085} | Config0],
  49. Cluster = [Node1 | _] = cluster(TestCase, Config),
  50. {ok, API} = init_api(Node1),
  51. [
  52. {api, API},
  53. {cluster, Cluster}
  54. | Config
  55. ];
  56. init_per_testcase(_TestCase, Config) ->
  57. Config.
  58. end_per_testcase(t_cluster_update_order, Config) ->
  59. Cluster = ?config(cluster, Config),
  60. emqx_cth_cluster:stop(Cluster),
  61. end_per_testcase(common, Config);
  62. end_per_testcase(_TestCase, _Config) ->
  63. emqx_common_test_helpers:call_janitor(),
  64. ok.
  65. t_plugins(Config) ->
  66. DemoShDir = proplists:get_value(demo_sh_dir, Config),
  67. PackagePath = get_demo_plugin_package(DemoShDir),
  68. ct:pal("package_location:~p install dir:~p", [PackagePath, emqx_plugins:install_dir()]),
  69. NameVsn = filename:basename(PackagePath, ?PACKAGE_SUFFIX),
  70. ok = emqx_plugins:ensure_uninstalled(NameVsn),
  71. ok = emqx_plugins:delete_package(NameVsn),
  72. ok = install_plugin(PackagePath),
  73. {ok, StopRes} = describe_plugins(NameVsn),
  74. Node = atom_to_binary(node()),
  75. ?assertMatch(
  76. #{
  77. <<"running_status">> := [
  78. #{<<"node">> := Node, <<"status">> := <<"stopped">>}
  79. ]
  80. },
  81. StopRes
  82. ),
  83. {ok, StopRes1} = update_plugin(NameVsn, "start"),
  84. ?assertEqual([], StopRes1),
  85. {ok, StartRes} = describe_plugins(NameVsn),
  86. ?assertMatch(
  87. #{
  88. <<"running_status">> := [
  89. #{<<"node">> := Node, <<"status">> := <<"running">>}
  90. ]
  91. },
  92. StartRes
  93. ),
  94. {ok, []} = update_plugin(NameVsn, "stop"),
  95. {ok, StopRes2} = describe_plugins(NameVsn),
  96. ?assertMatch(
  97. #{
  98. <<"running_status">> := [
  99. #{<<"node">> := Node, <<"status">> := <<"stopped">>}
  100. ]
  101. },
  102. StopRes2
  103. ),
  104. {ok, []} = uninstall_plugin(NameVsn),
  105. ok.
  106. t_install_plugin_matching_exisiting_name(Config) ->
  107. DemoShDir = proplists:get_value(demo_sh_dir, Config),
  108. PackagePath = get_demo_plugin_package(DemoShDir),
  109. NameVsn = filename:basename(PackagePath, ?PACKAGE_SUFFIX),
  110. ok = emqx_plugins:ensure_uninstalled(NameVsn),
  111. ok = emqx_plugins:delete_package(NameVsn),
  112. NameVsn1 = ?EMQX_PLUGIN_TEMPLATE_NAME ++ "_a" ++ "-" ++ ?EMQX_PLUGIN_TEMPLATE_VSN,
  113. PackagePath1 = create_renamed_package(PackagePath, NameVsn1),
  114. NameVsn1 = filename:basename(PackagePath1, ?PACKAGE_SUFFIX),
  115. ok = emqx_plugins:ensure_uninstalled(NameVsn1),
  116. ok = emqx_plugins:delete_package(NameVsn1),
  117. %% First, install plugin "emqx_plugin_template_a", then:
  118. %% "emqx_plugin_template" which matches the beginning
  119. %% of the previously installed plugin name
  120. ok = install_plugin(PackagePath1),
  121. ok = install_plugin(PackagePath),
  122. {ok, _} = describe_plugins(NameVsn),
  123. {ok, _} = describe_plugins(NameVsn1),
  124. {ok, _} = uninstall_plugin(NameVsn),
  125. {ok, _} = uninstall_plugin(NameVsn1).
  126. t_bad_plugin(Config) ->
  127. DemoShDir = proplists:get_value(demo_sh_dir, Config),
  128. PackagePathOrig = get_demo_plugin_package(DemoShDir),
  129. BackupPath = filename:join(["/tmp", [filename:basename(PackagePathOrig), ".backup"]]),
  130. {ok, _} = file:copy(PackagePathOrig, BackupPath),
  131. on_exit(fun() -> {ok, _} = file:rename(BackupPath, PackagePathOrig) end),
  132. PackagePath = filename:join([
  133. filename:dirname(PackagePathOrig),
  134. "bad_plugin-1.0.0.tar.gz"
  135. ]),
  136. on_exit(fun() -> file:delete(PackagePath) end),
  137. ct:pal("package_location:~p orig:~p", [PackagePath, PackagePathOrig]),
  138. %% rename plugin tarball
  139. file:copy(PackagePathOrig, PackagePath),
  140. file:delete(PackagePathOrig),
  141. {ok, {{"HTTP/1.1", 400, "Bad Request"}, _, _}} = install_plugin(PackagePath),
  142. ?assertEqual(
  143. {error, enoent},
  144. file:delete(
  145. filename:join([
  146. emqx_plugins:install_dir(),
  147. filename:basename(PackagePath)
  148. ])
  149. )
  150. ).
  151. t_delete_non_existing(_Config) ->
  152. Path = emqx_mgmt_api_test_util:api_path(["plugins", "non_exists"]),
  153. ?assertMatch(
  154. {error, {_, 404, _}},
  155. emqx_mgmt_api_test_util:request_api(delete, Path)
  156. ),
  157. ok.
  158. t_cluster_update_order(Config) ->
  159. DemoShDir = proplists:get_value(demo_sh_dir, Config),
  160. PackagePath1 = get_demo_plugin_package(DemoShDir),
  161. NameVsn1 = filename:basename(PackagePath1, ?PACKAGE_SUFFIX),
  162. Name2Str = ?EMQX_PLUGIN_TEMPLATE_NAME ++ "_a",
  163. NameVsn2 = Name2Str ++ "-" ++ ?EMQX_PLUGIN_TEMPLATE_VSN,
  164. PackagePath2 = create_renamed_package(PackagePath1, NameVsn2),
  165. Name1 = list_to_binary(?EMQX_PLUGIN_TEMPLATE_NAME),
  166. Name2 = list_to_binary(Name2Str),
  167. ok = install_plugin(Config, PackagePath1),
  168. ok = install_plugin(Config, PackagePath2),
  169. %% to get them configured...
  170. {ok, _} = update_plugin(Config, NameVsn1, "start"),
  171. {ok, _} = update_plugin(Config, NameVsn2, "start"),
  172. ?assertMatch(
  173. {ok, [
  174. #{<<"name">> := Name1},
  175. #{<<"name">> := Name2}
  176. ]},
  177. list_plugins(Config)
  178. ),
  179. ct:pal("moving to rear"),
  180. ?assertMatch({ok, _}, update_boot_order(NameVsn1, #{position => rear}, Config)),
  181. ?assertMatch(
  182. {ok, [
  183. #{<<"name">> := Name2},
  184. #{<<"name">> := Name1}
  185. ]},
  186. list_plugins(Config)
  187. ),
  188. ct:pal("moving to front"),
  189. ?assertMatch({ok, _}, update_boot_order(NameVsn1, #{position => front}, Config)),
  190. ?assertMatch(
  191. {ok, [
  192. #{<<"name">> := Name1},
  193. #{<<"name">> := Name2}
  194. ]},
  195. list_plugins(Config)
  196. ),
  197. ct:pal("moving after"),
  198. NameVsn2Bin = list_to_binary(NameVsn2),
  199. ?assertMatch(
  200. {ok, _},
  201. update_boot_order(NameVsn1, #{position => <<"after:", NameVsn2Bin/binary>>}, Config)
  202. ),
  203. ?assertMatch(
  204. {ok, [
  205. #{<<"name">> := Name2},
  206. #{<<"name">> := Name1}
  207. ]},
  208. list_plugins(Config)
  209. ),
  210. ct:pal("moving before"),
  211. ?assertMatch(
  212. {ok, _},
  213. update_boot_order(NameVsn1, #{position => <<"before:", NameVsn2Bin/binary>>}, Config)
  214. ),
  215. ?assertMatch(
  216. {ok, [
  217. #{<<"name">> := Name1},
  218. #{<<"name">> := Name2}
  219. ]},
  220. list_plugins(Config)
  221. ),
  222. ok.
  223. list_plugins(Config) ->
  224. #{host := Host, auth := Auth} = get_host_and_auth(Config),
  225. Path = emqx_mgmt_api_test_util:api_path(Host, ["plugins"]),
  226. case emqx_mgmt_api_test_util:request_api(get, Path, Auth) of
  227. {ok, Apps} -> {ok, emqx_utils_json:decode(Apps, [return_maps])};
  228. Error -> Error
  229. end.
  230. describe_plugins(Name) ->
  231. Path = emqx_mgmt_api_test_util:api_path(["plugins", Name]),
  232. case emqx_mgmt_api_test_util:request_api(get, Path) of
  233. {ok, Res} -> {ok, emqx_utils_json:decode(Res, [return_maps])};
  234. Error -> Error
  235. end.
  236. install_plugin(FilePath) ->
  237. {ok, _Role, Token} = emqx_dashboard_admin:sign_token(<<"admin">>, <<"public">>),
  238. Path = emqx_mgmt_api_test_util:api_path(["plugins", "install"]),
  239. case
  240. emqx_mgmt_api_test_util:upload_request(
  241. Path,
  242. FilePath,
  243. "plugin",
  244. <<"application/gzip">>,
  245. [],
  246. Token
  247. )
  248. of
  249. {ok, {{"HTTP/1.1", 204, "No Content"}, _Headers, <<>>}} -> ok;
  250. Error -> Error
  251. end.
  252. install_plugin(Config, FilePath) ->
  253. #{host := Host, auth := Auth} = get_host_and_auth(Config),
  254. Path = emqx_mgmt_api_test_util:api_path(Host, ["plugins", "install"]),
  255. case
  256. emqx_mgmt_api_test_util:upload_request(
  257. Path,
  258. FilePath,
  259. "plugin",
  260. <<"application/gzip">>,
  261. [],
  262. Auth
  263. )
  264. of
  265. {ok, {{"HTTP/1.1", 204, "No Content"}, _Headers, <<>>}} -> ok;
  266. Error -> Error
  267. end.
  268. update_plugin(Name, Action) ->
  269. Path = emqx_mgmt_api_test_util:api_path(["plugins", Name, Action]),
  270. emqx_mgmt_api_test_util:request_api(put, Path).
  271. update_plugin(Config, Name, Action) when is_list(Config) ->
  272. #{host := Host, auth := Auth} = get_host_and_auth(Config),
  273. Path = emqx_mgmt_api_test_util:api_path(Host, ["plugins", Name, Action]),
  274. emqx_mgmt_api_test_util:request_api(put, Path, Auth).
  275. update_boot_order(Name, MoveBody, Config) ->
  276. #{host := Host, auth := Auth} = get_host_and_auth(Config),
  277. Path = emqx_mgmt_api_test_util:api_path(Host, ["plugins", Name, "move"]),
  278. Opts = #{return_all => true},
  279. case emqx_mgmt_api_test_util:request_api(post, Path, "", Auth, MoveBody, Opts) of
  280. {ok, Res} ->
  281. Resp =
  282. case emqx_utils_json:safe_decode(Res, [return_maps]) of
  283. {ok, Decoded} -> Decoded;
  284. {error, _} -> Res
  285. end,
  286. ct:pal("update_boot_order response:\n ~p", [Resp]),
  287. {ok, Resp};
  288. Error ->
  289. Error
  290. end.
  291. uninstall_plugin(Name) ->
  292. DeletePath = emqx_mgmt_api_test_util:api_path(["plugins", Name]),
  293. emqx_mgmt_api_test_util:request_api(delete, DeletePath).
  294. get_demo_plugin_package(Dir) ->
  295. #{package := Pkg} = emqx_plugins_SUITE:get_demo_plugin_package(),
  296. FileName = ?EMQX_PLUGIN_TEMPLATE_NAME ++ "-" ++ ?EMQX_PLUGIN_TEMPLATE_VSN ++ ?PACKAGE_SUFFIX,
  297. PluginPath = "./" ++ FileName,
  298. Pkg = filename:join([Dir, FileName]),
  299. _ = os:cmd("cp " ++ Pkg ++ " " ++ PluginPath),
  300. true = filelib:is_regular(PluginPath),
  301. PluginPath.
  302. create_renamed_package(PackagePath, NewNameVsn) ->
  303. {ok, Content} = erl_tar:extract(PackagePath, [compressed, memory]),
  304. {ok, NewName, _Vsn} = emqx_plugins:parse_name_vsn(NewNameVsn),
  305. NewNameB = atom_to_binary(NewName, utf8),
  306. Content1 = lists:map(
  307. fun({F, B}) ->
  308. [_ | PathPart] = filename:split(F),
  309. B1 = update_release_json(PathPart, B, NewNameB),
  310. {filename:join([NewNameVsn | PathPart]), B1}
  311. end,
  312. Content
  313. ),
  314. NewPackagePath = filename:join(filename:dirname(PackagePath), NewNameVsn ++ ?PACKAGE_SUFFIX),
  315. ok = erl_tar:create(NewPackagePath, Content1, [compressed]),
  316. NewPackagePath.
  317. update_release_json(["release.json"], FileContent, NewName) ->
  318. ContentMap = emqx_utils_json:decode(FileContent, [return_maps]),
  319. emqx_utils_json:encode(ContentMap#{<<"name">> => NewName});
  320. update_release_json(_FileName, FileContent, _NewName) ->
  321. FileContent.
  322. cluster(TestCase, Config) ->
  323. APIPort = ?config(api_port, Config),
  324. AppSpecs = app_specs(Config),
  325. Node1Apps = AppSpecs ++ [app_spec_dashboard(APIPort)],
  326. Node2Apps = AppSpecs,
  327. Node1Name = emqx_mgmt_api_plugins_SUITE1,
  328. Node1 = emqx_cth_cluster:node_name(Node1Name),
  329. emqx_cth_cluster:start(
  330. [
  331. {Node1Name, #{role => core, apps => Node1Apps, join_to => Node1}},
  332. {emqx_mgmt_api_plugins_SUITE2, #{role => core, apps => Node2Apps, join_to => Node1}}
  333. ],
  334. #{work_dir => emqx_cth_suite:work_dir(TestCase, Config)}
  335. ).
  336. app_specs(_Config) ->
  337. [
  338. emqx,
  339. emqx_conf,
  340. emqx_management,
  341. emqx_plugins
  342. ].
  343. app_spec_dashboard(APIPort) ->
  344. {emqx_dashboard, #{
  345. config =>
  346. #{
  347. dashboard =>
  348. #{
  349. listeners =>
  350. #{
  351. http =>
  352. #{bind => APIPort}
  353. }
  354. }
  355. }
  356. }}.
  357. init_api(Node) ->
  358. erpc:call(Node, emqx_common_test_http, create_default_app, []).
  359. get_host_and_auth(Config) when is_list(Config) ->
  360. API = ?config(api, Config),
  361. APIPort = ?config(api_port, Config),
  362. Host = ?CLUSTER_API_SERVER(APIPort),
  363. Auth = emqx_common_test_http:auth_header(API),
  364. #{host => Host, auth => Auth}.