aboutsummaryrefslogtreecommitdiffstats
path: root/lib/megaco/test/megaco_examples_test.erl
blob: 3d2d79f4139150b4de348fc0e5555783421accc4 (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
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2001-2019. All Rights Reserved.
%% 
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%% 
%% %CopyrightEnd%
%%

%%
%%----------------------------------------------------------------------
%% Purpose: Test application config
%%----------------------------------------------------------------------

-module(megaco_examples_test).

-compile(export_all).

-include("megaco_test_lib.hrl").
-include_lib("megaco/include/megaco.hrl").
-include_lib("megaco/include/megaco_message_v1.hrl").

t()     -> megaco_test_lib:t(?MODULE).
t(Case) -> megaco_test_lib:t({?MODULE, Case}).

%% Test server callbacks
init_per_testcase(Case, Config) ->
    put(dbg,true),
    purge_examples(),
    load_examples(),
    megaco:enable_trace(max, io),
    megaco_test_lib:init_per_testcase(Case, Config).

end_per_testcase(Case, Config) ->
    purge_examples(),
    erase(dbg),
    megaco:disable_trace(),
    megaco_test_lib:end_per_testcase(Case, Config).

example_modules() ->
    [megaco_simple_mg, megaco_simple_mgc].

load_examples() ->
    case code:lib_dir(megaco) of
	{error, Reason} ->
	    {error, Reason};
	Dir ->
	    [code:load_abs(filename:join([Dir, examples, simple, M])) || 
                M <- example_modules()]
    end.

purge_examples() ->
    case code:lib_dir(megaco) of
	{error, Reason} ->
	    {error, Reason};
	_Dir ->
	    [code:purge(M) || M <- example_modules()]
    end.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Top test case

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
all() -> 
    [simple].

groups() -> 
    [].

init_per_group(_GroupName, Config) ->
    Config.

end_per_group(_GroupName, Config) ->
    Config.


simple(suite) ->
    [];
simple(Config) when is_list(Config) ->
    ?ACQUIRE_NODES(1, Config),
    d("simple -> proxy start"),
    ProxyPid = megaco_test_lib:proxy_start({?MODULE, ?LINE}),

    d("simple -> start megaco"),
    ?VERIFY(ok, megaco:start()),
    
    start_mgc(ProxyPid),

    ?SLEEP(1000), % This is just to make it easier to read the test logs

    start_mg(ProxyPid),
    
    ?SLEEP(1000), % This is just to make it easier to read the test logs

    d("simple -> verify info()"),
    info(),

    d("simple -> verify system_info(users)"),
    users(),

    d("simple -> stop mgc"),
    ?VERIFY(5, length(megaco_simple_mgc:stop())),
    ?SLEEP(1000), % This is just to make it easier to read the test logs

    d("simple -> verify system_info(users)"),
    users(),

    d("simple -> stop megaco"),
    ?VERIFY(ok, megaco:stop()),
    ?SLEEP(1000), % This is just to make it easier to read the test logs

    d("simple -> kill (exit) ProxyPid: ~p", [ProxyPid]),
    exit(ProxyPid, shutdown), % Controlled kill of transport supervisors

    ok.


start_mgc(ProxyPid) ->
    d("start_mgc -> start"),
    ?APPLY(ProxyPid, fun() -> megaco_simple_mgc:start() end),
    receive
	{res, _, {ok, MgcAll}} when is_list(MgcAll) ->
            ?SLEEP(1000), % This is just to make it easier to read the test logs
            d("start_mgc -> received MGC response: "
              "~n   ~p", [MgcAll]),
	    MgcBad = [MgcRes || MgcRes <- MgcAll, element(1, MgcRes) /= ok],
	    ?VERIFY([], MgcBad),
            ok;
        Error ->
            ?ERROR(Error)
    end.

start_mg(ProxyPid) ->
    d("start_mg -> start"),
    ?APPLY(ProxyPid, fun() -> megaco_simple_mg:start() end),
    receive
        {res, _, MgList} when is_list(MgList) andalso (length(MgList) =:= 4) ->
            ?SLEEP(1000), % This is just to make it easier to read the test logs
            d("start_mg -> received MG response: "
              "~n   ~p", [MgList]),
            Verify = 
                fun({_MgMid, {TransId, Res}}) when TransId =:= 1 ->
                        case Res of
                            {ok, [AR]} when is_record(AR, 'ActionReply') ->
                                case AR#'ActionReply'.commandReply of
                                    [{serviceChangeReply, SCR}] ->
                                        case SCR#'ServiceChangeReply'.serviceChangeResult of
                                            {serviceChangeResParms, MgcMid}
                                              when MgcMid =/= asn1_NOVALUE ->
                                                ok;
                                            Error ->
                                                d("start_mg -> "
                                                  "invalid service "
                                                  "change parms: "
                                                  "~n   ~p", [SCR]),
                                               ?ERROR(Error)
                                        end;
                                    Error ->
                                        d("start_mg -> "
                                          "invalid service change reply: "
                                          "~n   ~p", [Error]),
                                        ?ERROR(Error)
                                end;
                            Error ->
                                d("start_mg -> "
                                  "invalid result: "
                                  "~n   ~p", [Error]),
                                ?ERROR(Error)
                        end;
                   (Error) ->
                        d("start_mg -> "
                          "invalid reply: "
                          "~n   ~p", [Error]),
                        ?ERROR(Error)
                end,
            lists:map(Verify, MgList);
        Error -> 
            d("start_mg -> "
              "invalid result: "
              "~n   ~p", [Error]),
           ?ERROR(Error)
    end.    
                
info() ->
    case (catch megaco:info()) of
	{'EXIT', _} = Error ->
	    ?ERROR(Error);
	Info ->
	    ?LOG("Ok, ~p~n", [Info])
    end.

users() ->
    case (catch megaco:system_info(users)) of
	{'EXIT', _} = Error ->
	    ?ERROR(Error);
	Users ->
	    ?LOG("Ok, ~p~n", [Users])
    end.




d(F) ->
    d(F, []).
d(F, A) ->
    d(get(dbg), F, A).

d(true, F, A) ->
    io:format("DBG: ~s " ++ F ++ "~n", [?FT() | A]);
d(_, _F, _A) ->
    ok.