From 0a9cbe30809a7fb8511114f5f7628be8b690d4bc Mon Sep 17 00:00:00 2001 From: Andrew Mayorov Date: Tue, 13 Feb 2024 18:47:28 +0100 Subject: [PATCH] test(license): update and simplify tests --- apps/emqx_license/test/emqx_license_SUITE.erl | 115 ++---------------- .../test/emqx_license_checker_SUITE.erl | 31 ++--- .../test/emqx_license_cli_SUITE.erl | 29 ++--- .../test/emqx_license_http_api_SUITE.erl | 56 ++++----- .../test/emqx_license_parser_SUITE.erl | 32 ++--- .../test/emqx_license_resources_SUITE.erl | 32 ++--- 6 files changed, 92 insertions(+), 203 deletions(-) diff --git a/apps/emqx_license/test/emqx_license_SUITE.erl b/apps/emqx_license/test/emqx_license_SUITE.erl index fcbced33a..f0252495a 100644 --- a/apps/emqx_license/test/emqx_license_SUITE.erl +++ b/apps/emqx_license/test/emqx_license_SUITE.erl @@ -17,121 +17,32 @@ all() -> init_per_suite(Config) -> emqx_license_test_lib:mock_parser(), - _ = application:load(emqx_conf), - emqx_config:save_schema_mod_and_names(emqx_license_schema), - emqx_common_test_helpers:start_apps([emqx_license], fun set_special_configs/1), - Config. + Apps = emqx_cth_suite:start( + [ + emqx, + emqx_conf, + {emqx_license, "license { key = \"default\" }"} + ], + #{work_dir => emqx_cth_suite:work_dir(Config)} + ), + [{suite_apps, Apps} | Config]. -end_per_suite(_) -> +end_per_suite(Config) -> emqx_license_test_lib:unmock_parser(), - emqx_common_test_helpers:stop_apps([emqx_license]), - ok. + ok = emqx_cth_suite:stop(?config(suite_apps, Config)). init_per_testcase(Case, Config) -> - {ok, _} = emqx_cluster_rpc:start_link(node(), emqx_cluster_rpc, 1000), - Paths = set_override_paths(Case), - Config0 = setup_test(Case, Config), - Paths ++ Config0 ++ Config. + setup_test(Case, Config) ++ Config. end_per_testcase(Case, Config) -> - clean_overrides(Case, Config), - teardown_test(Case, Config), - ok. + teardown_test(Case, Config). -set_override_paths(_TestCase) -> - []. - -clean_overrides(_TestCase, _Config) -> - ok. - -setup_test(TestCase, Config) when - TestCase =:= t_update_file_cluster_backup --> - DataDir = ?config(data_dir, Config), - {LicenseKey, _License} = mk_license( - [ - %% license format version - "220111", - %% license type - "0", - %% customer type - "10", - %% customer name - "Foo", - %% customer email - "contact@foo.com", - %% deplayment name - "bar-deployment", - %% start date - "20220111", - %% days - "100000", - %% max connections - "19" - ] - ), - Cluster = emqx_common_test_helpers:emqx_cluster( - [core, core], - [ - {apps, [emqx_conf, emqx_license]}, - {load_schema, false}, - {schema_mod, emqx_enterprise_schema}, - {env_handler, fun - (emqx) -> - emqx_config:save_schema_mod_and_names(emqx_enterprise_schema), - %% emqx_config:save_schema_mod_and_names(emqx_license_schema), - application:set_env(emqx, boot_modules, []), - application:set_env( - emqx, - data_dir, - filename:join([ - DataDir, - TestCase, - node() - ]) - ), - ok; - (emqx_conf) -> - emqx_config:save_schema_mod_and_names(emqx_enterprise_schema), - %% emqx_config:save_schema_mod_and_names(emqx_license_schema), - application:set_env( - emqx, - data_dir, - filename:join([ - DataDir, - TestCase, - node() - ]) - ), - ok; - (emqx_license) -> - set_special_configs(emqx_license), - ok; - (_) -> - ok - end} - ] - ), - Nodes = [emqx_common_test_helpers:start_peer(Name, Opts) || {Name, Opts} <- Cluster], - [{nodes, Nodes}, {cluster, Cluster}, {old_license, LicenseKey}]; setup_test(_TestCase, _Config) -> []. teardown_test(_TestCase, _Config) -> ok. -set_special_configs(emqx_license) -> - Config = #{key => default}, - emqx_config:put([license], Config), - RawConfig = #{<<"key">> => <<"default">>}, - emqx_config:put_raw([<<"license">>], RawConfig); -set_special_configs(_) -> - ok. - -assert_on_nodes(Nodes, RunFun, CheckFun) -> - Res = [{N, erpc:call(N, RunFun)} || N <- Nodes], - lists:foreach(CheckFun, Res). - %%------------------------------------------------------------------------------ %% Tests %%------------------------------------------------------------------------------ diff --git a/apps/emqx_license/test/emqx_license_checker_SUITE.erl b/apps/emqx_license/test/emqx_license_checker_SUITE.erl index 5733a09ce..9519cb0bc 100644 --- a/apps/emqx_license/test/emqx_license_checker_SUITE.erl +++ b/apps/emqx_license/test/emqx_license_checker_SUITE.erl @@ -14,34 +14,35 @@ all() -> emqx_common_test_helpers:all(?MODULE). -init_per_suite(CtConfig) -> - _ = application:load(emqx_conf), +init_per_suite(Config) -> emqx_license_test_lib:mock_parser(), - ok = emqx_common_test_helpers:start_apps([emqx_license], fun set_special_configs/1), - CtConfig. + Apps = emqx_cth_suite:start( + [ + emqx, + emqx_conf, + {emqx_license, #{ + config => #{license => #{key => emqx_license_test_lib:default_test_license()}} + }} + ], + #{work_dir => emqx_cth_suite:work_dir(Config)} + ), + [{suite_apps, Apps} | Config]. -end_per_suite(_) -> +end_per_suite(Config) -> emqx_license_test_lib:unmock_parser(), - ok = emqx_common_test_helpers:stop_apps([emqx_license]). + ok = emqx_cth_suite:stop(?config(suite_apps, Config)). init_per_testcase(t_default_limits, Config) -> - ok = emqx_common_test_helpers:stop_apps([emqx_license]), + ok = application:stop(emqx_license), Config; init_per_testcase(_Case, Config) -> - {ok, _} = emqx_cluster_rpc:start_link(node(), emqx_cluster_rpc, 1000), Config. end_per_testcase(t_default_limits, _Config) -> - ok = emqx_common_test_helpers:start_apps([emqx_license], fun set_special_configs/1); + {ok, _} = application:ensure_all_started(emqx_license); end_per_testcase(_Case, _Config) -> ok. -set_special_configs(emqx_license) -> - Config = #{key => emqx_license_test_lib:default_test_license()}, - emqx_config:put([license], Config); -set_special_configs(_) -> - ok. - %%------------------------------------------------------------------------------ %% Tests %%------------------------------------------------------------------------------ diff --git a/apps/emqx_license/test/emqx_license_cli_SUITE.erl b/apps/emqx_license/test/emqx_license_cli_SUITE.erl index ed6593aac..08d697c08 100644 --- a/apps/emqx_license/test/emqx_license_cli_SUITE.erl +++ b/apps/emqx_license/test/emqx_license_cli_SUITE.erl @@ -14,32 +14,29 @@ all() -> emqx_common_test_helpers:all(?MODULE). init_per_suite(Config) -> - _ = application:load(emqx_conf), - emqx_config:save_schema_mod_and_names(emqx_license_schema), - emqx_common_test_helpers:start_apps([emqx_license], fun set_special_configs/1), - Config. + Apps = emqx_cth_suite:start( + [ + emqx, + emqx_conf, + {emqx_license, #{ + config => #{license => #{key => emqx_license_test_lib:default_license()}} + }} + ], + #{work_dir => emqx_cth_suite:work_dir(Config)} + ), + [{suite_apps, Apps} | Config]. -end_per_suite(_) -> - emqx_common_test_helpers:stop_apps([emqx_license]), - ok. +end_per_suite(Config) -> + ok = emqx_cth_suite:stop(?config(suite_apps, Config)). init_per_testcase(_Case, Config) -> emqx_license_test_lib:mock_parser(), - {ok, _} = emqx_cluster_rpc:start_link(node(), emqx_cluster_rpc, 1000), Config. end_per_testcase(_Case, _Config) -> emqx_license_test_lib:unmock_parser(), ok. -set_special_configs(emqx_license) -> - Config = #{key => emqx_license_test_lib:default_license()}, - emqx_config:put([license], Config), - RawConfig = #{<<"key">> => emqx_license_test_lib:default_license()}, - emqx_config:put_raw([<<"license">>], RawConfig); -set_special_configs(_) -> - ok. - %%------------------------------------------------------------------------------ %% Tests %%------------------------------------------------------------------------------ diff --git a/apps/emqx_license/test/emqx_license_http_api_SUITE.erl b/apps/emqx_license/test/emqx_license_http_api_SUITE.erl index 799e4f591..7d9cfb96f 100644 --- a/apps/emqx_license/test/emqx_license_http_api_SUITE.erl +++ b/apps/emqx_license/test/emqx_license_http_api_SUITE.erl @@ -7,7 +7,6 @@ -compile(nowarn_export_all). -compile(export_all). --include_lib("emqx/include/emqx_mqtt.hrl"). -include_lib("eunit/include/eunit.hrl"). -include_lib("common_test/include/ct.hrl"). @@ -20,41 +19,34 @@ all() -> init_per_suite(Config) -> emqx_license_test_lib:mock_parser(), - _ = application:load(emqx_conf), - emqx_config:save_schema_mod_and_names(emqx_license_schema), - emqx_common_test_helpers:start_apps([emqx_license, emqx_dashboard], fun set_special_configs/1), - Config. + Apps = emqx_cth_suite:start( + [ + emqx, + emqx_conf, + {emqx_license, #{ + config => #{ + license => #{ + key => emqx_license_test_lib:make_license(#{max_connections => "100"}), + connection_low_watermark => <<"75%">>, + connection_high_watermark => <<"80%">> + } + } + }}, + {emqx_dashboard, + "dashboard {" + "\n listeners.http { enable = true, bind = 18083 }" + "\n default_username = \"license_admin\"" + "\n}"} + ], + #{work_dir => emqx_cth_suite:work_dir(Config)} + ), + [{suite_apps, Apps} | Config]. -end_per_suite(_) -> - emqx_common_test_helpers:stop_apps([emqx_license, emqx_dashboard]), - LicenseKey = emqx_license_test_lib:make_license(#{max_connections => "100"}), - Config = #{key => LicenseKey}, - emqx_config:put([license], Config), - RawConfig = #{<<"key">> => LicenseKey}, - emqx_config:put_raw([<<"license">>], RawConfig), +end_per_suite(Config) -> emqx_license_test_lib:unmock_parser(), - ok. - -set_special_configs(emqx_dashboard) -> - emqx_dashboard_api_test_helpers:set_default_config(<<"license_admin">>); -set_special_configs(emqx_license) -> - LicenseKey = emqx_license_test_lib:make_license(#{max_connections => "100"}), - Config = #{ - key => LicenseKey, connection_low_watermark => 0.75, connection_high_watermark => 0.8 - }, - emqx_config:put([license], Config), - RawConfig = #{ - <<"key">> => LicenseKey, - <<"connection_low_watermark">> => <<"75%">>, - <<"connection_high_watermark">> => <<"80%">> - }, - emqx_config:put_raw([<<"license">>], RawConfig), - ok; -set_special_configs(_) -> - ok. + ok = emqx_cth_suite:stop(?config(suite_apps, Config)). init_per_testcase(_TestCase, Config) -> - {ok, _} = emqx_cluster_rpc:start_link(node(), emqx_cluster_rpc, 1000), Config. end_per_testcase(_TestCase, _Config) -> diff --git a/apps/emqx_license/test/emqx_license_parser_SUITE.erl b/apps/emqx_license/test/emqx_license_parser_SUITE.erl index 0315a8a0b..0f06e76c3 100644 --- a/apps/emqx_license/test/emqx_license_parser_SUITE.erl +++ b/apps/emqx_license/test/emqx_license_parser_SUITE.erl @@ -14,26 +14,20 @@ all() -> emqx_common_test_helpers:all(?MODULE). init_per_suite(Config) -> - _ = application:load(emqx_conf), - emqx_common_test_helpers:start_apps([emqx_license], fun set_special_configs/1), - Config. + Apps = emqx_cth_suite:start( + [ + emqx, + emqx_conf, + {emqx_license, #{ + config => #{license => #{key => emqx_license_test_lib:default_license()}} + }} + ], + #{work_dir => emqx_cth_suite:work_dir(Config)} + ), + [{suite_apps, Apps} | Config]. -end_per_suite(_) -> - emqx_common_test_helpers:stop_apps([emqx_license]), - ok. - -init_per_testcase(_Case, Config) -> - {ok, _} = emqx_cluster_rpc:start_link(node(), emqx_cluster_rpc, 1000), - Config. - -end_per_testcase(_Case, _Config) -> - ok. - -set_special_configs(emqx_license) -> - Config = #{key => emqx_license_test_lib:default_license()}, - emqx_config:put([license], Config); -set_special_configs(_) -> - ok. +end_per_suite(Config) -> + ok = emqx_cth_suite:stop(?config(suite_apps, Config)). %%------------------------------------------------------------------------------ %% Tests diff --git a/apps/emqx_license/test/emqx_license_resources_SUITE.erl b/apps/emqx_license/test/emqx_license_resources_SUITE.erl index 66e5d4a61..35510915f 100644 --- a/apps/emqx_license/test/emqx_license_resources_SUITE.erl +++ b/apps/emqx_license/test/emqx_license_resources_SUITE.erl @@ -15,26 +15,20 @@ all() -> emqx_common_test_helpers:all(?MODULE). init_per_suite(Config) -> - _ = application:load(emqx_conf), - emqx_common_test_helpers:start_apps([emqx_license], fun set_special_configs/1), - Config. + Apps = emqx_cth_suite:start( + [ + emqx, + emqx_conf, + {emqx_license, #{ + config => #{license => #{key => emqx_license_test_lib:default_license()}} + }} + ], + #{work_dir => emqx_cth_suite:work_dir(Config)} + ), + [{suite_apps, Apps} | Config]. -end_per_suite(_) -> - emqx_common_test_helpers:stop_apps([emqx_license]), - ok. - -init_per_testcase(_Case, Config) -> - {ok, _} = emqx_cluster_rpc:start_link(node(), emqx_cluster_rpc, 1000), - Config. - -end_per_testcase(_Case, _Config) -> - ok. - -set_special_configs(emqx_license) -> - Config = #{key => emqx_license_test_lib:default_license()}, - emqx_config:put([license], Config); -set_special_configs(_) -> - ok. +end_per_suite(Config) -> + ok = emqx_cth_suite:stop(?config(suite_apps, Config)). %%------------------------------------------------------------------------------ %% Tests