test(plugins): update and simplify test suite

This commit is contained in:
Andrew Mayorov 2024-02-14 19:00:04 +01:00
parent 35bf805504
commit cffb52ab28
No known key found for this signature in database
GPG Key ID: 2837C62ACFBFED5D
1 changed files with 83 additions and 114 deletions

View File

@ -73,23 +73,21 @@ end_per_group(_Group, _Config) ->
ok. ok.
init_per_suite(Config) -> init_per_suite(Config) ->
WorkDir = proplists:get_value(data_dir, Config), WorkDir = emqx_cth_suite:work_dir(Config),
filelib:ensure_path(WorkDir), InstallDir = filename:join([WorkDir, "plugins"]),
OrigInstallDir = emqx_plugins:get_config(install_dir, undefined), Apps = emqx_cth_suite:start(
emqx_common_test_helpers:start_apps([emqx_conf, emqx_plugins]), [
emqx_plugins:put_config(install_dir, WorkDir), emqx_conf,
[{orig_install_dir, OrigInstallDir} | Config]. emqx_ctl,
{emqx_plugins, #{config => #{plugins => #{install_dir => InstallDir}}}}
],
#{work_dir => WorkDir}
),
ok = filelib:ensure_path(InstallDir),
[{suite_apps, Apps}, {install_dir, InstallDir} | Config].
end_per_suite(Config) -> end_per_suite(Config) ->
emqx_common_test_helpers:boot_modules(all), ok = emqx_cth_suite:stop(?config(suite_apps, Config)).
emqx_config:erase(plugins),
%% restore config
case proplists:get_value(orig_install_dir, Config) of
undefined -> ok;
OrigInstallDir -> emqx_plugins:put_config(install_dir, OrigInstallDir)
end,
emqx_common_test_helpers:stop_apps([emqx_plugins, emqx_conf]),
ok.
init_per_testcase(TestCase, Config) -> init_per_testcase(TestCase, Config) ->
emqx_plugins:put_configured([]), emqx_plugins:put_configured([]),
@ -206,7 +204,7 @@ t_demo_install_start_stop_uninstall(Config) ->
%% but since we are using hocon:load to load it %% but since we are using hocon:load to load it
%% ad-hoc test files can be in hocon format %% ad-hoc test files can be in hocon format
write_info_file(Config, NameVsn, Content) -> write_info_file(Config, NameVsn, Content) ->
WorkDir = proplists:get_value(data_dir, Config), WorkDir = proplists:get_value(install_dir, Config),
InfoFile = filename:join([WorkDir, NameVsn, "release.json"]), InfoFile = filename:join([WorkDir, NameVsn, "release.json"]),
ok = filelib:ensure_dir(InfoFile), ok = filelib:ensure_dir(InfoFile),
ok = file:write_file(InfoFile, Content). ok = file:write_file(InfoFile, Content).
@ -371,7 +369,7 @@ t_bad_tar_gz({init, Config}) ->
t_bad_tar_gz({'end', _Config}) -> t_bad_tar_gz({'end', _Config}) ->
ok; ok;
t_bad_tar_gz(Config) -> t_bad_tar_gz(Config) ->
WorkDir = proplists:get_value(data_dir, Config), WorkDir = proplists:get_value(install_dir, Config),
FakeTarTz = filename:join([WorkDir, "fake-vsn.tar.gz"]), FakeTarTz = filename:join([WorkDir, "fake-vsn.tar.gz"]),
ok = file:write_file(FakeTarTz, "a\n"), ok = file:write_file(FakeTarTz, "a\n"),
?assertMatch( ?assertMatch(
@ -396,7 +394,7 @@ t_bad_tar_gz(Config) ->
%% create with incomplete info file %% create with incomplete info file
%% failed install attempts should not leave behind extracted dir %% failed install attempts should not leave behind extracted dir
t_bad_tar_gz2({init, Config}) -> t_bad_tar_gz2({init, Config}) ->
WorkDir = proplists:get_value(data_dir, Config), WorkDir = proplists:get_value(install_dir, Config),
NameVsn = "foo-0.2", NameVsn = "foo-0.2",
%% this an invalid info file content (description missing) %% this an invalid info file content (description missing)
BadInfo = "name=foo, rel_vsn=\"0.2\", rel_apps=[foo]", BadInfo = "name=foo, rel_vsn=\"0.2\", rel_apps=[foo]",
@ -422,7 +420,7 @@ t_bad_tar_gz2(Config) ->
%% test that we even cleanup content that doesn't match the expected name-vsn %% test that we even cleanup content that doesn't match the expected name-vsn
%% pattern %% pattern
t_tar_vsn_content_mismatch({init, Config}) -> t_tar_vsn_content_mismatch({init, Config}) ->
WorkDir = proplists:get_value(data_dir, Config), WorkDir = proplists:get_value(install_dir, Config),
NameVsn = "bad_tar-0.2", NameVsn = "bad_tar-0.2",
%% this an invalid info file content %% this an invalid info file content
BadInfo = "name=foo, rel_vsn=\"0.2\", rel_apps=[\"foo-0.2\"], description=\"lorem ipsum\"", BadInfo = "name=foo, rel_vsn=\"0.2\", rel_apps=[\"foo-0.2\"], description=\"lorem ipsum\"",
@ -606,7 +604,7 @@ t_load_config_from_cli(Config) when is_list(Config) ->
ok. ok.
group_t_copy_plugin_to_a_new_node({init, Config}) -> group_t_copy_plugin_to_a_new_node({init, Config}) ->
WorkDir = proplists:get_value(data_dir, Config), WorkDir = proplists:get_value(install_dir, Config),
FromInstallDir = filename:join(WorkDir, atom_to_list(plugins_copy_from)), FromInstallDir = filename:join(WorkDir, atom_to_list(plugins_copy_from)),
file:del_dir_r(FromInstallDir), file:del_dir_r(FromInstallDir),
ok = filelib:ensure_path(FromInstallDir), ok = filelib:ensure_path(FromInstallDir),
@ -614,25 +612,25 @@ group_t_copy_plugin_to_a_new_node({init, Config}) ->
file:del_dir_r(ToInstallDir), file:del_dir_r(ToInstallDir),
ok = filelib:ensure_path(ToInstallDir), ok = filelib:ensure_path(ToInstallDir),
#{package := Package, release_name := PluginName} = get_demo_plugin_package(FromInstallDir), #{package := Package, release_name := PluginName} = get_demo_plugin_package(FromInstallDir),
[{CopyFrom, CopyFromOpts}, {CopyTo, CopyToOpts}] = Apps = [
emqx_common_test_helpers:emqx_cluster( emqx,
emqx_conf,
emqx_ctl,
emqx_plugins
],
[SpecCopyFrom, SpecCopyTo] =
emqx_cth_cluster:mk_nodespecs(
[ [
{core, plugins_copy_from}, {plugins_copy_from, #{role => core, apps => Apps}},
{core, plugins_copy_to} {plugins_copy_to, #{role => core, apps => Apps}}
], ],
#{ #{
apps => [emqx_conf, emqx_plugins], work_dir => emqx_cth_suite:work_dir(?FUNCTION_NAME, Config)
env => [
{emqx, boot_modules, []}
],
load_schema => false
} }
), ),
CopyFromNode = emqx_common_test_helpers:start_peer( [CopyFromNode] = emqx_cth_cluster:start([SpecCopyFrom#{join_to => undefined}]),
CopyFrom, maps:remove(join_to, CopyFromOpts)
),
ok = rpc:call(CopyFromNode, emqx_plugins, put_config, [install_dir, FromInstallDir]), ok = rpc:call(CopyFromNode, emqx_plugins, put_config, [install_dir, FromInstallDir]),
CopyToNode = emqx_common_test_helpers:start_peer(CopyTo, maps:remove(join_to, CopyToOpts)), [CopyToNode] = emqx_cth_cluster:start([SpecCopyTo#{join_to => undefined}]),
ok = rpc:call(CopyToNode, emqx_plugins, put_config, [install_dir, ToInstallDir]), ok = rpc:call(CopyToNode, emqx_plugins, put_config, [install_dir, ToInstallDir]),
NameVsn = filename:basename(Package, ?PACKAGE_SUFFIX), NameVsn = filename:basename(Package, ?PACKAGE_SUFFIX),
ok = rpc:call(CopyFromNode, emqx_plugins, ensure_installed, [NameVsn]), ok = rpc:call(CopyFromNode, emqx_plugins, ensure_installed, [NameVsn]),
@ -656,16 +654,9 @@ group_t_copy_plugin_to_a_new_node({init, Config}) ->
| Config | Config
]; ];
group_t_copy_plugin_to_a_new_node({'end', Config}) -> group_t_copy_plugin_to_a_new_node({'end', Config}) ->
CopyFromNode = proplists:get_value(copy_from_node, Config), CopyFromNode = ?config(copy_from_node, Config),
CopyToNode = proplists:get_value(copy_to_node, Config), CopyToNode = ?config(copy_to_node, Config),
ok = rpc:call(CopyFromNode, emqx_config, delete_override_conf_files, []), ok = emqx_cth_cluster:stop([CopyFromNode, CopyToNode]);
ok = rpc:call(CopyToNode, emqx_config, delete_override_conf_files, []),
rpc:call(CopyToNode, ekka, leave, []),
rpc:call(CopyFromNode, ekka, leave, []),
ok = emqx_common_test_helpers:stop_peer(CopyToNode),
ok = emqx_common_test_helpers:stop_peer(CopyFromNode),
ok = file:del_dir_r(proplists:get_value(to_install_dir, Config)),
ok = file:del_dir_r(proplists:get_value(from_install_dir, Config));
group_t_copy_plugin_to_a_new_node(Config) -> group_t_copy_plugin_to_a_new_node(Config) ->
CopyFromNode = proplists:get_value(copy_from_node, Config), CopyFromNode = proplists:get_value(copy_from_node, Config),
CopyToNode = proplists:get_value(copy_to_node, Config), CopyToNode = proplists:get_value(copy_to_node, Config),
@ -706,62 +697,48 @@ group_t_copy_plugin_to_a_new_node(Config) ->
%% checks that we can start a cluster with a lone node. %% checks that we can start a cluster with a lone node.
group_t_copy_plugin_to_a_new_node_single_node({init, Config}) -> group_t_copy_plugin_to_a_new_node_single_node({init, Config}) ->
PrivDataDir = ?config(priv_dir, Config), WorkDir = ?config(install_dir, Config),
ToInstallDir = filename:join(PrivDataDir, "plugins_copy_to"), ToInstallDir = filename:join(WorkDir, "plugins_copy_to"),
file:del_dir_r(ToInstallDir), file:del_dir_r(ToInstallDir),
ok = filelib:ensure_path(ToInstallDir), ok = filelib:ensure_path(ToInstallDir),
#{package := Package, release_name := PluginName} = get_demo_plugin_package(ToInstallDir), #{package := Package, release_name := PluginName} = get_demo_plugin_package(ToInstallDir),
NameVsn = filename:basename(Package, ?PACKAGE_SUFFIX), NameVsn = filename:basename(Package, ?PACKAGE_SUFFIX),
[{CopyTo, CopyToOpts}] = Apps = [
emqx_common_test_helpers:emqx_cluster( emqx,
[ emqx_conf,
{core, plugins_copy_to} emqx_ctl,
], {emqx_plugins, #{
#{ config => #{
apps => [emqx_conf, emqx_plugins], plugins => #{
env => [ install_dir => ToInstallDir,
{emqx, boot_modules, []} states => [#{name_vsn => NameVsn, enable => true}]
], }
env_handler => fun
(emqx_plugins) ->
ok = emqx_plugins:put_config(install_dir, ToInstallDir),
%% this is to simulate an user setting the state
%% via environment variables before starting the node
ok = emqx_plugins:put_config(
states,
[#{name_vsn => NameVsn, enable => true}]
),
ok;
(_) ->
ok
end,
priv_data_dir => PrivDataDir,
schema_mod => emqx_conf_schema,
load_schema => true
} }
), }}
],
[CopyToNode] = emqx_cth_cluster:start(
[{plugins_copy_to, #{role => core, apps => Apps}}],
#{
work_dir => emqx_cth_suite:work_dir(?FUNCTION_NAME, Config)
}
),
[ [
{to_install_dir, ToInstallDir}, {to_install_dir, ToInstallDir},
{copy_to_node_name, CopyTo}, {copy_to_node, CopyToNode},
{copy_to_opts, CopyToOpts},
{name_vsn, NameVsn}, {name_vsn, NameVsn},
{plugin_name, PluginName} {plugin_name, PluginName}
| Config | Config
]; ];
group_t_copy_plugin_to_a_new_node_single_node({'end', Config}) -> group_t_copy_plugin_to_a_new_node_single_node({'end', Config}) ->
CopyToNode = proplists:get_value(copy_to_node_name, Config), CopyToNode = proplists:get_value(copy_to_node, Config),
ok = emqx_common_test_helpers:stop_peer(CopyToNode), ok = emqx_cth_cluster:stop([CopyToNode]);
ok = file:del_dir_r(proplists:get_value(to_install_dir, Config)),
ok;
group_t_copy_plugin_to_a_new_node_single_node(Config) -> group_t_copy_plugin_to_a_new_node_single_node(Config) ->
CopyTo = ?config(copy_to_node_name, Config), CopyToNode = ?config(copy_to_node, Config),
CopyToOpts = ?config(copy_to_opts, Config),
ToInstallDir = ?config(to_install_dir, Config), ToInstallDir = ?config(to_install_dir, Config),
NameVsn = proplists:get_value(name_vsn, Config), NameVsn = proplists:get_value(name_vsn, Config),
%% Start the node for the first time. The plugin should start %% Start the node for the first time. The plugin should start
%% successfully even if it's not extracted yet. Simply starting %% successfully even if it's not extracted yet. Simply starting
%% the node would crash if not working properly. %% the node would crash if not working properly.
CopyToNode = emqx_common_test_helpers:start_peer(CopyTo, CopyToOpts),
ct:pal("~p config:\n ~p", [ ct:pal("~p config:\n ~p", [
CopyToNode, erpc:call(CopyToNode, emqx_plugins, get_config, [[], #{}]) CopyToNode, erpc:call(CopyToNode, emqx_plugins, get_config, [[], #{}])
]), ]),
@ -775,52 +752,44 @@ group_t_copy_plugin_to_a_new_node_single_node(Config) ->
ok. ok.
group_t_cluster_leave({init, Config}) -> group_t_cluster_leave({init, Config}) ->
PrivDataDir = ?config(priv_dir, Config), WorkDir = ?config(install_dir, Config),
ToInstallDir = filename:join(PrivDataDir, "plugins_copy_to"), ToInstallDir = filename:join(WorkDir, "plugins_copy_to"),
file:del_dir_r(ToInstallDir), file:del_dir_r(ToInstallDir),
ok = filelib:ensure_path(ToInstallDir), ok = filelib:ensure_path(ToInstallDir),
#{package := Package, release_name := PluginName} = get_demo_plugin_package(ToInstallDir), #{package := Package, release_name := PluginName} = get_demo_plugin_package(ToInstallDir),
NameVsn = filename:basename(Package, ?PACKAGE_SUFFIX), NameVsn = filename:basename(Package, ?PACKAGE_SUFFIX),
Cluster = Apps = [
emqx_common_test_helpers:emqx_cluster( emqx,
[core, core], emqx_conf,
#{ emqx_ctl,
apps => [emqx_conf, emqx_plugins], {emqx_plugins, #{
env => [ config => #{
{emqx, boot_modules, []} plugins => #{
], install_dir => ToInstallDir,
env_handler => fun states => [#{name_vsn => NameVsn, enable => true}]
(emqx_plugins) -> }
ok = emqx_plugins:put_config(install_dir, ToInstallDir),
%% this is to simulate an user setting the state
%% via environment variables before starting the node
ok = emqx_plugins:put_config(
states,
[#{name_vsn => NameVsn, enable => true}]
),
ok;
(_) ->
ok
end,
priv_data_dir => PrivDataDir,
schema_mod => emqx_conf_schema,
load_schema => true
} }
), }}
Nodes = [emqx_common_test_helpers:start_peer(Name, Opts) || {Name, Opts} <- Cluster], ],
Nodes = emqx_cth_cluster:start(
[
{group_t_cluster_leave1, #{role => core, apps => Apps}},
{group_t_cluster_leave2, #{role => core, apps => Apps}}
],
#{
work_dir => emqx_cth_suite:work_dir(?FUNCTION_NAME, Config)
}
),
[ [
{to_install_dir, ToInstallDir}, {to_install_dir, ToInstallDir},
{cluster, Cluster},
{nodes, Nodes}, {nodes, Nodes},
{name_vsn, NameVsn}, {name_vsn, NameVsn},
{plugin_name, PluginName} {plugin_name, PluginName}
| Config | Config
]; ];
group_t_cluster_leave({'end', Config}) -> group_t_cluster_leave({'end', Config}) ->
Nodes = proplists:get_value(nodes, Config), Nodes = ?config(nodes, Config),
[ok = emqx_common_test_helpers:stop_peer(N) || N <- Nodes], ok = emqx_cth_cluster:stop(Nodes);
ok = file:del_dir_r(proplists:get_value(to_install_dir, Config)),
ok;
group_t_cluster_leave(Config) -> group_t_cluster_leave(Config) ->
[N1, N2] = ?config(nodes, Config), [N1, N2] = ?config(nodes, Config),
NameVsn = proplists:get_value(name_vsn, Config), NameVsn = proplists:get_value(name_vsn, Config),