From d7101fd2c2ce8bf609670043585c57010a3707c6 Mon Sep 17 00:00:00 2001 From: Siri Hansen Date: Tue, 14 Mar 2017 11:18:46 +0100 Subject: [ct] Add 'keep_logs' option If setting the value for this option to an integer, N, common_test will remove all ct_run.* directories in the current log directory, except the N newest. The default value for the 'keep_logs' option is 'all', which means that no logs will be deleted. 'keep_logs' can be used in combination with refresh_logs, or in a normal common_test test run. --- lib/common_test/src/ct_logs.erl | 37 ++++++++++++++++++++++++++++++++++++- lib/common_test/src/ct_run.erl | 18 ++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) (limited to 'lib/common_test/src') diff --git a/lib/common_test/src/ct_logs.erl b/lib/common_test/src/ct_logs.erl index 09ad709da5..544b019b64 100644 --- a/lib/common_test/src/ct_logs.erl +++ b/lib/common_test/src/ct_logs.erl @@ -41,6 +41,7 @@ -export([xhtml/2, locate_priv_file/1, make_relative/1]). -export([insert_javascript/1]). -export([uri/1]). +-export([parse_keep_logs/1]). %% Logging stuff directly from testcase -export([tc_log/3, tc_log/4, tc_log/5, tc_log/6, @@ -1946,7 +1947,11 @@ make_all_runs_index(When) -> end, Dirs = filelib:wildcard(logdir_prefix()++"*.*"), - DirsSorted = (catch sort_all_runs(Dirs)), + DirsSorted0 = (catch sort_all_runs(Dirs)), + DirsSorted = + if When == start -> DirsSorted0; + true -> maybe_delete_old_dirs(DirsSorted0) + end, LogCacheInfo = get_cache_data(UseCache), @@ -2064,6 +2069,36 @@ sort_ct_runs(Dirs) -> {DateHH1,MM1,SS1} =< {DateHH2,MM2,SS2} end, Dirs). +parse_keep_logs([Str="all"]) -> + parse_keep_logs(list_to_atom(Str)); +parse_keep_logs([NStr]) -> + parse_keep_logs(list_to_integer(NStr)); +parse_keep_logs(all) -> + all; +parse_keep_logs(N) when is_integer(N), N>0 -> + N. + +maybe_delete_old_dirs(Sorted) -> + {Keep,Delete} = + case application:get_env(common_test, keep_logs) of + {ok,MaxN} when is_integer(MaxN), length(Sorted)>MaxN -> + lists:split(MaxN,Sorted); + _ -> + {Sorted,[]} + end, + delete_old_dirs(Delete), + Keep. + +delete_old_dirs([]) -> + ok; +delete_old_dirs(Dirs) -> + io:put_chars("\n Removing old test directories:\n"), + [begin + io:put_chars(" " ++ Dir ++ "\n"), + rm_dir(Dir) + end|| Dir <- Dirs], + ok. + dir_diff_all_runs(Dirs, LogCache) -> case LogCache#log_cache.all_runs of [] -> diff --git a/lib/common_test/src/ct_run.erl b/lib/common_test/src/ct_run.erl index cac176de3a..ba27303c81 100644 --- a/lib/common_test/src/ct_run.erl +++ b/lib/common_test/src/ct_run.erl @@ -363,6 +363,12 @@ script_start1(Parent, Args) -> _ -> application:set_env(common_test, disable_log_cache, true) end, + %% log_cleanup - used by ct_logs + KeepLogs = get_start_opt(keep_logs, + fun ct_logs:parse_keep_logs/1, + all, + Args), + application:set_env(common_test, keep_logs, KeepLogs), Opts = #opts{label = Label, profile = Profile, vts = Vts, shell = Shell, @@ -970,6 +976,12 @@ run_test1(StartOpts) when is_list(StartOpts) -> stop_trace(Tracing), exit(Res); RefreshDir -> + %% log_cleanup - used by ct_logs + KeepLogs = get_start_opt(keep_logs, + fun ct_logs:parse_keep_logs/1, + all, + StartOpts), + application:set_env(common_test, keep_logs, KeepLogs), ok = refresh_logs(?abs(RefreshDir)), exit(done) end. @@ -1131,6 +1143,12 @@ run_test2(StartOpts) -> DisableCacheBool -> application:set_env(common_test, disable_log_cache, DisableCacheBool) end, + %% log_cleanup - used by ct_logs + KeepLogs = get_start_opt(keep_logs, + fun ct_logs:parse_keep_logs/1, + all, + StartOpts), + application:set_env(common_test, keep_logs, KeepLogs), %% stepped execution Step = get_start_opt(step, value, StartOpts), -- cgit v1.2.3