The Common Test framework uses configuration files to describe data related to a test and/or an SUT (System Under Test). The configuration data makes it possible to change properties without changing the test program itself. Configuration data can for example be:
A configuration file can contain any number of elements of the type:
{Key,Value}.
where
Key = atom() Value = term() | [{Key,Value}]
In a test suite, one must require that a configuration variable exists before attempting to read the associated value in a test case.
A
To read the value of a config variable, use the function
Example:
suite() -> [{require, domain, 'CONN_SPEC_DNS_SUFFIX'}]. ... testcase(Config) -> Domain = ct:get_config(domain), ...
If a configuration variable is defined in multiple files and you
want to access all possible values, you may use the
It is possible to encrypt configuration files containing sensitive data if these files must be stored in open and shared directories.
Call
Common Test also provides decryption functions,
Please see the
There are two different methods for opening a connection
by means of the support functions in e.g.
When a target name is used for referencing the configuration data (that specifies the connection to be opened), the same name may be used as connection identity in all subsequent calls related to the connection (also for closing it). It's only possible to have one open connection per target name. If attempting to open a new connection using a name already associated with an open connection, Common Test will return the already existing handle so that the previously opened connection will be used. This is a practical feature since it makes it possible to call the function for opening a particular connection whenever useful. An action like this will not necessarily open any new connections unless it's required (which could be the case if e.g. the previous connection has been closed unexpectedly by the server). Another benefit of using named connections is that it's not necessary to pass handle references around in the suite for these connections.
When a configuration variable name is used as reference to the data specifying the connection, the handle returned as a result of opening the connection must be used in all subsequent calls (also for closing the connection). Repeated calls to the open function with the same variable name as reference will result in multiple connections being opened. This can be useful e.g. if a test case needs to open multiple connections to the same server on the target node (using the same configuration data for each connection).
The nature of the configuration variables can be not only plain text files with the key-value tuples, they also can be loaded from the files in various formats, fetched via http from the Web, or can be loaded with help of some driver process. For this purpose, mechanism of plugging in user configuration handling callback modules is implemented in the Common Test.
Two callback modules for handling of configuration files are provided with the Common Test application:
This is an example of the XML configuration file:
"targethost" "tester" "letmein" "/test/loadmodules" ]]>
This configuration file, once read, will produce the same configuration variables as the following text file:
{ftp_host, [{ftp,"targethost"}, {username,"tester"}, {password,"letmein"}]}. {lm_directory, "/test/loadmodules"}.
Own handler can be written to handle special configuration file formats. The parameter can be either file name(s) or configuration string (empty list is valid).
The callback module is completely responsible for the configuration string correctness checks during runtime.
To perform validation of the configuration string, callback module should have the following function exported:
Input value will be passed from the Common Test, as defined in the test specification or given as an option to the run_test.
Return value should be any of the following value indicating if given configuration parameter is valid:
To perform actual reading, in cases of initial loading of the configuration variables and runtime re-loading, function
should be exported from the callback module
Input value is the same as for
Return value should be either:
Above, the
[{ftp_host, [{ftp, "targethost"}, {username, "tester"}, {password, "letmein"}]}, {lm_directory, "/test/loadmodules"}]
A config file for using the FTP client to access files on a remote host could look like this:
{ftp_host, [{ftp,"targethost"}, {username,"tester"}, {password,"letmein"}]}. {lm_directory, "/test/loadmodules"}.
XML version shown in chapter above can also be used, but it should be
explicitly specified that
Example of how to assert that the configuration data is available and use it for an FTP session:
init_per_testcase(ftptest, Config) -> {ok,_} = ct_ftp:open(ftp), Config. end_per_testcase(ftptest, _Config) -> ct_ftp:close(ftp). ftptest() -> [{require,ftp,ftp_host}, {require,lm_directory}]. ftptest(Config) -> Remote = filename:join(ct:get_config(lm_directory), "loadmodX"), Local = filename:join(?config(priv_dir,Config), "loadmodule"), ok = ct_ftp:recv(ftp, Remote, Local), ...
An example of how the above functions could be rewritten if necessary to open multiple connections to the FTP server:
init_per_testcase(ftptest, Config) -> {ok,Handle1} = ct_ftp:open(ftp_host), {ok,Handle2} = ct_ftp:open(ftp_host), [{ftp_handles,[Handle1,Handle2]} | Config]. end_per_testcase(ftptest, Config) -> lists:foreach(fun(Handle) -> ct_ftp:close(Handle) end, ?config(ftp_handles,Config)). ftptest() -> [{require,ftp_host}, {require,lm_directory}]. ftptest(Config) -> Remote = filename:join(ct:get_config(lm_directory), "loadmodX"), Local = filename:join(?config(priv_dir,Config), "loadmodule"), [Handle | MoreHandles] = ?config(ftp_handles,Config), ok = ct_ftp:recv(Handle, Remote, Local), ...
Simple configuration hanling driver which will ask external server for configuration data can be implemented this way:
-module(config_driver). -export([read_config/1, check_parameter/1]). read_config(ServerName)-> ServerModule = list_to_atom(ServerName), ServerModule:start(), ServerModule:get_config(). check_parameter(ServerName)-> ServerModule = list_to_atom(ServerName), case code:is_loaded(ServerModule) of {file, _}-> {ok, {config, ServerName}}; false-> case code:load_file(ServerModule) of {module, ServerModule}-> {ok, {config, ServerName}}; {error, nofile}-> {error, {wrong_config, "File not found: " ++ ServerName ++ ".beam"}} end end.
Configuration string for this driver may be "config_server", if the config_server.erl module below is built and is exist in the code path during test execution:
-module(config_server). -export([start/0, stop/0, init/1, get_config/0, loop/0]). -define(REGISTERED_NAME, ct_test_config_server). -define(vsn, 0.1). start()-> case whereis(?REGISTERED_NAME) of undefined-> spawn(?MODULE, init, [?REGISTERED_NAME]), wait(); _Pid-> ok end, ?REGISTERED_NAME. init(Name)-> register(Name, self()), loop(). get_config()-> call(self(), get_config). stop()-> call(self(), stop). call(Client, Request)-> case whereis(?REGISTERED_NAME) of undefined-> {error, not_started, Request}; Pid-> Pid ! {Client, Request}, receive Reply-> {ok, Reply} after 4000-> {error, timeout, Request} end end. loop()-> receive {Pid, stop}-> Pid ! ok; {Pid, get_config}-> {D,T} = erlang:localtime(), Pid ! [{localtime, [{date, D}, {time, T}]}, {node, erlang:node()}, {now, erlang:now()}, {config_server_pid, self()}, {config_server_vsn, ?vsn}], ?MODULE:loop() end. wait()-> case whereis(?REGISTERED_NAME) of undefined-> wait(); _Pid-> ok end.
There two modules provide the ability to dynamically reload configuration
variables. If the