User's Guide
Reference Manual
Release Notes
PDF
Top

Common Test
Reference Manual
Version 1.11.1

Expand All
Contract All

Table of Contents

- common_test
  - Top of manual page
  - Module:all/0
  - Module:groups/0
  - Module:suite/0
  - Module:init_per_suite/1
  - Module:end_per_suite/1
  - Module:group/1
  - Module:init_per_group/2
  - Module:end_per_group/2
  - Module:init_per_testcase/2
  - Module:end_per_testcase/2
  - Module:Testcase/0
  - Module:Testcase/1
- ct_run
- ct
  - Top of manual page
  - abort_current_testcase/1
  - add_config/2
  - break/1
  - break/2
  - capture_get/0
  - capture_get/1
  - capture_start/0
  - capture_stop/0
  - comment/1
  - comment/2
  - continue/0
  - continue/1
  - decrypt_config_file/2
  - decrypt_config_file/3
  - encrypt_config_file/2
  - encrypt_config_file/3
  - fail/1
  - fail/2
  - get_config/1
  - get_config/2
  - get_config/3
  - get_event_mgr_ref/0
  - get_status/0
  - get_target_name/1
  - get_testspec_terms/0
  - get_testspec_terms/1
  - get_timetrap_info/0
  - install/1
  - listenv/1
  - log/1
  - log/2
  - log/3
  - log/4
  - make_priv_dir/0
  - notify/2
  - pal/1
  - pal/2
  - pal/3
  - pal/4
  - parse_table/1
  - print/1
  - print/2
  - print/3
  - print/4
  - reload_config/1
  - remove_config/2
  - require/1
  - require/2
  - run/1
  - run/2
  - run/3
  - run_test/1
  - run_testspec/1
  - sleep/1
  - start_interactive/0
  - step/3
  - step/4
  - stop_interactive/0
  - sync_notify/2
  - testcases/2
  - timetrap/1
  - userdata/2
  - userdata/3
- ct_master
  - Top of manual page
  - abort/0
  - abort/1
  - basic_html/1
  - get_event_mgr_ref/0
  - progress/0
  - run/1
  - run/3
  - run/4
  - run_on_node/2
  - run_on_node/3
  - run_test/2
- ct_cover
  - Top of manual page
  - add_nodes/1
  - cross_cover_analyse/2
  - remove_nodes/1
- ct_ftp
  - Top of manual page
  - cd/2
  - close/1
  - delete/2
  - get/3
  - ls/2
  - open/1
  - put/3
  - recv/2
  - recv/3
  - send/2
  - send/3
  - type/2
- ct_ssh
  - Top of manual page
  - apread/4
  - apread/5
  - apwrite/4
  - apwrite/5
  - aread/3
  - aread/4
  - awrite/3
  - awrite/4
  - close/2
  - close/3
  - connect/1
  - connect/2
  - connect/3
  - del_dir/2
  - del_dir/3
  - delete/2
  - delete/3
  - disconnect/1
  - exec/2
  - exec/3
  - exec/4
  - get_file_info/2
  - get_file_info/3
  - list_dir/2
  - list_dir/3
  - make_dir/2
  - make_dir/3
  - make_symlink/3
  - make_symlink/4
  - open/3
  - open/4
  - opendir/2
  - opendir/3
  - position/3
  - position/4
  - pread/4
  - pread/5
  - pwrite/4
  - pwrite/5
  - read/3
  - read/4
  - read_file/2
  - read_file/3
  - read_file_info/2
  - read_file_info/3
  - read_link/2
  - read_link/3
  - read_link_info/2
  - read_link_info/3
  - receive_response/2
  - receive_response/3
  - receive_response/4
  - rename/3
  - rename/4
  - send/3
  - send/4
  - send/5
  - send_and_receive/3
  - send_and_receive/4
  - send_and_receive/5
  - send_and_receive/6
  - session_close/2
  - session_open/1
  - session_open/2
  - sftp_connect/1
  - subsystem/3
  - subsystem/4
  - write/3
  - write/4
  - write_file/3
  - write_file/4
  - write_file_info/3
  - write_file_info/4
