%% %% %CopyrightBegin% %% %% Copyright Ericsson AB 2003-2011. All Rights Reserved. %% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. %% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. %% %% %CopyrightEnd% %% %%% @doc Logging functionality for Common Test Framework. %%% %%%
This module implements %%%
This function is called by ct_util.erl when testing is %%% started. A new directory named ct_run.<timestamp> is created %%% and all logs are stored under this directory.
%%% init(Mode) -> Self = self(), Pid = spawn_link(fun() -> logger(Self,Mode) end), MRef = erlang:monitor(process,Pid), receive {started,Pid,Result} -> erlang:demonitor(MRef, [flush]), Result; {'DOWN',MRef,process,_,Reason} -> exit({could_not_start_process,?MODULE,Reason}) end. make_dirname({{YY,MM,DD},{H,M,S}}) -> io_lib:format(logdir_node_prefix()++".~w-~2.2.0w-~2.2.0w_~2.2.0w.~2.2.0w.~2.2.0w", [YY,MM,DD,H,M,S]). logdir_prefix() -> "ct_run". logdir_node_prefix() -> logdir_prefix()++"."++atom_to_list(node()). %%%----------------------------------------------------------------- %%% @spec close(Info, StartDir) -> ok %%% %%% @doc Create index pages with test results and close the CT Log %%% (tool-internal use only). close(Info, StartDir) -> make_last_run_index(), ct_event:notify(#event{name=stop_logging,node=node(),data=[]}), case whereis(?MODULE) of Pid when is_pid(Pid) -> MRef = erlang:monitor(process,Pid), ?MODULE ! stop, receive {'DOWN',MRef,process,_,_} -> ok end; undefined -> ok end, if Info == clean -> case cleanup() of ok -> ok; Error -> io:format("Warning! Cleanup failed: ~p~n", [Error]) end, make_all_suites_index(stop), make_all_runs_index(stop); true -> file:set_cwd(".."), make_all_suites_index(stop), make_all_runs_index(stop), case ct_util:get_profile_data(browser, StartDir) of undefined -> ok; BrowserData -> case {proplists:get_value(prog, BrowserData), proplists:get_value(args, BrowserData), proplists:get_value(page, BrowserData)} of {Prog,Args,Page} when is_list(Args), is_list(Page) -> URL = "\"file://" ++ ?abs(Page) ++ "\"", ct_util:open_url(Prog, Args, URL); _ -> ok end end end, ok. %%%----------------------------------------------------------------- %%% @spec set_stylesheet(TC,SSFile) -> ok set_stylesheet(TC, SSFile) -> cast({set_stylesheet,TC,SSFile}). %%%----------------------------------------------------------------- %%% @spec clear_stylesheet(TC) -> ok clear_stylesheet(TC) -> cast({clear_stylesheet,TC}). %%%----------------------------------------------------------------- %%% @spec get_log_dir() -> {ok,Dir} | {error,Reason} get_log_dir() -> call({get_log_dir,false}). %%%----------------------------------------------------------------- %%% @spec get_log_dir(Abs) -> {ok,Dir} | {error,Reason} get_log_dir(Abs) -> call({get_log_dir,Abs}). %%%----------------------------------------------------------------- %%% make_last_run_index() -> ok make_last_run_index() -> call(make_last_run_index). call(Msg) -> case whereis(?MODULE) of undefined -> {error,does_not_exist}; Pid -> MRef = erlang:monitor(process,Pid), Ref = make_ref(), ?MODULE ! {Msg,{self(),Ref}}, receive {Ref, Result} -> erlang:demonitor(MRef, [flush]), Result; {'DOWN',MRef,process,_,Reason} -> {error,{process_down,?MODULE,Reason}} end end. return({To,Ref},Result) -> To ! {Ref, Result}. cast(Msg) -> case whereis(?MODULE) of undefined -> {error,does_not_exist}; _Pid -> ?MODULE ! Msg end. %%%----------------------------------------------------------------- %%% @spec init_tc(RefreshLog) -> ok %%% %%% @doc Test case initiation (tool-internal use only). %%% %%%This function is called by ct_framework:init_tc/3
init_tc(RefreshLog) -> call({init_tc,self(),group_leader(),RefreshLog}), ok. %%%----------------------------------------------------------------- %%% @spec end_tc(TCPid) -> ok | {error,Reason} %%% %%% @doc Test case clean up (tool-internal use only). %%% %%%This function is called by ct_framework:end_tc/3
end_tc(TCPid) -> %% use call here so that the TC process will wait and receive %% possible exit signals from ct_logs before end_tc returns ok call({end_tc,TCPid}). %%%----------------------------------------------------------------- %%% @spec log(Heading,Format,Args) -> ok %%% %%% @doc Log internal activity (tool-internal use only). %%% %%%This function writes an entry to the currently active log, %%% i.e. either the CT log or a test case log.
%%% %%%Heading
is a short string indicating what type of
%%% activity it is. Format
and Args
is the
%%% data to log (as in io:format(Format,Args)
).
This function must be used in combination with
%%% cont_log/2
and end_log/0
. The intention
%%% is to call start_log
once, then cont_log
%%% any number of times and finally end_log
once.
For information about the parameters, see log/3
.
Log
in the test case
%%% log.
%%%
%%% The given Logs
must exist in the priv dir of the
%%% calling test suite.
This function is called by ct
when logging
%%% stuff directly from a testcase (i.e. not from within the CT
%%% framework).
This function is called by ct
when printing
%%% stuff a testcase on the user console.
This function is called by ct
when logging
%%% stuff directly from a testcase. The info is written both in the
%%% log and on the console.
This function is called by internal ct functions to %%% force logging to the ct framework log
ct_log(Category,Format,Args) -> cast({ct_log,[{div_header(Category),[]}, {Format,Args}, {div_footer(),[]}]}), ok. %%%================================================================= %%% Internal functions int_header() -> "\n", "PROGRESS LOG
\n\n"), []), Fd. print_style(Fd,undefined) -> case basic_html() of true -> io:format(Fd, "\n", []); _ -> ok end; print_style(Fd,StyleSheet) -> case file:read_file(StyleSheet) of {ok,Bin} -> Str = binary_to_list(Bin), Pos0 = case string:str(Str,"") of 0 -> string:str(Str,""); N1 -> N1 end, case Pos1 of 0 -> print_style_error(Fd,StyleSheet,missing_style_end_tag); _ -> Style = string:sub_string(Str,Pos0,Pos1+7), io:format(Fd,"~s\n",[Style]) end end; {error,Reason} -> print_style_error(Fd,StyleSheet,Reason) end. %% Simple link version, doesn't work with all browsers unfortunately. :-( %% print_style(Fd, StyleSheet) -> %% io:format(Fd, %% "", %% [StyleSheet]). print_style_error(Fd,StyleSheet,Reason) -> io:format(Fd,"\n\n", [StyleSheet,Reason]), print_style(Fd,undefined). close_ctlog(Fd) -> io:format(Fd,"",[]), io:format(Fd,footer(),[]), file:close(Fd). %%%----------------------------------------------------------------- %%% Make an index page for the last run make_last_run_index(StartTime) -> IndexName = ?index_name, AbsIndexName = ?abs(IndexName), case catch make_last_run_index1(StartTime,IndexName) of {'EXIT', Reason} -> io:put_chars("CRASHED while updating " ++ AbsIndexName ++ "!\n"), io:format("~p~n", [Reason]), {error, Reason}; {error, Reason} -> io:put_chars("FAILED while updating " ++ AbsIndexName ++ "\n"), io:format("~p~n", [Reason]), {error, Reason}; ok -> % io:put_chars("done\n"), ok; Err -> io:format("Unknown internal error while updating ~s. " "Please report.\n(Err: ~p, ID: 1)", [AbsIndexName,Err]), {error, Err} end. make_last_run_index1(StartTime,IndexName) -> %% this manoeuvre is to ensure the tests get logged %% in correct order of time (the 1 sec resolution %% of the dirnames may be too big) Logs1 = case filelib:wildcard([$*|?logdir_ext]) of [Log] -> % first test [Log]; Logs -> case read_totals_file(?totals_name) of {_Node,_Lbl,Logs0,_Totals} -> insert_dirs(Logs,Logs0); _ -> %% someone deleted the totals file!? Logs end end, Missing = case file:read_file(?missing_suites_info) of {ok,Bin} -> binary_to_term(Bin); _ -> [] end, Label = case application:get_env(common_test, test_label) of {ok,Lbl} -> Lbl; _ -> undefined end, {ok,Index0,Totals} = make_last_run_index(Logs1, index_header(Label,StartTime), 0, 0, 0, 0, 0, Missing), %% write current Totals to file, later to be used in all_runs log write_totals_file(?totals_name,Label,Logs1,Totals), Index = [Index0|index_footer()], case force_write_file(IndexName, Index) of ok -> ok; {error, Reason} -> {error,{index_write_error, Reason}} end. insert_dirs([NewDir|NewDirs],Dirs) -> Dirs1 = insert_dir(NewDir,Dirs), insert_dirs(NewDirs,Dirs1); insert_dirs([],Dirs) -> Dirs. insert_dir(D,Dirs=[D|_]) -> Dirs; insert_dir(D,[D1|Ds]) -> [D1|insert_dir(D,Ds)]; insert_dir(D,[]) -> [D]. make_last_run_index([Name|Rest], Result, TotSucc, TotFail, UserSkip, AutoSkip, TotNotBuilt, Missing) -> case last_test(Name) of false -> %% Silently skip. make_last_run_index(Rest, Result, TotSucc, TotFail, UserSkip, AutoSkip, TotNotBuilt, Missing); LastLogDir -> SuiteName = filename:rootname(filename:basename(Name)), case make_one_index_entry(SuiteName, LastLogDir, "-", false, Missing) of {Result1,Succ,Fail,USkip,ASkip,NotBuilt} -> %% for backwards compatibility AutoSkip1 = case catch AutoSkip+ASkip of {'EXIT',_} -> undefined; Res -> Res end, make_last_run_index(Rest, [Result|Result1], TotSucc+Succ, TotFail+Fail, UserSkip+USkip, AutoSkip1, TotNotBuilt+NotBuilt, Missing); error -> make_last_run_index(Rest, Result, TotSucc, TotFail, UserSkip, AutoSkip, TotNotBuilt, Missing) end end; make_last_run_index([], Result, TotSucc, TotFail, UserSkip, AutoSkip, TotNotBuilt, _) -> {ok, [Result|total_row(TotSucc, TotFail, UserSkip, AutoSkip, TotNotBuilt, false)], {TotSucc,TotFail,UserSkip,AutoSkip,TotNotBuilt}}. make_one_index_entry(SuiteName, LogDir, Label, All, Missing) -> case count_cases(LogDir) of {Succ,Fail,UserSkip,AutoSkip} -> NotBuilt = not_built(SuiteName, LogDir, All, Missing), NewResult = make_one_index_entry1(SuiteName, LogDir, Label, Succ, Fail, UserSkip, AutoSkip, NotBuilt, All, normal), {NewResult,Succ,Fail,UserSkip,AutoSkip,NotBuilt}; error -> error end. make_one_index_entry1(SuiteName, Link, Label, Success, Fail, UserSkip, AutoSkip, NotBuilt, All, Mode) -> LogFile = filename:join(Link, ?suitelog_name ++ ".html"), CrashDumpLink = case Mode of cached -> ""; normal -> CrashDumpName = SuiteName ++ "_erl_crash.dump", case filelib:is_file(CrashDumpName) of true -> [" (CrashDump)"]; false -> "" end end, CtRunDir = filename:dirname(filename:dirname(Link)), {Lbl,Timestamp,Node,AllInfo} = case All of {true,OldRuns} -> [_Prefix,NodeOrDate|_] = string:tokens(Link,"."), Node1 = case string:chr(NodeOrDate,$@) of 0 -> "-"; _ -> NodeOrDate end, TS = timestamp(CtRunDir), N = xhtml(["",Node1, " \n"], ["",Node1," \n"]), L = xhtml(["",Label, " \n"], ["",Label," \n"]), T = xhtml(["",TS," \n"], ["",TS," \n"]), CtLogFile = filename:join(CtRunDir,?ct_log_name), OldRunsLink = case OldRuns of [] -> "none"; _ -> "Old Runs" end, A = xhtml(["CT Log \n", "",OldRunsLink," \n"], ["CT Log \n", "",OldRunsLink," \n"]), {L,T,N,A}; false -> {"","","",""} end, NotBuiltStr = if NotBuilt == 0 -> ["",integer_to_list(NotBuilt)," \n"]; true -> ["", integer_to_list(NotBuilt)," \n"] end, FailStr = if Fail > 0 -> ["", integer_to_list(Fail),""]; true -> integer_to_list(Fail) end, {AllSkip,UserSkipStr,AutoSkipStr} = if AutoSkip == undefined -> {UserSkip,"?","?"}; true -> ASStr = if AutoSkip > 0 -> ["", integer_to_list(AutoSkip),""]; true -> integer_to_list(AutoSkip) end, {UserSkip+AutoSkip,integer_to_list(UserSkip),ASStr} end, [xhtml("\n", [" \n"]), xhtml(" \n"]. total_row(Success, Fail, UserSkip, AutoSkip, NotBuilt, All) -> {Label,TimestampCell,AllInfo} = case All of true -> {"",SuiteName,"", CrashDumpLink, xhtml(" \n", "\n"), Lbl, Timestamp, "",integer_to_list(Success)," \n", "",FailStr," \n", "",integer_to_list(AllSkip), " (",UserSkipStr,"/",AutoSkipStr,") \n", NotBuiltStr, Node, AllInfo, "\n", " \n", " \n" " \n" " \n"}; false -> {"","",""} end, {AllSkip,UserSkipStr,AutoSkipStr} = if AutoSkip == undefined -> {UserSkip,"?","?"}; true -> {UserSkip+AutoSkip, integer_to_list(UserSkip),integer_to_list(AutoSkip)} end, [xhtml(" \n", [" \n"]), " \n"]. not_built(_BaseName,_LogDir,_All,[]) -> 0; not_built(BaseName,_LogDir,_All,Missing) -> %% find out how many suites didn't compile %% BaseName = %% Top.ObjDir | Top.ObjDir.suites | Top.ObjDir.Suite | %% Top.ObjDir.Suite.cases | Top.ObjDir.Suite.Case Failed = case string:tokens(BaseName,".") of [T,O] when is_list(T) -> % all under Top.ObjDir locate_info({T,O},all,Missing); [T,O,"suites"] -> locate_info({T,O},suites,Missing); [T,O,S] -> locate_info({T,O},list_to_atom(S),Missing); [T,O,S,_] -> locate_info({T,O},list_to_atom(S),Missing); _ -> % old format - don't crash [] end, length(Failed). locate_info(Path={Top,Obj},AllOrSuite,[{{Dir,Suite},Failed}|Errors]) -> case lists:reverse(filename:split(Dir)) of ["test",Obj,Top|_] -> get_missing_suites(AllOrSuite,{Suite,Failed}) ++ locate_info(Path,AllOrSuite,Errors); [Obj,Top|_] -> get_missing_suites(AllOrSuite,{Suite,Failed}) ++ locate_info(Path,AllOrSuite,Errors); _ -> locate_info(Path,AllOrSuite,Errors) end; locate_info(_,_,[]) -> []. get_missing_suites(all,{"all",Failed}) -> Failed; get_missing_suites(suites,{_Suite,Failed}) -> Failed; get_missing_suites(Suite,{Suite,Failed}) -> Failed; get_missing_suites(_,_) -> []. term_to_text(Term) -> lists:flatten(io_lib:format("~p.\n", [Term])). %%% Headers and footers. index_header(Label, StartTime) -> Head = case Label of undefined -> header("Test Results", format_time(StartTime)); _ -> header("Test Results for '" ++ Label ++ "'", format_time(StartTime)) end, [Head | ["Total \n", Label, TimestampCell, "",integer_to_list(Success)," \n", "",integer_to_list(Fail)," \n", "",integer_to_list(AllSkip), " (",UserSkipStr,"/",AutoSkipStr,") \n", "",integer_to_list(NotBuilt)," \n", AllInfo, "\n", xhtml([""], ["
" " "]), xhtml("
\n", "
\n"), xhtml(["\n"], "
\n"), "
Test Name \n", xhtml(["_Ok" "_ \n"], "Ok \n"), "Failed \n", "Skipped", xhtml(" \n" "
", "
"), "(User/Auto)Missing", xhtml(" \n" "\n"]]. all_suites_index_header() -> {ok,Cwd} = file:get_cwd(), all_suites_index_header(Cwd). all_suites_index_header(IndexDir) -> LogDir = filename:basename(IndexDir), AllRuns = xhtml(["All test runs in \"" ++ LogDir ++ "\""], "ALL RUNS"), AllRunsLink = xhtml(["",AllRuns,"\n"], [" "]), [header("Test Results") | ["
", "
"), "Suites\n", AllRunsLink, xhtml("
\n", "
\n"), xhtml(["\n"], "
\n"), "
Test Name \n", "Label \n", "Test Run Started \n", xhtml(["_Ok" "_ \n"], "Ok \n"), "Failed \n", "Skipped \n" "
(User/Auto)Missing \n" "
SuitesNode \n", "CT Log \n", "Old Runs \n", "\n"]]. all_runs_header() -> {ok,Cwd} = file:get_cwd(), LogDir = filename:basename(Cwd), Title = "All test runs in \"" ++ LogDir ++ "\"", IxLink = [xhtml([""], [" "]), xhtml("
\n", "
\n")], [header(Title) | ["\n", IxLink, xhtml([" \n"], "
\n"), "
\n" "\n" | footer()]. footer() -> ["History \n" "Node \n" "Label \n" "Tests \n" "Test Names \n" "Total \n", xhtml(["_Ok" "_ \n"], "Ok \n"), "Failed \n" "Skipped \n" "
(User/Auto)Missing \n" "\n"]]. header(Title) -> header1(Title, ""). header(Title, SubTitle) -> header1(Title, SubTitle). header1(Title, SubTitle) -> SubTitleHTML = if SubTitle =/= "" -> ["
Suites\n", " \n" ++ SubTitle ++ "
\n", xhtml("
\n", "\n
\n")]; true -> xhtml("
\n", "
\n") end, CTPath = code:lib_dir(common_test), CSSFile = filename:join(filename:join(CTPath, "priv"), "ct_default.css"), [xhtml(["\n", "\n"], ["\n", "\n"]), "\n", "\n", "" ++ Title ++ " " ++ SubTitle ++ " \n", "\n", xhtml("", [""]), "\n", body_tag(), "\n", " \n", SubTitleHTML,"\n"]. index_footer() -> ["" ++ Title ++ "
\n", "\n", xhtml(" \n" "\n"]. body_tag() -> CTPath = code:lib_dir(common_test), TileFile = filename:join(filename:join(CTPath,"priv"),"tile1.jpg"), xhtml("\n", "\n"). current_time() -> format_time(calendar:local_time()). format_time({{Y, Mon, D}, {H, Min, S}}) -> Weekday = weekday(calendar:day_of_the_week(Y, Mon, D)), lists:flatten(io_lib:format("~s ~s ~p ~w ~2.2.0w:~2.2.0w:~2.2.0w", [Weekday, month(Mon), D, Y, H, Min, S])). weekday(1) -> "Mon"; weekday(2) -> "Tue"; weekday(3) -> "Wed"; weekday(4) -> "Thu"; weekday(5) -> "Fri"; weekday(6) -> "Sat"; weekday(7) -> "Sun". month(1) -> "Jan"; month(2) -> "Feb"; month(3) -> "Mar"; month(4) -> "Apr"; month(5) -> "May"; month(6) -> "Jun"; month(7) -> "Jul"; month(8) -> "Aug"; month(9) -> "Sep"; month(10) -> "Oct"; month(11) -> "Nov"; month(12) -> "Dec". year() -> {Y, _, _} = date(), integer_to_list(Y). %% Count test cases in the given directory (a directory of the type %% run.1997-08-04_09.58.52). count_cases(Dir) -> SumFile = filename:join(Dir, ?run_summary), case read_summary(SumFile, [summary]) of {ok, [{Succ,Fail,Skip}]} -> {Succ,Fail,Skip,undefined}; {ok, [Summary]} -> Summary; {error, _} -> LogFile = filename:join(Dir, ?suitelog_name), case file:read_file(LogFile) of {ok, Bin} -> case count_cases1(binary_to_list(Bin), {undefined,undefined,undefined,undefined}) of {error,not_complete} -> %% The test is not complete - dont write summary %% file yet. {0,0,0,0}; Summary -> write_summary(SumFile, Summary), Summary end; {error, _Reason} -> io:format("\nFailed to read ~p (skipped)\n", [LogFile]), error end end. write_summary(Name, Summary) -> File = [term_to_text({summary, Summary})], force_write_file(Name, File). read_summary(Name, Keys) -> case file:consult(Name) of {ok, []} -> {error, "Empty summary file"}; {ok, Terms} -> {ok, lists:map(fun(Key) -> {value, {_, Value}} = lists:keysearch(Key, 1, Terms), Value end, Keys)}; {error, Reason} -> {error, Reason} end. count_cases1("=failed" ++ Rest, {Success, _Fail, UserSkip,AutoSkip}) -> {NextLine, Count} = get_number(Rest), count_cases1(NextLine, {Success, Count, UserSkip,AutoSkip}); count_cases1("=successful" ++ Rest, {_Success, Fail, UserSkip,AutoSkip}) -> {NextLine, Count} = get_number(Rest), count_cases1(NextLine, {Count, Fail, UserSkip,AutoSkip}); count_cases1("=skipped" ++ Rest, {Success, Fail, _UserSkip,_AutoSkip}) -> {NextLine, Count} = get_number(Rest), count_cases1(NextLine, {Success, Fail, Count,undefined}); count_cases1("=user_skipped" ++ Rest, {Success, Fail, _UserSkip,AutoSkip}) -> {NextLine, Count} = get_number(Rest), count_cases1(NextLine, {Success, Fail, Count,AutoSkip}); count_cases1("=auto_skipped" ++ Rest, {Success, Fail, UserSkip,_AutoSkip}) -> {NextLine, Count} = get_number(Rest), count_cases1(NextLine, {Success, Fail, UserSkip,Count}); count_cases1([], {Su,F,USk,_ASk}) when Su==undefined;F==undefined; USk==undefined -> {error,not_complete}; count_cases1([], Counters) -> Counters; count_cases1(Other, Counters) -> count_cases1(skip_to_nl(Other), Counters). get_number([$\s|Rest]) -> get_number(Rest); get_number([Digit|Rest]) when $0 =< Digit, Digit =< $9 -> get_number(Rest, Digit-$0). get_number([Digit|Rest], Acc) when $0 =< Digit, Digit =< $9 -> get_number(Rest, Acc*10+Digit-$0); get_number([$\n|Rest], Acc) -> {Rest, Acc}; get_number([_|Rest], Acc) -> get_number(Rest, Acc). skip_to_nl([$\n|Rest]) -> Rest; skip_to_nl([_|Rest]) -> skip_to_nl(Rest); skip_to_nl([]) -> []. config_table(Vars) -> [config_table_header()|config_table1(Vars)]. config_table_header() -> [ xhtml(["
\n
\n", "
\n"), xhtml("\n", "
"), "Copyright © ", year(), " Open Telecom Platform", xhtml("\n"), "
\n", "
\n"), "Updated: ", current_time(), "", xhtml("
\n", "
\n"), xhtml("\n", "Configuration
\n" "CONFIGURATION\n
\n"), "
\n"]. make_all_runs_index(When) -> put(basic_html, basic_html()), AbsName = ?abs(?all_runs_name), notify_and_lock_file(AbsName), if When == start -> ok; true -> io:put_chars("Updating " ++ AbsName ++ "... ") end, Dirs = filelib:wildcard(logdir_prefix()++"*.*"), DirsSorted = (catch sort_all_runs(Dirs)), Header = all_runs_header(), Index = [runentry(Dir) || Dir <- DirsSorted], Result = file:write_file(AbsName,Header++Index++index_footer()), if When == start -> ok; true -> io:put_chars("done\n") end, notify_and_unlock_file(AbsName), Result. sort_all_runs(Dirs) -> %% sort on time string, always last and on the format: %% "YYYY-MM-DD_HH.MM.SS" KeyList = lists:map(fun(Dir) -> case lists:reverse(string:tokens(Dir,[$.,$_])) of [SS,MM,HH,Date|_] -> {{Date,HH,MM,SS},Dir}; _Other -> throw(Dirs) end end,Dirs), lists:reverse(lists:map(fun({_,Dir}) -> Dir end,lists:keysort(1,KeyList))). interactive_link() -> [Dir|_] = lists:reverse(filelib:wildcard(logdir_prefix()++"*.*")), CtLog = filename:join(Dir,"ctlog.html"), Body = ["Log from last interactive run: ", timestamp(Dir),""], file:write_file("last_interactive.html",Body), io:format("~n~nUpdated ~s\n" "Any CT activities will be logged here\n", [?abs("last_interactive.html")]). runentry(Dir) -> TotalsFile = filename:join(Dir,?totals_name), TotalsStr = case read_totals_file(TotalsFile) of {Node,Label,Logs,{TotSucc,TotFail,UserSkip,AutoSkip,NotBuilt}} -> TotFailStr = if TotFail > 0 -> ["", integer_to_list(TotFail),""]; true -> integer_to_list(TotFail) end, {AllSkip,UserSkipStr,AutoSkipStr} = if AutoSkip == undefined -> {UserSkip,"?","?"}; true -> ASStr = if AutoSkip > 0 -> ["", integer_to_list(AutoSkip),""]; true -> integer_to_list(AutoSkip) end, {UserSkip+AutoSkip,integer_to_list(UserSkip),ASStr} end, NoOfTests = case length(Logs) of 0 -> "-"; N -> integer_to_list(N) end, StripExt = fun(File) -> string:sub_string(File,1, length(File)- length(?logdir_ext)) ++ ", " end, Polish = fun(S) -> case lists:reverse(S) of [32,$,|Rev] -> lists:reverse(Rev); [$,|Rev] -> lists:reverse(Rev); _ -> S end end, TestNames = Polish(lists:flatten(lists:map(StripExt,Logs))), TestNamesTrunc = if TestNames=="" -> ""; length(TestNames) < ?testname_width -> TestNames; true -> Trunc = Polish(string:substr(TestNames,1,?testname_width-3)), lists:flatten(io_lib:format("~s...",[Trunc])) end, Total = TotSucc+TotFail+AllSkip, A = xhtml(["\n"]. config_table1([{Key,Value}|Vars]) -> [xhtml([" Key Value \n"], [" ", atom_to_list(Key), " \n", " ",io_lib:format("~p",[Value]),"\n", " \n"]) | config_table1(Vars)]; config_table1([]) -> ["", atom_to_list(Key), " \n", "", io_lib:format("~p",[Value]), " \n",Node, " \n", "",Label, " \n", "",NoOfTests," \n"], ["",Node," \n", "",Label," \n", "",NoOfTests," \n"]), B = xhtml(["", TestNamesTrunc," \n"], ["", TestNamesTrunc," \n"]), C = ["",integer_to_list(Total)," \n", "",integer_to_list(TotSucc)," \n", "",TotFailStr," \n", "",integer_to_list(AllSkip), " (",UserSkipStr,"/",AutoSkipStr,") \n", "",integer_to_list(NotBuilt)," \n"], A++B++C; _ -> A = xhtml(["" "Test data missing or corrupt \n", "? \n", "? \n"], ["" "Test data missing or corrupt \n", "? \n", "? \n"]), B = xhtml(["? \n"], ["? \n"]), C = ["? \n", "? \n", "? \n", "? \n", "? \n"], A++B++C end, Index = filename:join(Dir,?index_name), [xhtml("\n", [" \n"]), xhtml([" \n"]. write_totals_file(Name,Label,Logs,Totals) -> AbsName = ?abs(Name), notify_and_lock_file(AbsName), force_write_file(AbsName, term_to_binary({atom_to_list(node()), Label,Logs,Totals})), notify_and_unlock_file(AbsName). %% this function needs to convert from old formats to new so that old %% test results (prev ct versions) can be listed together with new read_totals_file(Name) -> AbsName = ?abs(Name), notify_and_lock_file(AbsName), Result = case file:read_file(AbsName) of {ok,Bin} -> case catch binary_to_term(Bin) of {'EXIT',_Reason} -> % corrupt file {"-",[],undefined}; {Node,Label,Ls,Tot} -> % all info available Label1 = case Label of undefined -> "-"; _ -> Label end, case Tot of {_Ok,_Fail,_USkip,_ASkip,_NoBuild} -> % latest format {Node,Label1,Ls,Tot}; {TotSucc,TotFail,AllSkip,NotBuilt} -> {Node,Label1,Ls,{TotSucc,TotFail,AllSkip,undefined,NotBuilt}} end; {Node,Ls,Tot} -> % no label found case Tot of {_Ok,_Fail,_USkip,_ASkip,_NoBuild} -> % latest format {Node,"-",Ls,Tot}; {TotSucc,TotFail,AllSkip,NotBuilt} -> {Node,"-",Ls,{TotSucc,TotFail,AllSkip,undefined,NotBuilt}} end; %% for backwards compatibility {Ls,Tot} -> {"-",Ls,Tot}; Tot -> {"-",[],Tot} end; Error -> Error end, notify_and_unlock_file(AbsName), Result. force_write_file(Name,Contents) -> force_delete(Name), file:write_file(Name,Contents). force_delete(Name) -> case file:delete(Name) of {error,eacces} -> force_rename(Name,Name++".old.",0); Other -> Other end. force_rename(From,To,Number) -> Dest = [To|integer_to_list(Number)], case file:read_file_info(Dest) of {ok,_} -> force_rename(From,To,Number+1); {error,_} -> file:rename(From,Dest) end. timestamp(Dir) -> TsR = lists:reverse(string:tokens(Dir,".-_")), [S,Min,H,D,M,Y] = [list_to_integer(N) || N <- lists:sublist(TsR,6)], format_time({{Y,M,D},{H,Min,S}}). %% ----------------------------- NOTE -------------------------------------- %% The top level index file is generated based on the file contents under %% logdir. This takes place initially when the test run starts (When = start) %% and an update takes place at the end of the test run, or when the user %% requests an explicit refresh (When = refresh). %% The index file needs to be updated also at the start of each individual %% test (in order for the user to be able to track test progress by refreshing %% the browser). Since it would be too expensive to generate a new file from %% scratch every time (by reading the data from disk), a copy of the dir tree %% is cached as a result of the first index file creation. This copy is then %% used for all top level index page updates that occur during the test run. %% This means that any changes to the dir tree under logdir during the test %% run will not show until after the final refresh. %% ------------------------------------------------------------------------- %% Creates the top level index file. When == start | refresh. %% A copy of the dir tree under logdir is cached as a result. make_all_suites_index(When) when is_atom(When) -> put(basic_html, basic_html()), AbsIndexName = ?abs(?index_name), notify_and_lock_file(AbsIndexName), LogDirs = filelib:wildcard(logdir_prefix()++".*/*"++?logdir_ext), Sorted = sort_logdirs(LogDirs, []), Result = make_all_suites_index1(When, AbsIndexName, Sorted), notify_and_unlock_file(AbsIndexName), Result; %% This updates the top level index file using cached data from %% the initial index file creation. make_all_suites_index(NewTestData = {_TestName,DirName}) -> put(basic_html, basic_html()), %% AllLogDirs = [{TestName,Label,Missing,{LastLogDir,Summary},OldDirs}|...] {AbsIndexName,LogDirData} = ct_util:get_testdata(test_index), CtRunDirPos = length(filename:split(AbsIndexName)), CtRunDir = filename:join(lists:sublist(filename:split(DirName), CtRunDirPos)), Label = case read_totals_file(filename:join(CtRunDir, ?totals_name)) of {_,"-",_,_} -> "..."; {_,Lbl,_,_} -> Lbl; _ -> "..." end, notify_and_lock_file(AbsIndexName), Result = case catch make_all_suites_ix_cached(AbsIndexName, NewTestData, Label, LogDirData) of {'EXIT',Reason} -> io:put_chars("CRASHED while updating " ++ AbsIndexName ++ "!\n"), io:format("~p~n", [Reason]), {error,Reason}; {error,Reason} -> io:put_chars("FAILED while updating " ++ AbsIndexName ++ "\n"), io:format("~p~n", [Reason]), {error,Reason}; ok -> ok; Err -> io:format("Unknown internal error while updating ~s. " "Please report.\n(Err: ~p, ID: 1)", [AbsIndexName,Err]), {error, Err} end, notify_and_unlock_file(AbsIndexName), Result. sort_logdirs([Dir|Dirs],Groups) -> TestName = filename:rootname(filename:basename(Dir)), case filelib:wildcard(filename:join(Dir,"run.*")) of [RunDir] -> Groups1 = insert_test(TestName,{filename:basename(RunDir),RunDir},Groups), sort_logdirs(Dirs,Groups1); _ -> % ignore missing run directory sort_logdirs(Dirs,Groups) end; sort_logdirs([],Groups) -> lists:keysort(1,sort_each_group(Groups)). insert_test(Test,IxDir,[{Test,IxDirs}|Groups]) -> [{Test,[IxDir|IxDirs]}|Groups]; insert_test(Test,IxDir,[]) -> [{Test,[IxDir]}]; insert_test(Test,IxDir,[TestDir|Groups]) -> [TestDir|insert_test(Test,IxDir,Groups)]. sort_each_group([{Test,IxDirs}|Groups]) -> Sorted = lists:reverse([Dir || {_,Dir} <- lists:keysort(1,IxDirs)]), [{Test,Sorted}| sort_each_group(Groups)]; sort_each_group([]) -> []. make_all_suites_index1(When, AbsIndexName, AllLogDirs) -> IndexName = ?index_name, if When == start -> ok; true -> io:put_chars("Updating " ++ AbsIndexName ++ "... ") end, case catch make_all_suites_index2(IndexName, AllLogDirs) of {'EXIT', Reason} -> io:put_chars("CRASHED while updating " ++ AbsIndexName ++ "!\n"), io:format("~p~n", [Reason]), {error, Reason}; {error, Reason} -> io:put_chars("FAILED while updating " ++ AbsIndexName ++ "\n"), io:format("~p~n", [Reason]), {error, Reason}; {ok,CacheData} -> case When of start -> ct_util:set_testdata_async({test_index,{AbsIndexName, CacheData}}), ok; _ -> io:put_chars("done\n"), ok end; Err -> io:format("Unknown internal error while updating ~s. " "Please report.\n(Err: ~p, ID: 1)", [AbsIndexName,Err]), {error, Err} end. make_all_suites_index2(IndexName, AllTestLogDirs) -> {ok,Index0,_Totals,CacheData} = make_all_suites_index3(AllTestLogDirs, all_suites_index_header(), 0, 0, 0, 0, 0, [], []), Index = [Index0|index_footer()], case force_write_file(IndexName, Index) of ok -> {ok,CacheData}; {error, Reason} -> {error,{index_write_error, Reason}} end. make_all_suites_index3([{TestName,[LastLogDir|OldDirs]}|Rest], Result, TotSucc, TotFail, UserSkip, AutoSkip, TotNotBuilt, Labels, CacheData) -> [EntryDir|_] = filename:split(LastLogDir), Missing = case file:read_file(filename:join(EntryDir, ?missing_suites_info)) of {ok,Bin} -> binary_to_term(Bin); _ -> [] end, {Label,Labels1} = case proplists:get_value(EntryDir, Labels) of undefined -> case read_totals_file(filename:join(EntryDir, ?totals_name)) of {_,Lbl,_,_} -> {Lbl,[{EntryDir,Lbl}|Labels]}; _ -> {"-",[{EntryDir,"-"}|Labels]} end; Lbl -> {Lbl,Labels} end, case make_one_index_entry(TestName, LastLogDir, Label, {true,OldDirs}, Missing) of {Result1,Succ,Fail,USkip,ASkip,NotBuilt} -> %% for backwards compatibility AutoSkip1 = case catch AutoSkip+ASkip of {'EXIT',_} -> undefined; Res -> Res end, IxEntry = {TestName,Label,Missing, {LastLogDir,{Succ,Fail,USkip,ASkip}},OldDirs}, make_all_suites_index3(Rest, [Result|Result1], TotSucc+Succ, TotFail+Fail, UserSkip+USkip, AutoSkip1, TotNotBuilt+NotBuilt, Labels1, [IxEntry|CacheData]); error -> IxEntry = {TestName,Label,Missing,{LastLogDir,error},OldDirs}, make_all_suites_index3(Rest, Result, TotSucc, TotFail, UserSkip, AutoSkip, TotNotBuilt, Labels1, [IxEntry|CacheData]) end; make_all_suites_index3([], Result, TotSucc, TotFail, UserSkip, AutoSkip, TotNotBuilt, _, CacheData) -> {ok, [Result|total_row(TotSucc, TotFail, UserSkip, AutoSkip, TotNotBuilt,true)], {TotSucc,TotFail,UserSkip,AutoSkip,TotNotBuilt}, lists:reverse(CacheData)}. make_all_suites_ix_cached(AbsIndexName, NewTestData, Label, AllTestLogDirs) -> AllTestLogDirs1 = insert_new_test_data(NewTestData, Label, AllTestLogDirs), IndexDir = filename:dirname(AbsIndexName), Index0 = make_all_suites_ix_cached1(AllTestLogDirs1, all_suites_index_header(IndexDir), 0, 0, 0, 0, 0), Index = [Index0|index_footer()], case force_write_file(AbsIndexName, Index) of ok -> ok; {error, Reason} -> {error,{index_write_error, Reason}} end. insert_new_test_data({NewTestName,NewTestDir}, NewLabel, AllTestLogDirs) -> AllTestLogDirs1 = case lists:keysearch(NewTestName, 1, AllTestLogDirs) of {value,{_,_,_,{LastLogDir,_},OldDirs}} -> [{NewTestName,NewLabel,[],{NewTestDir,{0,0,0,0}}, [LastLogDir|OldDirs]} | lists:keydelete(NewTestName, 1, AllTestLogDirs)]; false -> [{NewTestName,NewLabel,[],{NewTestDir,{0,0,0,0}},[]} | AllTestLogDirs] end, lists:keysort(1, AllTestLogDirs1). make_all_suites_ix_cached1([{TestName,Label,Missing,LastLogDirData,OldDirs}|Rest], Result, TotSucc, TotFail, UserSkip, AutoSkip, TotNotBuilt) -> case make_one_ix_entry_cached(TestName, LastLogDirData, Label, {true,OldDirs}, Missing) of {Result1,Succ,Fail,USkip,ASkip,NotBuilt} -> %% for backwards compatibility AutoSkip1 = case catch AutoSkip+ASkip of {'EXIT',_} -> undefined; Res -> Res end, make_all_suites_ix_cached1(Rest, [Result|Result1], TotSucc+Succ, TotFail+Fail, UserSkip+USkip, AutoSkip1, TotNotBuilt+NotBuilt); error -> make_all_suites_ix_cached1(Rest, Result, TotSucc, TotFail, UserSkip, AutoSkip, TotNotBuilt) end; make_all_suites_ix_cached1([], Result, TotSucc, TotFail, UserSkip, AutoSkip, TotNotBuilt) -> [Result|total_row(TotSucc, TotFail, UserSkip, AutoSkip, TotNotBuilt, true)]. make_one_ix_entry_cached(TestName, {LogDir,Summary}, Label, All, Missing) -> case Summary of {Succ,Fail,UserSkip,AutoSkip} -> NotBuilt = not_built(TestName, LogDir, All, Missing), NewResult = make_one_index_entry1(TestName, LogDir, Label, Succ, Fail, UserSkip, AutoSkip, NotBuilt, All, cached), {NewResult,Succ,Fail,UserSkip,AutoSkip,NotBuilt}; error -> error end. %%----------------------------------------------------------------- %% Remove log files. %% Cwd should always be set to the root logdir when finished. cleanup() -> {ok,Cwd} = file:get_cwd(), ok = file:set_cwd("../"), {ok,Top} = file:get_cwd(), Result = case catch try_cleanup(Cwd) of ok -> ok; {'EXIT',Reason} -> {error,Reason}; Error -> {error,Error} end, ok = file:set_cwd(Top), Result. try_cleanup(CTRunDir) -> %% ensure we're removing the ct_run directory case lists:reverse(filename:split(CTRunDir)) of [[$c,$t,$_,$r,$u,$n,$.|_]|_] -> case filelib:wildcard(filename:join(CTRunDir,"ct_run.*")) of [] -> % "double check" rm_dir(CTRunDir); _ -> unknown_logdir end; _ -> unknown_logdir end. rm_dir(Dir) -> case file:list_dir(Dir) of {error,Errno} -> exit({ls_failed,Dir,Errno}); {ok,Files} -> rm_files([filename:join(Dir, F) || F <- Files]), case file:del_dir(Dir) of {error,Errno} -> exit({rmdir_failed,Errno}); ok -> ok end end. rm_files([F | Fs]) -> Base = filename:basename(F), if Base == "." ; Base == ".." -> rm_files(Fs); true -> case file:read_file_info(F) of {ok,#file_info{type=directory}} -> rm_dir(F), rm_files(Fs); {ok,_Regular} -> case file:delete(F) of ok -> rm_files(Fs); {error,Errno} -> exit({del_failed,F,Errno}) end end end; rm_files([]) -> ok. %%%----------------------------------------------------------------- %%% @spec simulate() -> pid() %%% %%% @doc Simulate the logger process. %%% %%%",timestamp(Dir),"", TotalsStr," \n"], ["",timestamp(Dir),"",TotalsStr," \n"]), "Simulate the logger process - for use when testing code using %%% ct_logs logging mechanism without using the ct %%% environment. (E.g. when testing code with ts)
simulate() -> cast(stop), S = self(), Pid = spawn(fun() -> register(?MODULE,self()), S ! {self(),started}, simulate_logger_loop() end), receive {Pid,started} -> Pid end. simulate_logger_loop() -> receive {log,_,_,List} -> S = [[io_lib:format(Str,Args),io_lib:nl()] || {Str,Args} <- List], io:format("~s",[S]), simulate_logger_loop(); stop -> ok end. %%%----------------------------------------------------------------- %%% @spec notify_and_lock_file(Files) -> ok %%% %%% @doc %%% notify_and_lock_file(File) -> case ct_event:is_alive() of true -> ct_event:sync_notify(#event{name=start_write_file, node=node(), data=File}); false -> ok end. %%%----------------------------------------------------------------- %%% @spec notify_and_unlock_file(Files) -> ok %%% %%% @doc %%% notify_and_unlock_file(File) -> case ct_event:is_alive() of true -> ct_event:sync_notify(#event{name=finished_write_file, node=node(), data=File}); false -> ok end. %%%----------------------------------------------------------------- %%% @spec last_test(Dir) -> string() | false %%% %%% @doc %%% last_test(Dir) -> last_test(filelib:wildcard(filename:join(Dir, "run.[1-2]*")), false). last_test([Run|Rest], false) -> last_test(Rest, Run); last_test([Run|Rest], Latest) when Run > Latest -> last_test(Rest, Run); last_test([_|Rest], Latest) -> last_test(Rest, Latest); last_test([], Latest) -> Latest. %%%----------------------------------------------------------------- %%% @spec xhtml(HTML, XHTML) -> HTML | XHTML %%% %%% @doc %%% xhtml(HTML, XHTML) -> case get(basic_html) of true -> HTML; _ -> XHTML end. %%%----------------------------------------------------------------- %%% @spec odd_or_even() -> "odd" | "even" %%% %%% @doc %%% odd_or_even() -> case get(odd_or_even) of even -> put(odd_or_even, odd), "even"; _ -> put(odd_or_even, even), "odd" end. %%%----------------------------------------------------------------- %%% @spec basic_html() -> true | false %%% %%% @doc %%% basic_html() -> case application:get_env(common_test, basic_html) of {ok,true} -> true; _ -> false end. %%%----------------------------------------------------------------- %%% @spec get_ts_html_wrapper(TestName, PrintLabel) -> {Mode,Header,Footer} %%% %%% @doc %%% get_ts_html_wrapper(TestName, PrintLabel) -> TestName1 = lists:flatten(io_lib:format("~p", [TestName])), Basic = basic_html(), LabelStr = if not PrintLabel -> ""; true -> case {Basic,application:get_env(common_test, test_label)} of {true,{ok,Lbl}} when Lbl =/= undefined -> "" ++ Lbl ++ "
\n"; {_,{ok,Lbl}} when Lbl =/= undefined -> "'" ++ Lbl ++ "'\n"; _ -> "" end end, CTPath = code:lib_dir(common_test), {ok,CtLogdir} = get_log_dir(true), AllRuns = filename:join(filename:dirname(CtLogdir), ?all_runs_name), TestIndex = filename:join(filename:dirname(CtLogdir), ?index_name), case Basic of true -> TileFile = filename:join(filename:join(CTPath,"priv"),"tile1.jpg"), Bgr = " background=\"" ++ TileFile ++ "\"", Copyright = ["\n", "Copyright © ", year(), " ", "Open Telecom Platform
\n"], {basic_html, ["\n", "\n", "
\n", "Updated: ", current_time(), "", "
\nTest ", TestName1, " results \n", "\n", "\n", "\n", LabelStr, "\n"], ["\n \n\n\n"]}; _ -> Copyright = ["\n"], CSSFile = filename:join(filename:join(CTPath, "priv"), "ct_default.css"), {xhtml, ["\n", "\n", "\n\n", "Test run history\n | ", "Top level test index\n\n
\n", Copyright,"Test ", TestName1, " results \n", "\n", "", "\n","\n", LabelStr, "\n"], ["\n \n\n\n"]} end.\n", "Test run history\n | ", "Top level test index\n\n
\n", Copyright,"