test: avoid stopping cover and using meck

This commit is contained in:
Thales Macedo Garitezi 2022-08-04 10:22:04 -03:00
parent 69ec76f586
commit 7e6b94fc12
3 changed files with 14 additions and 26 deletions

View File

@ -72,9 +72,16 @@
%% API
%%--------------------------------------------------------------------
-ifdef(TEST).
-spec parse(string() | binary()) -> {ok, license()} | {error, term()}.
parse(Content) ->
PubKey = persistent_term:get({emqx_license_parser_test, pubkey}, ?PUBKEY),
parse(Content, PubKey).
-else.
-spec parse(string() | binary()) -> {ok, license()} | {error, term()}.
parse(Content) ->
parse(Content, ?PUBKEY).
-endif.
parse(Content, Pem) ->
[PemEntry] = public_key:pem_decode(Pem),

View File

@ -16,9 +16,6 @@ all() ->
emqx_common_test_helpers:all(?MODULE).
init_per_suite(Config) ->
%% hack needed to avoid inter-suite flakiness when using meck...
ok = cover:stop(),
{ok, _} = cover:start(),
_ = 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),
@ -144,18 +141,9 @@ setup_test(TestCase, Config) when
emqx_config:put([license], LicConfig),
RawConfig = #{<<"type">> => file, <<"file">> => LicensePath},
emqx_config:put_raw([<<"license">>], RawConfig),
ok = meck:new(emqx_license_parser, [
non_strict, passthrough, no_history, no_link
]),
meck:expect(
emqx_license_parser,
parse,
fun(X) ->
emqx_license_parser:parse(
X,
emqx_license_test_lib:public_key_pem()
)
end
ok = persistent_term:put(
{emqx_license_parser_test, pubkey},
emqx_license_test_lib:public_key_pem()
),
ok;
(_) ->

View File

@ -25,12 +25,12 @@ init_per_suite(Config) ->
Config.
end_per_suite(_) ->
ok = meck:unload([emqx_license_parser]),
emqx_common_test_helpers:stop_apps([emqx_license, emqx_dashboard]),
Config = #{type => file, file => emqx_license_test_lib:default_license()},
emqx_config:put([license], Config),
RawConfig = #{<<"type">> => file, <<"file">> => emqx_license_test_lib:default_license()},
emqx_config:put_raw([<<"license">>], RawConfig),
persistent_term:erase({emqx_license_parser_test, pubkey}),
ok.
set_special_configs(emqx_dashboard) ->
@ -41,16 +41,9 @@ set_special_configs(emqx_license) ->
emqx_config:put([license], Config),
RawConfig = #{<<"type">> => key, <<"key">> => LicenseKey},
emqx_config:put_raw([<<"license">>], RawConfig),
ok = meck:new(emqx_license_parser, [non_strict, passthrough, no_history, no_link]),
ok = meck:expect(
emqx_license_parser,
parse,
fun(X) ->
emqx_license_parser:parse(
X,
emqx_license_test_lib:public_key_pem()
)
end
ok = persistent_term:put(
{emqx_license_parser_test, pubkey},
emqx_license_test_lib:public_key_pem()
),
ok;
set_special_configs(_) ->