- ct_netconfc
  - Top of manual page
  - action/2
  - action/3
  - close_session/1
  - close_session/2
  - copy_config/3
  - copy_config/4
  - create_subscription/1
  - create_subscription/2
  - create_subscription/3
  - create_subscription/4
  - create_subscription/5
  - create_subscription/6
  - delete_config/2
  - delete_config/3
  - edit_config/3
  - edit_config/4
  - edit_config/5
  - get/2
  - get/3
  - get_capabilities/1
  - get_capabilities/2
  - get_config/3
  - get_config/4
  - get_event_streams/2
  - get_event_streams/3
  - get_session_id/1
  - get_session_id/2
  - hello/1
  - hello/2
  - hello/3
  - kill_session/2
  - kill_session/3
  - lock/2
  - lock/3
  - only_open/1
  - only_open/2
  - open/1
  - open/2
  - send/2
  - send/3
  - send_rpc/2
  - send_rpc/3
  - unlock/2
  - unlock/3
- ct_rpc
  - Top of manual page
  - app_node/2
  - app_node/3
  - app_node/4
  - call/4
  - call/5
  - call/6
  - cast/4
  - cast/5
- ct_snmp
  - Top of manual page
  - get_next_values/3
  - get_values/3
  - load_mibs/1
  - register_agents/2
  - register_users/2
  - register_usm_users/2
  - set_info/1
  - set_values/4
  - start/2
  - start/3
  - stop/1
  - unload_mibs/1
  - unregister_agents/1
  - unregister_agents/2
  - unregister_users/1
  - unregister_users/2
  - unregister_usm_users/1
  - unregister_usm_users/2
- ct_telnet
  - Top of manual page
  - close/1
  - cmd/2
  - cmd/3
  - cmdf/3
  - cmdf/4
  - expect/2
  - expect/3
  - format_data/2
  - get_data/1
  - open/1
  - open/2
  - open/3
  - open/4
  - send/2
  - send/3
  - sendf/3
  - sendf/4
- unix_telnet
  - Top of manual page
  - connect/6
  - get_prompt_regexp/0
- ct_slave
  - Top of manual page
  - start/1
  - start/2
  - start/3
  - stop/1
  - stop/2
- ct_hooks
  - Top of manual page
  - Module:init/2
  - Module:pre_init_per_suite/3
  - Module:post_init_per_suite/4
  - Module:pre_init_per_group/3
  - Module:post_init_per_group/4
  - Module:pre_init_per_testcase/3
  - Module:post_end_per_testcase/4
  - Module:pre_end_per_group/3
  - Module:post_end_per_group/4
  - Module:pre_end_per_suite/3
  - Module:post_end_per_suite/4
  - Module:on_tc_fail/3
  - Module:on_tc_skip/3
  - Module:terminate/1
  - Module:id/1
- ct_property_test
  - Top of manual page
  - init_per_suite/1
  - quickcheck/2

# ct_telnet

### MODULE

ct_telnet

### MODULE SUMMARY

Common Test specific layer on top of telnet client ct_telnet_client.erl

### DESCRIPTION

Common Test specific layer on top of telnet client ct_telnet_client.erl

Use this module to set up telnet connections, send commands and perform string matching on the result. See the unix_telnet manual page for information about how to use ct_telnet, and configure connections, specifically for unix hosts.

The following default values are defined in ct_telnet:

      Connection timeout = 10 sec (time to wait for connection)
      Command timeout = 10 sec (time to wait for a command to return)
      Max no of reconnection attempts = 3
      Reconnection interval = 5 sek (time to wait in between reconnection attempts)
      Keep alive = true (will send NOP to the server every 8 sec if connection is idle)
      Polling limit = 0 (max number of times to poll to get a remaining string terminated)
      Polling interval = 1 sec (sleep time between polls)

