4
%% Copyright Ericsson AB 2006-2011. All Rights Reserved.
6
%% The contents of this file are subject to the Erlang Public License,
7
%% Version 1.1, (the "License"); you may not use this file except in
8
%% compliance with the License. You should have received a copy of the
9
%% Erlang Public License along with this software. If not, it can be
10
%% retrieved online at http://www.erlang.org/.
12
%% Software distributed under the License is distributed on an "AS IS"
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
%% the License for the specific language governing rights and limitations
19
-module(os_sup_SUITE).
20
-include_lib("test_server/include/test_server.hrl").
22
%% Test server specific exports
23
-export([all/0, suite/0,groups/0,init_per_group/2,end_per_group/2]).
24
-export([init_per_suite/1, end_per_suite/1]).
25
-export([init_per_testcase/2, end_per_testcase/2]).
29
-export([config/1, port/1]).
31
%% Default timetrap timeout (set in init_per_testcase)
32
-define(default_timeout, ?t:minutes(1)).
34
-define(TAG, test_tag).
35
-define(MFA, {?MODULE, test_mfa, [?TAG]}).
37
-export([test_mfa/2]).
39
init_per_suite(Config) when is_list(Config) ->
40
spawn(fun() -> message_receptor() end),
41
?line application:load(os_mon),
42
?line ok = application:set_env(os_mon, start_os_sup, true),
43
?line ok = application:set_env(os_mon, os_sup_mfa, ?MFA),
44
?line ok = application:set_env(os_mon, os_sup_enable, false),
45
?line ok = application:start(os_mon),
48
end_per_suite(Config) when is_list(Config) ->
49
?line application:stop(os_mon),
50
?line ok = application:set_env(os_mon, start_os_sup, false),
51
MFA = {os_sup, error_report, [std_error]},
52
?line ok = application:set_env(os_mon, os_sup_mfa, MFA),
53
?line ok = application:set_env(os_mon, os_sup_enable, true),
54
?line exit(whereis(message_receptor), done),
57
init_per_testcase(_Case, Config) ->
58
Dog = ?t:timetrap(?default_timeout),
59
[{watchdog,Dog} | Config].
61
end_per_testcase(_Case, Config) ->
62
Dog = ?config(watchdog, Config),
63
?t:timetrap_cancel(Dog),
66
suite() -> [{ct_hooks,[ts_install_cth]}].
69
case test_server:os_type() of
70
{unix, sunos} -> [message, config, port];
71
{win32, _OSname} -> [message];
73
Str = io_lib:format("os_sup not available for ~p",
75
{skip, lists:flatten(Str)}
81
init_per_group(_GroupName, Config) ->
84
end_per_group(_GroupName, Config) ->
91
["Test OS message handling"];
92
message(Config) when is_list(Config) ->
95
Data = "10H11386278426HSystem4HTest5HError5HTesto",
96
?line os_sup_server ! {faked_port, {data, Data}},
98
%% Check with message_receptor that it has been received
99
?t:sleep(?t:seconds(1)),
105
{?TAG,{{1138,627842,0},"System","Test","Error","Testo"}}
107
?line message_receptor ! {check, self(), Msg},
112
?t:fail({no_message, Rec})
120
["Test configuration"];
121
config(Config) when is_list(Config) ->
123
%% os_sup_enable==true and os_sup_own/os_sup_syslogconf cannot
124
%% be tested as test_server is not running is root
126
%% os_sup_mfa is already tested, sort of (in init_per_suite)
128
%% os_sup_errortag should be tested, however
135
["Test that os_sup handles a terminating port program"];
136
port(Config) when is_list(Config) ->
137
?line Str = os:cmd("ps -e | grep '[f]errule'"),
138
case io_lib:fread("~s", Str) of
139
{ok, [Pid], _Rest} ->
141
%% Monitor os_sup_server
142
?line MonRef = erlang:monitor(process, os_sup_server),
144
%% Kill the port program
145
case os:cmd("kill -9 " ++ Pid) of
148
%% os_sup_server should now terminate
150
{'DOWN', MonRef, _, _, {port_died, _Reason}} ->
152
{'DOWN', MonRef, _, _, Reason} ->
153
?line ?t:fail({unexpected_exit_reason, Reason})
156
?line ?t:fail(still_alive)
159
%% Give os_mon_sup time to restart os_sup
160
?t:sleep(?t:seconds(3)),
161
?line true = is_pid(whereis(os_sup_server)),
166
erlang:demonitor(MonRef),
167
{skip, {not_killed, Line}}
170
{skip, {os_pid_not_found}}
173
%%----------------------------------------------------------------------
175
%%----------------------------------------------------------------------
177
test_mfa(Message, Tag) ->
178
message_receptor ! {Tag, Message}.
180
message_receptor() ->
181
register(message_receptor, self()),
182
message_receptor([]).
184
message_receptor(Received) ->
186
%% Check if a certain message has been received
187
{check, From, Msg} ->
188
case lists:member(Msg, Received) of
190
From ! {result, true},
191
message_receptor(lists:delete(Msg, Received));
193
From ! {result, Received},
194
message_receptor(Received)
197
%% Save all other messages
199
message_receptor([Msg|Received])