Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugins): update plugin order on whole cluster #11548

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/emqx/test/emqx_cth_cluster.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
-export([stop/1]).

-export([share_load_module/2]).
-export([node_name/1]).

-define(APPS_CLUSTERING, [gen_rpc, mria, ekka]).

Expand Down
12 changes: 9 additions & 3 deletions apps/emqx_dashboard/test/emqx_dashboard_api_test_helpers.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
multipart_formdata_request/4,
host/0,
uri/0,
uri/1
uri/1,
uri/2
]).

-define(HOST, "http://127.0.0.1:18083").
Expand Down Expand Up @@ -96,10 +97,15 @@ request(Username, Method, Url, Body) ->
host() ->
?HOST.

uri() -> uri([]).
uri() ->
uri([]).

uri(Parts) when is_list(Parts) ->
uri(host(), Parts).

uri(Host, Parts) when is_list(Host), is_list(Parts) ->
NParts = [E || E <- Parts],
host() ++ "/" ++ to_list(filename:join([?BASE_PATH, ?API_VERSION | NParts])).
Host ++ "/" ++ to_list(filename:join([?BASE_PATH, ?API_VERSION | NParts])).

auth_header(Username) ->
Password = <<"public">>,
Expand Down
2 changes: 1 addition & 1 deletion apps/emqx_management/src/emqx_mgmt_api_plugins.erl
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ update_boot_order(post, #{bindings := #{name := Name}, body := Body}) ->
{error, Reason} ->
{400, #{code => 'BAD_POSITION', message => Reason}};
Position ->
case emqx_plugins:ensure_enabled(Name, Position) of
case emqx_plugins:ensure_enabled(Name, Position, _ConfLocation = global) of
ok ->
{200};
{error, Reason} ->
Expand Down
190 changes: 182 additions & 8 deletions apps/emqx_management/test/emqx_mgmt_api_plugins_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
-compile(nowarn_export_all).

-include_lib("eunit/include/eunit.hrl").
-include_lib("common_test/include/ct.hrl").

-define(EMQX_PLUGIN_TEMPLATE_NAME, "emqx_plugin_template").
-define(EMQX_PLUGIN_TEMPLATE_VSN, "5.0.0").
-define(PACKAGE_SUFFIX, ".tar.gz").

-define(CLUSTER_API_SERVER(PORT), ("http://127.0.0.1:" ++ (integer_to_list(PORT)))).

all() ->
emqx_common_test_helpers:all(?MODULE).

Expand All @@ -48,6 +51,25 @@ end_per_suite(Config) ->
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),
ok;
end_per_testcase(_TestCase, _Config) ->
ok.

t_plugins(Config) ->
DemoShDir = proplists:get_value(demo_sh_dir, Config),
PackagePath = get_demo_plugin_package(DemoShDir),
Expand Down Expand Up @@ -141,9 +163,83 @@ t_delete_non_existing(_Config) ->
),
ok.

list_plugins() ->
Path = emqx_mgmt_api_test_util:api_path(["plugins"]),
case emqx_mgmt_api_test_util:request_api(get, Path) of
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.
Expand Down Expand Up @@ -172,16 +268,46 @@ install_plugin(FilePath) ->
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", 200, "OK"}, _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_boot_order(Name, MoveBody) ->
Auth = emqx_mgmt_api_test_util:auth_header_(),
Path = emqx_mgmt_api_test_util:api_path(["plugins", Name, "move"]),
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"]),
case emqx_mgmt_api_test_util:request_api(post, Path, "", Auth, MoveBody) of
{ok, Res} -> {ok, emqx_utils_json:decode(Res, [return_maps])};
Error -> Error
{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) ->
Expand Down Expand Up @@ -218,3 +344,51 @@ update_release_json(["release.json"], FileContent, NewName) ->
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 => filename:join(?config(priv_dir, Config), TestCase)}
).

app_specs(_Config) ->
[
emqx_conf,
emqx,
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}.
19 changes: 16 additions & 3 deletions apps/emqx_management/test/emqx_mgmt_api_test_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ request(Method, Url, Body) ->
uri(Parts) ->
emqx_dashboard_api_test_helpers:uri(Parts).

uri(Host, Parts) ->
emqx_dashboard_api_test_helpers:uri(Host, Parts).

%% compatible_mode will return as same as 'emqx_dashboard_api_test_helpers:request'
request_api_with_body(Method, Url, Body) ->
Opts = #{compatible_mode => true, httpc_req_opts => [{body_format, binary}]},
Expand Down Expand Up @@ -144,9 +147,15 @@ build_http_header(X) when is_list(X) ->
build_http_header(X) ->
[X].

default_server() ->
?SERVER.

api_path(Parts) ->
join_http_path([?SERVER, ?BASE_PATH | Parts]).

api_path(Host, Parts) ->
join_http_path([Host, ?BASE_PATH | Parts]).

api_path_without_base_path(Parts) ->
join_http_path([?SERVER | Parts]).

Expand Down Expand Up @@ -193,9 +202,13 @@ upload_request(URL, FilePath, Name, MimeType, RequestData, AuthorizationToken) -
ContentLength = integer_to_list(length(binary_to_list(RequestBody))),
Headers = [
{"Content-Length", ContentLength},
case AuthorizationToken =/= undefined of
true -> {"Authorization", "Bearer " ++ binary_to_list(AuthorizationToken)};
false -> {}
case AuthorizationToken of
_ when is_tuple(AuthorizationToken) ->
AuthorizationToken;
_ when is_binary(AuthorizationToken) ->
{"Authorization", "Bearer " ++ binary_to_list(AuthorizationToken)};
_ ->
{}
end
],
HTTPOptions = [],
Expand Down
2 changes: 1 addition & 1 deletion apps/emqx_plugins/src/emqx_plugins.app.src
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%% -*- mode: erlang -*-
{application, emqx_plugins, [
{description, "EMQX Plugin Management"},
{vsn, "0.1.5"},
{vsn, "0.1.6"},
{modules, []},
{mod, {emqx_plugins_app, []}},
{applications, [kernel, stdlib, emqx]},
Expand Down