test(license): test setting HTTP API

This commit is contained in:
zhongwencool 2023-07-12 14:07:13 +08:00
parent f03d4d090e
commit 43866b8e55
4 changed files with 40 additions and 2 deletions

View File

@ -0,0 +1 @@
Support HTTP API for setting alarm watermark of license.

View File

@ -164,11 +164,13 @@ fields(key_license) ->
%% FIXME: remove when 5.2.0 %% FIXME: remove when 5.2.0
{connection_low_watermark, #{ {connection_low_watermark, #{
type => emqx_schema:percent(), type => emqx_schema:percent(),
example => <<"75%">>,
deprecated => true, deprecated => true,
desc => ?DESC(connection_low_watermark_field_deprecated) desc => ?DESC(connection_low_watermark_field_deprecated)
}}, }},
{connection_high_watermark, #{ {connection_high_watermark, #{
type => emqx_schema:percent(), type => emqx_schema:percent(),
example => <<"80%">>,
deprecated => true, deprecated => true,
desc => ?DESC(connection_high_watermark_field_deprecated) desc => ?DESC(connection_high_watermark_field_deprecated)
}} }}

View File

@ -46,11 +46,13 @@ fields(key_license) ->
{connection_low_watermark, #{ {connection_low_watermark, #{
type => emqx_schema:percent(), type => emqx_schema:percent(),
default => <<"75%">>, default => <<"75%">>,
example => <<"75%">>,
desc => ?DESC(connection_low_watermark_field) desc => ?DESC(connection_low_watermark_field)
}}, }},
{connection_high_watermark, #{ {connection_high_watermark, #{
type => emqx_schema:percent(), type => emqx_schema:percent(),
default => <<"80%">>, default => <<"80%">>,
example => <<"80%">>,
desc => ?DESC(connection_high_watermark_field) desc => ?DESC(connection_high_watermark_field)
}} }}
]. ].

View File

@ -38,9 +38,15 @@ set_special_configs(emqx_dashboard) ->
emqx_dashboard_api_test_helpers:set_default_config(<<"license_admin">>); emqx_dashboard_api_test_helpers:set_default_config(<<"license_admin">>);
set_special_configs(emqx_license) -> set_special_configs(emqx_license) ->
LicenseKey = emqx_license_test_lib:make_license(#{max_connections => "100"}), LicenseKey = emqx_license_test_lib:make_license(#{max_connections => "100"}),
Config = #{key => LicenseKey}, Config = #{
key => LicenseKey, connection_low_watermark => 0.75, connection_high_watermark => 0.8
},
emqx_config:put([license], Config), emqx_config:put([license], Config),
RawConfig = #{<<"key">> => LicenseKey}, RawConfig = #{
<<"key">> => LicenseKey,
<<"connection_low_watermark">> => <<"75%">>,
<<"connection_high_watermark">> => <<"80%">>
},
emqx_config:put_raw([<<"license">>], RawConfig), emqx_config:put_raw([<<"license">>], RawConfig),
ok = persistent_term:put( ok = persistent_term:put(
emqx_license_test_pubkey, emqx_license_test_pubkey,
@ -172,3 +178,30 @@ t_license_upload_key_not_json(_Config) ->
), ),
assert_untouched_license(), assert_untouched_license(),
ok. ok.
t_license_setting(_Config) ->
%% get
GetRes = request(get, uri(["license", "setting"]), []),
validate_setting(GetRes, <<"75%">>, <<"80%">>),
%% update
Low = <<"50%">>,
High = <<"55%">>,
UpdateRes = request(post, uri(["license", "setting"]), #{
<<"connection_low_watermark">> => Low,
<<"connection_high_watermark">> => High
}),
validate_setting(UpdateRes, Low, High),
?assertEqual(0.5, emqx_config:get([license, connection_low_watermark])),
?assertEqual(0.55, emqx_config:get([license, connection_high_watermark])),
ok.
validate_setting(Res, ExpectLow, ExpectHigh) ->
?assertMatch({ok, 200, _}, Res),
{ok, 200, Payload} = Res,
?assertEqual(
#{
<<"connection_low_watermark">> => ExpectLow,
<<"connection_high_watermark">> => ExpectHigh
},
emqx_utils_json:decode(Payload, [return_maps])
).