From b690a5fff4cae716648bdf009c644efa23b73c12 Mon Sep 17 00:00:00 2001 From: Luca Succi Date: Mon, 27 May 2024 12:40:53 +0200 Subject: [PATCH] Test ping timeout and move reconnection tests in dedicated SUITE --- test/grisp_connect_api_SUITE.erl | 18 +---- test/grisp_connect_log_SUITE.erl | 3 +- test/grisp_connect_manager.erl | 12 ++-- test/grisp_connect_reconnect_SUITE.erl | 94 ++++++++++++++++++++++++++ test/grisp_connect_test_client.erl | 1 + 5 files changed, 108 insertions(+), 20 deletions(-) create mode 100644 test/grisp_connect_reconnect_SUITE.erl diff --git a/test/grisp_connect_api_SUITE.erl b/test/grisp_connect_api_SUITE.erl index 71e9819..dc5f4be 100644 --- a/test/grisp_connect_api_SUITE.erl +++ b/test/grisp_connect_api_SUITE.erl @@ -9,6 +9,7 @@ -import(grisp_connect_test_client, [wait_connection/0]). -import(grisp_connect_test_client, [wait_connection/1]). -import(grisp_connect_test_client, [wait_disconnection/0]). +-import(grisp_connect_test_client, [wait_disconnection/1]). -import(grisp_connect_test_client, [serial_number/0]). -import(grisp_connect_test_client, [cert_dir/0]). @@ -30,7 +31,8 @@ init_per_suite(Config) -> ?assertEqual(ok, file:write_file(PolicyFile, <<>>)), application:set_env(seabac, policy_file, PolicyFile), - Config2 = grisp_connect_manager:start(CertDir, Config), + Config2 = grisp_connect_manager:start(Config), + grisp_connect_manager:kraft_start(CertDir), [{cert_dir, CertDir} | Config2]. end_per_suite(Config) -> @@ -71,20 +73,6 @@ link_device_test(_) -> ?assertMatch({ok, <<"ok">>}, grisp_connect:link_device()), ?assertMatch({ok, <<"pong">>}, grisp_connect:ping()). -reconnect_on_gun_crash_test(_) -> - {state, GunPid, _, _, _, _} = sys:get_state(grisp_connect_ws), - proc_lib:stop(GunPid), - ?assertMatch(ok, wait_disconnection()), - ?assertMatch(ok, wait_connection()). - -reconnect_on_disconnection_test(Config) -> - KraftRef = ?config(kraft_instance, Config), - kraft:stop(KraftRef), - ?assertMatch(ok, wait_disconnection()), - KraftRef2 = grisp_connect_manager:kraft_start(cert_dir()), - ?assertMatch(ok, wait_connection(100)), - [{kraft_instance, KraftRef2} | proplists:delete(kraft_instance, Config)]. - %--- Internal ------------------------------------------------------------------ flush() -> diff --git a/test/grisp_connect_log_SUITE.erl b/test/grisp_connect_log_SUITE.erl index 9ff22b7..31d4eb8 100644 --- a/test/grisp_connect_log_SUITE.erl +++ b/test/grisp_connect_log_SUITE.erl @@ -28,7 +28,8 @@ init_per_suite(Config) -> ?assertEqual(ok, file:write_file(PolicyFile, <<>>)), application:set_env(seabac, policy_file, PolicyFile), - Config2 = grisp_connect_manager:start(CertDir, Config), + Config2 = grisp_connect_manager:start(Config), + grisp_connect_manager:kraft_start(CertDir), grisp_connect_manager:link_device(), [{cert_dir, CertDir} | Config2]. diff --git a/test/grisp_connect_manager.erl b/test/grisp_connect_manager.erl index 8886618..debd1f7 100644 --- a/test/grisp_connect_manager.erl +++ b/test/grisp_connect_manager.erl @@ -5,7 +5,7 @@ -include_lib("common_test/include/ct.hrl"). -start(CertDir, Config) -> +start(Config) -> PrivDir = ?config(priv_dir, Config), application:set_env(mnesia, dir, PrivDir), @@ -18,23 +18,27 @@ start(CertDir, Config) -> application:start(mnesia), {ok, Started2} = application:ensure_all_started(kraft), - KraftRef = kraft_start(CertDir), {ok, Started3} = application:ensure_all_started(grisp_manager), - [{apps, Started1 ++ Started2 ++ Started3}, {kraft_instance , KraftRef} | Config]. + Apps = Started1 ++ Started2 ++ Started3, + [{apps, Apps} | Config]. kraft_start(CertDir) -> + kraft_start(CertDir, #{}). + +kraft_start(CertDir, OverrideOpts) -> SslOpts = [ {verify, verify_peer}, {keyfile, filename:join(CertDir, "server.key")}, {certfile, filename:join(CertDir, "server.crt")}, {cacertfile, filename:join(CertDir, "CA.crt")} ], - KraftOpts = #{ + Opts = #{ port => 3030, ssl_opts => SslOpts, app => grisp_manager }, + KraftOpts = mapz:deep_merge(Opts, OverrideOpts), KraftRoutes = [ {"/grisp-connect/ws", {ws, grisp_manager_device_api}, #{}, #{type => json_rpc}} diff --git a/test/grisp_connect_reconnect_SUITE.erl b/test/grisp_connect_reconnect_SUITE.erl new file mode 100644 index 0000000..429284c --- /dev/null +++ b/test/grisp_connect_reconnect_SUITE.erl @@ -0,0 +1,94 @@ +-module(grisp_connect_reconnect_SUITE). + +-behaviour(ct_suite). +-include_lib("common_test/include/ct.hrl"). +-include_lib("stdlib/include/assert.hrl"). + +-compile([export_all, nowarn_export_all]). + +-import(grisp_connect_test_client, [wait_connection/0]). +-import(grisp_connect_test_client, [wait_connection/1]). +-import(grisp_connect_test_client, [wait_disconnection/0]). +-import(grisp_connect_test_client, [wait_disconnection/1]). +-import(grisp_connect_test_client, [serial_number/0]). +-import(grisp_connect_test_client, [cert_dir/0]). + +%--- API ----------------------------------------------------------------------- + +all() -> + [ + F + || + {F, 1} <- ?MODULE:module_info(exports), + lists:suffix("_test", atom_to_list(F)) + ]. + +init_per_suite(Config) -> + PrivDir = ?config(priv_dir, Config), + CertDir = cert_dir(), + + PolicyFile = filename:join(PrivDir, "policies.term"), + ?assertEqual(ok, file:write_file(PolicyFile, <<>>)), + application:set_env(seabac, policy_file, PolicyFile), + + Config2 = grisp_connect_manager:start(Config), + [{cert_dir, CertDir} | Config2]. + +end_per_suite(Config) -> + grisp_connect_manager:cleanup_apps(?config(apps, Config)). + +init_per_testcase(_, Config) -> + % the kraf instance links to this process + process_flag(trap_exit, true), + {ok, _} = application:ensure_all_started(kraft), + KraftRef = grisp_connect_manager:kraft_start(?config(cert_dir, Config)), + {ok, _} = application:ensure_all_started(grisp_emulation), + application:set_env(grisp_connect, test_cert_dir, ?config(cert_dir, Config)), + {ok, _} = application:ensure_all_started(grisp_connect), + [{kraft_instance, KraftRef} | Config]. + +end_per_testcase(_, Config) -> + ok = application:stop(grisp_connect), + kraft:stop(?config(kraft_instance, Config)), + ok = application:stop(kraft), + mnesia:activity(transaction, fun() -> + mnesia:delete({grisp_device, serial_number()}) + end), + flush(), + Config. + +%--- Tests --------------------------------------------------------------------- + +reconnect_on_gun_crash_test(_) -> + ?assertMatch(ok, wait_connection(100)), + {state, GunPid, _, _, _, _} = sys:get_state(grisp_connect_ws), + proc_lib:stop(GunPid), + ?assertMatch(ok, wait_disconnection()), + ?assertMatch(ok, wait_connection()). + +reconnect_on_disconnection_test(Config) -> + ?assertMatch(ok, wait_connection()), + ok = kraft:stop(?config(kraft_instance, Config)), + ?assertMatch(ok, wait_disconnection()), + KraftRef2 = grisp_connect_manager:kraft_start(cert_dir()), + ?assertMatch(ok, wait_connection(100)), + [{kraft_instance, KraftRef2} | proplists:delete(kraft_instance, Config)]. + +reconnect_on_ping_timeout_test(_) -> + ?assertMatch(ok, wait_connection()), + {state, GunPid, _, _, _, _} = sys:get_state(grisp_connect_ws), + proc_lib:stop(GunPid), + % Now decrease ping timeout so that the WS closes after just 1 second + application:set_env(grisp_connect, ws_ping_timeout, 1000), + ?assertMatch(ok, wait_disconnection()), + ?assertMatch(ok, wait_connection(100)), + ?assertMatch(ok, wait_disconnection()), + ?assertMatch(ok, wait_connection(100)), + ?assertMatch(ok, wait_disconnection()). + +%--- Internal ------------------------------------------------------------------ + +flush() -> + receive Any -> ct:pal("Flushed: ~p", [Any]), flush() + after 0 -> ok + end. diff --git a/test/grisp_connect_test_client.erl b/test/grisp_connect_test_client.erl index fb007b9..587e26d 100644 --- a/test/grisp_connect_test_client.erl +++ b/test/grisp_connect_test_client.erl @@ -8,6 +8,7 @@ -export([wait_connection/0]). -export([wait_connection/1]). -export([wait_disconnection/0]). +-export([wait_disconnection/1]). %--- API -----------------------------------------------------------------------