2013-12-11 13:02:08 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
2018-05-28 21:13:12 +00:00
|
|
|
:codeauthor: Pedro Algarvio (pedro@algarvio.me)
|
2013-12-11 13:02:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
tests.unit.utils.vt_test
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
VirtualTerminal tests
|
|
|
|
'''
|
|
|
|
|
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-07-25 01:47:15 +00:00
|
|
|
# Import Python libs
|
2018-01-14 01:07:59 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2014-07-21 23:11:20 +00:00
|
|
|
import os
|
2014-06-11 03:10:16 +00:00
|
|
|
import sys
|
2013-12-11 13:02:08 +00:00
|
|
|
import random
|
2014-07-22 13:38:09 +00:00
|
|
|
import subprocess
|
2014-12-29 17:26:48 +00:00
|
|
|
import time
|
2013-12-11 13:02:08 +00:00
|
|
|
|
|
|
|
# Import Salt Testing libs
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import TestCase, skipIf
|
2013-12-11 13:02:08 +00:00
|
|
|
|
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-07-25 01:47:15 +00:00
|
|
|
# Import Salt libs
|
2017-07-18 16:31:01 +00:00
|
|
|
import salt.utils.files
|
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-07-25 01:47:15 +00:00
|
|
|
import salt.utils.platform
|
2016-08-11 16:45:24 +00:00
|
|
|
import salt.utils.vt
|
2013-12-11 13:02:08 +00:00
|
|
|
|
2014-11-22 11:34:34 +00:00
|
|
|
# Import 3rd-party libs
|
|
|
|
from salt.ext.six.moves import range # pylint: disable=import-error,redefined-builtin
|
|
|
|
|
2013-12-11 13:02:08 +00:00
|
|
|
|
|
|
|
class VTTestCase(TestCase):
|
|
|
|
|
2014-09-25 21:38:01 +00:00
|
|
|
@skipIf(True, 'Disabled until we can figure out why this fails when whole test suite runs.')
|
2013-12-11 13:02:08 +00:00
|
|
|
def test_vt_size(self):
|
|
|
|
'''Confirm that the terminal size is being set'''
|
2014-06-11 03:10:16 +00:00
|
|
|
if not sys.stdin.isatty():
|
|
|
|
self.skipTest('Not attached to a TTY. The test would fail.')
|
2013-12-11 13:02:08 +00:00
|
|
|
cols = random.choice(range(80, 250))
|
2016-08-11 16:45:24 +00:00
|
|
|
terminal = salt.utils.vt.Terminal(
|
2014-06-11 03:10:16 +00:00
|
|
|
'echo "Foo!"',
|
2013-12-11 13:02:08 +00:00
|
|
|
shell=True,
|
2014-06-11 03:10:16 +00:00
|
|
|
cols=cols,
|
|
|
|
rows=24,
|
|
|
|
stream_stdout=False,
|
|
|
|
stream_stderr=False
|
2013-12-11 13:02:08 +00:00
|
|
|
)
|
2013-12-11 18:59:37 +00:00
|
|
|
# First the assertion
|
2013-12-11 13:02:08 +00:00
|
|
|
self.assertEqual(
|
|
|
|
terminal.getwinsize(), (24, cols)
|
|
|
|
)
|
2013-12-11 18:59:37 +00:00
|
|
|
# Then wait for the terminal child to exit
|
|
|
|
terminal.wait()
|
2014-06-11 03:10:16 +00:00
|
|
|
terminal.close()
|
|
|
|
|
2014-09-14 22:54:38 +00:00
|
|
|
@skipIf(True, 'Disabled until we can find out why this kills the tests suite with an exit code of 134')
|
2014-06-11 03:10:16 +00:00
|
|
|
def test_issue_10404_ptys_not_released(self):
|
|
|
|
n_executions = 15
|
2014-07-23 18:43:18 +00:00
|
|
|
|
|
|
|
def current_pty_count():
|
|
|
|
# Get current number of PTY's
|
|
|
|
try:
|
|
|
|
if os.path.exists('/proc/sys/kernel/pty/nr'):
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen('/proc/sys/kernel/pty/nr') as fh_:
|
2014-07-23 18:43:18 +00:00
|
|
|
return int(fh_.read().strip())
|
|
|
|
|
2014-07-22 13:38:09 +00:00
|
|
|
proc = subprocess.Popen(
|
|
|
|
'sysctl -a 2> /dev/null | grep pty.nr | awk \'{print $3}\'',
|
|
|
|
shell=True,
|
|
|
|
stdout=subprocess.PIPE
|
|
|
|
)
|
|
|
|
stdout, _ = proc.communicate()
|
2014-07-23 18:43:18 +00:00
|
|
|
return int(stdout.strip())
|
|
|
|
except (ValueError, OSError, IOError):
|
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-07-25 01:47:15 +00:00
|
|
|
if salt.utils.platform.is_darwin():
|
2014-07-23 21:07:27 +00:00
|
|
|
# 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'
|
|
|
|
)
|
2014-07-23 18:43:18 +00:00
|
|
|
self.fail('Unable to find out how many PTY\'s are open')
|
|
|
|
|
|
|
|
nr_ptys = current_pty_count()
|
2014-06-11 03:10:16 +00:00
|
|
|
|
|
|
|
# Using context manager's
|
|
|
|
for idx in range(0, nr_ptys + n_executions):
|
|
|
|
try:
|
2016-08-11 16:45:24 +00:00
|
|
|
with salt.utils.vt.Terminal('echo "Run {0}"'.format(idx),
|
2014-06-11 03:10:16 +00:00
|
|
|
shell=True,
|
|
|
|
stream_stdout=False,
|
|
|
|
stream_stderr=False) as terminal:
|
|
|
|
terminal.wait()
|
|
|
|
try:
|
2014-07-23 18:43:18 +00:00
|
|
|
if current_pty_count() > (nr_ptys + (n_executions/2)):
|
2014-06-11 03:10:16 +00:00
|
|
|
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:
|
2016-08-11 16:45:24 +00:00
|
|
|
terminal = salt.utils.vt.Terminal('echo "Run {0}"'.format(idx),
|
2014-06-11 03:10:16 +00:00
|
|
|
shell=True,
|
|
|
|
stream_stdout=False,
|
|
|
|
stream_stderr=False)
|
|
|
|
terminal.wait()
|
|
|
|
try:
|
2014-07-23 18:43:18 +00:00
|
|
|
if current_pty_count() > (nr_ptys + (n_executions/2)):
|
2014-06-11 03:10:16 +00:00
|
|
|
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
|
|
|
|
|
2015-06-10 20:04:14 +00:00
|
|
|
@skipIf(True, 'Disabled until we can figure out how to make this more reliable.')
|
2014-09-30 19:57:14 +00:00
|
|
|
def test_isalive_while_theres_data_to_read(self):
|
2014-10-12 02:26:37 +00:00
|
|
|
expected_data = 'Alive!\n'
|
2016-08-11 16:45:24 +00:00
|
|
|
term = salt.utils.vt.Terminal('echo "Alive!"',
|
|
|
|
shell=True,
|
|
|
|
stream_stdout=False,
|
|
|
|
stream_stderr=False)
|
2014-10-12 02:26:37 +00:00
|
|
|
buffer_o = buffer_e = ''
|
2014-09-30 19:57:14 +00:00
|
|
|
try:
|
|
|
|
while term.has_unread_data:
|
|
|
|
stdout, stderr = term.recv()
|
2014-10-12 02:26:37 +00:00
|
|
|
if stdout:
|
|
|
|
buffer_o += stdout
|
|
|
|
if stderr:
|
|
|
|
buffer_e += stderr
|
2014-10-13 16:19:42 +00:00
|
|
|
# While there's data to be read, the process is alive
|
|
|
|
if stdout is None and stderr is None:
|
2014-09-30 19:57:14 +00:00
|
|
|
self.assertFalse(term.isalive())
|
|
|
|
|
|
|
|
# term should be dead now
|
2014-10-12 02:26:37 +00:00
|
|
|
self.assertEqual(buffer_o, expected_data)
|
|
|
|
self.assertFalse(term.isalive())
|
|
|
|
|
2014-09-30 19:57:14 +00:00
|
|
|
stdout, stderr = term.recv()
|
|
|
|
self.assertFalse(term.isalive())
|
|
|
|
self.assertIsNone(stderr)
|
|
|
|
self.assertIsNone(stdout)
|
|
|
|
finally:
|
|
|
|
term.close(terminate=True, kill=True)
|
|
|
|
|
2014-10-12 02:26:37 +00:00
|
|
|
expected_data = 'Alive!\n'
|
2016-08-11 16:45:24 +00:00
|
|
|
term = salt.utils.vt.Terminal('echo "Alive!" 1>&2',
|
|
|
|
shell=True,
|
|
|
|
stream_stdout=False,
|
|
|
|
stream_stderr=False)
|
2014-10-12 02:26:37 +00:00
|
|
|
buffer_o = buffer_e = ''
|
2014-09-30 19:57:14 +00:00
|
|
|
try:
|
|
|
|
while term.has_unread_data:
|
|
|
|
stdout, stderr = term.recv()
|
2014-10-12 02:26:37 +00:00
|
|
|
if stdout:
|
|
|
|
buffer_o += stdout
|
|
|
|
if stderr:
|
|
|
|
buffer_e += stderr
|
2014-10-13 16:19:42 +00:00
|
|
|
# While there's data to be read, the process is alive
|
|
|
|
if stdout is None and stderr is None:
|
2014-09-30 19:57:14 +00:00
|
|
|
self.assertFalse(term.isalive())
|
|
|
|
|
|
|
|
# term should be dead now
|
2014-10-12 02:26:37 +00:00
|
|
|
self.assertEqual(buffer_e, expected_data)
|
|
|
|
self.assertFalse(term.isalive())
|
|
|
|
|
2014-09-30 19:57:14 +00:00
|
|
|
stdout, stderr = term.recv()
|
|
|
|
self.assertFalse(term.isalive())
|
|
|
|
self.assertIsNone(stderr)
|
|
|
|
self.assertIsNone(stdout)
|
|
|
|
finally:
|
|
|
|
term.close(terminate=True, kill=True)
|
|
|
|
|
2014-10-12 02:26:37 +00:00
|
|
|
expected_data = 'Alive!\nAlive!\n'
|
2016-08-11 16:45:24 +00:00
|
|
|
term = salt.utils.vt.Terminal('echo "Alive!"; sleep 5; echo "Alive!"',
|
|
|
|
shell=True,
|
|
|
|
stream_stdout=False,
|
|
|
|
stream_stderr=False)
|
2014-10-12 02:26:37 +00:00
|
|
|
buffer_o = buffer_e = ''
|
|
|
|
try:
|
|
|
|
while term.has_unread_data:
|
|
|
|
stdout, stderr = term.recv()
|
|
|
|
if stdout:
|
|
|
|
buffer_o += stdout
|
|
|
|
if stderr:
|
|
|
|
buffer_e += stderr
|
2014-10-13 16:19:42 +00:00
|
|
|
# While there's data to be read, the process is alive
|
|
|
|
if stdout is None and stderr is None:
|
2014-10-12 02:26:37 +00:00
|
|
|
self.assertFalse(term.isalive())
|
|
|
|
|
|
|
|
if buffer_o != expected_data:
|
|
|
|
self.assertTrue(term.isalive())
|
2014-12-29 17:26:48 +00:00
|
|
|
# Don't spin
|
2014-12-29 18:27:05 +00:00
|
|
|
time.sleep(0.1)
|
2014-09-30 19:57:14 +00:00
|
|
|
|
2014-10-12 02:26:37 +00:00
|
|
|
# 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)
|