|
|
@@ -1,5 +1,17 @@
|
|
|
%%--------------------------------------------------------------------
|
|
|
-%% Copyright (c) 2022-2023 EMQ Technologies Co., Ltd. All Rights Reserved.
|
|
|
+%% Copyright (c) 2023 EMQ Technologies Co., Ltd. All Rights Reserved.
|
|
|
+%%
|
|
|
+%% Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
+%% you may not use this file except in compliance with the License.
|
|
|
+%% You may obtain a copy of the License at
|
|
|
+%%
|
|
|
+%% http://www.apache.org/licenses/LICENSE-2.0
|
|
|
+%%
|
|
|
+%% Unless required by applicable law or agreed to in writing, software
|
|
|
+%% distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
+%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
+%% See the License for the specific language governing permissions and
|
|
|
+%% limitations under the License.
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
|
-module(emqx_conf_schema_tests).
|
|
|
@@ -488,3 +500,60 @@ check(Config) ->
|
|
|
atom_key => false, required => false, format => map
|
|
|
}),
|
|
|
emqx_utils_maps:unsafe_atom_key_map(Conf).
|
|
|
+
|
|
|
+with_file(Path, Content, F) ->
|
|
|
+ ok = file:write_file(Path, Content),
|
|
|
+ try
|
|
|
+ F()
|
|
|
+ after
|
|
|
+ file:delete(Path)
|
|
|
+ end.
|
|
|
+
|
|
|
+load_and_check_test_() ->
|
|
|
+ [
|
|
|
+ {"non-existing file", fun() ->
|
|
|
+ File = "/tmp/nonexistingfilename.hocon",
|
|
|
+ ?assertEqual(
|
|
|
+ {error, {enoent, File}},
|
|
|
+ emqx_hocon:load_and_check(emqx_conf_schema, File)
|
|
|
+ )
|
|
|
+ end},
|
|
|
+ {"bad syntax", fun() ->
|
|
|
+ %% use abs path to match error return
|
|
|
+ File = "/tmp/emqx-conf-bad-syntax-test.hocon",
|
|
|
+ with_file(
|
|
|
+ File,
|
|
|
+ "{",
|
|
|
+ fun() ->
|
|
|
+ ?assertMatch(
|
|
|
+ {error, #{file := File}},
|
|
|
+ emqx_hocon:load_and_check(emqx_conf_schema, File)
|
|
|
+ )
|
|
|
+ end
|
|
|
+ )
|
|
|
+ end},
|
|
|
+ {"type-check failure", fun() ->
|
|
|
+ File = "emqx-conf-type-check-failure.hocon",
|
|
|
+ %% typecheck fail because cookie is required field
|
|
|
+ with_file(
|
|
|
+ File,
|
|
|
+ "node {}",
|
|
|
+ fun() ->
|
|
|
+ ?assertMatch(
|
|
|
+ {error, #{
|
|
|
+ kind := validation_error,
|
|
|
+ path := "node.cookie",
|
|
|
+ reason := required_field
|
|
|
+ }},
|
|
|
+ emqx_hocon:load_and_check(emqx_conf_schema, File)
|
|
|
+ )
|
|
|
+ end
|
|
|
+ )
|
|
|
+ end},
|
|
|
+ {"ok load", fun() ->
|
|
|
+ File = "emqx-conf-test-tmp-file-load-ok.hocon",
|
|
|
+ with_file(File, "plugins: {}", fun() ->
|
|
|
+ ?assertMatch({ok, _}, emqx_hocon:load_and_check(emqx_conf_schema, File))
|
|
|
+ end)
|
|
|
+ end}
|
|
|
+ ].
|