Browse Source

feat: node array support array(atom()) and comma_separated_atoms

某文 2 years atrás
parent
commit
933e6727ba

+ 5 - 2
apps/emqx_conf/src/emqx_conf_schema.erl

@@ -135,7 +135,7 @@ fields("cluster") ->
             )},
         {"core_nodes",
             sc(
-                emqx_schema:comma_separated_atoms(),
+                node_array(),
                 #{
                     mapping => "mria.core_nodes",
                     default => [],
@@ -203,7 +203,7 @@ fields(cluster_static) ->
     [
         {"seeds",
             sc(
-                hoconsc:array(atom()),
+                node_array(),
                 #{
                     default => [],
                     desc => ?DESC(cluster_static_seeds),
@@ -1312,3 +1312,6 @@ validator_string_re(Val, RE, Error) ->
     catch
         _:_ -> {error, Error}
     end.
+
+node_array() ->
+    hoconsc:union([emqx_schema:comma_separated_atoms(), hoconsc:array(atom())]).

+ 40 - 0
apps/emqx_conf/test/emqx_conf_schema_tests.erl

@@ -5,6 +5,46 @@
 -module(emqx_conf_schema_tests).
 
 -include_lib("eunit/include/eunit.hrl").
+array_nodes_test() ->
+    ExpectNodes = ['emqx1@127.0.0.1', 'emqx2@127.0.0.1'],
+    BaseConf =
+        ""
+        "\n"
+        "     node {\n"
+        "        name = \"emqx1@127.0.0.1\"\n"
+        "        cookie = \"emqxsecretcookie\"\n"
+        "        data_dir = \"data\"\n"
+        "     }\n"
+        "     cluster {\n"
+        "        name = emqxcl\n"
+        "        discovery_strategy = static\n"
+        "        static.seeds = ~p\n"
+        "        core_nodes = ~p\n"
+        "     }\n"
+        "   "
+        "",
+    lists:foreach(
+        fun(Nodes) ->
+            ConfFile = iolist_to_binary(io_lib:format(BaseConf, [Nodes, Nodes])),
+            {ok, Conf} = hocon:binary(ConfFile, #{format => richmap}),
+            ConfList = hocon_tconf:generate(emqx_conf_schema, Conf),
+            ClusterDiscovery = proplists:get_value(
+                cluster_discovery, proplists:get_value(ekka, ConfList)
+            ),
+            ?assertEqual(
+                {static, [{seeds, ExpectNodes}]},
+                ClusterDiscovery,
+                Nodes
+            ),
+            ?assertEqual(
+                ExpectNodes,
+                proplists:get_value(core_nodes, proplists:get_value(mria, ConfList)),
+                Nodes
+            )
+        end,
+        [["emqx1@127.0.0.1", "emqx2@127.0.0.1"], "emqx1@127.0.0.1, emqx2@127.0.0.1"]
+    ),
+    ok.
 
 doc_gen_test() ->
     %% the json file too large to encode.