1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2010-2017. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20
21%%%-------------------------------------------------------------------
22%%% File: ct_config_SUITE
23%%%
24%%% Description:
25%%% Test configuration handling in Common Test suites.
26%%%
27%%% The suites used for the test are located in the data directory.
28%%%-------------------------------------------------------------------
29-module(ct_config_SUITE).
30
31-compile(export_all).
32
33-include_lib("common_test/include/ct.hrl").
34-include_lib("common_test/include/ct_event.hrl").
35
36-define(eh, ct_test_support_eh).
37
38%%--------------------------------------------------------------------
39%% TEST SERVER CALLBACK FUNCTIONS
40%%--------------------------------------------------------------------
41
42%%--------------------------------------------------------------------
43%% Description: Since Common Test starts another Test Server
44%% instance, the tests need to be performed on a separate node (or
45%% there will be clashes with logging processes etc).
46%%--------------------------------------------------------------------
47init_per_suite(Config) ->
48    DataDir = ?config(data_dir, Config),
49    PathDir = filename:join(DataDir, "config/test"),
50    Config1 = ct_test_support:init_per_suite([{path_dirs,[PathDir]} | Config]),
51    PrivDir = ?config(priv_dir, Config1),
52    ConfigDir = filename:join(PrivDir, "config"),
53    ok = file:make_dir(ConfigDir),
54    [{config_dir,ConfigDir} | Config1].
55
56end_per_suite(Config) ->
57    ct_test_support:end_per_suite(Config).
58
59init_per_testcase(TestCase, Config) ->
60    ct_test_support:init_per_testcase(TestCase, Config).
61
62end_per_testcase(install_config = TestCase, Config) ->
63    ok = rpc:call(proplists:get_value(ct_node, Config), ct_config, stop, []),
64    ct_test_support:end_per_testcase(TestCase, Config);
65end_per_testcase(TestCase, Config) ->
66    ct_test_support:end_per_testcase(TestCase, Config).
67
68suite() -> [{ct_hooks,[ts_install_cth]}].
69
70all() ->
71    [require, install_config, userconfig_static,
72     userconfig_dynamic, testspec_legacy, testspec_static,
73     testspec_dynamic].
74
75groups() ->
76    [].
77
78init_per_group(_GroupName, Config) ->
79	Config.
80
81end_per_group(_GroupName, Config) ->
82	Config.
83
84
85%%--------------------------------------------------------------------
86%% TEST CASES
87%%--------------------------------------------------------------------
88require(Config) when is_list(Config) ->
89    DataDir = ?config(data_dir, Config),
90    run_test(config_static_SUITE,
91	     Config,
92	     [{config, [filename:join(DataDir, "config/shadow.txt"),
93			filename:join(DataDir, "config/config.txt")]}],
94             ["config_static_SUITE"]).
95
96install_config(Config) when is_list(Config) ->
97    DataDir = ?config(data_dir, Config),
98    CTNode = proplists:get_value(ct_node, Config),
99    rpc:call(CTNode, ct, install,
100	     [[{config, [filename:join(DataDir, "config/config.txt")]}]]),
101    case rpc:call(CTNode, ct_config, start, [interactive]) of
102	Pid when is_pid(Pid) ->
103	    ok
104    end.
105
106
107userconfig_static(Config) when is_list(Config) ->
108    DataDir = ?config(data_dir, Config),
109    run_test(config_static_SUITE,
110	     Config,
111	     [{userconfig, {ct_config_xml, filename:join(DataDir, "config/config.xml")}},
112	      {config, filename:join(DataDir, "config/shadow.txt")}],
113             ["config_static_SUITE"]).
114
115userconfig_dynamic(Config) when is_list(Config) ->
116    case skip_dynamic() of
117	true -> {skip,"TimeWarpingOS"};
118	false ->
119	    run_test(config_dynamic_SUITE,
120		     Config,
121		     {userconfig, {config_driver, "config_server"}},
122		     ["config_dynamic_SUITE"])
123    end.
124
125testspec_legacy(Config) when is_list(Config) ->
126    DataDir = ?config(data_dir, Config),
127    ConfigDir = ?config(config_dir, Config),
128    make_spec(DataDir, ConfigDir,
129	      "spec_legacy.spec",
130	      [config_static_SUITE],
131	      [{config, filename:join(DataDir, "config/shadow.txt")},
132	       {config, filename:join(DataDir, "config/config.txt")}]),
133    run_test(config_static_SUITE,
134	     Config,
135	     {spec, filename:join(ConfigDir, "spec_legacy.spec")},
136             []),
137    file:delete(filename:join(ConfigDir, "spec_legacy.spec")).
138
139testspec_static(Config) when is_list(Config) ->
140    DataDir = ?config(data_dir, Config),
141    ConfigDir = ?config(config_dir, Config),
142    make_spec(DataDir, ConfigDir,
143	      "spec_static.spec",
144	      [config_static_SUITE],
145	      [{userconfig, {ct_config_xml, filename:join(DataDir, "config/config.xml")}},
146	       {config, filename:join(DataDir, "config/shadow.txt")}]),
147    run_test(config_static_SUITE,
148	     Config,
149	     {spec, filename:join(ConfigDir, "spec_static.spec")},
150             []),
151    file:delete(filename:join(ConfigDir, "spec_static.spec")).
152
153testspec_dynamic(Config) when is_list(Config) ->
154    case skip_dynamic() of
155	true -> {skip,"TimeWarpingOS"};
156	false ->
157	    DataDir = ?config(data_dir, Config),
158	    ConfigDir = ?config(config_dir, Config),
159	    make_spec(DataDir, ConfigDir, "spec_dynamic.spec",
160		      [config_dynamic_SUITE],
161		      [{userconfig, {config_driver, "config_server"}}]),
162	    run_test(config_dynamic_SUITE,
163		     Config,
164		     {spec, filename:join(ConfigDir, "spec_dynamic.spec")},
165		     []),
166	    file:delete(filename:join(ConfigDir, "spec_dynamic.spec"))
167    end.
168
169
170
171%%%-----------------------------------------------------------------
172%%% HELP FUNCTIONS
173%%%-----------------------------------------------------------------
174make_spec(DataDir, ConfigDir, Filename, Suites, Config)->
175    {ok, Fd} = file:open(filename:join(ConfigDir, Filename),
176                         [write, {encoding,utf8}]),
177    ok = io:format(Fd,"{suites, \"~tsconfig/test/\", ~p}.~n", [DataDir, Suites]),
178    lists:foreach(fun(C)-> ok=io:format(Fd, "~tp.~n", [C]) end, Config),
179    ok = file:close(Fd).
180
181run_test(Name, Config, CTConfig, SuiteNames)->
182    DataDir = ?config(data_dir, Config),
183    Joiner = fun(Suite) -> filename:join(DataDir, "config/test/"++Suite) end,
184    Suites = lists:map(Joiner, SuiteNames),
185    {Opts,ERPid} = setup_env({suite,Suites}, Config, CTConfig),
186
187    ok = ct_test_support:run(Opts, Config),
188    TestEvents = ct_test_support:get_events(ERPid, Config),
189    ct_test_support:log_events(Name,
190			       reformat_events(TestEvents, ?eh),
191			       ?config(config_dir, Config),
192			       Opts),
193    ExpEvents = events_to_check(Name),
194    ok = ct_test_support:verify_events(ExpEvents, TestEvents, Config).
195
196setup_env(Test, Config, CTConfig) when is_list(CTConfig) ->
197    Opts0 = ct_test_support:get_opts(Config),
198    Level = ?config(trace_level, Config),
199    EvHArgs = [{cbm,ct_test_support},{trace_level,Level}],
200    Opts = Opts0 ++ [Test,{event_handler,{?eh,EvHArgs}} | CTConfig],
201    ERPid = ct_test_support:start_event_receiver(Config),
202    {Opts,ERPid};
203setup_env(Test, Config, CTConfig) ->
204    setup_env(Test, Config, [CTConfig]).
205
206reformat_events(Events, EH) ->
207    ct_test_support:reformat(Events, EH).
208
209
210%%%-----------------------------------------------------------------
211%%% Test related to 'localtime' will often fail if the test host is
212%%% time warping, so let's just skip the 'dynamic' tests then.
213skip_dynamic() ->
214    case os:getenv("TS_EXTRA_PLATFORM_LABEL") of
215	TSExtraPlatformLabel when is_list(TSExtraPlatformLabel) ->
216	    case string:find(TSExtraPlatformLabel,"TimeWarpingOS") of
217		nomatch -> false;
218		_ -> true
219	    end;
220	_ ->
221	    false
222    end.
223
224
225
226%%%-----------------------------------------------------------------
227%%% TEST EVENTS
228%%%-----------------------------------------------------------------
229events_to_check(Test) ->
230    %% 2 tests (ct:run_test + script_start) is default
231    events_to_check(Test, 2).
232
233events_to_check(_, 0) ->
234    [];
235events_to_check(Test, N) ->
236    expected_events(Test) ++ events_to_check(Test, N-1).
237
238-define(ok(Name,Suite,Stat),{?eh,tc_start,{Suite,Name}},
239	{?eh,tc_done,{Suite,Name,ok}},
240	{?eh,test_stats,Stat}).
241-define(nok(Name,Suite,Reason,Stat),{?eh,tc_start,{Suite,Name}},
242     {?eh,tc_done,{Suite,Name,Reason}},
243     {?eh,test_stats,Stat}).
244
245-define(sok(Name,Stat),?ok(Name,config_static_SUITE,Stat)).
246-define(snok(Name,Reason,Stat),?nok(Name,config_static_SUITE,Reason,Stat)).
247
248-define(dok(Name,Stat),?ok(Name,config_dynamic_SUITE,Stat)).
249-define(dnok(Name,Reason,Stat),?nok(Name,config_dynamic_SUITE,Reason,Stat)).
250
251expected_events(config_static_SUITE)->
252    [
253     {?eh,start_logging,{'DEF','RUNDIR'}},
254     {?eh,test_start,{'DEF',{'START_TIME','LOGDIR'}}},
255     {?eh,start_info,{1,1,'_'}},
256     {?eh,tc_start,{config_static_SUITE,init_per_suite}},
257     {?eh,tc_done,{config_static_SUITE,init_per_suite,ok}},
258     ?sok(test_get_config_simple,{1,0,{0,0}}),
259     ?sok(test_get_config_nested,{2,0,{0,0}}),
260     ?sok(test_get_config_deep_nested,{3,0,{0,0}}),
261     ?sok(test_default_suitewide,{4,0,{0,0}}),
262     ?snok(test_config_name_already_in_use1,
263	   {failed,{error,{config_name_already_in_use,[x1]}}},{4,1,{0,0}}),
264     ?sok(test_default_tclocal,{5,1,{0,0}}),
265     ?snok(test_config_name_already_in_use2,
266	   {failed,{error,{config_name_already_in_use,[alias,x1]}}},{5,2,{0,0}}),
267     ?sok(test_alias_tclocal,{6,2,{0,0}}),
268     ?sok(test_get_config_undefined,{7,2,{0,0}}),
269     ?sok(test_require_subvals,{8,2,{0,0}}),
270     ?snok(test_require_subvals2,
271	   {auto_skipped,{require_failed,
272			  {not_available,{gen_cfg,[a,b,c,d]}}}},{8,2,{0,1}}),
273     ?sok(test_require_deep_config,{9,2,{0,1}}),
274     ?sok(test_shadow_all,{10,2,{0,1}}),
275     ?sok(test_element,{11,2,{0,1}}),
276     ?sok(test_shadow_all_element,{12,2,{0,1}}),
277     ?sok(test_internal_deep,{13,2,{0,1}}),
278     ?sok(test_alias_tclocal_nested,{14,2,{0,1}}),
279     ?sok(test_alias_tclocal_nested_backward_compat,{15,2,{0,1}}),
280     ?sok(test_alias_tclocal_nested_backward_compat_subvals,{16,2,{0,1}}),
281     ?sok(test_config_same_name_already_in_use,{17,2,{0,1}}),
282     {?eh,tc_start,{config_static_SUITE,end_per_suite}},
283     {?eh,tc_done,{config_static_SUITE,end_per_suite,ok}},
284     {?eh,test_done,{'DEF','STOP_TIME'}},
285     {?eh,stop_logging,[]}
286    ];
287
288expected_events(config_dynamic_SUITE)->
289    [
290     {?eh,start_logging,{'DEF','RUNDIR'}},
291     {?eh,test_start,{'DEF',{'START_TIME','LOGDIR'}}},
292     {?eh,start_info,{1,1,'_'}},
293     {?eh,tc_start,{config_dynamic_SUITE,init_per_suite}},
294     {?eh,tc_done,{config_dynamic_SUITE,init_per_suite,ok}},
295     ?dok(test_get_known_variable,{1,0,{0,0}}),
296     ?dok(test_localtime_update,{2,0,{0,0}}),
297     ?dok(test_server_pid,{3,0,{0,0}}),
298     ?dok(test_disappearable_variable,{4,0,{0,0}}),
299     ?dok(test_disappearable_variable_alias,{5,0,{0,0}}),
300     {?eh,tc_start,{config_dynamic_SUITE,end_per_suite}},
301     {?eh,tc_done,{config_dynamic_SUITE,end_per_suite,ok}},
302     {?eh,test_done,{'DEF','STOP_TIME'}},
303     {?eh,stop_logging,[]}
304    ].
305