ソースを参照

test: fix addrinuse errors in listeners API test SUITE

Zaiming (Stone) Shi 3 年 前
コミット
3d6756cbe8
1 ファイル変更49 行追加18 行削除
  1. 49 18
      apps/emqx_management/test/emqx_mgmt_api_listeners_SUITE.erl

+ 49 - 18
apps/emqx_management/test/emqx_mgmt_api_listeners_SUITE.erl

@@ -20,6 +20,8 @@
 
 
 -include_lib("eunit/include/eunit.hrl").
 -include_lib("eunit/include/eunit.hrl").
 
 
+-define(PORT, (20000 + ?LINE)).
+
 all() ->
 all() ->
     emqx_common_test_helpers:all(?MODULE).
     emqx_common_test_helpers:all(?MODULE).
 
 
@@ -32,13 +34,38 @@ end_per_suite(_) ->
     emqx_conf:remove([listeners, tcp, new1], #{override_to => local}),
     emqx_conf:remove([listeners, tcp, new1], #{override_to => local}),
     emqx_mgmt_api_test_util:end_suite([emqx_conf]).
     emqx_mgmt_api_test_util:end_suite([emqx_conf]).
 
 
-t_max_connection_default(_Config) ->
+init_per_testcase(Case, Config) ->
+    try
+        ?MODULE:Case({init, Config})
+    catch
+        error:function_clause ->
+            Config
+    end.
+
+end_per_testcase(Case, Config) ->
+    try
+        ?MODULE:Case({'end', Config})
+    catch
+        error:function_clause ->
+            ok
+    end.
+
+t_max_connection_default({init, Config}) ->
     emqx_mgmt_api_test_util:end_suite([emqx_conf]),
     emqx_mgmt_api_test_util:end_suite([emqx_conf]),
     Etc = filename:join(["etc", "emqx.conf.all"]),
     Etc = filename:join(["etc", "emqx.conf.all"]),
+    TmpConfName = atom_to_list(?FUNCTION_NAME) ++ ".conf",
+    Inc = filename:join(["etc", TmpConfName]),
     ConfFile = emqx_common_test_helpers:app_path(emqx_conf, Etc),
     ConfFile = emqx_common_test_helpers:app_path(emqx_conf, Etc),
-    Bin = <<"listeners.tcp.max_connection_test {bind = \"0.0.0.0:3883\"}">>,
-    ok = file:write_file(ConfFile, Bin, [append]),
+    IncFile = emqx_common_test_helpers:app_path(emqx_conf, Inc),
+    Port = integer_to_binary(?PORT),
+    Bin = <<"listeners.tcp.max_connection_test {bind = \"0.0.0.0:", Port/binary, "\"}">>,
+    ok = file:write_file(IncFile, Bin),
+    ok = file:write_file(ConfFile, ["include \"", TmpConfName, "\""], [append]),
     emqx_mgmt_api_test_util:init_suite([emqx_conf]),
     emqx_mgmt_api_test_util:init_suite([emqx_conf]),
+    [{tmp_config_file, IncFile} | Config];
+t_max_connection_default({'end', Config}) ->
+    ok = file:delete(proplists:get_value(tmp_config_file, Config));
+t_max_connection_default(Config) when is_list(Config) ->
     %% Check infinity is binary not atom.
     %% Check infinity is binary not atom.
     #{<<"listeners">> := Listeners} = emqx_mgmt_api_listeners:do_list_listeners(),
     #{<<"listeners">> := Listeners} = emqx_mgmt_api_listeners:do_list_listeners(),
     Target = lists:filter(
     Target = lists:filter(
@@ -51,7 +78,7 @@ t_max_connection_default(_Config) ->
     emqx_conf:remove([listeners, tcp, max_connection_test], #{override_to => cluster}),
     emqx_conf:remove([listeners, tcp, max_connection_test], #{override_to => cluster}),
     ok.
     ok.
 
 
-t_list_listeners(_) ->
+t_list_listeners(Config) when is_list(Config) ->
     Path = emqx_mgmt_api_test_util:api_path(["listeners"]),
     Path = emqx_mgmt_api_test_util:api_path(["listeners"]),
     Res = request(get, Path, [], []),
     Res = request(get, Path, [], []),
     #{<<"listeners">> := Expect} = emqx_mgmt_api_listeners:do_list_listeners(),
     #{<<"listeners">> := Expect} = emqx_mgmt_api_listeners:do_list_listeners(),
@@ -71,9 +98,10 @@ t_list_listeners(_) ->
     ?assertMatch({error, {"HTTP/1.1", 404, _}}, request(get, NewPath, [], [])),
     ?assertMatch({error, {"HTTP/1.1", 404, _}}, request(get, NewPath, [], [])),
 
 
     OriginListener2 = maps:remove(<<"id">>, OriginListener),
     OriginListener2 = maps:remove(<<"id">>, OriginListener),
+    Port = integer_to_binary(?PORT),
     NewConf = OriginListener2#{
     NewConf = OriginListener2#{
         <<"name">> => <<"new">>,
         <<"name">> => <<"new">>,
-        <<"bind">> => <<"0.0.0.0:2883">>,
+        <<"bind">> => <<"0.0.0.0:", Port/binary>>,
         <<"max_connections">> := <<"infinity">>
         <<"max_connections">> := <<"infinity">>
     },
     },
     Create = request(post, Path, [], NewConf),
     Create = request(post, Path, [], NewConf),
@@ -89,7 +117,7 @@ t_list_listeners(_) ->
     ?assertMatch({error, {"HTTP/1.1", 404, _}}, request(get, NewPath, [], [])),
     ?assertMatch({error, {"HTTP/1.1", 404, _}}, request(get, NewPath, [], [])),
     ok.
     ok.
 
 
-t_tcp_crud_listeners_by_id(_) ->
+t_tcp_crud_listeners_by_id(Config) when is_list(Config) ->
     ListenerId = <<"tcp:default">>,
     ListenerId = <<"tcp:default">>,
     NewListenerId = <<"tcp:new">>,
     NewListenerId = <<"tcp:new">>,
     MinListenerId = <<"tcp:min">>,
     MinListenerId = <<"tcp:min">>,
@@ -97,7 +125,7 @@ t_tcp_crud_listeners_by_id(_) ->
     Type = <<"tcp">>,
     Type = <<"tcp">>,
     crud_listeners_by_id(ListenerId, NewListenerId, MinListenerId, BadId, Type).
     crud_listeners_by_id(ListenerId, NewListenerId, MinListenerId, BadId, Type).
 
 
-t_ssl_crud_listeners_by_id(_) ->
+t_ssl_crud_listeners_by_id(Config) when is_list(Config) ->
     ListenerId = <<"ssl:default">>,
     ListenerId = <<"ssl:default">>,
     NewListenerId = <<"ssl:new">>,
     NewListenerId = <<"ssl:new">>,
     MinListenerId = <<"ssl:min">>,
     MinListenerId = <<"ssl:min">>,
@@ -105,7 +133,7 @@ t_ssl_crud_listeners_by_id(_) ->
     Type = <<"ssl">>,
     Type = <<"ssl">>,
     crud_listeners_by_id(ListenerId, NewListenerId, MinListenerId, BadId, Type).
     crud_listeners_by_id(ListenerId, NewListenerId, MinListenerId, BadId, Type).
 
 
-t_ws_crud_listeners_by_id(_) ->
+t_ws_crud_listeners_by_id(Config) when is_list(Config) ->
     ListenerId = <<"ws:default">>,
     ListenerId = <<"ws:default">>,
     NewListenerId = <<"ws:new">>,
     NewListenerId = <<"ws:new">>,
     MinListenerId = <<"ws:min">>,
     MinListenerId = <<"ws:min">>,
@@ -113,7 +141,7 @@ t_ws_crud_listeners_by_id(_) ->
     Type = <<"ws">>,
     Type = <<"ws">>,
     crud_listeners_by_id(ListenerId, NewListenerId, MinListenerId, BadId, Type).
     crud_listeners_by_id(ListenerId, NewListenerId, MinListenerId, BadId, Type).
 
 
-t_wss_crud_listeners_by_id(_) ->
+t_wss_crud_listeners_by_id(Config) when is_list(Config) ->
     ListenerId = <<"wss:default">>,
     ListenerId = <<"wss:default">>,
     NewListenerId = <<"wss:new">>,
     NewListenerId = <<"wss:new">>,
     MinListenerId = <<"wss:min">>,
     MinListenerId = <<"wss:min">>,
@@ -121,7 +149,7 @@ t_wss_crud_listeners_by_id(_) ->
     Type = <<"wss">>,
     Type = <<"wss">>,
     crud_listeners_by_id(ListenerId, NewListenerId, MinListenerId, BadId, Type).
     crud_listeners_by_id(ListenerId, NewListenerId, MinListenerId, BadId, Type).
 
 
-t_api_listeners_list_not_ready(_Config) ->
+t_api_listeners_list_not_ready(Config) when is_list(Config) ->
     net_kernel:start(['listeners@127.0.0.1', longnames]),
     net_kernel:start(['listeners@127.0.0.1', longnames]),
     ct:timetrap({seconds, 120}),
     ct:timetrap({seconds, 120}),
     snabbkaffe:fix_ct_logging(),
     snabbkaffe:fix_ct_logging(),
@@ -151,16 +179,17 @@ t_api_listeners_list_not_ready(_Config) ->
         emqx_common_test_helpers:stop_slave(Node2)
         emqx_common_test_helpers:stop_slave(Node2)
     end.
     end.
 
 
-t_clear_certs(_) ->
+t_clear_certs(Config) when is_list(Config) ->
     ListenerId = <<"ssl:default">>,
     ListenerId = <<"ssl:default">>,
     NewListenerId = <<"ssl:clear">>,
     NewListenerId = <<"ssl:clear">>,
 
 
     OriginPath = emqx_mgmt_api_test_util:api_path(["listeners", ListenerId]),
     OriginPath = emqx_mgmt_api_test_util:api_path(["listeners", ListenerId]),
     NewPath = emqx_mgmt_api_test_util:api_path(["listeners", NewListenerId]),
     NewPath = emqx_mgmt_api_test_util:api_path(["listeners", NewListenerId]),
     ConfTempT = request(get, OriginPath, [], []),
     ConfTempT = request(get, OriginPath, [], []),
+    Port = integer_to_binary(?PORT),
     ConfTemp = ConfTempT#{
     ConfTemp = ConfTempT#{
         <<"id">> => NewListenerId,
         <<"id">> => NewListenerId,
-        <<"bind">> => <<"0.0.0.0:2883">>
+        <<"bind">> => <<"0.0.0.0:", Port/binary>>
     },
     },
 
 
     %% create, make sure the cert files are created
     %% create, make sure the cert files are created
@@ -245,9 +274,11 @@ crud_listeners_by_id(ListenerId, NewListenerId, MinListenerId, BadId, Type) ->
     %% create with full options
     %% create with full options
     ?assertEqual({error, not_found}, is_running(NewListenerId)),
     ?assertEqual({error, not_found}, is_running(NewListenerId)),
     ?assertMatch({error, {"HTTP/1.1", 404, _}}, request(get, NewPath, [], [])),
     ?assertMatch({error, {"HTTP/1.1", 404, _}}, request(get, NewPath, [], [])),
+    Port1 = integer_to_binary(?PORT),
+    Port2 = integer_to_binary(?PORT),
     NewConf = OriginListener#{
     NewConf = OriginListener#{
         <<"id">> => NewListenerId,
         <<"id">> => NewListenerId,
-        <<"bind">> => <<"0.0.0.0:2883">>
+        <<"bind">> => <<"0.0.0.0:", Port1/binary>>
     },
     },
     Create = request(post, NewPath, [], NewConf),
     Create = request(post, NewPath, [], NewConf),
     ?assertEqual(lists:sort(maps:keys(OriginListener)), lists:sort(maps:keys(Create))),
     ?assertEqual(lists:sort(maps:keys(OriginListener)), lists:sort(maps:keys(Create))),
