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

feat: add api to delete override conf files

used in tests
Zaiming (Stone) Shi 4 лет назад
Родитель
Сommit
bf7ac80a83

+ 41 - 18
apps/emqx/src/emqx_config.erl

@@ -21,6 +21,7 @@
 -export([ init_load/1
         , init_load/2
         , read_override_conf/1
+        , delete_override_conf_files/0
         , check_config/2
         , fill_defaults/1
         , fill_defaults/2
@@ -252,23 +253,7 @@ init_load(SchemaMod) ->
 %% in the rear of the list overrides prior values.
 -spec init_load(module(), [string()] | binary() | hocon:config()) -> ok.
 init_load(SchemaMod, Conf) when is_list(Conf) orelse is_binary(Conf) ->
-    IncDir = include_dirs(),
-    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, parse_hocon(Conf));
 init_load(SchemaMod, RawConf) when is_map(RawConf) ->
     ok = save_schema_mod_and_names(SchemaMod),
     %% 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}),
     LocalOverrides = read_override_conf(#{override_to => local}),
     Overrides = hocon:deep_merge(ClusterOverrides, LocalOverrides),
-    %% TODO: log overrides
     RawConfWithOverrides = hocon:deep_merge(RawConfWithEnvs, Overrides),
     %% check configs against the schema
     {_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),
                             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() ->
     [filename:join(emqx:data_dir(), "configs")].
 
@@ -328,6 +334,23 @@ fill_defaults(SchemaMod, RawConf) ->
         #{nullable => true, only_fill_defaults => true},
         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().
 read_override_conf(#{} = Opts) ->
     File = override_conf_file(Opts),

+ 5 - 0
apps/emqx/test/emqx_common_test_helpers.erl

@@ -48,6 +48,7 @@
         , not_wait_mqtt_payload/1
         , render_config_file/2
         , read_schema_configs/2
+        , load_config/2
         ]).
 
 -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, "/"]),
     ok;
 copy_certs(_, _) -> ok.
+
+load_config(SchemaModule, Config) ->
+    ok = emqx_config:delete_override_conf_files(),
+    ok = emqx_config:init_load(SchemaModule, Config).

+ 1 - 1
apps/emqx_exhook/test/emqx_exhook_SUITE.erl

@@ -59,7 +59,7 @@ init_per_suite(Cfg) ->
     meck:expect(emqx_alarm, deactivate, 3, ok),
 
     _ = 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]),
     Cfg.
 

+ 1 - 1
apps/emqx_exhook/test/emqx_exhook_api_SUITE.erl

@@ -50,7 +50,7 @@ init_per_suite(Config) ->
     meck:expect(emqx_alarm, deactivate, 3, ok),
 
     _ = 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]),
     [Conf] = emqx:get_config([exhook, servers]),
     [{template, Conf} | Config].