mvtest APIs

utils

mvtest library with general purpose utility APIs

class ExpectShell[source]

Provides a set of APIs for interactive shell. Has four main methods

  1. connect:
    Spawns an expect shell
  2. run_cmd:
    Combines both expect send and match.
  3. match_group:
    Returns the matched regex pattern as tuples.
  4. terminate:
    Terminate the spawned process.
connect(cmd, timeout=30)[source]
Returns:

pexpect.spwan object: on success.

False: on Failure

Args:

cmd (string): command to spawn.

timeout (int): default 30 seconds.

Examples:

EX1:

expect = ExpectShell()
expect.connect('telnet shark 2305', timeout=60)

Ex2:

expect = ExpectShell()
expect.connect('gdb a.out')
match_groups()[source]
Returns:
A tuple of matched groups.
Examples:
expect = ExpectShell()
expect.connect('telnet shark 2305', timeout=60)
index = expect.run_cmd('ls', pattern=['(*.py)', '*.sh'])
assert index == 1 or index == 0
log.info(expect.match_groups())
run_cmd(cmd, pattern, sleep=None, timeout=30)[source]
Returns:

Index into the pattern list: on success.

If the pattern was not a list this returns index 0 on a successful match.

None: on Failure

Args:

cmd (string): command to run.

pattern: StringType, or pexpect.EOF, a compiled re, or list.

sleep (int): Default is None. Will sleep after sending command.

timeout (int): default 30 seconds

Examples:
expect = ExpectShell()
expect.connect('telnet shark 2305', timeout=60)
index = expect.run_cmd('ls', pattern=['(*.py)', '*.sh'])
assert index == 1 or index == 0
terminate()[source]
Returns:

True: on success.

False: on failure.

This forces a child process to terminate. It starts nicely with SIGHUP and SIGINT. If force is True then moves onto SIGKILL.

class RemoteMachine(host, username=’root’, password=”)[source]
Returns:
None: on failure
Args:

host (string): hostname or ipaddress of remote host.

username (string): Default is root. If not root, specify username.

password (string): Default is NULL. If not, specify password.

file_get(src, dst)[source]

Transfer files from remote machine to local target.

Returns:

True: on success

False: on failure

Args:

src: Absolute path of the file on remote.

dst: Absolute path of the file on local.

Examples:
remote = RemoteMachine('bogart')
remote.file_get('/tmp/remote_file', '/tmp/local_file')
file_put(src, dst)[source]

Transfer files from local target to remote machine.

Returns::

True: on success.

False: on failure

Args:

src : Absolute path of the file on target.

dst : Absolute path of the file on remote

Examples:
remote = RemoteMachine('bogart')
remote.file_put('/tmp/local_file', '/tmp/remote_file')
run_cmd(cmd)[source]

Run a command in remote host.

Returns:

None: on failure (non-zero return code) OR

string: NullOutput or stripped command output on success.

Args:
cmd (string) : command to run on remote host.
Examples:
remote = RemoteMachine('bogart')
remote.run_cmd('uptime')

remote = RemoteMachine('192.168.10.2')
remote.run_cmd('uptime')
check_kernel_configs(args, logging=True)[source]

check if the given kernel config/s is enabled in the kernel.

Returns:
bool: On failure, missing kernel configs are listed as python list.
Args:

args (string or list): string for single kernel config OR a python list of kernel configs. See examples.

logging (bool): logs errors if True. By default is True.

Examples:
1. check_kernel_configs('PREEMPT_RT_FULL')
2. check_kernel_configs(['PREEMPT_RT_FULL', 'RELOCATABLE'])
3. check_kernel_configs(['PREEMPT_RT_FULL=n', 'RELOCATABLE=m'])
4. check_kernel_configs(['PREEMPT_RT_FULL=y', 'RELOCATABLE'])
get_nr_cpus()[source]

Return number of available online cpus.

Returns:
int: Number of online cpus from /proc/cpuinfo
get_online_cpus()[source]

Returns a list of online cpus from /sys/devices/system/cpu/online.

Returns:
list: cpu0 is not included as its expected be online.
get_status_output(cmd, wdir=None)[source]

Runs a command via the shell.

Returns:
tuple: (exit_code, text_output)
getip()[source]

Return local ip address

make_cpu_offline(cpus=None)[source]

Enable the list of cpus online.

Returns:
bool: True on success. False on failure.

Examples:

make_cpu_offline('5')
make_cpu_offline(['2', '5', '7'])
make_cpu_offline(5)
make_cpu_online(cpus=None, online=True)[source]

Enable the list of cpus online.

Returns:
bool: True on success. False on failure.

Examples:

make_cpu_online('5') # make cpu5 online
make_cpu_online(['2', '5', '7']) # make cpu2,5,7 online
make_cpu_online(5)
ping(ripaddr=None, count=None)[source]

Ping remote machine with N number of packets.

Args:

ripaddr (string): Remote IPv4 address.

count (string): Number of packets to be sent.

Returns:

bool: True on 0% packet loss or False on partial/100% loss. OR

Popen object: If count is not specified.

run_cmd(cmd, check_rc=True, wdir=None, stdout_arg=-1, stdin_arg=None, shell=True, background=False, **kwargs)[source]

Run a command, either interactively (default) or in background.

Returns:

None: on failure (non-zero return code) OR

string: NullOutput or stripped command output on success OR

Popen object: If background=True. Following methods & attributes could be used

p = run_cmd(cmd, background=True)
p.communicate()
p.returncode
Args:

cmd (string): command to run.

check_rc (bool): Default is True. False will not log command, output/ errors.

wdir (string): Run cmd from specificed directory.

background (bool): Default is False. True returns Popen object. Run command in background. For usage see example below.

stdout_arg: Default is subprocess.PIPE. Else File descriptor to output to a file.

stdin_arg: Default is None. Progam’s standard input.

shell (bool): Default is True. Should be True for piped commands. See Ex 2: to know how to send piped commands.

kwargs: optional arguments to be passed to Popen()

Examples:

Ex 1: To kill the parent and its child process and ensure no zombies are present.

p = run_cmd(cmd, background=True, preexec_fn=os.setpgrp).
do_something()
do_something_else()
os.kill(-p.pid, signal.SIGKILL)
    **OR**
run_cmd('killall cmd')

Ex 2: piped commands alternatively can be executed directly as

run_cmd('cat sample | wc -l', shell=True)
run_stress(cpu=None, mem=None, membytes=None, io=None, timeout=None)[source]

Impose cpu or memory or IO stress or all 3 at once in background.

Returns:
Popen: on success or False on failure.
Args:

cpu (int): Spawn N workers spinning on sqrt().

mem (int): Spawn N workers spinning on malloc/free.

io (int): Spawn N workers spinning on sync.

membytes (string): malloc B bytes per vm worker.

size can be B,K,M or G.

timeout (string): Timeout after N seconds.

time can be in s, m, h, d, y