aboutsummaryrefslogtreecommitdiffstats
path: root/lib/os_mon/test/os_sup_SUITE.erl
blob: 873db06317c7341e97f8cef7b0e53983f22ed2e6 (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
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2006-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%
%%
-module(os_sup_SUITE).
-include_lib("test_server/include/test_server.hrl").

%% Test server specific exports
-export([all/0, suite/0,groups/0,init_per_group/2,end_per_group/2]).
-export([init_per_suite/1, end_per_suite/1]).
-export([init_per_testcase/2, end_per_testcase/2]).

%% Test cases
-export([message/1]).
-export([config/1, port/1]).

%% Default timetrap timeout (set in init_per_testcase)
-define(default_timeout, ?t:minutes(1)).

-define(TAG, test_tag).
-define(MFA, {?MODULE, test_mfa, [?TAG]}).

-export([test_mfa/2]).

init_per_suite(Config) when is_list(Config) ->
    spawn(fun() -> message_receptor() end),
    ?line application:load(os_mon),
    ?line ok = application:set_env(os_mon, start_os_sup, true),
    ?line ok = application:set_env(os_mon, os_sup_mfa, ?MFA),
    ?line ok = application:set_env(os_mon, os_sup_enable, false),
    ?line ok = application:start(os_mon),
    Config.

end_per_suite(Config) when is_list(Config) ->
    ?line application:stop(os_mon),
    ?line ok = application:set_env(os_mon, start_os_sup, false),
    MFA = {os_sup, error_report, [std_error]},
    ?line ok = application:set_env(os_mon, os_sup_mfa, MFA),
    ?line ok = application:set_env(os_mon, os_sup_enable, true),
    ?line exit(whereis(message_receptor), done),
    Config.

init_per_testcase(_Case, Config) ->
    Dog = ?t:timetrap(?default_timeout),
    [{watchdog,Dog} | Config].

end_per_testcase(_Case, Config) ->
    Dog = ?config(watchdog, Config),
    ?t:timetrap_cancel(Dog),
    ok.

suite() -> [{ct_hooks,[ts_install_cth]}].

all() -> 
    case test_server:os_type() of
	{unix, sunos} -> [message, config, port];
	{win32, _OSname} -> [message];
	OS ->
	    Str = io_lib:format("os_sup not available for ~p",
				[OS]),
	    {skip, lists:flatten(Str)}
    end.

groups() -> 
    [].

init_per_group(_GroupName, Config) ->
    Config.

end_per_group(_GroupName, Config) ->
    Config.


message(suite) ->
    [];
message(doc) ->
    ["Test OS message handling"];
message(Config) when is_list(Config) ->

    %% Fake an OS message
    Data = "10H11386278426HSystem4HTest5HError5HTesto",
    ?line os_sup_server ! {faked_port, {data, Data}},

    %% Check with message_receptor that it has been received
    ?t:sleep(?t:seconds(1)),
    Msg =
	case ?t:os_type() of
	    {unix, sunos} ->
		{?TAG, Data};
	    {win32, _} ->
		{?TAG,{{1138,627842,0},"System","Test","Error","Testo"}}
	end,
    ?line message_receptor ! {check, self(), Msg},
    receive
	{result, true} ->
	    ok;
	{result, Rec} ->
	    ?t:fail({no_message, Rec})
    end,

    ok.

config(suite) ->
    [];
config(doc) ->
    ["Test configuration"];
config(Config) when is_list(Config) ->

    %% os_sup_enable==true and os_sup_own/os_sup_syslogconf cannot
    %% be tested as test_server is not running is root

    %% os_sup_mfa is already tested, sort of (in init_per_suite)

    %% os_sup_errortag should be tested, however

    ok.

port(suite) ->
    [];
port(doc) ->
    ["Test that os_sup handles a terminating port program"];
port(Config) when is_list(Config) ->
    ?line Str = os:cmd("ps -e | grep '[f]errule'"),
    case io_lib:fread("~s", Str) of
	 {ok, [Pid], _Rest} ->

	    %% Monitor os_sup_server
	    ?line MonRef = erlang:monitor(process, os_sup_server),

	    %% Kill the port program
	    case os:cmd("kill -9 " ++ Pid) of
		[] ->

		    %% os_sup_server should now terminate
		    receive
			{'DOWN', MonRef, _, _, {port_died, _Reason}} ->
			    ok;
			{'DOWN', MonRef, _, _, Reason} ->
			    ?line ?t:fail({unexpected_exit_reason, Reason})
		    after
			3000 ->
			    ?line ?t:fail(still_alive)
		    end,

		    %% Give os_mon_sup time to restart os_sup
		    ?t:sleep(?t:seconds(3)),
		    ?line true = is_pid(whereis(os_sup_server)),

		    ok;

		Line ->
		    erlang:demonitor(MonRef),
		    {skip, {not_killed, Line}}
	    end;
	_ ->
	    {skip, {os_pid_not_found}}
    end.

%%----------------------------------------------------------------------
%% Auxiliary
%%----------------------------------------------------------------------

test_mfa(Message, Tag) ->
    message_receptor ! {Tag, Message}.

message_receptor() ->
    register(message_receptor, self()),
    message_receptor([]).

message_receptor(Received) ->
    receive
	%% Check if a certain message has been received
	{check, From, Msg} ->
	    case lists:member(Msg, Received) of
		true ->
		    From ! {result, true},
		    message_receptor(lists:delete(Msg, Received));
		false ->
		    From ! {result, Received},
		    message_receptor(Received)
	    end;

	%% Save all other messages
	Msg ->
	    message_receptor([Msg|Received])
    end.