These parameters can be altered by the user with the following configuration term:

      {telnet_settings, [{connect_timeout,Millisec},
                         {command_timeout,Millisec},
                         {reconnection_attempts,N},
                         {reconnection_interval,Millisec},
                         {keep_alive,Bool},
                         {poll_limit,N},
                         {poll_interval,Millisec}]}.

Millisec = integer(), N = integer()

Enter the telnet_settings term in a configuration file included in the test and ct_telnet will retrieve the information automatically. Note that keep_alive may be specified per connection if required. See unix_telnet for details.

Logging

The default logging behaviour of ct_telnet is to print information to the test case HTML log about performed operations and commands and their corresponding results. What won't be printed to the HTML log are text strings sent from the telnet server that are not explicitly received by means of a ct_telnet function such as expect/3. ct_telnet may however be configured to use a special purpose event handler, implemented in ct_conn_log_h, for logging all telnet traffic. To use this handler, you need to install a Common Test hook named cth_conn_log. Example (using the test suite info function):

      suite() ->
          [{ct_hooks, [{cth_conn_log, [{conn_mod(),hook_options()}]}]}].

conn_mod() is the name of the common_test module implementing the connection protocol, i.e. ct_telnet.

The cth_conn_log hook performs unformatted logging of telnet data to a separate text file. All telnet communication is captured and printed, including arbitrary data sent from the server. The link to this text file can be found on the top of the test case HTML log.

By default, data for all telnet connections is logged in one common file (named default), which might get messy e.g. if multiple telnet sessions are running in parallel. It is therefore possible to create a separate log file for each connection. To configure this, use the hook option hosts and list the names of the servers/connections that will be used in the suite. Note that the connections must be named for this to work (see the open function below).

The hook option named log_type may be used to change the cth_conn_log behaviour. The default value of this option is raw, which results in the behaviour described above. If the value is set to html, all telnet communication is printed to the test case HTML log instead.

All cth_conn_log hook options described above can also be specified in a configuration file with the configuration variable ct_conn_log. Example:

      {ct_conn_log, [{ct_telnet,[{log_type,raw},
                                 {hosts,[key_or_name()]}]}]}

Note that hook options specified in a configuration file will overwrite any hardcoded hook options in the test suite!

Logging example

The following ct_hooks statement will cause printing of telnet traffic to separate logs for the connections named server1 and server2. Traffic for any other connections will be logged in the default telnet log.

      suite() ->
          [{ct_hooks,
            [{cth_conn_log, [{ct_telnet,[{hosts,[server1,server2]}]}]}]}].

As previously explained, the above specification could also be provided by means of an entry like this in a configuration file:

      {ct_conn_log, [{ct_telnet,[{hosts,[server1,server2]}]}]}.

in which case the ct_hooks statement in the test suite may simply look like this:

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

### DATA TYPES

connection() = handle() | {target_name() (see module ct), connection_type()} | target_name() (see module ct)
connection_type() = telnet | ts1 | ts2
handle() = handle() (see module ct_gen_conn)

  Handle for a specific telnet connection.

prompt_regexp() = string()

  A regular expression which matches all possible prompts for a specific type of target. The regexp must not have any groups i.e. when matching, re:run/3 shall return a list with one single element.

### EXPORTS

close(Connection) -> ok | {error, Reason}

Types:

Connection = connection() (see module ct_telnet)
Reason = term()

Close the telnet connection and stop the process managing it.

A connection may be associated with a target name and/or a handle. If Connection has no associated target name, it may only be closed with the handle value (see the open/4 function).

cmd(Connection, Cmd) -> {ok, Data} | {error, Reason}

Equivalent to cmd(Connection, Cmd, []).

cmd(Connection, Cmd, Opts) -> {ok, Data} | {error, Reason}

Types:

Connection = connection() (see module ct_telnet)
Cmd = string()
Opts = [Opt]
Opt = {timeout, timeout()} | {newline, boolean()}
Data = [string()]
Reason = term()

Send a command via telnet and wait for prompt.

This function will by default add a newline to the end of the given command. If this is not desired, the option {newline,false} can be used. This is necessary, for example, when sending telnet command sequences (prefixed with the Interprete As Command, IAC, character).

