Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test group annotations to riak_test #674

Merged
merged 3 commits into from
Aug 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 74 additions & 16 deletions src/riak_test_escript.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,26 @@
-export([add_deps/1]).

main(Args) ->
{ParsedArgs, HarnessArgs, Tests} = prepare(Args),
OutDir = proplists:get_value(outdir, ParsedArgs),
Results = execute(Tests, OutDir, report(ParsedArgs), HarnessArgs),
finalize(Results, ParsedArgs).
case prepare(Args) of
ok ->
ok;
{ParsedArgs, HarnessArgs, Tests} ->
OutDir = proplists:get_value(outdir, ParsedArgs),
Results = execute(Tests, OutDir, report(ParsedArgs), HarnessArgs),
finalize(Results, ParsedArgs)
end.

prepare(Args) ->
{ParsedArgs, _, Tests} = ParseResults = parse_args(Args),
io:format("Tests to run: ~p~n", [Tests]),
ok = erlang_setup(ParsedArgs),
ok = test_setup(ParsedArgs),
ParseResults.
case parse_args(Args) of
{ParsedArgs, _, Tests} = ParseResults ->
io:format("Tests to run: ~p~n", [Tests]),
ok = erlang_setup(ParsedArgs),
ok = test_setup(ParsedArgs),
ParseResults;
ok ->
io:format("No tests to run.~n"),
ok
end.

execute(Tests, Outdir, Report, HarnessArgs) ->
TestCount = length(Tests),
Expand Down Expand Up @@ -66,6 +75,7 @@ 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"},
{groups, $g, "groups", string, "specifiy a list of test groups to run"},
{dir, $d, "dir", string, "run all tests in the specified directory"},
{skip, $x, "skip", string, "list of tests to skip in a directory"},
{verbose, $v, "verbose", undefined, "verbose output"},
Expand Down Expand Up @@ -119,23 +129,40 @@ parse_args(Args) ->
help_or_parse_args({ok, {[], _}}) ->
print_help();
help_or_parse_args({ok, {ParsedArgs, HarnessArgs}}) ->
help_or_parse_tests(ParsedArgs, HarnessArgs, lists:member(help, ParsedArgs));
help_or_parse_tests(ParsedArgs,
HarnessArgs,
lists:member(help, ParsedArgs),
args_invalid(ParsedArgs));
help_or_parse_args(_) ->
print_help().

