Просмотр исходного кода

Merge pull request #12161 from zhongwencool/config-update-error-text

fix: ensure config update error text is readable
zhongwencool 2 лет назад
Родитель
Сommit
b46438efc9

+ 7 - 2
apps/emqx_audit/test/emqx_audit_api_SUITE.erl

@@ -208,6 +208,8 @@ t_cli(_Config) ->
 
 t_max_size(_Config) ->
     {ok, _} = emqx:update_config([log, audit, max_filter_size], 999),
+    %% Make sure this process is using latest max_filter_size.
+    ?assertEqual(ignore, gen_server:call(emqx_audit, whatever)),
     SizeFun =
         fun() ->
             AuditPath = emqx_mgmt_api_test_util:api_path(["audit"]),
@@ -297,6 +299,9 @@ wait_for_dirty_write_log_done(Prev, RemainMs) ->
     SleepMs = 100,
     ct:sleep(SleepMs),
     case mnesia:table_info(emqx_audit, size) of
-        Prev -> Prev;
-        New -> wait_for_dirty_write_log_done(New, RemainMs - SleepMs)
+        Prev ->
+            ct:sleep(SleepMs * 2),
+            Prev;
+        New ->
+            wait_for_dirty_write_log_done(New, RemainMs - SleepMs)
     end.

+ 6 - 2
apps/emqx_management/src/emqx_mgmt_api_configs.erl

@@ -354,8 +354,12 @@ configs(get, #{query_string := QueryStr, headers := Headers}, _Req) ->
     end;
 configs(put, #{body := Conf, query_string := #{<<"mode">> := Mode}}, _Req) ->
     case emqx_conf_cli:load_config(Conf, #{mode => Mode, log => none}) of
-        ok -> {200};
-        {error, Msg} -> {400, #{<<"content-type">> => <<"text/plain">>}, Msg}
+        ok ->
+            {200};
+        {error, MsgList} ->
+            JsonFun = fun(K, V) -> {K, emqx_utils_maps:binary_string(V)} end,
+            JsonMap = emqx_utils_maps:jsonable_map(maps:from_list(MsgList), JsonFun),
+            {400, #{<<"content-type">> => <<"text/plain">>}, JsonMap}
     end.
 
 find_suitable_accept(Headers, Preferences) when is_list(Preferences), length(Preferences) > 0 ->

+ 22 - 5
apps/emqx_management/test/emqx_mgmt_api_configs_SUITE.erl

@@ -70,7 +70,7 @@ t_update(_Config) ->
     %% update failed
     ErrorSysMon = emqx_utils_maps:deep_put([<<"vm">>, <<"busy_port">>], SysMon, "123"),
     ?assertMatch(
-        {error, {"HTTP/1.1", 400, _}},
+        {error, {"HTTP/1.1", 400, "Bad Request"}},
         update_config(<<"sysmon">>, ErrorSysMon)
     ),
     {ok, SysMon2} = get_config(<<"sysmon">>),
@@ -328,6 +328,18 @@ t_configs_key(_Config) ->
     Log1 = emqx_utils_maps:deep_put([<<"log">>, <<"console">>, <<"level">>], Log, <<"error">>),
     ?assertEqual([], update_configs_with_binary(iolist_to_binary(hocon_pp:do(Log1, #{})))),
     ?assertEqual(<<"error">>, read_conf([<<"log">>, <<"console">>, <<"level">>])),
+    BadLog = emqx_utils_maps:deep_put([<<"log">>, <<"console">>, <<"level">>], Log, <<"erro1r">>),
+    {error, Error} = update_configs_with_binary(iolist_to_binary(hocon_pp:do(BadLog, #{}))),
+    ExpectError = #{
+        <<"log">> =>
+            #{
+                <<"kind">> => <<"validation_error">>,
+                <<"path">> => <<"log.console.level">>,
+                <<"reason">> => <<"unable_to_convert_to_enum_symbol">>,
+                <<"value">> => <<"erro1r">>
+            }
+    },
+    ?assertEqual(ExpectError, emqx_utils_json:decode(Error, [return_maps])),
     ok.
 
 t_get_configs_in_different_accept(_Config) ->
@@ -348,7 +360,7 @@ t_get_configs_in_different_accept(_Config) ->
         end
     end,
 
-    %% returns text/palin if text/plain is acceptable
+    %% returns text/plain if text/plain is acceptable
     ?assertMatch({200, "text/plain", _}, Request(<<"text/plain">>)),
     ?assertMatch({200, "text/plain", _}, Request(<<"*/*">>)),
     ?assertMatch(
@@ -416,9 +428,14 @@ update_configs_with_binary(Bin) ->
     Auth = emqx_mgmt_api_test_util:auth_header_(),
     Headers = [{"accept", "text/plain"}, Auth],
     case httpc:request(put, {Path, Headers, "text/plain", Bin}, [], []) of
-        {ok, {_, _, Res}} -> Res;
-        {ok, Res} -> Res;
-        Error -> Error
+        {ok, {{"HTTP/1.1", Code, _}, _Headers, Body}} when
+            Code >= 200 andalso Code =< 299
+        ->
+            Body;
+        {ok, {{"HTTP/1.1", _Code, _}, _Headers, Body}} ->
+            {error, Body};
+        Error ->
+            Error
     end.
 
 update_config(Name, Change) ->