aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stdlib/test/slave_SUITE.erl
blob: 3b737af64deb9a031dac1d5c3f68ffb0744b91a1 (plain) (blame)
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
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1997-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%
%%
-module(slave_SUITE).

-include("test_server.hrl").

-export([all/1, t_start/1, t_start_link/1,
	 start_link_nodedown/1, errors/1]).

%% Internal exports.
-export([fun_init/1, test_errors/1]).
-export([timeout_test/1, auth_test/1, rsh_test/1, start_a_slave/3]).

all(suite) ->
    [t_start_link, start_link_nodedown, t_start, errors].

t_start_link(suite) -> [];
t_start_link(Config) when list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(20)),

    %% Define useful variables.

    ?line Host = host(),
    ?line Slave1 = node_name(Host, slave1),
    ?line Slave2 = node_name(Host, slave2),

    %% Test slave:start_link() with one, two, and three arguments.

    ?line ThisNode = node(),
    ?line {error, {already_running, ThisNode}} = slave:start_link(Host),
    ?line {ok, Slave1} = slave:start_link(Host, slave1),
    ?line {ok, Slave2} = slave:start_link(Host, slave2, "-my_option 42"),
    ?line {ok, [["42"]]} = rpc:call(Slave2, init, get_argument, [my_option]),

    %% Kill the two slave nodes and verify that they are dead.

    ?line rpc:cast(Slave1, erlang, halt, []),
    ?line rpc:cast(Slave2, erlang, halt, []),
    ?line is_dead(Slave1),
    ?line is_dead(Slave2),

    %% Start two slave nodes from another process and verify that
    %% the slaves die when that process terminates.

    Parent = self(),
    Pid = fun_spawn(fun () ->
			    {ok, Slave1} = slave:start_link(Host, slave1),
			    {ok, Slave2} = slave:start_link(Host, slave2),
			    Parent ! slaves_started,
			    receive never -> ok end
		    end),
    ?line receive slaves_started -> ok end,
    ?line process_flag(trap_exit, true),
    ?line wait_alive(Slave1),
    ?line wait_alive(Slave2),
    ?line exit(Pid, kill),
    ?line receive {'EXIT', Pid, killed} -> ok end,
    ?line test_server:sleep(250),
    ?line is_dead(Slave1),
    ?line is_dead(Slave2),
		  
    ?line test_server:timetrap_cancel(Dog),
    ok.

%% Test that slave:start_link() works when the master exits.

start_link_nodedown(suite) -> [];
start_link_nodedown(Config) when list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(20)),

    %% Define useful variables.

    ?line Host = host(),
    ?line Master = node_name(Host, my_master),
    ?line Slave = node_name(Host, my_slave),

    ?line Pa = "-pa " ++ filename:dirname(code:which(?MODULE)),
    ?line {ok, Master} = slave:start_link(Host, my_master, Pa),
    ?line spawn(Master, ?MODULE, start_a_slave, [self(), Host, my_slave]),
    ?line {reply, {ok, _Node}} = receive Any -> Any end,
    
    ?line rpc:call(Master, erlang, halt, []),
    ?line receive after 200 -> ok end,
    ?line pang = net_adm:ping(Slave),

    ?line test_server:timetrap_cancel(Dog),
    ok.

start_a_slave(ReplyTo, Host, Name) ->
    ReplyTo ! {reply, slave:start_link(Host, Name)},
    receive never -> ok end.

%% Test slave:start().

t_start(suite) -> [];
t_start(Config) when list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(20)),
    
    %% Define useful variables.

    ?line Host = host(),
    ?line Slave1 = node_name(Host, slave1),
    ?line Slave2 = node_name(Host, slave2),

    %% By running all tests from this master node which is linked
    %% to this test case, we ensure that all slaves are killed
    %% if this test case fails.  (If they are not, and therefore further
    %% test cases fail, there is a bug in slave.)

    ?line {ok, Master} = slave:start_link(Host, master),
    
    %% Test slave:start() with one, two, and three arguments.

    ?line ThisNode = node(),
    ?line {error, {already_running, ThisNode}} = slave:start(Host),
    ?line {ok, Slave1} = rpc:call(Master, slave, start, [Host, slave1]),
    ?line {ok, Slave2} = rpc:call(Master, slave, start,
				  [Host, slave2, "-my_option 42"]),
    ?line {ok, [["42"]]} = rpc:call(Slave2, init, get_argument, [my_option]),

    %% Test that a slave terminates when its master node terminates.

    ?line ok = slave:stop(Slave2),
    ?line is_dead(Slave2),
    ?line {ok, Slave2} = rpc:call(Slave1, slave, start, [Host, slave2]),
    ?line is_alive(Slave2),
    ?line rpc:call(Slave1, erlang, halt, []),	% Kill master.
    receive after 1000 -> ok end,		% Make sure slaves have noticed
						% their dead master.
    ?line is_dead(Slave1),
    ?line is_dead(Slave2),			% Slave should be dead, too.

    %% Kill all slaves and verify that they are dead.

    ?line ok = slave:stop(Slave1),
    ?line ok = slave:stop(Slave2),
    ?line is_dead(Slave1),
    ?line is_dead(Slave2),

    ?line test_server:timetrap_cancel(Dog),
    ok.

