summaryrefslogtreecommitdiff
path: root/lib/common_test/test/ct_config_SUITE_data/config/test/config_dynamic_SUITE.erl
blob: 8ee12a2e4d62b3c04b2c91748ec70188cd2a0a35 (plain)
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
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 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%
%%

%%%-------------------------------------------------------------------
%%% File: config_dynamic_SUITE
%%%
%%% Description:
%%% Test suite for common_test which tests the userconfig functionality
%%%-------------------------------------------------------------------
-module(config_dynamic_SUITE).

-compile(export_all).

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

%% This suite will be run with dynamic userconfig
%% test_driver.erl is compliant to ct_config_* callback
%% and test_server is simple server for getting runtime-changing data
%% which will return the list with the following variables:
%% localtime = the erlang:localtime() result in list [{date, Date}, {time, Time}]
%% node = erlang:node() - can be compared in the testcase
%% now = erlang:now() - easier to compare than localtime()
%% config_server_pid - pid of the config server, should NOT change!
%% config_server_vsn - .19
%% config_server_iteration - a number of iteration config_server's loop done
%% disappearable_variable - hereAmI - will be absent on even iterations

suite() ->
    [
     {timetrap, {seconds,10}}
    ].

init_per_suite(Config) ->
    Config.

end_per_suite(_) ->
    ok.

all() -> [test_get_known_variable, test_localtime_update,
	  test_server_pid, test_disappearable_variable,
	  test_disappearable_variable_alias].

init_per_testcase(_, Config) ->
    Config.

end_per_testcase(_, _) ->
    ok.

% test that usual config works
test_get_known_variable(_)->
    Node = erlang:node(),
    0.19 = ct:get_config(config_server_vsn),
    Node = ct:get_config(node),
    ok.

% localtime will be updated in 5 seconds, check that
test_localtime_update(_)->
    Seconds = 5,
    LT1 = ct:get_config(localtime),
    timer:sleep(Seconds*1000),
    LT2 = ct:reload_config(localtime),
    case is_diff_ok(LT1, LT2, Seconds) of
	{false, Actual, Exp}->
	    ct:fail(io_lib:format("Time difference ~p is not ok, expected ~p", [Actual, Exp]));
	true->
	    ok
    end.

% server pid should not change
test_server_pid()->
    [{require, cfvsn, config_server_vsn}].
test_server_pid(_)->
    Pid = ct:get_config(config_server_pid),
    Pid = ct:reload_config(config_server_pid),
    Vsn = ct:get_config(config_server_vsn),
    % aliases remain after config reloading
    Vsn = ct:get_config(cfvsn),
    ok.

% test that variables may disappear from the config_2_SUITE
test_disappearable_variable(_)->
    % ask CT for config_server_iteration variable
    Iter = ct:reload_config(config_server_iteration),
    % here we should reload this variable in case it's odd
    if Iter rem 2 == 1->
	Iter2 = ct:reload_config(config_server_iteration),
	Iter2 = Iter+1;
	true->ok
    end,
    % now disappearable_variable should be in place
    hereAmI = ct:get_config(disappearable_variable),
    % and now it should disappear
    undefined = ct:reload_config(disappearable_variable).

% alias of disappearable_variable should disappear too
test_disappearable_variable_alias(_)->
    % the same rules apply for this testcase as for previous one
    Iter = ct:reload_config(config_server_iteration),
    Iter2 = if
	Iter rem 2 == 1 ->
	    NewIter = ct:reload_config(config_server_iteration),
	    NewIter = Iter+1;
	true->
	    Iter
    end,
    ct:require(diav, disappearable_variable),
    hereAmI = ct:get_config(disappearable_variable),
    hereAmI = ct:get_config(diav),
    ct:reload_config(disappearable_variable),
    undefined = ct:get_config(disappearable_variable),
    % after reloading, it's even again
    Iter3=ct:get_config(config_server_iteration),
    Iter3 = Iter2+1,
    % and alias does not exist
    undefined = ct:get_config(diav).

my_dt_to_datetime([{date, D},{time, T}])->
    {D, T}.

is_diff_ok(DT1, DT2, Seconds)->
    GS1 = calendar:datetime_to_gregorian_seconds(my_dt_to_datetime(DT1)),
    GS2 = calendar:datetime_to_gregorian_seconds(my_dt_to_datetime(DT2)),
    if
	GS2-GS1 > Seconds+Seconds/2;
	GS2-GS1 < Seconds-Seconds/2->
	    {false, GS2-GS1, Seconds};
	true->
	    true
    end.