pysys.process.user module¶
Contains the ProcessUser
class used by both BaseTest
and BaseRunner
to provide process-related capabilities including cleanup.
-
class
pysys.process.user.
ProcessUser
[source]¶ Bases:
object
Class providing basic operations over interacting with processes.
The ProcessUser class provides the minimum set of operations for managing and interacting with processes. The class is designed to be extended by the
pysys.baserunner.BaseRunner
andpysys.basetest.BaseTest
classes so that they prescribe a common set of process operations that any child test can use. Process operations have associated potential outcomes in their execution, e.g.BLOCKED
,TIMEDOUT
,DUMPEDCORE
etc. As such the class additionally acts as the container for storing the list of outcomes from all child test related actions.Apart from the
addOutcome
method this class is not thread-safe, so if you need to access it from multiple threads be sure to add your own locking around use of its fields and methods, including any cleanup functions.Variables: - input (string) – Location for input to any processes (defaults to current working directory)
- output (string) – Location for output from any processes (defaults to current working directory)
- disableCoverage – Set to True to disable all code coverage collection for processes started from this instance. For example, to disable coverage in tests tagged with the ‘performance’ group you could use a line like this in your BaseTest:: if ‘performance’ in self.descriptor.groups: self.disableCoverage = True The built-in Python code coverage functionality in
startPython
checks this flag. It is recommended that any other languages supporting code coverage also check the self.disableCoverage flag.
-
abort
(outcome, outcomeReason, callRecord=None)[source]¶ Raise an AbortException.
See also
skipTest
.Parameters: - outcome – The outcome, which will override any existing outcomes previously recorded.
- outcomeReason – A string summarizing the reason for the outcome
-
addCleanupFunction
(fn)[source]¶ Registers a zero-arg function that will be called as part of the cleanup of this object.
Cleanup functions are invoked in reverse order with the most recently added first (LIFO), and before the automatic termination of any remaining processes associated with this object.
e.g. self.addCleanupFunction(lambda: self.cleanlyShutdownProcessX(params))
-
addOutcome
(outcome, outcomeReason='', printReason=True, abortOnError=False, callRecord=None, override=False)[source]¶ Add a validation outcome (and optionally a reason string) to the validation list.
The method provides the ability to add a validation outcome to the internal data structure storing the list of validation outcomes. Multiple validations may be performed, the current supported validation outcomes of which are:
The outcomes are considered to have a precedence order, as defined by the order of the outcomes listed above. Thus a
BLOCKED
outcome has a higher precedence than aPASSED
outcome. The outcomes are defined inpysys.constants
.This method is thread-safe.
Parameters: - outcome – The outcome to add
- outcomeReason – A string summarizing the reason for the outcome, for example “Grep on x.log contains ‘ERROR: server failed’”.
- printReason – If True the specified outcomeReason will be printed
- abortOnError – If true abort the test on any error outcome. This should usually be set to False for assertions, or the configured self.defaultAbortOnError setting (typically True) for operations that involve waiting.
- callRecord – An array of strings indicating the call stack that lead to this outcome. This will be appended to the log output for better test triage.
- override – Remove any existing test outcomes when adding this one, ensuring that this outcome is the one and only one reported even if an existing outcome has higher precedence.
-
allocateUniqueStdOutErr
(processKey)[source]¶ Allocate unique filenames of the form processKey[.n].out/.err which can be used for the
startProcess
stdouterr
parameter.The first time this is called it will return names like (‘myprocess.out’, ‘myprocess.err’), the second time it will return (‘myprocess.1.out’, ‘myprocess.1.err’), then (‘myprocess.2.out’, ‘myprocess.2.err’) etc.
Parameters: processKey – A user-defined identifier that will form the prefix onto which [.n].out is appended Returns: A STDOUTERR_TUPLE named tuple of (stdout, stderr) Return type: tuple
-
cleanup
()[source]¶ Cleanup function that frees resources managed by this object.
Should be called exactly once when this object is no longer needed. Instead of overriding this function, use
addCleanupFunction
.
-
static
compareVersions
(v1, v2)[source]¶ Compares two alphanumeric dotted version strings to see which is more recent.
Example usage:
if self.compareVersions(thisversion, '1.2.alpha-3') > 0: ... # thisversion is newer than 1.2.alpha-3
The comparison algorithm ignores case, and normalizes separators ./-/_ so that ‘1.alpha2’==‘1Alpha2’. Any string components are compared lexicographically with other strings, and compared to numbers strings are always considered greater.
>>> ProcessUser.compareVersions('10-alpha5.dev10', '10alpha-5-dEv_10') == 0 # normalization of case and separators True
>>> ProcessUser.compareVersions(b'1....alpha.2', u'1Alpha2') == 0 # ascii byte and unicode strings both supported True
>>> ProcessUser.compareVersions('1.2.0', '1.2') 0
>>> ProcessUser.compareVersions('1.02', '1.2') 0
>>> ProcessUser().compareVersions('1.2.3', '1.2') > 0 True
>>> ProcessUser.compareVersions('1.2', '1.2.3') -1
>>> ProcessUser.compareVersions('10.2', '1.2') 1
>>> ProcessUser.compareVersions('1.2.text', '1.2.0') # letters are > numbers 1
>>> ProcessUser.compareVersions('1.2.text', '1.2') # letters are > numbers 1
>>> ProcessUser.compareVersions('10.2alpha1', '10.2alpha') 1
>>> ProcessUser.compareVersions('10.2dev', '10.2alpha') # letters are compared lexicographically 1
>>> ProcessUser.compareVersions('', '') 0
>>> ProcessUser.compareVersions('1', '') 1
Parameters: - v1 – A string containing a version number, with any number of components.
- v2 – A string containing a version number, with any number of components.
Returns: an integer > 0 if v1>v2, an integer < 0 if v1<v2, or 0 if they are semantically the same.
-
copy
(src, dest, mappers=[], encoding=None)[source]¶ Copy a single text or binary file, optionally tranforming the contents by filtering each line through a list of mapping functions.
If any mappers are provided, the file is copied in text mode and each mapper is given the chance to modify or omit each line. If no mappers are provided, the file is copied in binary mode.
In addition to the file contents the mode is also copied, for example the executable permission will be retained.
This function is useful both for creating a modified version of an output file that’s more suitable for later validation steps such as diff-ing, and also for copying required files from the input to the output directory.
For example:
self.copy('output-raw.txt', 'output-processed.txt', encoding='utf-8', mappers=[ lambda line: None if ('Timestamp: ' in line) else line, lambda line: line.replace('foo', 'bar'), ])
Parameters: - src – The source filename, which can be an absolute path, or a path relative to the self.output directory. Use src=self.input+’/myfile’ if you wish to copy a file from the test input directory.
- dest – The source filename, which can be an absolute path, or a path relative to the self.output directory. If this is a directory name, the file is copied to this directory with the same basename as src.
- mappers – A list of filter functions that will be applied, in order, to each line read from the file. Each function accepts a string for the current line as input and returns either a string to write or None if the line is to be omitted.
- encoding – The encoding to use to open the file. The default value is None which indicates that the decision will be delegated to the
getDefaultFileEncoding()
method.
-
createEnvirons
(overrides=None, addToLibPath=[], addToExePath=[], command=None, **kwargs)[source]¶ Create a new customized dictionary of environment variables suitable for passing to
startProcess()
’s environs argument.As a starting point, this method uses the value returned by
getDefaultEnvirons()
for this command. See the documentation on that method for more details. If you don’t care about minimizing the risk of your local environment affecting the test processes you start, just useenvirons=os.environ
to allow child processes to inherit the entire parent environment instead of using this method.Parameters: - overrides – A dictionary of environment variables whose values will be used instead of any existing values. You can use os.getenv(‘VARNAME’,’‘) if you need to pass selected variables from the current process as part of the overrides list. If the value is set to None then any variable of this name will be deleted. Use unicode strings if possible (byte strings will be converted depending on the platform). A list of dictionaries can be specified, in which case the latest will override the earlier if there are any conflicts.
- addToLibPath – A path or list of paths to be prepended to the default value for the environment variable used to load libraries (or the value specified in overrides, if any), i.e. [DY]LD_LIBRARY_PATH on Unix or PATH on Windows. This is usually more convenient than adding it directly to overrides.
- addToExePath – A path or list of paths to be prepended to the default value for the environment variable used to locate executables (or the value specified in overrides, if any), i.e. PATH on both Unix and Windows. This is usually more convenient than adding it directly to overrides.
- command – If known, the full path of the executable for which a default environment is being created (passed to
getDefaultEnvirons
). - kwargs – Overrides of this method should pass any additional kwargs down to the super implementation, to allow for future extensions.
Returns: A new dictionary containing the environment variables.
-
deleteDir
(path, **kwargs)[source]¶ Recursively delete the specified directory.
Does nothing if it does not exist. Raises an exception if the deletion fails.
Parameters: - path – The path to be deleted. This can be an absolute path or relative to the testcase output directory.
- kwargs – Any additional arguments are passed to
pysys.utils.fileutils.deletedir()
.
-
getBoolProperty
(propertyName, default=False)[source]¶ Get a True/False indicating whether the specified property is set on this object (typically as a result of specifying -X on the command line), or else from the project configuration.
Parameters: propertyName – The name of a property set on the command line or project configuration.
-
getDefaultEnvirons
(command=None, **kwargs)[source]¶ Create a new dictionary of environment variables, suitable for passing to
startProcess()
, with a minimal clean set of environment variables for this platform, unaffected (as much as possible) by the environment that the tests are being run under.This environment contains a minimal PATH/LD_LIBRARY_PATH but does not attempt to replicate the full set of default environment variables on each OS, and in particular it does not include any that identify the the current username or home area. Additional environment variables can be added as needed with
createEnvirons
overrides. If you don’t care about minimizing the risk of your local environment affecting the test processes you start, just useenvirons=os.environ
to allow child processes to inherit the entire parent environment.The
createEnvirons()
andstartProcess()
methods use this as the basis for creating a new set of default environment variables.If needed this method can be overridden in subclasses to add common environment variables for every process invoked by startProcess, for example to enable options such as code coverage for Java/Python/etc. This is also a good place to customize behaviour for different operating systems.
Some features of this method can be configured by setting project properties:
- defaultEnvironsDefaultLang: if set to a value such as en_US.UTF-8 the specified value is set for the LANG= variable on Unix; otherwise, the LANG variable is not set (which might result in use of the legacy POSIX/C encoding).
- defaultEnvironsTempDir: if set the expression will be passed to Python eval() and used to set the OS-specific temp directory environment variables. A typical value is self.output.
- defaultEnvironsLegacyMode: set to true to enable compatibility mode which keeps the behaviour the same as PySys v1.1, 1.2 and 1.3, namely using a completely empty default environment on Unix, and a copy of the entire parent environment on Windows. This is not recommended unless you have a lot of legacy tests that cannot easily be changed to only set minimal required environment variables using createEnvirons().
Parameters: - command – If known, the full path of the executable for which a default environment is being created (when called from startProcess this is always set). This allows default environment variables to be customized for different process types e.g. Java, Python, etc. When using command=sys.executable to launch another copy of the current Python executable, extra items from this process’s path environment variables are added to the returned dictionary so that it can start correctly. On Unix-based systems this includes copying all of the load library path environment variable from the parent process.
- kwargs – Overrides of this method should pass any additional kwargs down to the super implementation, to allow for future extensions.
Returns: A new dictionary containing the environment variables.
-
getDefaultFileEncoding
(file, **xargs)[source]¶ Specifies what encoding should be used to read or write the specified text file.
This method is used to select the appropriate encoding whenever PySys needs to open a file, for example to wait for a signal, for a file-based assertion, or to write a file with replacements. Many methods allow the encoding to be overridden for just that call, but getDefaultFileEncoding exists to allow global defaults to be specified based on the filename.
For example, this method could be overridden to specify that utf-8 encoding is to be used for opening filenames ending in .xml, .json and .yaml.
The default implementation of this method uses pysysproject.xml configuration rules such as:
<default-file-encoding pattern="\*.xml" encoding="utf-8"/>
A return value of None indicates default behaviour, which on Python 3 is to use the default encoding, as specified by python’s locale.getpreferredencoding(), and on Python 2 is to use binary “str” objects with no character encoding or decoding applied.
Parameters: - file – The filename to be read or written. This may be an absolute path or a relative path.
- xargs – Ensure that an **xargs argument is specified so that additional information can be passed to this method in future releases.
Returns: The encoding to use for this file, or None if default behaviour is to be used.
-
getExprFromFile
(path, expr, groups=[1], returnAll=False, returnNoneIfMissing=False, encoding=None)[source]¶ Searches for a regular expression in the specified file, and returns it.
If the regex contains groups, the specified group is returned. If the expression is not found, an exception is raised, unless getAll=True or returnNoneIfMissing=True. For example;
self.getExprFromFile(‘test.txt’, ‘myKey=”(.*)”’) on a file containing ‘myKey=”foobar”’ would return “foobar” self.getExprFromFile(‘test.txt’, ‘foo’) on a file containing ‘myKey=foobar’ would return “foo”
Parameters: - path – file to search (located in the output dir unless an absolute path is specified)
- expr – the regular expression, optionally containing the regex group operator (…)
- groups – which regex groups (as indicated by brackets in the regex) shoud be returned; default is [‘1’] meaning the first group. If more than one group is specified, the result will be a tuple of group values, otherwise the result will be the value of the group at the specified index.
- returnAll – returns all matching lines if True, the first matching line otherwise.
- returnNoneIfMissing – set this to return None instead of throwing an exception if the regex is not found in the file
- encoding – The encoding to use to open the file. The default value is None which indicates that the decision will be delegated to the
getDefaultFileEncoding()
method.
-
getInstanceCount
(displayName)[source]¶ (Deprecated) Return the number of processes started within the testcase matching the supplied displayName.
The ProcessUser class maintains a reference count of processes started within the class instance via the
startProcess()
method. The reference count is maintained against a logical name for the process, which is thedisplayName
used in the method call tostartProcess()
, or the basename of the command if no displayName was supplied. The method returns the number of processes started with the supplied logical name, or 0 if no processes have been started.Deprecated: The recommended way to allocate unique names is now allocateUniqueStdOutErr
Parameters: displayName – The process display name Returns: The number of processes started matching the command basename Return type: integer
-
getOutcome
()[source]¶ Get the overall outcome based on the precedence order.
The method returns the overall outcome of the test based on the outcomes stored in the internal data structure. The precedence order of the possible outcomes is used to determined the overall outcome of the test, e.g. if
PASSED
,BLOCKED
andFAILED
were recorded during the execution of the test, the overall outcome would beBLOCKED
.The method returns the integer value of the outcome as defined in
pysys.constants
. To convert this to a string representation use thepysys.constants.LOOKUP
dictionary i.e.LOOKUP[test.getOutcome()]
.Returns: The overall outcome Return type: integer
-
getOutcomeReason
()[source]¶ Get the reason string for the current overall outcome (if specified).
Returns: The overall test outcome reason or ‘’ if not specified Return type: string
-
lock
= None¶ A recursive lock that can be used for protecting the fields of this instance from access by background threads, as needed.
-
log
= None¶ The logger instance that should be used to log from this class.
-
logFileContents
(path, includes=None, excludes=None, maxLines=20, tail=False, encoding=None)[source]¶ Logs some or all of the lines in the specified file.
If the file does not exist or cannot be opened, does nothing. The method is useful for providing key diagnostic information (e.g. error messages from tools executed by the test) directly in run.log, or to make test failures easier to triage quickly.
Parameters: - path – May be an absolute, or relative to the test output directory
- includes – Optional list of regex strings. If specified, only matches of these regexes will be logged
- excludes – Optional list of regex strings. If specified, no line containing these will be logged
- maxLines – Upper limit on the number of lines from the file that will be logged. Set to zero for unlimited
- tail – Prints the _last_ ‘maxLines’ in the file rather than the first ‘maxLines’
- encoding – The encoding to use to open the file. The default value is None which indicates that the decision will be delegated to the
getDefaultFileEncoding()
method.
Returns: True if anything was logged, False if not
-
mkdir
(path)[source]¶ Create a directory, with recursive creation of any parent directories.
This function is a no-op (does not throw) if the directory already exists.
Parameters: path – The path to be created. This can be an absolute path or relative to the testcase output directory. Returns: the absolute path of the new directory, to facilitate fluent-style method calling.
-
project
= None¶ The
pysys.xml.project.Project
instance containing settings for this PySys project.
-
signalProcess
(process, signal, abortOnError=None)[source]¶ Send a signal to a running process (Unix only).
This method uses the
pysys.process.helper
module to send a signal to a running process. Should the request to send the signal to the running process fail, aBLOCKED
outcome will be added to the outcome list.Parameters: - process – The process handle returned from the
startProcess
method - signal – The integer value of the signal to send
- abortOnError – If true abort the test on any error outcome (defaults to the defaultAbortOnError project setting)
- process – The process handle returned from the
-
skipTest
(outcomeReason, callRecord=None)[source]¶ Raise an AbortException that will set the test outcome to SKIPPED and ensure that the rest of the execute() and validate() methods do not execute.
This is useful when a test should not be executed in the current mode or platform.
Parameters: outcomeReason – A string summarizing the reason the test is being skipped, for example “Feature X is not supported on Windows”.
-
startProcess
(command, arguments, environs=None, workingDir=None, state=11, timeout=600, stdout=None, stderr=None, displayName=None, abortOnError=None, expectedExitStatus='==0', ignoreExitStatus=None, quiet=False, stdouterr=None)[source]¶ Start a process running in the foreground or background, and return the
ProcessWrapper
process object.Typical use is:
myexecutable = self.startProcess('path_to_my_executable', arguments=['myoperation', 'arg1','arg2'], environs=self.createEnvirons(addToLibPath=['my_ld_lib_path']), # if a customized environment is needed stdouterr=self.allocateUniqueStdOutErr('myoperation'), # for stdout/err files, pick a suitable logical name for what it's doing state=BACKGROUND # or remove for default behaviour of executing in foreground )
The method allows spawning of new processes in a platform independent way. The command, arguments, environment and working directory to run the process in can all be specified in the arguments to the method, along with the filenames used for capturing the stdout and stderr of the process. Processes may be started in the
FOREGROUND
, in which case the method does not return until the process has completed or a time out occurs, or in theBACKGROUND
in which case the method returns immediately to the caller returning a handle to the process to allow manipulation at a later stage, typically withwaitProcess
. All processes started in theBACKGROUND
and not explicitly killed using the returned process object are automatically killed on completion of the test via thecleanup()
destructor.Parameters: - command – The path to the executable to be launched (should include the full path)
- arguments – A list of arguments to pass to the command
- environs – A dictionary specifying the environment to run the process in. If a None or empty dictionary is passed,
getDefaultEnvirons
will be invoked to produce a suitable clean default environment for this command, containing a minimal set of variables. If you wish to specify a customized environment,createEnvirons()
is a great way to create it. - workingDir – The working directory for the process to run in (defaults to the testcase output subdirectory)
- state – Run the process either in the
FOREGROUND
orBACKGROUND
(defaults toFOREGROUND
) - timeout – The timeout period after which to terminate processes running in the
FOREGROUND
. - stdouterr – The filename prefix to use for the stdout and stderr of the process (.out/.err will be appended), or a tuple of (stdout,stderr) as returned from
allocateUniqueStdOutErr
. The stdouterr prefix is also used to form a default display name for the process if none is explicitly provided. The files are created relative to the test output directory. The filenames can be accessed from the returned process object usingpysys.process.helper.CommonProcessWrapper.stdout
andpysys.process.helper.CommonProcessWrapper.stderr
. - stdout – The filename used to capture the stdout of the process. It is usually simpler to use stdouterr instead of this.
- stderr – The filename used to capture the stderr of the process. It is usually simpler to use stdouterr instead of this.
- displayName – Logical name of the process used for display (defaults to a string generated from the stdouterr and/or the command).
- abortOnError – If true abort the test on any error outcome (defaults to the defaultAbortOnError project setting)
- expectedExitStatus – The condition string used to determine whether the exit status/code returned by the process is correct. The default is ‘==0’, as an exit code of zero usually indicates success, but if you are expecting a non-zero exit status (for example because you are testing correct handling of a failure condition) this could be set to ‘!=0’ or a specific value such as ‘==5’.
- ignoreExitStatus – If False, a BLOCKED outcome is added if the process terminates with an exit code that doesn’t match expectedExitStatus (or if the command cannot be run at all). This can be set to True in cases where you do not care whether the command succeeds or fails, or wish to handle the exit status separately with more complicated logic. The default value of ignoreExitStatus=None means the value will be taken from the project property defaultIgnoreExitStatus, which can be configured in the project XML (the recommended default property value is defaultIgnoreExitStatus=False), or is set to True for compatibility with older PySys releases if no project property is set.
- quiet – If True, this method will not do any INFO or WARN level logging (only DEBUG level), unless a failure outcome is appended. This parameter can be useful to avoid filling up the log where it is necessary to repeatedly execute a command check for completion of some operation until it succeeds; in such cases you should usually set ignoreExitStatus=True as well since both success and failure exit statuses are valid.
Returns: The process handle of the process (
ProcessWrapper
).Return type: ProcessWrapper
-
startPython
(arguments, disableCoverage=False, **kwargs)[source]¶ Start a Python process with the specified arguments.
Uses the same Python process the tests are running under.
If PySys was run with the arguments -X pythonCoverage=true then startPython will add the necessary arguments to enable generation of code coverage. Note that this requried the coverage.py library to be installed. If a project property called pythonCoverageArgs exists then its value will be added as (space-delimited) arguments to the coverage tool.
Parameters: - arguments – The arguments to pass to the Python executable. Typically the first one be either the name of a Python script to execute, or ‘-m’ followed by a module name.
- kwargs – See
startProcess
for detail on available arguments. - disableCoverage – Disables code coverage for this specific process. Coverage can also be disabled by setting self.disableCoverage==True on this test instance.
Returns: The process handle of the process (
ProcessWrapper
).Return type: ProcessWrapper
-
stopProcess
(process, abortOnError=None)[source]¶ Send a soft or hard kill to a running process to stop its execution.
This method uses the
pysys.process.helper
module to stop a running process. Should the request to stop the running process fail, aBLOCKED
outcome will be added to the outcome list. Failures will result in an exception unless the project property defaultAbortOnError=False.Parameters: - process – The process handle returned from the
startProcess
method - abortOnError – If true abort the test on any error outcome (defaults to the defaultAbortOnError project setting)
- process – The process handle returned from the
-
waitForFile
(file, filedir=None, timeout=30, abortOnError=None)[source]¶ Wait for a file to exist on disk.
This method blocks until a file is created on disk. This is useful for test timing where a component under test creates a file (e.g. for logging) indicating it has performed all initialisation actions and is ready for the test execution steps. If a file is not created on disk within the specified timeout interval, the method returns to the caller.
Parameters: - file – The basename of the file used to wait to be created
- filedir – The dirname of the file (defaults to the testcase output subdirectory)
- timeout – The timeout in seconds to wait for the file to be created
- abortOnError – If true abort the test on any error outcome (defaults to the defaultAbortOnError project setting)
-
waitForSignal
(file, filedir=None, expr='', condition='>=1', timeout=60, poll=0.25, ignores=[], process=None, errorExpr=[], abortOnError=None, encoding=None)[source]¶ Wait for a particular regular expression to be seen on a set number of lines in a text file.
This method blocks until a particular regular expression is seen in a text file on a set number of lines. The number of lines which should match the regular expression is given by the
condition
argument in textual form i.e. for a match on more than 2 lines use condition =”>2”. If the regular expression is not seen in the file matching the supplied condition within the specified timeout interval, the method returns to the caller.Example:
self.waitForSignal('myprocess.log', expr='INFO .\*Started successfully', process=myprocess, errorExpr=[' ERROR ', ' FATAL '], encoding='utf-8')
Parameters: - file – The absolute or relative name of the file used to wait for the signal
- filedir – The dirname of the file (defaults to the testcase output subdirectory)
- expr – The regular expression to search for in the text file
- condition – The condition to be met for the number of lines matching the regular expression
- timeout – The timeout in seconds to wait for the regular expression and to check against the condition
- poll – The time in seconds to poll the file looking for the regular expression and to check against the condition
- ignores – A list of regular expressions used to denote lines in the files which should be ignored when matching both expr and errorExpr.
- process – If a handle to the process object producing output is specified, the wait will abort if the process dies before the expected signal appears.
- errorExpr – Optional list of regular expressions, which if found in the file will cause waiting for the main expression to be aborted with an error outcome. This is useful to avoid waiting a long time for the expected expression when an ERROR is logged that means it will never happen, and also provides much clearer test failure messages in this case.
- abortOnError – If true abort the test on any error outcome (defaults to the defaultAbortOnError project setting)
- encoding – The encoding to use to open the file. The default value is None which indicates that the decision will be delegated to the
getDefaultFileEncoding()
method.
-
waitForSocket
(port, host='localhost', timeout=60, abortOnError=None, process=None)[source]¶ Wait until it is possible to establish a socket connection to a server running on the specified port.
This method blocks until connection to a particular host:port pair can be established. This is useful for test timing where a component under test creates a socket for client server interaction - calling of this method ensures that on return of the method call the server process is running and a client is able to create connections to it. If a connection cannot be made within the specified timeout interval, the method returns to the caller, or aborts the test if abortOnError=True.
Parameters: - port – The port value in the socket host:port pair
- host – The host value in the socket host:port pair
- timeout – The timeout in seconds to wait for connection to the socket
- abortOnError – If true abort the test on any error outcome (defaults to the defaultAbortOnError project setting)
- process – If a handle to a process is specified, the wait will abort if the process dies before the socket becomes available.
-
waitProcess
(process, timeout, abortOnError=None)[source]¶ Wait for a background process to terminate, return on termination or expiry of the timeout.
Timeouts will result in an exception unless the project property defaultAbortOnError=False.
This method does not check the exit code, but you can manually check the value of process.exitStatus if you wish to check it succeeded.
Parameters: - process – The process handle returned from the
startProcess
method - timeout – The timeout value in seconds to wait before returning
- abortOnError – If true abort the test on any error outcome (defaults to the defaultAbortOnError project setting)
- process – The process handle returned from the
-
writeProcess
(process, data, addNewLine=True)[source]¶ Write binary data to the stdin of a process.
This method uses the
pysys.process.helper
module to write data to the stdin of a process. This wrapper around the write method of the process helper only adds checking of the process running status prior to the write being performed, and logging to the testcase run log to detail the write.Parameters: - process – The process handle returned from the
startProcess()
method - data – The data to write to the process stdin. As only binary data can be written to a process stdin, if a character string rather than a byte object is passed as the data, it will be automatically converted to a bytes object using the encoding given by locale.getpreferredencoding().
- addNewLine – True if a new line character is to be added to the end of the data string
- process – The process handle returned from the
-
write_text
(file, text, encoding=None)[source]¶ Writes the specified text to a file in the output directory.
Parameters: - file – The path of the file to write, either an absolute path or relative to the self.output directory.
- text – The string to write to the file, with n for newlines (do not use os.linesep as the file will be opened in text mode so platform line separators will be added automatically). On Python 3 this must be a character string. On Python 2 this can be a character or byte string containing ASCII characters. If non-ASCII characters are used, it must be a unicode string if there is an encoding specified for this file/type, or else a byte string.
- encoding – The encoding to use to open the file. The default value is None which indicates that the decision will be delegated to the
getDefaultFileEncoding()
method.
-
pysys.process.user.
STDOUTERR_TUPLE
¶ alias of
pysys.process.user.stdouterr