refactor: allow testing different plugin repos more easily

This commit is contained in:
Thales Macedo Garitezi 2022-02-22 10:49:05 -03:00
parent f173a2f61f
commit b784c2e1ec
No known key found for this signature in database
GPG Key ID: DD279F8152A9B6DD
2 changed files with 54 additions and 15 deletions

View File

@ -56,16 +56,37 @@ end_per_testcase(TestCase, Config) ->
?MODULE:TestCase({'end', Config}). ?MODULE:TestCase({'end', Config}).
build_demo_plugin_package() -> build_demo_plugin_package() ->
build_demo_plugin_package(
#{ target_path => "_build/default/emqx_plugrel"
, release_name => "emqx_plugin_template"
, git_url => "https://github.com/emqx/emqx-plugin-template.git"
, vsn => ?EMQX_PLUGIN_TEMPLATE_VSN
, workdir => "demo_src"
}).
build_demo_plugin_package(#{ target_path := TargetPath
, release_name := ReleaseName
, git_url := GitUrl
, vsn := PluginVsn
, workdir := DemoWorkDir
} = Opts) ->
WorkDir = emqx_plugins:install_dir(), WorkDir = emqx_plugins:install_dir(),
BuildSh = filename:join([WorkDir, "build-demo-plugin.sh"]), BuildSh = filename:join([WorkDir, "build-demo-plugin.sh"]),
case emqx_run_sh:do(BuildSh ++ " " ++ ?EMQX_PLUGIN_TEMPLATE_VSN, Cmd = string:join([ BuildSh
[{cd, WorkDir}]) of , PluginVsn
, TargetPath
, ReleaseName
, GitUrl
, DemoWorkDir
],
" "),
case emqx_run_sh:do(Cmd, [{cd, WorkDir}]) of
{ok, _} -> {ok, _} ->
Pkg = filename:join([WorkDir, "emqx_plugin_template-" ++ Pkg = filename:join([WorkDir, ReleaseName ++ "-" ++
?EMQX_PLUGIN_TEMPLATE_VSN ++ PluginVsn ++
?PACKAGE_SUFFIX]), ?PACKAGE_SUFFIX]),
case filelib:is_regular(Pkg) of case filelib:is_regular(Pkg) of
true -> Pkg; true -> Opts#{package => Pkg};
false -> error(#{reason => unexpected_build_result, not_found => Pkg}) false -> error(#{reason => unexpected_build_result, not_found => Pkg})
end; end;
{error, {Rc, Output}} -> {error, {Rc, Output}} ->
@ -78,12 +99,18 @@ bin(L) when is_list(L) -> unicode:characters_to_binary(L, utf8);
bin(B) when is_binary(B) -> B. bin(B) when is_binary(B) -> B.
t_demo_install_start_stop_uninstall({init, Config}) -> t_demo_install_start_stop_uninstall({init, Config}) ->
Package = build_demo_plugin_package(), Opts = #{package := Package} = build_demo_plugin_package(),
NameVsn = filename:basename(Package, ?PACKAGE_SUFFIX), NameVsn = filename:basename(Package, ?PACKAGE_SUFFIX),
[{name_vsn, NameVsn} | Config]; [ {name_vsn, NameVsn}
, {plugin_opts, Opts}
| Config
];
t_demo_install_start_stop_uninstall({'end', _Config}) -> ok; t_demo_install_start_stop_uninstall({'end', _Config}) -> ok;
t_demo_install_start_stop_uninstall(Config) -> t_demo_install_start_stop_uninstall(Config) ->
NameVsn = proplists:get_value(name_vsn, Config), NameVsn = proplists:get_value(name_vsn, Config),
#{ release_name := ReleaseName
, vsn := PluginVsn
} = proplists:get_value(plugin_opts, Config),
ok = emqx_plugins:ensure_installed(NameVsn), ok = emqx_plugins:ensure_installed(NameVsn),
%% idempotent %% idempotent
ok = emqx_plugins:ensure_installed(NameVsn), ok = emqx_plugins:ensure_installed(NameVsn),
@ -111,8 +138,16 @@ t_demo_install_start_stop_uninstall(Config) ->
ok = assert_app_running(emqx_plugin_template, false), ok = assert_app_running(emqx_plugin_template, false),
ok = assert_app_running(map_sets, false), ok = assert_app_running(map_sets, false),
%% still listed after stopped %% still listed after stopped
?assertMatch([#{<<"name">> := <<"emqx_plugin_template">>, ReleaseNameBin = list_to_binary(ReleaseName),
<<"rel_vsn">> := <<?EMQX_PLUGIN_TEMPLATE_VSN>> PluginVsnBin = list_to_binary(PluginVsn),
?assertMatch([#{<<"name">> := ReleaseNameBin,
<<"rel_vsn">> := PluginVsnBin
}], emqx_plugins:list()),
ok = emqx_plugins:ensure_uninstalled(NameVsn),
?assertEqual([], emqx_plugins:list()),
ok.
?assertMatch([#{<<"name">> := ReleaseName,
<<"rel_vsn">> := PluginVsn
}], emqx_plugins:list()), }], emqx_plugins:list()),
ok = emqx_plugins:ensure_uninstalled(NameVsn), ok = emqx_plugins:ensure_uninstalled(NameVsn),
?assertEqual([], emqx_plugins:list()), ?assertEqual([], emqx_plugins:list()),
@ -129,7 +164,7 @@ write_info_file(Config, NameVsn, Content) ->
ok = file:write_file(InfoFile, Content). ok = file:write_file(InfoFile, Content).
t_start_restart_and_stop({init, Config}) -> t_start_restart_and_stop({init, Config}) ->
Package = build_demo_plugin_package(), #{package := Package} = build_demo_plugin_package(),
NameVsn = filename:basename(Package, ?PACKAGE_SUFFIX), NameVsn = filename:basename(Package, ?PACKAGE_SUFFIX),
[{name_vsn, NameVsn} | Config]; [{name_vsn, NameVsn} | Config];
t_start_restart_and_stop({'end', _Config}) -> ok; t_start_restart_and_stop({'end', _Config}) -> ok;
@ -175,7 +210,7 @@ t_start_restart_and_stop(Config) ->
ok. ok.
t_enable_disable({init, Config}) -> t_enable_disable({init, Config}) ->
Package = build_demo_plugin_package(), #{package := Package} = build_demo_plugin_package(),
NameVsn = filename:basename(Package, ?PACKAGE_SUFFIX), NameVsn = filename:basename(Package, ?PACKAGE_SUFFIX),
[{name_vsn, NameVsn} | Config]; [{name_vsn, NameVsn} | Config];
t_enable_disable({'end', Config}) -> t_enable_disable({'end', Config}) ->

View File

@ -3,9 +3,13 @@
set -euo pipefail set -euo pipefail
vsn="${1}" vsn="${1}"
workdir="demo_src" target_path="${2}"
target_name="emqx_plugin_template-${vsn}.tar.gz" release_name="${3}"
target="$workdir/_build/default/emqx_plugrel/${target_name}" git_url="${4}"
workdir="${5}"
target_name="${release_name}-${vsn}.tar.gz"
target="$workdir/${target_path}/${target_name}"
if [ -f "${target}" ]; then if [ -f "${target}" ]; then
cp "$target" ./ cp "$target" ./
exit 0 exit 0
@ -14,7 +18,7 @@ fi
# cleanup # cleanup
rm -rf "${workdir}" rm -rf "${workdir}"
git clone https://github.com/emqx/emqx-plugin-template.git -b "${vsn}" ${workdir} git clone "${git_url}" -b "${vsn}" "${workdir}"
make -C "$workdir" rel make -C "$workdir" rel
cp "$target" ./ cp "$target" ./