aboutsummaryrefslogtreecommitdiffstats
path: root/lib/common_test/test/ct_keep_logs_SUITE.erl
blob: f0c85a88a2eb4ddb61042be131050757608e3583 (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
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2009-2017. 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%
%%

%%%-------------------------------------------------------------------
%%% File: ct_keep_logs_SUITE
%%%
%%% Description:
%%% Test the 'keep_logs' option
%%%
%%%-------------------------------------------------------------------
-module(ct_keep_logs_SUITE).

-compile(export_all).

-include_lib("common_test/include/ct.hrl").
-include_lib("common_test/include/ct_event.hrl").

-define(eh, ct_test_support_eh).

%%--------------------------------------------------------------------
%% TEST SERVER CALLBACK FUNCTIONS
%%--------------------------------------------------------------------

init_per_suite(Config0) ->
    ct_test_support:init_per_suite(Config0).

end_per_suite(Config) ->
    ct_test_support:end_per_suite(Config).

init_per_testcase(TestCase, Config) ->
    ct_test_support:init_per_testcase(TestCase, Config).

end_per_testcase(TestCase, Config) ->
    ct_test_support:end_per_testcase(TestCase, Config).

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

all() ->
    [
     keep_logs,
     refresh_logs
    ].

%%--------------------------------------------------------------------
%% TEST CASES
%%--------------------------------------------------------------------

%% Test the keep_logs option with normal common_test runs
keep_logs(Config) ->
    DataDir = ?config(data_dir, Config),
    Suite = filename:join(DataDir, "keep_logs_SUITE"),
    Opts0 = ct_test_support:get_opts(Config),
    Opts = [{suite,Suite},{label,keep_logs} | Opts0],

    LogDir=?config(logdir,Opts),
    KeepLogsDir = filename:join(LogDir,unique_name("keep_logs-")),
    ok = file:make_dir(KeepLogsDir),
    Opts1 = lists:keyreplace(logdir,1,Opts,{logdir,KeepLogsDir}),
    ct:log("New LogDir = ~s", [KeepLogsDir]),

    %% Create 6 ct_run.* log directories
    [ok = ct_test_support:run(Opts1, Config) || _ <- lists:seq(1,3)],

    %% Verify the number of directories
    WC = filename:join(KeepLogsDir,"ct_run.ct@*"),
    L1 = filelib:wildcard(WC),
    6 = length(L1),

    %% Keep all logs
    {1,0,{0,0}}=ct_test_support:run_ct_run_test([{keep_logs,all}|Opts1], Config),
    L2 = filelib:wildcard(WC),
    7 = length(L2),
    0 = ct_test_support:run_ct_script_start([{keep_logs,all}|Opts1], Config),
    L3 = filelib:wildcard(WC),
    8 = length(L3),

    %% N<length of list
    {1,0,{0,0}}=ct_test_support:run_ct_run_test([{keep_logs,7}|Opts1], Config),
    L4 = filelib:wildcard(WC),
    7 = length(L4),
    0 = ct_test_support:run_ct_script_start([{keep_logs,6}|Opts1], Config),
    L5 = filelib:wildcard(WC),
    6 = length(L5),

    %% N>length of list
    {1,0,{0,0}}=ct_test_support:run_ct_run_test([{keep_logs,10}|Opts1], Config),
    L6 = filelib:wildcard(WC),
    7 = length(L6),
    0 = ct_test_support:run_ct_script_start([{keep_logs,10}|Opts1], Config),
    L7 = filelib:wildcard(WC),
    8 = length(L7),

    %% N==length of list
    {1,0,{0,0}}=ct_test_support:run_ct_run_test([{keep_logs,8}|Opts1], Config),
    L8 = filelib:wildcard(WC),
    8 = length(L8),
    0 = ct_test_support:run_ct_script_start([{keep_logs,8}|Opts1], Config),
    L9 = filelib:wildcard(WC),
    8 = length(L9),

    %% N==length of list + current run
    {1,0,{0,0}}=ct_test_support:run_ct_run_test([{keep_logs,9}|Opts1], Config),
    L10 = filelib:wildcard(WC),
    9 = length(L10),
    0 = ct_test_support:run_ct_script_start([{keep_logs,10}|Opts1], Config),
    L11 = filelib:wildcard(WC),
    10 = length(L11).

%% Test the keep_logs option togwther with the refresh_logs option
refresh_logs(Config) ->
    DataDir = ?config(data_dir, Config),
    Suite = filename:join(DataDir, "keep_logs_SUITE"),
    Opts0 = ct_test_support:get_opts(Config),
    LogDir=?config(logdir,Opts0),
    KeepLogsDir = filename:join(LogDir,unique_name("refresh_logs-")),
    ok = file:make_dir(KeepLogsDir),
    Opts1 = lists:keyreplace(logdir,1,Opts0,{logdir,KeepLogsDir}),
    ct:log("New LogDir = ~s", [KeepLogsDir]),

    %% Create 6 ct_run.* log directories
    SuiteOpts = [{suite,Suite},{label,refresh_logs} | Opts1],
    [ok = ct_test_support:run(SuiteOpts, Config) || _ <- lists:seq(1,3)],

    %% Verify the number of directories
    WC = filename:join(KeepLogsDir,"ct_run.ct@*"),
    L1 = filelib:wildcard(WC),
    6 = length(L1),

    RefreshOpts =  [{refresh_logs,KeepLogsDir},{label,refresh_logs} | Opts1],

    %% Keep all logs (note that refresh_logs option prevents the
    %% creation of a new log directory for the current run)
    done = ct_test_support:run_ct_run_test([{keep_logs,all}|RefreshOpts], Config),
    L2 = filelib:wildcard(WC),
    6 = length(L2),
    0 = ct_test_support:run_ct_script_start([{keep_logs,all}|RefreshOpts],Config),
    L3 = filelib:wildcard(WC),
    6 = length(L3),

    %% N<length of list
    done = ct_test_support:run_ct_run_test([{keep_logs,5}|RefreshOpts], Config),
    L5 = filelib:wildcard(WC),
    5 = length(L5),
    0 = ct_test_support:run_ct_script_start([{keep_logs,4}|RefreshOpts], Config),
    L6 = filelib:wildcard(WC),
    4 = length(L6),

    %% N>length of list
    done = ct_test_support:run_ct_run_test([{keep_logs,5}|RefreshOpts], Config),
    L7 = filelib:wildcard(WC),
    4 = length(L7),
    0 = ct_test_support:run_ct_script_start([{keep_logs,5}|RefreshOpts], Config),
    L8 = filelib:wildcard(WC),
    4 = length(L8),

    %% N==length of list
    done = ct_test_support:run_ct_run_test([{keep_logs,4}|RefreshOpts], Config),
    L9 = filelib:wildcard(WC),
    4 = length(L9),
    0 = ct_test_support:run_ct_script_start([{keep_logs,4}|RefreshOpts], Config),
    L10 = filelib:wildcard(WC),
    4 = length(L10),

    ok.
%%%-----------------------------------------------------------------
%%% Internal
unique_name(Prefix) ->
    I = erlang:unique_integer([positive]),
    Prefix ++ integer_to_list(I).