%% Test the various error conditions in parallell (since the timeout
%% in slave is 32 seconds).

errors(suite) -> [];
errors(Config) when list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(50)),

    ?line process_flag(trap_exit, true),
    ?line Pa = filename:dirname(code:which(?MODULE)),
    ?line {ok, Master} = slave_start_link(host(), master,
					  "-rsh no_rsh_program -pa "++Pa++
					  " -env ERL_CRASH_DUMP erl_crash_dump.master"),
    ?line Pids = rpc:call(Master, ?MODULE, test_errors, [self()]),
    ?line wait_for_result(Pids),

    ?line test_server:timetrap_cancel(Dog),
    ok.

wait_for_result([]) ->
    ok;
wait_for_result(Pids) ->
    ?line receive
	      {'EXIT', Pid, normal} ->
		  io:format("Process ~p terminated", [Pid]),
		  wait_for_result(lists:delete(Pid, Pids));
	      {'EXIT', _, Reason} ->
		  exit(Reason)
	  end.

show_process_info(Pid) ->
    io:format("~p: ~p", [Pid, catch process_info(Pid, initial_call)]).

test_errors(ResultTo) ->
    %% Sigh!  We use ordinary spawn instead of fun_spawn/1 to be able
    %% identify the processes by their initial call.
    ?line P1 = spawn(?MODULE, timeout_test, [ResultTo]),
    ?line P2 = spawn(?MODULE, auth_test, [ResultTo]),
    ?line P3 = spawn(?MODULE, rsh_test, [ResultTo]),
    Pids =[P1, P2, P3],
    ?line lists:foreach(fun show_process_info/1, Pids),
    Pids.

timeout_test(ResultTo) ->
    link(ResultTo),
    ?line {error, timeout} = slave:start(host(), slave1, "-boot no_boot_script").

auth_test(ResultTo) ->
    link(ResultTo),
    ?line {error, timeout} = slave:start(host(), slave2,
					 "-setcookie definitely_not_a_cookie").

rsh_test(ResultTo) ->
    link(ResultTo),
    ?line {error, no_rsh} = slave:start(super, slave3).


%%% Utilities.    


wait_alive(Node) ->
    wait_alive_1(10, Node).

wait_alive_1(0, Node) ->
    ?t:fail({still_not_alive,Node});
wait_alive_1(N, Node) ->
    case rpc:call(Node, init, get_status, []) of
	{started,_} ->
	    ok;
	{starting,_} ->
	    receive after 1 -> ok end,
	    wait_alive_1(N-1, Node)
    end.

is_alive(Node) ->
    {started, _} = rpc:call(Node, init, get_status, []).

is_dead(Node) ->
    {badrpc, nodedown} = rpc:call(Node, init, get_status, []).

node_name(Host, Name) ->
    list_to_atom(lists:concat([Name, "@", Host])).

host() ->
    from($@, atom_to_list(node())).

from(H, [H | T]) -> T;
from(H, [_ | T]) -> from(H, T);
from(_H, []) -> [].

slave_start_link(Host, Name, Args) ->
    case slave:start_link(Host, Name, Args) of
	{ok, Node} ->
	    {ok, Node};
	Other ->
	    io:format("slave:start_link(~p, ~p, ~p) -> ~p",
		      [Host, Name, Args, Other])
    end.

fun_spawn(Fun) ->
    spawn_link(?MODULE, fun_init, [Fun]).

fun_init(Fun) ->
    Fun().