-
Notifications
You must be signed in to change notification settings - Fork 23
/
run_common_test.erl
169 lines (152 loc) · 6.05 KB
/
run_common_test.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
-module(run_common_test).
-export([ct/0, ct_cover/0, cover_summary/0]).
-define(CT_DIR, filename:join([".", "tests"])).
-define(CT_REPORT, filename:join([".", "ct_report"])).
-define(CT_CONFIG, "test.config").
tests_to_run() -> [
{config, [?CT_CONFIG]},
{dir, ?CT_DIR},
{logdir, ?CT_REPORT}
].
tests_to_run(none) ->
tests_to_run();
tests_to_run(Node) ->
[{dir, Node}] ++ tests_to_run().
ct() ->
Result = ct:run_test(tests_to_run()),
case Result of
{error, Reason} ->
throw({ct_error, Reason});
_ ->
ok
end,
init:stop(0).
ct_cover() ->
run_ct_covers(get_tested_nodes()),
init:stop(0).
run_ct_covers([]) ->
%%There is no tested nodes configuration. Assume that ejabberd node is running
run_ct_cover1();
run_ct_covers([Node | []]) ->
run_ct_cover(Node),
run_ejabberd(Node),
cover_summary(),
stop_ejabberd(Node);
run_ct_covers([Node | Tail]) ->
run_ct_cover(Node),
run_ct_covers(Tail).
run_ct_cover(Node) ->
io:format("will test node ~p~n", [Node]),
StartStatus = run_ejabberd(Node),
io:format("start status ~p~n", [StartStatus]),
NodeStr = get_node_str(Node),
os:cmd("cp -R tests "++NodeStr),
run_ct_cover1(NodeStr),
StopStatus = stop_ejabberd(Node),
os:cmd("rm -rf "++NodeStr),
io:format("stop status ~p~n", [StopStatus]).
run_ct_cover1() ->
run_ct_cover1(none).
run_ct_cover1(Node) ->
prepare(),
ct:run_test(tests_to_run(Node)),
analyze(Node),
{MS,S,_} = now(),
FileName = lists:flatten(io_lib:format("/tmp/ejd_test_run_~b~b.coverdata",[MS,S])),
io:format("export current cover ~p~n", [cover_call(export, [FileName])]),
io:format("test finished~n").
cover_summary() ->
prepare(),
Files = rpc:call(get_ejabberd_node(), filelib, wildcard, ["/tmp/ejd_test_run_*.coverdata"]),
lists:foreach(fun(F) ->
io:format("import ~p cover ~p~n", [F, cover_call(import, [F])])
end,
Files),
analyze(summary),
io:format("summary completed~n"),
init:stop(0).
prepare() ->
cover_call(start),
Compiled = cover_call(compile_beam_directory,["lib/ejabberd-2.1.8/ebin"]),
rpc:call(get_ejabberd_node(), application, stop, [ejabberd]),
StartStatus = rpc:call(get_ejabberd_node(), application, start, [ejabberd, permanent]),
io:format("start ~p~n", [StartStatus]),
io:format("Compiled modules ~p~n", [Compiled]).
%%timer:sleep(10000).
analyze(Node) ->
Modules = cover_call(modules),
io:format("node ~s~n", [Node]),
FilePath = case {Node, file:read_file(?CT_REPORT++"/index.html")} of
{summary, {ok, IndexFileData}} ->
R = re:replace(IndexFileData, "<a href=\"all_runs.html\">ALL RUNS</a>", "& <a href=\"cover.html\" style=\"margin-right:5px\">COVER</a>"),
file:write_file(?CT_REPORT++"/index.html", R),
?CT_REPORT++"/cover.html";
{_, {ok, IndexFileData}} ->
{match, [PosLen]} = re:run(IndexFileData, "<a href=\"(.*"++Node++".*)\">",[{capture, [1]}]),
filename:dirname(?CT_REPORT++"/"++binary_to_list(binary:part(IndexFileData, PosLen)))++"/cover.html";
_ -> skip
end,
CoverageDir = filename:dirname(FilePath)++"/coverage",
rpc:call(get_ejabberd_node(), file, make_dir, ["/tmp/coverage"]),
{ok, File} = file:open(FilePath, [write]),
file:write(File, "<html>\n<head></head>\n<body bgcolor=\"white\" text=\"black\" link=\"blue\" vlink=\"purple\" alink=\"red\">\n"),
file:write(File, "<h1>Coverage for application 'esl-ejabberd'</h1>\n"),
file:write(File, "<table border=3 cellpadding=5>\n"),
file:write(File, "<tr><th>Module</th><th>Covered (%)</th><th>Covered (Lines)</th><th>Not covered (Lines)</th><th>Total (Lines)</th></tr>"),
Fun = fun(Module, {CAcc, NCAcc}) ->
FileName = lists:flatten(io_lib:format("~s.COVER.html",[Module])),
FilePathC = filename:join(["/tmp/coverage", FileName]),
cover_call(analyse_to_file, [Module, FilePathC, [html]]),
{ok, {Module, {C, NC}}} = cover_call(analyse, [Module, module]),
file:write(File, row(atom_to_list(Module), C, NC, percent(C,NC),"coverage/"++FileName)),
{CAcc + C, NCAcc + NC}
end,
io:format("coverage analyzing~n"),
{CSum, NCSum} = lists:foldl(Fun, {0, 0}, Modules),
os:cmd("cp -R /tmp/coverage "++CoverageDir),
file:write(File, row("Summary", CSum, NCSum, percent(CSum, NCSum), "#")),
file:close(File).
cover_call(Function) ->
cover_call(Function, []).
cover_call(Function, Args) ->
rpc:call(get_ejabberd_node(), cover, Function, Args).
get_ejabberd_node() ->
{ok, Props} = file:consult(?CT_CONFIG),
{ejabberd_node, Node} = proplists:lookup(ejabberd_node, Props),
Node.
get_tested_nodes() ->
{ok, Props} = file:consult(?CT_CONFIG),
case proplists:lookup(ejabberd_nodes, Props) of
none -> [];
{ejabberd_nodes, Nodes} -> Nodes
end.
percent(0, _) -> 0;
percent(C, NC) when C /= 0; NC /= 0 -> round(C / (NC+C) * 100);
percent(_, _) -> 100.
row(Row, C, NC, Percent, Path) ->
[
"<tr>",
"<td><a href='", Path, "'>", Row, "</a></td>",
"<td>", integer_to_list(Percent), "%</td>",
"<td>", integer_to_list(C), "</td>",
"<td>", integer_to_list(NC), "</td>",
"<td>", integer_to_list(C+NC), "</td>",
"</tr>\n"
].
get_node_str({Node, _, _}) ->
get_node_str(Node);
get_node_str(Node) -> atom_to_list(Node).
run_ejabberd({_Node, StartCmd, _}) ->
do_start_cmd(StartCmd);
run_ejabberd(Node) ->
StartCmd = lists:flatten(io_lib:format("../dev/ejabberd_~p/bin/ejabberd start", [Node])),
do_start_cmd(StartCmd).
do_start_cmd(StartCmd) ->
Status = os:cmd(StartCmd),
timer:sleep(3000),
Status.
stop_ejabberd({_Node, _, StopCmd}) ->
os:cmd(StopCmd);
stop_ejabberd(Node) ->
StopCmd = lists:flatten(io_lib:format("../dev/ejabberd_~p/bin/ejabberd stop", [Node])),
os:cmd(StopCmd).