test: allow pre-load configs before emqx_conf app

This commit is contained in:
Zaiming (Stone) Shi 2023-05-01 15:35:33 +02:00
parent 43c80ba635
commit a03f2dd64b
1 changed files with 9 additions and 1 deletions

View File

@ -360,12 +360,13 @@ read_override_confs() ->
hocon:deep_merge(ClusterOverrides, LocalOverrides). hocon:deep_merge(ClusterOverrides, LocalOverrides).
%% keep the raw and non-raw conf has the same keys to make update raw conf easier. %% keep the raw and non-raw conf has the same keys to make update raw conf easier.
%% TODO: remove raw_with_default as it's now always true.
maybe_fill_defaults(SchemaMod, RawConf0, #{raw_with_default := true}) -> maybe_fill_defaults(SchemaMod, RawConf0, #{raw_with_default := true}) ->
RootSchemas = hocon_schema:roots(SchemaMod), RootSchemas = hocon_schema:roots(SchemaMod),
%% the roots which are missing from the loaded configs %% the roots which are missing from the loaded configs
MissingRoots = lists:filtermap( MissingRoots = lists:filtermap(
fun({BinName, Sc}) -> fun({BinName, Sc}) ->
case maps:is_key(BinName, RawConf0) of case maps:is_key(BinName, RawConf0) orelse is_already_loaded(BinName) of
true -> false; true -> false;
false -> {true, Sc} false -> {true, Sc}
end end
@ -383,6 +384,13 @@ maybe_fill_defaults(SchemaMod, RawConf0, #{raw_with_default := true}) ->
maybe_fill_defaults(_SchemaMod, RawConf, _Opts) -> maybe_fill_defaults(_SchemaMod, RawConf, _Opts) ->
RawConf. RawConf.
%% So far, this can only return true when testing.
%% e.g. when testing an app, we need to load its config first
%% then start emqx_conf application which will load the
%% possibly empty config again (then filled with defaults).
is_already_loaded(Name) ->
?MODULE:get_raw([Name], #{}) =/= #{}.
%% if a root is not found in the raw conf, fill it with default values. %% if a root is not found in the raw conf, fill it with default values.
seed_default(Schema) -> seed_default(Schema) ->
case hocon_schema:field_schema(Schema, default) of case hocon_schema:field_schema(Schema, default) of