salt/tests/unit/utils/test_vt.py
Erik Johnson 3184168365 Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.

A summary of the changes in this PR is as follows:

* Moves the following functions from `salt.utils` to new locations
  (including a deprecation warning if invoked from `salt.utils`):
  `to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
  `dequote`, `is_hex`, `is_bin_str`, `rand_string`,
  `contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
  `which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
  `is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
  `is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
  `is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
  `salt/utils/__init__.py` for better organization, so we can keep the
  deprecated ones separate from the ones yet to be deprecated as we
  continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
  unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
  import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
  accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
  such that it returns a string matching the passed size. Previously
  this function would get `size` bytes from `os.urandom()`,
  base64-encode it, and return the result, which would in most cases not
  be equal to the passed size.
2017-08-08 13:33:43 -05:00

215 lines
7.8 KiB
Python

# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Pedro Algarvio (pedro@algarvio.me)`
tests.unit.utils.vt_test
~~~~~~~~~~~~~~~~~~~~~~~~
VirtualTerminal tests
'''
# Import Python libs
from __future__ import absolute_import
import os
import sys
import random
import subprocess
import time
# Import Salt Testing libs
from tests.support.unit import TestCase, skipIf
# Import Salt libs
import salt.utils.files
import salt.utils.platform
import salt.utils.vt
# Import 3rd-party libs
from salt.ext.six.moves import range # pylint: disable=import-error,redefined-builtin
class VTTestCase(TestCase):
@skipIf(True, 'Disabled until we can figure out why this fails when whole test suite runs.')
def test_vt_size(self):
'''Confirm that the terminal size is being set'''
if not sys.stdin.isatty():
self.skipTest('Not attached to a TTY. The test would fail.')
cols = random.choice(range(80, 250))
terminal = salt.utils.vt.Terminal(
'echo "Foo!"',
shell=True,
cols=cols,
rows=24,
stream_stdout=False,
stream_stderr=False
)
# First the assertion
self.assertEqual(
terminal.getwinsize(), (24, cols)
)
# Then wait for the terminal child to exit
terminal.wait()
terminal.close()
@skipIf(True, 'Disabled until we can find out why this kills the tests suite with an exit code of 134')
def test_issue_10404_ptys_not_released(self):
n_executions = 15
def current_pty_count():
# Get current number of PTY's
try:
if os.path.exists('/proc/sys/kernel/pty/nr'):
with salt.utils.files.fopen('/proc/sys/kernel/pty/nr') as fh_:
return int(fh_.read().strip())
proc = subprocess.Popen(
'sysctl -a 2> /dev/null | grep pty.nr | awk \'{print $3}\'',
shell=True,
stdout=subprocess.PIPE
)
stdout, _ = proc.communicate()
return int(stdout.strip())
except (ValueError, OSError, IOError):
if salt.utils.platform.is_darwin():
# We're unable to findout how many PTY's are open
self.skipTest(
'Unable to find out how many PTY\'s are open on Darwin - '
'Skipping for now'
)
self.fail('Unable to find out how many PTY\'s are open')
nr_ptys = current_pty_count()
# Using context manager's
for idx in range(0, nr_ptys + n_executions):
try:
with salt.utils.vt.Terminal('echo "Run {0}"'.format(idx),
shell=True,
stream_stdout=False,
stream_stderr=False) as terminal:
terminal.wait()
try:
if current_pty_count() > (nr_ptys + (n_executions/2)):
self.fail('VT is not cleaning up PTY\'s')
except (ValueError, OSError, IOError):
self.fail('Unable to find out how many PTY\'s are open')
except Exception as exc:
if 'out of pty devices' in exc:
# We're not cleaning up
raise
# We're pushing the system resources, let's keep going
continue
# Not using context manager's
for idx in range(0, nr_ptys + n_executions):
try:
terminal = salt.utils.vt.Terminal('echo "Run {0}"'.format(idx),
shell=True,
stream_stdout=False,
stream_stderr=False)
terminal.wait()
try:
if current_pty_count() > (nr_ptys + (n_executions/2)):
self.fail('VT is not cleaning up PTY\'s')
except (ValueError, OSError, IOError):
self.fail('Unable to find out how many PTY\'s are open')
except Exception as exc:
if 'out of pty devices' in exc:
# We're not cleaning up
raise
# We're pushing the system resources, let's keep going
continue
@skipIf(True, 'Disabled until we can figure out how to make this more reliable.')
def test_isalive_while_theres_data_to_read(self):
expected_data = 'Alive!\n'
term = salt.utils.vt.Terminal('echo "Alive!"',
shell=True,
stream_stdout=False,
stream_stderr=False)
buffer_o = buffer_e = ''
try:
while term.has_unread_data:
stdout, stderr = term.recv()
if stdout:
buffer_o += stdout
if stderr:
buffer_e += stderr
# While there's data to be read, the process is alive
if stdout is None and stderr is None:
self.assertFalse(term.isalive())
# term should be dead now
self.assertEqual(buffer_o, expected_data)
self.assertFalse(term.isalive())
stdout, stderr = term.recv()
self.assertFalse(term.isalive())
self.assertIsNone(stderr)
self.assertIsNone(stdout)
finally:
term.close(terminate=True, kill=True)
expected_data = 'Alive!\n'
term = salt.utils.vt.Terminal('echo "Alive!" 1>&2',
shell=True,
stream_stdout=False,
stream_stderr=False)
buffer_o = buffer_e = ''
try:
while term.has_unread_data:
stdout, stderr = term.recv()
if stdout:
buffer_o += stdout
if stderr:
buffer_e += stderr
# While there's data to be read, the process is alive
if stdout is None and stderr is None:
self.assertFalse(term.isalive())
# term should be dead now
self.assertEqual(buffer_e, expected_data)
self.assertFalse(term.isalive())
stdout, stderr = term.recv()
self.assertFalse(term.isalive())
self.assertIsNone(stderr)
self.assertIsNone(stdout)
finally:
term.close(terminate=True, kill=True)
expected_data = 'Alive!\nAlive!\n'
term = salt.utils.vt.Terminal('echo "Alive!"; sleep 5; echo "Alive!"',
shell=True,
stream_stdout=False,
stream_stderr=False)
buffer_o = buffer_e = ''
try:
while term.has_unread_data:
stdout, stderr = term.recv()
if stdout:
buffer_o += stdout
if stderr:
buffer_e += stderr
# While there's data to be read, the process is alive
if stdout is None and stderr is None:
self.assertFalse(term.isalive())
if buffer_o != expected_data:
self.assertTrue(term.isalive())
# Don't spin
time.sleep(0.1)
# term should be dead now
self.assertEqual(buffer_o, expected_data)
self.assertFalse(term.isalive())
stdout, stderr = term.recv()
self.assertFalse(term.isalive())
self.assertIsNone(stderr)
self.assertIsNone(stdout)
finally:
term.close(terminate=True, kill=True)