help_or_parse_tests(_, _, true) ->
help_or_parse_tests(_, _, true, _) ->
print_help();
help_or_parse_tests(_, _, false, true) ->
print_help();
help_or_parse_tests(ParsedArgs, HarnessArgs, false) ->
help_or_parse_tests(ParsedArgs, HarnessArgs, false, false) ->
%% Have to load the `riak_test' config prior to assembling the
%% test metadata
load_initial_config(ParsedArgs),

TestData = compose_test_data(ParsedArgs),
Groups = proplists:get_all_values(groups, ParsedArgs),
TestData = load_tests(Groups, ParsedArgs),

Tests = which_tests_to_run(report(ParsedArgs), TestData),
Offset = rt_config:get(offset, undefined),
Workers = rt_config:get(workers, undefined),
shuffle_tests(ParsedArgs, HarnessArgs, Tests, Offset, Workers).

args_invalid(ParsedArgs) ->
case { proplists:is_defined(groups, ParsedArgs),
proplists:is_defined(tests, ParsedArgs) } of
{true, true} ->
io:format("--groups and --tests are currently mutually exclusive.~n~n"),
true;
{_, _} ->
false
end.

load_initial_config(ParsedArgs) ->
%% Loads application defaults
application:load(riak_test),
Expand Down Expand Up @@ -231,9 +258,11 @@ maybe_teardown(true, TestResults, Coverage, Verbose) ->
end,
ok.

compose_test_data(ParsedArgs) ->
load_tests([], ParsedArgs) ->
RawTestList = proplists:get_all_values(tests, ParsedArgs),
TestList = lists:foldl(fun(X, Acc) -> string:tokens(X, ", ") ++ Acc end, [], RawTestList),
TestList = lists:foldl(fun(X, Acc) ->
string:tokens(X, ", ") ++ Acc
end, [], RawTestList),
%% Parse Command Line Tests
{CodePaths, SpecificTests} =
lists:foldl(fun extract_test_names/2,
Expand All @@ -246,8 +275,35 @@ compose_test_data(ParsedArgs) ->
Dirs = proplists:get_all_values(dir, ParsedArgs),
SkipTests = string:tokens(proplists:get_value(skip, ParsedArgs, []), [$,]),
DirTests = lists:append([load_tests_in_dir(Dir, SkipTests) || Dir <- Dirs]),
Project = list_to_binary(rt_config:get(rt_project, "undefined")),
compose_test_data(DirTests, SpecificTests, ParsedArgs);
load_tests(RawGroupList, ParsedArgs) ->
Groups = lists:foldl(fun(X, Acc) ->
string:tokens(X, ", ") ++ Acc
end, [], RawGroupList),
Dirs = proplists:get_value(dir, ParsedArgs, ["./ebin"]),
AllDirTests = lists:append([load_tests_in_dir(Dir, []) || Dir <- Dirs]),
DirTests = get_group_tests(AllDirTests, Groups),
compose_test_data(DirTests, [], ParsedArgs).

get_group_tests(Tests, Groups) ->
lists:filter(fun(Test) ->
Mod = list_to_atom(Test),
Attrs = Mod:module_info(attributes),
match_group_attributes(Attrs, Groups)
end, Tests).

match_group_attributes(Attributes, Groups) ->
case proplists:get_value(test_type, Attributes) of
undefined ->
false;
TestTypes ->
lists:member(true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was searching for some way to express this a little more clearly. Using ordsets would be nice, but the process getting the groups converted is perhaps more painful, but the groups could be converted to atoms before reaching this point then you could write:

 not ordsets:is_disjoint(ordsets:from_list(TestTypes), 
                                       ordsets:from_list(Groups)),     

It's arguably easier to understand, but really moot without a having to iterate through the groups list twice. It would be really nice if the getopt library let you specify something like string-list as a type to indicate and automatically tokenize comma-separated option lists.

[ TestType == list_to_atom(Group)
|| Group <- Groups, TestType <- TestTypes ])
end.

compose_test_data(DirTests, SpecificTests, ParsedArgs) ->
Project = list_to_binary(rt_config:get(rt_project, "undefined")),
Backends = case proplists:get_all_values(backend, ParsedArgs) of
[] -> [undefined];
Other -> Other
Expand All @@ -259,6 +315,7 @@ compose_test_data(ParsedArgs) ->
TestFoldFun = test_data_fun(rt:get_version(), Project, Backends, Upgrades),
lists:foldl(TestFoldFun, [], lists:usort(DirTests ++ SpecificTests)).


test_data_fun(Version, Project, Backends, Upgrades) ->
fun(Test, Tests) ->
[{list_to_atom(Test),
Expand Down Expand Up @@ -492,3 +549,4 @@ so_kill_riak_maybe() ->
io:format("Leaving Riak Up... "),
rt:whats_up()
end.

2 changes: 2 additions & 0 deletions tests/bucket_types.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
-include_lib("eunit/include/eunit.hrl").
-include("rt.hrl").

-test_type([bucket_types]).

properties() ->
CustomConfig = rt_cluster:augment_config(riak_core,
{default_bucket_props, [{n_val, 2}]},
Expand Down
2 changes: 2 additions & 0 deletions tests/ensemble_basic.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
-export([confirm/0]).
-include_lib("eunit/include/eunit.hrl").

-test_type([ensemble]).

confirm() ->
NumNodes = 5,
NVal = 5,
Expand Down
2 changes: 2 additions & 0 deletions tests/http_bucket_types.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
-include_lib("riakc/include/riakc.hrl").
-include("rt.hrl").

-test_type([bucket_types, http]).

properties() ->
CustomConfig = rt_cluster:augment_config(riak_core,
{default_bucket_props, [{n_val, 2}]},
Expand Down