emqx_machine_SUITE.erl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_machine_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("emqx/include/emqx.hrl").
  20. -include_lib("eunit/include/eunit.hrl").
  21. -include_lib("common_test/include/ct.hrl").
  22. -include_lib("snabbkaffe/include/snabbkaffe.hrl").
  23. -define(APPS, [
  24. emqx_prometheus,
  25. emqx_modules,
  26. emqx_dashboard,
  27. emqx_gateway,
  28. emqx_resource,
  29. emqx_rule_engine,
  30. emqx_bridge,
  31. emqx_management,
  32. emqx_retainer,
  33. emqx_exhook,
  34. emqx_auth,
  35. emqx_plugin,
  36. emqx_opentelemetry
  37. ]).
  38. all() -> emqx_common_test_helpers:all(?MODULE).
  39. init_per_suite(Config) ->
  40. emqx_common_test_helpers:start_apps([emqx_conf, emqx_opentelemetry]),
  41. application:load(emqx_dashboard),
  42. Config.
  43. end_per_suite(_Config) ->
  44. emqx_common_test_helpers:stop_apps([emqx_opentelemetry, emqx_conf]).
  45. init_per_testcase(t_custom_shard_transports, Config) ->
  46. OldConfig = application:get_env(emqx_machine, custom_shard_transports),
  47. [{old_config, OldConfig} | Config];
  48. init_per_testcase(t_open_ports_check = TestCase, Config) ->
  49. AppSpecs = [emqx],
  50. Cluster = [
  51. {emqx_machine_SUITE1, #{role => core, apps => AppSpecs}},
  52. {emqx_machine_SUITE2, #{role => core, apps => AppSpecs}},
  53. {emqx_machine_SUITE3, #{role => replicant, apps => AppSpecs}}
  54. ],
  55. Nodes = emqx_cth_cluster:start(Cluster, #{work_dir => emqx_cth_suite:work_dir(TestCase, Config)}),
  56. [{nodes, Nodes} | Config];
  57. init_per_testcase(t_sorted_reboot_apps, Config) ->
  58. application:set_env(emqx_machine, applications, ?APPS ++ [emqx_license]),
  59. Config;
  60. init_per_testcase(_TestCase, Config) ->
  61. application:set_env(emqx_machine, applications, ?APPS),
  62. Config.
  63. end_per_testcase(t_custom_shard_transports, Config) ->
  64. OldConfig0 = ?config(old_config, Config),
  65. application:stop(ekka),
  66. case OldConfig0 of
  67. {ok, OldConfig} ->
  68. application:set_env(emqx_machine, custom_shard_transports, OldConfig);
  69. undefined ->
  70. application:unset_env(emqx_machine, custom_shard_transports)
  71. end,
  72. ok;
  73. end_per_testcase(t_open_ports_check, Config) ->
  74. Nodes = ?config(nodes, Config),
  75. ok = emqx_cth_cluster:stop(Nodes),
  76. ok;
  77. end_per_testcase(_TestCase, _Config) ->
  78. ok.
  79. t_shutdown_reboot(_Config) ->
  80. emqx_machine_boot:stop_apps(),
  81. false = emqx:is_running(node()),
  82. emqx_machine_boot:ensure_apps_started(),
  83. true = emqx:is_running(node()),
  84. ok = emqx_machine_boot:stop_apps(),
  85. false = emqx:is_running(node()).
  86. t_sorted_reboot_apps(_Config) ->
  87. Apps = emqx_machine_boot:sorted_reboot_apps(),
  88. SortApps = [App || App <- Apps, (App =:= emqx_dashboard orelse App =:= emqx_license)],
  89. %% make sure emqx_license start early than emqx_dashboard
  90. ?assertEqual([emqx_license, emqx_dashboard], SortApps).
  91. t_custom_shard_transports(_Config) ->
  92. %% used to ensure the atom exists
  93. Shard = test_shard,
  94. %% the config keys are binaries
  95. ShardBin = atom_to_binary(Shard),
  96. DefaultTransport = distr,
  97. ?assertEqual(DefaultTransport, mria_config:shard_transport(Shard)),
  98. application:set_env(emqx_machine, custom_shard_transports, #{ShardBin => distr}),
  99. emqx_machine:start(),
  100. ?assertEqual(distr, mria_config:shard_transport(Shard)),
  101. ok.
  102. t_node_status(_Config) ->
  103. JSON = emqx_machine:node_status(),
  104. ?assertMatch(
  105. #{
  106. <<"backend">> := _,
  107. <<"role">> := <<"core">>
  108. },
  109. jsx:decode(JSON)
  110. ).
  111. t_open_ports_check(Config) ->
  112. [Core1, Core2, Replicant] = ?config(nodes, Config),
  113. Plan = erpc:call(Core1, emqx_machine, create_plan, []),
  114. ?assertMatch(
  115. [{Core2, #{ports_to_check := [_GenRPC0, _Ekka0], resolved_ips := [_]}}],
  116. Plan
  117. ),
  118. [{Core2, #{ports_to_check := [GenRPCPort, EkkaPort], resolved_ips := [_]}}] = Plan,
  119. ?assertMatch(
  120. [{Core1, #{ports_to_check := [_GenRPC1, _Ekka1], resolved_ips := [_]}}],
  121. erpc:call(Core2, emqx_machine, create_plan, [])
  122. ),
  123. ?assertMatch(
  124. [],
  125. erpc:call(Replicant, emqx_machine, create_plan, [])
  126. ),
  127. ?assertEqual(ok, erpc:call(Core1, emqx_machine, open_ports_check, [])),
  128. ?assertEqual(ok, erpc:call(Core2, emqx_machine, open_ports_check, [])),
  129. ?assertEqual(ok, erpc:call(Replicant, emqx_machine, open_ports_check, [])),
  130. true = erlang:monitor_node(Core2, true),
  131. ok = emqx_cth_cluster:stop_node(Core2),
  132. receive
  133. {nodedown, Core2} -> ok
  134. after 10000 ->
  135. ct:fail("nodedown message not received after 10 seconds.")
  136. end,
  137. ?assertEqual(ok, erpc:call(Replicant, emqx_machine, open_ports_check, [])),
  138. ?retry(200, 20, begin
  139. Results = erpc:call(Core1, emqx_machine, open_ports_check, []),
  140. ?assertMatch(
  141. #{
  142. msg := "some ports are unreachable",
  143. results :=
  144. #{
  145. Core2 :=
  146. #{
  147. open_ports := #{},
  148. ports_to_check := [_, _],
  149. resolved_ips := [_],
  150. status := bad_ports
  151. }
  152. }
  153. },
  154. Results,
  155. #{core2 => Core2, gen_rpc_port => GenRPCPort, ekka_port => EkkaPort}
  156. ),
  157. %% 2 ports to check; we don't assert the exact ekka port because, when running
  158. %% multiple nodes on the same machine as we do in tests, the order of returned ports
  159. %% might change between invocations.
  160. NumPorts = 2,
  161. ?assertEqual(
  162. NumPorts, map_size(emqx_utils_maps:deep_get([results, Core2, open_ports], Results))
  163. ),
  164. ok
  165. end),
  166. ok.