| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- %%--------------------------------------------------------------------
- %% Copyright (c) 2020-2024 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_mgmt_api_plugins_SUITE).
- -compile(export_all).
- -compile(nowarn_export_all).
- -include_lib("eunit/include/eunit.hrl").
- -include_lib("common_test/include/ct.hrl").
- -define(EMQX_PLUGIN_TEMPLATE_NAME, "my_emqx_plugin").
- -define(EMQX_PLUGIN_TEMPLATE_VSN, "5.1.0").
- -define(PACKAGE_SUFFIX, ".tar.gz").
- -define(CLUSTER_API_SERVER(PORT), ("http://127.0.0.1:" ++ (integer_to_list(PORT)))).
- -import(emqx_common_test_helpers, [on_exit/1]).
- all() ->
- emqx_common_test_helpers:all(?MODULE).
- init_per_suite(Config) ->
- WorkDir = proplists:get_value(data_dir, Config),
- ok = filelib:ensure_dir(WorkDir),
- DemoShDir1 = string:replace(WorkDir, "emqx_mgmt_api_plugins", "emqx_plugins"),
- DemoShDir = lists:flatten(string:replace(DemoShDir1, "emqx_management", "emqx_plugins")),
- OrigInstallDir = emqx_plugins:get_config_interal(install_dir, undefined),
- ok = filelib:ensure_dir(DemoShDir),
- emqx_mgmt_api_test_util:init_suite([emqx_conf, emqx_plugins]),
- emqx_plugins:put_config_internal(install_dir, DemoShDir),
- [{demo_sh_dir, DemoShDir}, {orig_install_dir, OrigInstallDir} | Config].
- end_per_suite(Config) ->
- emqx_common_test_helpers:boot_modules(all),
- %% restore config
- case proplists:get_value(orig_install_dir, Config) of
- undefined -> ok;
- OrigInstallDir -> emqx_plugins:put_config_internal(install_dir, OrigInstallDir)
- end,
- emqx_mgmt_api_test_util:end_suite([emqx_plugins, emqx_conf]),
- ok.
- init_per_testcase(t_cluster_update_order = TestCase, Config0) ->
- Config = [{api_port, 18085} | Config0],
- Cluster = [Node1 | _] = cluster(TestCase, Config),
- {ok, API} = init_api(Node1),
- [
- {api, API},
- {cluster, Cluster}
- | Config
- ];
- init_per_testcase(_TestCase, Config) ->
- Config.
- end_per_testcase(t_cluster_update_order, Config) ->
- Cluster = ?config(cluster, Config),
- emqx_cth_cluster:stop(Cluster),
- end_per_testcase(common, Config);
- end_per_testcase(_TestCase, _Config) ->
- emqx_common_test_helpers:call_janitor(),
- ok.
- t_plugins(Config) ->
- DemoShDir = proplists:get_value(demo_sh_dir, Config),
- PackagePath = get_demo_plugin_package(DemoShDir),
- ct:pal("package_location:~p install dir:~p", [PackagePath, emqx_plugins:install_dir()]),
- NameVsn = filename:basename(PackagePath, ?PACKAGE_SUFFIX),
- ok = emqx_plugins:ensure_uninstalled(NameVsn),
- ok = emqx_plugins:delete_package(NameVsn),
- ok = install_plugin(PackagePath),
- {ok, StopRes} = describe_plugins(NameVsn),
- Node = atom_to_binary(node()),
- ?assertMatch(
- #{
- <<"running_status">> := [
- #{<<"node">> := Node, <<"status">> := <<"stopped">>}
- ]
- },
- StopRes
- ),
- {ok, StopRes1} = update_plugin(NameVsn, "start"),
- ?assertEqual([], StopRes1),
- {ok, StartRes} = describe_plugins(NameVsn),
- ?assertMatch(
- #{
- <<"running_status">> := [
- #{<<"node">> := Node, <<"status">> := <<"running">>}
- ]
- },
- StartRes
- ),
- {ok, []} = update_plugin(NameVsn, "stop"),
- {ok, StopRes2} = describe_plugins(NameVsn),
- ?assertMatch(
- #{
- <<"running_status">> := [
- #{<<"node">> := Node, <<"status">> := <<"stopped">>}
- ]
- },
- StopRes2
- ),
- {ok, []} = uninstall_plugin(NameVsn),
- ok.
- t_install_plugin_matching_exisiting_name(Config) ->
- DemoShDir = proplists:get_value(demo_sh_dir, Config),
- PackagePath = get_demo_plugin_package(DemoShDir),
- NameVsn = filename:basename(PackagePath, ?PACKAGE_SUFFIX),
- ok = emqx_plugins:ensure_uninstalled(NameVsn),
- ok = emqx_plugins:delete_package(NameVsn),
- NameVsn1 = ?EMQX_PLUGIN_TEMPLATE_NAME ++ "_a" ++ "-" ++ ?EMQX_PLUGIN_TEMPLATE_VSN,
- PackagePath1 = create_renamed_package(PackagePath, NameVsn1),
- NameVsn1 = filename:basename(PackagePath1, ?PACKAGE_SUFFIX),
- ok = emqx_plugins:ensure_uninstalled(NameVsn1),
- ok = emqx_plugins:delete_package(NameVsn1),
- %% First, install plugin "emqx_plugin_template_a", then:
- %% "emqx_plugin_template" which matches the beginning
- %% of the previously installed plugin name
- ok = install_plugin(PackagePath1),
- ok = install_plugin(PackagePath),
- {ok, _} = describe_plugins(NameVsn),
- {ok, _} = describe_plugins(NameVsn1),
- {ok, _} = uninstall_plugin(NameVsn),
- {ok, _} = uninstall_plugin(NameVsn1).
- t_bad_plugin(Config) ->
- DemoShDir = proplists:get_value(demo_sh_dir, Config),
- PackagePathOrig = get_demo_plugin_package(DemoShDir),
- BackupPath = filename:join(["/tmp", [filename:basename(PackagePathOrig), ".backup"]]),
- {ok, _} = file:copy(PackagePathOrig, BackupPath),
- on_exit(fun() -> {ok, _} = file:rename(BackupPath, PackagePathOrig) end),
- PackagePath = filename:join([
- filename:dirname(PackagePathOrig),
- "bad_plugin-1.0.0.tar.gz"
- ]),
- on_exit(fun() -> file:delete(PackagePath) end),
- ct:pal("package_location:~p orig:~p", [PackagePath, PackagePathOrig]),
- %% rename plugin tarball
- file:copy(PackagePathOrig, PackagePath),
- file:delete(PackagePathOrig),
- {ok, {{"HTTP/1.1", 400, "Bad Request"}, _, _}} = install_plugin(PackagePath),
- ?assertEqual(
- {error, enoent},
- file:delete(
- filename:join([
- emqx_plugins:install_dir(),
- filename:basename(PackagePath)
- ])
- )
- ).
- t_delete_non_existing(_Config) ->
- Path = emqx_mgmt_api_test_util:api_path(["plugins", "non_exists"]),
- ?assertMatch(
- {error, {_, 404, _}},
- emqx_mgmt_api_test_util:request_api(delete, Path)
- ),
- ok.
- t_cluster_update_order(Config) ->
- DemoShDir = proplists:get_value(demo_sh_dir, Config),
- PackagePath1 = get_demo_plugin_package(DemoShDir),
- NameVsn1 = filename:basename(PackagePath1, ?PACKAGE_SUFFIX),
- Name2Str = ?EMQX_PLUGIN_TEMPLATE_NAME ++ "_a",
- NameVsn2 = Name2Str ++ "-" ++ ?EMQX_PLUGIN_TEMPLATE_VSN,
- PackagePath2 = create_renamed_package(PackagePath1, NameVsn2),
- Name1 = list_to_binary(?EMQX_PLUGIN_TEMPLATE_NAME),
- Name2 = list_to_binary(Name2Str),
- ok = install_plugin(Config, PackagePath1),
- ok = install_plugin(Config, PackagePath2),
- %% to get them configured...
- {ok, _} = update_plugin(Config, NameVsn1, "start"),
- {ok, _} = update_plugin(Config, NameVsn2, "start"),
- ?assertMatch(
- {ok, [
- #{<<"name">> := Name1},
- #{<<"name">> := Name2}
- ]},
- list_plugins(Config)
- ),
- ct:pal("moving to rear"),
- ?assertMatch({ok, _}, update_boot_order(NameVsn1, #{position => rear}, Config)),
- ?assertMatch(
- {ok, [
- #{<<"name">> := Name2},
- #{<<"name">> := Name1}
- ]},
- list_plugins(Config)
- ),
- ct:pal("moving to front"),
- ?assertMatch({ok, _}, update_boot_order(NameVsn1, #{position => front}, Config)),
- ?assertMatch(
- {ok, [
- #{<<"name">> := Name1},
- #{<<"name">> := Name2}
- ]},
- list_plugins(Config)
- ),
- ct:pal("moving after"),
- NameVsn2Bin = list_to_binary(NameVsn2),
- ?assertMatch(
- {ok, _},
- update_boot_order(NameVsn1, #{position => <<"after:", NameVsn2Bin/binary>>}, Config)
- ),
- ?assertMatch(
- {ok, [
- #{<<"name">> := Name2},
- #{<<"name">> := Name1}
- ]},
- list_plugins(Config)
- ),
- ct:pal("moving before"),
- ?assertMatch(
- {ok, _},
- update_boot_order(NameVsn1, #{position => <<"before:", NameVsn2Bin/binary>>}, Config)
- ),
- ?assertMatch(
- {ok, [
- #{<<"name">> := Name1},
- #{<<"name">> := Name2}
- ]},
- list_plugins(Config)
- ),
- ok.
- list_plugins(Config) ->
- #{host := Host, auth := Auth} = get_host_and_auth(Config),
- Path = emqx_mgmt_api_test_util:api_path(Host, ["plugins"]),
- case emqx_mgmt_api_test_util:request_api(get, Path, Auth) of
- {ok, Apps} -> {ok, emqx_utils_json:decode(Apps, [return_maps])};
- Error -> Error
- end.
- describe_plugins(Name) ->
- Path = emqx_mgmt_api_test_util:api_path(["plugins", Name]),
- case emqx_mgmt_api_test_util:request_api(get, Path) of
- {ok, Res} -> {ok, emqx_utils_json:decode(Res, [return_maps])};
- Error -> Error
- end.
- install_plugin(FilePath) ->
- {ok, _Role, Token} = emqx_dashboard_admin:sign_token(<<"admin">>, <<"public">>),
- Path = emqx_mgmt_api_test_util:api_path(["plugins", "install"]),
- case
- emqx_mgmt_api_test_util:upload_request(
- Path,
- FilePath,
- "plugin",
- <<"application/gzip">>,
- [],
- Token
- )
- of
- {ok, {{"HTTP/1.1", 204, "No Content"}, _Headers, <<>>}} -> ok;
- Error -> Error
- end.
- install_plugin(Config, FilePath) ->
- #{host := Host, auth := Auth} = get_host_and_auth(Config),
- Path = emqx_mgmt_api_test_util:api_path(Host, ["plugins", "install"]),
- case
- emqx_mgmt_api_test_util:upload_request(
- Path,
- FilePath,
- "plugin",
- <<"application/gzip">>,
- [],
- Auth
- )
- of
- {ok, {{"HTTP/1.1", 204, "No Content"}, _Headers, <<>>}} -> ok;
- Error -> Error
- end.
- update_plugin(Name, Action) ->
- Path = emqx_mgmt_api_test_util:api_path(["plugins", Name, Action]),
- emqx_mgmt_api_test_util:request_api(put, Path).
- update_plugin(Config, Name, Action) when is_list(Config) ->
- #{host := Host, auth := Auth} = get_host_and_auth(Config),
- Path = emqx_mgmt_api_test_util:api_path(Host, ["plugins", Name, Action]),
- emqx_mgmt_api_test_util:request_api(put, Path, Auth).
- update_boot_order(Name, MoveBody, Config) ->
- #{host := Host, auth := Auth} = get_host_and_auth(Config),
- Path = emqx_mgmt_api_test_util:api_path(Host, ["plugins", Name, "move"]),
- Opts = #{return_all => true},
- case emqx_mgmt_api_test_util:request_api(post, Path, "", Auth, MoveBody, Opts) of
- {ok, Res} ->
- Resp =
- case emqx_utils_json:safe_decode(Res, [return_maps]) of
- {ok, Decoded} -> Decoded;
- {error, _} -> Res
- end,
- ct:pal("update_boot_order response:\n ~p", [Resp]),
- {ok, Resp};
- Error ->
- Error
- end.
- uninstall_plugin(Name) ->
- DeletePath = emqx_mgmt_api_test_util:api_path(["plugins", Name]),
- emqx_mgmt_api_test_util:request_api(delete, DeletePath).
- get_demo_plugin_package(Dir) ->
- #{package := Pkg} = emqx_plugins_SUITE:get_demo_plugin_package(),
- FileName = ?EMQX_PLUGIN_TEMPLATE_NAME ++ "-" ++ ?EMQX_PLUGIN_TEMPLATE_VSN ++ ?PACKAGE_SUFFIX,
- PluginPath = "./" ++ FileName,
- Pkg = filename:join([Dir, FileName]),
- _ = os:cmd("cp " ++ Pkg ++ " " ++ PluginPath),
- true = filelib:is_regular(PluginPath),
- PluginPath.
- create_renamed_package(PackagePath, NewNameVsn) ->
- {ok, Content} = erl_tar:extract(PackagePath, [compressed, memory]),
- {ok, NewName, _Vsn} = emqx_plugins:parse_name_vsn(NewNameVsn),
- NewNameB = atom_to_binary(NewName, utf8),
- Content1 = lists:map(
- fun({F, B}) ->
- [_ | PathPart] = filename:split(F),
- B1 = update_release_json(PathPart, B, NewNameB),
- {filename:join([NewNameVsn | PathPart]), B1}
- end,
- Content
- ),
- NewPackagePath = filename:join(filename:dirname(PackagePath), NewNameVsn ++ ?PACKAGE_SUFFIX),
- ok = erl_tar:create(NewPackagePath, Content1, [compressed]),
- NewPackagePath.
- update_release_json(["release.json"], FileContent, NewName) ->
- ContentMap = emqx_utils_json:decode(FileContent, [return_maps]),
- emqx_utils_json:encode(ContentMap#{<<"name">> => NewName});
- update_release_json(_FileName, FileContent, _NewName) ->
- FileContent.
- cluster(TestCase, Config) ->
- APIPort = ?config(api_port, Config),
- AppSpecs = app_specs(Config),
- Node1Apps = AppSpecs ++ [app_spec_dashboard(APIPort)],
- Node2Apps = AppSpecs,
- Node1Name = emqx_mgmt_api_plugins_SUITE1,
- Node1 = emqx_cth_cluster:node_name(Node1Name),
- emqx_cth_cluster:start(
- [
- {Node1Name, #{role => core, apps => Node1Apps, join_to => Node1}},
- {emqx_mgmt_api_plugins_SUITE2, #{role => core, apps => Node2Apps, join_to => Node1}}
- ],
- #{work_dir => emqx_cth_suite:work_dir(TestCase, Config)}
- ).
- app_specs(_Config) ->
- [
- emqx,
- emqx_conf,
- emqx_management,
- emqx_plugins
- ].
- app_spec_dashboard(APIPort) ->
- {emqx_dashboard, #{
- config =>
- #{
- dashboard =>
- #{
- listeners =>
- #{
- http =>
- #{bind => APIPort}
- }
- }
- }
- }}.
- init_api(Node) ->
- erpc:call(Node, emqx_common_test_http, create_default_app, []).
- get_host_and_auth(Config) when is_list(Config) ->
- API = ?config(api, Config),
- APIPort = ?config(api_port, Config),
- Host = ?CLUSTER_API_SERVER(APIPort),
- Auth = emqx_common_test_http:auth_header(API),
- #{host => Host, auth => Auth}.
|