diff options
Diffstat (limited to 'lib/common_test/src')
-rw-r--r-- | lib/common_test/src/ct_conn_log_h.erl | 26 | ||||
-rw-r--r-- | lib/common_test/src/ct_telnet.erl | 101 | ||||
-rw-r--r-- | lib/common_test/src/cth_conn_log.erl | 17 |
3 files changed, 112 insertions, 32 deletions
diff --git a/lib/common_test/src/ct_conn_log_h.erl b/lib/common_test/src/ct_conn_log_h.erl index 2b83ff1abb..d733df27dc 100644 --- a/lib/common_test/src/ct_conn_log_h.erl +++ b/lib/common_test/src/ct_conn_log_h.erl @@ -39,36 +39,34 @@ init({GL,ConnLogs}) -> open_files(GL,ConnLogs,#state{default_gl=GL}). -open_files(GL,[{ConnMod,{LogType,ConnLogs}}|T],State) -> - case do_open_files(GL,ConnLogs,[]) of +open_files(GL,[{ConnMod,{LogType,LogFiles}}|T],State=#state{logs=Logs}) -> + case do_open_files(LogFiles,[]) of {ok,Fds} -> - open_files(GL,T,State#state{logs=[{GL,[{ConnMod,{LogType,Fds}}]} | - State#state.logs]}); + ConnInfo = proplists:get_value(GL,Logs,[]), + Logs1 = [{GL,[{ConnMod,{LogType,Fds}}|ConnInfo]} | + proplists:delete(GL,Logs)], + open_files(GL,T,State#state{logs=Logs1}); Error -> Error end; open_files(_GL,[],State) -> {ok,State}. -do_open_files(GL,[{Tag,File}|ConnLogs],Acc) -> +do_open_files([{Tag,File}|LogFiles],Acc) -> case file:open(File, [write,append,{encoding,utf8}]) of {ok,Fd} -> - do_open_files(GL,ConnLogs,[{Tag,Fd}|Acc]); + do_open_files(LogFiles,[{Tag,Fd}|Acc]); {error,Reason} -> {error,{could_not_open_log,File,Reason}} end; -do_open_files(_GL,[],Acc) -> +do_open_files([],Acc) -> {ok,lists:reverse(Acc)}. handle_event({info_report,_,{From,update,{GL,ConnLogs}}}, State) when node(GL) == node() -> - - %%! --- Tue Jan 28 12:18:50 2014 --- peppe was here! - io:format(user, "!!! ADDING NEW LOGS FOR ~p~n", [GL]), - - open_files(GL,ConnLogs,#state{}), + Result = open_files(GL,ConnLogs,State), From ! {updated,GL}, - {ok, State}; + Result; handle_event({_Type, GL, _Msg}, State) when node(GL) /= node() -> {ok, State}; handle_event({_Type,GL,{Pid,{ct_connection,Mod,Action,ConnName},Report}}, @@ -126,7 +124,7 @@ write_error(Time,#conn_log{module=ConnMod}=Info,Report,GL,State) -> end. get_log(Info,GL,State) -> - case proplists:get_value(GL, State#state.logs) of + case proplists:get_value(GL,State#state.logs) of undefined -> {html,State#state.default_gl}; ConnLogs -> diff --git a/lib/common_test/src/ct_telnet.erl b/lib/common_test/src/ct_telnet.erl index f9dd62ee37..698915b37c 100644 --- a/lib/common_test/src/ct_telnet.erl +++ b/lib/common_test/src/ct_telnet.erl @@ -42,7 +42,106 @@ %%% <p>Enter the <code>telnet_settings</code> term in a configuration %%% file included in the test and ct_telnet will retrieve the information %%% automatically. Note that <c>keep_alive</c> may be specified per connection if -%%% required. See <c>unix_telnet</c> for details.</p></doc> +%%% required. See <c>unix_telnet</c> for details.</p> +%%% +%%% @end +%%% +%%% == Logging == +%%% +%%% `ct_telnet' can be configured to uses the `error_logger' for logging telnet +%%% traffic. A special purpose error handler is implemented in +%%% `ct_conn_log_h'. To use this error handler, add the `cth_conn_log' +%%% hook in your test suite, e.g. +%%% +%%% +%%% ``` +%%% suite() -> +%%% [{ct_hooks, [{cth_conn_log, [{conn_mod(),hook_options()}]}]}]. +%%%''' +%%% +%%% `conn_mod()' is the name of the common_test module implementing +%%% the connection protocol, i.e. `ct_telnet'. +%%% +%%% The hook option `log_type' specifies the type of logging: +%%% +%%% <dl> +%%% <dt>`raw'</dt> +%%% <dd>The sent and received telnet data is logged to a separate +%%% text file as is without any formatting. A link to the file is +%%% added to the test case HTML log.</dd> +%%% +%%% <dt>`html (default)'</dt> +%%% <dd>The sent and received telnet traffic is pretty printed +%%% directly in the test case HTML log.</dd> +%%% +%%% <dt>`silent'</dt> +%%% <dd>Telnet traffic is not logged.</dd> +%%% </dl> +%%% +%%% By default, all telnet traffic is logged in one single log +%%% file. However, it is possible to have different connections logged +%%% in separate files. To do this, use the hook option `hosts' and +%%% list the names of the servers/connections that will be used in the +%%% suite. Note that the connections must be named for this to work. +%%% +%%% The `hosts' option has no effect if `log_type' is set to `html' or +%%% `silent'. +%%% +%%% The hook options can also be specified in a configuration file with +%%% the configuration variable `ct_conn_log': +%%% +%%% ``` +%%% {ct_conn_log,[{conn_mod(),hook_options()}]}. +%%% ''' +%%% +%%% For example: +%% +%%% ``` +%%% {ct_conn_log,[{ct_telnet,[{log_type,raw}, +%%% {hosts,[key_or_name()]}]}]} +%%% ''' +%%% +%%% <b>Note</b> that hook options specified in a configuration file +%%% will overwrite any hardcoded hook options in the test suite. +%% +%%% === Logging example 1 === +%%% +%%% The following `ct_hooks' statement will cause raw printing of +%%% telnet traffic to separate logs for the connections named +%%% `server1' and `server2'. Any other connections will be logged +%%% to default telnet log. +%%% +%%% ``` +%%% suite() -> +%%% [{ct_hooks, [{cth_conn_log, [{ct_telnet,[{log_type,raw}}, +%%% {hosts,[server1,server2]}]} +%%% ]}]}]. +%%%''' +%%% +%%% === Logging example 2 === +%%% +%%% The following configuration file will cause raw logging of all +%%% telnet traffic into one single text file. +%%% +%%% ``` +%%% {ct_conn_log,[{ct_telnet,[{log_type,raw}]}]}. +%%% ''' +%%% +%%% The `ct_hooks' statement must look like this: +%%% +%%% ``` +%%% suite() -> +%%% [{ct_hooks, [{cth_conn_log, []}]}]. +%%% ''' +%%% +%%% The same `ct_hooks' statement without the configuration file would +%%% cause HTML logging of all telnet connections into the test case +%%% HTML log. +%%% +%%% <b>Note</b> that if the `cth_conn_log' hook is not added, telnet +%%% traffic is still logged in the test case log files (on the legacy +%%% `ct_telnet' format). + %%% @type connection_type() = telnet | ts1 | ts2 diff --git a/lib/common_test/src/cth_conn_log.erl b/lib/common_test/src/cth_conn_log.erl index 050bda6eda..a731c8054c 100644 --- a/lib/common_test/src/cth_conn_log.erl +++ b/lib/common_test/src/cth_conn_log.erl @@ -140,19 +140,9 @@ pre_init_per_testcase(TestCase,Config,CthState) -> GL = group_leader(), Update = fun(Init) when Init == undefined; Init == [] -> - - %%! --- Tue Jan 28 12:13:08 2014 --- peppe was here! - io:format(user, "### ~p: ADDING NEW HANDLER FOR ~p~n", - [TestCase,GL]), - error_logger:add_report_handler(ct_conn_log_h,{GL,Logs}), [TestCase]; (PrevUsers) -> - - %%! --- Tue Jan 28 12:13:08 2014 --- peppe was here! - io:format(user, "### ~p: CONNECTING ~p TO EXISTING HANDLER~n", - [TestCase,GL]), - error_logger:info_report(update,{GL,Logs}), receive {updated,GL} -> @@ -179,17 +169,10 @@ post_end_per_testcase(TestCase,_Config,Return,CthState) -> deleted -> [ct_util:delete_testdata({?MODULE,ConnMod}) || {ConnMod,_} <- CthState], - - %%! --- Tue Jan 28 13:29:37 2014 --- peppe was here! - io:format(user, "### ~p: REMOVING ERROR LOGGER~n", [TestCase]), - error_logger:delete_report_handler(ct_conn_log_h); {error,no_response} -> exit({?MODULE,no_response_from_logger}); _PrevUsers -> - %%! --- Tue Jan 28 13:29:37 2014 --- peppe was here! - io:format(user, "### ~p: *NOT* REMOVING ERROR LOGGER~n", [TestCase]), - ok end, {Return,CthState}. |