From fffce9316ca03b13ff4617a7edd35b982f5bff49 Mon Sep 17 00:00:00 2001 From: Thales Macedo Garitezi Date: Thu, 1 Dec 2022 11:28:45 -0300 Subject: [PATCH] test(fix): avoid trying to connect to system_monitor db in tests When `system_monitor`'s application is loaded, it sets a default hostname: ```erlang 1> application:get_env(system_monitor, db_hostname). undefined 2> application:load(system_monitor). ok 3> application:get_env(system_monitor, db_hostname). {ok,"localhost"} ``` And that makes tests crash when somehow it gets loaded. By having a semantic `enable` field, we could avoid starting this application by checking if the hostname is filled or not. ``` =ERROR REPORT==== 1-Dec-2022::13:57:20.855070 === ** Generic server <0.7646.3> terminating ** Last message in was {command,epgsql_cmd_connect, #{codecs => [],database => "postgres", host => [], password => #Fun, port => 5432,timeout => 5000, username => "system_monitor"}} ** When Server state == {state,undefined,undefined,<<>>,undefined,on_message, undefined, {[],[]}, undefined,undefined,undefined,undefined,[], information_redacted,[],undefined,undefined, undefined,undefined,undefined,#{}} ** Reason for termination == ** {badarg, [{gen_tcp,connect_0,4,[{file,"gen_tcp.erl"},{line,207}]}, {epgsql_cmd_connect,open_socket,2, [{file, "/emqx/_build/default/lib/epgsql/src/commands/epgsql_cmd_connect.erl"}, {line,94}]}, {epgsql_cmd_connect,execute,2, [{file, "/emqx/_build/default/lib/epgsql/src/commands/epgsql_cmd_connect.erl"}, {line,54}]}, {epgsql_sock,command_exec,4, [{file,"/emqx/_build/default/lib/epgsql/src/epgsql_sock.erl"}, {line,338}]}, {gen_server,try_handle_call,4,[{file,"gen_server.erl"},{line,721}]}, {gen_server,handle_msg,6,[{file,"gen_server.erl"},{line,750}]}, {proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,226}]}]} ** Client system_monitor_pg stacktrace ** [{gen,do_call,4,[{file,"gen.erl"},{line,214}]}, {gen_server,call,3,[{file,"gen_server.erl"},{line,243}]}, {epgsql,call_connect,2, [{file,"/emqx/_build/default/lib/epgsql/src/epgsql.erl"}, {line,203}]}, {system_monitor_pg,connect,0, [{file,"/emqx/_build/default/lib/system_monitor/src/system_monitor_pg.erl"}, {line,151}]}, {system_monitor_pg,initialize,0, [{file,"/emqx/_build/default/lib/system_monitor/src/system_monitor_pg.erl"}, {line,142}]}, {system_monitor_pg,handle_info,2, [{file,"/emqx/_build/default/lib/system_monitor/src/system_monitor_pg.erl"}, {line,92}]}, {gen_server,try_dispatch,4,[{file,"gen_server.erl"},{line,695}]}, {gen_server,handle_msg,6,[{file,"gen_server.erl"},{line,771}]}, {proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,226}]}] =CRASH REPORT==== 1-Dec-2022::13:57:20.855697 === crasher: initial call: epgsql_sock:init/1 pid: <0.7646.3> registered_name: [] exception exit: badarg in function gen_tcp:connect_0/4 (gen_tcp.erl, line 207) in call from epgsql_cmd_connect:open_socket/2 (/emqx/_build/default/lib/epgsql/src/commands/epgsql_cmd_connect.erl, line 94) in call from epgsql_cmd_connect:execute/2 (/emqx/_build/default/lib/epgsql/src/commands/epgsql_cmd_connect.erl, line 54) in call from epgsql_sock:command_exec/4 (/emqx/_build/default/lib/epgsql/src/epgsql_sock.erl, line 338) in call from gen_server:try_handle_call/4 (gen_server.erl, line 721) in call from gen_server:handle_msg/6 (gen_server.erl, line 750) ancestors: [system_monitor_pg,system_monitor2_sup,system_monitor_sup, <0.4297.3>] message_queue_len: 0 messages: [] links: [<0.4303.3>] dictionary: [] trap_exit: false status: running heap_size: 1598 stack_size: 29 reductions: 734 neighbours: ``` --- apps/emqx/test/emqx_common_test_helpers.erl | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/apps/emqx/test/emqx_common_test_helpers.erl b/apps/emqx/test/emqx_common_test_helpers.erl index 87d9c1368..a56b2c3fa 100644 --- a/apps/emqx/test/emqx_common_test_helpers.erl +++ b/apps/emqx/test/emqx_common_test_helpers.erl @@ -161,7 +161,17 @@ boot_modules(Mods) -> -spec start_apps(Apps :: apps()) -> ok. start_apps(Apps) -> - start_apps(Apps, fun(_) -> ok end). + %% to avoid keeping the `db_hostname' that is set then loading + %% `system_monitor' application in `emqx_machine', and then it + %% crashing when trying to connect. + %% FIXME: add an `enable' option to sysmon_top and use that to + %% decide whether to start it or not. + DefaultHandler = + fun(_) -> + application:set_env(system_monitor, db_hostname, ""), + ok + end, + start_apps(Apps, DefaultHandler). -spec start_apps(Apps :: apps(), Handler :: special_config_handler()) -> ok. start_apps(Apps, SpecAppConfig) when is_function(SpecAppConfig) ->