2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2018-05-28 16:13:12 -05:00
|
|
|
:codeauthor: Pedro Algarvio (pedro@algarvio.me)
|
2017-06-05 17:07:58 -06:00
|
|
|
:copyright: Copyright 2014 by the SaltStack Team, see AUTHORS for more details.
|
2017-02-27 13:58:07 +00:00
|
|
|
:license: Apache 2.0, see LICENSE for more details.
|
|
|
|
|
|
|
|
|
|
|
|
tests.support.xmlunit
|
|
|
|
~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
XML Unit Tests
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2024-02-27 10:24:22 +00:00
|
|
|
|
Proxy the `.fileno()` method.
This avoids tracebacks when daemonizing code which is running under the XMLRunner:
```
23:22:46 Traceback (most recent call last):
23:22:47 File "/testing/salt/utils/process.py", line 644, in _run
23:22:47 return self._original_run()
23:22:47 File "/usr/lib64/python3.4/multiprocessing/process.py", line 93, in run
23:22:47 self._target(*self._args, **self._kwargs)
23:22:47 File "/testing/salt/client/mixins.py", line 464, in _proc_function
23:22:47 salt.utils.daemonize()
23:22:47 File "/testing/salt/utils/__init__.py", line 502, in daemonize
23:22:47 os.dup2(dev_null.fileno(), sys.stdout.fileno())
23:22:47 io.UnsupportedOperation: fileno
23:22:47 Process SignalHandlingMultiprocessingProcess-2:
23:22:47 Traceback (most recent call last):
23:22:47 File "/usr/lib64/python3.4/multiprocessing/process.py", line 254, in _bootstrap
23:22:47 self.run()
23:22:47 File "/testing/salt/utils/process.py", line 644, in _run
23:22:47 return self._original_run()
23:22:47 File "/usr/lib64/python3.4/multiprocessing/process.py", line 93, in run
23:22:47 self._target(*self._args, **self._kwargs)
23:22:47 File "/testing/salt/client/mixins.py", line 464, in _proc_function
23:22:47 salt.utils.daemonize()
23:22:47 File "/testing/salt/utils/__init__.py", line 502, in daemonize
23:22:47 os.dup2(dev_null.fileno(), sys.stdout.fileno())
23:22:47 io.UnsupportedOperation: fileno
```
2017-03-09 14:50:27 +00:00
|
|
|
# pylint: disable=wrong-import-order,wrong-import-position
|
2017-02-27 13:58:07 +00:00
|
|
|
|
2020-04-02 20:10:20 -05:00
|
|
|
|
Proxy the `.fileno()` method.
This avoids tracebacks when daemonizing code which is running under the XMLRunner:
```
23:22:46 Traceback (most recent call last):
23:22:47 File "/testing/salt/utils/process.py", line 644, in _run
23:22:47 return self._original_run()
23:22:47 File "/usr/lib64/python3.4/multiprocessing/process.py", line 93, in run
23:22:47 self._target(*self._args, **self._kwargs)
23:22:47 File "/testing/salt/client/mixins.py", line 464, in _proc_function
23:22:47 salt.utils.daemonize()
23:22:47 File "/testing/salt/utils/__init__.py", line 502, in daemonize
23:22:47 os.dup2(dev_null.fileno(), sys.stdout.fileno())
23:22:47 io.UnsupportedOperation: fileno
23:22:47 Process SignalHandlingMultiprocessingProcess-2:
23:22:47 Traceback (most recent call last):
23:22:47 File "/usr/lib64/python3.4/multiprocessing/process.py", line 254, in _bootstrap
23:22:47 self.run()
23:22:47 File "/testing/salt/utils/process.py", line 644, in _run
23:22:47 return self._original_run()
23:22:47 File "/usr/lib64/python3.4/multiprocessing/process.py", line 93, in run
23:22:47 self._target(*self._args, **self._kwargs)
23:22:47 File "/testing/salt/client/mixins.py", line 464, in _proc_function
23:22:47 salt.utils.daemonize()
23:22:47 File "/testing/salt/utils/__init__.py", line 502, in daemonize
23:22:47 os.dup2(dev_null.fileno(), sys.stdout.fileno())
23:22:47 io.UnsupportedOperation: fileno
```
2017-03-09 14:50:27 +00:00
|
|
|
import io
|
2017-02-27 13:58:07 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
import xmlrunner.result
|
2022-07-20 10:42:30 +01:00
|
|
|
import xmlrunner.runner
|
2020-04-02 20:10:20 -05:00
|
|
|
|
2017-02-27 13:58:07 +00:00
|
|
|
HAS_XMLRUNNER = True
|
|
|
|
|
2019-11-26 18:19:05 +00:00
|
|
|
class _DuplicateWriter(io.TextIOBase):
|
|
|
|
"""
|
|
|
|
Duplicate output from the first handle to the second handle
|
|
|
|
The second handle is expected to be a StringIO and not to block.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, first, second):
|
2021-04-01 16:29:01 -07:00
|
|
|
super().__init__()
|
2019-11-26 18:19:05 +00:00
|
|
|
self._first = first
|
|
|
|
self._second = second
|
|
|
|
|
|
|
|
def flush(self):
|
|
|
|
self._first.flush()
|
|
|
|
self._second.flush()
|
|
|
|
|
|
|
|
def writable(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def writelines(self, lines):
|
|
|
|
self._first.writelines(lines)
|
|
|
|
self._second.writelines(lines)
|
|
|
|
|
|
|
|
def write(self, b):
|
|
|
|
if isinstance(self._first, io.TextIOBase):
|
|
|
|
wrote = self._first.write(b)
|
2017-02-27 13:58:07 +00:00
|
|
|
|
2019-11-26 18:19:05 +00:00
|
|
|
if wrote is not None:
|
|
|
|
# expected to always succeed to write
|
|
|
|
self._second.write(b[:wrote])
|
2017-02-27 13:58:07 +00:00
|
|
|
|
2019-11-26 18:19:05 +00:00
|
|
|
return wrote
|
|
|
|
else:
|
|
|
|
# file-like object in Python2
|
|
|
|
# It doesn't return wrote bytes.
|
|
|
|
self._first.write(b)
|
|
|
|
self._second.write(b)
|
|
|
|
return len(b)
|
2017-02-27 13:58:07 +00:00
|
|
|
|
Proxy the `.fileno()` method.
This avoids tracebacks when daemonizing code which is running under the XMLRunner:
```
23:22:46 Traceback (most recent call last):
23:22:47 File "/testing/salt/utils/process.py", line 644, in _run
23:22:47 return self._original_run()
23:22:47 File "/usr/lib64/python3.4/multiprocessing/process.py", line 93, in run
23:22:47 self._target(*self._args, **self._kwargs)
23:22:47 File "/testing/salt/client/mixins.py", line 464, in _proc_function
23:22:47 salt.utils.daemonize()
23:22:47 File "/testing/salt/utils/__init__.py", line 502, in daemonize
23:22:47 os.dup2(dev_null.fileno(), sys.stdout.fileno())
23:22:47 io.UnsupportedOperation: fileno
23:22:47 Process SignalHandlingMultiprocessingProcess-2:
23:22:47 Traceback (most recent call last):
23:22:47 File "/usr/lib64/python3.4/multiprocessing/process.py", line 254, in _bootstrap
23:22:47 self.run()
23:22:47 File "/testing/salt/utils/process.py", line 644, in _run
23:22:47 return self._original_run()
23:22:47 File "/usr/lib64/python3.4/multiprocessing/process.py", line 93, in run
23:22:47 self._target(*self._args, **self._kwargs)
23:22:47 File "/testing/salt/client/mixins.py", line 464, in _proc_function
23:22:47 salt.utils.daemonize()
23:22:47 File "/testing/salt/utils/__init__.py", line 502, in daemonize
23:22:47 os.dup2(dev_null.fileno(), sys.stdout.fileno())
23:22:47 io.UnsupportedOperation: fileno
```
2017-03-09 14:50:27 +00:00
|
|
|
def fileno(self):
|
2019-11-26 18:19:05 +00:00
|
|
|
return self._first.fileno()
|
Proxy the `.fileno()` method.
This avoids tracebacks when daemonizing code which is running under the XMLRunner:
```
23:22:46 Traceback (most recent call last):
23:22:47 File "/testing/salt/utils/process.py", line 644, in _run
23:22:47 return self._original_run()
23:22:47 File "/usr/lib64/python3.4/multiprocessing/process.py", line 93, in run
23:22:47 self._target(*self._args, **self._kwargs)
23:22:47 File "/testing/salt/client/mixins.py", line 464, in _proc_function
23:22:47 salt.utils.daemonize()
23:22:47 File "/testing/salt/utils/__init__.py", line 502, in daemonize
23:22:47 os.dup2(dev_null.fileno(), sys.stdout.fileno())
23:22:47 io.UnsupportedOperation: fileno
23:22:47 Process SignalHandlingMultiprocessingProcess-2:
23:22:47 Traceback (most recent call last):
23:22:47 File "/usr/lib64/python3.4/multiprocessing/process.py", line 254, in _bootstrap
23:22:47 self.run()
23:22:47 File "/testing/salt/utils/process.py", line 644, in _run
23:22:47 return self._original_run()
23:22:47 File "/usr/lib64/python3.4/multiprocessing/process.py", line 93, in run
23:22:47 self._target(*self._args, **self._kwargs)
23:22:47 File "/testing/salt/client/mixins.py", line 464, in _proc_function
23:22:47 salt.utils.daemonize()
23:22:47 File "/testing/salt/utils/__init__.py", line 502, in daemonize
23:22:47 os.dup2(dev_null.fileno(), sys.stdout.fileno())
23:22:47 io.UnsupportedOperation: fileno
```
2017-03-09 14:50:27 +00:00
|
|
|
|
2019-11-26 18:19:05 +00:00
|
|
|
xmlrunner.result._DuplicateWriter = _DuplicateWriter
|
2017-02-27 13:58:07 +00:00
|
|
|
|
|
|
|
class _XMLTestResult(xmlrunner.result._XMLTestResult):
|
|
|
|
def startTest(self, test):
|
2019-11-26 18:19:05 +00:00
|
|
|
log.debug(">>>>> START >>>>> %s", test.id())
|
2017-02-27 13:58:07 +00:00
|
|
|
# xmlrunner classes are NOT new-style classes
|
|
|
|
xmlrunner.result._XMLTestResult.startTest(self, test)
|
|
|
|
|
|
|
|
def stopTest(self, test):
|
2019-11-26 18:19:05 +00:00
|
|
|
log.debug("<<<<< END <<<<<<< %s", test.id())
|
2017-02-27 13:58:07 +00:00
|
|
|
# xmlrunner classes are NOT new-style classes
|
|
|
|
return xmlrunner.result._XMLTestResult.stopTest(self, test)
|
|
|
|
|
|
|
|
class XMLTestRunner(xmlrunner.runner.XMLTestRunner):
|
|
|
|
def _make_result(self):
|
|
|
|
return _XMLTestResult(
|
|
|
|
self.stream, self.descriptions, self.verbosity, self.elapsed_times
|
|
|
|
)
|
|
|
|
|
|
|
|
def run(self, test):
|
|
|
|
result = xmlrunner.runner.XMLTestRunner.run(self, test)
|
|
|
|
self.stream.writeln("Finished generating XML reports")
|
|
|
|
return result
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
HAS_XMLRUNNER = False
|
|
|
|
|
2021-04-01 16:29:01 -07:00
|
|
|
class XMLTestRunner:
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|
2017-02-27 13:58:07 +00:00
|
|
|
This is a dumb class just so we don't break projects at import time
|
2020-04-02 20:10:20 -05:00
|
|
|
"""
|