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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2007-2009. 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%
%%
%%%-------------------------------------------------------------------
%%% File : erlexec_SUITE.erl
%%% Author : Rickard Green <[email protected]>
%%% Description : Test erlexec's command line parsing
%%%
%%% Created : 22 May 2007 by Rickard Green <[email protected]>
%%%-------------------------------------------------------------------
-module(erlexec_SUITE).
%-define(line_trace, 1).
-define(DEFAULT_TIMEOUT, ?t:minutes(1)).
-export([all/1, init_per_testcase/2, fin_per_testcase/2]).
-export([args_file/1, evil_args_file/1, env/1, args_file_env/1, otp_7461/1, otp_7461_remote/1, otp_8209/1]).
-include("test_server.hrl").
init_per_testcase(Case, Config) ->
Dog = ?t:timetrap(?DEFAULT_TIMEOUT),
SavedEnv = save_env(),
[{testcase, Case}, {watchdog, Dog}, {erl_flags_env, SavedEnv} |Config].
fin_per_testcase(_Case, Config) ->
Dog = ?config(watchdog, Config),
SavedEnv = ?config(erl_flags_env, Config),
restore_env(SavedEnv),
cleanup_nodes(),
?t:timetrap_cancel(Dog),
ok.
all(doc) -> [];
all(suite) ->
[args_file, evil_args_file, env, args_file_env, otp_7461, otp_8209].
otp_8209(doc) ->
["Test that plain first argument does not "
"destroy -home switch [OTP-8209]"];
otp_8209(suite) ->
[];
otp_8209(Config) when is_list(Config) ->
?line {ok,[[PName]]} = init:get_argument(progname),
?line SNameS = "erlexec_test_01",
?line SName = list_to_atom(SNameS++"@"++
hd(tl(string:tokens(atom_to_list(node()),"@")))),
?line Cmd = PName ++ " dummy_param -sname "++SNameS++" -setcookie "++
atom_to_list(erlang:get_cookie()),
?line open_port({spawn,Cmd},[]),
?line pong = loop_ping(SName,40),
?line {ok,[[_]]} = rpc:call(SName,init,get_argument,[home]),
?line ["dummy_param"] = rpc:call(SName,init,get_plain_arguments,[]),
?line ok = cleanup_nodes(),
ok.
cleanup_nodes() ->
cleanup_node("erlexec_test_01",20).
cleanup_node(SNameS,0) ->
{error, {would_not_die,list_to_atom(SNameS)}};
cleanup_node(SNameS,N) ->
SName = list_to_atom(SNameS++"@"++
hd(tl(string:tokens(atom_to_list(node()),"@")))),
case rpc:call(SName,init,stop,[]) of
{badrpc,_} ->
ok;
ok ->
receive after 500 -> ok end,
cleanup_node(SNameS,N-1)
end.
loop_ping(_,0) ->
pang;
loop_ping(Node,N) ->
case net_adm:ping(Node) of
pang ->
receive
after 500 ->
ok
end,
loop_ping(Node, N-1);
pong ->
pong
end.
args_file(doc) -> [];
args_file(suite) -> [];
args_file(Config) when is_list(Config) ->
?line AFN1 = privfile("1", Config),
?line AFN2 = privfile("2", Config),
?line AFN3 = privfile("3", Config),
?line AFN4 = privfile("4", Config),
?line AFN5 = privfile("5", Config),
?line AFN6 = privfile("6", Config),
?line write_file(AFN1,
"-MiscArg2~n"
"# a comment +\\#1000~n"
"+\\#200 # another comment~n"
"~n"
"# another config file to read~n"
" -args_file ~s#acomment~n"
"~n"
"-MiscArg7~n"
"#~n"
"+\\#700~n"
"-extra +XtraArg6~n",
[AFN2]),
?line write_file(AFN2,
"-MiscArg3~n"
"+\\#300~n"
"-args_file ~s~n"
"-MiscArg5~n"
"+\\#500#anothercomment -MiscArg10~n"
"-args_file ~s~n"
"-args_file ~s~n"
"-args_file ~s~n"
"-extra +XtraArg5~n",
[AFN3, AFN4, AFN5, AFN6]),
?line write_file(AFN3,
"# comment again~n"
" -MiscArg4 +\\#400 -extra +XtraArg1"),
?line write_file(AFN4,
" -MiscArg6 +\\#600 -extra +XtraArg2~n"
"+XtraArg3~n"
"+XtraArg4~n"
"# comment again~n"),
?line write_file(AFN5, ""),
?line write_file(AFN6, "-extra # +XtraArg10~n"),
?line CmdLine = "+#100 -MiscArg1 "
++ "-args_file " ++ AFN1
++ " +#800 -MiscArg8 -extra +XtraArg7 +XtraArg8",
?line {Emu, Misc, Extra} = emu_args(CmdLine),
?line verify_args(["-#100", "-#200", "-#300", "-#400",
"-#500", "-#600", "-#700", "-#800"], Emu),
?line verify_args(["-MiscArg1", "-MiscArg2", "-MiscArg3", "-MiscArg4",
"-MiscArg5", "-MiscArg6", "-MiscArg7", "-MiscArg8"],
Misc),
?line verify_args(["+XtraArg1", "+XtraArg2", "+XtraArg3", "+XtraArg4",
"+XtraArg5", "+XtraArg6", "+XtraArg7", "+XtraArg8"],
Extra),
?line verify_not_args(["-MiscArg10", "-#1000", "+XtraArg10",
"-MiscArg1", "-MiscArg2", "-MiscArg3", "-MiscArg4",
"-MiscArg5", "-MiscArg6", "-MiscArg7", "-MiscArg8",
"+XtraArg1", "+XtraArg2", "+XtraArg3", "+XtraArg4",
"+XtraArg5", "+XtraArg6", "+XtraArg7", "+XtraArg8"],
Emu),
?line verify_not_args(["-MiscArg10", "-#1000", "+XtraArg10",
"-#100", "-#200", "-#300", "-#400",
"-#500", "-#600", "-#700", "-#800",
"+XtraArg1", "+XtraArg2", "+XtraArg3", "+XtraArg4",
"+XtraArg5", "+XtraArg6", "+XtraArg7", "+XtraArg8"],
Misc),
?line verify_not_args(["-MiscArg10", "-#1000", "+XtraArg10",
"-#100", "-#200", "-#300", "-#400",
"-#500", "-#600", "-#700", "-#800",
"-MiscArg1", "-MiscArg2", "-MiscArg3", "-MiscArg4",
"-MiscArg5", "-MiscArg6", "-MiscArg7", "-MiscArg8"],
Extra),
?line ok.
evil_args_file(doc) -> [];
evil_args_file(suite) -> [];
evil_args_file(Config) when is_list(Config) ->
?line Lim = 300,
?line FNums = lists:seq(1, Lim),
lists:foreach(fun (End) when End == Lim ->
?line AFN = privfile(integer_to_list(End), Config),
?line write_file(AFN,
"-MiscArg~p ",
[End]);
(I) ->
?line AFNX = privfile(integer_to_list(I), Config),
?line AFNY = privfile(integer_to_list(I+1), Config),
{Frmt, Args} =
case I rem 2 of
0 ->
{"-MiscArg~p -args_file ~s -MiscArg~p",
[I, AFNY, I]};
_ ->
{"-MiscArg~p -args_file ~s",
[I, AFNY]}
end,
?line write_file(AFNX, Frmt, Args)
end,
FNums),
?line {_Emu, Misc, _Extra} = emu_args("-args_file "
++ privfile("1", Config)),
?line ANums = FNums
++ lists:reverse(lists:filter(fun (I) when I == Lim -> false;
(I) when I rem 2 == 0 -> true;
(_) -> false
end, FNums)),
?line verify_args(lists:map(fun (I) -> "-MiscArg"++integer_to_list(I) end,
ANums),
Misc),
?line ok.
env(doc) -> [];
env(suite) -> [];
env(Config) when is_list(Config) ->
?line os:putenv("ERL_AFLAGS", "-MiscArg1 +#100 -extra +XtraArg1 +XtraArg2"),
?line CmdLine = "+#200 -MiscArg2 -extra +XtraArg3 +XtraArg4",
?line os:putenv("ERL_FLAGS", "-MiscArg3 +#300 -extra +XtraArg5"),
?line os:putenv("ERL_ZFLAGS", "-MiscArg4 +#400 -extra +XtraArg6"),
?line {Emu, Misc, Extra} = emu_args(CmdLine),
?line verify_args(["-#100", "-#200", "-#300", "-#400"], Emu),
?line verify_args(["-MiscArg1", "-MiscArg2", "-MiscArg3", "-MiscArg4"],
Misc),
?line verify_args(["+XtraArg1", "+XtraArg2", "+XtraArg3", "+XtraArg4",
"+XtraArg5", "+XtraArg6"],
Extra),
?line ok.
args_file_env(doc) -> [];
args_file_env(suite) -> [];
args_file_env(Config) when is_list(Config) ->
?line AFN1 = privfile("1", Config),
?line AFN2 = privfile("2", Config),
?line write_file(AFN1, "-MiscArg2 +\\#200 -extra +XtraArg1"),
?line write_file(AFN2, "-MiscArg3 +\\#400 -extra +XtraArg3"),
?line os:putenv("ERL_AFLAGS",
"-MiscArg1 +#100 -args_file "++AFN1++ " -extra +XtraArg2"),
?line CmdLine = "+#300 -args_file "++AFN2++" -MiscArg4 -extra +XtraArg4",
?line os:putenv("ERL_FLAGS", "-MiscArg5 +#500 -extra +XtraArg5"),
?line os:putenv("ERL_ZFLAGS", "-MiscArg6 +#600 -extra +XtraArg6"),
?line {Emu, Misc, Extra} = emu_args(CmdLine),
?line verify_args(["-#100", "-#200", "-#300", "-#400",
"-#500", "-#600"], Emu),
?line verify_args(["-MiscArg1", "-MiscArg2", "-MiscArg3", "-MiscArg4",
"-MiscArg5", "-MiscArg6"],
Misc),
?line verify_args(["+XtraArg1", "+XtraArg2", "+XtraArg3", "+XtraArg4",
"+XtraArg5", "+XtraArg6"],
Extra),
?line ok.
%% Make sure "erl -detached" survives when parent process group gets killed
otp_7461(doc) -> [];
otp_7461(suite) -> [];
otp_7461(Config) when is_list(Config) ->
case os:type() of
{unix,_} ->
{NetStarted, _} = net_kernel:start([test_server, shortnames]),
try
net_kernel:monitor_nodes(true),
register(otp_7461, self()),
otp_7461_do(Config)
after
catch unregister(otp_7461),
catch net_kernel:monitor_nodes(false),
case NetStarted of
ok -> net_kernel:stop();
_ -> ok
end
end;
_ ->
{skip,"Only on Unix."}
end.
otp_7461_do(Config) ->
io:format("alive=~p node=~p\n",[is_alive(), node()]),
TestProg = filename:join([?config(data_dir, Config), "erlexec_tests"]),
{ok, [[ErlProg]]} = init:get_argument(progname),
?line Cmd = TestProg ++ " " ++ ErlProg ++
" -detached -sname " ++ get_nodename(otp_7461) ++
" -setcookie " ++ atom_to_list(erlang:get_cookie()) ++
" -pa " ++ filename:dirname(code:which(?MODULE)) ++
" -s erlexec_SUITE otp_7461_remote init " ++ atom_to_list(node()),
%% otp_7461 --------> erlexec_tests.c --------> cerl -detached
%% open_port fork+exec
io:format("spawn port prog ~p\n",[Cmd]),
?line Port = open_port({spawn, Cmd}, [eof]),
io:format("Wait for node to connect...\n",[]),
?line {nodeup, Slave} = receive Msg -> Msg
after 20*1000 -> timeout end,
io:format("Node alive: ~p\n", [Slave]),
?line pong = net_adm:ping(Slave),
io:format("Ping ok towards ~p\n", [Slave]),
?line Port ! { self(), {command, "K"}}, % Kill child process group
?line {Port, {data, "K"}} = receive Msg2 -> Msg2 end,
?line port_close(Port),
%% Now the actual test. Detached node should still be alive.
?line pong = net_adm:ping(Slave),
io:format("Ping still ok towards ~p\n", [Slave]),
%% Halt node
?line rpc:cast(Slave, ?MODULE, otp_7461_remote, [[halt, self()]]),
?line {nodedown, Slave} = receive Msg3 -> Msg3
after 20*1000 -> timeout end,
io:format("Node dead: ~p\n", [Slave]),
ok.
%% Executed on slave node
otp_7461_remote([init, Master]) ->
io:format("otp_7461_remote(init,~p) at ~p\n",[Master, node()]),
net_kernel:connect_node(Master);
otp_7461_remote([halt, Pid]) ->
io:format("halt order from ~p to node ~p\n",[Pid,node()]),
halt().
%%
%% Utils
%%
save_env() ->
{erl_flags,
os:getenv("ERL_AFLAGS"),
os:getenv("ERL_FLAGS"),
os:getenv("ERL_"++erlang:system_info(otp_release)++"_FLAGS"),
os:getenv("ERL_ZFLAGS")}.
restore_env(EVar, false) when is_list(EVar) ->
restore_env(EVar, "");
restore_env(EVar, "") when is_list(EVar) ->
case os:getenv(EVar) of
false -> ok;
"" -> ok;
" " -> ok;
_ -> os:putenv(EVar, " ")
end;
restore_env(EVar, Value) when is_list(EVar), is_list(Value) ->
case os:getenv(EVar) of
Value -> ok;
_ -> os:putenv(EVar, Value)
end.
restore_env({erl_flags, AFlgs, Flgs, RFlgs, ZFlgs}) ->
restore_env("ERL_AFLAGS", AFlgs),
restore_env("ERL_FLAGS", Flgs),
restore_env("ERL_"++erlang:system_info(otp_release)++"_FLAGS", RFlgs),
restore_env("ERL_ZFLAGS", ZFlgs),
ok.
privfile(Name, Config) ->
filename:join([?config(priv_dir, Config),
atom_to_list(?config(testcase, Config)) ++ "." ++ Name]).
write_file(FileName, Frmt) ->
write_file(FileName, Frmt, []).
write_file(FileName, Frmt, Args) ->
{ok, File} = file:open(FileName, [write]),
io:format(File, Frmt, Args),
ok = file:close(File).
verify_args([], _Ys) ->
ok;
verify_args(Xs, []) ->
exit({args_not_found_in_order, Xs});
verify_args([X|Xs], [X|Ys]) ->
verify_args(Xs, Ys);
verify_args(Xs, [_Y|Ys]) ->
verify_args(Xs, Ys).
verify_not_args(Xs, Ys) ->
lists:foreach(fun (X) ->
case lists:member(X, Ys) of
true -> exit({arg_present, X});
false -> ok
end
end,
Xs).
emu_args(CmdLineArgs) ->
io:format("CmdLineArgs = ~s~n", [CmdLineArgs]),
{ok,[[Erl]]} = init:get_argument(progname),
EmuCL = os:cmd(Erl ++ " -emu_args_exit " ++ CmdLineArgs),
io:format("EmuCL = ~s", [EmuCL]),
split_emu_clt(string:tokens(EmuCL, [$ ,$\t,$\n,$\r])).
split_emu_clt(EmuCLT) ->
split_emu_clt(EmuCLT, [], [], [], emu).
split_emu_clt([], _Emu, _Misc, _Extra, emu) ->
exit(bad_cmd_line);
split_emu_clt([], Emu, Misc, Extra, _Type) ->
{lists:reverse(Emu), lists:reverse(Misc), lists:reverse(Extra)};
split_emu_clt(["--"|As], Emu, Misc, Extra, emu) ->
split_emu_clt(As, Emu, Misc, Extra, misc);
split_emu_clt([A|As], Emu, Misc, Extra, emu = Type) ->
split_emu_clt(As, [A|Emu], Misc, Extra, Type);
split_emu_clt(["-extra"|As], Emu, Misc, Extra, misc) ->
split_emu_clt(As, Emu, Misc, Extra, extra);
split_emu_clt([A|As], Emu, Misc, Extra, misc = Type) ->
split_emu_clt(As, Emu, [A|Misc], Extra, Type);
split_emu_clt([A|As], Emu, Misc, Extra, extra = Type) ->
split_emu_clt(As, Emu, Misc, [A|Extra], Type).
get_nodename(T) ->
{A, B, C} = now(),
atom_to_list(T)
++ "-"
++ atom_to_list(?MODULE)
++ "-"
++ integer_to_list(A)
++ "-"
++ integer_to_list(B)
++ "-"
++ integer_to_list(C).
|