Explorar el Código

test(license): test setting HTTP API

zhongwencool hace 2 años
padre
commit
43866b8e55

+ 1 - 0
changes/ce/feat-11249.en.md

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

+ 2 - 0
lib-ee/emqx_license/src/emqx_license_http_api.erl

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

+ 2 - 0
lib-ee/emqx_license/src/emqx_license_schema.erl

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

+ 35 - 2
lib-ee/emqx_license/test/emqx_license_http_api_SUITE.erl

@@ -38,9 +38,15 @@ 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},
+    Config = #{
+        key => LicenseKey, connection_low_watermark => 0.75, connection_high_watermark => 0.8
+    },
     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),
     ok = persistent_term:put(
         emqx_license_test_pubkey,
@@ -172,3 +178,30 @@ t_license_upload_key_not_json(_Config) ->
     ),
     assert_untouched_license(),
     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])
+    ).