aboutsummaryrefslogtreecommitdiffstats
path: root/lib/test_server/src/ts_selftest.erl
blob: 655aa4bab3fcaed451f17c4515ec15768785dccd (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
%%
%% %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(ts_selftest).
-export([selftest/0]).

selftest() ->
    case node() of
	nonode@nohost ->
	    io:format("Sorry, you have to start this node distributed.~n"),
	    exit({error, node_not_distributed});
	_ ->
	    ok
    end,
    case catch ts:tests(test_server) of
	{'EXIT', _} ->
	    io:format("Test Server self test not availiable.");
	Other ->
	    selftest1()
    end.

selftest1() ->
    % Batch starts
    io:format("Selftest #1: Whole spec, batch mode:~n"),
    io:format("------------------------------------~n"),
    ts:run(test_server, [batch]),
    ok=check_result(1, "test_server.logs", 2),

    io:format("Selftest #2: One module, batch mode:~n"),
    io:format("------------------------------------~n"),
    ts:run(test_server, test_server_SUITE, [batch]),
    ok=check_result(2, "test_server_SUITE.logs", 2),

    io:format("Selftest #3: One testcase, batch mode:~n"),
    io:format("--------------------------------------~n"),
    ts:run(test_server, test_server_SUITE, msgs, [batch]),
    ok=check_result(3, "test_server_SUITE.logs", 0),

    % Interactive starts
    io:format("Selftest #4: Whole spec, interactive mode:~n"),
    io:format("------------------------------------------~n"),
    ts:run(test_server),
    kill_test_server(),
    ok=check_result(4, "test_server.logs", 2),

    io:format("Selftest #5: One module, interactive mode:~n"),
    io:format("------------------------------------------~n"),
    ts:run(test_server, test_server_SUITE),
    kill_test_server(),
    ok=check_result(5, "test_server_SUITE.logs", 2),

    io:format("Selftest #6: One testcase, interactive mode:~n"),
    io:format("--------------------------------------------~n"),
    ts:run(test_server, test_server_SUITE, msgs),
    kill_test_server(),
    ok=check_result(6, "test_server_SUITE.logs", 0),
    
    ok.

check_result(Test, TDir, ExpSkip) ->
    Dir=ts_lib:last_test(TDir),
    {Total, Failed, Skipped}=ts_reports:count_cases(Dir),
		io:format("Selftest #~p:",[Test]),
    case {Total, Failed, Skipped} of
	{_, 0, ExpSkip} ->        % 2 test cases should be skipped.
	    io:format("All ok.~n~n"),
	    ok;
	{_, _, _} ->
	    io:format("Not completely successful.~n~n"),
	    error
    end.


%% Wait for test server to get started.
kill_test_server() ->
    Node=list_to_atom("test_server@"++atom_to_list(hostname())),
    net_adm:ping(Node),
    case whereis(test_server_ctrl) of
	undefined ->
	    kill_test_server();
	Pid ->
	    kill_test_server(0, Pid)
    end.

%% Wait for test server to finish.
kill_test_server(30, Pid) ->
    exit(self(), test_server_is_dead);
kill_test_server(Num, Pid) ->
    case whereis(test_server_ctrl) of
	undefined ->
	    slave:stop(node(Pid));
	Pid ->
	    receive
	    after
		1000 ->
		    kill_test_server(Num+1, Pid)
	    end
    end.


hostname() ->
    list_to_atom(from($@, atom_to_list(node()))).
from(H, [H | T]) -> T;
from(H, [_ | T]) -> from(H, T);
from(H, []) -> [].