From dee961d8f61ae85a95cd05a1affbc6726323aca3 Mon Sep 17 00:00:00 2001 From: Joe DeVivo Date: Thu, 30 Aug 2012 13:42:13 -0400 Subject: [PATCH] Added file output --- .gitignore | 3 ++- src/riak_test.erl | 44 ++++++++++++++++++++++++++++++---------- src/riak_test_runner.erl | 36 +++++++++++++++++++------------- src/rtdev.erl | 5 +---- 4 files changed, 58 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index 0d280a93..8cebc171 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ ebin log riak_test .eunit -.DS_Store \ No newline at end of file +.DS_Store +out diff --git a/src/riak_test.erl b/src/riak_test.erl index 79e49f0e..9d19f6c0 100644 --- a/src/riak_test.erl +++ b/src/riak_test.erl @@ -14,13 +14,16 @@ cli_options() -> {config, $c, "conf", string, "specifies the project configuration"}, {tests, $t, "tests", string, "specifies which tests to run"}, {suites, $s, "suites", string, "which suites to run"}, - {dir, $d, "dir", string, "run all tests in the specified directory"} + {dir, $d, "dir", string, "run all tests in the specified directory"}, + {verbose, $v, "verbose", undefined, "verbose output"}, + {outdir, $o, "outdir", string, "output directory"} ]. main(Args) -> {ok, {ParsedArgs, HarnessArgs}} = getopt:parse(cli_options(), Args), + Verbose = proplists:is_defined(verbose, ParsedArgs), Config = proplists:get_value(config, ParsedArgs), SpecificTests = proplists:get_all_values(tests, ParsedArgs), Suites = proplists:get_all_values(suites, ParsedArgs), @@ -55,33 +58,52 @@ main(Args) -> %% Start Lager application:load(lager), - LagerLevel = rt:config(rt_lager_level, debug), - application:set_env(lager, handlers, [{lager_console_backend, LagerLevel}]), + %% Fileoutput + Outdir = proplists:get_value(outdir, ParsedArgs), + ConsoleLagerLevel = case Outdir of + undefined -> rt:config(rt_lager_level, debug); + _ -> + filelib:ensure_dir(Outdir), + notice + end, + + application:set_env(lager, handlers, [{lager_console_backend, ConsoleLagerLevel}]), lager:start(), %% rt:set_config(rtdev_path, Path), %% rt:set_config(rt_max_wait_time, 180000), %% rt:set_config(rt_retry_delay, 500), %% rt:set_config(rt_harness, rtbe), - TestResults = [ run_test(Test, HarnessArgs) || Test <- Tests], + + TestResults = [ run_test(Test, Outdir, HarnessArgs) || Test <- Tests], - print_summary(TestResults), + print_summary(TestResults, Verbose), ok. -run_test(Test, HarnessArgs) -> +run_test(Test, Outdir, HarnessArgs) -> rt:setup_harness(Test, HarnessArgs), TestA = list_to_atom(Test), - SingleTestResult = riak_test_runner:confirm(TestA), + SingleTestResult = riak_test_runner:confirm(TestA, Outdir), rt:cleanup_harness(), SingleTestResult. -print_summary(TestResults) -> +print_summary(TestResults, Verbose) -> io:format("~nTest Results:~n"), - Results = [ [ atom_to_list(proplists:get_value(test, SingleTestResult)), - proplists:get_value(status, SingleTestResult)] || SingleTestResult <- TestResults], + Results = [ + [ atom_to_list(proplists:get_value(test, SingleTestResult)), + proplists:get_value(status, SingleTestResult), + proplists:get_value(reason, SingleTestResult)] + || SingleTestResult <- TestResults], Width = test_name_width(Results), - [ io:format("~s: ~s~n", [string:left(Name, Width), Result]) || [Name, Result] <- Results], + + Print = fun(Test, Status, Reason) -> + case {Status, Verbose} of + {fail, true} -> io:format("~s: ~s ~p~n", [string:left(Test, Width), Status, Reason]); + _ -> io:format("~s: ~s~n", [string:left(Test, Width), Status]) + end + end, + [ Print(Test, Status, Reason) || [Test, Status, Reason] <- Results], PassCount = length(lists:filter(fun(X) -> proplists:get_value(status, X) =:= pass end, TestResults)), FailCount = length(lists:filter(fun(X) -> proplists:get_value(status, X) =:= fail end, TestResults)), diff --git a/src/riak_test_runner.erl b/src/riak_test_runner.erl index 4a14d021..73a05dc0 100644 --- a/src/riak_test_runner.erl +++ b/src/riak_test_runner.erl @@ -1,44 +1,52 @@ -module(riak_test_runner). %% @doc riak_test_runner runs a riak_test module's run/0 function. --export([confirm/1]). +-export([confirm/2]). --spec(confirm(atom()) -> [tuple()]). +-spec(confirm(atom(), string()) -> [tuple()]). %% @doc Runs a module's run/0 function after setting up a log capturing backend for lager. %% It then cleans up that backend and returns the logs as part of the return proplist. -confirm(TestModule) -> - start_lager_backend(), +confirm(TestModule, Outdir) -> + start_lager_backend(TestModule, Outdir), %% Check for api compatibility - Result = case proplists:get_value(confirm, + {Status, Reason} = case proplists:get_value(confirm, proplists:get_value(exports, TestModule:module_info()), -1) of 0 -> - lager:info("Running Test ~s", [TestModule]), + lager:notice("Running Test ~s", [TestModule]), execute(TestModule); _ -> lager:info("~s is not a runable test", [TestModule]), - not_a_runable_test + {not_a_runable_test, undefined} end, - lager:info("~s Test Run Complete", [TestModule]), + lager:notice("~s Test Run Complete", [TestModule]), {ok, Log} = stop_lager_backend(), - [{test, TestModule}, {status, Result}, {log, Log}]. + RetList = [{test, TestModule}, {status, Status}, {log, Log}], + case Status of + fail -> RetList ++ [{reason, Reason}]; + _ -> RetList + end. -start_lager_backend() -> - LagerLevel = rt:config(rt_lager_level, debug), - gen_event:add_handler(lager_event, riak_test_lager_backend, [LagerLevel, false]). +start_lager_backend(TestModule, Outdir) -> + case Outdir of + undefined -> ok; + _ -> gen_event:add_handler(lager_event, lager_file_backend, {Outdir ++ "/" ++ atom_to_list(TestModule) ++ ".dat_test_output", debug, 10485760, "$D0", 1}) + end, + gen_event:add_handler(lager_event, riak_test_lager_backend, [debug, false]). stop_lager_backend() -> + gen_event:delete_handler(lager_event, lager_file_backend, []), gen_event:delete_handler(lager_event, riak_test_lager_backend, []). execute(TestModule) -> try TestModule:confirm() of - ReturnVal -> ReturnVal + ReturnVal -> {ReturnVal, undefined} catch error:Error -> lager:warning("~s failed: ~p", [TestModule, Error]), - fail + {fail, Error} end. \ No newline at end of file diff --git a/src/rtdev.erl b/src/rtdev.erl index 2cda2ee5..a2b32fab 100644 --- a/src/rtdev.erl +++ b/src/rtdev.erl @@ -29,9 +29,6 @@ run_git(Path, Cmd) -> os:cmd(gitcmd(Path, Cmd)). run_riak(N, Path, Cmd) -> - %% io:format("~p~n", [riakcmd(Path, N, Cmd)]), - %%?debugFmt("RR: ~p~n", [[N,Path,Cmd]]), - %%?debugFmt("~p~n", [os:cmd(riakcmd(Path, N, Cmd))]). lager:info("Running: ~s", [riakcmd(Path, N, Cmd)]), os:cmd(riakcmd(Path, N, Cmd)). @@ -201,7 +198,7 @@ admin(Node, Args) -> Cmd = riak_admin_cmd(Path, N, Args), lager:debug("Running: ~s", [Cmd]), Result = os:cmd(Cmd), - io:format("~s", [Result]), + lager:debug("~s", [Result]), ok. node_id(Node) ->