@@ -271,7 +302,7 @@ crud_listeners_by_id(ListenerId, NewListenerId, MinListenerId, BadId, Type) ->
             } ->
             } ->
                 #{
                 #{
                     <<"id">> => MinListenerId,
                     <<"id">> => MinListenerId,
-                    <<"bind">> => <<"0.0.0.0:3883">>,
+                    <<"bind">> => <<"0.0.0.0:", Port2/binary>>,
                     <<"type">> => Type,
                     <<"type">> => Type,
                     <<"ssl_options">> => #{
                     <<"ssl_options">> => #{
                         <<"cacertfile">> => CaCertFile,
                         <<"cacertfile">> => CaCertFile,
@@ -282,7 +313,7 @@ crud_listeners_by_id(ListenerId, NewListenerId, MinListenerId, BadId, Type) ->
             _ ->
             _ ->
                 #{
                 #{
                     <<"id">> => MinListenerId,
                     <<"id">> => MinListenerId,
-                    <<"bind">> => <<"0.0.0.0:3883">>,
+                    <<"bind">> => <<"0.0.0.0:", Port2/binary>>,
                     <<"type">> => Type
                     <<"type">> => Type
                 }
                 }
         end,
         end,
@@ -296,7 +327,7 @@ crud_listeners_by_id(ListenerId, NewListenerId, MinListenerId, BadId, Type) ->
     BadPath = emqx_mgmt_api_test_util:api_path(["listeners", BadId]),
     BadPath = emqx_mgmt_api_test_util:api_path(["listeners", BadId]),
     BadConf = OriginListener#{
     BadConf = OriginListener#{
         <<"id">> => BadId,
         <<"id">> => BadId,