The option timeout specifies how long the client shall wait for prompt. If the time expires, the function returns {error,timeout}. See the module description for information about the default value for the command timeout.

cmdf(Connection, CmdFormat, Args) -> {ok, Data} | {error, Reason}

Equivalent to cmdf(Connection, CmdFormat, Args, []).

cmdf(Connection, CmdFormat, Args, Opts) -> {ok, Data} | {error, Reason}

Types:

Connection = connection() (see module ct_telnet)
CmdFormat = string()
Args = list()
Opts = [Opt]
Opt = {timeout, timeout()} | {newline, boolean()}
Data = [string()]
Reason = term()

Send a telnet command and wait for prompt (uses a format string and list of arguments to build the command).

See cmd/3 further description.

expect(Connection, Patterns) -> term()

Equivalent to expect(Connections, Patterns, []).

expect(Connection, Patterns, Opts) -> {ok, Match} | {ok, MatchList, HaltReason} | {error, Reason}

Types:

Connection = connection() (see module ct_telnet)
Patterns = Pattern | [Pattern]
Pattern = string() | {Tag, string()} | prompt | {prompt, Prompt}
Prompt = string()
Tag = term()
Opts = [Opt]
Opt = {idle_timeout, IdleTimeout} | {total_timeout, TotalTimeout} | repeat | {repeat, N} | sequence | {halt, HaltPatterns} | ignore_prompt | no_prompt_check | wait_for_prompt | {wait_for_prompt, Prompt}
IdleTimeout = infinity | integer()
TotalTimeout = infinity | integer()
N = integer()
HaltPatterns = Patterns
MatchList = [Match]
Match = RxMatch | {Tag, RxMatch} | {prompt, Prompt}
RxMatch = [string()]
HaltReason = done | Match
Reason = timeout | {prompt, Prompt}

Get data from telnet and wait for the expected pattern.

Pattern can be a POSIX regular expression. The function returns as soon as a pattern has been successfully matched (at least one, in the case of multiple patterns).

RxMatch is a list of matched strings. It looks like this: [FullMatch, SubMatch1, SubMatch2, ...] where FullMatch is the string matched by the whole regular expression and SubMatchN is the string that matched subexpression no N. Subexpressions are denoted with '(' ')' in the regular expression

If a Tag is given, the returned Match will also include the matched Tag. Else, only RxMatch is returned.

The idle_timeout option indicates that the function shall return if the telnet client is idle (i.e. if no data is received) for more than IdleTimeout milliseconds. Default timeout is 10 seconds.

The total_timeout option sets a time limit for the complete expect operation. After TotalTimeout milliseconds, {error,timeout} is returned. The default value is infinity (i.e. no time limit).

The function will return when a prompt is received, even if no pattern has yet been matched. In this event, {error,{prompt,Prompt}} is returned. However, this behaviour may be modified with the ignore_prompt or no_prompt_check option, which tells expect to return only when a match is found or after a timeout.

If the ignore_prompt option is used, ct_telnet will ignore any prompt found. This option is useful if data sent by the server could include a pattern that would match the prompt regexp (as returned by TargedMod:get_prompt_regexp/0), but which should not cause the function to return.

If the no_prompt_check option is used, ct_telnet will not search for a prompt at all. This is useful if, for instance, the Pattern itself matches the prompt.

The wait_for_prompt option forces ct_telnet to wait until the prompt string has been received before returning (even if a pattern has already been matched). This is equal to calling: expect(Conn, Patterns++[{prompt,Prompt}], [sequence|Opts]). Note that idle_timeout and total_timeout may abort the operation of waiting for prompt.

The repeat option indicates that the pattern(s) shall be matched multiple times. If N is given, the pattern(s) will be matched N times, and the function will return with HaltReason = done.

The sequence option indicates that all patterns shall be matched in a sequence. A match will not be concluded untill all patterns are matched.

