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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2010. 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 TS Installed SCB
%%%
%%% This module does what the ts:install/0 command combined with the make
%%% parts of the ts:run/x command did.
-module(ts_install_scb).
%% Suite Callbacks
-export([init/1]).
-export([pre_init_per_suite/3]).
-export([post_init_per_suite/4]).
-export([pre_end_per_suite/3]).
-export([post_end_per_suite/4]).
-export([pre_init_per_group/3]).
-export([post_init_per_group/4]).
-export([pre_end_per_group/3]).
-export([post_end_per_group/4]).
-export([pre_init_per_testcase/3]).
-export([post_end_per_testcase/4]).
-export([on_tc_fail/3]).
-export([on_tc_skip/3]).
-export([terminate/1]).
-include_lib("kernel/include/file.hrl").
-type proplist() :: list({atom(),term()}).
-type config() :: proplist().
-type reason() :: term().
-type skip_or_fail() :: {skip, reason()} |
{auto_skip, reason()} |
{fail, reason()}.
-record(state, { ts_conf_dir, target_system, install_opts, nodenames, nodes }).
%% @doc Always called before any other callback function.
-spec init(Opts :: proplist()) ->
{Id :: term(), State :: #state{}}.
init(Opts) ->
% ct:log("CurrWD: ~p",[file:get_cwd()]),
Nodenames = proplists:get_value(nodenames, Opts, 0),
Nodes = proplists:get_value(nodes, Opts, 0),
TSConfDir = proplists:get_value(ts_conf_dir, Opts),
TargetSystem = proplists:get_value(target_system, Opts, install_local),
InstallOpts = proplists:get_value(install_opts, Opts, []),
{?MODULE, #state{ nodenames = Nodenames,
nodes = Nodes,
ts_conf_dir = TSConfDir,
target_system = TargetSystem,
install_opts = InstallOpts }}.
%% @doc Called before init_per_suite is called.
-spec pre_init_per_suite(Suite :: atom(),
Config :: config(),
State :: #state{}) ->
{config() | skip_or_fail(), NewState :: #state{}}.
pre_init_per_suite(Suite,Config,#state{ ts_conf_dir = undefined} = State) ->
DataDir = proplists:get_value(data_dir, Config),
ParentDir = filename:join(
lists:reverse(
tl(lists:reverse(filename:split(DataDir))))),
TSConfDir = filename:join([ParentDir, "..","test_server"]),
pre_init_per_suite(Suite, Config, State#state{ ts_conf_dir = TSConfDir });
pre_init_per_suite(_Suite,Config,State) ->
% ct:log("pre_init_per_suite(~p,~p,~p)",[_Suite,Config,State]),
DataDir = proplists:get_value(data_dir, Config),
try
% install(State#state.ts_conf_dir,
% State#state.target_system,
% State#state.install_opts),
{ok,Variables} =
file:consult(filename:join(State#state.ts_conf_dir,"variables")),
%% Make the stuff in all_SUITE_data if it exists
AllDir = filename:join(DataDir,"../all_SUITE_data"),
case filelib:is_dir(AllDir) of
true ->
make_non_erlang(AllDir,Variables);
false ->
ok
end,
make_non_erlang(DataDir, Variables),
{lists:keystore(nodenames, 1, Config,
{nodenames,generate_nodenames(State#state.nodenames)}),
State}
catch Error:Reason ->
Stack = erlang:get_stacktrace(),
ct:pal("~p failed! ~p:{~p,~p}",[?MODULE,Error,Reason,Stack]),
{fail,{?MODULE,{Error,Reason, Stack}}}
end.
%% @doc Called after init_per_suite.
-spec post_init_per_suite(Suite :: atom(),
Config :: config(),
Return :: config() | skip_or_fail(),
State :: #state{}) ->
{config() | skip_or_fail(), NewState :: #state{}}.
post_init_per_suite(_Suite,_Config,Return,State) ->
test_server_ctrl:kill_slavenodes(),
{Return, State}.
%% @doc Called before end_per_suite.
-spec pre_end_per_suite(Suite :: atom(),
Config :: config() | skip_or_fail(),
State :: #state{}) ->
{ok | skip_or_fail(), NewState :: #state{}}.
pre_end_per_suite(_Suite,Config,State) ->
{Config, State}.
%% @doc Called after end_per_suite.
-spec post_end_per_suite(Suite :: atom(),
Config :: config(),
Return :: term(),
State :: #state{}) ->
{ok | skip_or_fail(), NewState :: #state{}}.
post_end_per_suite(_Suite,_Config,Return,State) ->
{Return, State}.
%% @doc Called before each init_per_group.
-spec pre_init_per_group(Group :: atom(),
Config :: config(),
State :: #state{}) ->
{config() | skip_or_fail(), NewState :: #state{}}.
pre_init_per_group(_Group,Config,State) ->
{Config, State}.
%% @doc Called after each init_per_group.
-spec post_init_per_group(Group :: atom(),
Config :: config(),
Return :: config() | skip_or_fail(),
State :: #state{}) ->
{config() | skip_or_fail(), NewState :: #state{}}.
post_init_per_group(_Group,_Config,Return,State) ->
{Return, State}.
%% @doc Called after each end_per_group.
-spec pre_end_per_group(Group :: atom(),
Config :: config() | skip_or_fail(),
State :: #state{}) ->
{ok | skip_or_fail(), NewState :: #state{}}.
pre_end_per_group(_Group,Config,State) ->
{Config, State}.
%% @doc Called after each end_per_group.
-spec post_end_per_group(Group :: atom(),
Config :: config(),
Return :: term(),
State :: #state{}) ->
{ok | skip_or_fail(), NewState :: #state{}}.
post_end_per_group(_Group,_Config,Return,State) ->
{Return, State}.
%% @doc Called before each test case.
-spec pre_init_per_testcase(TC :: atom(),
Config :: config(),
State :: #state{}) ->
{config() | skip_or_fail(), NewState :: #state{}}.
pre_init_per_testcase(_TC,Config,State) ->
{Config, State}.
%% @doc Called after each test case.
-spec post_end_per_testcase(TC :: atom(),
Config :: config(),
Return :: term(),
State :: #state{}) ->
{ok | skip_or_fail(), NewState :: #state{}}.
post_end_per_testcase(_TC,_Config,Return,State) ->
{Return, State}.
%% @doc Called after a test case failed.
-spec on_tc_fail(TC :: init_per_suite | end_per_suite |
init_per_group | end_per_group | atom(),
Reason :: term(), State :: #state{}) ->
NewState :: #state{}.
on_tc_fail(_TC, _Reason, State) ->
State.
%% @doc Called when a test case is skipped.
-spec on_tc_skip(TC :: end_per_suite | init_per_group | end_per_group | atom(),
{tc_auto_skip, {failed, {Mod :: atom(), Function :: atom(),
Reason :: term()}}} |
{tc_user_skip, {skipped, Reason :: term()}},
State :: #state{}) ->
NewState :: #state{}.
on_tc_skip(_TC, _Reason, State) ->
State.
%% @doc Called when the scope of the SCB is done.
-spec terminate(State :: #state{}) ->
term().
terminate(_State) ->
% ct:log("Terminate called"),
ok.
%%% ============================================================================
%%% Local functions
%%% ============================================================================
%% Install the test environment
install(ConfDir, TargetSystem, InstallOpts) ->
{ok,CurrWD} = file:get_cwd(),
try
ConfVars = filename:join(ConfDir, "variables"),
ConfVarsIn = filename:join(ConfDir, "conf_vars.in"),
Configure = filename:join(ConfDir, "configure"),
case has_changed([ConfVars],[ConfVarsIn, Configure]) of
true ->
file:set_cwd(ConfDir),
ts_install:install(TargetSystem, InstallOpts);
false ->
ct:log("Already installed!",[])
end
after
file:set_cwd(CurrWD)
end.
%% Configure and run all the Makefiles in the data dirs of the suite
%% in question
make_non_erlang(DataDir, Variables) ->
{ok,CurrWD} = file:get_cwd(),
try
file:set_cwd(DataDir),
MakeCommand = proplists:get_value(make_command,Variables),
FirstMakefile = filename:join(DataDir,"Makefile.first"),
case filelib:is_regular(FirstMakefile) of
true ->
ct:log("Making ~p",[FirstMakefile]),
ok = ts_make:make(
MakeCommand, DataDir, filename:basename(FirstMakefile));
false ->
ok
end,
MakefileSrc = filename:join(DataDir,"Makefile.src"),
MakefileDest = filename:join(DataDir,"Makefile"),
case filelib:is_regular(MakefileSrc) of
true ->
ok = ts_lib:subst_file(MakefileSrc,MakefileDest,Variables),
ct:log("Making ~p",[MakefileDest]),
ok = ts_make:make([{makefile,"Makefile"},{data_dir,DataDir}
| Variables]);
false ->
ok
end
after
file:set_cwd(CurrWD),
timer:sleep(100)
end.
%% Check if the source files have been changed after the dest files
has_changed(Dest, Source) ->
[] == [D || D <- Dest, S <- Source, get_mtime(D) > get_mtime(S)].
get_mtime(File) ->
case file:read_file_info(File) of
{ok,#file_info{ mtime = MTime }} ->
MTime;
_Else ->
{{0,0,0},{0,0,0}}
end.
%% Copied from test_server_ctrl.erl
generate_nodenames(Num) ->
{ok,Name} = inet:gethostname(),
generate_nodenames2(Num, [Name], []).
generate_nodenames2(0, _Hosts, Acc) ->
Acc;
generate_nodenames2(N, Hosts, Acc) ->
Host=lists:nth((N rem (length(Hosts)))+1, Hosts),
Name=list_to_atom(temp_nodename("nod", []) ++ "@" ++ Host),
generate_nodenames2(N-1, Hosts, [Name|Acc]).
temp_nodename([], Acc) ->
lists:flatten(Acc);
temp_nodename([Chr|Base], Acc) ->
{A,B,C} = erlang:now(),
New = [Chr | integer_to_list(Chr bxor A bxor B+A bxor C+B)],
temp_nodename(Base, [New|Acc]).
|