mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 17:09:03 +00:00
3273bbdab7
Conflicts: - doc/ref/configuration/master.rst - doc/ref/modules/all/index.rst - doc/topics/grains/index.rst - doc/topics/releases/2016.3.4.rst - doc/topics/spm/spm_formula.rst - doc/topics/tutorials/cron.rst - doc/topics/tutorials/index.rst - doc/topics/tutorials/stormpath.rst - salt/engines/slack.py - salt/log/handlers/fluent_mod.py - salt/modules/cyg.py - salt/modules/junos.py - salt/modules/namecheap_dns.py - salt/modules/namecheap_domains.py - salt/modules/namecheap_ns.py - salt/modules/namecheap_ssl.py - salt/modules/namecheap_users.py - salt/modules/reg.py - salt/modules/tomcat.py - salt/modules/vault.py - salt/modules/win_file.py - salt/modules/zpool.py - salt/output/highstate.py - salt/renderers/pass.py - salt/runners/cache.py - salt/states/boto_apigateway.py - salt/states/boto_iam.py - salt/states/boto_route53.py - salt/states/msteams.py - salt/states/reg.py - salt/states/win_iis.py - tests/integration/modules/test_cmdmod.py - tests/integration/states/test_user.py - tests/support/helpers.py - tests/unit/cloud/clouds/test_openstack.py - tests/unit/fileserver/test_gitfs.py - tests/unit/modules/test_junos.py - tests/unit/pillar/test_git.py - tests/unit/states/test_win_path.py - tests/unit/test_pillar.py - tests/unit/utils/test_format_call.py - tests/unit/utils/test_utils.py - tests/unit/utils/test_warnings.py
215 lines
7.9 KiB
Python
215 lines
7.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
'''
|
|
:codeauthor: Pedro Algarvio (pedro@algarvio.me)
|
|
|
|
|
|
tests.unit.utils.vt_test
|
|
~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
VirtualTerminal tests
|
|
'''
|
|
|
|
# Import Python libs
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
|
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)
|