-        <<"bind">> => <<"0.0.0.0:2883">>
+        <<"bind">> => <<"0.0.0.0:", Port1/binary>>
     },
     },
     ?assertMatch({error, {"HTTP/1.1", 400, _}}, request(post, BadPath, [], BadConf)),
     ?assertMatch({error, {"HTTP/1.1", 400, _}}, request(post, BadPath, [], BadConf)),
 
 
@@ -332,12 +363,12 @@ crud_listeners_by_id(ListenerId, NewListenerId, MinListenerId, BadId, Type) ->
     ?assertEqual([], delete(NewPath)),
     ?assertEqual([], delete(NewPath)),
     ok.
     ok.
 
 
-t_delete_nonexistent_listener(_) ->
+t_delete_nonexistent_listener(Config) when is_list(Config) ->
     NonExist = emqx_mgmt_api_test_util:api_path(["listeners", "tcp:nonexistent"]),
     NonExist = emqx_mgmt_api_test_util:api_path(["listeners", "tcp:nonexistent"]),
     ?assertEqual([], delete(NonExist)),
     ?assertEqual([], delete(NonExist)),
     ok.
     ok.
 
 
-t_action_listeners(_) ->
+t_action_listeners(Config) when is_list(Config) ->
     ID = "tcp:default",
     ID = "tcp:default",
     action_listener(ID, "stop", false),
     action_listener(ID, "stop", false),
     action_listener(ID, "start", true),
     action_listener(ID, "start", true),