feat: add api to delete override conf files

used in tests
This commit is contained in:
Zaiming (Stone) Shi 2022-01-25 08:29:04 +01:00
parent 21183f3b16
commit bf7ac80a83
4 changed files with 48 additions and 20 deletions

View File

@ -21,6 +21,7 @@
-export([ init_load/1 -export([ init_load/1
, init_load/2 , init_load/2
, read_override_conf/1 , read_override_conf/1
, delete_override_conf_files/0
, check_config/2 , check_config/2
, fill_defaults/1 , fill_defaults/1
, fill_defaults/2 , fill_defaults/2
@ -252,23 +253,7 @@ init_load(SchemaMod) ->
%% in the rear of the list overrides prior values. %% in the rear of the list overrides prior values.
-spec init_load(module(), [string()] | binary() | hocon:config()) -> ok. -spec init_load(module(), [string()] | binary() | hocon:config()) -> ok.
init_load(SchemaMod, Conf) when is_list(Conf) orelse is_binary(Conf) -> init_load(SchemaMod, Conf) when is_list(Conf) orelse is_binary(Conf) ->
IncDir = include_dirs(), init_load(SchemaMod, parse_hocon(Conf));
ParseOptions = #{format => map, include_dirs => IncDir},
Parser = case is_binary(Conf) of
true -> fun hocon:binary/2;
false -> fun hocon:files/2
end,
case Parser(Conf, ParseOptions) of
{ok, RawRichConf} ->
init_load(SchemaMod, RawRichConf);
{error, Reason} ->
?SLOG(error, #{msg => "failed_to_load_hocon_conf",
reason => Reason,
pwd => file:get_cwd(),
include_dirs => IncDir
}),
error(failed_to_load_hocon_conf)
end;
init_load(SchemaMod, RawConf) when is_map(RawConf) -> init_load(SchemaMod, RawConf) when is_map(RawConf) ->
ok = save_schema_mod_and_names(SchemaMod), ok = save_schema_mod_and_names(SchemaMod),
%% Merge environment varialbe overrides on top %% Merge environment varialbe overrides on top
@ -276,7 +261,6 @@ init_load(SchemaMod, RawConf) when is_map(RawConf) ->
ClusterOverrides = read_override_conf(#{override_to => cluster}), ClusterOverrides = read_override_conf(#{override_to => cluster}),
LocalOverrides = read_override_conf(#{override_to => local}), LocalOverrides = read_override_conf(#{override_to => local}),
Overrides = hocon:deep_merge(ClusterOverrides, LocalOverrides), Overrides = hocon:deep_merge(ClusterOverrides, LocalOverrides),
%% TODO: log overrides
RawConfWithOverrides = hocon:deep_merge(RawConfWithEnvs, Overrides), RawConfWithOverrides = hocon:deep_merge(RawConfWithEnvs, Overrides),
%% check configs against the schema %% check configs against the schema
{_AppEnvs, CheckedConf} = {_AppEnvs, CheckedConf} =
@ -285,6 +269,28 @@ init_load(SchemaMod, RawConf) when is_map(RawConf) ->
ok = save_to_config_map(maps:with(get_atom_root_names(), CheckedConf), ok = save_to_config_map(maps:with(get_atom_root_names(), CheckedConf),
maps:with(RootNames, RawConfWithEnvs)). maps:with(RootNames, RawConfWithEnvs)).
parse_hocon(Conf) ->
IncDirs = include_dirs(),
case do_parse_hocon(Conf, IncDirs) of
{ok, HoconMap} ->
HoconMap;
{error, Reason} ->
?SLOG(error, #{msg => "failed_to_load_hocon_conf",
reason => Reason,
pwd => file:get_cwd(),
include_dirs => IncDirs,
config_file => Conf
}),
error(failed_to_load_hocon_conf)
end.
do_parse_hocon(Conf, IncDirs) ->
Opts = #{format => map, include_dirs => IncDirs},
case is_binary(Conf) of
true -> hocon:binary(Conf, Opts);
false -> hocon:files(Conf, Opts)
end.
include_dirs() -> include_dirs() ->
[filename:join(emqx:data_dir(), "configs")]. [filename:join(emqx:data_dir(), "configs")].
@ -328,6 +334,23 @@ fill_defaults(SchemaMod, RawConf) ->
#{nullable => true, only_fill_defaults => true}, #{nullable => true, only_fill_defaults => true},
root_names_from_conf(RawConf)). root_names_from_conf(RawConf)).
%% @doc Only for test cleanups.
%% Delete override config files.
-spec delete_override_conf_files() -> ok.
delete_override_conf_files() ->
F1 = override_conf_file(#{override_to => local}),
F2 = override_conf_file(#{override_to => cluster}),
ok = ensure_file_deleted(F1),
ok = ensure_file_deleted(F2).
ensure_file_deleted(F) ->
case file:delete(F) of
ok -> ok;
{error, enoent} -> ok;
{error, Reason} -> error({F, Reason})
end.
-spec read_override_conf(map()) -> raw_config(). -spec read_override_conf(map()) -> raw_config().
read_override_conf(#{} = Opts) -> read_override_conf(#{} = Opts) ->
File = override_conf_file(Opts), File = override_conf_file(Opts),

View File

@ -48,6 +48,7 @@
, not_wait_mqtt_payload/1 , not_wait_mqtt_payload/1
, render_config_file/2 , render_config_file/2
, read_schema_configs/2 , read_schema_configs/2
, load_config/2
]). ]).
-define( CERTS_PATH(CertName), filename:join( [ "etc", "certs", CertName ]) ). -define( CERTS_PATH(CertName), filename:join( [ "etc", "certs", CertName ]) ).
@ -428,3 +429,7 @@ copy_certs(emqx_conf, Dest0) ->
os:cmd( ["cp -rf ", From, "/certs ", Dest, "/"]), os:cmd( ["cp -rf ", From, "/certs ", Dest, "/"]),
ok; ok;
copy_certs(_, _) -> ok. copy_certs(_, _) -> ok.
load_config(SchemaModule, Config) ->
ok = emqx_config:delete_override_conf_files(),
ok = emqx_config:init_load(SchemaModule, Config).

View File

@ -59,7 +59,7 @@ init_per_suite(Cfg) ->
meck:expect(emqx_alarm, deactivate, 3, ok), meck:expect(emqx_alarm, deactivate, 3, ok),
_ = emqx_exhook_demo_svr:start(), _ = emqx_exhook_demo_svr:start(),
ok = emqx_config:init_load(emqx_exhook_schema, ?CONF_DEFAULT), ok = emqx_common_test_helpers:load_config(emqx_exhook_schema, ?CONF_DEFAULT),
emqx_common_test_helpers:start_apps([emqx_exhook]), emqx_common_test_helpers:start_apps([emqx_exhook]),
Cfg. Cfg.

View File

@ -50,7 +50,7 @@ init_per_suite(Config) ->
meck:expect(emqx_alarm, deactivate, 3, ok), meck:expect(emqx_alarm, deactivate, 3, ok),
_ = emqx_exhook_demo_svr:start(), _ = emqx_exhook_demo_svr:start(),
ok = emqx_config:init_load(emqx_exhook_schema, ?CONF_DEFAULT), ok = emqx_common_test_helpers:load_config(emqx_exhook_schema, ?CONF_DEFAULT),
emqx_mgmt_api_test_util:init_suite([emqx_exhook]), emqx_mgmt_api_test_util:init_suite([emqx_exhook]),
[Conf] = emqx:get_config([exhook, servers]), [Conf] = emqx:get_config([exhook, servers]),
[{template, Conf} | Config]. [{template, Conf} | Config].