Both repeat and sequence can be interrupted by one or more HaltPatterns. When sequence or repeat is used, there will always be a MatchList returned, i.e. a list of Match instead of only one Match. There will also be a HaltReason returned.

Examples:
expect(Connection,[{abc,"ABC"},{xyz,"XYZ"}], [sequence,{halt,[{nnn,"NNN"}]}]).
will try to match "ABC" first and then "XYZ", but if "NNN" appears the function will return {error,{nnn,["NNN"]}}. If both "ABC" and "XYZ" are matched, the function will return {ok,[AbcMatch,XyzMatch]}.

expect(Connection,[{abc,"ABC"},{xyz,"XYZ"}], [{repeat,2},{halt,[{nnn,"NNN"}]}]).
will try to match "ABC" or "XYZ" twice. If "NNN" appears the function will return with HaltReason = {nnn,["NNN"]}.

The repeat and sequence options can be combined in order to match a sequence multiple times.

format_data(How, X2) -> term()

get_data(Connection) -> {ok, Data} | {error, Reason}

Types:

Connection = connection() (see module ct_telnet)
Data = [string()]
Reason = term()

Get all data that has been received by the telnet client since the last command was sent. Note that only newline terminated strings are returned. If the last string received has not yet been terminated, the connection may be polled automatically until the string is complete. The polling feature is controlled by the poll_limit and poll_interval config values and is by default disabled (meaning the function will immediately return all complete strings received and save a remaining non-terminated string for a later get_data call).

open(Name) -> {ok, Handle} | {error, Reason}

Equivalent to open(Name, telnet).

open(Name, ConnType) -> {ok, Handle} | {error, Reason}

Types:

Name = target_name()
ConnType = connection_type() (see module ct_telnet)
Handle = handle() (see module ct_telnet)
Reason = term()

Open a telnet connection to the specified target host.

open(KeyOrName, ConnType, TargetMod) -> {ok, Handle} | {error, Reason}

Equivalent to open(KeyOrName, ConnType, TargetMod, []).

open(KeyOrName, ConnType, TargetMod, Extra) -> {ok, Handle} | {error, Reason}

Types:

KeyOrName = Key | Name
Key = atom()
Name = target_name() (see module ct)
ConnType = connection_type()
TargetMod = atom()
Extra = term()
Handle = handle()
Reason = term()

Open a telnet connection to the specified target host.

The target data must exist in a configuration file. The connection may be associated with either Name and/or the returned Handle. To allocate a name for the target, use ct:require/2 in a test case, or use a require statement in the suite info function (suite/0), or in a test case info function. If you want the connection to be associated with Handle only (in case you need to open multiple connections to a host for example), simply use Key, the configuration variable name, to specify the target. Note that a connection that has no associated target name can only be closed with the handle value.

TargetMod is a module which exports the functions connect(Ip,Port,KeepAlive,Extra) and get_prompt_regexp() for the given TargetType (e.g. unix_telnet).

See also: ct:require/2.

send(Connection, Cmd) -> ok | {error, Reason}

Equivalent to send(Connection, Cmd, []).

send(Connection, Cmd, Opts) -> ok | {error, Reason}

Types:

Connection = connection() (see module ct_telnet)
Cmd = string()
Opts = [Opt]
Opt = {newline, boolean()}
Reason = term()

Send a telnet command and return immediately.

This function will by default add a newline to the end of the given command. If this is not desired, the option {newline,false} can be used. This is necessary, for example, when sending telnet command sequences (prefixed with the Interprete As Command, IAC, character).

The resulting output from the command can be read with get_data/1 or expect/2/3.

sendf(Connection, CmdFormat, Args) -> ok | {error, Reason}

Equivalent to sendf(Connection, CmdFormat, Args, []).

sendf(Connection, CmdFormat, Args, Opts) -> ok | {error, Reason}

Types:

Connection = connection() (see module ct_telnet)
CmdFormat = string()
Args = list()
Opts = [Opt]
Opt = {newline, boolean()}
Reason = term()

Send a telnet command and return immediately (uses a format string and a list of arguments to build the command).

### See also

unix_telnet